def _set_inv_trans_equations(curv_coord_name): """ Store information about inverse transformation equations for pre-defined coordinate systems. Parameters ========== curv_coord_name : str Name of coordinate system """ if curv_coord_name == 'cartesian': return lambda x, y, z: (x, y, z) if curv_coord_name == 'spherical': return lambda x, y, z: ( sqrt(x**2 + y**2 + z**2), acos(z/sqrt(x**2 + y**2 + z**2)), atan2(y, x) ) if curv_coord_name == 'cylindrical': return lambda x, y, z: ( sqrt(x**2 + y**2), atan2(y, x), z ) raise ValueError('Wrong set of parameters.' 'Type of coordinate system is defined')
def test_as_real_imag(): n = pi**1000 # the special code for working out the real # and complex parts of a power with Integer exponent # should not run if there is no imaginary part, hence # this should not hang assert n.as_real_imag() == (n, 0) # issue 6261 x = Symbol('x') assert sqrt(x).as_real_imag() == \ ((re(x)**2 + im(x)**2)**Rational(1, 4)*cos(atan2(im(x), re(x))/2), (re(x)**2 + im(x)**2)**Rational(1, 4)*sin(atan2(im(x), re(x))/2)) # issue 3853 a, b = symbols('a,b', real=True) assert ((1 + sqrt(a + b*I))/2).as_real_imag() == \ ( (a**2 + b**2)**Rational( 1, 4)*cos(atan2(b, a)/2)/2 + S.Half, (a**2 + b**2)**Rational(1, 4)*sin(atan2(b, a)/2)/2) assert sqrt(a**2).as_real_imag() == (sqrt(a**2), 0) i = symbols('i', imaginary=True) assert sqrt(i**2).as_real_imag() == (0, abs(i)) assert ((1 + I) / (1 - I)).as_real_imag() == (0, 1) assert ((1 + I)**3 / (1 - I)).as_real_imag() == (-2, 0)
def test_Dimension_functions(): raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(cos(length))) raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(acos(angle))) raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(atan2(length, time))) raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(log(length))) raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(log(100, length))) raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(log(length, 10))) assert dimsys_SI.get_dimensional_dependencies(pi) == {} assert dimsys_SI.get_dimensional_dependencies(cos(1)) == {} assert dimsys_SI.get_dimensional_dependencies(cos(angle)) == {} assert dimsys_SI.get_dimensional_dependencies(atan2(length, length)) == {} assert dimsys_SI.get_dimensional_dependencies( log(length / length, length / length)) == {} assert dimsys_SI.get_dimensional_dependencies(Abs(length)) == {length: 1} assert dimsys_SI.get_dimensional_dependencies(Abs(length / length)) == {} assert dimsys_SI.get_dimensional_dependencies(sqrt(-1)) == {}
def brewster_angle(medium1, medium2): """ This function calculates the Brewster's angle of incidence to Medium 2 from Medium 1 in radians. Parameters ========== medium 1 : Medium or sympifiable Refractive index of Medium 1 medium 2 : Medium or sympifiable Refractive index of Medium 1 Examples ======== >>> from sympy.physics.optics import brewster_angle >>> brewster_angle(1, 1.33) 0.926093295503462 """ n1 = refractive_index_of_medium(medium1) n2 = refractive_index_of_medium(medium2) return atan2(n2, n1)
def eval(cls, arg): a = arg for i in range(3): if isinstance(a, cls): a = a.args[0] else: if i == 2 and a.is_extended_real: return S.NaN break else: return S.NaN if isinstance(arg, exp_polar): return periodic_argument(arg, oo) if not arg.is_Atom: c, arg_ = factor_terms(arg).as_coeff_Mul() if arg_.is_Mul: arg_ = Mul(*[a if (sign(a) not in (-1, 1)) else sign(a) for a in arg_.args]) arg_ = sign(c)*arg_ else: arg_ = arg if any(i.is_extended_positive is None for i in arg_.atoms(AppliedUndef)): return x, y = arg_.as_real_imag() rv = atan2(y, x) if rv.is_number: return rv if arg_ != arg: return cls(arg_, evaluate=False)
def test_atan2(): assert refine(atan2(y, x), Q.real(y) & Q.positive(x)) == atan(y/x) assert refine(atan2(y, x), Q.negative(y) & Q.positive(x)) == atan(y/x) assert refine(atan2(y, x), Q.negative(y) & Q.negative(x)) == atan(y/x) - pi assert refine(atan2(y, x), Q.positive(y) & Q.negative(x)) == atan(y/x) + pi assert refine(atan2(y, x), Q.zero(y) & Q.negative(x)) == pi assert refine(atan2(y, x), Q.positive(y) & Q.zero(x)) == pi/2 assert refine(atan2(y, x), Q.negative(y) & Q.zero(x)) == -pi/2 assert refine(atan2(y, x), Q.zero(y) & Q.zero(x)) is nan
def test_functions_subs(): f, g = symbols('f g', cls=Function) l = Lambda((x, y), sin(x) + y) assert (g(y, x) + cos(x)).subs(g, l) == sin(y) + x + cos(x) assert (f(x)**2).subs(f, sin) == sin(x)**2 assert (f(x, y)).subs(f, log) == log(x, y) assert (f(x, y)).subs(f, sin) == f(x, y) assert (sin(x) + atan2(x, y)).subs([[atan2, f], [sin, g]]) == \ f(x, y) + g(x) assert (g(f(x + y, x))).subs([[f, l], [g, exp]]) == exp(x + sin(x + y))
def gouy(self): """ The Gouy phase. Examples ======== >>> from sympy.physics.optics import BeamParameter >>> p = BeamParameter(530e-9, 1, w=1e-3) >>> p.gouy atan(0.53/pi) """ return atan2(self.z, self.z_r)
def eval(cls, arg): if not arg.is_Atom: c, arg_ = factor_terms(arg).as_coeff_Mul() if arg_.is_Mul: arg_ = Mul(*[a if (sign(a) not in (-1, 1)) else sign(a) for a in arg_.args]) arg_ = sign(c)*arg_ else: arg_ = arg x, y = re(arg_), im(arg_) rv = atan2(y, x) if rv.is_number and not rv.atoms(AppliedUndef): return rv if arg_ != arg: return cls(arg_, evaluate=False)
def eval(cls, arg): if not arg.is_Atom: c, arg_ = factor_terms(arg).as_coeff_Mul() if arg_.is_Mul: arg_ = Mul(*[a if (sign(a) not in (-1, 1)) else sign(a) for a in arg_.args]) arg_ = sign(c)*arg_ else: arg_ = arg x, y = re(arg_), im(arg_) rv = atan2(y, x) if rv.is_number and not rv.atoms(AppliedUndef): return rv if arg_ != arg: return cls(arg_, evaluate=False)
def test_instrinsic_math2_codegen(): # not included: frexp, ldexp, modf, fmod from sympy.core.evalf import N from sympy.functions.elementary.trigonometric import atan2 name_expr = [ ("test_atan2", atan2(x, y)), ("test_pow", x**y), ] numerical_tests = [] for name, expr in name_expr: for xval, yval in (0.2, 1.3), (0.5, -0.2), (0.8, 0.8): expected = N(expr.subs(x, xval).subs(y, yval)) numerical_tests.append((name, (xval, yval), expected, 1e-14)) for lang, commands in valid_lang_commands: run_test("intrinsic_math2", name_expr, numerical_tests, lang, commands)
def test_issue_8514(): from sympy.simplify.simplify import simplify a, b, c, = symbols('a b c', positive=True) t = symbols('t', positive=True) ft = simplify(inverse_laplace_transform(1/(a*s**2+b*s+c),s, t)) assert ft == (I*exp(t*cos(atan2(0, -4*a*c + b**2)/2)*sqrt(Abs(4*a*c - b**2))/a)*sin(t*sin(atan2(0, -4*a*c + b**2)/2)*sqrt(Abs( 4*a*c - b**2))/(2*a)) + exp(t*cos(atan2(0, -4*a*c + b**2) /2)*sqrt(Abs(4*a*c - b**2))/a)*cos(t*sin(atan2(0, -4*a*c + b**2)/2)*sqrt(Abs(4*a*c - b**2))/(2*a)) + I*sin(t*sin( atan2(0, -4*a*c + b**2)/2)*sqrt(Abs(4*a*c - b**2))/(2*a)) - cos(t*sin(atan2(0, -4*a*c + b**2)/2)*sqrt(Abs(4*a*c - b**2))/(2*a)))*exp(-t*(b + cos(atan2(0, -4*a*c + b**2)/2) *sqrt(Abs(4*a*c - b**2)))/(2*a))/sqrt(-4*a*c + b**2)
def eval(cls, arg): if isinstance(arg, exp_polar): return periodic_argument(arg, oo) if not arg.is_Atom: c, arg_ = factor_terms(arg).as_coeff_Mul() if arg_.is_Mul: arg_ = Mul(*[a if (sign(a) not in (-1, 1)) else sign(a) for a in arg_.args]) arg_ = sign(c)*arg_ else: arg_ = arg if arg_.atoms(AppliedUndef): return x, y = arg_.as_real_imag() rv = atan2(y, x) if rv.is_number: return rv if arg_ != arg: return cls(arg_, evaluate=False)
def __add__(self, other): """ Addition of two waves will result in their superposition. The type of interference will depend on their phase angles. """ if isinstance(other, TWave): if self._frequency == other._frequency and self.wavelength == other.wavelength: return TWave( sqrt(self._amplitude**2 + other._amplitude**2 + 2 * self._amplitude * other._amplitude * cos(self._phase - other.phase)), self._frequency, atan2( self._amplitude * sin(self._phase) + other._amplitude * sin(other._phase), self._amplitude * cos(self._phase) + other._amplitude * cos(other._phase))) else: raise NotImplementedError( "Interference of waves with different frequencies" " has not been implemented.") else: raise TypeError( type(other).__name__ + " and TWave objects cannot be added.")
def angle(self): r""" Returns the angle of the quaternion measured in the real-axis plane. Explanation =========== Given a quaternion $q = a + bi + cj + dk$ where a, b, c and d are real numbers, returns the angle of the quaternion given by .. math:: angle := atan2(\sqrt{b^2 + c^2 + d^2}, {a}) Examples ======== >>> from sympy.algebras.quaternion import Quaternion >>> q = Quaternion(1, 4, 4, 4) >>> q.angle() atan(4*sqrt(3)) """ return atan2(self.vector_part().norm(), self.scalar_part())
def test_sympy__functions__elementary__trigonometric__atan2(): from sympy.functions.elementary.trigonometric import atan2 assert _test_args(atan2(2, 3))
def test_quaternion_functions(): q = Quaternion(w, x, y, z) q1 = Quaternion(1, 2, 3, 4) q0 = Quaternion(0, 0, 0, 0) assert conjugate(q) == Quaternion(w, -x, -y, -z) assert q.norm() == sqrt(w**2 + x**2 + y**2 + z**2) assert q.normalize() == Quaternion(w, x, y, z) / sqrt(w**2 + x**2 + y**2 + z**2) assert q.inverse() == Quaternion(w, -x, -y, -z) / (w**2 + x**2 + y**2 + z**2) assert q.inverse() == q.pow(-1) raises(ValueError, lambda: q0.inverse()) assert q.pow(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2 * w * x, 2 * w * y, 2 * w * z) assert q**(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2 * w * x, 2 * w * y, 2 * w * z) assert q1.pow(-2) == Quaternion(Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225)) assert q1**(-2) == Quaternion(Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225)) assert q1.pow(-0.5) == NotImplemented raises(TypeError, lambda: q1**(-0.5)) assert q1.exp() == \ Quaternion(E * cos(sqrt(29)), 2 * sqrt(29) * E * sin(sqrt(29)) / 29, 3 * sqrt(29) * E * sin(sqrt(29)) / 29, 4 * sqrt(29) * E * sin(sqrt(29)) / 29) assert q1._ln() == \ Quaternion(log(sqrt(30)), 2 * sqrt(29) * acos(sqrt(30)/30) / 29, 3 * sqrt(29) * acos(sqrt(30)/30) / 29, 4 * sqrt(29) * acos(sqrt(30)/30) / 29) assert q1.pow_cos_sin(2) == \ Quaternion(30 * cos(2 * acos(sqrt(30)/30)), 60 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29, 90 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29, 120 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29) assert diff(Quaternion(x, x, x, x), x) == Quaternion(1, 1, 1, 1) assert integrate(Quaternion(x, x, x, x), x) == \ Quaternion(x**2 / 2, x**2 / 2, x**2 / 2, x**2 / 2) assert Quaternion.rotate_point((1, 1, 1), q1) == (S.One / 5, 1, S(7) / 5) n = Symbol('n') raises(TypeError, lambda: q1**n) n = Symbol('n', integer=True) raises(TypeError, lambda: q1**n) assert Quaternion(22, 23, 55, 8).scalar_part() == 22 assert Quaternion(w, x, y, z).scalar_part() == w assert Quaternion(22, 23, 55, 8).vector_part() == Quaternion(0, 23, 55, 8) assert Quaternion(w, x, y, z).vector_part() == Quaternion(0, x, y, z) assert q1.axis() == Quaternion(0, 2 * sqrt(29) / 29, 3 * sqrt(29) / 29, 4 * sqrt(29) / 29) assert q1.axis().pow(2) == Quaternion(-1, 0, 0, 0) assert q0.axis().scalar_part() == 0 assert q.axis() == Quaternion(0, x / sqrt(x**2 + y**2 + z**2), y / sqrt(x**2 + y**2 + z**2), z / sqrt(x**2 + y**2 + z**2)) assert q0.is_pure() == True assert q1.is_pure() == False assert Quaternion(0, 0, 0, 3).is_pure() == True assert Quaternion(0, 2, 10, 3).is_pure() == True assert Quaternion(w, 2, 10, 3).is_pure() == None assert q1.angle() == atan(sqrt(29)) assert q.angle() == atan2(sqrt(x**2 + y**2 + z**2), w) assert Quaternion.arc_coplanar(q1, Quaternion(2, 4, 6, 8)) == True assert Quaternion.arc_coplanar(q1, Quaternion(1, -2, -3, -4)) == True assert Quaternion.arc_coplanar(q1, Quaternion(1, 8, 12, 16)) == True assert Quaternion.arc_coplanar(q1, Quaternion(1, 2, 3, 4)) == True assert Quaternion.arc_coplanar(q1, Quaternion(w, 4, 6, 8)) == True assert Quaternion.arc_coplanar(q1, Quaternion(2, 7, 4, 1)) == False assert Quaternion.arc_coplanar(q1, Quaternion(w, x, y, z)) == None raises(ValueError, lambda: Quaternion.arc_coplanar(q1, q0)) assert Quaternion.vector_coplanar(Quaternion(0, 8, 12, 16), Quaternion(0, 4, 6, 8), Quaternion(0, 2, 3, 4)) == True assert Quaternion.vector_coplanar(Quaternion(0, 0, 0, 0), Quaternion(0, 4, 6, 8), Quaternion(0, 2, 3, 4)) == True assert Quaternion.vector_coplanar(Quaternion(0, 8, 2, 6), Quaternion(0, 1, 6, 6), Quaternion(0, 0, 3, 4)) == False assert Quaternion.vector_coplanar(Quaternion(0, 1, 3, 4), Quaternion(0, 4, w, 6), Quaternion(0, 6, 8, 1)) == None raises(ValueError, lambda: Quaternion.vector_coplanar(q0, Quaternion(0, 4, 6, 8), q1)) assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 4, 6)) == True assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 2, 6)) == False assert Quaternion(0, 1, 2, 3).parallel(Quaternion(w, x, y, 6)) == None raises(ValueError, lambda: q0.parallel(q1)) assert Quaternion(0, 1, 2, 3).orthogonal(Quaternion(0, -2, 1, 0)) == True assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(0, 2, 2, 6)) == False assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(w, x, y, 6)) == None raises(ValueError, lambda: q0.orthogonal(q1)) assert q1.index_vector() == Quaternion(0, 2 * sqrt(870) / 29, 3 * sqrt(870) / 29, 4 * sqrt(870) / 29) assert Quaternion(0, 3, 9, 4).index_vector() == Quaternion(0, 3, 9, 4) assert Quaternion(4, 3, 9, 4).mensor() == log(sqrt(122)) assert Quaternion(3, 3, 0, 2).mensor() == log(sqrt(22)) assert q0.is_zero_quaternion() == True assert q1.is_zero_quaternion() == False assert Quaternion(w, 0, 0, 0).is_zero_quaternion() == None
def test_arg_rewrite(): assert arg(1 + I) == atan2(1, 1) x = Symbol('x', real=True) y = Symbol('y', real=True) assert arg(x + I * y).rewrite(atan2) == atan2(y, x)
def test_implicit(): x, y = symbols('x,y') assert fcode(sin(x)) == " sin(x)" assert fcode(atan2(x, y)) == " atan2(x, y)" assert fcode(conjugate(x)) == " conjg(x)"
def _eval_rewrite_as_atan2(self, arg): x, y = re(self.args[0]), im(self.args[0]) return atan2(y, x)
def _eval_rewrite_as_atan2(self, arg, **kwargs): x, y = self.args[0].as_real_imag() return atan2(y, x)
def test_twave(): A1, phi1, A2, phi2, f = symbols('A1, phi1, A2, phi2, f') n = Symbol('n') # Refractive index t = Symbol('t') # Time x = Symbol('x') # Spatial variable E = Function('E') 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 assert w1.angular_velocity == 2 * pi * f assert w1.wavenumber == 2 * pi * f * n / c assert w1.speed == c / n w3 = w1 + w2 assert w3.amplitude == sqrt(A1**2 + 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) assert w3.frequency == f assert w3.phase == atan2(A1 * sin(phi1) + A2 * sin(phi2), A1 * cos(phi1) + A2 * cos(phi2)) assert w3.wavelength == c / (f * n) assert w3.time_period == 1 / f assert w3.angular_velocity == 2 * pi * f assert w3.wavenumber == 2 * pi * f * n / c assert w3.speed == c / n assert simplify(w3.rewrite(sin) - w2.rewrite(sin) - w1.rewrite(sin)) == 0 assert w3.rewrite('pde') == epsilon * mu * Derivative(E( x, t), t, t) + Derivative(E(x, t), x, x) assert w3.rewrite( cos) == sqrt(A1**2 + 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * cos(pi * f * n * x * s / (149896229 * m) - 2 * pi * f * t + atan2(A1 * sin(phi1) + A2 * sin(phi2), A1 * cos(phi1) + A2 * cos(phi2))) assert w3.rewrite( exp) == sqrt(A1**2 + 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * exp( I * (-2 * pi * f * t + atan2(A1 * sin(phi1) + A2 * sin(phi2), A1 * cos(phi1) + A2 * cos(phi2)) + pi * s * f * n * x / (149896229 * m))) w4 = TWave(A1, None, 0, 1 / f) assert w4.frequency == f w5 = w1 - w2 assert w5.amplitude == sqrt(A1**2 - 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) assert w5.frequency == f assert w5.phase == atan2(A1 * sin(phi1) - A2 * sin(phi2), A1 * cos(phi1) - A2 * cos(phi2)) assert w5.wavelength == c / (f * n) assert w5.time_period == 1 / f assert w5.angular_velocity == 2 * pi * f assert w5.wavenumber == 2 * pi * f * n / c assert w5.speed == c / n assert simplify(w5.rewrite(sin) - w1.rewrite(sin) + w2.rewrite(sin)) == 0 assert w5.rewrite('pde') == epsilon * mu * Derivative(E( x, t), t, t) + Derivative(E(x, t), x, x) assert w5.rewrite(cos) == sqrt( A1**2 - 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * cos(-2 * pi * f * t + atan2(A1 * sin(phi1) - A2 * sin(phi2), A1 * cos(phi1) - A2 * cos(phi2)) + pi * s * f * n * x / (149896229 * m)) assert w5.rewrite( exp) == sqrt(A1**2 - 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * exp( I * (-2 * pi * f * t + atan2(A1 * sin(phi1) - A2 * sin(phi2), A1 * cos(phi1) - A2 * cos(phi2)) + pi * s * f * n * x / (149896229 * m))) w6 = 2 * w1 assert w6.amplitude == 2 * A1 assert w6.frequency == f assert w6.phase == phi1 w7 = -w6 assert w7.amplitude == -2 * A1 assert w7.frequency == f assert w7.phase == phi1 raises(ValueError, lambda: TWave(A1)) raises(ValueError, lambda: TWave(A1, f, phi1, t))
def test_re(): x, y = symbols('x,y') a, b = symbols('a,b', real=True) r = Symbol('r', real=True) i = Symbol('i', imaginary=True) assert re(nan) is nan assert re(oo) is oo assert re(-oo) is -oo assert re(0) == 0 assert re(1) == 1 assert re(-1) == -1 assert re(E) == E assert re(-E) == -E assert unchanged(re, x) assert re(x * I) == -im(x) assert re(r * I) == 0 assert re(r) == r assert re(i * I) == I * i assert re(i) == 0 assert re(x + y) == re(x) + re(y) assert re(x + r) == re(x) + r assert re(re(x)) == re(x) assert re(2 + I) == 2 assert re(x + I) == re(x) assert re(x + y * I) == re(x) - im(y) assert re(x + r * I) == re(x) assert re(log(2 * I)) == log(2) assert re((2 + I)**2).expand(complex=True) == 3 assert re(conjugate(x)) == re(x) assert conjugate(re(x)) == re(x) assert re(x).as_real_imag() == (re(x), 0) assert re(i * r * x).diff(r) == re(i * x) assert re(i * r * x).diff(i) == I * r * im(x) assert re( sqrt(a + b * I)) == (a**2 + b**2)**Rational(1, 4) * cos(atan2(b, a) / 2) assert re(a * (2 + b * I)) == 2 * a assert re((1 + sqrt(a + b*I))/2) == \ (a**2 + b**2)**Rational(1, 4)*cos(atan2(b, a)/2)/2 + S.Half assert re(x).rewrite(im) == x - S.ImaginaryUnit * im(x) assert (x + re(y)).rewrite(re, im) == x + y - S.ImaginaryUnit * im(y) a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) x = Symbol('x') assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False assert re(S.ComplexInfinity) is S.NaN n, m, l = symbols('n m l') A = MatrixSymbol('A', n, m) assert re(A) == (S.Half) * (A + conjugate(A)) A = Matrix([[1 + 4 * I, 2], [0, -3 * I]]) assert re(A) == Matrix([[1, 2], [0, 0]]) A = ImmutableMatrix([[1 + 3 * I, 3 - 2 * I], [0, 2 * I]]) assert re(A) == ImmutableMatrix([[1, 3], [0, 0]]) X = SparseMatrix([[2 * j + i * I for i in range(5)] for j in range(5)]) assert re(X) - Matrix([[0, 0, 0, 0, 0], [2, 2, 2, 2, 2], [4, 4, 4, 4, 4], [6, 6, 6, 6, 6], [8, 8, 8, 8, 8] ]) == Matrix.zeros(5) assert im(X) - Matrix([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4] ]) == Matrix.zeros(5) X = FunctionMatrix(3, 3, Lambda((n, m), n + m * I)) assert re(X) == Matrix([[0, 0, 0], [1, 1, 1], [2, 2, 2]])
def test_transformation_equations(): x, y, z = symbols('x y z') # Str a = CoordSys3D('a', transformation='spherical', variable_names=["r", "theta", "phi"]) r, theta, phi = a.base_scalars() assert r == a.r assert theta == a.theta assert phi == a.phi raises(AttributeError, lambda: a.x) raises(AttributeError, lambda: a.y) raises(AttributeError, lambda: a.z) assert a.transformation_to_parent() == (r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)) assert a.lame_coefficients() == (1, r, r * sin(theta)) assert a.transformation_from_parent_function()( x, y, z) == (sqrt(x**2 + y**2 + z**2), acos((z) / sqrt(x**2 + y**2 + z**2)), atan2(y, x)) a = CoordSys3D('a', transformation='cylindrical', variable_names=["r", "theta", "z"]) r, theta, z = a.base_scalars() assert a.transformation_to_parent() == (r * cos(theta), r * sin(theta), z) assert a.lame_coefficients() == (1, a.r, 1) assert a.transformation_from_parent_function()(x, y, z) == (sqrt(x**2 + y**2), atan2(y, x), z) a = CoordSys3D('a', 'cartesian') assert a.transformation_to_parent() == (a.x, a.y, a.z) assert a.lame_coefficients() == (1, 1, 1) assert a.transformation_from_parent_function()(x, y, z) == (x, y, z) # Variables and expressions # Cartesian with equation tuple: x, y, z = symbols('x y z') a = CoordSys3D('a', ((x, y, z), (x, y, z))) a._calculate_inv_trans_equations() assert a.transformation_to_parent() == (a.x1, a.x2, a.x3) assert a.lame_coefficients() == (1, 1, 1) assert a.transformation_from_parent_function()(x, y, z) == (x, y, z) r, theta, z = symbols("r theta z") # Cylindrical with equation tuple: a = CoordSys3D('a', [(r, theta, z), (r * cos(theta), r * sin(theta), z)], variable_names=["r", "theta", "z"]) r, theta, z = a.base_scalars() assert a.transformation_to_parent() == (r * cos(theta), r * sin(theta), z) assert a.lame_coefficients() == ( sqrt(sin(theta)**2 + cos(theta)**2), sqrt(r**2 * sin(theta)**2 + r**2 * cos(theta)**2), 1 ) # ==> this should simplify to (1, r, 1), tests are too slow with `simplify`. # Definitions with `lambda`: # Cartesian with `lambda` a = CoordSys3D('a', lambda x, y, z: (x, y, z)) assert a.transformation_to_parent() == (a.x1, a.x2, a.x3) assert a.lame_coefficients() == (1, 1, 1) a._calculate_inv_trans_equations() assert a.transformation_from_parent_function()(x, y, z) == (x, y, z) # Spherical with `lambda` a = CoordSys3D( 'a', lambda r, theta, phi: (r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)), variable_names=["r", "theta", "phi"]) r, theta, phi = a.base_scalars() assert a.transformation_to_parent() == (r * sin(theta) * cos(phi), r * sin(phi) * sin(theta), r * cos(theta)) assert a.lame_coefficients() == ( sqrt( sin(phi)**2 * sin(theta)**2 + sin(theta)**2 * cos(phi)**2 + cos(theta)**2), sqrt(r**2 * sin(phi)**2 * cos(theta)**2 + r**2 * sin(theta)**2 + r**2 * cos(phi)**2 * cos(theta)**2), sqrt(r**2 * sin(phi)**2 * sin(theta)**2 + r**2 * sin(theta)**2 * cos(phi)**2) ) # ==> this should simplify to (1, r, sin(theta)*r), `simplify` is too slow. # Cylindrical with `lambda` a = CoordSys3D('a', lambda r, theta, z: (r * cos(theta), r * sin(theta), z), variable_names=["r", "theta", "z"]) r, theta, z = a.base_scalars() assert a.transformation_to_parent() == (r * cos(theta), r * sin(theta), z) assert a.lame_coefficients() == ( sqrt(sin(theta)**2 + cos(theta)**2), sqrt(r**2 * sin(theta)**2 + r**2 * cos(theta)**2), 1 ) # ==> this should simplify to (1, a.x, 1) raises( TypeError, lambda: CoordSys3D('a', transformation={ x: x * sin(y) * cos(z), y: x * sin(y) * sin(z), z: x * cos(y) }))
def test_create_new(): a = CoordSys3D('a') c = a.create_new('c', transformation='spherical') assert c._parent == a assert c.transformation_to_parent() == \ (c.r*sin(c.theta)*cos(c.phi), c.r*sin(c.theta)*sin(c.phi), c.r*cos(c.theta)) assert c.transformation_from_parent() == \ (sqrt(a.x**2 + a.y**2 + a.z**2), acos(a.z/sqrt(a.x**2 + a.y**2 + a.z**2)), atan2(a.y, a.x))
def _eval_rewrite_as_atan2(self, arg): x, y = re(self.args[0]), im(self.args[0]) return atan2(y, x)
def test_sympy__functions__elementary__trigonometric__atan2(): from sympy.functions.elementary.trigonometric import atan2 assert _test_args(atan2(2, 3))
def test_gauss_opt(): mat = RayTransferMatrix(1, 2, 3, 4) assert mat == Matrix([[1, 2], [3, 4]]) assert mat == RayTransferMatrix(Matrix([[1, 2], [3, 4]])) assert [mat.A, mat.B, mat.C, mat.D] == [1, 2, 3, 4] d, f, h, n1, n2, R = symbols('d f h n1 n2 R') lens = ThinLens(f) assert lens == Matrix([[1, 0], [-1 / f, 1]]) assert lens.C == -1 / f assert FreeSpace(d) == Matrix([[1, d], [0, 1]]) assert FlatRefraction(n1, n2) == Matrix([[1, 0], [0, n1 / n2]]) assert CurvedRefraction(R, n1, n2) == Matrix([[1, 0], [(n1 - n2) / (R * n2), n1 / n2]]) assert FlatMirror() == Matrix([[1, 0], [0, 1]]) assert CurvedMirror(R) == Matrix([[1, 0], [-2 / R, 1]]) assert ThinLens(f) == Matrix([[1, 0], [-1 / f, 1]]) mul = CurvedMirror(R) * FreeSpace(d) mul_mat = Matrix([[1, 0], [-2 / R, 1]]) * Matrix([[1, d], [0, 1]]) assert mul.A == mul_mat[0, 0] assert mul.B == mul_mat[0, 1] assert mul.C == mul_mat[1, 0] assert mul.D == mul_mat[1, 1] angle = symbols('angle') assert GeometricRay(h, angle) == Matrix([[h], [angle]]) assert FreeSpace(d) * GeometricRay(h, angle) == Matrix([[angle * d + h], [angle]]) assert GeometricRay(Matrix(((h, ), (angle, )))) == Matrix([[h], [angle]]) assert (FreeSpace(d) * GeometricRay(h, angle)).height == angle * d + h assert (FreeSpace(d) * GeometricRay(h, angle)).angle == angle p = BeamParameter(530e-9, 1, w=1e-3) assert streq(p.q, 1 + 1.88679245283019 * I * pi) assert streq(N(p.q), 1.0 + 5.92753330865999 * I) assert streq(N(p.w_0), Float(0.00100000000000000)) assert streq(N(p.z_r), Float(5.92753330865999)) fs = FreeSpace(10) p1 = fs * p assert streq(N(p.w), Float(0.00101413072159615)) assert streq(N(p1.w), Float(0.00210803120913829)) w, wavelen = symbols('w wavelen') assert waist2rayleigh(w, wavelen) == pi * w**2 / wavelen z_r, wavelen = symbols('z_r wavelen') assert rayleigh2waist(z_r, wavelen) == sqrt(wavelen * z_r) / sqrt(pi) a, b, f = symbols('a b f') assert geometric_conj_ab(a, b) == a * b / (a + b) assert geometric_conj_af(a, f) == a * f / (a - f) assert geometric_conj_bf(b, f) == b * f / (b - f) assert geometric_conj_ab(oo, b) == b assert geometric_conj_ab(a, oo) == a s_in, z_r_in, f = symbols('s_in z_r_in f') assert gaussian_conj(s_in, z_r_in, f)[0] == 1 / (-1 / (s_in + z_r_in**2 / (-f + s_in)) + 1 / f) assert gaussian_conj( s_in, z_r_in, f)[1] == z_r_in / (1 - s_in**2 / f**2 + z_r_in**2 / f**2) assert gaussian_conj( s_in, z_r_in, f)[2] == 1 / sqrt(1 - s_in**2 / f**2 + z_r_in**2 / f**2) l, w_i, w_o, f = symbols('l w_i w_o f') assert conjugate_gauss_beams( l, w_i, w_o, f=f)[0] == f * (-sqrt(w_i**2 / w_o**2 - pi**2 * w_i**4 / (f**2 * l**2)) + 1) assert factor(conjugate_gauss_beams( l, w_i, w_o, f=f)[1]) == f * w_o**2 * (w_i**2 / w_o**2 - sqrt(w_i**2 / w_o**2 - pi**2 * w_i**4 / (f**2 * l**2))) / w_i**2 assert conjugate_gauss_beams(l, w_i, w_o, f=f)[2] == f z, l, w_0 = symbols('z l w_0', positive=True) p = BeamParameter(l, z, w=w_0) assert p.radius == z * (pi**2 * w_0**4 / (l**2 * z**2) + 1) assert p.w == w_0 * sqrt(l**2 * z**2 / (pi**2 * w_0**4) + 1) assert p.w_0 == w_0 assert p.divergence == l / (pi * w_0) assert p.gouy == atan2(z, pi * w_0**2 / l) assert p.waist_approximation_limit == 2 * l / pi p = BeamParameter(530e-9, 1, w=1e-3, n=2) assert streq(p.q, 1 + 3.77358490566038 * I * pi) assert streq(N(p.z_r), Float(11.8550666173200)) assert streq(N(p.w_0), Float(0.00100000000000000))
def _eval_rewrite_as_atan2(self, arg, **kwargs): x, y = self.args[0].as_real_imag() return atan2(y, x)
'R2', 'R2_origin', 'relations_2d', 'R2_r', 'R2_p', 'R3', 'R3_origin', 'relations_3d', 'R3_r', 'R3_c', 'R3_s' ] ############################################################################### # R2 ############################################################################### R2 = Manifold('R^2', 2) # type: Any R2_origin = Patch('origin', R2) # type: Any x, y = symbols('x y', real=True) r, theta = symbols('rho theta', nonnegative=True) relations_2d = { ('rectangular', 'polar'): [(x, y), (sqrt(x**2 + y**2), atan2(y, x))], ('polar', 'rectangular'): [(r, theta), (r * cos(theta), r * sin(theta))], } R2_r = CoordSystem('rectangular', R2_origin, (x, y), relations_2d) # type: Any R2_p = CoordSystem('polar', R2_origin, (r, theta), relations_2d) # type: Any # support deprecated feature with warnings.catch_warnings(): warnings.simplefilter("ignore") x, y, r, theta = symbols('x y r theta', cls=Dummy) R2_r.connect_to(R2_p, [x, y], [sqrt(x**2 + y**2), atan2(y, x)], inverse=False, fill_in_gaps=False) R2_p.connect_to(R2_r, [r, theta], [r * cos(theta), r * sin(theta)],
def test_im(): x, y = symbols('x,y') a, b = symbols('a,b', real=True) r = Symbol('r', real=True) i = Symbol('i', imaginary=True) assert im(nan) is nan assert im(oo * I) is oo assert im(-oo * I) is -oo assert im(0) == 0 assert im(1) == 0 assert im(-1) == 0 assert im(E * I) == E assert im(-E * I) == -E assert unchanged(im, x) assert im(x * I) == re(x) assert im(r * I) == r assert im(r) == 0 assert im(i * I) == 0 assert im(i) == -I * i assert im(x + y) == im(x) + im(y) assert im(x + r) == im(x) assert im(x + r * I) == im(x) + r assert im(im(x) * I) == im(x) assert im(2 + I) == 1 assert im(x + I) == im(x) + 1 assert im(x + y * I) == im(x) + re(y) assert im(x + r * I) == im(x) + r assert im(log(2 * I)) == pi / 2 assert im((2 + I)**2).expand(complex=True) == 4 assert im(conjugate(x)) == -im(x) assert conjugate(im(x)) == im(x) assert im(x).as_real_imag() == (im(x), 0) assert im(i * r * x).diff(r) == im(i * x) assert im(i * r * x).diff(i) == -I * re(r * x) assert im( sqrt(a + b * I)) == (a**2 + b**2)**Rational(1, 4) * sin(atan2(b, a) / 2) assert im(a * (2 + b * I)) == a * b assert im((1 + sqrt(a + b*I))/2) == \ (a**2 + b**2)**Rational(1, 4)*sin(atan2(b, a)/2)/2 assert im(x).rewrite(re) == -S.ImaginaryUnit * (x - re(x)) assert (x + im(y)).rewrite(im, re) == x - S.ImaginaryUnit * (y - re(y)) a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) x = Symbol('x') assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False assert im(S.ComplexInfinity) is S.NaN n, m, l = symbols('n m l') A = MatrixSymbol('A', n, m) assert im(A) == (S.One / (2 * I)) * (A - conjugate(A)) A = Matrix([[1 + 4 * I, 2], [0, -3 * I]]) assert im(A) == Matrix([[4, 0], [0, -3]]) A = ImmutableMatrix([[1 + 3 * I, 3 - 2 * I], [0, 2 * I]]) assert im(A) == ImmutableMatrix([[3, -2], [0, 2]]) X = ImmutableSparseMatrix([[i * I + i for i in range(5)] for i in range(5)]) Y = SparseMatrix([[i for i in range(5)] for i in range(5)]) assert im(X).as_immutable() == Y X = FunctionMatrix(3, 3, Lambda((n, m), n + m * I)) assert im(X) == Matrix([[0, 1, 2], [0, 1, 2], [0, 1, 2]])
def _eval_rewrite_as_atan2(self, arg, **kwargs): from sympy.functions.elementary.trigonometric import atan2 x, y = self.args[0].as_real_imag() return atan2(y, x)