Changeset 0a8b32 in git
- Timestamp:
- Feb 27, 2017, 2:46:18 PM (7 years ago)
- Branches:
- (u'spielwiese', '8e0ad00ce244dfd0756200662572aef8402f13d5')
- Children:
- 5c773a32c223f8f216df09eef7badedc4d0754a6
- Parents:
- e059d4cdef9e5f024a0949809f58353d2dee7a11
- Location:
- Singular/dyn_modules/python
- Files:
-
- 1 deleted
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/dyn_modules/python/IIntvec.h
re059d4 r0a8b32 3 3 #include <vector> 4 4 #include <misc/intvec.h> 5 class Intvec: public std::vector<int>{ 5 class Intvec: public std::vector<int> 6 { 6 7 public: 7 Intvec(iterator first, 8 iterator last, 9 const allocator_type& __a = allocator_type()): 10 std::vector<int>(first,last,__a){ 11 } 8 Intvec(iterator first, 9 iterator last, 10 const allocator_type& __a = allocator_type()): 11 std::vector<int>(first,last,__a){} 12 12 Intvec(int n=0):std::vector<int>(n){} 13 Intvec(intvec& iv):std::vector<int>(iv.length()){ 13 Intvec(intvec& iv):std::vector<int>(iv.length()) 14 { 14 15 int n=iv.length(); 15 for(int i=0;i<n;i++){ 16 for(int i=0;i<n;i++) 17 { 16 18 (*this)[i]=iv[i]; 17 19 } 18 20 } 19 intvec* allocate_legacy_intvec_copy() const{ 21 intvec* allocate_legacy_intvec_copy() const 22 { 20 23 int s=size(); 21 24 intvec* iv=new intvec(s); 22 23 for(int i=0;i<s;i++){ 25 26 for(int i=0;i<s;i++) 27 { 24 28 (*iv)[i]=(*this)[i]; 25 29 } -
Singular/dyn_modules/python/Ideal.h
re059d4 r0a8b32 6 6 //base for ideals as well for modules 7 7 //doesn't need a destructor, as Polys will destroy itself 8 template <class poly_type> class IdealBase { 8 template <class poly_type> class IdealBase 9 { 9 10 protected: 10 11 std::vector<poly_type> storage; … … 15 16 typedef typename std::vector<poly_type>::difference_type difference_type; 16 17 typedef typename std::vector<poly_type>::allocator_type allocator_type; 17 IdealBase(){ 18 } 19 20 IdealBase(iterator first, 21 iterator last, 22 const typename 23 std::vector<poly_type>::allocator_type& __a = allocator_type()): 18 IdealBase(){} 19 20 IdealBase(iterator first, 21 iterator last, 22 const typename 23 std::vector<poly_type>::allocator_type& __a = allocator_type()): 24 24 storage(first,last,__a) 25 { 26 27 } 28 ring getRing() const{ 25 {} 26 ring getRing() const 27 { 29 28 //FIXME: is a hack 30 if (size()>0){ 29 if (size()>0) 30 { 31 31 return storage[0].getRing(); 32 32 } 33 33 else 34 return (ring) NULL;34 return (ring) NULL; 35 35 } 36 poly_type& operator[] (int n){ 36 poly_type& operator[] (int n) 37 { 37 38 return storage[n]; 38 39 } 39 const poly_type& operator[](int n) const{ 40 const poly_type& operator[](int n) const 41 { 40 42 return storage[n]; 41 43 } 42 void push_back(const poly_type& p){ 44 void push_back(const poly_type& p) 45 { 43 46 storage.push_back(p); 44 47 } 45 void push_front(const poly_type& p){ 48 void push_front(const poly_type& p) 49 { 46 50 storage.push_front(p); 47 51 } 48 52 49 iterator begin(){ 53 iterator begin() 54 { 50 55 return storage.begin(); 51 56 } 52 iterator end(){ 57 iterator end() 58 { 53 59 return storage.end(); 54 60 } 55 size_type size() const{ 61 size_type size() const 62 { 56 63 return storage.size(); 57 64 } 58 65 iterator 59 insert(iterator __position, const value_type& __x){ 60 return storage.insert(__position,__x); 61 } 66 insert(iterator __position, const value_type& __x) 67 { 68 return storage.insert(__position,__x); 69 } 62 70 iterator 63 erase(iterator __position){ 64 return storage.erase(__position); 65 } 71 erase(iterator __position) 72 { 73 return storage.erase(__position); 74 } 66 75 iterator 67 erase(iterator __first, iterator __last){ 68 return storage.erase(__first,__last); 69 } 70 void insert(iterator __pos, iterator __first, iterator __last){ 76 erase(iterator __first, iterator __last) 77 { 78 return storage.erase(__first,__last); 79 } 80 void insert(iterator __pos, iterator __first, iterator __last) 81 { 71 82 return insert(__pos,__first,__last); 72 83 } 73 74 84 }; 75 85 76 86 class Ideal: 77 public IdealBase<Poly>{ 87 public IdealBase<Poly> 88 { 78 89 public: 79 Ideal(){ 80 } 81 Ideal(ideal i, ring r){ 82 for(int j=0;j<IDELEMS(i);j++){ 90 Ideal(){} 91 Ideal(ideal i, ring r) 92 { 93 for(int j=0;j<IDELEMS(i);j++) 94 { 83 95 storage.push_back(Poly(i->m[j],r)); 84 96 } 85 97 } 86 Ideal(iterator first, 87 88 89 IdealBase<Poly>(first,last,__a){ 90 }91 ideal as_ideal() const{98 Ideal(iterator first, 99 iterator last, 100 const allocator_type& __a = allocator_type()): 101 IdealBase<Poly>(first,last,__a){} 102 ideal as_ideal() const 103 { 92 104 //no checks for rings 93 105 int s=size(); 94 95 if (s==0) 96 s=1; 97 106 107 if (s==0) s=1; 108 98 109 ideal result=idInit(s,1); 99 110 result->m[0]=NULL; 100 111 s=size(); 101 for(int i=0;i<s;i++){ 112 for(int i=0;i<s;i++) 113 { 102 114 result->m[i]=storage[i].as_poly(); 103 115 } … … 106 118 }; 107 119 class Module: 108 public IdealBase<Vector>{ 120 public IdealBase<Vector> 121 { 109 122 public: 110 Module(ideal i, ring r){ 111 for(int j=0;j<IDELEMS(i);j++){ 123 Module(ideal i, ring r) 124 { 125 for(int j=0;j<IDELEMS(i);j++) 126 { 112 127 storage.push_back(Vector(i->m[j],r)); 113 128 } 114 129 } 115 ideal as_module() const {116 130 ideal as_module() const 131 { 117 132 //no checks for rings 118 133 int s=size(); 119 134 120 135 if (s==0) 121 136 s=1; 122 137 123 138 ideal result=idInit(s,1); 124 139 result->m[0]=NULL; 125 140 s=size(); 126 for(int i=0;i<s;i++){ 141 for(int i=0;i<s;i++) 142 { 127 143 result->m[i]=storage[i].as_poly(); 128 129 144 } 130 145 if (size()==0) … … 133 148 result->rank=id_RankFreeModule(result,storage[0].getRing()); 134 149 return result; 135 136 150 } 137 Module(iterator first, 138 139 151 Module(iterator first, 152 iterator last, 153 const allocator_type& __a = allocator_type()): 140 154 IdealBase<Vector>(first,last,__a) 141 { 155 {} 142 156 Module() 143 { 157 {} 144 158 }; 145 159 #endif -
Singular/dyn_modules/python/Makefile
re059d4 r0a8b32 3 3 PYTHON_VERSION=2.7 4 4 SINGULAR_BASE=../../.. 5 CFLAGS = -DSING_NDEBUG -DSING_OMNDEBUG -I. -I${SINGULAR_BASE} -I${SINGULAR_BASE}/kernel -I${SINGULAR_BASE}/Singular -I/usr/include/python${PYTHON_VERSION} -I/tmp2/wawa -I${SINGULAR_BASE}/libpolys -I/tmp2/wawa/libpolys -I${SINGULAR_BASE}/factory/include 6 LDFLAGS= -lboost_python -lpython${PYTHON_VERSION} 5 CFLAGS = -DSING_NDEBUG -DSING_OMNDEBUG -DDYNAMIC_VERSION \ 6 -I. -I${SINGULAR_BASE} -I${SINGULAR_BASE}/Singular -I${SINGULAR_BASE}/libpolys -I/tmp2/wawa/libpolys -I/usr/include/python${PYTHON_VERSION} -I/tmp2/wawa -I${SINGULAR_BASE}/factory/include 7 LDFLAGS= -lboost_python-${PYTHON_VERSION} -lpython${PYTHON_VERSION} 7 8 #LD = 8 9 MKINSTALLDIRS=mkdir -p … … 12 13 INSTALL_DATA = ${INSTALL} -m 644 13 14 SRCS = python.cc poly_wrap.cc vector_wrap.cc CF_wrap.cc\ 14 Åumber_wrap.cc 15 Åumber_wrap.cc ring_wrap.cc ideal_wrap.cc intvec_wrap.cc \ 16 interpreter_support.cc 15 17 OBJS = python.o poly_wrap.o vector_wrap.o CF_wrap.o\ 16 number_wrap.o 18 number_wrap.o ring_wrap.o ideal_wrap.o intvec_wrap.o \ 19 interpreter_support.o 17 20 18 21 all: python_module.so 19 22 20 %.o: %.cc Makefile23 %.o: %.cc 21 24 ${CXX} ${CFLAGS} -c -fPIC -DPIC $< -o $*.o 22 25 -
Singular/dyn_modules/python/Poly.h
re059d4 r0a8b32 21 21 22 22 }; 23 class ExceptionBasedErrorHandler{ 23 class ExceptionBasedErrorHandler 24 { 24 25 public: 25 26 static const bool handleErrors=true; 26 static void handleDifferentRing(ring r, ring s){ 27 PrintS("throwing"); 28 throw DifferentDomainException(); 29 } 27 static void handleDifferentRing(ring r, ring s) 28 { 29 PrintS("throwing"); 30 throw DifferentDomainException(); 31 } 30 32 }; 31 33 32 34 //PolyImpl is a 08/15 poly wrapper 33 35 //Poly wraps around PolyImpl with reference counting using boost 34 class TrivialErrorHandler{ 36 class TrivialErrorHandler 37 { 35 38 public: 36 39 static const bool handleErrors=false; 37 static void handleDifferentRing(ring r, ring s) {38 }40 static void handleDifferentRing(ring r, ring s) 41 {} 39 42 }; 40 43 typedef TrivialErrorHandler MyErrorHandler; -
Singular/dyn_modules/python/PowerSeries.h
re059d4 r0a8b32 8 8 std::input_iterator_tag, 9 9 typename traits::expansion_type, 10 int, 10 int, 11 11 shared_ptr<const typename traits::expansion_type>, 12 const typename traits::expansion_type 13 > { 12 const typename traits::expansion_type 13 > 14 { 14 15 private: 15 16 typedef typename traits::denominator_type denominator_type; … … 23 24 denominator_type lastPot; 24 25 public: 25 PowerSeriesInputIterator(numerator_type num_arg, 26 27 data(den_arg.getRing()), 26 PowerSeriesInputIterator(numerator_type num_arg, 27 denominator_type den_arg): 28 data(den_arg.getRing()), 28 29 lastPot(den_arg.getRing()), 29 30 numerator(num_arg), 30 denominator(den_arg){ 31 32 ring r=denominator.getRing(); 33 34 //not the lead coef Number c=denominator.leadCoef(); 35 Number c(1,r); 36 typename traits::denominator_type::iterator it=denominator.begin(); 37 typename traits::denominator_type::iterator end=denominator.end(); 38 while(it!=end){ 39 40 if ((*it).isConstant()){ 41 //change this type 42 c=denominator_type(*it).leadCoef(); 43 44 break; 31 denominator(den_arg) 32 { 33 ring r=denominator.getRing(); 34 //not the lead coef Number c=denominator.leadCoef(); 35 Number c(1,r); 36 typename traits::denominator_type::iterator it=denominator.begin(); 37 typename traits::denominator_type::iterator end=denominator.end(); 38 while(it!=end) 39 { 40 if ((*it).isConstant()) 41 { 42 //change this type 43 c=denominator_type(*it).leadCoef(); 44 break; 45 } 46 ++it; 45 47 } 46 47 ++it; 48 49 50 48 c=Number(1,r)/c; 49 numerator*=c; 50 denominator*=c; 51 toPot=denominator+denominator_type(-1,r); 52 toPot*=Number(-1,r); 53 //change this type 54 lastPot=denominator_type(1,r); 55 data=numerator; 56 state=0; 51 57 } 52 c=Number(1,r)/c; 53 numerator*=c; 54 denominator*=c; 55 toPot=denominator+denominator_type(-1,r); 56 57 toPot*=Number(-1,r); 58 //change this type 59 lastPot=denominator_type(1,r); 60 data=numerator; 61 state=0; 62 63 } 64 PowerSeriesInputIterator(){ 58 PowerSeriesInputIterator() 59 { 65 60 state=-1; 66 61 } 67 void shorten(){ 62 void shorten() 63 { 68 64 typename expansion_type::iterator it=data.begin(); 69 65 typename expansion_type::iterator end=data.end(); 70 66 ring r=data.getRing(); 71 67 expansion_type remove(r); 72 while(it!=end){ 73 if(it->lmTotalDegree()<state){ 74 remove+=expansion_type(*it); 68 while(it!=end) 69 { 70 if(it->lmTotalDegree()<state) 71 { 72 remove+=expansion_type(*it); 75 73 } 76 74 it++; … … 79 77 data+=remove; 80 78 } 81 expansion_type getValue(){ 79 expansion_type getValue() 80 { 82 81 typename expansion_type::iterator it=data.begin(); 83 82 typename expansion_type::iterator end=data.end(); 84 83 ring r=data.getRing(); 85 84 expansion_type res(r); 86 while(it!=end){ 85 while(it!=end) 86 { 87 87 if(it->lmTotalDegree()==state) 88 89 90 88 { 89 res+=expansion_type(*it); 90 } 91 91 it++; 92 92 } 93 93 return res; 94 94 } 95 PowerSeriesInputIterator& operator++(){ 95 PowerSeriesInputIterator& operator++() 96 { 96 97 state++; 97 98 shorten(); 98 99 lastPot*=toPot; 99 100 100 data+=lastPot*numerator; 101 102 103 101 return *this; 104 105 102 } 106 103 //bad if this are iterators for different PowerSeries 107 bool operator==(const PowerSeriesInputIterator& t2){ 104 bool operator==(const PowerSeriesInputIterator& t2) 105 { 108 106 return state==t2.state; 109 107 } 110 bool operator!=(const PowerSeriesInputIterator& t2){ 108 bool operator!=(const PowerSeriesInputIterator& t2) 109 { 111 110 return state!=t2.state; 112 111 } 113 114 115 116 PowerSeriesInputIterator operator++(int){ 112 PowerSeriesInputIterator operator++(int) 113 { 117 114 PowerSeriesInputIterator it(*this); 118 115 ++(*this); 119 116 return it; 120 117 } 121 const expansion_type operator*(){ 118 const expansion_type operator*() 119 { 122 120 return expansion_type(getValue()); 123 121 } 124 shared_ptr<const expansion_type> operator->(){ 122 shared_ptr<const expansion_type> operator->() 123 { 125 124 return shared_ptr<const expansion_type>(new expansion_type(getValue())); 126 125 } 127 126 }; 128 127 129 128 130 template<class traits> class PowerSeriesBase{ 129 template<class traits> class PowerSeriesBase 130 { 131 131 public: 132 132 typedef typename traits::denominator_type denominator_type; … … 136 136 numerator_type numerator; 137 137 public: 138 139 PowerSeriesBase(){ 140 } 141 PowerSeriesBase(const numerator_type &a, const denominator_type & b):numerator(a),denominator(b){ 138 PowerSeriesBase(){} 139 PowerSeriesBase(const numerator_type &a, const denominator_type & b):numerator(a),denominator(b) 140 { 142 141 assume(a.getRing()==b.getRing()); 143 142 //asume b!=NULL 144 143 } 145 144 typedef PowerSeriesInputIterator<traits> iterator; 146 iterator begin(){ 145 iterator begin() 146 { 147 147 return iterator(numerator,denominator); 148 148 } 149 iterator end(){ 149 iterator end() 150 { 150 151 return iterator(); 151 152 } 152 153 154 153 }; 155 154 class PowerSeriesPolyTraits; … … 157 156 typedef PowerSeriesBase<PowerSeriesPolyTraits> PowerSeries; 158 157 typedef PowerSeriesBase<PowerSeriesVectorTraits> VectorPowerSeries; 159 class PowerSeriesPolyTraits{ 158 class PowerSeriesPolyTraits 159 { 160 160 public: 161 161 typedef Poly numerator_type; … … 164 164 typedef Poly expansion_type; 165 165 }; 166 class PowerSeriesVectorTraits{ 166 class PowerSeriesVectorTraits 167 { 167 168 public: 168 169 typedef Vector numerator_type; … … 172 173 }; 173 174 174 175 175 #endif -
Singular/dyn_modules/python/interpreter_support.cc
re059d4 r0a8b32 1 2 1 #include <sstream> 3 2 #include <boost/python.hpp> … … 7 6 #include "Poly.h" 8 7 #include "Ideal.h" 9 #include " lists.h"10 #include " structs.h"11 #include " ipshell.h"12 #include " numbers.h"13 14 #include "febase.h" 15 #include " ipid.h"16 #include " ipshell.h"17 #include " matpol.h"8 #include "kernel/structs.h" 9 #include "Singular/lists.h" 10 #include "Singular/ipid.h" 11 #include "Singular/ipshell.h" 12 #include "coeffs/numbers.h" 13 14 #include "Singular/ipid.h" 15 #include "Singular/ipshell.h" 16 #include "polys/matpol.h" 18 17 #include "ring_wrap.h" 19 18 #include "intvec_wrap.h" 20 19 #include "poly_wrap.h" 21 extern BOOLEAN errorreported;22 20 extern int inerror; 23 21 using namespace boost::python; 24 22 using boost::python::numeric::array; 25 23 using boost::python::extract; 26 static void free_leftv(leftv args){ 24 static void free_leftv(leftv args) 25 { 27 26 args->CleanUp(); 28 27 omFreeBin(args, sleftv_bin); 29 28 } 30 29 31 matrix matrixFromArray(const array& f){ 30 matrix matrixFromArray(const boost::python::numeric::array& f) 31 { 32 32 object o=f.attr("shape"); 33 34 33 object o1=o[0]; 35 36 34 object o2=o[1]; 37 35 int l1=extract<int>(o1); 38 39 40 36 int l2=extract<int>(o2); 41 37 matrix m=mpNew(l1,l2); 42 for(int i=0;i<l1;i++){ 43 for(int j=0;j<l2;j++){ 38 for(int i=0;i<l1;i++) 39 { 40 for(int j=0;j<l2;j++) 41 { 44 42 Poly& x = boost::python::extract<Poly&>(f[boost::python::make_tuple(i,j)]); 45 43 poly p=x.as_poly(); … … 49 47 return m; 50 48 } 51 bool is_builtin(const char* name){ 49 bool is_builtin(const char* name) 50 { 52 51 int cmd_n=-1; 53 52 IsCmd(name,cmd_n); 54 53 return (cmd_n!=-1); 55 54 } 56 class arg_list{ 55 class arg_list 56 { 57 57 public: 58 58 leftv args; 59 arg_list(){ 59 arg_list() 60 { 60 61 args=(leftv) NULL; 61 62 } 62 ~arg_list(){ 63 if (args!=NULL){ 63 ~arg_list() 64 { 65 if (args!=NULL) 66 { 64 67 args->CleanUp(); 65 68 omFreeBin(args, sleftv_bin); 66 69 } 67 68 }69 leftv pop_front(){70 } 71 leftv pop_front() 72 { 70 73 assume(args!=NULL); 71 74 leftv res=args; … … 74 77 return res; 75 78 } 76 int length(){ 79 int length() 80 { 77 81 leftv v=args; 78 82 int l=0; 79 while(v!=NULL){ 83 while(v!=NULL) 84 { 80 85 l++; 81 86 v=v->next; … … 83 88 return l; 84 89 } 85 void appendPoly(const Poly& p){ 90 void appendPoly(const Poly& p) 91 { 86 92 leftv v=initArg(); 87 93 v->data=p.as_poly(); … … 89 95 internal_append(v); 90 96 } 91 void appendIdeal(const Ideal& p){ 97 void appendIdeal(const Ideal& p) 98 { 92 99 leftv v=initArg(); 93 100 v->data=p.as_ideal(); … … 95 102 internal_append(v); 96 103 } 97 void appendModule(const Module& p){ 104 void appendModule(const Module& p) 105 { 98 106 leftv v=initArg(); 99 107 v->data=p.as_module(); … … 101 109 internal_append(v); 102 110 } 103 void appendint(int p){ 104 leftv v=initArg(); 111 void appendint(int p) 112 { 113 leftv v=initArg(); 105 114 v->data=(void*)((long)p); 106 115 v->rtyp=INT_CMD; 107 116 internal_append(v); 108 117 } 109 void appendNumber(const Number& p){ 110 leftv v=initArg(); 118 void appendNumber(const Number& p) 119 { 120 leftv v=initArg(); 111 121 v->data=(void*) p.as_number(); 112 122 v->rtyp=NUMBER_CMD; 113 123 internal_append(v); 114 124 } 115 void appendVector(const Vector& p){ 125 void appendVector(const Vector& p) 126 { 116 127 leftv v=initArg(); 117 128 v->data=p.as_poly(); … … 119 130 internal_append(v); 120 131 } 121 void appendArray(const array& f){ 132 void appendArray(const boost::python::numeric::array& f) 133 { 122 134 leftv v=initArg(); 123 135 matrix m=matrixFromArray(f); … … 125 137 v->rtyp=MATRIX_CMD; 126 138 internal_append(v); 127 128 }129 void appendString(const char* s){139 } 140 void appendString(const char* s) 141 { 130 142 leftv v=initArg(); 131 143 v->data=omStrDup(s); … … 133 145 internal_append(v); 134 146 } 135 void appendRing(const Ring& r){ 147 void appendRing(const Ring& r) 148 { 136 149 leftv v=initArg(); 137 150 v->data=r.pimpl.get(); … … 140 153 internal_append(v); 141 154 } 142 143 lists dumpToLists(){155 lists dumpToLists() 156 { 144 157 int n=length(); 145 146 158 lists res=(lists)omAlloc0Bin(slists_bin); 147 159 res->Init(n); 148 for(int i=0;i<n;i++){ 149 leftv iv=pop_front(); 150 //swap the content 151 memcpy(&res->m[i],iv,sizeof(sleftv)); 152 //iv->Init(); 153 omFreeBin(iv, sleftv_bin); 160 for(int i=0;i<n;i++) 161 { 162 leftv iv=pop_front(); 163 //swap the content 164 memcpy(&res->m[i],iv,sizeof(sleftv)); 165 //iv->Init(); 166 omFreeBin(iv, sleftv_bin); 154 167 } 155 168 return res; 156 157 }158 void appendPrelist(arg_list& l){169 } 170 void appendPrelist(arg_list& l) 171 { 159 172 leftv v=initArg(); 160 173 v->data=l.dumpToLists(); 161 162 174 v->rtyp=LIST_CMD; 163 175 internal_append(v); 164 176 } 165 void appendIntvec(Intvec& iv){ 177 void appendIntvec(Intvec& iv) 178 { 166 179 leftv v=initArg(); 167 180 v->data=iv.allocate_legacy_intvec_copy(); 168 169 181 v->rtyp=INTVEC_CMD; 170 182 internal_append(v); 171 183 } 172 173 protected:174 leftv initArg(){184 protected: 185 leftv initArg() 186 { 175 187 leftv res=(leftv)omAllocBin(sleftv_bin); 176 188 res->Init(); 177 189 return res; 178 190 } 179 void internal_append(leftv v){ 180 if (args!=NULL){ 191 void internal_append(leftv v) 192 { 193 if (args!=NULL) 194 { 181 195 leftv last=args; 182 while(last->next!=NULL){ 196 while(last->next!=NULL) 197 { 183 198 last=last->next; 184 199 } 185 200 last->next=v; 186 } else 201 } 202 else 187 203 args=v; 188 204 } 189 190 205 }; 191 206 192 class idhdl_wrap{ 207 class idhdl_wrap 208 { 193 209 public: 194 210 idhdl id; 195 idhdl_wrap(idhdl id){ 211 idhdl_wrap(idhdl id) 212 { 196 213 this->id=id; 197 214 } 198 idhdl_wrap(){ 215 idhdl_wrap() 216 { 199 217 id=(idhdl) NULL; 200 218 } 201 bool is_zero(){ 219 bool is_zero() 220 { 202 221 return id==NULL; 203 222 } 204 bool id_is_proc(){ 223 bool id_is_proc() 224 { 205 225 return (id->typ==PROC_CMD); 206 226 } 207 bool print_type(){ 227 bool print_type() 228 { 208 229 Print("type:%d\n",id->typ); 209 230 } 210 void writePoly(const Poly& p){ 211 212 if (id->typ==POLY_CMD){ 231 void writePoly(const Poly& p) 232 { 233 if (id->typ==POLY_CMD) 234 { 213 235 p_Delete(&id->data.p, currRing); 214 236 id->data.p=p.as_poly(); 215 237 } 216 217 } 218 void writeIdeal(const Ideal& p){ 219 if (id->typ==IDEAL_CMD){ 238 } 239 void writeIdeal(const Ideal& p) 240 { 241 if (id->typ==IDEAL_CMD) 242 { 220 243 id_Delete(&id->data.uideal, currRing); 221 222 244 id->data.uideal=p.as_ideal(); 223 245 } 224 246 } 225 void writeModule(const Module& p){ 226 if (id->typ==MODUL_CMD){ 247 void writeModule(const Module& p) 248 { 249 if (id->typ==MODUL_CMD) 250 { 227 251 id_Delete(&id->data.uideal, currRing); 228 229 252 id->data.uideal=p.as_module(); 230 253 } 231 254 } 232 void writeint(int p){ 233 if (id->typ==INT_CMD){ 255 void writeint(int p) 256 { 257 if (id->typ==INT_CMD) 258 { 234 259 id->data.i=p; 235 260 } 236 261 } 237 void writeNumber(const Number& p){ 238 239 if (id->typ==NUMBER_CMD){ 262 void writeNumber(const Number& p) 263 { 264 if (id->typ==NUMBER_CMD) 265 { 240 266 n_Delete(&id->data.n, currRing); 241 267 id->data.n=p.as_number(); 242 268 } 243 269 } 244 void writeVector(const Vector& p){ 245 246 if (id->typ==VECTOR_CMD){ 270 void writeVector(const Vector& p) 271 { 272 if (id->typ==VECTOR_CMD) 273 { 247 274 p_Delete(&id->data.p, currRing); 248 275 id->data.p=p.as_poly(); 249 276 } 250 277 } 251 void writeArray(const array& f){ 252 if(id->typ=MATRIX_CMD){ 278 void writeArray(const boost::python::numeric::array& f) 279 { 280 if(id->typ=MATRIX_CMD) 281 { 253 282 matrix m=matrixFromArray(f); 254 283 id_Delete((ideal*) &id->data.umatrix,currRing); … … 256 285 } 257 286 } 258 void writeRing(const Ring& r){ 259 if(id->typ=RING_CMD){ 287 void writeRing(const Ring& r) 288 { 289 if(id->typ=RING_CMD) 290 { 260 291 r.pimpl->ref++; 261 262 292 ((ring) id->data.uring)->ref--;//FIXME: destruct it 263 293 ring r2=r.pimpl.get(); … … 265 295 } 266 296 } 267 void writeString(const char* s){ 268 if(id->typ=STRING_CMD){ 297 void writeString(const char* s) 298 { 299 if(id->typ=STRING_CMD) 300 { 269 301 omFree((ADDRESS) id->data.ustring); 270 302 id->data.ustring=omStrDup(s); 271 303 } 272 304 } 273 void writeIntvec(const Intvec& iv){ 274 if(id->typ=INTVEC_CMD){ 305 void writeIntvec(const Intvec& iv) 306 { 307 if(id->typ=INTVEC_CMD) 308 { 275 309 delete id->data.iv; 276 310 id->data.iv=iv.allocate_legacy_intvec_copy();; 277 311 } 278 312 } 279 void writeList(arg_list& f){ 313 void writeList(arg_list& f) 314 { 280 315 //warning f gets empty 281 if(id->typ=LIST_CMD){ 316 if(id->typ=LIST_CMD) 317 { 282 318 id->data.l->Clean(currRing); 283 319 id->data.l=f.dumpToLists(); 284 320 } 285 }321 } 286 322 }; 287 323 288 324 289 static array buildPythonMatrix(matrix m, ring r){ 325 static boost::python::numeric::array buildPythonMatrix(matrix m, ring r) 326 { 290 327 using boost::python::numeric::array; 291 328 using boost::python::self; … … 296 333 297 334 list l; 298 299 300 for(int i=1;i<=MATROWS(m);i++){ 335 for(int i=1;i<=MATROWS(m);i++) 336 { 301 337 list row; 302 for(int j=1;j<=MATCOLS(m);j++){ 338 for(int j=1;j<=MATCOLS(m);j++) 339 { 303 340 Poly ip(MATELEM(m,i,j),r);//copy it 304 341 row.append(ip); … … 309 346 } 310 347 //FIXME: should call this only once 311 array::set_module_and_type("Numeric",348 boost::python::numeric::array::set_module_and_type("Numeric", 312 349 "ArrayType" 313 350 ); 314 315 return array(l); 351 return boost::python::numeric::array(l); 316 352 } 317 353 boost::python::object buildPyObjectFromLeftv(leftv v); 318 boost::python::list buildPythonList(lists l, ring r){ 354 boost::python::list buildPythonList(lists l, ring r) 355 { 319 356 using boost::python::list; 320 357 list res; 321 358 322 for(int i=0;i<=l->nr;i++){ 359 for(int i=0;i<=l->nr;i++) 360 { 323 361 leftv lv=&l->m[i]; 324 362 object o=buildPyObjectFromLeftv(lv); … … 328 366 } 329 367 330 boost::python::object buildPyObjectFromLeftv(leftv v){ 368 boost::python::object buildPyObjectFromLeftv(leftv v) 369 { 331 370 using boost::python::object; 332 switch (v->rtyp){ 371 switch (v->rtyp) 372 { 333 373 case INT_CMD: 334 374 return object((int)((long)v->data)); … … 362 402 using boost::python::object; 363 403 364 switch (id.id->typ){ 404 switch (id.id->typ) 405 { 365 406 case STRING_CMD: 366 407 return str((const char*) id.id->data.ustring); … … 402 443 } 403 444 404 boost::python::object call_interpreter_method(const idhdl_wrap& proc, const arg_list& args){ 445 boost::python::object call_interpreter_method(const idhdl_wrap& proc, const arg_list& args) 446 { 405 447 //idhdl oldPackHDL=currPackHdl; 406 448 … … 410 452 //FIXME: will call procedure from different package, maybe use iiMakeProc 411 453 412 return buildPyObjectFromLeftv(iiMake_proc(proc.id,NULL,args.args));413 454 int err=iiPStart(proc.id, args.args); 414 455 //currPack=oldPack; … … 417 458 errorreported=inerror=0; 418 459 419 return buildPyObjectFromLeftv(&iiRETURNEXPR [voice]);460 return buildPyObjectFromLeftv(&iiRETURNEXPR); 420 461 421 462 //return res; 422 463 } 423 boost::python::object call_builtin_method_general(const char* name, arg_list& l){ 424 425 464 boost::python::object call_builtin_method_general(const char* name, arg_list& l) 465 { 426 466 int cmd_n=-1; 427 467 IsCmd(name,cmd_n); … … 429 469 430 470 // return Py_None; 431 if (cmd_n<0){ 432 433 434 return object(); 435 436 } else { 437 438 471 if (cmd_n<0) 472 { 473 return object(); 474 } 475 else 476 { 439 477 leftv res=(leftv)omAllocBin(sleftv_bin); 440 478 res->Init(); 441 switch(l.length()){ 479 switch(l.length()) 480 { 442 481 case 1: 443 482 iiExprArith1(res,l.args,cmd_n); … … 457 496 leftv arg2=l.pop_front(); 458 497 leftv arg3=l.pop_front(); 459 460 461 498 iiExprArith3(res,cmd_n,arg1,arg2,arg3); 462 499 free_leftv(arg1); … … 476 513 } 477 514 } 478 static boost::python::str idhdl_as_str(idhdl_wrap iw){ 515 static boost::python::str idhdl_as_str(idhdl_wrap iw) 516 { 479 517 idhdl i=iw.id; 480 518 using boost::python::str; 481 519 //ring r=p.getRing(); 482 520 483 484 521 std::basic_stringstream<char> s; 485 522 s<<i; 486 523 return boost::python::str(s.str()); 487 524 } 488 static idhdl_wrap get_idhdl(const char *n){ 489 //idhdl ggetid(const char *n, BOOLEAN local = FALSE); 490 return idhdl_wrap(ggetid(n, FALSE)); 525 static idhdl_wrap get_idhdl(const char *n) 526 { 527 //idhdl ggetid(const char *n); 528 return idhdl_wrap(ggetid(n)); 491 529 } 492 530 void export_interpreter() … … 525 563 def("transfer_to_python",buildPyObjectFromIdhdl); 526 564 def("is_builtin", is_builtin); 527 528 } 529 530 565 } 566 -
Singular/dyn_modules/python/intvec_wrap.cc
re059d4 r0a8b32 2 2 #include <boost/python/suite/indexing/vector_indexing_suite.hpp> 3 3 #include <kernel/mod2.h> 4 #include "IIntvec.h" 4 5 #include "intvec_wrap.h" 5 6 -
Singular/dyn_modules/python/intvec_wrap.h
re059d4 r0a8b32 1 1 #ifndef INTVEC_WRAP_HEADER 2 2 #define INTVEC_WRAP_HEADER 3 #include <vector> 4 #include <kernel/mod2.h> 5 #include "misc/intvec.h" 6 #include "IIntvec.h" 3 7 4 void export_intvec(); 8 5 9 10 6 #endif -
Singular/dyn_modules/python/mywrapper.cc
re059d4 r0a8b32 4 4 #include "wrapper.h" 5 5 6 void initPySingular() { 6 void initPySingular() 7 { 7 8 initSingular(); 8 9 init_Singular(); -
Singular/dyn_modules/python/playground.cc
re059d4 r0a8b32 25 25 } 26 26 boost::python::numeric::array::set_module_and_type("Numeric", 27 28 27 "ArrayType" 28 ); 29 29 boost::python::numeric::array a(l); 30 30 return a; -
Singular/dyn_modules/python/python.cc
re059d4 r0a8b32 10 10 #include <kernel/mod2.h> 11 11 #include <tok.h> 12 #include <structs.h> 12 #include <kernel/structs.h> 13 #include <Singular/mod_lib.h> 13 14 #include <ipid.h> 14 15 15 16 #include <locals.h> 16 17 #include <omalloc/omalloc.h> 17 #include "python.h"18 18 19 19 #include <stdio.h> … … 24 24 #include "wrapper.h" 25 25 26 //int mod_init( 27 // int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic, 28 // BOOLEAN(*func)(leftv res, leftv v)) 29 // ) 30 extern "C" int mod_init( SModulFunctions* psModulFunctions) 31 { 32 Py_Initialize(); 33 PyRun_SimpleString("from sys import path\n\ 34 path.insert(0,'.')\n"); 35 initSingular(); 36 init_Singular(); 37 38 psModulFunctions->iiAddCproc(currPack->libname,"python",FALSE, mod_python); 39 return 0; 40 } 41 42 extern "C" BOOLEAN mod_python(leftv __res, leftv __h) 26 static BOOLEAN mod_python(leftv __res, leftv __h) 43 27 { 44 28 leftv __v = __h, __v_save; … … 68 52 return TRUE; 69 53 } 54 //int mod_init( 55 // int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic, 56 // BOOLEAN(*func)(leftv res, leftv v)) 57 // ) 58 extern "C" int SI_MOD_INIT(python_module)(SModulFunctions* psModulFunctions) 59 { 60 Py_Initialize(); 61 PyRun_SimpleString("from sys import path\n\ 62 path.insert(0,'.')\n"); 63 initSingular(); 64 init_Singular(); 65 66 psModulFunctions->iiAddCproc(currPack->libname,"python",FALSE, mod_python); 67 return MAX_TOK; 68 } 69 -
Singular/dyn_modules/python/ring_wrap.cc
re059d4 r0a8b32 4 4 #include "ring_wrap.h" 5 5 #include "poly_wrap.h" 6 #include "febase.h"7 6 static boost::python::object Ring_as_str(const Ring& r) 8 7 { … … 10 9 StringSetS(""); 11 10 rWrite(r.pimpl.get()); 12 char* out=String AppendS("");11 char* out=StringEndS(); 13 12 return boost::python::str(out,strlen(out)); 14 13 } 15 void ring_set(Ring & r){ 14 void ring_set(Ring & r) 15 { 16 16 //FIXME: only a hack, no solution 17 17 char name_buffer[100]; … … 25 25 currRingHdl=shadow_hdl; 26 26 27 } 28 void export_ring(){ 27 } 28 void export_ring() 29 { 29 30 boost::python::class_<Ring>("Ring", "reference to a Singular ring") 30 31 .def("__str__", Ring_as_str) 31 32 .def("set", ring_set,"equivalent to the singular command setring, which is not mapped as it is a command") 32 33 .def(boost::python::init <>()); 33 34 34 } -
Singular/dyn_modules/python/ring_wrap.h
re059d4 r0a8b32 18 18 19 19 //typedef boosRing 20 class Ring{ 20 class Ring 21 { 21 22 public: 22 23 intrusive_ptr<ip_sring> pimpl; 23 Ring(ring r=currRing): pimpl(r){ 24 25 } 26 Ring(const Ring& r2):pimpl(r2.pimpl){ 27 28 } 29 ~Ring(){ 30 31 } 32 24 Ring(ring r=currRing): pimpl(r){} 25 Ring(const Ring& r2):pimpl(r2.pimpl){} 26 ~Ring(){} 33 27 }; 34 28 void export_ring(); -
Singular/dyn_modules/python/wrapper.h
re059d4 r0a8b32 7 7 8 8 9 #include " structs.h"9 #include "kernel/structs.h" 10 10 #include "coeffs/numbers.h" 11 11 12 //#include "febase.h" 13 #include "ipid.h" 14 #include "ipshell.h" 15 //#include "util.h" 12 #include "Singular/ipid.h" 13 #include "Singular/ipshell.h" 16 14 #include "Number.h" 17 15 #include "Poly.h" … … 24 22 #include "CF_wrap.h" 25 23 #include "number_wrap.h" 26 //#include "playground.h"27 24 #include "interpreter_support.h" 28 25 #include "ring_wrap.h" … … 33 30 using namespace boost::python; 34 31 35 Vector unitVector0(int i){ 32 Vector unitVector0(int i) 33 { 36 34 return unitVector(i,currRing); 37 35 } 38 36 39 40 41 42 43 44 45 //typedef void * idhdl; 46 void different_ring_translator(DifferentDomainException const& x) { 37 void different_ring_translator(DifferentDomainException const& x) 38 { 47 39 PrintS("hoho"); 48 40 PyErr_SetString(PyExc_UserWarning, "Objects didn't have the same ring"); 49 41 } 50 BOOST_PYTHON_MODULE(Singular){ 42 BOOST_PYTHON_MODULE(Singular) 43 { 51 44 //Print("ref count after add: %d", currRing->ref); 52 45 register_exception_translator< 53 46 DifferentDomainException>(different_ring_translator); 54 47 export_poly(); 55 56 57 58 48 export_number(); 59 49 export_vector(); … … 71 61 .def("__iter__", boost::python::iterator<VectorPowerSeries>()); 72 62 def("gen",unitVector0); 73 74 75 63 // .def(self+=self) 76 77 64 // .def(self+self) 78 65 //.def(self*=Number()) 79 66 //.def(Number() * self); 80 81 67 } 82 BOOST_PYTHON_MODULE(factory){ 68 BOOST_PYTHON_MODULE(factory) 69 { 83 70 boost::python::class_<Variable>("variable") 84 71 .def(boost::python::init <const int, char>()) 85 72 .def(boost::python::init <char>()) 86 87 73 .def(boost::python::init <const int>()); 88 74 export_CF(); 89 75 } 90 BOOST_PYTHON_MODULE(_Singular){ 76 BOOST_PYTHON_MODULE(_Singular) 77 { 91 78 export_interpreter(); 92 93 79 } 94 80 #endif
Note: See TracChangeset
for help on using the changeset viewer.