1 | initial version |
diff
only works on symbolic expression (elements of the symbolic ring SR
), not on python objects like functions. But you are in luck because f(*v)
is precisely an element of SR
. So the correct syntax is f(*v).diff(v[i])
.
As a side remark, f
can be simplified a lot, by defining f = (M*vector(v)).norm()
. In this case, f
is a symbolic expression.
Here is a code that computes all the partial derivatives for some matrix M:
n = 3
v = list(var('v_%d' % i) for i in range(1, n+1))
M = Matrix([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
f = (M*vector(v)).norm()
[diff(f, vi) for vi in v]
The output is:
[1/2*(v_1 + conjugate(v_1))/sqrt(abs(v_1)^2 + abs(v_2)^2 + abs(v_3)^2),
1/2*(v_2 + conjugate(v_2))/sqrt(abs(v_1)^2 + abs(v_2)^2 + abs(v_3)^2),
1/2*(v_3 + conjugate(v_3))/sqrt(abs(v_1)^2 + abs(v_2)^2 + abs(v_3)^2)]
Of course you can take the dot product of this list with any tangent vector.