from sympy import * #%% create a list of test cases for the extended math library for automatic testing x = S(1)/2 print("S x = %.30f;" % x) print("S tol = 1e-12;") print("REQUIRE(fabs(sec(x) - %.30f) < tol);" % sec(x)) print("REQUIRE(fabs(cot(x) - %.30f) < tol);" % cot(x)) print("REQUIRE(fabs(csc(x) - %.30f) < tol);" % csc(x)) print("REQUIRE(fabs(asec(3*x) - %.30f) < tol);" % asec(3*x)) print("REQUIRE(fabs(acot(x) - %.30f) < tol);" % acot(x)) print("REQUIRE(fabs(acsc(3*x) - %.30f) < tol);" % acsc(3*x)) print("REQUIRE(fabs(sech(x) - %.30f) < tol);" % sech(x)) print("REQUIRE(fabs(coth(x) - %.30f) < tol);" % coth(x)) print("REQUIRE(fabs(csch(x) - %.30f) < tol);" % csch(x)) print("REQUIRE(fabs(asech(x) - %.30f) < tol);" % asech(x)) print("REQUIRE(fabs(acoth(3*x) - %.30f) < tol);" % acoth(3*x)) print("REQUIRE(fabs(acsch(x) - %.30f) < tol);" % acsch(x)) print("REQUIRE(fabs(exp10(x) - %.30f) < tol);" % 10**x) print("REQUIRE(fabs(sign(x) - %.30f) < tol);" % sign(x)) print("REQUIRE(fabs(sign(-x) - %.30f) < tol);" % sign(-x)) print("REQUIRE(fabs(heaviside(x) - %.30f) < tol);" % 1) print("REQUIRE(fabs(heaviside(0.) - %.30f) < tol);" % 1) print("REQUIRE(fabs(heaviside(-x) - %.30f) < tol);" % 0) #%% Create list x, xf = symbols("x xf", real=True) # xf = S(1)/2 print("S xf = 0.500000000000000000000000000000;") print("D x(xf); x.diff(0);") print("S tol = 1e-12;") expr = (cos(x), sin(x), tan(x), acos(x), asin(x), atan(x), sec(x), cot(x), csc(x), asec(3*x), acot(x), acsc(3*x), cosh(x), sinh(x), atanh(x), asinh(x), acosh(3*x), atanh(x), sech(x), coth(x), csch(x), asech(x), acoth(3*x), acsch(x), exp(x), pow(2, x), pow(10, x), log(x), log(x)/log(2), log(x)/log(10), sqrt(x), cbrt(x), abs(x), pow(x, S(1)/7), pow(S(1)/7, x)) for i in range(len(expr)): f = expr[i] df = f.diff(x) print("CHECK(fabs((%s).d(0) - (%s)) < tol);" % (ccode(f), ccode(df.subs(x, xf)))) #%% Generate small test functions expr2 = (x**(x**x), log(sin(x)), pow(x, 2)/(log(x)), cbrt(sin(x))) for i in range(len(expr2)): f = expr2[i] df = f.diff(x) print("CHECK(fabs((%s).d(0) - (%s)) < tol);" % (ccode(f), ccode(df.subs(x, xf)))) #%% Generate test functions x, y, z = symbols("x y z", real=True) print(" ") print("fct 1 :") fct1 = (cos(x) + sqrt(x) + cbrt(sin(x)) + exp(-x*x)/log(x) + atanh(5*x) - 2*csc(x)) print(ccode(fct1)) print(ccode(fct1.diff(x))) print(" ") print("fct 2 :") fct2 = (cos(x) + sqrt(y) + cbrt(sin(z))*tan(y) + 2*sqrt(x)*sqrt(y)/log(sin(z)) + exp(-x*x - y*y - z*z)/(log(x)*log(y)*log(z)) + atanh(5*x)*2*csc(y)) print("fct2(x,y,z) =", ccode(fct2)) print("dfct2/dx =", ccode(fct2.diff(x))) print("dfct2/dy =", ccode(fct2.diff(y))) print("dfct2/dz =", ccode(fct2.diff(z))) fct2.subs(((x,.11),(y,.12),(z,.13))) fct2.subs(((x,.11),(y,.12),(z,.14))) (fct2.subs(((x,.11),(y,.12),(z,.14)))-fct2.subs(((x,.11),(y,.12),(z,.13))))/.01 fct2.diff(z).subs(((x,.11),(y,.12),(z,.13)))