示例#1
0
def test_add_1():
    assert O(x + x) == O(x)
    assert O(3 * x - 2 * x**2) == O(x)
    assert O(1 + x) == O(1, x)
    assert O(1 + 1 / x) == O(1 / x)
    assert O(ln(x) + 1 / ln(x)) == O(ln(x))
    assert O(exp(1 / x) + x) == O(exp(1 / x))
    assert O(exp(1 / x) + 1 / x**20) == O(exp(1 / x))
示例#2
0
def test_order_at_infinity():
    assert O(1 + x, (x, oo)) == O(x, (x, oo))
    assert O(3 * x, (x, oo)) == O(x, (x, oo))
    assert O(x, (x, oo)) * 3 == O(x, (x, oo))
    assert -28 * O(x, (x, oo)) == O(x, (x, oo))
    assert O(O(x, (x, oo)), (x, oo)) == O(x, (x, oo))
    assert O(O(x, (x, oo)), (y, oo)) == O(x, (x, oo), (y, oo))
    assert O(3, (x, oo)) == O(1, (x, oo))
    assert O(x**2 + x + y, (x, oo)) == O(x**2, (x, oo))
    assert O(x**2 + x + y, (y, oo)) == O(y, (y, oo))

    assert O(2 * x, (x, oo)) * x == O(x**2, (x, oo))
    assert O(2 * x, (x, oo)) / x == O(1, (x, oo))
    assert O(2 * x, (x, oo)) * x * exp(1 / x) == O(x**2 * exp(1 / x), (x, oo))
    assert O(2 * x, (x, oo)) * x * exp(1 / x) / ln(x)**3 == O(
        x**2 * exp(1 / x) * ln(x)**-3, (x, oo))

    assert O(x, (x, oo)) + 1 / x == 1 / x + O(x, (x, oo)) == O(x, (x, oo))
    assert O(x, (x, oo)) + 1 == 1 + O(x, (x, oo)) == O(x, (x, oo))
    assert O(x, (x, oo)) + x == x + O(x, (x, oo)) == O(x, (x, oo))
    assert O(x, (x, oo)) + x**2 == x**2 + O(x, (x, oo))
    assert O(1 / x,
             (x, oo)) + 1 / x**2 == 1 / x**2 + O(1 / x,
                                                 (x, oo)) == O(1 / x, (x, oo))
    assert O(x, (x, oo)) + exp(1 / x) == exp(1 / x) + O(x, (x, oo))

    assert O(x, (x, oo))**2 == O(x**2, (x, oo))

    assert O(x, (x, oo)) + O(x**2, (x, oo)) == O(x**2, (x, oo))
    assert O(x, (x, oo)) + O(x**-2, (x, oo)) == O(x, (x, oo))
    assert O(x, (x, oo)) + O(1 / x, (x, oo)) == O(x, (x, oo))

    assert O(x, (x, oo)) - O(x, (x, oo)) == O(x, (x, oo))
    assert O(x, (x, oo)) + O(1, (x, oo)) == O(x, (x, oo))
    assert O(x, (x, oo)) + O(x**2, (x, oo)) == O(x**2, (x, oo))
    assert O(1 / x, (x, oo)) + O(1, (x, oo)) == O(1, (x, oo))
    assert O(x, (x, oo)) + O(exp(1 / x), (x, oo)) == O(x, (x, oo))
    assert O(x**3, (x, oo)) + O(exp(2 / x), (x, oo)) == O(x**3, (x, oo))
    assert O(x**-3, (x, oo)) + O(exp(2 / x), (x, oo)) == O(exp(2 / x), (x, oo))

    # issue sympy/sympy#7207
    assert O(exp(x), (x, oo)).expr == O(2 * exp(x), (x, oo)).expr == exp(x)
    assert O(y**x, (x, oo)).expr == O(2 * y**x, (x, oo)).expr == y**x

    # issue sympy/sympy#9917
    assert O(x * sin(x) + 1, (x, oo)) != O(x * sin(x), (x, oo))

    # issue sympy/sympy#15539
    assert O(x**-6, (x, -oo)) == O(x**(-6), (x, -oo), evaluate=False)
示例#3
0
def test_intrinsic_math1_codegen():
    # not included: log10
    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), strict=False)
            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)
示例#4
0
def test_intrinsic_math1_codegen():
    # not included: log10
    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}), strict=False)
            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)
示例#5
0
def test_intrinsic_math1_codegen():
    # not included: log10
    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}), strict=False)
            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)
示例#6
0
def test_ansi_math1_codegen():
    # not included: log10
    name_expr = [
        ("test_fabs", Abs(x)),
        ("test_acos", acos(x)),
        ("test_asin", asin(x)),
        ("test_atan", atan(x)),
        ("test_ceil", ceiling(x)),
        ("test_cos", cos(x)),
        ("test_cosh", cosh(x)),
        ("test_floor", floor(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)),
    ]
    result = codegen(name_expr, "C", "file", header=False, empty=False)
    assert result[0][0] == "file.c"
    assert result[0][1] == (
        '#include "file.h"\n#include <math.h>\n'
        'double test_fabs(double x) {\n   double test_fabs_result;\n   test_fabs_result = fabs(x);\n   return test_fabs_result;\n}\n'
        'double test_acos(double x) {\n   double test_acos_result;\n   test_acos_result = acos(x);\n   return test_acos_result;\n}\n'
        'double test_asin(double x) {\n   double test_asin_result;\n   test_asin_result = asin(x);\n   return test_asin_result;\n}\n'
        'double test_atan(double x) {\n   double test_atan_result;\n   test_atan_result = atan(x);\n   return test_atan_result;\n}\n'
        'double test_ceil(double x) {\n   double test_ceil_result;\n   test_ceil_result = ceil(x);\n   return test_ceil_result;\n}\n'
        'double test_cos(double x) {\n   double test_cos_result;\n   test_cos_result = cos(x);\n   return test_cos_result;\n}\n'
        'double test_cosh(double x) {\n   double test_cosh_result;\n   test_cosh_result = cosh(x);\n   return test_cosh_result;\n}\n'
        'double test_floor(double x) {\n   double test_floor_result;\n   test_floor_result = floor(x);\n   return test_floor_result;\n}\n'
        'double test_log(double x) {\n   double test_log_result;\n   test_log_result = log(x);\n   return test_log_result;\n}\n'
        'double test_ln(double x) {\n   double test_ln_result;\n   test_ln_result = log(x);\n   return test_ln_result;\n}\n'
        'double test_sin(double x) {\n   double test_sin_result;\n   test_sin_result = sin(x);\n   return test_sin_result;\n}\n'
        'double test_sinh(double x) {\n   double test_sinh_result;\n   test_sinh_result = sinh(x);\n   return test_sinh_result;\n}\n'
        'double test_sqrt(double x) {\n   double test_sqrt_result;\n   test_sqrt_result = sqrt(x);\n   return test_sqrt_result;\n}\n'
        'double test_tan(double x) {\n   double test_tan_result;\n   test_tan_result = tan(x);\n   return test_tan_result;\n}\n'
        'double test_tanh(double x) {\n   double test_tanh_result;\n   test_tanh_result = tanh(x);\n   return test_tanh_result;\n}\n'
    )
    assert result[1][0] == "file.h"
    assert result[1][1] == (
        '#ifndef PROJECT__FILE__H\n#define PROJECT__FILE__H\n'
        'double test_fabs(double x);\ndouble test_acos(double x);\n'
        'double test_asin(double x);\ndouble test_atan(double x);\n'
        'double test_ceil(double x);\ndouble test_cos(double x);\n'
        'double test_cosh(double x);\ndouble test_floor(double x);\n'
        'double test_log(double x);\ndouble test_ln(double x);\n'
        'double test_sin(double x);\ndouble test_sinh(double x);\n'
        'double test_sqrt(double x);\ndouble test_tan(double x);\n'
        'double test_tanh(double x);\n#endif\n'
    )
示例#7
0
def test_power_x_x1():
    assert (exp(x*ln(x))).nseries(x, n=4) == \
        1 + x*log(x) + x**2*log(x)**2/2 + x**3*log(x)**3/6 + O(x**4*log(x)**4)
示例#8
0
def test_mul_1():
    assert (x*ln(2 + x)).nseries(x, n=4) == x*log(2) + x**2/2 - x**3/8 + \
        x**4/24 + O(x**5)
    assert (x * ln(1 + x)).nseries(x,
                                   n=4) == x**2 - x**3 / 2 + x**4 / 3 + O(x**5)
示例#9
0
def test_mul_0():
    assert (x * ln(x)).nseries(x, n=5) == x * ln(x)
示例#10
0
def test_power_x_x1():
    assert (exp(x*ln(x))).nseries(x, n=4) == \
        1 + x*log(x) + x**2*log(x)**2/2 + x**3*log(x)**3/6 + O(x**4*log(x)**4)
示例#11
0
def test_mul_1():
    assert (x*ln(2 + x)).nseries(x, n=4) == x*log(2) + x**2/2 - x**3/8 + \
        x**4/24 + O(x**5)
    assert (x*ln(1 + x)).nseries(x, n=4) == x**2 - x**3/2 + x**4/3 + O(x**5)
示例#12
0
def test_mul_0():
    assert (x*ln(x)).nseries(x, n=5) == x*ln(x)
示例#13
0
def test_simple_2():
    assert O(2 * x) * x == O(x**2)
    assert O(2 * x) / x == O(1, x)
    assert O(2 * x) * x * exp(1 / x) == O(x**2 * exp(1 / x))
    assert (O(2 * x) * x * exp(1 / x) /
            ln(x)**3).expr == x**2 * exp(1 / x) * ln(x)**-3
示例#14
0
def test_lineintegral():
    c = Curve([E**t + 1, E**t - 1], (t, 0, ln(2)))
    assert line_integrate(x + y, c, [x, y]) == 3 * sqrt(2)
示例#15
0
def test_intrinsic_math_codegen():
    # not included: log10
    name_expr = [
        ("test_abs", 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)),
    ]
    result = codegen(name_expr, "F95", "file", header=False, empty=False)
    assert result[0][0] == "file.f90"
    expected = (
        'REAL*8 function test_abs(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_abs = Abs(x)\n'
        'end function\n'
        'REAL*8 function test_acos(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_acos = acos(x)\n'
        'end function\n'
        'REAL*8 function test_asin(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_asin = asin(x)\n'
        'end function\n'
        'REAL*8 function test_atan(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_atan = atan(x)\n'
        'end function\n'
        'REAL*8 function test_cos(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_cos = cos(x)\n'
        'end function\n'
        'REAL*8 function test_cosh(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_cosh = cosh(x)\n'
        'end function\n'
        'REAL*8 function test_log(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_log = log(x)\n'
        'end function\n'
        'REAL*8 function test_ln(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_ln = log(x)\n'
        'end function\n'
        'REAL*8 function test_sin(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_sin = sin(x)\n'
        'end function\n'
        'REAL*8 function test_sinh(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_sinh = sinh(x)\n'
        'end function\n'
        'REAL*8 function test_sqrt(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_sqrt = sqrt(x)\n'
        'end function\n'
        'REAL*8 function test_tan(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_tan = tan(x)\n'
        'end function\n'
        'REAL*8 function test_tanh(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'test_tanh = tanh(x)\n'
        'end function\n'
    )
    assert result[0][1] == expected

    assert result[1][0] == "file.h"
    expected = (
        'interface\n'
        'REAL*8 function test_abs(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_acos(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_asin(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_atan(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_cos(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_cosh(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_log(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_ln(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_sin(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_sinh(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_sqrt(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_tan(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test_tanh(x)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'end function\n'
        'end interface\n'
    )
    assert result[1][1] == expected