我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用sympy.N。
def event_bounds_expressions(self, event_bounds_exp): if hasattr(self, 'output_equations'): assert len(event_bounds_exp)+1 == self.output_equations.shape[0] if hasattr(self, 'output_equations_functions'): assert len(event_bounds_exp)+1 == \ self.output_equations_functions.size if hasattr(self, 'state_equations'): assert len(event_bounds_exp)+1 == self.state_equations.shape[0] if hasattr(self, 'state_equations_functions'): assert len(event_bounds_exp)+1 == \ self.state_equations_functions.size self._event_bounds_expressions = event_bounds_exp self.event_bounds = np.array( [sp.N(bound, subs=self.constants_values) for bound in event_bounds_exp], dtype=np.float_ )
def Nga(x, prec=5): if isinstance(x, MV): Px = MV(x) Px.obj = Nsympy(x.obj, prec) return Px else: return Nsympy(x, prec)
def test_intrinsic_math1_codegen(): # not included: log10 from sympy import acos, asin, atan, ceiling, cos, cosh, floor, log, ln, \ sin, sinh, sqrt, tan, tanh, N name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs(x, xval)) numerical_tests.append((name, (xval,), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == "C": name_expr_C = [("test_floor", floor(x)), ("test_ceil", ceiling(x))] else: name_expr_C = [] run_test("intrinsic_math1", name_expr + name_expr_C, numerical_tests, lang, commands)
def test_instrinsic_math2_codegen(): # not included: frexp, ldexp, modf, fmod from sympy import atan2, N 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_complicated_codegen(): from sympy import sin, cos, tan, N name_expr = [ ("test1", ((sin(x) + cos(y) + tan(z))**7).expand()), ("test2", cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))), ] numerical_tests = [] for name, expr in name_expr: for xval, yval, zval in (0.2, 1.3, -0.3), (0.5, -0.2, 0.0), (0.8, 2.1, 0.8): expected = N(expr.subs(x, xval).subs(y, yval).subs(z, zval)) numerical_tests.append((name, (xval, yval, zval), expected, 1e-12)) for lang, commands in valid_lang_commands: run_test( "complicated_codegen", name_expr, numerical_tests, lang, commands)
def p_magic(self, program): """ magic : MAGIC REAL """ program[0] = node.Magic([node.Real(sympy.N(program[2]))])
def sym(self, nested_scope=None): """Return the correspond symbolic number.""" return N(self.value)
def _uniqueid(n=30): """Return a unique string with length n. :parameter int N: number of character in the uniqueid :return: the uniqueid :rtype: str """ return ''.join(random.SystemRandom().choice( string.ascii_uppercase + string.ascii_lowercase) for _ in range(n))
def test_intrinsic_math1_codegen(): # not included: log10 from sympy import acos, asin, atan, ceiling, cos, cosh, floor, log, ln, \ sin, sinh, sqrt, tan, tanh, N name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs(x, xval)) numerical_tests.append((name, (xval,), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang.startswith("C"): name_expr_C = [("test_floor", floor(x)), ("test_ceil", ceiling(x))] else: name_expr_C = [] run_test("intrinsic_math1", name_expr + name_expr_C, numerical_tests, lang, commands)
def chebyshev1(n, decimal_places): # There are explicit representations, too, but for the sake of consistency # go for the recurrence coefficients approach here. _, _, alpha, beta = \ recurrence_coefficients.chebyshev1(n, 'monic', symbolic=True) beta[0] = sympy.N(beta[0], decimal_places) return custom(alpha, beta, mode='mpmath', decimal_places=decimal_places)
def chebyshev2(n, decimal_places): # There are explicit representations, too, but for the sake of consistency # go for the recurrence coefficients approach here. _, _, alpha, beta = \ recurrence_coefficients.chebyshev2(n, 'monic', symbolic=True) beta[0] = sympy.N(beta[0], decimal_places) return custom(alpha, beta, mode='mpmath', decimal_places=decimal_places)
def hermite(n, decimal_places): _, _, alpha, beta = recurrence_coefficients.hermite( n, standardization='physicist', symbolic=True ) b0 = sympy.N(beta[0], decimal_places) beta = [b/4 for b in beta] beta[0] = b0 return custom(alpha, beta, mode='mpmath', decimal_places=decimal_places)
def test_xk(k): n = 10 moments = orthopy.line.compute_moments(lambda x: x**k, -1, +1, 2*n) alpha, beta = orthopy.line.chebyshev(moments) assert (alpha == 0).all() assert beta[0] == moments[0] assert beta[1] == sympy.Rational(k+1, k+3) assert beta[2] == sympy.Rational(4, (k+5) * (k+3)) orthopy.line.schemes.custom( numpy.array([sympy.N(a) for a in alpha], dtype=float), numpy.array([sympy.N(b) for b in beta], dtype=float), mode='numpy' ) # a, b = \ # orthopy.line.recurrence_coefficients.legendre( # 2*n, mode='sympy' # ) # moments = orthopy.line.compute_moments( # lambda x: x**2, -1, +1, 2*n, # polynomial_class=orthopy.line.legendre # ) # alpha, beta = orthopy.line.chebyshev_modified(moments, a, b) # points, weights = orthopy.line.schemes.custom( # numpy.array([sympy.N(a) for a in alpha], dtype=float), # numpy.array([sympy.N(b) for b in beta], dtype=float) # ) return