X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fsymbolic.py;h=ec3fc99e1e451b95b33eed22790b3dc089f617d1;hb=9a08c865c94e7efd3350e65ff73f67341c191be5;hp=e4c6d23faef7117bcd7807da3d95d84a39fd18ad;hpb=3a61b951df61f4175bcf61700454ed978b63b2ef;p=sage.d.git diff --git a/mjo/symbolic.py b/mjo/symbolic.py index e4c6d23..ec3fc99 100644 --- a/mjo/symbolic.py +++ b/mjo/symbolic.py @@ -9,7 +9,23 @@ def set_simplification_domain(d): INPUT: - - d -- The domain, either 'real' or 'complex'. + - ``d`` -- The domain, either 'real' or 'complex'. + + TESTS: + + With the default 'complex' domain, we don't simplify this:: + + sage: (abs(x)^2).simplify() + abs(x)^2 + + But in the 'real' domain, we do:: + + sage: set_simplification_domain('real') + 'real' + sage: (abs(x)^2).simplify() + x^2 + sage: set_simplification_domain('complex') + 'complex' """ cmd = 'domain: %s;' % d @@ -30,16 +46,43 @@ def safe_simplify(expr): return expr -def medium_simplify(expr): +def matrix_subs_expr(m, *equations): """ - A reasonably-safe set of simplifications, much better than - simplify() and safer than simplify_full() + Symbolic matrices have a `subs()` method, but no `subs_expr()`. + This makes it diffucult to substitute in a list of solutions obtained + with `solve()`. + + INPUT: + + - ``m`` -- A symbolic matrix. + + - ``equations`` - One or more symbolic equations, presumably for + the entries of `m`. + + OUTPUT: + + The result of substituting each equation into `m`, one after another. + + EXAMPLES:: + + sage: w,x,y,z = SR.var('w,x,y,z') + sage: A = matrix(SR, [[w,x],[y,z]]) + sage: matrix_subs_expr(A, w == 1, x == 2, y == 3, z == 4) + [1 2] + [3 4] - Uses a top-level function because we can't monkey-patch Cython - classes. """ - expr = expr.simplify_factorial() - expr = expr.simplify_trig() - expr = expr.simplify_rational() - expr = expr.simplify_log() - return expr + from sage.symbolic.expression import is_SymbolicEquation + + if not m.base_ring() == SR: + raise TypeError, 'the matrix "m" must be symbolic' + + if isinstance(equations[0], dict): + eq_dict = equations[0] + equations = [ x == eq_dict[x] for x in eq_dict.keys() ] + + if not all([is_SymbolicEquation(eq) for eq in equations]): + raise TypeError, "each expression must be an equation" + + d = dict([(eq.lhs(), eq.rhs()) for eq in equations]) + return m.subs(d)