testing new functions for giac
Modérateur : xcasadmin
testing new functions for giac
Hello Bernard,
I am interested in testing a linear programming solver for Giac that I've coded in c++. I found an explanation how to do it here: https://www-fourier.ujf-grenoble.fr/~pa ... ac_us.html (Unary functions), but it seems outdated. Giac functions now have additional argument, GIAC_CONTEXT. How should I modify the example to work with the current version of Giac?
Thanks for the help.
I am interested in testing a linear programming solver for Giac that I've coded in c++. I found an explanation how to do it here: https://www-fourier.ujf-grenoble.fr/~pa ... ac_us.html (Unary functions), but it seems outdated. Giac functions now have additional argument, GIAC_CONTEXT. How should I modify the example to work with the current version of Giac?
Thanks for the help.
Re: testing new functions for giac
Indeed, I have updated the html webpage.
In short, just add the macro GIAC_CONTEXT as last arg in your own C++ functions and use the variable contextptr in your code as last argument when you call giac commands that require it. contextptr is a pointer of type context * that allows several threads to run with one copy of the library without interferences of the context (defined variable, angle mode, etc.).
In short, just add the macro GIAC_CONTEXT as last arg in your own C++ functions and use the variable contextptr in your code as last argument when you call giac commands that require it. contextptr is a pointer of type context * that allows several threads to run with one copy of the library without interferences of the context (defined variable, angle mode, etc.).
Re: testing new functions for giac
Thanks for the update. Trying to make a trivial code that works, I wrote lpsolve.h:and lpsolve.cc:where I added linesbecause the ones from the exampledid not work (of course, I first changed 'example' to 'lpsolve'). I compile with:
g++ -g -c lpsolve.cc
and it goes fine without errors. Then I wrote test.cc:which I successfully compiled with:
g++ -g lpsolve.o test.cc -lgiac -lgmp
But, when I run a.out, it segfaults immediately. I think it happens before any command in main subroutine is executed. Can you please tell me how to fix it?
Code : Tout sélectionner
#ifndef __LPSOLVE_H
#define __LPSOLVE_H
#include <giac/gen.h>
#include <giac/unary.h>
#ifndef NO_NAMESPACE_GIAC
namespace giac {
#endif // ndef NO_NAMESPACE_GIAC
gen _lpsolve(const gen & args,GIAC_CONTEXT);
extern const unary_function_ptr * const at_lpsolve;
#ifndef NO_NAMESPACE_GIAC
} // namespace giac
#endif // ndef NO_NAMESPACE_GIAC
#endif // __LPSOLVE_H
Code : Tout sélectionner
using namespace std;
#include <giac/giac.h>
#include "lpsolve.h"
#ifndef NO_NAMESPACE_GIAC
namespace giac {
#endif // ndef NO_NAMESPACE_GIAC
gen _lpsolve(const gen & args,GIAC_CONTEXT) {
return args;
}
static const char _lpsolve_s []="lpsolve";
static define_unary_function_eval (__lpsolve,&_lpsolve,_lpsolve_s);
define_unary_function_ptr5( at_lpsolve ,alias_at_lpsolve,&__lpsolve,0,true);
#ifndef NO_NAMESPACE_GIAC
}
#endif // ndef NO_NAMESPACE_GIAC
Code : Tout sélectionner
static define_unary_function_eval (__lpsolve,&_lpsolve,_lpsolve_s);
define_unary_function_ptr5( at_lpsolve ,alias_at_lpsolve,&__lpsolve,0,true);
Code : Tout sélectionner
unary_function_unary __example(&_example,_example_s);
unary_function_ptr at_example (&__example,0,true);
g++ -g -c lpsolve.cc
and it goes fine without errors. Then I wrote test.cc:
Code : Tout sélectionner
#include "lpsolve.h"
using namespace std;
using namespace giac;
int main(){
gen args;
context ct;
cout << "Enter argument of example function";
cin >> args;
cout << "Result: " << _lpsolve(args,&ct) << endl;
return 0;
}
g++ -g lpsolve.o test.cc -lgiac -lgmp
But, when I run a.out, it segfaults immediately. I think it happens before any command in main subroutine is executed. Can you please tell me how to fix it?
Re: testing new functions for giac
It's related to the configuration. If you add in lpsolve.cc
before the other headers and recompile, then it should not segfault. I've updated giac_us.html accordingly.
Code : Tout sélectionner
#include <giac/config.h>
Re: testing new functions for giac
Yes, I confirm that it works now. Thanks again.
Re: testing new functions for giac
Some predefined constants of type gen, such as zero, plus_one, plus_inf etc. are causing segfault when included in the above example code. How does one avoid crashing in these cases?