Where A is a function of "v". The non-linear system of equations necessary to find "v" is obtained doing:
According to the differentiation rules, this system could be written in matrix form as:
So I've implemented in Sympy, with the help of NumPy "ndarray" the differentiation about a vector. In this case the shape of (\partial A / \partial v is (n X n X n), where "n" is the length of vector "v".
The function is simple:
def vdiff(x, vector):
x = np.array(x)
shape = x.shape
ans = [np.array([e.diff(vi) for e in x.ravel()]) for vi in vector]
ans = [a.reshape(shape) for a in ans]
return np.array(ans).swapaxes(0, 1)
And I've tested with two different cases:
def test_00():
from sympy.abc import a, b, c, x
A11 = x*a*c**2
A12 = x**2*a*b*c
A13 = x**2*a**3*b**5
A21 = x**3*a**2*b*c
A22 = x**4*a*b**2*c**5
A23 = 5*x**4*a*b**2*c
A31 = x**4*a*b**2*c**4
A32 = 4*x**4*a*b**2*c**2
A33 = 4*x**4*a**5*b**2*c
A = np.array([[A11, A12, A13],
[A21, A22, A23],
[A31, A32, A33]])
v = np.array([a, b, c])
F = (v.T.dot(A)).dot(v)
Av = vdiff(A, v)
p1 = v.dot(A)
p2 = A.dot(v)
p3 = v.dot(Av.dot(v))
new = p1 + p2 + p3
ref = np.array([F.diff(a), F.diff(b), F.diff(c)])
print sympy.simplify(Matrix(ref-new))
def test_01():
from sympy.abc import a, b, c, x
sympy.var('c1, c2')
A11 = x*a*c**2
A12 = x**2*a*b*c
A13 = x**2*a**3*b**5
A21 = x**3*a**2*b*c
A22 = x**4*a*b**2*c**5
A23 = 5*x**4*a*b**2*c
A = np.array([[A11, A12, A13],
[A21, A22, A23]])
v = np.array([a, b, c])
cc = np.array([c1, c2])
F = cc.dot(A.dot(v))
ref = np.array([F.diff(a), F.diff(b), F.diff(c)])
Av = vdiff(A, v)
p1 = cc.dot(A)
p2 = cc.dot(Av.dot(v))
new = p1 + p2
print sympy.simplify(Matrix(ref-new))
I'd like to share here and get some feedback in case someone has a more straightforward way to implement this...
Thank you!
Saullo
--
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+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.
>>> from sympy import Function
>>> from sympy.diffgeom.rn import R2
>>> from sympy.diffgeom import Differential
>>> from sympy import pprint
>>> g = Function('g')
>>> s_field = R2.x**2 * R2.y
>>> e_x, e_y, = R2.e_x, R2.e_y
>>> dg = Differential(s_field)
>>> pprint(dg(e_x))
2*x*y
>>> pprint(dg(e_y)) 2
x
I think that what is really worth, is to make diffgeom interact with matrices and vectors, and maybe simplify its usage when the 4 points here above are verified.
--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/P8Q3G5bHe-U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
Consider Lagrangian field theory where the derivatives are taken with respect to the gradient of a field. In the case of quantum electrodynamics with respect to the gradient of a spinor field.
On Saturday, November 2, 2013 12:23:00 AM UTC+1, brombo wrote:Consider Lagrangian field theory where the derivatives are taken with respect to the gradient of a field. In the case of quantum electrodynamics with respect to the gradient of a spinor field.
Yes, that would be needed. I am wondering, is there a generic algorithm for functional derivatives, or is it more likely to be a complicated matter?
--
I believe we can make a variable transform and then apply the derivative using the expression converted to a variable, like:x = a**2 + c + d**3x.diff((a + c**2))changing variables:v = a + c**2a = v - c**2c = (v-a)**0.5the new x will be:x2 = (v-c**2)**2 + (v-a)**0.5 + d**3and the derivative could be computed as:x2.diff(v)is that reasonable?
--
But in general, you can't invert formulas (and even if you
mathematically can, it doesn't mean that solve() can do it).
>>> x.diff(f(x))
Derivative(x, f(x))
>>> x.diff(f(y))
Derivative(x, f(y))
>>> f(x).diff(g(x))
Derivative(f(x), g(x))
>>> f(x).diff(g(y))
Derivative(f(x), g(y))
def vdiff(x, vector): x = np.array(x) shape = x.shape ans = [] for vi in vector: if vi.is_Symbol: tmp = [] for e in x.ravel(): tmp.append(e.diff(vi)) else: subs = {} new_var = sympy.var('new_var') for s in vi.free_symbols: subs[s] = solve(new_var - vi, s)[0] tmp = [] for e in x.ravel(): e = e.subs(subs) e = e.diff(new_var) e = e.subs({new_var: vi}) tmp.append(e.diff(new_var)) ans.append(np.array(tmp)) ans = [a.reshape(shape) for a in ans] return np.array(ans).swapaxes(0, 1)--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/P8Q3G5bHe-U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.