Ticket #11634: trac_11634_pickling_for_PPL.patch
File trac_11634_pickling_for_PPL.patch, 14.3 KB (added by , 10 years ago) 


sage/geometry/polyhedron/base.py
# HG changeset patch # User Volker Braun <vbraun@stp.dias.ie> # Date 1314754708 14400 # Node ID e1901734e40c50a2c74659614500cbb4a089f5fa # Parent 79395f6835a58d5f1e46bb194742ef28dc1a2966 Trac #11634: Pickling for PPL Polyhedra diff git a/sage/geometry/polyhedron/base.py b/sage/geometry/polyhedron/base.py
a b 98 98 TESTS:: 99 99 100 100 sage: p = Polyhedron() 101 sage: TestSuite(p).run( skip='_test_pickling')101 sage: TestSuite(p).run() 102 102 """ 103 103 104 104 def __init__(self, ambient_dim, Vrep, Hrep, **kwds): 
sage/libs/ppl.pyx
diff git a/sage/libs/ppl.pyx b/sage/libs/ppl.pyx
a b 2432 2432 return result 2433 2433 2434 2434 2435 2435 2436 #################################################### 2436 2437 ### C_Polyhedron ################################### 2437 2438 #################################################### … … 2534 2535 sage: C_Polyhedron(3, 'empty') # indirect doctest 2535 2536 The empty polyhedron in QQ^3 2536 2537 """ 2537 if PY_TYPE_CHECK(arg, C_Polyhedron):2538 if isinstance(arg, C_Polyhedron): 2538 2539 ph = <C_Polyhedron>arg 2539 2540 self.thisptr = new PPL_C_Polyhedron(<PPL_C_Polyhedron&>ph.thisptr[0]) 2540 2541 return 2541 if PY_TYPE_CHECK(arg, Generator):2542 if isinstance(arg, Generator): 2542 2543 arg = Generator_System(arg) 2543 if PY_TYPE_CHECK(arg, Constraint):2544 if isinstance(arg, Constraint): 2544 2545 arg = Constraint_System(arg) 2545 if PY_TYPE_CHECK(arg, Generator_System):2546 if isinstance(arg, Generator_System): 2546 2547 gs = <Generator_System>arg 2547 2548 self.thisptr = new PPL_C_Polyhedron(gs.thisptr[0]) 2548 2549 return 2549 if PY_TYPE_CHECK(arg, Constraint_System):2550 if isinstance(arg, Constraint_System): 2550 2551 cs = <Constraint_System>arg 2551 2552 self.thisptr = new PPL_C_Polyhedron(cs.thisptr[0]) 2552 2553 return … … 2588 2589 """ 2589 2590 del self.thisptr 2590 2591 2591 2592 2593 def __reduce__(self): 2594 """ 2595 Pickle object 2596 2597 TESTS:: 2598 2599 sage: from sage.libs.ppl import C_Polyhedron, Variable 2600 sage: P = C_Polyhedron(3, 'empty') 2601 sage: loads(dumps(P)) 2602 The empty polyhedron in QQ^3 2603 2604 sage: Q = C_Polyhedron(5, 'universe') 2605 sage: loads(dumps(Q)) 2606 The spacefilling polyhedron in QQ^5 2607 2608 sage: x = Variable(0) 2609 sage: y = Variable(1) 2610 sage: H = C_Polyhedron( 5*x2*y >= x+y1 ) 2611 sage: loads(dumps(H)) 2612 A 2dimensional polyhedron in QQ^2 defined as the convex hull of 1 point, 1 ray, 1 line 2613 """ 2614 if self.is_empty(): 2615 return (C_Polyhedron, (self.space_dimension(), 'empty')) 2616 elif self.is_universe(): 2617 return (C_Polyhedron, (self.space_dimension(), 'universe')) 2618 else: 2619 return (C_Polyhedron, (self.generators(),)) 2592 2620 2593 2621 2594 2622 #################################################### … … 2683 2711 sage: NNC_Polyhedron(3, 'empty') # indirect doctest 2684 2712 The empty polyhedron in QQ^3 2685 2713 """ 2686 if PY_TYPE_CHECK(arg, NNC_Polyhedron):2714 if isinstance(arg, NNC_Polyhedron): 2687 2715 p_nnc = <NNC_Polyhedron>arg 2688 2716 self.thisptr = new PPL_NNC_Polyhedron(<PPL_NNC_Polyhedron&>p_nnc.thisptr[0]) 2689 2717 return 2690 if PY_TYPE_CHECK(arg, C_Polyhedron):2718 if isinstance(arg, C_Polyhedron): 2691 2719 p_c = <C_Polyhedron>arg 2692 2720 self.thisptr = new PPL_NNC_Polyhedron(<PPL_C_Polyhedron&>p_c.thisptr[0]) 2693 2721 return 2694 if PY_TYPE_CHECK(arg, Generator):2722 if isinstance(arg, Generator): 2695 2723 arg = Generator_System(arg) 2696 if PY_TYPE_CHECK(arg, Constraint):2724 if isinstance(arg, Constraint): 2697 2725 arg = Constraint_System(arg) 2698 if PY_TYPE_CHECK(arg, Generator_System):2726 if isinstance(arg, Generator_System): 2699 2727 gs = <Generator_System>arg 2700 2728 self.thisptr = new PPL_NNC_Polyhedron(gs.thisptr[0]) 2701 2729 return 2702 if PY_TYPE_CHECK(arg, Constraint_System):2730 if isinstance(arg, Constraint_System): 2703 2731 cs = <Constraint_System>arg 2704 2732 self.thisptr = new PPL_NNC_Polyhedron(cs.thisptr[0]) 2705 2733 return … … 2741 2769 """ 2742 2770 del self.thisptr 2743 2771 2772 2773 def __reduce__(self): 2774 """ 2775 Pickle object 2776 2777 TESTS:: 2778 2779 sage: from sage.libs.ppl import NNC_Polyhedron, Variable 2780 sage: P = NNC_Polyhedron(3, 'empty') 2781 sage: loads(dumps(P)) 2782 The empty polyhedron in QQ^3 2783 2784 sage: Q = NNC_Polyhedron(5, 'universe') 2785 sage: loads(dumps(Q)) 2786 The spacefilling polyhedron in QQ^5 2787 2788 sage: x = Variable(0) 2789 sage: y = Variable(1) 2790 sage: H = NNC_Polyhedron( 5*x2*y > x+y1 ) 2791 sage: loads(dumps(H)) 2792 A 2dimensional polyhedron in QQ^2 defined as the convex hull of 1 point, 2793 1 closure_point, 1 ray, 1 line 2794 """ 2795 if self.is_empty(): 2796 return (NNC_Polyhedron, (self.space_dimension(), 'empty')) 2797 elif self.is_universe(): 2798 return (NNC_Polyhedron, (self.space_dimension(), 'universe')) 2799 else: 2800 return (NNC_Polyhedron, (self.generators(),)) 2744 2801 2745 2802 2746 2803 #################################################### … … 2965 3022 sage: 15 * y 2966 3023 15*x1 2967 3024 """ 2968 if PY_TYPE_CHECK(self, Variable):3025 if isinstance(self, Variable): 2969 3026 return Linear_Expression(self) * other 2970 3027 else: 2971 3028 return Linear_Expression(other) * self … … 3133 3190 else: 3134 3191 assert False, 'Cannot initialize with more than 2 arguments.' 3135 3192 3136 if PY_TYPE_CHECK(arg, Variable):3193 if isinstance(arg, Variable): 3137 3194 v = <Variable>arg 3138 3195 self.thisptr = new PPL_Linear_Expression(v.thisptr[0]) 3139 3196 return 3140 if PY_TYPE_CHECK(arg, Linear_Expression):3197 if isinstance(arg, Linear_Expression): 3141 3198 e = <Linear_Expression>arg 3142 3199 self.thisptr = new PPL_Linear_Expression(e.thisptr[0]) 3143 3200 return … … 3472 3529 """ 3473 3530 cdef Linear_Expression e 3474 3531 cdef Integer c 3475 if PY_TYPE_CHECK(self, Linear_Expression):3532 if isinstance(self, Linear_Expression): 3476 3533 e = <Linear_Expression>self 3477 3534 c = Integer(other) 3478 3535 else: … … 3539 3596 return _make_Constraint_from_richcmp(self, other, op) 3540 3597 3541 3598 3599 def __reduce__(self): 3600 """ 3601 Pickle object 3602 3603 EXAMPLES:: 3604 3605 sage: from sage.libs.ppl import Linear_Expression 3606 sage: le = loads(dumps(Linear_Expression([1,2,3],4))) 3607 sage: le.coefficients() == (1,2,3) 3608 True 3609 sage: le.inhomogeneous_term() == 4 3610 True 3611 """ 3612 return (Linear_Expression, (self.coefficients(), self.inhomogeneous_term())) 3613 3614 3542 3615 #################################################### 3543 3616 ### Generator ###################################### 3544 3617 #################################################### … … 4202 4275 return self.thisptr.OK() 4203 4276 4204 4277 4278 def __reduce__(self): 4279 """ 4280 Pickle object. 4281 4282 TESTS:: 4283 4284 sage: from sage.libs.ppl import Generator, Variable, line, ray, point, closure_point 4285 sage: x = Variable(0); y = Variable(1); 4286 sage: loads(dumps(Generator.point(2*x+7*y, 3))) 4287 point(2/3, 7/3) 4288 sage: loads(dumps(Generator.closure_point(2*x+7*y, 3))) 4289 closure_point(2/3, 7/3) 4290 sage: loads(dumps(Generator.line(2*x+7*y))) 4291 line(2, 7) 4292 sage: loads(dumps(Generator.ray(2*x+7*y))) 4293 ray(2, 7) 4294 """ 4295 t = self.thisptr.type() 4296 le = Linear_Expression(self.coefficients(), 0) 4297 if t==LINE: 4298 return (line, (le,)) 4299 elif t==RAY: 4300 return (ray, (le,)) 4301 elif t==POINT: 4302 return (point, (le, self.divisor())) 4303 elif t==CLOSURE_POINT: 4304 return (closure_point, (le, self.divisor())) 4305 assert False 4306 4307 4205 4308 4206 4309 #################################################### 4207 4310 def line(expression): … … 4332 4435 if arg is None: 4333 4436 self.thisptr = new PPL_Generator_System() 4334 4437 return 4335 if PY_TYPE_CHECK(arg, Generator):4438 if isinstance(arg, Generator): 4336 4439 g = <Generator>arg 4337 4440 self.thisptr = new PPL_Generator_System(g.thisptr[0]) 4338 4441 return 4339 if PY_TYPE_CHECK(arg, Generator_System):4442 if isinstance(arg, Generator_System): 4340 4443 gs = <Generator_System>arg 4341 4444 self.thisptr = new PPL_Generator_System(gs.thisptr[0]) 4342 4445 return 4446 if isinstance(arg, (list,tuple)): 4447 self.thisptr = new PPL_Generator_System() 4448 for generator in arg: 4449 self.insert(generator) 4450 return 4343 4451 raise ValueError, 'Cannot initialize with '+str(arg)+'.' 4344 4452 4345 4453 … … 4511 4619 return s 4512 4620 4513 4621 4622 def __reduce__(self): 4623 """ 4624 Pickle object. 4625 4626 TESTS:: 4627 4628 sage: from sage.libs.ppl import Generator_System, Variable, point, ray 4629 sage: x = Variable(0) 4630 sage: y = Variable(1) 4631 sage: gs = Generator_System((point(3*x+2*y+1), ray(x))); gs 4632 Generator_System {point(3/1, 2/1), ray(1, 0)} 4633 sage: loads(dumps(gs)) 4634 Generator_System {point(3/1, 2/1), ray(1, 0)} 4635 """ 4636 return (Generator_System, (tuple(self), )) 4637 4638 4639 4514 4640 #################################################### 4515 4641 ### Generator_System_iterator ###################### 4516 4642 #################################################### … … 5079 5205 """ 5080 5206 return self.thisptr.OK() 5081 5207 5208 5209 def __reduce__(self): 5210 """ 5211 Pickle object. 5212 5213 EXAMPLES:: 5214 5215 sage: from sage.libs.ppl import Linear_Expression, Variable 5216 sage: x = Variable(0) 5217 sage: y = Variable(1) 5218 sage: loads(dumps(3*x+2*y+1>=5)) 5219 3*x0+2*x14>=0 5220 sage: loads(dumps(3*x+2*y+1>5)) 5221 3*x0+2*x14>0 5222 sage: loads(dumps(3*x+2*y+1==5)) 5223 3*x0+2*x14==0 5224 """ 5225 le = Linear_Expression(self.coefficients(), self.inhomogeneous_term()) 5226 if self.is_nonstrict_inequality(): 5227 return (inequality, (le, )) 5228 elif self.is_strict_inequality(): 5229 return (strict_inequality, (le, )) 5230 elif self.is_equality(): 5231 return (equation, (le, )) 5232 else: 5233 assert False 5234 5235 5236 5237 #################################################### 5238 def inequality(expression): 5239 """ 5240 Constuct an inequality. 5241 5242 INPUT: 5243 5244  ``expression``  a :class:`Linear_Expression`. 5245 5246 OUTPUT: 5247 5248 The inequality ``expression`` >= 0. 5249 5250 EXAMPLES:: 5251 5252 sage: from sage.libs.ppl import Variable, inequality 5253 sage: y = Variable(1) 5254 sage: 2*y+1 >= 0 5255 2*x1+1>=0 5256 sage: inequality(2*y+1) 5257 2*x1+1>=0 5258 """ 5259 return expression >= 0 5260 5261 5262 #################################################### 5263 def strict_inequality(expression): 5264 """ 5265 Constuct a strict inequality. 5266 5267 INPUT: 5268 5269  ``expression``  a :class:`Linear_Expression`. 5270 5271 OUTPUT: 5272 5273 The inequality ``expression`` > 0. 5274 5275 EXAMPLES:: 5276 5277 sage: from sage.libs.ppl import Variable, strict_inequality 5278 sage: y = Variable(1) 5279 sage: 2*y+1 > 0 5280 2*x1+1>0 5281 sage: strict_inequality(2*y+1) 5282 2*x1+1>0 5283 """ 5284 return expression > 0 5285 5286 5287 #################################################### 5288 def equation(expression): 5289 """ 5290 Constuct an equation. 5291 5292 INPUT: 5293 5294  ``expression``  a :class:`Linear_Expression`. 5295 5296 OUTPUT: 5297 5298 The equation ``expression`` == 0. 5299 5300 EXAMPLES:: 5301 5302 sage: from sage.libs.ppl import Variable, equation 5303 sage: y = Variable(1) 5304 sage: 2*y+1 == 0 5305 2*x1+1==0 5306 sage: equation(2*y+1) 5307 2*x1+1==0 5308 """ 5309 return expression == 0 5310 5311 5082 5312 5083 5313 #################################################### 5084 5314 ### Constraint_System ############################## … … 5134 5364 if arg is None: 5135 5365 self.thisptr = new PPL_Constraint_System() 5136 5366 return 5137 if PY_TYPE_CHECK(arg, Constraint):5367 if isinstance(arg, Constraint): 5138 5368 g = <Constraint>arg 5139 5369 self.thisptr = new PPL_Constraint_System(g.thisptr[0]) 5140 5370 return 5141 if PY_TYPE_CHECK(arg, Constraint_System):5371 if isinstance(arg, Constraint_System): 5142 5372 gs = <Constraint_System>arg 5143 5373 self.thisptr = new PPL_Constraint_System(gs.thisptr[0]) 5144 5374 return 5375 if isinstance(arg, (list,tuple)): 5376 self.thisptr = new PPL_Constraint_System() 5377 for constraint in arg: 5378 self.insert(constraint) 5379 return 5145 5380 raise ValueError, 'Cannot initialize with '+str(arg)+'.' 5146 5381 5147 5382 … … 5351 5586 sage: from sage.libs.ppl import Constraint_System, Variable 5352 5587 sage: x = Variable(0) 5353 5588 sage: y = Variable(1) 5354 sage: cs = Constraint_System( 3*x+2*y+1 < 3) 5355 sage: cs.insert( 0*x>x+1 ) 5589 sage: cs = Constraint_System([3*x+2*y+1 < 3, 0*x>x+1]) 5356 5590 sage: cs.__repr__() 5357 5591 'Constraint_System {3*x02*x1+2>0, x01>0}' 5358 5592 """ … … 5362 5596 return s 5363 5597 5364 5598 5599 def __reduce__(self): 5600 """ 5601 Pickle object. 5602 5603 TESTS:: 5604 5605 sage: from sage.libs.ppl import Constraint_System, Variable 5606 sage: x = Variable(0) 5607 sage: y = Variable(1) 5608 sage: cs = Constraint_System([3*x+2*y+1 < 3, 0*x>x+1]); cs 5609 Constraint_System {3*x02*x1+2>0, x01>0} 5610 sage: loads(dumps(cs)) 5611 Constraint_System {3*x02*x1+2>0, x01>0} 5612 """ 5613 return (Constraint_System, (tuple(self), )) 5614 5615 5365 5616 #################################################### 5366 5617 ### Constraint_System_iterator ##################### 5367 5618 ####################################################