X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fsymbolic.py;h=3760596c66e3f8f53cded6dbb9b3f9f636324387;hb=6c8c727709ab6e342f9b2dfa440a88e6ae35fcf5;hp=ec3fc99e1e451b95b33eed22790b3dc089f617d1;hpb=f352ef9a0ab5fbc784becdef65e20fe10d80e67e;p=sage.d.git diff --git a/mjo/symbolic.py b/mjo/symbolic.py index ec3fc99..3760596 100644 --- a/mjo/symbolic.py +++ b/mjo/symbolic.py @@ -46,43 +46,45 @@ def safe_simplify(expr): return expr -def matrix_subs_expr(m, *equations): +def matrix_simplify_full(A): """ - Symbolic matrices have a `subs()` method, but no `subs_expr()`. - This makes it diffucult to substitute in a list of solutions obtained - with `solve()`. + Simplify each entry of a symbolic matrix using the + Expression.simplify_full() method. INPUT: - - ``m`` -- A symbolic matrix. - - - ``equations`` - One or more symbolic equations, presumably for - the entries of `m`. + - ``A`` - The matrix whose entries we should simplify. OUTPUT: - The result of substituting each equation into `m`, one after another. + A copy of ``A`` with all of its entries simplified. - EXAMPLES:: + 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] + Symbolic matrices (examples stolen from Expression.simplify_full()) + will have their entries simplified:: - """ - from sage.symbolic.expression import is_SymbolicEquation + sage: a,n,k = SR.var('a,n,k') + sage: f1 = sin(x)^2 + cos(x)^2 + sage: f2 = sin(x/(x^2 + x)) + sage: f3 = binomial(n,k)*factorial(k)*factorial(n-k) + sage: f4 = x*sin(2)/(x^a) + sage: A = matrix(SR, [[f1,f2],[f3,f4]]) + sage: matrix_simplify_full(A) + [ 1 sin(1/(x + 1))] + [ factorial(n) x^(-a + 1)*sin(2)] - if not m.base_ring() == SR: - raise TypeError, 'the matrix "m" must be symbolic' + But an exception will be raised if ``A`` is not symbolic:: - if isinstance(equations[0], dict): - eq_dict = equations[0] - equations = [ x == eq_dict[x] for x in eq_dict.keys() ] + sage: A = matrix(QQ, [[1,2],[3,4]]) + sage: matrix_simplify_full(A) + Traceback (most recent call last): + ... + ValueError: The base ring of `A` must be the Symbolic Ring. - if not all([is_SymbolicEquation(eq) for eq in equations]): - raise TypeError, "each expression must be an equation" + """ + if not A.base_ring() == SR: + raise ValueError('The base ring of `A` must be the Symbolic Ring.') - d = dict([(eq.lhs(), eq.rhs()) for eq in equations]) - return m.subs(d) + M = A.matrix_space() + return M(map(lambda x: x.simplify_full(), A))