Examples
| Automatic
differentiation |
This example shows how Symbolic
Tools uses the Maple kernel to carry out analytical
differentiation, and then to create and compile a
GAUSS proc that has the same functionality. This
is the basis for Automatic Differentiation.
The process of creating a proc
based on symbolic code is a one line operation - symproc. This
command takes four arguments - the proc name, the
input argument(s), the output required, and the code. The
code ( txt ) is GAUSS,
with the gradp function overloaded to permit a symbolic
second argument. The call to symproc executes
the code in the Maple kernel, creates the equivalent
GAUSS proc, and compiles the proc, so that it can
be called immediately - as shown below.
library symbolic; // define the library
call symstate(reset); // turns on symbolic processing
proc difsin; endp; // dummy proc
txt = "
slist = {x,y};
llf = sin(x*y)^2;
llfg = gradp(llf,slist);
";
call symproc("difsin","x,y","llfg",txt); // create and compile the proc
rslt = difsin(1,2); // execute the proc
"rslt\n" rslt;
The proc that is created by the symproc command
:
proc difsin(x,y);
local t0, t1, t2, t3, t4,
unknown;
unknown = zeros(rows(y),2);
t1 = x .* y;
t2 = sin(t1);
t3 = cos(t1);
t4 = t2 .* t3;
unknown[.,1+0] = 2.0 .* t4 .* y;
unknown[.,1+1] = 2.0 .* t4 .* x;
retp(unknown);
endp;
The output from this example is
:
rslt -1.5136050 -0.75680251
This example shows how Symbolic
Tools undertakes symbolic arithmetic.
library symbolic; // define the library
call symstate(reset); // turns on symbolic processing
// Symbolic matrix
txt = " xmat = symmat(2,2,{a,b,c,d});
xd = det(xmat);" ;
call symrun(txt);
xd = symget("xd");
"\n det(xmat): " xd;
The symmat command
allows GAUSS to define a symbolic matrix. The
kernel evaluates the code in txt and
the symbolic determinant is returned as a string.
The output from this example is
:
det(xmat): a*d-b*c
This example shows how Symbolic
Tools extends the GAUSS language. Symbolic values
returned to GAUSS are represented as strings. Symbolic
values evaluated in the Maple kernel at some value
and returned to GAUSS are evaluated numerically.
library symbolic; // define the library
call symstate(reset); // turns on symbolic processing
//Language Extension
xm = rndu(3,4);
xnorm = symmaple("norm(xm)",0); //infinity norm
"\n norm(xm): " xnorm;
let xm[2,2] = 1 0 3 2;
xjord = symmaple("jordan(xm)",0); //Jordan form
"\n jordan(xm): " xjord;
let xm[2,2] = 1.5 2 2.2 2.5;
xzeta = symmaple("Zeta(xm)",1); // Riemann Zeta function
"\n Zeta(xm): " xzeta;
The symmaple command
allows GAUSS to access any Maple function, and returns
the numeric result to GAUSS. The second parameter
tells Maple whether the argument is a matrix or not.
The output from this example is
:
norm(xm): 3.0956951
jordan(xm):
1.0000000 0.00000000
0.00000000 2.0000000
Zeta(xm):
2.6123753 1.6449341
1.4905433 1.3414873
|