def main(): order = 2 V, tmp_syms, _ = vandermonde(order) print("Vandermonde matrix of order 2 in 1 dimension") pprint(V) print('-' * 79) print( "Computing the determinant and comparing to \sum_{0<i<j<=3}(a_j - a_i)" ) det_sum = 1 for j in range(order + 1): for i in range(j): det_sum *= (tmp_syms[j][0] - tmp_syms[i][0]) print(""" det(V) = %(det)s \sum = %(sum)s = %(sum_expand)s """ % { "det": V.det(), "sum": det_sum, "sum_expand": det_sum.expand(), }) print('-' * 79) print("Polynomial fitting with a Vandermonde Matrix:") x, y, z = symbols('x,y,z') points = [(0, 3), (1, 2), (2, 3)] print(""" Quadratic function, represented by 3 points: points = %(pts)s f = %(f)s """ % { "pts": points, "f": gen_poly(points, 2, [x]), }) points = [(0, 1, 1), (1, 0, 0), (1, 1, 0), (Rational(1, 2), 0, 0), (0, Rational(1, 2), 0), (Rational(1, 2), Rational(1, 2), 0)] print(""" 2D Quadratic function, represented by 6 points: points = %(pts)s f = %(f)s """ % { "pts": points, "f": gen_poly(points, 2, [x, y]), }) points = [(0, 1, 1, 1), (1, 1, 0, 0), (1, 0, 1, 0), (1, 1, 1, 1)] print(""" 3D linear function, represented by 4 points: points = %(pts)s f = %(f)s """ % { "pts": points, "f": gen_poly(points, 1, [x, y, z]), })
def main(): print(__doc__) x = symbols('x') # a numpy array we can apply the ufuncs to grid = np.linspace(-1, 1, 1000) # set mpmath precision to 20 significant numbers for verification mpmath.mp.dps = 20 print("Compiling legendre ufuncs and checking results:") # Let's also plot the ufunc's we generate for n in range(6): # Setup the Diofant expression to ufuncify expr = legendre(n, x) print("The polynomial of degree %i is" % n) pprint(expr) # This is where the magic happens: binary_poly = ufuncify(x, expr) # It's now ready for use with numpy arrays polyvector = binary_poly(grid) # let's check the values against mpmath's legendre function maxdiff = 0 for j in range(len(grid)): precise_val = mpmath.legendre(n, grid[j]) diff = abs(polyvector[j] - precise_val) if diff > maxdiff: maxdiff = diff print("The largest error in applied ufunc was %e" % maxdiff) assert maxdiff < 1e-14 # We can also attach the autowrapped legendre polynomial to a diofant # function and plot values as they are calculated by the binary function plot1 = plt.pyplot.plot(grid, polyvector, hold=True) print( "Here's a plot with values calculated by the wrapped binary functions") plt.pyplot.show()
def main(): L = [1] for i in range(1, 100): L.append(cos(i * x)) L.append(sin(i * x)) # next 2 lines equivalent to L = l2_gram_schmidt(L, (x, -pi, pi)), but faster: L[0] /= sqrt(2) L = [f / sqrt(pi) for f in L] f = series(L) print("Fourier series of the step function") pprint(f) x0 = msolve(f.diff(x), x) print("x-value of the maximum:", x0) max = f.subs(x, x0).evalf() print("y-value of the maximum:", max) g = max * pi / 2 print("Wilbraham-Gibbs constant :", g.evalf()) print("Wilbraham-Gibbs constant (exact):", Integral(sin(x) / x, (x, 0, pi)).evalf())
def main(): t = ReferenceSimplex(2) fe = Lagrange(2, 2) u = 0 # compute u = sum_i u_i N_i us = [] for i in range(0, fe.nbf()): ui = Symbol("u_%d" % i) us.append(ui) u += ui * fe.N[i] J = zeros(fe.nbf()) for i in range(0, fe.nbf()): Fi = u * fe.N[i] print(Fi) for j in range(0, fe.nbf()): uj = us[j] integrands = diff(Fi, uj) print(integrands) J[j, i] = t.integrate(integrands) pprint(J)
def main(): print("Initial metric:") pprint(gdd) print("-" * 40) print("Christoffel symbols:") pprint_Gamma_udd(0, 1, 0) pprint_Gamma_udd(0, 0, 1) print() pprint_Gamma_udd(1, 0, 0) pprint_Gamma_udd(1, 1, 1) pprint_Gamma_udd(1, 2, 2) pprint_Gamma_udd(1, 3, 3) print() pprint_Gamma_udd(2, 2, 1) pprint_Gamma_udd(2, 1, 2) pprint_Gamma_udd(2, 3, 3) print() pprint_Gamma_udd(3, 2, 3) pprint_Gamma_udd(3, 3, 2) pprint_Gamma_udd(3, 1, 3) pprint_Gamma_udd(3, 3, 1) print("-" * 40) print("Ricci tensor:") pprint_Rmn_dd(0, 0) e = Rmn.dd(1, 1) pprint_Rmn_dd(1, 1) pprint_Rmn_dd(2, 2) pprint_Rmn_dd(3, 3) print("-" * 40) print("Solve Einstein's equations:") e = e.subs(nu(r), -lam(r)).doit() l = dsolve(e, lam(r)) pprint(l) lamsol = solve(l, lam(r))[0] metric = gdd.subs(lam(r), lamsol).subs(nu(r), -lamsol) # .combine() print("metric:") pprint(metric)
def main(): r, phi, theta = symbols("r,phi,theta") Xi = Function('Xi') R, Phi, Theta, u = map(Function, ['R', 'Phi', 'Theta', 'u']) C1, C2 = symbols('C1,C2') pprint( "Separation of variables in Laplace equation in spherical coordinates") pprint("Laplace equation in spherical coordinates:") eq = Eq( D(Xi(r, phi, theta), r, 2) + 2 / r * D(Xi(r, phi, theta), r) + 1 / (r**2 * sin(phi)**2) * D(Xi(r, phi, theta), theta, 2) + cos(phi) / (r**2 * sin(phi)) * D(Xi(r, phi, theta), phi) + 1 / r**2 * D(Xi(r, phi, theta), phi, 2)) pprint(eq) pprint("We can either separate this equation in regards with variable r:") res_r = pde_separate(eq, Xi(r, phi, theta), [R(r), u(phi, theta)]) pprint(res_r) pprint("Or separate it in regards of theta:") res_theta = pde_separate(eq, Xi(r, phi, theta), [Theta(theta), u(r, phi)]) pprint(res_theta) res_phi = pde_separate(eq, Xi(r, phi, theta), [Phi(phi), u(r, theta)]) pprint("But we cannot separate it in regards of variable phi: ") pprint("Result: %s" % res_phi) pprint("\n\nSo let's make theta dependent part equal with -C1:") eq_theta = Eq(res_theta[0], -C1) pprint(eq_theta) pprint("\nThis also means that second part is also equal to -C1:") eq_left = Eq(res_theta[1], -C1) pprint(eq_left) pprint("\nLets try to separate phi again :)") res_theta = pde_separate(eq_left, u(r, phi), [Phi(phi), R(r)]) pprint("\nThis time it is successful:") pprint(res_theta) pprint("\n\nSo our final equations with separated variables are:") pprint(eq_theta) pprint(Eq(res_theta[0], C2)) pprint(Eq(res_theta[1], C2))
def eq4(): r = Symbol("r") e = Rmn.dd(3, 3) e = e.subs(nu(r), -lam(r)) pprint(dsolve(e, lam(r))) pprint(dsolve(e, lam(r), 'best'))
def eq3(): r = Symbol("r") e = Rmn.dd(2, 2) e = e.subs(nu(r), -lam(r)) pprint(dsolve(e, lam(r)))
def eq2(): r = Symbol("r") e = Rmn.dd(1, 1) C = Symbol("CC") e = e.subs(nu(r), -lam(r)) pprint(dsolve(e, lam(r)))
def pprint_Rmn_dd(i, j): pprint(Eq(Symbol('R_%i%i' % (i, j)), Rmn.dd(i, j)))
def pprint_Gamma_udd(i, k, l): pprint(Eq(Symbol('Gamma^%i_%i%i' % (i, k, l)), Gamma.udd(i, k, l)))
def transform(name, X, Y, g_correct=None, recursive=False): """ Transforms from cartesian coordinates X to any curvilinear coordinates Y. It printing useful information, like Jacobian, metric tensor, determinant of metric, Laplace operator in the new coordinates, ... g_correct ... if not None, it will be taken as the metric --- this is useful if diofant's trigsimp() is not powerful enough to simplify the metric so that it is usable for later calculation. Leave it as None, only if the metric that transform() prints is not simplified, you can help it by specifying the correct one. recursive ... apply recursive trigonometric simplification (use only when needed, as it is an expensive operation) """ print("_" * 80) print("Transformation:", name) for x, y in zip(X, Y): pprint(Eq(y, x)) J = X.jacobian(Y) print("Jacobian:") pprint(J) g = J.T * eye(J.shape[0]) * J g = g.applyfunc(expand) print("metric tensor g_{ij}:") pprint(g) if g_correct is not None: g = g_correct print("metric tensor g_{ij} specified by hand:") pprint(g) print("inverse metric tensor g^{ij}:") g_inv = g.inv(method="ADJ") g_inv = g_inv.applyfunc(simplify) pprint(g_inv) print("det g_{ij}:") g_det = g.det() pprint(g_det) f = Function("f")(*list(Y)) print("Laplace:") pprint(laplace(f, g_inv, g_det, Y))