Dear all,
I am trying to get SymPy 1.1.1 to replace derivatives of a Function (i.e.,
generated by sympy.Function) by a python function (created with def). I can
replace the Functions itself, but not their derivatives.
Right now, I am using some "toy" functions, but eventually, they will be
created as the interpolation of a data set.
I tried doing this in two ways: First, just adding a dictionary to
sympy.lambidify. A function is returned by lambidify, but calling it fails
with:
Traceback (most recent call last):
>
> File "./replace_direct.py", line 56, in <module>
>
> print(du_f(1, 1, 1))
>
> File "<string>", line 1, in <lambda>
>
> NameError: name 'Derivative' is not defined
>
>
>
I tried a similar approach as suggested in
https://groups.google.com/forum/#!topic/sympy/U3s0mSTszd4, namely, creating
a temporary name and then replacing it. This also fails, but with
[1, 2, 3]
>
> Traceback (most recent call last):
>
> File "./replace_indirect.py", line 67, in <module>
>
> print(du_f(1, 1, 1))
>
> File "<string>", line 1, in <lambda>
>
> NameError: name 'x' is not defined
>
>
>
Does anyone know the best way to do such replacement?
Bests,
Lucas
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/d5a20080-6571-4473-b78d-23780ed993f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Distributed under terms of the MIT license.
import sympy
from sympy.abc import x, y, z
# Create our replacement functions
def uxf(a, b, c):
return a
def uyf(a, b, c):
return b
def uzf(a, b, c):
return c
def zeros(a, b, c):
return 0
def ones(a, b, c):
return 1
# Basic vector
r = sympy.Array([x, y, z])
# Create each component of u
ux = sympy.Function("ux")(x, y, z)
uy = sympy.Function("uy")(x, y, z)
uz = sympy.Function("uz")(x, y, z)
# Create u proper
u = sympy.Array([ux, uy, uz])
# Create a callable version of u with u_is replaced
u_f = sympy.lambdify((x, y, z), u, {"ux": uxf, "uy": uyf, "uz": uzf})
# Derive u wrt r
du = sympy.derive_by_array(u, r)
# Try to create a callable version of du with proper
# replacements
du_f = sympy.lambdify((x, y, z), du, {
sympy.Derivative(ux, x): ones, sympy.Derivative(ux, y): zeros, sympy.Derivative(ux, z): zeros,
sympy.Derivative(uy, x): zeros, sympy.Derivative(uy, y): ones, sympy.Derivative(uy, z): zeros,
sympy.Derivative(uz, x): zeros, sympy.Derivative(uz, y): zeros, sympy.Derivative(uz, z): ones
})
# Print results
print(u_f(1, 2, 3))
print(du_f(1, 1, 1))
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Distributed under terms of the MIT license.
import sympy
from sympy.abc import x, y, z
# Create our replacement functions
def uxf(a, b, c):
return a
def uyf(a, b, c):
return b
def uzf(a, b, c):
return c
def zeros(a, b, c):
return 0
def ones(a, b, c):
return 1
# Basic vector
r = sympy.Array([x, y, z])
# Create each component of u
ux = sympy.Function("ux")(x, y, z)
uy = sympy.Function("uy")(x, y, z)
uz = sympy.Function("uz")(x, y, z)
# Create u proper
u = sympy.Array([ux, uy, uz])
# Create a callable version of u with u_is replaced
u_f = sympy.lambdify((x, y, z), u, {"ux": uxf, "uy": uyf, "uz": uzf})
# Derive u wrt r
du = sympy.derive_by_array(u, r)
# Here we will use the indirect approach suggested in
# https://groups.google.com/forum/#!topic/sympy/U3s0mSTszd4
# First, we replace the Derivative names with temporary names
du_s = du.subs({
sympy.Derivative(ux, x): "dxx(x, y, z)", sympy.Derivative(ux, y): "dxy(x, y, z)", sympy.Derivative(ux, z): "dxz(x, y, z)",
sympy.Derivative(uy, x): "dyx(x, y, z)", sympy.Derivative(uy, y): "dyy(x, y, z)", sympy.Derivative(uy, z): "dyz(x, y, z)",
sympy.Derivative(uz, x): "dzx(x, y, z)", sympy.Derivative(uz, y): "dzy(x, y, z)", sympy.Derivative(uz, z): "dzz(x, y, z)"})
# Then replace the temporary names with our functions
du_f = sympy.lambdify((x, y, z), du_s,
{
"dxx": ones, "dxy": zeros, "dxz": zeros,
"dyx": zeros, "dyy": ones, "dyz": zeros,
"dzx": zeros, "dzy": zeros, "dzz": ones
})
# Print results
print(u_f(1, 2, 3))
print(du_f(1, 1, 1))