ToMaplehfData - extract an encoded ALGEB from an hfdata structure
|
Calling Sequence
|
|
ToMaplehfData(kv, hf)
|
|
Parameters
|
|
kv
|
-
|
kernel handle of type MKernelVector
|
hf
|
-
|
an hfdata structure
|
|
|
|
|
Description
|
|
•
|
The ToMaplehfData function returns a Maple object that has been encoded in an hfdata structure.
|
•
|
All hfdata structures returned by EvalhfDataProc represent real floating point values. Currently the only way to get a Maple object encoded in an hfdata structure is to encode it using MapleTohfData.
|
|
|
Examples
|
|
#include <math.h>
|
#include "maplec.h"
|
ALGEB MyNewtonData( MKernelVector kv, ALGEB *args )
|
{
|
M_INT i;
|
FLOAT64 tolerance, newguess, res;
|
hfdata guess[2];
|
ALGEB f, fprime, x;
|
if( 3 != MapleNumArgs(kv,(ALGEB)args) ) {
|
MapleRaiseError(kv,"three arguments expected");
|
return( NULL );
|
}
|
if( IsMapleProcedure(kv,args[1]) ) {
|
f = args[1];
|
}
|
else {
|
ALGEB indets;
|
indets = EvalMapleProc(kv,ToMapleName(kv,"indets",TRUE),1,args[1]);
|
if( !IsMapleSet(kv,indets) || MapleNumArgs(kv,indets) != 1 ) {
|
MapleRaiseError(kv,"unable to find roots");
|
return( NULL );
|
}
|
i = 1;
|
f = EvalMapleProc(kv,ToMapleName(kv,"unapply",TRUE),2,args[1],
|
MapleSelectIndexed(kv,indets,1,&i));
|
if( !f || !IsMapleProcedure(kv,f) ) {
|
MapleRaiseError(kv,"unable to convert first arg to a procedure");
|
return( NULL );
|
}
|
}
|
x = ToMapleName(kv,"x",FALSE);
|
fprime = EvalMapleProc(kv,ToMapleName(kv,"unapply",TRUE),2,
|
EvalMapleProc(kv,ToMapleName(kv,"diff",TRUE),2,
|
ToMapleFunction(kv,f,1,x),x),x);
|
if( !fprime || !IsMapleProcedure(kv,fprime) ) {
|
MapleRaiseError(kv,"unable to compute derivative");
|
return( NULL );
|
}
|
DoubleTohfData( kv, MapleEvalhf( kv, args[2] ), 0, guess+1 );
|
tolerance = MapleEvalhf(kv,args[3]);
|
res = 0.0;
|
for( i=0; i<500; ++i ) {
|
res = RealhfData( kv, EvalhfDataProc(kv,f,1,guess) );
|
if( fabs( res ) <= tolerance )
|
break;
|
newguess = RealhfData( kv, guess[1] ) -
|
res / RealhfData( kv, EvalhfDataProc(kv,fprime,1,guess) );
|
DoubleTohfData( kv, newguess, 0, guess+1 );
|
}
|
if( i == 500 ) {
|
MapleRaiseError(kv,"unable to find root after 500 iterations");
|
return( NULL );
|
}
|
return( ToMapleFloat(kv, RealhfData( kv, guess[1] ) ) );
|
}
|
|
|
Execute the external function from Maple.
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
| (1) |
>
|
|
| (2) |
>
|
|
| (3) |
>
|
|
| (4) |
>
|
|
| (5) |
>
|
|
>
|
|
| (6) |
>
|
|
>
|
|
| (7) |
>
|
|
| (8) |
|
|