Ticket #3952: trac_3952.patch
| File trac_3952.patch, 13.6 kB (added by mhansen, 4 months ago) |
|---|
-
a/sage/calculus/calculus.py
old new 3888 3888 3889 3889 def _fast_float_(self, *vars): 3890 3890 """ 3891 Returns an object which provides fast floating point evaluation of 3892 self. 3893 3894 See sage.ext.fast_eval? for more information. 3895 3891 3896 EXAMPLES: 3892 3897 sage: x,y,z = var('x,y,z') 3893 3898 sage: f = 1 + sin(x)/x + sqrt(z^2+y^2)/cosh(x) … … 4060 4065 return True 4061 4066 4062 4067 def _fast_float_(self, *vars): 4068 """ 4069 Returns an object which provides fast floating point evaluation of 4070 self. 4071 4072 See sage.ext.fast_eval? for more information. 4073 4074 EXAMPLES: 4075 sage: f = SR(2)._fast_float_() 4076 sage: f(3) 4077 2.0 4078 """ 4063 4079 return fast_float.fast_float_constant(float(self)) 4064 4080 4065 4081 def _recursive_sub(self, kwds): … … 4673 4689 4674 4690 def _fast_float_(self, *vars): 4675 4691 """ 4692 Returns an object which provides fast floating point evaluation of 4693 self. 4694 4695 See sage.ext.fast_eval? for more information. 4696 4676 4697 EXAMPLES: 4677 4698 sage: x,y = var('x,y') 4678 4699 sage: f = x*x-y 4679 4700 sage: ff = f._fast_float_('x','y') 4680 4701 sage: ff(2,3) 4681 4702 1.0 4682 """ 4703 4704 sage: a = x + 2*y 4705 sage: f = a._fast_float_() 4706 sage: f(1,0) 4707 1.0 4708 sage: f(0,1) 4709 2.0 4710 """ 4711 if vars == (): 4712 vars = self.arguments() 4683 4713 fops = [op._fast_float_(*vars) for op in self._operands] 4684 4714 return self._operator(*fops) 4685 4715 … … 5191 5221 2.0 5192 5222 sage: sqrt(2)._fast_float_()(2) 5193 5223 1.4142135623730951 5194 """ 5224 5225 sage: f = x._fast_float_() 5226 sage: f(1.2) 5227 1.2 5228 """ 5229 #if no var 5230 if vars == (): 5231 return fast_float.fast_float_arg(0) 5232 5195 5233 if self._name in vars: 5196 5234 return fast_float.fast_float_arg(list(vars).index(self._name)) 5197 5235 svars = [repr(x) for x in vars] 5198 5236 if self._name in svars: 5199 5237 return fast_float.fast_float_arg(list(svars).index(self._name)) 5238 5200 5239 try: 5201 5240 return fast_float.fast_float_constant(float(self)) 5202 5241 except TypeError: … … 5591 5630 return self._expr._maxima_init_() 5592 5631 5593 5632 def _fast_float_(self, *vars): 5633 """ 5634 EXAMPLES: 5635 sage: a = var('a') 5636 sage: g(x) = sin(x) + 2 5637 sage: f = g._fast_float_() 5638 sage: f(0) 5639 2.0 5640 """ 5641 if vars == (): 5642 vars = self.arguments() 5594 5643 return self._expr._fast_float_(*vars) 5595 5644 5596 5645 def __float__(self): … … 6038 6087 return float(f._approx_(float(g))) 6039 6088 6040 6089 def _fast_float_(self, *vars): 6090 """ 6091 Returns an object which provides fast floating point evaluation of 6092 self. 6093 6094 See sage.ext.fast_eval? for more information. 6095 6096 EXAMPLES: 6097 sage: f = sqrt(x)._fast_float_('x') 6098 sage: f(2) 6099 1.41421356237309... 6100 sage: y = var('y') 6101 sage: f = sqrt(x+y)._fast_float_('x', 'y') 6102 sage: f(1,1) 6103 1.41421356237309... 6104 6105 sage: f = sqrt(x+2*y)._fast_float_() 6106 sage: f(2,0) 6107 1.41421356237309... 6108 sage: f(0,1) 6109 1.41421356237309... 6110 6111 """ 6112 if vars == (): 6113 vars = self.arguments() 6114 6041 6115 f = self._operands[0] 6042 6116 g = self._operands[1]._fast_float_(*vars) 6043 6117 try: … … 6568 6642 return math.sin(x) 6569 6643 return SymbolicComposition(self, SR(x)) 6570 6644 6571 def _fast_float_(self): 6645 def _fast_float_(self, *vars): 6646 """ 6647 Returns an object which provides fast floating point evaluation of 6648 self. 6649 6650 See sage.ext.fast_eval? for more information. 6651 6652 EXAMPLES: 6653 sage: from sage.ext.fast_eval import fast_float 6654 sage: fast_float(sin) 6655 <built-in function sin> 6656 sage: sin._fast_float_() 6657 <built-in function sin> 6658 6659 """ 6572 6660 return math.sin 6573 6661 6574 6662 sin = Function_sin() … … 6594 6682 return math.cos(x) 6595 6683 return SymbolicComposition(self, SR(x)) 6596 6684 6597 def _fast_float_(self): 6685 def _fast_float_(self, *vars): 6686 """ 6687 Returns an object which provides fast floating point evaluation of 6688 self. 6689 6690 See sage.ext.fast_eval? for more information. 6691 6692 EXAMPLES: 6693 sage: from sage.ext.fast_eval import fast_float 6694 sage: fast_float(cos) 6695 <built-in function cos> 6696 sage: cos._fast_float_() 6697 <built-in function cos> 6698 6699 """ 6598 6700 return math.cos 6599 6701 6600 6702 -
a/sage/plot/animate.py
old new 61 61 62 62 TESTS: 63 63 This illustrates ticket \#2066 is fixed (setting axes ranges when an endpoint is 0): 64 sage: animate( plot(sin, -1,1), xmin=0, ymin=0)._Animation__xmin64 sage: animate([plot(sin, -1,1)], xmin=0, ymin=0)._Animation__xmin 65 65 0 66 66 """ 67 67 def __init__(self, v, -
a/sage/plot/plot.py
old new 116 116 sage: g.show(dpi=200, axes=False) 117 117 118 118 Another graph: 119 sage: P = plot(lambda x: sin(x)/x, -4,4, rgbcolor=(0,0,1)) + \ 120 ... plot(lambda x: x*cos(x), -4,4, rgbcolor=(1,0,0)) + \ 121 ... plot(lambda x: tan(x),-4,4,rgbcolor=(0,1,0)) 119 sage: x = var('x') 120 sage: P = plot(sin(x)/x, -4,4, rgbcolor=(0,0,1)) + \ 121 ... plot(x*cos(x), -4,4, rgbcolor=(1,0,0)) + \ 122 ... plot(tan(x),-4,4,rgbcolor=(0,1,0)) 122 123 ... 123 124 sage: P.show(ymin=-pi,ymax=pi) 124 125 … … 148 149 sage: g1 + g2 # show their sum 149 150 150 151 An illustration of integration: 151 sage: f = lambda x:(x-3)*(x-5)*(x-7)+40152 sage: f = (x-3)*(x-5)*(x-7)+40 152 153 sage: P = line([(2,0),(2,f(2))], rgbcolor=(0,0,0)) 153 154 sage: P += line([(8,0),(8,f(8))], rgbcolor=(0,0,0)) 154 155 sage: P += polygon([(2,0),(2,f(2))] + [(x, f(x)) for x in [2,2.1,..,8]] + [(8,0),(2,0)], rgbcolor=(0.8,0.8,0.8)) … … 1299 1300 ... gridlinesstyle=dict(color="blue", linestyle=":")) 1300 1301 1301 1302 Change the style of the horizontal or vertical grid lines separately. 1302 sage: p = polar_plot( lambda x:2 + 2*cos(x), 0, 2*pi, rgbcolor=hue(0.3))1303 sage: p = polar_plot(2 + 2*cos(x), 0, 2*pi, rgbcolor=hue(0.3)) 1303 1304 sage: p.show(gridlines=True, \ 1304 1305 ... hgridlinesstyle=dict(color="orange", linewidth=1.0), \ 1305 1306 ... vgridlinesstyle=dict(color="blue", linestyle=":")) … … 1322 1323 ... gridlinesstyle=dict(marker='x',rgbcolor="black")) 1323 1324 1324 1325 Grid lines can be added to contour plots. 1325 sage: f = lambda x,y:sin(x^2 + y^2)*cos(x)*sin(y)1326 sage: f = sin(x^2 + y^2)*cos(x)*sin(y) 1326 1327 sage: c = contour_plot(f, (-4, 4), (-4, 4), plot_points=100) 1327 1328 sage: c.show(gridlines=True, gridlinesstyle={'linestyle':':','linewidth':1, 'rgbcolor':'red'}) 1328 1329 … … 2848 2849 EXAMPLES: 2849 2850 2850 2851 Here we plot a simple function of two variables: 2851 sage: f = lambda x,y: cos(x^2 + y^2)2852 sage: contour_plot( f, (-4, 4), (-4, 4))2852 sage: x,y = var('x,y') 2853 sage: contour_plot(cos(x^2+y^2), (-4, 4), (-4, 4)) 2853 2854 2854 2855 2855 2856 Here we change the ranges and add some options: 2856 sage: h = lambda x,y: (x^2)*cos(x*y) 2857 sage: contour_plot(h, (-10, 5), (-5, 5), fill=False, plot_points=100) 2857 sage: contour_plot((x^2)*cos(x*y), (-10, 5), (-5, 5), fill=False, plot_points=100) 2858 2858 2859 2859 2860 2860 An even more complicated plot. 2861 sage: f = lambda x,y: sin(x^2 + y^2)*cos(x)*sin(y) 2862 sage: contour_plot(f, (-4, 4), (-4, 4),plot_points=100) 2861 sage: contour_plot(sin(x^2 + y^2)*cos(x)*sin(y), (-4, 4), (-4, 4),plot_points=100) 2863 2862 2864 2863 Some elliptic curves, but with symbolic endpoints. In the first 2865 2864 example, the plot is rotated 90 degrees because we switch the 2866 2865 variables x,y. 2867 sage: x, y = var('x,y')2868 2866 sage: contour_plot(y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi)) 2869 sage: contour_plot(lambda x,y: y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi))2870 2867 sage: contour_plot(y^2 + 1 - x^3 - x, (-pi,pi), (-pi,pi)) 2871 2868 2872 2869 2873 2870 We can play with the contour levels. 2874 sage: f = lambda x,y:x^2 + y^22871 sage: f = x^2 + y^2 2875 2872 sage: contour_plot(f, (-2, 2), (-2, 2)) 2876 2873 sage: contour_plot(f, (-2, 2), (-2, 2), contours=2) 2877 2874 sage: contour_plot(f, (-2, 2), (-2, 2), contours=(0.1, 1.0, 1.2, 1.4), cmap='hsv') … … 3157 3154 3158 3155 3159 3156 TESTS: 3160 sage: p = plot_vector_field(( lambda x,y:.01*x,x+y), (10,20), (10,20))3157 sage: p = plot_vector_field((.01*x,x+y), (10,20), (10,20)) 3161 3158 sage: p.xmin() 3162 3159 10.0 3163 3160 sage: p.ymin() … … 3435 3432 We plot with randomize=False, which makes the initial sample 3436 3433 points evenly spaced (hence always the same). Adaptive plotting 3437 3434 might insert other points, however, unless plot_division=0. 3438 sage: p=plot( lambda x:1, (x,0,3), plot_points=4, randomize=False, plot_division=0)3435 sage: p=plot(1, (x,0,3), plot_points=4, randomize=False, plot_division=0) 3439 3436 sage: list(p[0]) 3440 3437 [(0.0, 1.0), (1.0, 1.0), (2.0, 1.0), (3.0, 1.0)] 3441 3438 … … 3545 3542 3546 3543 options.update(kwds) 3547 3544 3545 if not is_fast_float(funcs): 3546 funcs = fast_float(funcs) 3547 3548 3548 #parametric_plot will be a list or tuple of two functions (f,g) 3549 3549 #and will plotted as (f(x), g(x)) for all x in the given range 3550 3550 if parametric: 3551 if len(funcs) == 3: 3552 raise ValueError, "use parametric_plot3d for parametric plots in 3d dimensions." 3553 elif len(funcs) == 2: 3554 # 2d 3555 f,g = funcs 3556 else: 3557 raise ValueError, "parametric plots only implemented in 2 and 3 dimensions." 3558 3551 f, g = funcs 3559 3552 #or we have only a single function to be plotted: 3560 3553 else: 3561 3554 f = funcs … … 3702 3695 tmax -- end value of t 3703 3696 other options -- passed to plot. 3704 3697 3705 EXAMPLE :3706 We draw a 2d parametric plot:3698 EXAMPLES: 3699 We draw some 2d parametric plots: 3707 3700 sage: t = var('t') 3708 3701 sage: parametric_plot( (sin(t), sin(2*t)), 0, 2*pi, rgbcolor=hue(0.6) ) 3702 sage: parametric_plot((1, t), 0, 4) 3703 sage: parametric_plot((t, t^2), -4, 4) 3709 3704 3710 3705 We draw a 3d parametric plot: 3711 3706 sage: parametric_plot3d( (5*cos(x), 5*sin(x), x), (-12, 12), plot_points=150, color="red") 3712 """ 3707 3708 TESTS: 3709 sage: parametric_plot((x, t^2), -4, 4) 3710 Traceback (most recent call last): 3711 ... 3712 ValueError: there cannot be more than one free variable in funcs 3713 3714 sage: parametric_plot((1, x+t), -4, 4) 3715 Traceback (most recent call last): 3716 ... 3717 ValueError: there cannot be more than one free variable in funcs 3718 3719 """ 3720 if len(funcs) == 3: 3721 raise ValueError, "use parametric_plot3d for parametric plots in 3d dimensions." 3722 elif len(funcs) != 2: 3723 raise ValueError, "parametric plots only implemented in 2 and 3 dimensions." 3724 else: 3725 vars = [] 3726 f,g = funcs 3727 if hasattr(f, 'variables'): 3728 vars += list(f.variables()) 3729 if hasattr(g, 'variables'): 3730 vars += list(g.variables()) 3731 vars = [str(v) for v in vars] 3732 3733 from sage.misc.misc import uniq 3734 if len(uniq(vars)) > 1: 3735 raise ValueError, "there cannot be more than one free variable in funcs" 3736 3713 3737 return plot(funcs, tmin, tmax, parametric=True, **kwargs) 3714 3738 3715 3739 def polar_plot(funcs, xmin, xmax, **kwargs): … … 3719 3743 3720 3744 EXAMPLES: 3721 3745 Here is a blue 8-leaved petal: 3722 sage: polar_plot( lambda x:sin(5*x)^2, 0, 2*pi, rgbcolor=hue(0.6))3746 sage: polar_plot(sin(5*x)^2, 0, 2*pi, rgbcolor=hue(0.6)) 3723 3747 3724 3748 A red figure-8: 3725 sage: polar_plot( lambda x:abs(sqrt(1 - sin(x)^2)), 0, 2*pi, rgbcolor=hue(1.0))3749 sage: polar_plot(abs(sqrt(1 - sin(x)^2)), 0, 2*pi, rgbcolor=hue(1.0)) 3726 3750 3727 3751 A green limacon of Pascal: 3728 sage: polar_plot( lambda x:2 + 2*cos(x), 0, 2*pi, rgbcolor=hue(0.3))3752 sage: polar_plot(2 + 2*cos(x), 0, 2*pi, rgbcolor=hue(0.3)) 3729 3753 3730 3754 """ 3731 3755 return plot(funcs, xmin, xmax, polar=True, **kwargs) -
a/sage/structure/sage_object.pyx
old new 91 91 if hasattr(self, '_repr_'): 92 92 return self._repr_() 93 93 return str(type(self)) 94 95 def plot(self, *args, **kwds):96 import sage.plot.plot97 if len(args) == 0 and len(kwds) == 0:98 return sage.plot.plot.text(repr(self), (0,0))99 else:100 try:101 return sage.plot.plot.text(repr(self), *args, **kwds)102 except TypeError:103 return sage.plot.plot.text(repr(self), (0,0))104 94 105 95 def __hash__(self): 106 96 return hash(self.__repr__())