Previous Up Next

25.7.2  Defining new giac functions

New giac functions can be defined with a C++ program. All data in the program used in formal calculations needs to be gen type. A variable g can be declared to be gen type with

gen g;

In this case, g.type can have different values. The value of g is fetched differently for different types:

As an example, put the following program in a file called pgcd.cpp.

#include <stdexcept> #include <cmath> #include <cstdlib> #include <giac/config.h> #include <giac/giac.h> using namespace std; #ifndef NO_NAMESPACE_GIAC namespace giac { #endif gen monpgcd(const gen & a0,const gen & b0) { gen q,r,a=a0,b=b0; for (;b!=0;) { r=irem(a,b,q); a=b; b=r; } return a; } gen _monpgcd(const gen & args,GIAC_CONTEXT) { if ((args.type!=_VECT) || (args._VECTptr->size()!=2)) return gensizeerr(contextptr); // return an error vecteur &v=*args._VECTptr; return monpgcd(v[0],v[1]); } const string _monpgcd_s[]="monpgcd"; unary_function_eval __monpgcd(0,&_monpgcd,_monpgcd_s); unary_function_ptr at_monpgcd(&__monpgcd,0,true); #ifndef NO_NAMESPACE_GIAC } #endif

After compiling this with the commands with

g++ -I.. -fPIC -DPIC -g -c pgcd.cpp -o pgcd.lo && ln -sf pgcd.lo pgcd.o && \ gcc -shared pgcd.lo -lc -lgiac -Wl,-soname -Wl,libpgcd.so.0 -o \ libpgcd.so.0.0.0 && ln -sf libpgcd.so.0.0.0 libpgcd.so.0 && \ ln -sf libpgcd.so.0.0.0 libpgcd.so

the new command can be inserted with the insmod command in giac, where insmod takes the full absolute path of the libpgcd.so file as argument.

insmod("/path/to/file/libpgcd.so")

Afterwards, the monpgcd command will be another giac command. For example, input:

monpgcd(30,36)
     
6           

Previous Up Next