]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/symbolic.py
Add the matrix_simplify_full() function to symbolic.py.
[sage.d.git] / mjo / symbolic.py
1 from sage.all import *
2 from sage.interfaces.maxima_lib import maxima_lib
3 from sage.symbolic.expression import Expression
4
5
6 def set_simplification_domain(d):
7 """
8 Set Maxima's simplification domain.
9
10 INPUT:
11
12 - ``d`` -- The domain, either 'real' or 'complex'.
13
14 TESTS:
15
16 With the default 'complex' domain, we don't simplify this::
17
18 sage: (abs(x)^2).simplify()
19 abs(x)^2
20
21 But in the 'real' domain, we do::
22
23 sage: set_simplification_domain('real')
24 'real'
25 sage: (abs(x)^2).simplify()
26 x^2
27 sage: set_simplification_domain('complex')
28 'complex'
29
30 """
31 cmd = 'domain: %s;' % d
32 result = maxima_lib._eval_line(cmd)
33 return result
34
35
36 def safe_simplify(expr):
37 """
38 What should be a totally safe simplification operation that works
39 a little better than the plain simplify().
40
41 Uses a top-level function because we can't monkey-patch Cython
42 classes.
43 """
44 expr = expr.simplify_factorial()
45 expr = expr.simplify_log()
46 return expr
47
48
49 def matrix_subs_expr(m, *equations):
50 """
51 Symbolic matrices have a `subs()` method, but no `subs_expr()`.
52 This makes it diffucult to substitute in a list of solutions obtained
53 with `solve()`.
54
55 INPUT:
56
57 - ``m`` -- A symbolic matrix.
58
59 - ``equations`` - One or more symbolic equations, presumably for
60 the entries of `m`.
61
62 OUTPUT:
63
64 The result of substituting each equation into `m`, one after another.
65
66 EXAMPLES::
67
68 sage: w,x,y,z = SR.var('w,x,y,z')
69 sage: A = matrix(SR, [[w,x],[y,z]])
70 sage: matrix_subs_expr(A, w == 1, x == 2, y == 3, z == 4)
71 [1 2]
72 [3 4]
73
74 """
75 from sage.symbolic.expression import is_SymbolicEquation
76
77 if not m.base_ring() == SR:
78 raise TypeError, 'the matrix "m" must be symbolic'
79
80 if isinstance(equations[0], dict):
81 eq_dict = equations[0]
82 equations = [ x == eq_dict[x] for x in eq_dict.keys() ]
83
84 if not all([is_SymbolicEquation(eq) for eq in equations]):
85 raise TypeError, "each expression must be an equation"
86
87 d = dict([(eq.lhs(), eq.rhs()) for eq in equations])
88 return m.subs(d)
89
90
91 def matrix_simplify_full(A):
92 """
93 Simplify each entry of a symbolic matrix using the
94 Expression.simplify_full() method.
95
96 INPUT:
97
98 - ``A`` - The matrix whose entries we should simplify.
99
100 OUTPUT:
101
102 A copy of ``A`` with all of its entries simplified.
103
104 EXAMPLES:
105
106 Symbolic matrices (examples stolen from Expression.simplify_full())
107 will have their entries simplified::
108
109 sage: a,n,k = SR.var('a,n,k')
110 sage: f1 = sin(x)^2 + cos(x)^2
111 sage: f2 = sin(x/(x^2 + x))
112 sage: f3 = binomial(n,k)*factorial(k)*factorial(n-k)
113 sage: f4 = x*sin(2)/(x^a)
114 sage: A = matrix(SR, [[f1,f2],[f3,f4]])
115 sage: matrix_simplify_full(A)
116 [ 1 sin(1/(x + 1))]
117 [ factorial(n) x^(-a + 1)*sin(2)]
118
119 But an exception will be raised if ``A`` is not symbolic::
120
121 sage: A = matrix(QQ, [[1,2],[3,4]])
122 sage: matrix_simplify_full(A)
123 Traceback (most recent call last):
124 ...
125 ValueError: The base ring of `A` must be the Symbolic Ring.
126
127 """
128 if not A.base_ring() == SR:
129 raise ValueError('The base ring of `A` must be the Symbolic Ring.')
130
131 M = A.matrix_space()
132 return M(map(lambda x: x.simplify_full(), A))