# ----------------------------------------------------------------------------# SymForce - Copyright 2022, Skydio, Inc.# This source code is under the Apache 2.0 license found in the LICENSE file.# ----------------------------------------------------------------------------"""Functions for dealing with logical operations represented by scalars"""importsymforce.internal.symbolicassf
[docs]defis_positive(x:sf.Scalar)->sf.Scalar:""" Returns 1 if x is positive, 0 otherwise """returnsf.Max(sf.sign(x),0)
[docs]defis_negative(x:sf.Scalar)->sf.Scalar:""" Returns 1 if x is negative, 0 otherwise """returnsf.Max(sf.sign(-x),0)
[docs]defis_nonnegative(x:sf.Scalar)->sf.Scalar:""" Returns 1 if x is >= 0, 0 if x is negative """return1-sf.Max(sf.sign(-x),0)
[docs]defis_nonpositive(x:sf.Scalar)->sf.Scalar:""" Returns 1 if x is <= 0, 0 if x is positive """return1-sf.Max(sf.sign(x),0)
[docs]defless_equal(x:sf.Scalar,y:sf.Scalar)->sf.Scalar:""" Returns 1 if x <= y, 0 otherwise """returnis_nonpositive(x-y)
[docs]defgreater_equal(x:sf.Scalar,y:sf.Scalar)->sf.Scalar:""" Returns 1 if x >= y, 0 otherwise """returnis_nonnegative(x-y)
[docs]defless(x:sf.Scalar,y:sf.Scalar)->sf.Scalar:""" Returns 1 if x < y, 0 otherwise """returnis_negative(x-y)
[docs]defgreater(x:sf.Scalar,y:sf.Scalar)->sf.Scalar:""" Returns 1 if x > y, 0 otherwise """returnis_positive(x-y)
[docs]deflogical_and(*args:sf.Scalar,unsafe:bool=False)->sf.Scalar:""" Logical ``and`` of two or more Scalars Input values are treated as true if they are positive, false if they are 0 or negative. The returned value is 1 for true, 0 for false. If unsafe is True, the resulting expression is fewer ops but assumes the inputs are exactly 0 or 1; results for other (finite) inputs will be finite, but are otherwise undefined. """ifunsafe:returnsf.Min(*args)else:returnsf.Max(sum(sf.sign(x)forxinargs),len(args)-1)-(len(args)-1)
[docs]deflogical_or(*args:sf.Scalar,unsafe:bool=False)->sf.Scalar:""" Logical ``or`` of two or more Scalars Input values are treated as true if they are positive, false if they are 0 or negative. The returned value is 1 for true, 0 for false. If unsafe is True, the resulting expression is fewer ops but assumes the inputs are exactly 0 or 1; results for other (finite) inputs will be finite, but are otherwise undefined. """ifunsafe:returnsf.Max(*args)else:returnsf.Max(*[sf.sign(x)forxinargs],0)
[docs]deflogical_not(a:sf.Scalar,unsafe:bool=False)->sf.Scalar:""" Logical ``not`` of a Scalar Input value is treated as true if it is positive, false if it is 0 or negative. The returned value is 1 for true, 0 for false. If unsafe is True, the resulting expression is fewer ops but assumes the inputs are exactly 0 or 1; results for other (finite) inputs will be finite, but are otherwise undefined. """ifunsafe:return1-aelse:return1-sf.Max(sf.sign(a),0)