File size: 554 Bytes
cc646d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
def sgns(*args):
for i in args:
if i < 0:
break
else:
return 1
for i in args:
if i > 0:
break
else:
return -1
return 0
def sq_func_sgn(a, b, c):
r"""
let f(x) = a+bx+cx^2, x \in [0, 1]
:return: f(x)>=0? 1; f(x)<=0? -1; 0
"""
f0 = a
f1 = a + b + c
fm = 0
if c != 0:
x_m = -b / (2 * c)
if 0 <= x_m <= 1:
fm = a + b * x_m + c * x_m**2
return sgns(f0, f1, fm)
def lin_func_sgn(a, b):
return sgns(a, a + b)
|