MapleTohfData - Maple Programming Help

Home : Support : Online Help : Programming : OpenMaple : C Application Programming Interface : Evaluation and Error Handling : OpenMaple/C/MapleTohfData

MapleTohfData

initialize hfdata structures for use in external code

 Calling Sequence MapleTohfData(kv, a, hf) DoubleTohfData(kv, re, im, hf) ComplexTohfData(kv, re, im, hf)

Parameters

 kv - kernel handle of type MKernelVector a - Maple PROC or RTABLE object re - the value to use as the real component im - the value to use as the imaginary component hf - a pointer to the hfdata structure to initialize

Description

 • These functions can be used in external code with OpenMaple or define_external.
 • MapleTohfData encodes a Maple PROC or an RTABLE of type float[8] into an hfdata structure.  These structures are used to pass arguments into and get results out of EvalhfDataProc.
 • DoubleTohfData and ComplexTohfData encode a complex number represented as two doubles into an hfdata structure.  They differ in that if the im argument to DoubleTohfData is 0.0 or -0.0 it will be ignored, so the resulting hf structure will be treated as representing a real number, not a real number with zero imaginary part.  These structures are used to pass arguments into and get results out of EvalhfDataProc.
 • A Maple object encoded in a hfdata structure can be extracted by calling ToMaplehfData.  The real and imaginary parts of a hfdata can be extracted by calling RealhfData and ImaginaryhfData.
 • All hfdata objects returned by EvalhfDataProc represent real floating point values.

Examples

 #include #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.

 > $\mathrm{with}\left(\mathrm{ExternalCalling}\right):$
 > $\mathrm{dll}≔\mathrm{ExternalLibraryName}\left("HelpExamples"\right):$
 > $\mathrm{newton}≔\mathrm{DefineExternal}\left("MyNewtonData",\mathrm{dll}\right):$
 > $f≔{x}^{4}-5{x}^{2}+6x-2:$
 > $\mathrm{newton}\left(f,0,0.001\right)$
 ${0.731892751250226237}$ (1)
 > $\genfrac{}{}{0}{}{f}{\phantom{x=}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{f}}{x=}$
 ${-}{0.000039355}$ (2)
 > $\mathrm{newton}\left(f,\sqrt{2},0.00001\right)$
 ${1.00195003210012135}$ (3)
 > $\genfrac{}{}{0}{}{f}{\phantom{x=}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{f}}{x=}$
 ${0.000003833}$ (4)
 > $\mathrm{newton}\left(f,-\mathrm{π},1.{10}^{-10}\right)$
 ${-}{2.73205080756887719}$ (5)
 > $\mathrm{Digits}≔15:$
 > $\genfrac{}{}{0}{}{f}{\phantom{x=}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{f}}{x=}$
 ${1.5}{}{{10}}^{{-13}}$ (6)
 > $f≔\mathrm{unapply}\left(f,x\right):$
 > $\mathrm{newton}\left(f,-\mathrm{π},1.{10}^{-10}\right)$
 ${-}{2.73205080756887719}$ (7)
 > $\mathrm{evalhf}\left(f\left(\right)\right)$
 ${-}{7.10542735760100186}{}{{10}}^{{-15}}$ (8)