Пример #1
0
def get_math_macros():
    """ Returns a dictionary with math-related macros from math.h/cmath

    Note that these macros are not strictly required by the C/C++-standard.
    For MSVC they are enabled by defining "_USE_MATH_DEFINES" (preferably
    via a compilation flag).

    Returns
    =======

    Dictionary mapping sympy expressions to strings (macro names)

    """
    from sympy.codegen.cfunctions import log2, Sqrt
    from sympy.functions.elementary.exponential import log
    from sympy.functions.elementary.miscellaneous import sqrt

    return {
        S.Exp1: 'M_E',
        log2(S.Exp1): 'M_LOG2E',
        1/log(2): 'M_LOG2E',
        log(2): 'M_LN2',
        log(10): 'M_LN10',
        S.Pi: 'M_PI',
        S.Pi/2: 'M_PI_2',
        S.Pi/4: 'M_PI_4',
        1/S.Pi: 'M_1_PI',
        2/S.Pi: 'M_2_PI',
        2/sqrt(S.Pi): 'M_2_SQRTPI',
        2/Sqrt(S.Pi): 'M_2_SQRTPI',
        sqrt(2): 'M_SQRT2',
        Sqrt(2): 'M_SQRT2',
        1/sqrt(2): 'M_SQRT1_2',
        1/Sqrt(2): 'M_SQRT1_2'
    }
Пример #2
0
def test_Sqrt():
    x = Symbol('x')

    # Expand
    assert Sqrt(x).expand(func=True) - x**S.Half == 0

    # Diff
    assert Sqrt(42 * x).diff(x) - 42 * (42 * x)**(S.Half - 1) / 2 == 0
    assert Sqrt(42 * x).diff(x) - Sqrt(42 * x).expand(func=True).diff(x) == 0
Пример #3
0
def test_Sqrt():
    x = Symbol('x')

    # Expand
    assert Sqrt(x).expand(func=True) - x**Rational(1, 2) == 0

    # Diff
    assert Sqrt(42 * x).diff(x) - 42 * (42 * x)**(Rational(1, 2) - 1) / 2 == 0
    assert Sqrt(42 * x).diff(x) - Sqrt(42 * x).expand(func=True).diff(x) == 0
Пример #4
0
def test_ccode_math_macros():
    assert ccode(z + exp(1)) == 'z + M_E'
    assert ccode(z + log2(exp(1))) == 'z + M_LOG2E'
    assert ccode(z + 1 / log(2)) == 'z + M_LOG2E'
    assert ccode(z + log(2)) == 'z + M_LN2'
    assert ccode(z + log(10)) == 'z + M_LN10'
    assert ccode(z + pi) == 'z + M_PI'
    assert ccode(z + pi / 2) == 'z + M_PI_2'
    assert ccode(z + pi / 4) == 'z + M_PI_4'
    assert ccode(z + 1 / pi) == 'z + M_1_PI'
    assert ccode(z + 2 / pi) == 'z + M_2_PI'
    assert ccode(z + 2 / sqrt(pi)) == 'z + M_2_SQRTPI'
    assert ccode(z + 2 / Sqrt(pi)) == 'z + M_2_SQRTPI'
    assert ccode(z + sqrt(2)) == 'z + M_SQRT2'
    assert ccode(z + Sqrt(2)) == 'z + M_SQRT2'
    assert ccode(z + 1 / sqrt(2)) == 'z + M_SQRT1_2'
    assert ccode(z + 1 / Sqrt(2)) == 'z + M_SQRT1_2'
Пример #5
0
def test_ccode_math_macros():
    assert ccode(z + exp(1)) == "z + M_E"
    assert ccode(z + log2(exp(1))) == "z + M_LOG2E"
    assert ccode(z + 1 / log(2)) == "z + M_LOG2E"
    assert ccode(z + log(2)) == "z + M_LN2"
    assert ccode(z + log(10)) == "z + M_LN10"
    assert ccode(z + pi) == "z + M_PI"
    assert ccode(z + pi / 2) == "z + M_PI_2"
    assert ccode(z + pi / 4) == "z + M_PI_4"
    assert ccode(z + 1 / pi) == "z + M_1_PI"
    assert ccode(z + 2 / pi) == "z + M_2_PI"
    assert ccode(z + 2 / sqrt(pi)) == "z + M_2_SQRTPI"
    assert ccode(z + 2 / Sqrt(pi)) == "z + M_2_SQRTPI"
    assert ccode(z + sqrt(2)) == "z + M_SQRT2"
    assert ccode(z + Sqrt(2)) == "z + M_SQRT2"
    assert ccode(z + 1 / sqrt(2)) == "z + M_SQRT1_2"
    assert ccode(z + 1 / Sqrt(2)) == "z + M_SQRT1_2"
Пример #6
0
def test_get_math_macros():
    macros = get_math_macros()
    assert macros[exp(1)] == 'M_E'
    assert macros[1 / Sqrt(2)] == 'M_SQRT1_2'
Пример #7
0
def test_Sqrt():
    if not np:
        skip("NumPy not installed")
    assert abs(lambdify((a,), Sqrt(a), 'numpy')(4) - 2) < 1e-16