Ticket #12075: trac_12075.patch
File trac_12075.patch, 9.5 KB (added by , 9 years ago) 


sage/calculus/all.py
# HG changeset patch # User Geoffrey Ehrman <gehrman@gmail.com> # Date 1326233123 28800 # Node ID 4953e331f0a71ed6161d8faf303eab7f1d3e6f37 # Parent 92c93226b64f933e0af00bbcbd1a8a79c444f43f Trac ticket 12075: (Create callable matrices in function notation) diff git a/sage/calculus/all.py b/sage/calculus/all.py
a b 21 21 lazy_import("sage.calculus.interpolators",["polygon_spline","complex_cubic_spline"]) 22 22 23 23 from sage.modules.all import vector 24 from sage.matrix.all import matrix 24 25 25 26 def symbolic_expression(x): 26 27 """ 27 Create a symbolic expression or vector of symbolic expressions from x. 28 From x, create one of the following: 29 30 a symbolic expression, 31 32 a vector of symbolic expressions, 33 34 a matrix of symbolic expressions. 28 35 29 36 INPUT: 30 37 … … 63 70 sage: symbolic_expression(E) in SR 64 71 True 65 72 66 If x is a list or tuple , create a vector of symbolic expressions::73 If x is a list or tuple one level deep, create a vector of symbolic expressions:: 67 74 68 75 sage: v=symbolic_expression([x,1]); v 69 76 (x, 1) … … 83 90 (x*y + y^2 + y == x^3 + x^2  10*x  10, x*y + y^2 + y == x^3 + x^2  10*x  10) 84 91 sage: v.base_ring() 85 92 Symbolic Ring 93 94 If x and x[0] are lists or tuples, create a matrix of symbolic expressions:: 95 96 sage: m=symbolic_expression([[x,1],[0,x]]); m 97 [x 1] 98 [0 x] 99 sage: m.base_ring() 100 Symbolic Ring 101 sage: m=symbolic_expression(((x,1),(0,x))); m 102 [x 1] 103 [0 x] 104 sage: m=symbolic_expression(([x,1],(0,x))); m #mixing tuples and lists is fine 105 [x 1] 106 [0 x] 107 sage: m.base_ring() 108 Symbolic Ring 109 sage: m=symbolic_expression(((3,0),(1,1))); m 110 [3 0] 111 [1 1] 112 sage: v.base_ring() 113 Symbolic Ring 114 sage: E = EllipticCurve('15a'); E 115 Elliptic Curve defined by y^2 + x*y + y = x^3 + x^2  10*x  10 over Rational Field 116 sage: m=symbolic_expression([[E,E],[E,E]]); m 117 [x*y + y^2 + y == x^3 + x^2  10*x  10 x*y + y^2 + y == x^3 + x^2  10*x  10] 118 [x*y + y^2 + y == x^3 + x^2  10*x  10 x*y + y^2 + y == x^3 + x^2  10*x  10] 119 sage: m.base_ring() 120 Symbolic Ring 86 121 """ 87 122 from sage.symbolic.expression import Expression 88 123 from sage.symbolic.ring import SR … … 90 125 return x 91 126 elif hasattr(x, '_symbolic_'): 92 127 return x._symbolic_(SR) 93 elif isinstance(x, (tuple,list)): 94 return vector(SR,x) 128 elif isinstance(x, (tuple, list)): 129 if isinstance(x[0], (tuple, list)): 130 return matrix(SR, x) 131 else: 132 return vector(SR, x) 95 133 else: 96 134 return SR(x) 97 135 
sage/matrix/matrix2.pyx
diff git a/sage/matrix/matrix2.pyx b/sage/matrix/matrix2.pyx
a b 11618 11618 companions.append(sage.matrix.constructor.companion_matrix(poly, format=format)) 11619 11619 return sage.matrix.constructor.block_diagonal_matrix(companions, subdivide=subdivide) 11620 11620 11621 def function(self, *args): 11622 """ 11623 Returns a matrix over a calllable symbolic expression ring. This is 11624 needed to create matrices with callable entries with function syntax. 11625 11626 EXAMPLES:: 11627 11628 sage: x, y, t = var('x y t') 11629 sage: m = matrix([[x, x*y  t^2*y*x],[0,y]]) 11630 sage: f = m.function([x, y, t]); f 11631 [ (x, y, t) > x (x, y, t) > t^2*x*y + x*y] 11632 [ (x, y, t) > 0 (x, y, t) > y] 11633 sage: f.base_ring() 11634 Callable function ring with arguments (x, y, t) 11635 sage: f(0, 0, 1) 11636 [0 0] 11637 [0 0] 11638 sage: f(1, 1, 0) 11639 [1 1] 11640 [0 1] 11641 sage: f(1, 1, 1) 11642 [1 0] 11643 [0 1] 11644 sage: f(1, 3) 11645 [ 1 3*t^2 + 3] 11646 [ 0 3] 11647 sage: f(t=0) 11648 [ x x*y] 11649 [ 0 y] 11650 sage: f(y=2,t=1/2,x=4) 11651 [4 6] 11652 [0 2] 11653 """ 11654 from sage.symbolic.callable import CallableSymbolicExpressionRing 11655 return matrix_space.MatrixSpace(CallableSymbolicExpressionRing(args), self.nrows(), self.ncols())(self.list()) 11656 11621 11657 # A limited number of accessonly properties are provided for matrices 11622 11658 property T: 11623 11659 r""" 
new file sage/matrix/matrix_callable_symbolic_dense.py
diff git a/sage/matrix/matrix_callable_symbolic_dense.py b/sage/matrix/matrix_callable_symbolic_dense.py new file mode 100644
 + 1 """ 2 Matrices over callable symbolic rings 3 4 AUTHOR: 5  Geoffrey Ehrman (2012) 6 7 EXAMPLES:: 8 sage: l(t) = ((cos(t), 0), (0, sin(t))) 9 sage: m(theta, z) = [[cos(theta), sin(theta), 0],[sin(theta), cos(theta), 0],[0,0,z]] 10 sage: m.parent() 11 Full MatrixSpace of 3 by 3 dense matrices over Callable function ring with arguments (theta, z) 12 sage: m 13 [ (theta, z) > cos(theta) (theta, z) > sin(theta) (theta, z) > 0] 14 [ (theta, z) > sin(theta) (theta, z) > cos(theta) (theta, z) > 0] 15 [ (theta, z) > 0 (theta, z) > 0 (theta, z) > z] 16 sage: type(m) 17 <type 'sage.matrix.matrix_generic_dense.Matrix_generic_dense'> 18 sage: m(0) 19 [1 0 0] 20 [0 1 0] 21 [0 0 z] 22 sage: m(z = 4) 23 [ cos(theta) sin(theta) 0] 24 [ sin(theta) cos(theta) 0] 25 [ 0 0 4] 26 sage: m(pi/2, 1) 27 [ 0 1 0] 28 [ 1 0 0] 29 [ 0 0 1] 30 sage: m.trace() 31 (theta, z) > z + 2*cos(theta) 32 sage: m.kernel() 33 Vector space of degree 3 and dimension 0 over Callable function ring with arguments (theta, z) 34 Basis matrix: 35 [] 36 sage: m.image() 37 Vector space of degree 3 and dimension 3 over Callable function ring with arguments (theta, z) 38 Basis matrix: 39 [(theta, z) > 1 (theta, z) > 0 (theta, z) > 0] 40 [(theta, z) > 0 (theta, z) > 1 (theta, z) > 0] 41 [(theta, z) > 0 (theta, z) > 0 (theta, z) > 1] 42 43 TESTS:: 44 45 sage: m(u,v,w) = ((2*u+v,uw),(u*v*w,w^2+u)) 46 sage: loads(dumps(m)) == m 47 True 48 49 50 """ 51 52 #***************************************************************************** 53 # Copyright (C) 2012 Geoffrey Ehrman <gehrman@gmail.com> 54 # 55 # Distributed under the terms of the GNU General Public License (GPL) 56 # 57 # This code is distributed in the hope that it will be useful, 58 # but WITHOUT ANY WARRANTY; without even the implied warranty of 59 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 60 # General Public License for more details. 61 # 62 # The full text of the GPL is available at: 63 # 64 # http://www.gnu.org/licenses/ 65 #***************************************************************************** 66 67 from sage.matrix.matrix_generic_dense import Matrix_generic_dense 68 from sage.symbolic.ring import SR 69 70 71 class Matrix_callable_symbolic_dense(Matrix_generic_dense): 72 """ 73 Matrices over callable symbolic rings. 74 75 Both the methods _repr_() and _latex_() are 76 """ 77 def _repr_(self): 78 """ 79 Return the string representation of the matrix. 80 81 EXAMPLES:: 82 sage: m(theta, z) = [[cos(theta), sin(theta), 0],[sin(theta), cos(theta), 0],[0,0,z]] 83 sage: m 84 sage: l(t) = ((cos(t), 0), (0, sin(t))) 85 sage: l 86 """ 87 ring = self.base_ring() 88 args = ring.arguments() 89 repr_x = self.change_ring(SR)._repr_() 90 if len(args) == 1: 91 return "%s > %s" % (args[0], repr_x) 92 else: 93 args = ", ".join(map(str, args)) 94 return "(%s) > %s" % (args, repr_x) 95 96 97 def _latex_(self): 98 """ 99 Returns the latex representation of the vector 100 101 TODO: 102 It may be best to rewrite this (at least partially) when mathjax is 103 integrated into the notebook in 5.0. Note the comment regarding 104 working around a JsMath bug. 105 106 EXAMPLES:: 107 sage: m(theta, z) = [[cos(theta), sin(theta), 0],[sin(theta), cos(theta), 0],[0,0,z]] 108 sage: latex(m) 109 sage: l(t) = ((cos(t), 0), (0, sin(t))) 110 sage: latex(l) 111 """ 112 from sage.misc.latex import latex 113 ring=self.base_ring() 114 args = ring.arguments() 115 args = [latex(arg) for arg in args] 116 latex_x = self.change_ring(SR)._latex_() 117 if len(args) == 1: 118 return r"%s \ {\mapsto}\ %s" % (args[0], latex_x) 119 else: 120 vars = ", ".join(args) 121 # the weird TeX is to workaround an apparent JsMath bug 122 return r"\left( %s \right) \ {\mapsto} \ %s" % (vars, latex_x) 123 
sage/matrix/matrix_space.py
diff git a/sage/matrix/matrix_space.py b/sage/matrix/matrix_space.py
a b 416 416 return self.__matrix_class(self, None, coerce=coerce, copy=copy) 417 417 418 418 if isinstance(entries, (list, tuple)) and len(entries) > 0 and \ 419 sage.modules.free_module_element.is_FreeModuleElement(entries[0]):419 sage.modules.free_module_element.is_FreeModuleElement(entries[0]): 420 420 #Try to determine whether or not the entries should 421 421 #be rows or columns 422 422 if rows is None: