My Project
Data Structures | Typedefs | Functions | Variables
interpreter_support.cc File Reference
#include "kernel/mod2.h"
#include <sstream>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include "Singular/subexpr.h"
#include "Poly.h"
#include "Ideal.h"
#include "kernel/structs.h"
#include "Singular/lists.h"
#include "Singular/ipid.h"
#include "Singular/ipshell.h"
#include "coeffs/numbers.h"
#include "polys/matpol.h"
#include "ring_wrap.h"
#include "intvec_wrap.h"
#include "poly_wrap.h"

Go to the source code of this file.

Data Structures

class  arg_list
 
class  idhdl_wrap
 

Typedefs

typedef boost::python::numeric::array NumpyArrayType
 

Functions

static void free_leftv (leftv args)
 
matrix matrixFromArray (const NumpyArrayType &f)
 
bool is_builtin (const char *name)
 
static NumpyArrayType buildPythonMatrix (matrix m, ring r)
 
boost::python::object buildPyObjectFromLeftv (leftv v)
 
boost::python::list buildPythonList (lists l, ring r)
 
boost::python::object buildPyObjectFromIdhdl (const idhdl_wrap &id)
 
boost::python::object call_interpreter_method (const idhdl_wrap &proc, const arg_list &args)
 
boost::python::object call_builtin_method_general (const char *name, arg_list &l)
 
static boost::python::str idhdl_as_str (idhdl_wrap iw)
 
static idhdl_wrap get_idhdl (const char *n)
 
void export_interpreter ()
 

Variables

EXTERN_VAR int inerror
 

Typedef Documentation

◆ NumpyArrayType

typedef boost::python::numeric::array NumpyArrayType

Definition at line 31 of file interpreter_support.cc.

Function Documentation

◆ buildPyObjectFromIdhdl()

boost::python::object buildPyObjectFromIdhdl ( const idhdl_wrap id)

Definition at line 411 of file interpreter_support.cc.

412 {
413  using boost::python::object;
414 
415  switch (id.id->typ)
416  {
417  case STRING_CMD:
418  return str((const char*) id.id->data.ustring);
419 
420  case INT_CMD:
421  return object((int)id.id->data.i);
422  case POLY_CMD:
423 
424  return object(Poly((poly) id.id->data.p, currRing));
425  case VECTOR_CMD:
426 
427  return object( Vector((poly) id.id->data.p, currRing));
428  case IDEAL_CMD:
429  //object res;
430 
431  return object(Ideal((ideal) id.id->data.uideal, currRing));
432  case MODUL_CMD:
433  //object res;
434 
435  return object(Module((ideal) id.id->data.uideal, currRing));
436  case NUMBER_CMD:
437 
438  return object(Number((number) id.id->data.n, currRing));
439  case MATRIX_CMD:
440  {
441  return buildPythonMatrix((matrix) id.id->data.umatrix,currRing);
442  }
443  case LIST_CMD:
444  return buildPythonList((lists) id.id->data.l, currRing);
445  case RING_CMD:
446  return object(Ring((ring) id.id->data.uring));
447  case INTVEC_CMD:
448  return object(Intvec(*(intvec*) id.id->data.iv));
449  default:
450  return object();
451  //Py_INCREF(Py_None);
452  //return Py_None;
453  }
454 }
Definition: Ideal.h:88
Definition: IIntvec.h:6
Definition: Ideal.h:121
Definition: Number.h:34
Definition: ring_wrap.h:21
Definition: Poly.h:509
Definition: intvec.h:23
Definition: lists.h:24
@ IDEAL_CMD
Definition: grammar.cc:284
@ MATRIX_CMD
Definition: grammar.cc:286
@ MODUL_CMD
Definition: grammar.cc:287
@ VECTOR_CMD
Definition: grammar.cc:292
@ NUMBER_CMD
Definition: grammar.cc:288
@ POLY_CMD
Definition: grammar.cc:289
@ RING_CMD
Definition: grammar.cc:281
static NumpyArrayType buildPythonMatrix(matrix m, ring r)
boost::python::list buildPythonList(lists l, ring r)
char * str(leftv arg)
Definition: shared.cc:704
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
Definition: janet.h:15
@ LIST_CMD
Definition: tok.h:118
@ INTVEC_CMD
Definition: tok.h:101
@ STRING_CMD
Definition: tok.h:185
@ INT_CMD
Definition: tok.h:96

◆ buildPyObjectFromLeftv()

boost::python::object buildPyObjectFromLeftv ( leftv  v)

Definition at line 379 of file interpreter_support.cc.

380 {
381  using boost::python::object;
382  switch (v->rtyp)
383  {
384  case INT_CMD:
385  return object((int)((long)v->data));
386  case POLY_CMD:
387  return object(Poly((poly) v->data, currRing));
388  case STRING_CMD:
389  return str((const char*) v->data);
390  case VECTOR_CMD:
391  return object( Vector((poly) v->data, currRing));
392  case IDEAL_CMD:
393  return object(Ideal((ideal) v->data, currRing));
394  case MODUL_CMD:
395  return object(Module((ideal) v->data, currRing));
396  case NUMBER_CMD:
397  return object(Number((number) v->data, currRing));
398  case MATRIX_CMD:
399  return buildPythonMatrix((matrix) v->data,currRing);
400  case LIST_CMD:
401  return buildPythonList((lists) v->data, currRing);
402  case RING_CMD:
403  return object(Ring((ring) v->data));
404  case INTVEC_CMD:
405  return object(Intvec(*(intvec*) v->data));
406 
407  default:
408  return object();
409  }
410 }
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39

◆ buildPythonList()

boost::python::list buildPythonList ( lists  l,
ring  r 
)

Definition at line 365 of file interpreter_support.cc.

366 {
367  using boost::python::list;
368  list res;
369 
370  for(int i=0;i<=l->nr;i++)
371  {
372  leftv lv=&l->m[i];
373  object o=buildPyObjectFromLeftv(lv);
374  res.append(o);
375  }
376  return res;
377 }
int l
Definition: cfEzgcd.cc:100
int i
Definition: cfEzgcd.cc:132
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
CanonicalForm res
Definition: facAbsFact.cc:60
boost::python::object buildPyObjectFromLeftv(leftv v)

◆ buildPythonMatrix()

static NumpyArrayType buildPythonMatrix ( matrix  m,
ring  r 
)
static

Definition at line 332 of file interpreter_support.cc.

333 {
334  //using boost::python::numeric::array;
335  using boost::python::self;
336  using boost::python::make_tuple;
337  using boost::python::tuple;
338  using boost::python::object;
339  using boost::python::list;
340 
341  list l;
342  for(int i=1;i<=MATROWS(m);i++)
343  {
344  list row;
345  for(int j=1;j<=MATCOLS(m);j++)
346  {
347  Poly ip(MATELEM(m,i,j),r);//copy it
348  row.append(ip);
349  //a[boost::python::make_tuple(i%2,i%5)]=ip;
350  //a[boost::python::make_tuple(i%2,i%5)]=ip;
351  }
352  l.append(row);
353  }
354  #if BOOST_VERSION <106500
355  //FIXME: should call this only once
356  NumpyArrayType::set_module_and_type("Numeric",
357  "ArrayType"
358  );
359  return NumpyArrayType(l);
360  #else
361  return boost::python::numpy::array(l);
362  #endif
363 }
int m
Definition: cfEzgcd.cc:128
int j
Definition: facHensel.cc:110
boost::python::numeric::array NumpyArrayType
#define MATELEM(mat, i, j)
1-based access to matrix
Definition: matpol.h:29
#define MATROWS(i)
Definition: matpol.h:26
#define MATCOLS(i)
Definition: matpol.h:27

◆ call_builtin_method_general()

boost::python::object call_builtin_method_general ( const char *  name,
arg_list l 
)

Definition at line 463 of file interpreter_support.cc.

464 {
465  int cmd_n=-1;
466  IsCmd(name,cmd_n);
467 // Py_INCREF(Py_None);
468 
469 // return Py_None;
470  if (cmd_n<0)
471  {
472  return object();
473  }
474  else
475  {
477  res->Init();
478  switch(l.length())
479  {
480  case 1:
481  iiExprArith1(res,l.args,cmd_n);
482  break;
483  case 2:
484  {
485  leftv arg1=l.pop_front();
486  leftv arg2=l.pop_front();
487  iiExprArith2(res,arg1,cmd_n,arg2,TRUE);
488  free_leftv(arg1);
489  free_leftv(arg2);
490  break;
491  }
492  case 3:
493  {
494  leftv arg1=l.pop_front();
495  leftv arg2=l.pop_front();
496  leftv arg3=l.pop_front();
497  iiExprArith3(res,cmd_n,arg1,arg2,arg3);
498  free_leftv(arg1);
499  free_leftv(arg2);
500  free_leftv(arg3);
501  break;
502  }
503  default:
504  iiExprArithM(res, l.args, cmd_n);
505  }
506  boost::python::object real_res=buildPyObjectFromLeftv(res);
507  res->CleanUp();
510  return real_res;
511  //cleanup not to forget
512  }
513 }
#define TRUE
Definition: auxiliary.h:100
char name(const Variable &v)
Definition: factory.h:189
VAR short errorreported
Definition: feFopen.cc:23
static void free_leftv(leftv args)
EXTERN_VAR int inerror
BOOLEAN iiExprArith2(leftv res, leftv a, int op, leftv b, BOOLEAN proccall)
Definition: iparith.cc:8870
int IsCmd(const char *n, int &tok)
Definition: iparith.cc:9469
BOOLEAN iiExprArith1(leftv res, leftv a, int op)
Definition: iparith.cc:9059
BOOLEAN iiExprArithM(leftv res, leftv a, int op)
Definition: iparith.cc:9360
BOOLEAN iiExprArith3(leftv res, int op, leftv a, leftv b, leftv c)
Definition: iparith.cc:9269
EXTERN_VAR omBin sleftv_bin
Definition: ipid.h:145
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
sleftv * leftv
Definition: structs.h:61

◆ call_interpreter_method()

boost::python::object call_interpreter_method ( const idhdl_wrap proc,
const arg_list args 
)

Definition at line 456 of file interpreter_support.cc.

457 {
458  int err=iiMake_proc(proc.id, NULL, args.args);
460 
462 }
unsigned char * proc[NUM_PROC]
Definition: checklibs.c:16
BOOLEAN iiMake_proc(idhdl pn, package pack, leftv args)
Definition: iplib.cc:504
INST_VAR sleftv iiRETURNEXPR
Definition: iplib.cc:474
#define NULL
Definition: omList.c:12

◆ export_interpreter()

void export_interpreter ( )

Definition at line 529 of file interpreter_support.cc.

530 {
531  def("get_idhdl", get_idhdl);
532  boost::python::class_<arg_list>("i_arg_list")
533  .def("append", &arg_list::appendPoly)
534  .def("append", &arg_list::appendArray)
535  .def("append", &arg_list::appendNumber)
536  .def("append", &arg_list::appendint)
537  .def("append", &arg_list::appendIdeal)
538  .def("append", &arg_list::appendModule)
539  .def("append", &arg_list::appendPrelist)
540  .def("append", &arg_list::appendVector)
541  .def("append", &arg_list::appendRing)
542  .def("append", &arg_list::appendIntvec)
543  .def("append", &arg_list::appendString);
544  boost::python::class_<idhdl_wrap>("interpreter_id")
545  .def("is_zero", &idhdl_wrap::is_zero)
546  .def("is_proc", &idhdl_wrap::id_is_proc)
547  .def("print_type", &idhdl_wrap::print_type)
548  .def("write", &idhdl_wrap::writePoly)
549  .def("write", &idhdl_wrap::writeArray)
550  .def("write", &idhdl_wrap::writeNumber)
551  .def("write", &idhdl_wrap::writeint)
552  .def("write", &idhdl_wrap::writeIdeal)
553  .def("write", &idhdl_wrap::writeModule)
554  .def("write", &idhdl_wrap::writeVector)
555  .def("write", &idhdl_wrap::writeList)
556  .def("write", &idhdl_wrap::writeString)
557  .def("write", &idhdl_wrap::writeIntvec)
558  .def("write", &idhdl_wrap::writeRing)
559  .def("__str__", idhdl_as_str);
560  def("call_interpreter_method",call_interpreter_method);
561  def("cbm",call_builtin_method_general);
562  def("transfer_to_python",buildPyObjectFromIdhdl);
563  def("is_builtin", is_builtin);
564 }
void appendint(int p)
void appendArray(const NumpyArrayType &f)
void appendPrelist(arg_list &l)
void appendNumber(const Number &p)
void appendVector(const Vector &p)
void appendString(const char *s)
void appendIntvec(Intvec &iv)
void appendModule(const Module &p)
void appendPoly(const Poly &p)
void appendIdeal(const Ideal &p)
void appendRing(const Ring &r)
void writeIdeal(const Ideal &p)
void writeList(arg_list &f)
void writePoly(const Poly &p)
void writeString(const char *s)
void writeNumber(const Number &p)
void writeIntvec(const Intvec &iv)
void writeVector(const Vector &p)
void writeArray(const NumpyArrayType &f)
void writeRing(const Ring &r)
void writeModule(const Module &p)
void writeint(int p)
bool is_builtin(const char *name)
boost::python::object call_builtin_method_general(const char *name, arg_list &l)
static boost::python::str idhdl_as_str(idhdl_wrap iw)
static idhdl_wrap get_idhdl(const char *n)
boost::python::object call_interpreter_method(const idhdl_wrap &proc, const arg_list &args)
boost::python::object buildPyObjectFromIdhdl(const idhdl_wrap &id)

◆ free_leftv()

static void free_leftv ( leftv  args)
static

Definition at line 24 of file interpreter_support.cc.

25 {
26  args->CleanUp();
27  omFreeBin(args, sleftv_bin);
28 }
void CleanUp(ring r=currRing)
Definition: subexpr.cc:348

◆ get_idhdl()

static idhdl_wrap get_idhdl ( const char *  n)
static

Definition at line 524 of file interpreter_support.cc.

525 {
526  //idhdl ggetid(const char *n);
527  return idhdl_wrap(ggetid(n));
528 }
idhdl ggetid(const char *n)
Definition: ipid.cc:571

◆ idhdl_as_str()

static boost::python::str idhdl_as_str ( idhdl_wrap  iw)
static

Definition at line 514 of file interpreter_support.cc.

515 {
516  idhdl i=iw.id;
517  using boost::python::str;
518  //ring r=p.getRing();
519 
520  std::basic_stringstream<char> s;
521  s<<i;
522  return boost::python::str(s.str());
523 }
Definition: idrec.h:35
const CanonicalForm int s
Definition: facAbsFact.cc:51

◆ is_builtin()

bool is_builtin ( const char *  name)

Definition at line 56 of file interpreter_support.cc.

57 {
58  int cmd_n=-1;
59  IsCmd(name,cmd_n);
60  return (cmd_n!=-1);
61 }

◆ matrixFromArray()

matrix matrixFromArray ( const NumpyArrayType f)

Definition at line 37 of file interpreter_support.cc.

38 {
39  object o=f.attr("shape");
40  object o1=o[0];
41  object o2=o[1];
42  int l1=extract<int>(o1);
43  int l2=extract<int>(o2);
44  matrix m=mpNew(l1,l2);
45  for(int i=0;i<l1;i++)
46  {
47  for(int j=0;j<l2;j++)
48  {
49  Poly& x = boost::python::extract<Poly&>(f[boost::python::make_tuple(i,j)]);
50  poly p=x.as_poly();
51  MATELEM(m,i+1,j+1)=p;
52  }
53  }
54  return m;
55 }
Variable x
Definition: cfModGcd.cc:4082
int p
Definition: cfModGcd.cc:4078
FILE * f
Definition: checklibs.c:9
matrix mpNew(int r, int c)
create a r x c zero-matrix
Definition: matpol.cc:37

Variable Documentation

◆ inerror

EXTERN_VAR int inerror

Definition at line 21 of file interpreter_support.cc.