testing new functions for giac

Messages in english

Modérateur : xcasadmin

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

testing new functions for giac

Message par lukamar » mar. août 01, 2017 2:21 pm

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.

parisse
Messages : 5765
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: testing new functions for giac

Message par parisse » mar. août 01, 2017 5:48 pm

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

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: testing new functions for giac

Message par lukamar » mer. août 02, 2017 8:05 am

Thanks for the update. Trying to make a trivial code that works, I wrote lpsolve.h:

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
and lpsolve.cc:

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
where I added lines

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);
because the ones from the example

Code : Tout sélectionner

unary_function_unary __example(&_example,_example_s);
unary_function_ptr at_example (&__example,0,true);
did 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:

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;
}
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?

parisse
Messages : 5765
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: testing new functions for giac

Message par parisse » mer. août 02, 2017 9:52 am

It's related to the configuration. If you add in lpsolve.cc

Code : Tout sélectionner

#include <giac/config.h>
before the other headers and recompile, then it should not segfault. I've updated giac_us.html accordingly.

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: testing new functions for giac

Message par lukamar » mer. août 02, 2017 10:46 am

Yes, I confirm that it works now. Thanks again.

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: testing new functions for giac

Message par lukamar » ven. août 04, 2017 2:25 pm

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?

Répondre