evaluate an object using hardware floats in external code - Maple Help

Online Help

All Products    Maple    MapleSim


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

MapleEvalhf - evaluate an object using hardware floats in external code

EvalhfMapleProc - evaluate a procedure using hardware floats in external code

Calling Sequence

MapleEvalhf(kv, s)

EvalhfMapleProc(kv, fn, n, args)

Parameters

kv

-

kernel handle of type MKernelVector

s

-

type ALGEB object

fn

-

Maple FUNCTION object

n

-

number of arguments

args

-

array of n hardware floats

Description

• 

These functions can be used in external code with OpenMaple or define_external.

• 

MapleEvalhf evaluates an expression to a numerical value using the hardware floating-point of the underlying system.  This command is equivalent to the Maple function, evalhf.

• 

EvalhfMapleProc evaluates the function, f(args), to a numerical value using evalhf.  The n arguments provided are all 64-bit hardware floating-point numbers. The first argument must be inserted at args[1].  For example, to call f(3.14,2.718), set args[1] = 3.14, and args[2] = 2.718.  The value at args[0] is not used.

Examples

    #include <math.h>

    #include "maplec.h"

    ALGEB M_DECL MyNewton( MKernelVector kv, ALGEB *args )

    {

    M_INT i;

    FLOAT64 guess[2], tolerance;

    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 );

    }

    guess[1] = MapleEvalhf(kv,args[2]);

    tolerance = MapleEvalhf(kv,args[3]);

    for( i=0; i<500; ++i ) {

        if( fabs(EvalhfMapleProc(kv,f,1,guess)) <= tolerance )

        break;

        guess[1] = guess[1] - EvalhfMapleProc(kv,f,1,guess) /

            EvalhfMapleProc(kv,fprime,1,guess);

    }

    if( i == 500 ) {

        MapleRaiseError(kv,"unable to find root after 500 iterations");

        return( NULL );

    }

    return( ToMapleFloat(kv,guess[1]) );

    }

Execute the external function from Maple.

withExternalCalling&colon;

dll:=ExternalLibraryNameHelpExamples&colon;

newton:=DefineExternalMyNewton&comma;dll&colon;

f:=x45x2&plus;6x2&colon;

newtonf&comma;0&comma;0.001

0.731892751250226237

(1)

fx&equals;|fx&equals;

0.000039355

(2)

newtonf&comma;2&comma;0.00001

1.00195003210012135

(3)

fx&equals;|fx&equals;

0.000003833

(4)

newtonf&comma;&pi;&comma;1.10-10

2.73205080756887719

(5)

Digits:=15&colon;

fx&equals;|fx&equals;

1.510-13

(6)

f:=unapplyf&comma;x&colon;

newtonf&comma;&pi;&comma;1.10-10

2.73205080756887719

(7)

evalhff

7.1054273576010018610-15

(8)

See Also

CustomWrapper, define_external, evalhf, OpenMaple, OpenMaple/C/API, OpenMaple/C/Examples


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam