Adding a new function to an existing file
Here we describe how to add new function to an existing file.
Lets take the same example as before: we want to add the functinos that takes two numbers, A and B and computes A^2+B^2.
-
In
libpolys/coeffs/numbers.h
add the linenumber n_myAdd(number A, number B, const coeffs R);
-
In
libpolys/coeffs/numbers.cc
add the function:number n_myAdd(number A, number B, const coeffs R) { number AA = n_Mult(A, A, R), BB = n_Mult(B, B, R), CC = n_Add(AA, BB, R); n_Delete(&AA, R); n_Delete(&BB, R); return CC; }
(Be sure you understand the memory management, in particular, the reason for the calls to
n_Delete()
) -
Now, after a compile, the function is ready to use - however, it is not yet available in the interpreter.
-
Next, we'll show how to incorporate this function - in the previous example we "hacked" it into the
Singular/extra.cc file
, now we'll do it properly.You need to add entries to
-
In
Singular/tok.h
:MYADD_CMD
, (it is not important, but try to keep the list alphabetically sorted) -
In
Singular/table.h
:-
in
cmdnames cmds[]
add in the correct (alphabetical) position:"myAdd", 0, MYADD_CMD , CMD_2
(where CMD_2 indicates that we take to arguments)
-
find
sValCmd2 dArith2
and add,{D(jjMyAdd), MYADD_CMD, NUMBER_CMD, NUMBER_CMD, NUMBER_CMD, ALLOW_PLURAL | ALLOW_RING}
-
-
In
Singular/iparith.cc
add a functionstatic BOOLEAN jjMyAdd(leftv res, leftv u,leftv v) { number a=(number)u->Data(); number b=(number)v->Data(), number c = n_myAdd(a,b,currRing); res ->data = (char *)c; return FALSE; }
Note that the declaration in
sValCmd2
guarantees thatjjMyAdd
is going to get called with 2 arguments, both of type number. Q: doesiparith
have to be in some special order?After a compile, you should have a new function in Singular:
myAdd
. Try usingring r=0,x,dp; number a =1; number b=2; myAdd(a,b);
-