Astrolabe
Astrolabe

Reputation: 147

Function of function(functional) in Maxima

I want to define a function of a function ( maybe functional) in Maxima. For example, I want to define T in the code:

f(r,GN,M)::=(2*GN*M^2)*(2*GN*M/r-(2*GN*M/(2*r))^2);
X(r,GN,M) ::= 2*GN*M/(2*r)+2*GN*M/r*(1-2*GN*M/(4*r));
T(r,GN,M) ::= diff(f(r,GN,M),r)+X(r,GN,M);

But I don't know the code.

Upvotes: 2

Views: 327

Answers (2)

austinlorenz
austinlorenz

Reputation: 181

Define T as a function, rather than a macro, so that it evaluates its arguments.

(%i) T(r,GN,M) := diff(f(r,GN,M),r)+X(r,GN,M)$

Now define a function t on T which quotes ' its arguments (so that T knows which variable to differentiate with respect to) and then evaluates the result %% with ev.

(%i) t(r,GN,M) := (T('r,'GN,'M),ev(%%))$
(%i) t(1,2,3);
(%o)                               2142

Upvotes: 1

slitvinov
slitvinov

Reputation: 5768

In maxima it is convenient to use expressions.

Define two expressions

(%i1) display2d: false $
(%i2) f: (2*GN*M^2)*(2*GN*M/r-(2*GN*M/(2*r))^2) $
(%i3) X: 2*GN*M/(2*r)+2*GN*M/r*(1-2*GN*M/(4*r)) $

Define a function which operates on two expressions

(%i4) T(f, X) := diff(f, r) + X $

Call it. Result is an expression

(%i5) dfpX: T(f, X);
(%o5) (2*GN*M*(1-(GN*M)/(2*r)))/r+(GN*M)/r+2*GN*M^2
                                            *((2*GN^2*M^2)/r^3-(2*GN*M)/r^2)

You can create a function based on dfpX

(%i6) define(f(r, GN, M), dfpX);
(%o6) f(r,GN,M):=(2*GN*M*(1-(GN*M)/(2*r)))/r+(GN*M)/r
        +2*GN*M^2*((2*GN^2*M^2)/r^3-(2*GN*M)/r^2)

and call it

(%i7) f(1, 2, 3);
(%o7) 2142

but you can stay with expressions

(%i8) subst([r=1, GN=2, M=3], dfpX);
(%o8) 2142

Upvotes: 1

Related Questions