Ticket #4539: plural_functions.patch
| File plural_functions.patch, 20.7 KB (added by PolyBoRi, 3 years ago) |
|---|
-
sage/libs/singular/function.pxd
diff -r 54342f65c59c sage/libs/singular/function.pxd
a b 19 19 from sage.libs.singular.decl cimport ring as singular_ring 20 20 from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular, MPolynomial_libsingular 21 21 22 cdef poly* access_singular_poly(p) except <poly*> -1 23 cdef singular_ring* access_singular_ring(r) except <singular_ring*> -1 24 22 25 cdef class RingWrap: 23 26 cdef singular_ring *_ring 24 27 25 28 cdef class Resolution: 26 29 cdef syStrategy *_resolution 27 cdef MPolynomialRing_libsingularbase_ring30 cdef object base_ring 28 31 29 32 cdef class Converter(SageObject): 30 33 cdef leftv *args … … 44 47 cdef leftv * append_intvec(self, v) except NULL 45 48 cdef leftv * append_list(self, l) except NULL 46 49 cdef leftv * append_matrix(self, a) except NULL 47 cdef leftv * append_ring(self, MPolynomialRing_libsingularr) except NULL50 cdef leftv * append_ring(self, r) except NULL 48 51 cdef leftv * append_module(self, m) except NULL 49 52 cdef to_sage_integer_matrix(self, intvec *mat) 50 53 cdef object to_sage_module_element_sequence_destructive(self, ideal *i) … … 70 73 71 74 cdef BaseCallHandler get_call_handler(self) 72 75 cdef bint function_exists(self) 73 cdef MPolynomialRing_libsingularcommon_ring(self, tuple args, ring=?)76 cdef common_ring(self, tuple args, ring=?) 74 77 75 78 cdef class SingularLibraryFunction(SingularFunction): 76 79 pass -
sage/libs/singular/function.pyx
diff -r 54342f65c59c sage/libs/singular/function.pyx
a b 13 13 - Martin Albrecht (2009-07): clean up, enhancements, etc. 14 14 - Michael Brickenstein (2009-10): extension to more Singular types 15 15 - Martin Albrecht (2010-01): clean up, support for attributes 16 - Burcin Erocal (2010-7): plural support 17 - Michael Brickenstein (2010-7): plural support 16 18 17 19 EXAMPLES: 18 20 … … 81 83 from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular 82 84 83 85 from sage.rings.polynomial.plural cimport NCPolynomialRing_plural, \ 84 NCPolynomial_plural 86 NCPolynomial_plural, new_NCP 85 87 from sage.rings.polynomial.multi_polynomial_ideal import NCPolynomialIdeal 86 88 87 89 from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal … … 122 124 123 125 for (i, p) in enumerate(v): 124 126 component = <int>i+1 125 poly_component = p_Copy( 126 (<MPolynomial_libsingular>p)._poly, r) 127 poly_component = copy_sage_polynomial_into_singular_poly(p) 127 128 p_iter = poly_component 128 129 while p_iter!=NULL: 129 130 p_SetComp(p_iter, component, r) … … 132 133 res=p_Add_q(res, poly_component, r) 133 134 return res 134 135 136 135 137 cdef class RingWrap: 136 138 """ 137 139 A simple wrapper around Singular's rings. … … 170 172 sage: M = syz(I) 171 173 sage: resolution = mres(M, 0) 172 174 """ 173 assert PY_TYPE_CHECK(base_ring, MPolynomialRing_libsingular) 174 self.base_ring = <MPolynomialRing_libsingular> base_ring 175 #FIXME: still not working noncommutative 176 assert is_sage_wrapper_for_singular_ring(base_ring) 177 self.base_ring = base_ring 175 178 def __repr__(self): 176 179 """ 177 180 EXAMPLE:: … … 229 232 args.CleanUp() 230 233 omFreeBin(args, sleftv_bin) 231 234 235 # ===================================== 236 # = Singular/Plural Abstraction Layer = 237 # ===================================== 232 238 233 def all_polynomials(s): 239 def is_sage_wrapper_for_singular_ring(ring): 240 if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular): 241 return True 242 if PY_TYPE_CHECK(ring, NCPolynomialRing_plural): 243 return True 244 return False 245 246 cdef new_sage_polynomial(ring, poly *p): 247 if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular): 248 return new_MP(ring, p) 249 else: 250 if PY_TYPE_CHECK(ring, NCPolynomialRing_plural): 251 return new_NCP(ring, p) 252 raise ValueError("not a singular or plural ring") 253 254 def is_singular_poly_wrapper(p): 255 """ 256 checks if p is some data type corresponding to some singular ``poly```. 257 258 EXAMPLE:: 259 sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural 260 sage: from sage.matrix.constructor import Matrix 261 sage: from sage.libs.singular.function import is_singular_poly_wrapper 262 sage: c=Matrix(2) 263 sage: c[0,1]=-1 264 sage: P = NCPolynomialRing_plural(QQ, 2, 'x,y', c=c, d=Matrix(2)) 265 sage: (x,y)=P.gens() 266 sage: is_singular_poly_wrapper(x+y) 267 True 268 269 """ 270 return PY_TYPE_CHECK(p, MPolynomial_libsingular) or PY_TYPE_CHECK(p, NCPolynomial_plural) 271 272 def all_singular_poly_wrapper(s): 234 273 """ 235 274 Tests for a sequence ``s``, whether it consists of 236 275 singular polynomials. 237 276 238 277 EXAMPLE:: 239 278 240 sage: from sage.libs.singular.function import all_ polynomials279 sage: from sage.libs.singular.function import all_singular_poly_wrapper 241 280 sage: P.<x,y,z> = QQ[] 242 sage: all_ polynomials([x+1, y])281 sage: all_singular_poly_wrapper([x+1, y]) 243 282 True 244 sage: all_ polynomials([x+1, y, 1])283 sage: all_singular_poly_wrapper([x+1, y, 1]) 245 284 False 246 285 """ 247 286 for p in s: 248 if not is instance(p, MPolynomial_libsingular):287 if not is_singular_poly_wrapper(p): 249 288 return False 250 289 return True 251 290 291 cdef poly* access_singular_poly(p) except <poly*> -1: 292 """ 293 Get the raw ``poly`` pointer from a wrapper object 294 EXAMPLE:: 295 # sage: P.<a,b,c> = PolynomialRing(QQ) 296 # sage: p=a+b 297 # sage: from sage.libs.singular.function import access_singular_poly 298 # sage: access_singular_poly(p) 299 """ 300 if PY_TYPE_CHECK(p, MPolynomial_libsingular): 301 return (<MPolynomial_libsingular> p)._poly 302 else: 303 if PY_TYPE_CHECK(p, NCPolynomial_plural): 304 return (<NCPolynomial_plural> p)._poly 305 raise ValueError("not a singular polynomial wrapper") 306 307 cdef ring* access_singular_ring(r) except <ring*> -1: 308 """ 309 Get the singular ``ring`` pointer from a 310 wrapper object. 311 312 EXAMPLE:: 313 # sage: P.<x,y,z> = QQ[] 314 # sage: from sage.libs.singular.function import access_singular_ring 315 # sage: access_singular_ring(P) 316 317 """ 318 if PY_TYPE_CHECK(r, MPolynomialRing_libsingular): 319 return (<MPolynomialRing_libsingular> r )._ring 320 if PY_TYPE_CHECK(r, NCPolynomialRing_plural): 321 return (<NCPolynomialRing_plural> r )._ring 322 raise ValueError("not a singular polynomial ring wrapper") 323 324 cdef poly* copy_sage_polynomial_into_singular_poly(p): 325 return p_Copy(access_singular_poly(p), access_singular_ring(p.parent())) 326 252 327 def all_vectors(s): 253 328 """ 254 329 Checks if a sequence ``s`` consists of free module … … 269 344 False 270 345 """ 271 346 for p in s: 272 if not ( isinstance(p, FreeModuleElement_generic_dense)\273 and is instance(p.parent().base_ring(), MPolynomialRing_libsingular)):347 if not (PY_TYPE_CHECK(p, FreeModuleElement_generic_dense)\ 348 and is_sage_wrapper_for_singular_ring(p.parent().base_ring())): 274 349 return False 275 350 return True 276 351 277 352 278 353 354 355 356 357 279 358 cdef class Converter(SageObject): 280 359 """ 281 360 A :class:`Converter` interfaces between Sage objects and Singular … … 305 384 cdef leftv *v 306 385 self.args = NULL 307 386 self._sage_ring = ring 308 if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular): 309 self._singular_ring = (<MPolynomialRing_libsingular>ring)._ring 310 elif PY_TYPE_CHECK(ring, NCPolynomialRing_plural): 311 self._singular_ring = (<NCPolynomialRing_plural>ring)._ring 387 if ring is not None: 388 self._singular_ring = access_singular_ring(ring) 312 389 313 390 from sage.matrix.matrix_mpolynomial_dense import Matrix_mpolynomial_dense 314 391 from sage.matrix.matrix_integer_dense import Matrix_integer_dense 392 from sage.matrix.matrix_generic_dense import Matrix_generic_dense 315 393 for a in args: 316 if PY_TYPE_CHECK(a, MPolynomial_libsingular) or \ 317 PY_TYPE_CHECK(a, NCPolynomial_plural): 394 if is_singular_poly_wrapper(a): 318 395 v = self.append_polynomial(a) 319 396 320 elif PY_TYPE_CHECK(a, MPolynomialRing_libsingular) or \ 321 PY_TYPE_CHECK(a, NCPolynomialRing_plural): 397 elif is_sage_wrapper_for_singular_ring(a): 322 398 v = self.append_ring(a) 323 399 324 400 elif PY_TYPE_CHECK(a, MPolynomialIdeal) or \ … … 336 412 337 413 elif PY_TYPE_CHECK(a, Matrix_integer_dense): 338 414 v = self.append_intmat(a) 339 415 416 elif PY_TYPE_CHECK(a, Matrix_generic_dense) and\ 417 is_sage_wrapper_for_singular_ring(a.parent().base_ring()): 418 self.append_matrix(a) 419 340 420 elif PY_TYPE_CHECK(a, Resolution): 341 421 v = self.append_resolution(a) 342 422 343 423 elif PY_TYPE_CHECK(a, FreeModuleElement_generic_dense)\ 344 and isinstance( 345 a.parent().base_ring(), 346 MPolynomialRing_libsingular): 424 and is_sage_wrapper_for_singular_ring( 425 a.parent().base_ring()): 347 426 v = self.append_vector(a) 348 427 349 428 # as output ideals get converted to sequences … … 351 430 # this means, that Singular lists should not be converted to Sequences, 352 431 # as we do not want ambiguities 353 432 elif PY_TYPE_CHECK(a, Sequence)\ 354 and all_ polynomials(a):433 and all_singular_poly_wrapper(a): 355 434 v = self.append_ideal(ring.ideal(a)) 356 435 elif PY_TYPE_CHECK(a, Sequence)\ 357 436 and all_vectors(a): … … 480 559 ncols = mat.ncols 481 560 nrows = mat.nrows 482 561 result = Matrix(self._sage_ring, nrows, ncols) 483 #FIXME: NCPolynomial484 cdef MPolynomial_libsingular p485 562 for i in xrange(nrows): 486 563 for j in xrange(ncols): 487 p = MPolynomial_libsingular(self._sage_ring) 488 p._poly = mat.m[i*ncols+j] 564 p = new_sage_polynomial(self._sage_ring, mat.m[i*ncols+j]) 489 565 mat.m[i*ncols+j]=NULL 490 566 result[i,j] = p 491 567 return result … … 527 603 else: 528 604 previous = p_iter 529 605 p_iter = pNext(p_iter) 530 result.append(new_MP(self._sage_ring, first)) 606 607 result.append(new_sage_polynomial(self._sage_ring, first)) 531 608 return free_module(result) 532 609 533 610 cdef object to_sage_module_element_sequence_destructive( self, ideal *i): … … 544 621 #cdef MPolynomialRing_libsingular sage_ring = self._ring 545 622 cdef int j 546 623 cdef int rank=i.rank 547 free_module = self._sage_ring **rank624 free_module = self._sage_ring ** rank 548 625 l = [] 549 626 550 627 for j from 0 <= j < IDELEMS(i): … … 578 655 Append the polynomial ``p`` to the list. 579 656 """ 580 657 cdef poly* _p 581 if PY_TYPE_CHECK(p, MPolynomial_libsingular): 582 _p = p_Copy((<MPolynomial_libsingular>p)._poly, <ring*>((<MPolynomial_libsingular>p)._parent)._ring) 583 elif PY_TYPE_CHECK(p, NCPolynomial_plural): 584 _p = p_Copy((<NCPolynomial_plural>p)._poly, <ring*>((<NCPolynomial_plural>p)._parent)._ring) 658 _p = copy_sage_polynomial_into_singular_poly(p) 659 585 660 return self._append(_p, POLY_CMD) 586 661 587 662 cdef leftv *append_ideal(self, i) except NULL: … … 617 692 cdef number *_n = sa2si(n, self._singular_ring) 618 693 return self._append(<void *>_n, NUMBER_CMD) 619 694 620 cdef leftv *append_ring(self, MPolynomialRing_libsingularr) except NULL:695 cdef leftv *append_ring(self, r) except NULL: 621 696 """ 622 697 Append the ring ``r`` to the list. 623 698 """ 624 #FIXME 625 cdef ring *_r = <ring*> r._ring 699 cdef ring *_r = access_singular_ring(r) 626 700 _r.ref+=1 627 701 return self._append(<void *>_r, RING_CMD) 628 702 629 703 cdef leftv *append_matrix(self, mat) except NULL: 630 #FIXME631 704 632 705 sage_ring = mat.base_ring() 633 cdef ring *r=<ring*> (<MPolynomialRing_libsingular> sage_ring)._ring706 cdef ring *r=<ring*> access_singular_ring(sage_ring) 634 707 635 708 cdef poly *p 636 709 ncols = mat.ncols() … … 639 712 for i in xrange(nrows): 640 713 for j in xrange(ncols): 641 714 #FIXME 642 p = p_Copy( 643 (<MPolynomial_libsingular> mat[i,j])._poly, r) 715 p = copy_sage_polynomial_into_singular_poly(mat[i,j]) 644 716 _m.m[ncols*i+j]=p 645 717 return self._append(_m, MATRIX_CMD) 646 718 … … 1078 1150 1079 1151 return prefix + get_docstring(self._name) 1080 1152 1081 cdef MPolynomialRing_libsingularcommon_ring(self, tuple args, ring=None):1153 cdef common_ring(self, tuple args, ring=None): 1082 1154 """ 1083 1155 Return the common ring for the argument list ``args``. 1084 1156 … … 1099 1171 if PY_TYPE_CHECK(a, MPolynomialIdeal) or \ 1100 1172 PY_TYPE_CHECK(a, NCPolynomialIdeal): 1101 1173 ring2 = a.ring() 1102 elif PY_TYPE_CHECK(a, MPolynomial_libsingular) or \ 1103 PY_TYPE_CHECK(a, NCPolynomial_plural): 1174 elif is_singular_poly_wrapper(a): 1104 1175 ring2 = a.parent() 1105 elif PY_TYPE_CHECK(a, MPolynomialRing_libsingular) or \ 1106 PY_TYPE_CHECK(a, NCPolynomialRing_plural): 1176 elif is_sage_wrapper_for_singular_ring(a): 1107 1177 ring2 = a 1108 elif PY_TYPE_CHECK(a, int) or PY_TYPE_CHECK(a, long) or PY_TYPE_CHECK(a, basestring): 1178 elif PY_TYPE_CHECK(a, int) or\ 1179 PY_TYPE_CHECK(a, long) or\ 1180 PY_TYPE_CHECK(a, basestring): 1109 1181 continue 1110 1182 elif PY_TYPE_CHECK(a, Matrix_integer_dense): 1111 1183 continue … … 1118 1190 elif PY_TYPE_CHECK(a, Resolution): 1119 1191 ring2 = (<Resolution> a).base_ring 1120 1192 elif PY_TYPE_CHECK(a, FreeModuleElement_generic_dense)\ 1121 and PY_TYPE_CHECK( 1122 a.parent().base_ring(), 1123 MPolynomialRing_libsingular): 1193 and is_sage_wrapper_for_singular_ring( 1194 a.parent().base_ring()): 1124 1195 ring2 = a.parent().base_ring() 1125 1196 elif ring is not None: 1126 1197 a.parent() is ring … … 1132 1203 raise ValueError("Rings do not match up.") 1133 1204 if ring is None: 1134 1205 raise ValueError("Could not detect ring.") 1135 return <MPolynomialRing_libsingular>ring1206 return ring 1136 1207 1137 1208 def __reduce__(self): 1138 1209 """ … … 1168 1239 global errorreported 1169 1240 1170 1241 cdef ring *si_ring 1171 if isinstance(R, MPolynomialRing_libsingular):1242 if PY_TYPE_CHECK(R, MPolynomialRing_libsingular): 1172 1243 si_ring = (<MPolynomialRing_libsingular>R)._ring 1173 1244 else: 1174 1245 si_ring = (<NCPolynomialRing_plural>R)._ring … … 1399 1470 <Resolution> 1400 1471 sage: singular_list(resolution) 1401 1472 [[(-2*y, 2, y + 1, 0), (0, -2, x - 1, 0), (x*y - y, -y + 1, 1, -y), (x^2 + 1, -x - 1, -1, -x)], [(-x - 1, y - 1, 2*x, -2*y)], [(0)]] 1402 1473 sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural 1474 sage: from sage.matrix.constructor import Matrix 1475 sage: c=Matrix(2) 1476 sage: c[0,1]=-1 1477 sage: P = NCPolynomialRing_plural(QQ, 2, 'x,y', c=c, d=Matrix(2)) 1478 sage: (x,y)=P.gens() 1479 sage: I= Sequence([x*y,x+y], check=False, immutable=True)#P.ideal(x*y,x+y) 1480 sage: twostd = singular_function("twostd") 1481 sage: twostd(I) 1482 [x + y, y^2] 1483 sage: M=syz(I) 1484 doctest... 1485 sage: M 1486 [(x + y, x*y)] 1487 sage: syz(M, ring=P) 1488 [(0)] 1489 sage: mres(I, 0) 1490 <Resolution> 1491 sage: M=P**3 1492 sage: v=M((100*x,5*y,10*y*x*y)) 1493 sage: leadcoef(v) 1494 -10 1495 sage: v = M([x+y,x*y+y**3,x]) 1496 sage: lead(v) 1497 (0, y^3) 1498 sage: jet(v, 2) 1499 (x + y, x*y, x) 1500 sage: l = ringlist(P) 1501 sage: len(l) 1502 6 1503 sage: ring(l, ring=P) 1504 <RingWrap> 1505 sage: I=twostd(I) 1506 sage: l[3]=I 1507 sage: ring(l, ring=P) 1508 <RingWrap> 1403 1509 1404 1510 """ 1405 1511 -
sage/modules/free_module.py
diff -r 54342f65c59c sage/modules/free_module.py
a b 182 182 from sage.structure.parent_gens import ParentWithGens 183 183 from sage.misc.cachefunc import cached_method 184 184 185 from warnings import warn 186 185 187 ############################################################################### 186 188 # 187 189 # Constructor functions … … 345 347 if not isinstance(sparse,bool): 346 348 raise TypeError, "Argument sparse (= %s) must be True or False" % sparse 347 349 350 351 # We should have two sided, left sided and right sided ideals, 352 # but that's another story .... 353 # 348 354 if not base_ring.is_commutative(): 349 raise TypeError, "The base_ring must be a commutative ring." 355 356 warn("""You are constructing a free module 357 over a noncommutative ring. Sage does 358 not have a concept of left/right 359 and both sided modules be careful. It's also 360 not guarantied that all multiplications 361 are done from the right side.""") 362 363 # raise TypeError, "The base_ring must be a commutative ring." 364 350 365 351 366 if not sparse and isinstance(base_ring,sage.rings.real_double.RealDoubleField_class): 352 367 return RealDoubleVectorSpace_class(rank) … … 558 573 Category of modules with basis over Integer Ring 559 574 560 575 """ 561 if not isinstance(base_ring, commutative_ring.CommutativeRing): 562 raise TypeError, "base_ring (=%s) must be a commutative ring"%base_ring 576 if not base_ring.is_commutative(): 577 warn("""You are constructing a free module over a noncommutative ring. Sage does not have a concept of left/right and both sided modules be careful. It's also not guarantied that all multiplications are done from the right side.""") 578 #raise TypeError, "base_ring (=%s) must be a commutative ring"%base_ring 579 563 580 rank = sage.rings.integer.Integer(rank) 564 581 if rank < 0: 565 582 raise ValueError, "rank (=%s) must be nonnegative"%rank -
sage/rings/polynomial/plural.pyx
diff -r 54342f65c59c sage/rings/polynomial/plural.pyx
a b 13 13 from sage.libs.singular.polynomial cimport singular_polynomial_deg, singular_polynomial_str_with_changed_varnames, singular_polynomial_latex, singular_polynomial_str, singular_polynomial_div_coeff 14 14 15 15 from sage.libs.singular.singular cimport si2sa, sa2si, overflow_check 16 16 from sage.rings.integer_ring import ZZ 17 17 from term_order import TermOrder 18 18 19 19 cdef class NCPolynomialRing_plural(Ring): … … 45 45 46 46 self.__ngens = n 47 47 ParentWithGens.__init__(self, base_ring, names) 48 self._populate_coercion_lists_() 49 48 50 #MPolynomialRing_generic.__init__(self, base_ring, n, names, order) 49 51 #self._has_singular = True 50 52 assert(n == len(self._names)) 51 53 52 54 self._one_element = new_NCP(self,p_ISet(1, self._ring)) 53 55 self._zero_element = new_NCP(self,NULL) 56 57 58 def _element_constructor_(self, x): 59 """ 60 Make sure x is a valid member of self, and return the constructed element. 61 """ 62 if x==0: 63 return self._zero_element 64 raise NotImplementedError("not able to constructor "+repr(x)) 65 66 def _coerce_map_from_(self, S): 67 """ 68 The only things that coerce into this ring are: 69 - the integer ring 70 - other localizations away from fewer primes 71 """ 72 if S is ZZ: 73 return True 74 75 def __pow__(self, n, _): 76 """ 77 Return the free module of rank `n` over this ring. 54 78 79 EXAMPLES:: 80 81 """ 82 import sage.modules.all 83 return sage.modules.all.FreeModule(self, n) 84 85 86 55 87 def term_order(self): 56 88 return self.__term_order 57 89 90 91 def is_commutative(self): 92 return False 93 94 def is_field(self): 95 return False 96 58 97 def _repr_(self): 59 98 """ 60 99 EXAMPLE:
