def rand_cone_uniform_check(): from sympy import sin, cos, sqrt, cbrt, Matrix u, v, w = sympy.symbols('u, v, w') x = cbrt(w) * sqrt(v) * cos(u) y = cbrt(w) * sqrt(v) * sin(u) z = cbrt(w) jacmat = Matrix([x, y, z]).jacobian(Matrix([u, v, w])) jac = sympy.simplify(jacmat.det()) print(jac) # -1/6
def rand_solidangle_uniform_check(): from sympy import sin, cos, sqrt, cbrt, Matrix u, v, w, alpha = sympy.symbols('u, v, w, alpha') d = 1 - v * (1 - cos(alpha)) x = cbrt(w) * sqrt(1 - d * d) * cos(u) y = cbrt(w) * sqrt(1 - d * d) * sin(u) z = cbrt(w) * d jacmat = Matrix([x, y, z]).jacobian(Matrix([u, v, w])) jac = sympy.simplify(jacmat.det()) print(jac) # cos(alpha)/3 - 1/3
def test_pow_1(): assert ((1 + x)**2).nseries(x, n=5) == x**2 + 2 * x + 1 # https://github.com/sympy/sympy/issues/21075 assert ((sqrt(x) + 1)**2).nseries(x) == 2 * sqrt(x) + x + 1 assert ((sqrt(x) + cbrt(x))**2).nseries(x) == 2*x**Rational(5, 6)\ + x**Rational(2, 3) + x
def test_branch_cuts(): assert limit(asin(I * x + 2), x, 0) == pi - asin(2) assert limit(asin(I * x + 2), x, 0, '-') == asin(2) assert limit(asin(I * x - 2), x, 0) == -asin(2) assert limit(asin(I * x - 2), x, 0, '-') == -pi + asin(2) assert limit(acos(I * x + 2), x, 0) == -acos(2) assert limit(acos(I * x + 2), x, 0, '-') == acos(2) assert limit(acos(I * x - 2), x, 0) == acos(-2) assert limit(acos(I * x - 2), x, 0, '-') == 2 * pi - acos(-2) assert limit(atan(x + 2 * I), x, 0) == I * atanh(2) assert limit(atan(x + 2 * I), x, 0, '-') == -pi + I * atanh(2) assert limit(atan(x - 2 * I), x, 0) == pi - I * atanh(2) assert limit(atan(x - 2 * I), x, 0, '-') == -I * atanh(2) assert limit(atan(1 / x), x, 0) == pi / 2 assert limit(atan(1 / x), x, 0, '-') == -pi / 2 assert limit(atan(x), x, oo) == pi / 2 assert limit(atan(x), x, -oo) == -pi / 2 assert limit(acot(x + S(1) / 2 * I), x, 0) == pi - I * acoth(S(1) / 2) assert limit(acot(x + S(1) / 2 * I), x, 0, '-') == -I * acoth(S(1) / 2) assert limit(acot(x - S(1) / 2 * I), x, 0) == I * acoth(S(1) / 2) assert limit(acot(x - S(1) / 2 * I), x, 0, '-') == -pi + I * acoth(S(1) / 2) assert limit(acot(x), x, 0) == pi / 2 assert limit(acot(x), x, 0, '-') == -pi / 2 assert limit(asec(I * x + S(1) / 2), x, 0) == asec(S(1) / 2) assert limit(asec(I * x + S(1) / 2), x, 0, '-') == -asec(S(1) / 2) assert limit(asec(I * x - S(1) / 2), x, 0) == 2 * pi - asec(-S(1) / 2) assert limit(asec(I * x - S(1) / 2), x, 0, '-') == asec(-S(1) / 2) assert limit(acsc(I * x + S(1) / 2), x, 0) == acsc(S(1) / 2) assert limit(acsc(I * x + S(1) / 2), x, 0, '-') == pi - acsc(S(1) / 2) assert limit(acsc(I * x - S(1) / 2), x, 0) == -pi + acsc(S(1) / 2) assert limit(acsc(I * x - S(1) / 2), x, 0, '-') == -acsc(S(1) / 2) assert limit(log(I * x - 1), x, 0) == I * pi assert limit(log(I * x - 1), x, 0, '-') == -I * pi assert limit(log(-I * x - 1), x, 0) == -I * pi assert limit(log(-I * x - 1), x, 0, '-') == I * pi assert limit(sqrt(I * x - 1), x, 0) == I assert limit(sqrt(I * x - 1), x, 0, '-') == -I assert limit(sqrt(-I * x - 1), x, 0) == -I assert limit(sqrt(-I * x - 1), x, 0, '-') == I assert limit(cbrt(I * x - 1), x, 0) == (-1)**(S(1) / 3) assert limit(cbrt(I * x - 1), x, 0, '-') == -(-1)**(S(2) / 3) assert limit(cbrt(-I * x - 1), x, 0) == -(-1)**(S(2) / 3) assert limit(cbrt(-I * x - 1), x, 0, '-') == (-1)**(S(1) / 3)
def rand_sphere_concentric_check(): from sympy import sin, cos, sqrt, cbrt, Matrix u, v, w, r0, r1 = sympy.symbols('u, v, w, r0, r1') r = cbrt(r0**3 + (r1**3 - r0**3) * w) x = r * sqrt(1 - v * v) * cos(u) y = r * sqrt(1 - v * v) * sin(u) z = r * v jacmat = Matrix([x, y, z]).jacobian(Matrix([u, v, w])) jac = sympy.simplify(jacmat.det()) print(jac) # -r0**3/3 + r1**3/3
def generate_alpha(self, epsilon=[0, 0, 0, 0, 0, 0], preserve_volume=False): '''Computes the (symmetric) deformation matrix in Cartesian coordinates that would lead to infinitesimal strain epsilon,i.e. alpha=1+epsilon, where epsilon is first converted to Cartesian notation. If preserve_volume is set to True, alpha will subsequently be rescaled by its determinant.''' alpha = UnVoigt(np.asarray(epsilon) + np.array([1, 1, 1, 0, 0, 0])) if preserve_volume: determinant = sp.det(sp.Matrix(alpha)) if isinstance(determinant, sp.Float): alpha = alpha / (float(determinant)**(1 / 3)) else: alpha = alpha / sp.cbrt(sp.factor(determinant)) self.alpha = alpha return alpha
def cbrt(x): return diffify(sympy.cbrt(x))
def test_minimal_polynomial(): assert minimal_polynomial(-7, x) == x + 7 assert minimal_polynomial(-1, x) == x + 1 assert minimal_polynomial(0, x) == x assert minimal_polynomial(1, x) == x - 1 assert minimal_polynomial(7, x) == x - 7 assert minimal_polynomial(sqrt(2), x) == x**2 - 2 assert minimal_polynomial(sqrt(5), x) == x**2 - 5 assert minimal_polynomial(sqrt(6), x) == x**2 - 6 assert minimal_polynomial(2 * sqrt(2), x) == x**2 - 8 assert minimal_polynomial(3 * sqrt(5), x) == x**2 - 45 assert minimal_polynomial(4 * sqrt(6), x) == x**2 - 96 assert minimal_polynomial(2 * sqrt(2) + 3, x) == x**2 - 6 * x + 1 assert minimal_polynomial(3 * sqrt(5) + 6, x) == x**2 - 12 * x - 9 assert minimal_polynomial(4 * sqrt(6) + 7, x) == x**2 - 14 * x - 47 assert minimal_polynomial(2 * sqrt(2) - 3, x) == x**2 + 6 * x + 1 assert minimal_polynomial(3 * sqrt(5) - 6, x) == x**2 + 12 * x - 9 assert minimal_polynomial(4 * sqrt(6) - 7, x) == x**2 + 14 * x - 47 assert minimal_polynomial(sqrt(1 + sqrt(6)), x) == x**4 - 2 * x**2 - 5 assert minimal_polynomial(sqrt(I + sqrt(6)), x) == x**8 - 10 * x**4 + 49 assert (minimal_polynomial(2 * I + sqrt(2 + I), x) == x**4 + 4 * x**2 + 8 * x + 37) assert minimal_polynomial(sqrt(2) + sqrt(3), x) == x**4 - 10 * x**2 + 1 assert (minimal_polynomial(sqrt(2) + sqrt(3) + sqrt(6), x) == x**4 - 22 * x**2 - 48 * x - 23) a = 1 - 9 * sqrt(2) + 7 * sqrt(3) assert (minimal_polynomial(1 / a, x) == 392 * x**4 - 1232 * x**3 + 612 * x**2 + 4 * x - 1) assert (minimal_polynomial(1 / sqrt(a), x) == 392 * x**8 - 1232 * x**6 + 612 * x**4 + 4 * x**2 - 1) raises(NotAlgebraic, lambda: minimal_polynomial(oo, x)) raises(NotAlgebraic, lambda: minimal_polynomial(2**y, x)) raises(NotAlgebraic, lambda: minimal_polynomial(sin(1), x)) assert minimal_polynomial(sqrt(2)).dummy_eq(x**2 - 2) assert minimal_polynomial(sqrt(2), x) == x**2 - 2 assert minimal_polynomial(sqrt(2), polys=True) == Poly(x**2 - 2) assert minimal_polynomial(sqrt(2), x, polys=True) == Poly(x**2 - 2) assert minimal_polynomial(sqrt(2), x, polys=True, compose=False) == Poly(x**2 - 2) a = AlgebraicNumber(sqrt(2)) b = AlgebraicNumber(sqrt(3)) assert minimal_polynomial(a, x) == x**2 - 2 assert minimal_polynomial(b, x) == x**2 - 3 assert minimal_polynomial(a, x, polys=True) == Poly(x**2 - 2) assert minimal_polynomial(b, x, polys=True) == Poly(x**2 - 3) assert minimal_polynomial(sqrt(a / 2 + 17), x) == 2 * x**4 - 68 * x**2 + 577 assert minimal_polynomial(sqrt(b / 2 + 17), x) == 4 * x**4 - 136 * x**2 + 1153 a, b = sqrt(2) / 3 + 7, AlgebraicNumber(sqrt(2) / 3 + 7) f = (81 * x**8 - 2268 * x**6 - 4536 * x**5 + 22644 * x**4 + 63216 * x**3 - 31608 * x**2 - 189648 * x + 141358) assert minimal_polynomial(sqrt(a) + sqrt(sqrt(a)), x) == f assert minimal_polynomial(sqrt(b) + sqrt(sqrt(b)), x) == f assert (minimal_polynomial(a**Q(3, 2), x) == 729 * x**4 - 506898 * x**2 + 84604519) # issue 5994 eq = S(""" -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)))""") assert minimal_polynomial(eq, x) == 8000 * x**2 - 1 ex = 1 + sqrt(2) + sqrt(3) mp = minimal_polynomial(ex, x) assert mp == x**4 - 4 * x**3 - 4 * x**2 + 16 * x - 8 ex = 1 / (1 + sqrt(2) + sqrt(3)) mp = minimal_polynomial(ex, x) assert mp == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1 p = (expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3))**Rational(1, 3) mp = minimal_polynomial(p, x) assert (mp == x**8 - 8 * x**7 - 56 * x**6 + 448 * x**5 + 480 * x**4 - 5056 * x**3 + 1984 * x**2 + 7424 * x - 3008) p = expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3) mp = minimal_polynomial(p, x) assert (mp == x**8 - 512 * x**7 - 118208 * x**6 + 31131136 * x**5 + 647362560 * x**4 - 56026611712 * x**3 + 116994310144 * x**2 + 404854931456 * x - 27216576512) assert minimal_polynomial(S("-sqrt(5)/2 - 1/2 + (-sqrt(5)/2 - 1/2)**2"), x) == x - 1 a = 1 + sqrt(2) assert minimal_polynomial((a * sqrt(2) + a)**3, x) == x**2 - 198 * x + 1 p = 1 / (1 + sqrt(2) + sqrt(3)) assert (minimal_polynomial(p, x, compose=False) == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1) p = 2 / (1 + sqrt(2) + sqrt(3)) assert (minimal_polynomial(p, x, compose=False) == x**4 - 4 * x**3 + 2 * x**2 + 4 * x - 2) assert minimal_polynomial(1 + sqrt(2) * I, x, compose=False) == x**2 - 2 * x + 3 assert minimal_polynomial(1 / (1 + sqrt(2)) + 1, x, compose=False) == x**2 - 2 assert (minimal_polynomial(sqrt(2) * I + I * (1 + sqrt(2)), x, compose=False) == x**4 + 18 * x**2 + 49) # minimal polynomial of I assert minimal_polynomial(I, x, domain=QQ.algebraic_field(I)) == x - I K = QQ.algebraic_field(I * (sqrt(2) + 1)) assert minimal_polynomial(I, x, domain=K) == x - I assert minimal_polynomial(I, x, domain=QQ) == x**2 + 1 assert minimal_polynomial(I, x, domain="QQ(y)") == x**2 + 1 # issue 11553 assert minimal_polynomial(GoldenRatio, x) == x**2 - x - 1 assert (minimal_polynomial(TribonacciConstant + 3, x) == x**3 - 10 * x**2 + 32 * x - 34) assert (minimal_polynomial(GoldenRatio, x, domain=QQ.algebraic_field(sqrt(5))) == 2 * x - sqrt(5) - 1) assert (minimal_polynomial(TribonacciConstant, x, domain=QQ.algebraic_field( cbrt(19 - 3 * sqrt(33)))) == 48 * x - 19 * (19 - 3 * sqrt(33))**Rational(2, 3) - 3 * sqrt(33) * (19 - 3 * sqrt(33))**Rational(2, 3) - 16 * (19 - 3 * sqrt(33))**Rational(1, 3) - 16)
# Ecuaciones sp.Eq(expresión_izquierda, expresión_derecha) # Crear ecuación expresión.lhs # Obtener expresión izquierda de la ecuación expresión.rhs # Obtener expresión derecha de la ecuación # Constrantes integradas sp.pi # Pi sp.E # euler sp.oo # Infinito sp.nan # Vacio sp.I # Unidad imaginaria # Potencias y raices sp.Pow(x, potencia) # Calcular la potencia n-esima sp.sqrt(num) # Calcular la raiz cuadrada sp.cbrt(num) # Calcular la raiz cubica sp.Pow(x, sp.Rational(1, n)) # Calcular la raiz n-esima # Exponentes y logaritmos sp.exp(num) # Calcular la exponencial sp.log(num) # Calcular el logaritmo natural sp.log(base, num) # Calcular el logaritmo en la base especificada # Conversión de angulos sp.deg(num) # Convertir ángulos de radianes a grados. sp.rad(num) # Convertir ángulos de grados a radianes. # Funciones para trigonometría (Angulos en radianes) sp.sin(num) # Seno sp.cos(num) # Coseno sp.tan(num) # tangente
def set_cbrt(coeff_low=1, coeff_high=8): return sp.cbrt(random.randint(coeff_low, coeff_high)*x + random.randint(coeff_low, coeff_high))
def generate_random_crater_sympy_expr( center, radiuses, depth=None, height=None, locations=None, standard_deviations=None, multiplier=None, ): """generate_random_crater_sympy_expr Parameters ---------- center : radiuses : depth : float The depth of the crater height : float The height of the crater locations : np.array The locations of all the Gaussian random bumps standard_deviations : np.array The standard deviations for the different Gaussian random bumps multiplier : float The multiplier used to balance the main and the random parts of the energy function Returns ------- """ assert locations.shape[1] == center.size assert standard_deviations.shape == (locations.shape[0], ) n_dim = center.size n_bumps = locations.shape[0] location = sympy.Array(sympy.symbols('x:{}'.format(n_dim)), (n_dim, )) center = sympy.Array(center, center.shape) r_squared = sympy_array_squared_norm(location - center) C = (3 * radiuses[1]**2 * sympy.cbrt(depth * height * (depth + sympy.sqrt(depth * (depth + height))))) Delta0 = -9 * depth * height * radiuses[1]**4 b_squared = -(1 / (3 * depth)) * (-3 * depth * radiuses[1]**2 + C + Delta0 / C) a = depth / (3 * b_squared * radiuses[1]**4 - radiuses[1]**6) crater_expr = (a * (2 * r_squared**3 - 3 * (b_squared + radiuses[1]**2) * r_squared**2 + 6 * b_squared * radiuses[1]**2 * r_squared) - depth) mollifier_expr = sympy.functions.exp( -radiuses[1] / (radiuses[1] - sympy_array_squared_norm(location - center)**10)) / np.exp(-1) random_components = [ sympy.functions.exp( -sympy_array_squared_norm(location - sympy.Array(locations[ii], (n_dim, ))) / (2 * standard_deviations[ii]**2)) for ii in range(n_bumps) ] random_expr = 0 for ii in range(n_bumps): random_expr += random_components[ii] sympy_expr = crater_expr + multiplier * mollifier_expr * random_expr gradient_expr = sympy.derive_by_array(sympy_expr, location) return location, gradient_expr