def calculate_absolute_uncertainty( self, *assumptions: List[AppliedPredicate], refine: bool = False, delta_char: str = '\\Delta ') -> 'Expression': """Calculate the absolute uncertainty in the expression (IB way), assuming all args given are independent. :return: the absolute uncertainty of this expression :rtype: Expression >>> Expression([a], c * a).calculate_absolute_uncertainty(sympy.Q.positive(c), refine=True, delta_char='Δ') f(Δa) = c*Δa >>> Expression([a, b, c], a + b - c).calculate_absolute_uncertainty(refine=True, delta_char='Δ') f(Δa, Δb, Δc) = Δa + Δb + Δc """ uncertainty_expr = sympy.Integer(0) # just in case uncertainty_args = [] global_assumptions.add(*assumptions) for var in self.args: d_var = sympy.Symbol(delta_char + sympy.latex(var)) uncertainty_args.append(d_var) uncertainty_expr += sympy.Abs(self.expr.diff(var)) * d_var global_assumptions.add(sympy.Q.positive(var)) if refine: uncertainty_expr = sympy.refine(uncertainty_expr) global_assumptions.clear() return Expression(uncertainty_args, uncertainty_expr)
def test_remove_safe(): global_assumptions.add(Q.integer(x)) with assuming(): assert ask(Q.integer(x)) global_assumptions.remove(Q.integer(x)) assert not ask(Q.integer(x)) assert ask(Q.integer(x)) global_assumptions.clear() # for the benefit of other tests
def test_global(): """Test for global assumptions""" global_assumptions.add(Q.is_true(x > 0)) assert Q.is_true(x > 0) in global_assumptions global_assumptions.remove(Q.is_true(x > 0)) assert not Q.is_true(x > 0) in global_assumptions # same with multiple of assumptions global_assumptions.add(Q.is_true(x > 0), Q.is_true(y > 0)) assert Q.is_true(x > 0) in global_assumptions assert Q.is_true(y > 0) in global_assumptions global_assumptions.clear() assert not Q.is_true(x > 0) in global_assumptions assert not Q.is_true(y > 0) in global_assumptions
def test_global(): """Test for global assumptions""" global_assumptions.add(x > 0) assert (x > 0) in global_assumptions global_assumptions.remove(x > 0) assert not (x > 0) in global_assumptions # same with multiple of assumptions global_assumptions.add(x > 0, y > 0) assert (x > 0) in global_assumptions assert (y > 0) in global_assumptions global_assumptions.clear() assert not (x > 0) in global_assumptions assert not (y > 0) in global_assumptions
def test_sympy(): """ Using position and velocity data from subscribing to detect_target/target_state topic, this function determines: - time of launch - time of intercept - azimuth and elevation angles for the projectile After solving the symbolic system of equations, the projectile is launched. :return: """ # constant variables # velocity magnitude velocity_mag = 20 # projectile pos proj_x = 1 proj_y = 1 proj_z = 0 # gravitational acc g = 9.8 # test variables vx = 15 vy = 10 vz = 5 x = 30 y = 15 z = 9 T = 10 # declare symbols tau, phi, theta = symbols("tau, phi, theta", positive=True, real=True) # create assumptions global_assumptions.add(Q.positive(tau)) global_assumptions.add(Q.real(tau)) print global_assumptions eq1 = symarray("0, 0, g", 3)*(T-tau)**2/2 + symarray("vx, vy, vz", 3)*T \ - velocity_mag*symarray("sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta)", 3)*(T-tau) \ + symarray("x - proj_x, y - proj_y, z - proj_z", 3) system = [eq1] eq_symbols = [tau, phi, theta] try: print "Started solving.." print nonlinsolve(system, eq_symbols) print "Done." except ValueError as ve: print ve except AttributeError as ae: print ae
def rotate_axa(rxn, lmk): # axis-angle (rodrigues) rotation global_assumptions.add(Q.real(rxn)) global_assumptions.add(Q.real(lmk)) v = lmk hsq = (rxn.T * rxn)[0, 0] h = sm.sqrt(hsq) u = rxn / h c = sm.cos(h) s = sm.sin(h) d = sm.ones(1, 3) * u.multiply_elementwise(v) p1 = (v * c) p2 = s * u.cross(v) p3 = (1. - c) * (d[0, 0]) * u s = p1 + p2 + p3 return s
def _abel_sym(): """ Analytical integration of the cell near the singular value in the abel transform The resulting formula is implemented in hedp.lib.integrators.abel_integrate """ from sympy import symbols, simplify, integrate, sqrt from sympy.assumptions.assume import global_assumptions r, y,r0, r1,r2, z,dr, c0, c_r, c_rr,c_z, c_zz, c_rz = symbols('r y r0 r1 r2 z dr c0 c_r c_rr c_z c_zz c_rz', positive=True) f0, f1, f2 = symbols('f0 f1 f2') global_assumptions.add(Q.is_true(r>y)) global_assumptions.add(Q.is_true(r1>y)) global_assumptions.add(Q.is_true(r2>y)) global_assumptions.add(Q.is_true(r2>r1)) P = c0 + (r-y)*c_r #+ (r-r0)**2*c_rr K_d = 1/sqrt(r**2-y**2) res = integrate(P*K_d, (r,y, r1)) sres= simplify(res) print(sres)
def tick(self, tick): unknowns = tick.blackboard.get( 'unknowns') # the current list of unknowns R = tick.blackboard.get('Robot') # get the current assignment u = tick.blackboard.get('curr_unk') one_unk = tick.blackboard.get('eqns_1u') two_unk = tick.blackboard.get('eqns_2u') if (self.BHdebug): print "running: ", self.Name, " , input RHS:" ulen = len(unknowns) print 'tan_id working on', ulen, ' unknowns:' # identify unknowns in T where one equation can be solved by # arctan(y,x) found = False # only if not identified as solvable by tangent yet if (not u.solvable_tan) and (not u.solved): terms = [sp.sin(u.symbol), sp.cos(u.symbol)] sin_eqn = [] cos_eqn = [] for e in (one_unk + two_unk): # spot the ones with common factors as well # fix the eqn, but not changing the original equation - DZ tmp = e.RHS - e.LHS lhs = l_1 - l_1 if (not (tmp).has(u.symbol)): continue # only look at equations having the current unknown in them if (self.BHdebug): print "\n\n tan_id: Looking for unknown: ", u.symbol, " in equation: ", print e print " which has ", count_unknowns( unknowns, e.RHS), " unknown(s) in RHS" print " and ", count_unknowns( unknowns, e.LHS), " unknown(s) in LHS" if (tmp.has(sp.sin(u.symbol)) and tmp.has(sp.cos(u.symbol))): continue # this should be caught by sinANDcos solver if tmp.has(sp.sin(u.symbol)): sin_eqn.append(e) if tmp.has(sp.cos(u.symbol)): cos_eqn.append(e) for es in sin_eqn: estst = (es.RHS - es.LHS).collect( terms) # get all the sin(th)s collected d1 = estst.match(Aw * sp.sin(u.symbol) + Bw) if self.BHdebug: print '---' print "\nsin equ: " print u.eqntosolve print "\nsin(): coefficients are : " print d1[Aw] print '---' for ec in cos_eqn: ectst = (ec.RHS - ec.LHS).collect( terms) # get all the cos(th)s collected d2 = ectst.match(Cw * sp.cos(u.symbol) + Dw) if self.BHdebug: print "\ncos equ: " print u.secondeqn print "\ncos(): coefficients" print d2[Cw] # check some things about potential solvable equations assert (d1 is not None and d2 is not None), 'somethings wrong!' co = d1[Aw] / d2[Cw] # take ratio # it's not solvable if (simplified) coefficient contains unknowns, or other parts have unknowns print 'tan_id: (', u.symbol, ') 0 = Aw*sin(th)+Bw , 0 = Cw*cos(th) + Dw ' print 'Aw: ', d1[Aw], ' Bw: ', d1[Bw] print 'Cw: ', d2[Cw], ' Dw: ', d2[Dw] too_many_unknowns = False if count_unknowns(unknowns, co) > 0 or count_unknowns( unknowns, d1[Bw]) > 0 or count_unknowns( unknowns, d2[Dw]) > 0: too_many_unknowns = True # a good match / solution candidate if not too_many_unknowns: found = True # found both terms for at least one variable u.eqntosolve = kc.kequation(0, estst) u.secondeqn = kc.kequation(0, ectst) u.readytosolve = True # u.eqntosolve and secondeqn are already set up above print 'tan_id: able to solve', u.symbol if count_unknowns( unknowns, co ) > 0: #cancellable unsolved term, add the nonzero assumption global_assumptions.add(sp.Q.nonzero(d2[Cw])) u.solvemethod = "atan2(y,x)" u.solvable_tan = True if (self.BHdebug and u.readytosolve): print '\n tan_id: Identified Solution: ', u.symbol e1tmp = u.eqntosolve.RHS + u.eqntosolve.LHS e2tmp = u.secondeqn.RHS + u.secondeqn.LHS print ' ', u.eqntosolve, ' (', count_unknowns( unknowns, e1tmp), 'unks)' for u in get_unknowns(unknowns, e1tmp): print u.symbol, print '' print ' ', u.secondeqn, ' (', count_unknowns( unknowns, e2tmp), 'unks)' for u in get_unknowns(unknowns, e1tmp): print u.symbol, print '' print '' if found: break if found: break #continue #stop looking at equations for the same unknown tick.blackboard.set('curr_unk', u) tick.blackboard.set('unknowns', unknowns) # the current list of unknowns if u.solvable_tan: return b3.SUCCESS else: return b3.FAILURE