]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
Add the matrix_simplify_full() function to symbolic.py.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 29 Oct 2014 22:24:52 +0000 (18:24 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 29 Oct 2014 22:24:52 +0000 (18:24 -0400)
mjo/symbolic.py

index ec3fc99e1e451b95b33eed22790b3dc089f617d1..0afd5a5651ce9dedc3bccab518a6b5f4ec0ee492 100644 (file)
@@ -86,3 +86,47 @@ def matrix_subs_expr(m, *equations):
 
     d = dict([(eq.lhs(), eq.rhs()) for eq in equations])
     return m.subs(d)
+
+
+def matrix_simplify_full(A):
+    """
+    Simplify each entry of a symbolic matrix using the
+    Expression.simplify_full() method.
+
+    INPUT:
+
+      - ``A`` - The matrix whose entries we should simplify.
+
+    OUTPUT:
+
+    A copy of ``A`` with all of its entries simplified.
+
+    EXAMPLES:
+
+    Symbolic matrices (examples stolen from Expression.simplify_full())
+    will have their entries simplified::
+
+        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)]
+
+    But an exception will be raised if ``A`` is not symbolic::
+
+        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 A.base_ring() == SR:
+        raise ValueError('The base ring of `A` must be the Symbolic Ring.')
+
+    M = A.matrix_space()
+    return M(map(lambda x: x.simplify_full(), A))