23.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
In this case, g.type can have different values.
The value of g is fetched differently for different types:
-
If g.type is _INT_, then g.val
is an integer type int.
- If g.type is _DOUBLE_, then
g._DOUBLE_val is a real double.
- If g.type is _SYMB, then
g._SYMBptr points to a value of type symbolic. The
function is contained in the sommet member and its arguments in
the feuille member.
- If g.type is _VECT, then
g._VECTptr points to a value of type vecteur,
which behaves like std::vector.
- If g.type is _ZINT, then
g._ZINTptr points to a value of integer type zint.
- If g.type is _IDNT, then
g._IDNTptr points to an identifier of type identificateur.
- If g.type will be _CPLX, then
g._CPLXptr and g._CPLXptr+1 are pointers to
the real and imaginary parts of g.
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: