X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fsymbolic.py;h=3760596c66e3f8f53cded6dbb9b3f9f636324387;hb=6c8c727709ab6e342f9b2dfa440a88e6ae35fcf5;hp=e4c6d23faef7117bcd7807da3d95d84a39fd18ad;hpb=3a61b951df61f4175bcf61700454ed978b63b2ef;p=sage.d.git diff --git a/mjo/symbolic.py b/mjo/symbolic.py index e4c6d23..3760596 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,45 @@ def safe_simplify(expr): return expr -def medium_simplify(expr): +def matrix_simplify_full(A): """ - A reasonably-safe set of simplifications, much better than - simplify() and safer than simplify_full() + 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. - 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 + 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))