我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sympy.pi()。
def test_clambdify(): x = Symbol('x') y = Symbol('y') z = Symbol('z') f1 = sqrt(x*y) pf1 = lambdify((x, y), f1, 'math') cf1 = clambdify((x, y), f1) for i in xrange(10): if cf1(i, 10 - i) != pf1(i, 10 - i): raise ValueError("Values should be equal") f2 = (x - y) / z * pi pf2 = lambdify((x, y, z), f2, 'math') cf2 = clambdify((x, y, z), f2) if round(pf2(1, 2, 3), 14) != round(cf2(1, 2, 3), 14): raise ValueError("Values should be equal") # FIXME: slight difference in precision
def __init__(self, n): self.degree = 2 self.dim = n n2 = fr(n, 2) if n % 2 == 0 else fr(n-1, 2) pts = [[ [sqrt(fr(2, n+2)) * cos(2*k*i*pi / (n+1)) for i in range(n+1)], [sqrt(fr(2, n+2)) * sin(2*k*i*pi / (n+1)) for i in range(n+1)], ] for k in range(1, n2+1)] if n % 2 == 1: sqrt3pm = numpy.full(n+1, 1/sqrt(n+2)) sqrt3pm[1::2] *= -1 pts.append(sqrt3pm) pts = numpy.vstack(pts).T data = [(fr(1, n+1), pts)] self.points, self.weights = untangle(data) self.weights *= volume_unit_ball(n) return
def test_pow_E(): assert 2**(y/log(2)) == S.Exp1**y assert 2**(y/log(2)/3) == S.Exp1**(y/3) assert 3**(1/log(-3)) != S.Exp1 assert (3 + 2*I)**(1/(log(-3 - 2*I) + I*pi)) == S.Exp1 assert (4 + 2*I)**(1/(log(-4 - 2*I) + I*pi)) == S.Exp1 assert (3 + 2*I)**(1/(log(-3 - 2*I, 3)/2 + I*pi/log(3)/2)) == 9 assert (3 + 2*I)**(1/(log(3 + 2*I, 3)/2)) == 9 # every time tests are run they will affirm with a different random # value that this identity holds while 1: b = x._random() r, i = b.as_real_imag() if i: break assert test_numerically(b**(1/(log(-b) + sign(i)*I*pi).n()), S.Exp1)
def equation(self, type='cosine'): """ Returns equation of the wave. Examples ======== >>> from sympy import symbols >>> from sympy.physics.optics import TWave >>> A, phi, f = symbols('A, phi, f') >>> w = TWave(A, f, phi) >>> w.equation('cosine') A*cos(2*pi*f*t + phi) """ if not isinstance(type, str): raise TypeError("type can only be a string.") if type == 'cosine': return self._amplitude*cos(self.angular_velocity*Symbol('t') + self._phase)
def test_twave(): A1, phi1, A2, phi2, f = symbols('A1, phi1, A2, phi2, f') c = Symbol('c') # Speed of light in vacuum n = Symbol('n') # Refractive index t = Symbol('t') # Time w1 = TWave(A1, f, phi1) w2 = TWave(A2, f, phi2) assert w1.amplitude == A1 assert w1.frequency == f assert w1.phase == phi1 assert w1.wavelength == c/(f*n) assert w1.time_period == 1/f w3 = w1 + w2 assert w3.amplitude == sqrt(A1**2 + 2*A1*A2*cos(phi1 - phi2) + A2**2) assert w3.frequency == f assert w3.wavelength == c/(f*n) assert w3.time_period == 1/f assert w3.angular_velocity == 2*pi*f assert w3.equation() == sqrt(A1**2 + 2*A1*A2*cos(phi1 - phi2) + A2**2)*cos(2*pi*f*t + phi1 + phi2)
def test_p(): assert Px.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert qapply(Px*PxKet(px)) == px*PxKet(px) assert PxKet(px).dual_class() == PxBra assert PxBra(x).dual_class() == PxKet assert (Dagger(PxKet(py))*PxKet(px)).doit() == DiracDelta(px - py) assert (XBra(x)*PxKet(px)).doit() == \ exp(I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(PxKet(px)) == DiracDelta(px - px_1) rep_x = represent(PxOp(), basis=XOp) assert rep_x == -hbar*I*DiracDelta(x_1 - x_2)*DifferentialOperator(x_1) assert rep_x == represent(PxOp(), basis=XOp()) assert rep_x == represent(PxOp(), basis=XKet) assert rep_x == represent(PxOp(), basis=XKet()) assert represent(PxOp()*XKet(), basis=XKet) == \ -hbar*I*DiracDelta(x - x_2)*DifferentialOperator(x) assert represent(XBra("y")*PxOp()*XKet(), basis=XKet) == \ -hbar*I*DiracDelta(x - y)*DifferentialOperator(x)
def test_quantum_fourier(): assert QFT(0, 3).decompose() == \ SwapGate(0, 2)*HadamardGate(0)*CGate((0,), PhaseGate(1)) * \ HadamardGate(1)*CGate((0,), TGate(2))*CGate((1,), PhaseGate(2)) * \ HadamardGate(2) assert IQFT(0, 3).decompose() == \ HadamardGate(2)*CGate((1,), RkGate(2, -2))*CGate((0,), RkGate(2, -3)) * \ HadamardGate(1)*CGate((0,), RkGate(1, -2))*HadamardGate(0)*SwapGate(0, 2) assert represent(QFT(0, 3), nqubits=3) == \ Matrix([[exp(2*pi*I/8)**(i*j % 8)/sqrt(8) for i in range(8)] for j in range(8)]) assert QFT(0, 4).decompose() # non-trivial decomposition assert qapply(QFT(0, 3).decompose()*Qubit(0, 0, 0)).expand() == qapply( HadamardGate(0)*HadamardGate(1)*HadamardGate(2)*Qubit(0, 0, 0) ).expand()
def test_dot_different_frames(): assert dot(N.x, A.x) == cos(q1) assert dot(N.x, A.y) == -sin(q1) assert dot(N.x, A.z) == 0 assert dot(N.y, A.x) == sin(q1) assert dot(N.y, A.y) == cos(q1) assert dot(N.y, A.z) == 0 assert dot(N.z, A.x) == 0 assert dot(N.z, A.y) == 0 assert dot(N.z, A.z) == 1 assert dot(N.x, A.x + A.y) == sqrt(2)*cos(q1 + pi/4) == dot(A.x + A.y, N.x) assert dot(A.x, C.x) == cos(q3) assert dot(A.x, C.y) == 0 assert dot(A.x, C.z) == sin(q3) assert dot(A.y, C.x) == sin(q2)*sin(q3) assert dot(A.y, C.y) == cos(q2) assert dot(A.y, C.z) == -sin(q2)*cos(q3) assert dot(A.z, C.x) == -cos(q2)*sin(q3) assert dot(A.z, C.y) == sin(q2) assert dot(A.z, C.z) == cos(q2)*cos(q3)
def test_vector_simplify(): x, y, z, k, n, m, w, f, s, A = symbols('x, y, z, k, n, m, w, f, s, A') N = ReferenceFrame('N') test1 = (1 / x + 1 / y) * N.x assert (test1 & N.x) != (x + y) / (x * y) test1 = test1.simplify() assert (test1 & N.x) == (x + y) / (x * y) test2 = (A**2 * s**4 / (4 * pi * k * m**3)) * N.x test2 = test2.simplify() assert (test2 & N.x) == (A**2 * s**4 / (4 * pi * k * m**3)) test3 = ((4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x)) * N.x test3 = test3.simplify() assert (test3 & N.x) == 0 test4 = ((-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2) * N.x test4 = test4.simplify() assert (test4 & N.x) == -2 * y
def test_dyadic_simplify(): x, y, z, k, n, m, w, f, s, A = symbols('x, y, z, k, n, m, w, f, s, A') N = ReferenceFrame('N') dy = N.x | N.x test1 = (1 / x + 1 / y) * dy assert (N.x & test1 & N.x) != (x + y) / (x * y) test1 = test1.simplify() assert (N.x & test1 & N.x) == (x + y) / (x * y) test2 = (A**2 * s**4 / (4 * pi * k * m**3)) * dy test2 = test2.simplify() assert (N.x & test2 & N.x) == (A**2 * s**4 / (4 * pi * k * m**3)) test3 = ((4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x)) * dy test3 = test3.simplify() assert (N.x & test3 & N.x) == 0 test4 = ((-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2) * dy test4 = test4.simplify() assert (N.x & test4 & N.x) == -2 * y
def test_Znm(): # http://en.wikipedia.org/wiki/Solid_harmonics#List_of_lowest_functions th, ph = Symbol("theta", real=True), Symbol("phi", real=True) from sympy.abc import n,m assert Znm(0, 0, th, ph) == Ynm(0, 0, th, ph) assert Znm(1, -1, th, ph) == (-sqrt(2)*I*(Ynm(1, 1, th, ph) - exp(-2*I*ph)*Ynm(1, 1, th, ph))/2) assert Znm(1, 0, th, ph) == Ynm(1, 0, th, ph) assert Znm(1, 1, th, ph) == (sqrt(2)*(Ynm(1, 1, th, ph) + exp(-2*I*ph)*Ynm(1, 1, th, ph))/2) assert Znm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi)) assert Znm(1, -1, th, ph).expand(func=True) == (sqrt(3)*I*sqrt(-cos(th)**2 + 1)*exp(I*ph)/(4*sqrt(pi)) - sqrt(3)*I*sqrt(-cos(th)**2 + 1)*exp(-I*ph)/(4*sqrt(pi))) assert Znm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi)) assert Znm(1, 1, th, ph).expand(func=True) == (-sqrt(3)*sqrt(-cos(th)**2 + 1)*exp(I*ph)/(4*sqrt(pi)) - sqrt(3)*sqrt(-cos(th)**2 + 1)*exp(-I*ph)/(4*sqrt(pi))) assert Znm(2, -1, th, ph).expand(func=True) == (sqrt(15)*I*sqrt(-cos(th)**2 + 1)*exp(I*ph)*cos(th)/(4*sqrt(pi)) - sqrt(15)*I*sqrt(-cos(th)**2 + 1)*exp(-I*ph)*cos(th)/(4*sqrt(pi))) assert Znm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi)) assert Znm(2, 1, th, ph).expand(func=True) == (-sqrt(15)*sqrt(-cos(th)**2 + 1)*exp(I*ph)*cos(th)/(4*sqrt(pi)) - sqrt(15)*sqrt(-cos(th)**2 + 1)*exp(-I*ph)*cos(th)/(4*sqrt(pi)))
def compute_stencil(order, n, x_value, h_value, x0=0.): """ computes a stencil of Order order """ h,x = symbols('h x') xs = [x+h*cos(i*pi/n) for i in range(n,-1,-1)] cs = finite_diff_weights(order, xs, x0)[order][n] cs = [simplify(c) for c in cs] cs = [simplify(expr.subs(h, h_value)) for expr in cs] xs = [simplify(expr.subs(h, h_value)) for expr in xs] cs = [simplify(expr.subs(x, x_value)) for expr in cs] xs = [simplify(expr.subs(x, x_value)) for expr in xs] return xs, cs ##############################################
def test_Mul_is_rational(): x = Symbol('x') n = Symbol('n', integer=True) m = Symbol('m', integer=True, nonzero=True) assert (n/m).is_rational is True assert (x/pi).is_rational is None assert (x/n).is_rational is None assert (m/pi).is_rational is False r = Symbol('r', rational=True) assert (pi*r).is_rational is None # issue 8008 z = Symbol('z', zero=True) i = Symbol('i', imaginary=True) assert (z*i).is_rational is None bi = Symbol('i', imaginary=True, finite=True) assert (z*bi).is_zero is True
def test_issue_8247_8354(): from sympy import tan z = sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) - sqrt(10 + 6*sqrt(3)) assert z.is_positive is False # it's 0 z = S('''-2**(1/3)*(3*sqrt(93) + 29)**2 - 4*(3*sqrt(93) + 29)**(4/3) + 12*sqrt(93)*(3*sqrt(93) + 29)**(1/3) + 116*(3*sqrt(93) + 29)**(1/3) + 174*2**(1/3)*sqrt(93) + 1678*2**(1/3)''') assert z.is_positive is False # it's 0 z = 2*(-3*tan(19*pi/90) + sqrt(3))*cos(11*pi/90)*cos(19*pi/90) - \ sqrt(3)*(-3 + 4*cos(19*pi/90)**2) assert z.is_positive is not True # it's zero and it shouldn't hang z = S('''9*(3*sqrt(93) + 29)**(2/3)*((3*sqrt(93) + 29)**(1/3)*(-2**(2/3)*(3*sqrt(93) + 29)**(1/3) - 2) - 2*2**(1/3))**3 + 72*(3*sqrt(93) + 29)**(2/3)*(81*sqrt(93) + 783) + (162*sqrt(93) + 1566)*((3*sqrt(93) + 29)**(1/3)*(-2**(2/3)*(3*sqrt(93) + 29)**(1/3) - 2) - 2*2**(1/3))**2''') assert z.is_positive is False # it's 0 (and a single _mexpand isn't enough)
def test_dot_rota_grad_SH(): theta, phi = symbols("theta phi") assert dot_rot_grad_Ynm(1, 1, 1, 1, 1, 0) != \ sqrt(30)*Ynm(2, 2, 1, 0)/(10*sqrt(pi)) assert dot_rot_grad_Ynm(1, 1, 1, 1, 1, 0).doit() == \ sqrt(30)*Ynm(2, 2, 1, 0)/(10*sqrt(pi)) assert dot_rot_grad_Ynm(1, 5, 1, 1, 1, 2) != \ 0 assert dot_rot_grad_Ynm(1, 5, 1, 1, 1, 2).doit() == \ 0 assert dot_rot_grad_Ynm(3, 3, 3, 3, theta, phi).doit() == \ 15*sqrt(3003)*Ynm(6, 6, theta, phi)/(143*sqrt(pi)) assert dot_rot_grad_Ynm(3, 3, 1, 1, theta, phi).doit() == \ sqrt(3)*Ynm(4, 4, theta, phi)/sqrt(pi) assert dot_rot_grad_Ynm(3, 2, 2, 0, theta, phi).doit() == \ 3*sqrt(55)*Ynm(5, 2, theta, phi)/(11*sqrt(pi)) assert dot_rot_grad_Ynm(3, 2, 3, 2, theta, phi).doit() == \ -sqrt(70)*Ynm(4, 4, theta, phi)/(11*sqrt(pi)) + \ 45*sqrt(182)*Ynm(6, 4, theta, phi)/(143*sqrt(pi))
def fcts(self): j = sympy.Matrix([ r**2 * z, r * z**2 ]) # Create an isotropic sigma vector Sig = sympy.Matrix([ [420/(sympy.pi)*(r*z)**2, 0], [0, 420/(sympy.pi)*(r*z)**2], ]) return j, Sig
def sol(self): # Do the inner product! - we are in cyl coordinates! j, Sig = self.fcts() jTSj = j.T*Sig*j # we are integrating in cyl coordinates ans = sympy.integrate(sympy.integrate(sympy.integrate(r * jTSj, (r, 0, 1)), (t, 0, 2*sympy.pi)), (z, 0, 1))[0] # The `[0]` is to make it an int. return ans
def fcts(self): j = sympy.Matrix([ r**2 * z, r * z**2 ]) # Create an isotropic sigma vector Sig = sympy.Matrix([ [120/(sympy.pi)*(r*z)**2, 0], [0, 420/(sympy.pi)*(r*z)**2], ]) return j, Sig
def fcts(self): h = sympy.Matrix([r**2 * z]) # Create an isotropic sigma vector Sig = sympy.Matrix([200/(sympy.pi)*(r*z)**2]) return h, Sig
def sol(self): h, Sig = self.fcts() # Do the inner product! - we are in cyl coordinates! hTSh = h.T*Sig*h ans = sympy.integrate(sympy.integrate(sympy.integrate(r * hTSh, (r, 0, 1)), (t, 0, 2*sympy.pi)), (z, 0, 1))[0] # The `[0]` is to make it an int. return ans
def __init__(self, n, index): self.dim = n if index == 2: self.degree = 2 r = sqrt(3) / 6 data = [ (1.0, numpy.array([numpy.full(n, 2*r)])), (+r, _s(n, -1, r)), (-r, _s(n, +1, r)), ] else: assert index == 3 self.degree = 3 n2 = n // 2 if n % 2 == 0 else (n-1)//2 i_range = range(1, 2*n+1) pts = [[ [sqrt(fr(2, 3)) * cos((2*k-1)*i*pi / n) for i in i_range], [sqrt(fr(2, 3)) * sin((2*k-1)*i*pi / n) for i in i_range], ] for k in range(1, n2+1)] if n % 2 == 1: sqrt3pm = numpy.full(2*n, 1/sqrt(3)) sqrt3pm[1::2] *= -1 pts.append(sqrt3pm) pts = numpy.vstack(pts).T data = [(fr(1, 2*n), pts)] self.points, self.weights = untangle(data) reference_volume = 2**n self.weights *= reference_volume return
def __init__(self, key): self.degree, data = _gen[key]() self.points, self.weights = untangle(data) self.weights *= sqrt(pi)**3 return
def __init__(self, n, index): self.dim = n if index == 'I': self.degree = 2 data = [ (fr(1, n+1), sqrt(fr(1, 2)) * _nsimplex(n)) ] elif index == 'II': self.degree = 3 nu = sqrt(fr(n, 2)) data = [ (fr(1, 2*n), fsd(n, (nu, 1))) ] elif index == 'III': self.degree = 3 nu = sqrt(fr(1, 2)) data = [ (fr(1, 2**n), pm(n, nu)) ] else: assert index == 'IV' self.degree = 5 nu = sqrt(fr(n+2, 2)) xi = sqrt(fr(n+2, 4)) A = fr(2, n+2) B = fr(4-n, 2 * (n+2)**2) C = fr(1, (n+2)**2) data = [ (A, numpy.full((1, n), 0)), (B, fsd(n, (nu, 1))), (C, fsd(n, (xi, 2))), ] self.points, self.weights = untangle(data) self.weights *= sqrt(pi)**n return
def __init__(self, n): self.name = 'Dobrodeev' self.degree = 7 self.dim = n A = fr(1, 8) B = fr(5-n, 4) C = fr((6 - n) * (1 - n**2) + 36, 4*(n + 3)) D = fr(81, (n + 3) * (n + 6)**2) E = fr(45*n**2 + 324*n + 216, n**2 + 12*n + 36) \ - fr(n * (n**2 - 12*n + 65), 6) r = sqrt(fr(3, n+6)) data = [ (A, fsd(n, (r, 3))), (B, fsd(n, (r, 2))), (C, fsd(n, (r, 1))), (D, fsd(n, (1, 1))), (E, z(n)), ] self.points, self.weights = untangle(data) self.weights /= ( fr(n, 2) * gamma(fr(n, 2)) / sqrt(pi)**n * fr(27 * (n+2) * (n+4), (n+6)**2) ) return
def __init__(self, n, key): self.dim = n self.degree, data = _gen[key](n) self.points, self.weights = untangle(data) self.weights *= 2 * sqrt(pi)**n * gamma(n) / gamma(fr(n, 2)) return
def __init__(self, n): self.weights = numpy.full(n, 2*sympy.pi/n) self.points = numpy.column_stack([ [sympy.cos(sympy.pi * sympy.Rational(2*k, n)) for k in range(n)], [sympy.sin(sympy.pi * sympy.Rational(2*k, n)) for k in range(n)], ]) self.degree = n - 1 return
def _gen4_1(): pts = 2 * sqrt(5) * numpy.array([ [cos(2*i*pi/5) for i in range(5)], [sin(2*i*pi/5) for i in range(5)], ]).T data = [ (fr(7, 10), numpy.array([[0, 0]])), (fr(3, 50), pts), ] return 4, data # The boolean tells whether the factor 2*pi is already in the weights
def __init__(self, key): self.degree, data = _gen[key][0]() weights_contain_2pi = _gen[key][1] self.points, self.weights = untangle(data) if not weights_contain_2pi: self.weights *= 2 * pi return
def __init__(self, settings): Trajectory.__init__(self, settings) # calculate symbolic derivatives up to order n t, a, f, off, p = sp.symbols("t, a, f, off, p") self.yd_sym = [] harmonic = a * sp.sin(2 * sp.pi * f * t + p) + off for idx in range(settings["differential_order"] + 1): self.yd_sym.append(harmonic.diff(t, idx)) # lambdify for idx, val in enumerate(self.yd_sym): self.yd_sym[idx] = sp.lambdify((t, a, f, off, p), val, "numpy")
def _desired_values(self, t): # yd = [] yd = np.zeros((self._settings['differential_order'] + 1), ) a = self._settings['Amplitude'] f = self._settings['Frequency'] off = self._settings["Offset"] p = self._settings["Phase in degree"] * np.pi / 180 for idx, val in enumerate(self.yd_sym): yd[idx] = val(t, a, f, off, p) # yd.append(val(t, a, f, off, p)) return yd
def test_finite_diff(): assert finite_diff(x**2 + 2*x + 1, x) == 2*x + 3 assert finite_diff(y**3 + 2*y**2 + 3*y + 5, y) == 3*y**2 + 7*y + 6 assert finite_diff(z**2 - 2*z + 3, z) == 2*z - 1 assert finite_diff(w**2 + 3*w - 2, w) == 2*w + 4 assert finite_diff(sin(x), x, pi/6) == -sin(x) + sin(x + pi/6) assert finite_diff(cos(y), y, pi/3) == -cos(y) + cos(y + pi/3) assert finite_diff(x**2 - 2*x + 3, x, 2) == 4*x assert finite_diff(n**2 - 2*n + 3, n, 3) == 6*n + 3
def test_expressions_failing(): assert residue(1/(x**4 + 1), x, exp(I*pi/4)) == -(S(1)/4 + I/4)/sqrt(2) n = Symbol('n', integer=True, positive=True) assert residue(exp(z)/(z - pi*I/4*a)**n, z, I*pi*a) == \ exp(I*pi*a/4)/factorial(n - 1) assert residue(1/(x**2 + a**2)**2, x, a*I) == -I/4/a**3
def _unpolarify(eq, exponents_only, pause=False): from sympy import polar_lift, exp, principal_branch, pi if isinstance(eq, bool) or eq.is_Atom: return eq if not pause: if eq.func is exp_polar: return exp(_unpolarify(eq.exp, exponents_only)) if eq.func is principal_branch and eq.args[1] == 2*pi: return _unpolarify(eq.args[0], exponents_only) if ( eq.is_Add or eq.is_Mul or eq.is_Boolean or eq.is_Relational and ( eq.rel_op in ('==', '!=') and 0 in eq.args or eq.rel_op not in ('==', '!=')) ): return eq.func(*[_unpolarify(x, exponents_only) for x in eq.args]) if eq.func is polar_lift: return _unpolarify(eq.args[0], exponents_only) if eq.is_Pow: expo = _unpolarify(eq.exp, exponents_only) base = _unpolarify(eq.base, exponents_only, not (expo.is_integer and not pause)) return base**expo if eq.is_Function and getattr(eq.func, 'unbranched', False): return eq.func(*[_unpolarify(x, exponents_only, exponents_only) for x in eq.args]) return eq.func(*[_unpolarify(x, exponents_only, True) for x in eq.args])
def test_Mul_is_rational(): x = Symbol('x') n = Symbol('n', integer=True) m = Symbol('m', integer=True) assert (n/m).is_rational is True assert (x/pi).is_rational is None assert (x/n).is_rational is None assert (n/pi).is_rational is False
def test_Add_is_rational(): x = Symbol('x') n = Symbol('n', rational=True) m = Symbol('m', rational=True) assert (n + m).is_rational is True assert (x + pi).is_rational is None assert (x + n).is_rational is None assert (n + pi).is_rational is False
def test_real_Pow(): k = Symbol('k', integer=True, nonzero=True) assert (k**(I*pi/log(k))).is_real
def test_Add_is_positive_2(): e = Rational(1, 3) - sqrt(8) assert e.is_positive is False assert e.is_negative is True e = pi - 1 assert e.is_positive is True assert e.is_negative is False
def test_float_int(): assert int(float(sqrt(10))) == int(sqrt(10)) assert int(pi**1000) % 10 == 2 assert int(Float('1.123456789012345678901234567890e20', '')) == \ long(112345678901234567890) assert int(Float('1.123456789012345678901234567890e25', '')) == \ long(11234567890123456789012345) # decimal forces float so it's not an exact integer ending in 000000 assert int(Float('1.123456789012345678901234567890e35', '')) == \ 112345678901234567890123456789000192 assert int(Float('123456789012345678901234567890e5', '')) == \ 12345678901234567890123456789000000 assert Integer(Float('1.123456789012345678901234567890e20', '')) == \ 112345678901234567890 assert Integer(Float('1.123456789012345678901234567890e25', '')) == \ 11234567890123456789012345 # decimal forces float so it's not an exact integer ending in 000000 assert Integer(Float('1.123456789012345678901234567890e35', '')) == \ 112345678901234567890123456789000192 assert Integer(Float('123456789012345678901234567890e5', '')) == \ 12345678901234567890123456789000000 assert Float('123000e-2','') == Float('1230.00', '') assert Float('123000e2','') == Float('12300000', '') assert int(1 + Rational('.9999999999999999999999999')) == 1 assert int(pi/1e20) == 0 assert int(1 + pi/1e20) == 1 assert int(Add(1.2, -2, evaluate=False)) == int(1.2 - 2) assert int(Add(1.2, +2, evaluate=False)) == int(1.2 + 2) assert int(Add(1 + Float('.99999999999999999', ''), evaluate=False)) == 1 raises(TypeError, lambda: float(x)) raises(TypeError, lambda: float(sqrt(-1))) assert int(12345678901234567890 + cos(1)**2 + sin(1)**2) == \ 12345678901234567891
def test_mul_coeff(): # It is important that all Numbers be removed from the seq; # This can be tricky when powers combine to produce those numbers p = exp(I*pi/3) assert p**2*x*p*y*p*x*p**2 == x**2*y
def test_sympy__physics__quantum__spin__Rotation(): from sympy.physics.quantum.spin import Rotation from sympy import pi assert _test_args(Rotation(pi, 0, pi/2))
def test_sympy__physics__quantum__state__Wavefunction(): from sympy.physics.quantum.state import Wavefunction from sympy.functions import sin from sympy import Piecewise, pi n = 1 L = 1 g = Piecewise((0, x < 0), (0, x > L), (sqrt(2//L)*sin(n*pi*x/L), True)) assert _test_args(Wavefunction(g, x))
def timeit_abs_pi(): abs(pi)
def test_gosper_sum_AeqB_part1(): f1a = n**4 f1b = n**3*2**n f1c = 1/(n**2 + sqrt(5)*n - 1) f1d = n**4*4**n/binomial(2*n, n) f1e = factorial(3*n)/(factorial(n)*factorial(n + 1)*factorial(n + 2)*27**n) f1f = binomial(2*n, n)**2/((n + 1)*4**(2*n)) f1g = (4*n - 1)*binomial(2*n, n)**2/((2*n - 1)**2*4**(2*n)) f1h = n*factorial(n - S(1)/2)**2/factorial(n + 1)**2 g1a = m*(m + 1)*(2*m + 1)*(3*m**2 + 3*m - 1)/30 g1b = 26 + 2**(m + 1)*(m**3 - 3*m**2 + 9*m - 13) g1c = (m + 1)*(m*(m**2 - 7*m + 3)*sqrt(5) - ( 3*m**3 - 7*m**2 + 19*m - 6))/(2*m**3*sqrt(5) + m**4 + 5*m**2 - 1)/6 g1d = -S(2)/231 + 2*4**m*(m + 1)*(63*m**4 + 112*m**3 + 18*m**2 - 22*m + 3)/(693*binomial(2*m, m)) g1e = -S(9)/2 + (81*m**2 + 261*m + 200)*factorial( 3*m + 2)/(40*27**m*factorial(m)*factorial(m + 1)*factorial(m + 2)) g1f = (2*m + 1)**2*binomial(2*m, m)**2/(4**(2*m)*(m + 1)) g1g = -binomial(2*m, m)**2/4**(2*m) g1h = 4*pi -(2*m + 1)**2*(3*m + 4)*factorial(m - S(1)/2)**2/factorial(m + 1)**2 g = gosper_sum(f1a, (n, 0, m)) assert g is not None and simplify(g - g1a) == 0 g = gosper_sum(f1b, (n, 0, m)) assert g is not None and simplify(g - g1b) == 0 g = gosper_sum(f1c, (n, 0, m)) assert g is not None and simplify(g - g1c) == 0 g = gosper_sum(f1d, (n, 0, m)) assert g is not None and simplify(g - g1d) == 0 g = gosper_sum(f1e, (n, 0, m)) assert g is not None and simplify(g - g1e) == 0 g = gosper_sum(f1f, (n, 0, m)) assert g is not None and simplify(g - g1f) == 0 g = gosper_sum(f1g, (n, 0, m)) assert g is not None and simplify(g - g1g) == 0 g = gosper_sum(f1h, (n, 0, m)) # need to call rewrite(gamma) here because we have terms involving # factorial(1/2) assert g is not None and simplify(g - g1h).rewrite(gamma) == 0
def test_pl_true_wrong_input(): from sympy import pi raises(ValueError, lambda: pl_true('John Cleese')) raises(ValueError, lambda: pl_true(42 + pi + pi ** 2)) raises(ValueError, lambda: pl_true(42))
def angular_velocity(self): """ Returns angular velocity of the wave. Examples ======== >>> from sympy import symbols >>> from sympy.physics.optics import TWave >>> A, phi, f = symbols('A, phi, f') >>> w = TWave(A, f, phi) >>> w.angular_velocity 2*pi*f """ return 2*pi*self._frequency
def get_target_matrix(self, format='sympy'): if format == 'sympy': return Matrix([[1, 0], [0, exp(Integer(2)*pi*I/(Integer(2)**self.k))]]) raise NotImplementedError( 'Invalid format for the R_k gate: %r' % format)