Example #1
0
def test_specfun():
    n = Symbol('n')
    for f in [besselj, bessely, besseli, besselk]:
        assert octave_code(f(n, x)) == f.__name__ + '(n, x)'
    assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)'
    assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)'
    assert octave_code(airyai(x)) == 'airy(0, x)'
    assert octave_code(airyaiprime(x)) == 'airy(1, x)'
    assert octave_code(airybi(x)) == 'airy(2, x)'
    assert octave_code(airybiprime(x)) == 'airy(3, x)'
    assert octave_code(jn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2'
    assert octave_code(yn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'
Example #2
0
def test_trigfun():
    for f in (
            sin,
            cos,
            tan,
            cot,
            sec,
            csc,
            asin,
            acos,
            acot,
            atan,
            asec,
            acsc,
            sinh,
            cosh,
            tanh,
            coth,
            csch,
            sech,
            asinh,
            acosh,
            atanh,
            acoth,
            asech,
            acsch,
    ):
        assert octave_code(f(x) == f.__name__ + "(x)")
Example #3
0
def test_specfun():
    n = Symbol('n')
    for f in [besselj, bessely, besseli, besselk]:
        assert octave_code(f(n, x)) == f.__name__ + '(n, x)'
    assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)'
    assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)'
    assert octave_code(airyai(x)) == 'airy(0, x)'
    assert octave_code(airyaiprime(x)) == 'airy(1, x)'
    assert octave_code(airybi(x)) == 'airy(2, x)'
    assert octave_code(airybiprime(x)) == 'airy(3, x)'
    assert octave_code(jn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2'
    assert octave_code(yn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'
Example #4
0
def expressionToCode(expression, language):
    '''Converts a SymPy Expression to a line of code in the target language'''
    if (language == "python"):
        return sympy.pycode(expression)
    elif (language == "javascript" or language == "typescript"):
        return sympy.jscode(expression)
    elif (language == "c"):
        return sympy.ccode(expression)
    elif (language == "cpp"):
        return sympy.cxxcode(expression)
    elif (language == "r"):
        return sympy.rcode(expression)
    elif (language == "fortran"):
        return sympy.fcode(expression)
    elif (language == "mathematica"):
        return sympy.mathematica_code(expression)
    elif (language == "matlab" or language == "octave"):
        return sympy.octave_code(expression)
    elif (language == "rust"):
        return sympy.rust_code(expression)
Example #5
0
def test_specfun():
    n = Symbol('n')
    for f in [besselj, bessely, besseli, besselk]:
        assert octave_code(f(n, x)) == f.__name__ + '(n, x)'
    assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)'
    assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)'
    assert octave_code(airyai(x)) == 'airy(0, x)'
    assert octave_code(airyaiprime(x)) == 'airy(1, x)'
    assert octave_code(airybi(x)) == 'airy(2, x)'
    assert octave_code(airybiprime(x)) == 'airy(3, x)'
    assert octave_code(uppergamma(n, x)) == 'gammainc(x, n, \'upper\')'
    assert octave_code(lowergamma(n, x)) == 'gammainc(x, n, \'lower\')'
    assert octave_code(jn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2'
    assert octave_code(yn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'
    assert octave_code(LambertW(x)) == 'lambertw(x)'
    assert octave_code(LambertW(x, n)) == 'lambertw(n, x)'
Example #6
0
def test_automatic_rewrite():
    assert octave_code(Li(x)) == 'logint(x) - logint(2)'
    assert octave_code(erf2(x, y)) == '-erf(x) + erf(y)'
Example #7
0
def test_zeta_printing_issue_14820():
    assert octave_code(zeta(x)) == 'zeta(x)'
    assert octave_code(zeta(x, y)) == '% Not supported in Octave:\n% zeta\nzeta(x, y)'
Example #8
0
def test_zeta_printing_issue_14820():
    assert octave_code(zeta(x)) == 'zeta(x)'
    assert octave_code(zeta(x, y)) == '% Not supported in Octave:\n% zeta\nzeta(x, y)'
Example #9
0
def test_specfun():
    n = Symbol('n')
    for f in [besselj, bessely, besseli, besselk]:
        assert octave_code(f(n, x)) == f.__name__ + '(n, x)'
    for f in (erfc, erfi, erf, erfinv, erfcinv, fresnelc, fresnels, gamma):
        assert octave_code(f(x)) == f.__name__ + '(x)'
    assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)'
    assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)'
    assert octave_code(airyai(x)) == 'airy(0, x)'
    assert octave_code(airyaiprime(x)) == 'airy(1, x)'
    assert octave_code(airybi(x)) == 'airy(2, x)'
    assert octave_code(airybiprime(x)) == 'airy(3, x)'
    assert octave_code(uppergamma(n, x)) == 'gammainc(x, n, \'upper\')'
    assert octave_code(lowergamma(n, x)) == 'gammainc(x, n, \'lower\')'
    assert octave_code(jn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2'
    assert octave_code(yn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'
    assert octave_code(LambertW(x)) == 'lambertw(x)'
    assert octave_code(LambertW(x, n)) == 'lambertw(n, x)'
Example #10
0
def test_trigfun():
    for f in (sin, cos, tan, cot, sec, csc, asin, acos, acot, atan, asec, acsc,
              sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, acoth,
              asech, acsch):
        assert octave_code(f(x) == f.__name__ + '(x)')
Example #11
0
def test_specfun():
    n = Symbol("n")
    for f in [besselj, bessely, besseli, besselk]:
        assert octave_code(f(n, x)) == f.__name__ + "(n, x)"
    for f in (erfc, erfi, erf, erfinv, erfcinv, fresnelc, fresnels, gamma):
        assert octave_code(f(x)) == f.__name__ + "(x)"
    assert octave_code(hankel1(n, x)) == "besselh(n, 1, x)"
    assert octave_code(hankel2(n, x)) == "besselh(n, 2, x)"
    assert octave_code(airyai(x)) == "airy(0, x)"
    assert octave_code(airyaiprime(x)) == "airy(1, x)"
    assert octave_code(airybi(x)) == "airy(2, x)"
    assert octave_code(airybiprime(x)) == "airy(3, x)"
    assert octave_code(uppergamma(n,
                                  x)) == "(gammainc(x, n, 'upper').*gamma(n))"
    assert octave_code(lowergamma(n, x)) == "(gammainc(x, n).*gamma(n))"
    assert octave_code(z**lowergamma(n, x)) == "z.^(gammainc(x, n).*gamma(n))"
    assert octave_code(jn(
        n, x)) == "sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2"
    assert octave_code(yn(
        n, x)) == "sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2"
    assert octave_code(LambertW(x)) == "lambertw(x)"
    assert octave_code(LambertW(x, n)) == "lambertw(n, x)"
Example #12
0
    lambda expr, **kw: sp.cxxcode(expr, user_functions=_ufns, **kw),
    'rust':
    lambda expr, **kw: sp.rust_code(expr, user_functions=_ufns, **kw),
    'fortran':
    lambda expr, **kw: sp.fcode(expr, standard=95, user_functions=_ufns, **kw),
    'js':
    lambda expr, **kw: sp.jscode(expr, user_functions=_ufns, **kw),
    'r':
    lambda expr, **kw: sp.rcode(expr, user_functions=_ufns, **kw),
    'julia':
    lambda expr, **kw: sp.julia_code(expr, user_functions=_ufns, **kw),
    'mathematica':
    lambda expr, assign_to=None, **kw: sp.mathematica_code(
        expr, user_functions=_ufns, **kw),
    'octave':
    lambda expr, **kw: sp.octave_code(expr, user_functions=_ufns, **kw),
}


def to_code(syexpr, dialect, assign_to='y', **kw):
    '''
    Shorthand for converting the resulting Sympy expression to code in various languages.


    Args:

        dialect (str): The target language. Can be one of:
            'c', 'cxx', 'rust', 'fortran', 'js', 'r', 'julia', 'mathematica', 'octave'.

    Kwargs: