I've been using sympy in jupyter notebooks as a simple free symbolic algebra system while helping my kids review calculus and physics in anticipation of starting as freshman at an engineering school in the fall.
We made it through all of single variable calculus without too much trouble, but now we're working through multivariable calculus and a specific set of exercises from Khan Academy is giving me trouble. I have code working that arrives at the correct solution, but it involves doing an awkward python list comprehension. I am seeking advice on whether there's a more idiomatic way to evaluate a scalar field at a vector, whether that means expressing things differently or using a different set of functions, etc. I have isolated an example problem into a stand-alone python file. I couldn't quite figure out how to best include a code snipped to this list. I will link to a gist and then also include the code below between triple backticks. Thank you. /rdj https://gist.github.com/rdj/af9a5da6a10bab1d821c82ae8d5e13ff ```python #!/usr/bin/env python3 # The Khan Academy exercises related to the multivariable chain rule # ask you to find the derivative of the composition of a scalar field # and a vector-valued function. Their article is available here: # # https://www.khanacademy.org/math/multivariable-calculus/multivariable-derivatives/differentiating-vector-valued-functions/a/multivariable-chain-rule-simple-version?modal=1 # # Here is one of the exercises from this section: # # Let f(x,y,z) = z*cos(y) + z^2*x and g(t) = (t, -t^2, -t). # # h(t) = f(g(t)) # # Find h'(t). import sympy as sp import sympy.vector as spv R3 = spv.CoordSys3D('') x,y,z = R3.base_scalars() # Let f(x,y,z) be a scalar field mapping R3 to R: f = z*sp.cos(y) + z**2*x print(F"f(x,y,z) = {f}") # Let g(t) be a vector valued function mapping R to R3: t = sp.Symbol("t", real=True) g = t * R3.i + -t**2 * R3.j + -t * R3.k print(F"g(t) = {g}") # Find the derivative with respect to t of the composition: # d/dt[f(g(t))] = ∇_{g'(t)}f(g(t)) # First, get g'(t), a new vector-valued function. gp = sp.diff(g, t) print(F"g'(t) = {gp}") # Next, get the directional derivative of f(x,y,z) in the direction # g'(t), a new scalar field. del_gp_f = spv.directional_derivative(f, gp) print(F"∇_{{g'(t)}} f(x,y,z) = {del_gp_f}") # Now, evaluate the directional derivative at g(t), yielding the result # d/dt[f(g(t))], which should be a real function mapping R to R. # HERE - Is there a less awkward way to evaluate the scalar field at a # given vector? # # Note: Of course we could calculate the desired result a different # way by evaluating gradient(f) at g then dotting with g', but again # mechanically that's evaluating a scalar field at a given vector so # we'd have to jump through the same hoops. dfog_dt = del_gp_f.subs([(s, g.components[v]) for v,s in zip(R3.base_vectors(), R3.base_scalars())]) print(F"d/dt[f(g(t))] = {dfog_dt}") ``` -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/c9e45103-82a5-49b1-acb0-260adeb579bfn%40googlegroups.com.