def test_C99CodePrinter_custom_type(): # We will look at __float128 (new in glibc 2.26) f128 = FloatType('_Float128', float128.nbits, float128.nmant, float128.nexp) p128 = C99CodePrinter( dict(type_aliases={real: f128}, type_literal_suffixes={f128: 'Q'}, type_func_suffixes={f128: 'f128'}, type_math_macro_suffixes={ real: 'f128', f128: 'f128' }, type_macros={f128: ('__STDC_WANT_IEC_60559_TYPES_EXT__', )})) assert p128.doprint(x) == 'x' assert not p128.headers assert not p128.libraries assert not p128.macros assert p128.doprint(2.0) == '2.0Q' assert not p128.headers assert not p128.libraries assert p128.macros == {'__STDC_WANT_IEC_60559_TYPES_EXT__'} assert p128.doprint(Rational(1, 2)) == '1.0Q/2.0Q' assert p128.doprint(sin(x)) == 'sinf128(x)' assert p128.doprint(cos(2., evaluate=False)) == 'cosf128(2.0Q)' var5 = Variable(x, {value_const}, f128) dcl5a = Declaration(var5) assert ccode(dcl5a) == 'const _Float128 x' dcl5b = Declaration(var5, pi) assert p128.doprint(dcl5b) == 'const _Float128 x = M_PIf128' dcl5c = Declaration(var5, Catalan.evalf(38)) assert p128.doprint(dcl5c) == 'const _Float128 x = %sQ' % Catalan.evalf( f128.decimal_dig)
def test_julia_noninline(): source = julia_code((x+y)/Catalan, assign_to='me', inline=False) expected = ( "const Catalan = %s\n" "me = (x + y)/Catalan" ) % Catalan.evalf(17) assert source == expected
def test_octave_noninline(): source = mcode((x+y)/Catalan, assign_to='me', inline=False) expected = ( "Catalan = %s;\n" "me = (x + y)/Catalan;" ) % Catalan.evalf(17) assert source == expected
def test_m_numbersymbol(): name_expr = ("test", pi**Catalan) (result, ) = codegen(name_expr, "Octave", header=False, empty=False) source = result[1] expected = ("function out1 = test()\n" " out1 = pi^%s;\n" "end\n") % Catalan.evalf(17) assert source == expected
def test_jscode_constants_other(): assert jscode( 2 * GoldenRatio ) == "var GoldenRatio = %s;\n2*GoldenRatio" % GoldenRatio.evalf(17) assert jscode( 2 * Catalan) == "var Catalan = %s;\n2*Catalan" % Catalan.evalf(17) assert jscode( 2 * EulerGamma ) == "var EulerGamma = %s;\n2*EulerGamma" % EulerGamma.evalf(17)
def test_constants_other(): assert rust_code( 2 * GoldenRatio ) == "const GoldenRatio: f64 = %s;\n2*GoldenRatio" % GoldenRatio.evalf(17) assert rust_code( 2 * Catalan) == "const Catalan: f64 = %s;\n2*Catalan" % Catalan.evalf(17) assert rust_code( 2 * EulerGamma ) == "const EulerGamma: f64 = %s;\n2*EulerGamma" % EulerGamma.evalf(17)
def test_numbersymbol(): name_expr = ("test", pi**Catalan) result, = codegen(name_expr, "Rust", header=False, empty=False) source = result[1] expected = ("fn test() -> f64 {\n" " const Catalan: f64 = %s;\n" " let out1 = PI.powf(Catalan);\n" " out1\n" "}\n") % Catalan.evalf(17) assert source == expected
def test_m_numbersymbol(): name_expr = ("test", pi**Catalan) result, = codegen(name_expr, "Octave", header=False, empty=False) source = result[1] expected = ( "function out1 = test()\n" " out1 = pi^%s;\n" "end\n" ) % Catalan.evalf(17) assert source == expected
def test_ccode_constants_other(): assert ccode( 2 * GoldenRatio ) == "const double GoldenRatio = %s;\n2*GoldenRatio" % GoldenRatio.evalf(17) assert ccode( 2 * Catalan ) == "const double Catalan = %s;\n2*Catalan" % Catalan.evalf(17) assert ccode( 2 * EulerGamma ) == "const double EulerGamma = %s;\n2*EulerGamma" % EulerGamma.evalf(17)
def test_numbersymbol(): name_expr = ("test", pi**Catalan) result, = codegen(name_expr, "Rust", header=False, empty=False) source = result[1] expected = ( "fn test() -> f64 {\n" " const Catalan: f64 = %s;\n" " let out1 = PI.powf(Catalan);\n" " out1\n" "}\n" ) % Catalan.evalf(17) assert source == expected
def test_ccode_inline_function(): x = symbols("x") g = implemented_function("g", Lambda(x, 2 * x)) assert ccode(g(x)) == "2*x" g = implemented_function("g", Lambda(x, 2 * x / Catalan)) assert ccode(g(x)) == "const double Catalan = %s;\n2*x/Catalan" % Catalan.evalf(17) A = IndexedBase("A") i = Idx("i", symbols("n", integer=True)) g = implemented_function("g", Lambda(x, x * (1 + x) * (2 + x))) assert ccode(g(A[i]), assign_to=A[i]) == ( "for (int i=0; i<n; i++){\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}" )
def test_jscode_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2 * x)) assert jscode(g(x)) == "2*x" g = implemented_function('g', Lambda(x, 2 * x / Catalan)) assert jscode(g(x)) == "var Catalan = %s;\n2*x/Catalan" % Catalan.evalf(17) A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x * (1 + x) * (2 + x))) assert jscode(g(A[i]), assign_to=A[i]) == ("for (var i=0; i<n; i++){\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}")
def test_C99CodePrinter_custom_type(): # We will look at __float128 (new in glibc 2.26) f128 = FloatType('_Float128', float128.nbits, float128.nmant, float128.nexp) p128 = C99CodePrinter(dict( type_aliases={real: f128}, type_literal_suffixes={f128: 'Q'}, type_func_suffixes={f128: 'f128'}, type_math_macro_suffixes={ real: 'f128', f128: 'f128' }, type_macros={ f128: ('__STDC_WANT_IEC_60559_TYPES_EXT__',) } )) assert p128.doprint(x) == 'x' assert not p128.headers assert not p128.libraries assert not p128.macros assert p128.doprint(2.0) == '2.0Q' assert not p128.headers assert not p128.libraries assert p128.macros == {'__STDC_WANT_IEC_60559_TYPES_EXT__'} assert p128.doprint(Rational(1, 2)) == '1.0Q/2.0Q' assert p128.doprint(sin(x)) == 'sinf128(x)' assert p128.doprint(cos(2., evaluate=False)) == 'cosf128(2.0Q)' var5 = Variable(x, f128, attrs={value_const}) dcl5a = Declaration(var5) assert ccode(dcl5a) == 'const _Float128 x' var5b = Variable(x, f128, pi, attrs={value_const}) dcl5b = Declaration(var5b) assert p128.doprint(dcl5b) == 'const _Float128 x = M_PIf128' var5b = Variable(x, f128, value=Catalan.evalf(38), attrs={value_const}) dcl5c = Declaration(var5b) assert p128.doprint(dcl5c) == 'const _Float128 x = %sQ' % Catalan.evalf(f128.decimal_dig)
def test_jscode_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2*x)) assert jscode(g(x)) == "2*x" g = implemented_function('g', Lambda(x, 2*x/Catalan)) assert jscode(g(x)) == "var Catalan = %s;\n2*x/Catalan" % Catalan.evalf(17) A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x*(1 + x)*(2 + x))) assert jscode(g(A[i]), assign_to=A[i]) == ( "for (var i=0; i<n; i++){\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}" )
def test_C99CodePrinter_custom_type(): # We will look at __float128 (new in glibc 2.26) f128 = FloatType("_Float128", float128.nbits, float128.nmant, float128.nexp) p128 = C99CodePrinter( dict( type_aliases={real: f128}, type_literal_suffixes={f128: "Q"}, type_func_suffixes={f128: "f128"}, type_math_macro_suffixes={real: "f128", f128: "f128"}, type_macros={f128: ("__STDC_WANT_IEC_60559_TYPES_EXT__",)}, ) ) assert p128.doprint(x) == "x" assert not p128.headers assert not p128.libraries assert not p128.macros assert p128.doprint(2.0) == "2.0Q" assert not p128.headers assert not p128.libraries assert p128.macros == {"__STDC_WANT_IEC_60559_TYPES_EXT__"} assert p128.doprint(Rational(1, 2)) == "1.0Q/2.0Q" assert p128.doprint(sin(x)) == "sinf128(x)" assert p128.doprint(cos(2.0, evaluate=False)) == "cosf128(2.0Q)" var5 = Variable(x, f128, attrs={value_const}) dcl5a = Declaration(var5) assert ccode(dcl5a) == "const _Float128 x" var5b = Variable(x, f128, pi, attrs={value_const}) dcl5b = Declaration(var5b) assert p128.doprint(dcl5b) == "const _Float128 x = M_PIf128" var5b = Variable(x, f128, value=Catalan.evalf(38), attrs={value_const}) dcl5c = Declaration(var5b) assert p128.doprint(dcl5c) == "const _Float128 x = %sQ" % Catalan.evalf( f128.decimal_dig )
def test_fcode_NumberSymbol(): prec = 17 p = FCodePrinter() assert fcode( Catalan ) == ' parameter (Catalan = %sd0)\n Catalan' % Catalan.evalf( prec) assert fcode( EulerGamma ) == ' parameter (EulerGamma = %sd0)\n EulerGamma' % EulerGamma.evalf( prec) assert fcode(E) == ' parameter (E = %sd0)\n E' % E.evalf(prec) assert fcode( GoldenRatio ) == ' parameter (GoldenRatio = %sd0)\n GoldenRatio' % GoldenRatio.evalf( prec) assert fcode( pi) == ' parameter (pi = %sd0)\n pi' % pi.evalf(prec) assert fcode( pi, precision=5) == ' parameter (pi = %sd0)\n pi' % pi.evalf(5) assert fcode(Catalan, human=False) == ({(Catalan, p._print(Catalan.evalf(prec)))}, set(), ' Catalan') assert fcode(EulerGamma, human=False) == ({ (EulerGamma, p._print(EulerGamma.evalf(prec))) }, set(), ' EulerGamma') assert fcode(E, human=False) == ({(E, p._print(E.evalf(prec)))}, set(), ' E') assert fcode(GoldenRatio, human=False) == ({ (GoldenRatio, p._print(GoldenRatio.evalf(prec))) }, set(), ' GoldenRatio') assert fcode(pi, human=False) == ({(pi, p._print(pi.evalf(prec)))}, set(), ' pi') assert fcode(pi, precision=5, human=False) == ({(pi, p._print(pi.evalf(5)))}, set(), ' pi')
def test_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2*x)) assert rust_code(g(x)) == "2*x" g = implemented_function('g', Lambda(x, 2*x/Catalan)) assert rust_code(g(x)) == ( "const Catalan: f64 = %s;\n2*x/Catalan" % Catalan.evalf(17)) A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x*(1 + x)*(2 + x))) assert rust_code(g(A[i]), assign_to=A[i]) == ( "for i in 0..n {\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}")
def test_numbersymbol_inline(): # FIXME: how to pass inline to the RustCodePrinter? name_expr = ("test", [pi**Catalan, EulerGamma]) result, = codegen(name_expr, "Rust", header=False, empty=False, inline=True) source = result[1] expected = ( "fn test() -> (f64, f64) {\n" " const Catalan: f64 = %s;\n" " const EulerGamma: f64 = %s;\n" " let out1 = PI.powf(Catalan);\n" " let out2 = EulerGamma);\n" " (out1, out2)\n" "}\n" ) % (Catalan.evalf(17), EulerGamma.evalf(17)) assert source == expected
def test_constants_other(): assert rust_code(2*GoldenRatio) == "const GoldenRatio: f64 = %s;\n2*GoldenRatio" % GoldenRatio.evalf(17) assert rust_code( 2*Catalan) == "const Catalan: f64 = %s;\n2*Catalan" % Catalan.evalf(17) assert rust_code(2*EulerGamma) == "const EulerGamma: f64 = %s;\n2*EulerGamma" % EulerGamma.evalf(17)
def test_ccode_constants_other(): assert ccode(2*GoldenRatio) == "const double GoldenRatio = %s;\n2*GoldenRatio" % GoldenRatio.evalf(17) assert ccode( 2*Catalan) == "const double Catalan = %s;\n2*Catalan" % Catalan.evalf(17) assert ccode(2*EulerGamma) == "const double EulerGamma = %s;\n2*EulerGamma" % EulerGamma.evalf(17)
def test_constants_other(): assert mcode(2*GoldenRatio) == "2*(1+sqrt(5))/2" assert mcode(2*Catalan) == "2*%s" % Catalan.evalf(17) assert mcode(2*EulerGamma) == "2*%s" % EulerGamma.evalf(17)
def test_constants_other(): assert mcode(2 * GoldenRatio) == "2*(1+sqrt(5))/2" assert mcode(2 * Catalan) == "2*%s" % Catalan.evalf(17) assert mcode(2 * EulerGamma) == "2*%s" % EulerGamma.evalf(17)
def test_jscode_constants_other(): assert jscode( 2*GoldenRatio) == "var GoldenRatio = %s;\n2*GoldenRatio" % GoldenRatio.evalf(17) assert jscode(2*Catalan) == "var Catalan = %s;\n2*Catalan" % Catalan.evalf(17) assert jscode( 2*EulerGamma) == "var EulerGamma = %s;\n2*EulerGamma" % EulerGamma.evalf(17)