Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I think that I understand what you aim at. Let's devine your expressions :

sage: Vars=var("h, g, A, B")
sage: x0 = 1 + h*g^2 + h
sage: y0 = g + h^2
sage: f1, f2 = function("f1, f2")
sage: d = sqrt(f1(*Vars)^2+f2(*Vars)^2)
sage: [d.diff(u) for u in Vars]

but declare f1 and f2 as undefined functions :

sage: f1, f2 = function("f1, f2")

We can not define d using these functions :

sage: d = sqrt(f1(*Vars)^2+f2(*Vars)^2)

Deriving these functions will be done using the chain rule explicitly :

sage: D1 = [d.diff(u) for u in Vars] ; D1
[(f1(h, g, A, B)*diff(f1(h, g, A, B), h) + f2(h, g, A, B)*diff(f2(h, g, A, B), h))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2),
 (f1(h, g, A, B)*diff(f1(h, g, A, B), g) + f2(h, g, A, B)*diff(f2(h, g, A, B), g))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2),
 (f1(h, g, A, B)*diff(f1(h, g, A, B), A) + f2(h, g, A, B)*diff(f2(h, g, A, B), A))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2),
 (f1(h, g, A, B)*diff(f1(h, g, A, B), B) + f2(h, g, A, B)*diff(f2(h, g, A, B), B))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2)]

Now, we can substitute f1 and f2 by their values :

sage: D2=[u.substitute_function(f1=(A*x*0+y0).function(*Vars)).substitute_function(f2=(B*y0+x0).function(*Vars)) for u in D1] ; D2
[((g^2*h + (h^2 + g)*B + h + 1)*(g^2 + 2*B*h + 1) + 2*(h^2 + g)*h)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
 ((g^2*h + (h^2 + g)*B + h + 1)*(2*g*h + B) + h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
 0,
 (g^2*h + (h^2 + g)*B + h + 1)*(h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2)]

We can also create an expressio using these substitutions :

sage: d2=d.substitute_function(f1=(A*x*0+y0).function(*Vars)).substitute_function(f2=(B*y0+x0).function(*Vars)) ; d2
sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2)

derive it :

sage: D3 = [d2.diff(u) for u in Vars] ; D3
[((g^2*h + (h^2 + g)*B + h + 1)*(g^2 + 2*B*h + 1) + 2*(h^2 + g)*h)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
 ((g^2*h + (h^2 + g)*B + h + 1)*(2*g*h + B) + h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
 0,
 (g^2*h + (h^2 + g)*B + h + 1)*(h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2)]

and check the equality with the previous expressions :

sage: all(map(lambda a,b:bool(a==b), D2, D3))
True

HTH,