Path: blob/master/sage/modules/vector_symbolic_dense.py
4057 views
"""1Vectors over the symbolic ring.23Implements vectors over the symbolic ring.45AUTHORS:67- Robert Bradshaw (2011-05-25): Added more element-wise simplification methods89- Joris Vankerschaver (2011-05-15): Initial version1011EXAMPLES::1213sage: x, y = var('x, y')14sage: u = vector([sin(x)^2 + cos(x)^2, log(2*y) + log(3*y)]); u15(sin(x)^2 + cos(x)^2, log(2*y) + log(3*y))16sage: type(u)17<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>18sage: u.simplify_full()19(1, log(6) + 2*log(y))2021TESTS:2223Check that the outcome of arithmetic with symbolic vectors is again24a symbolic vector (#11549)::2526sage: v = vector(SR, [1, 2])27sage: w = vector(SR, [sin(x), 0])28sage: type(v)29<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>30sage: type(w)31<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>32sage: type(v + w)33<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>34sage: type(-v)35<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>36sage: type(5*w)37<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>3839Test pickling/unpickling::4041sage: u = vector(SR, [sin(x^2)])42sage: loads(dumps(u)) == u43True4445"""4647#*****************************************************************************48# Copyright (C) 2011 Joris Vankerschaver ([email protected])49#50# Distributed under the terms of the GNU General Public License (GPL)51# as published by the Free Software Foundation; either version 2 of52# the License, or (at your option) any later version.53# http://www.gnu.org/licenses/54#*****************************************************************************5556import free_module_element57from sage.symbolic.all import Expression585960def apply_map(phi):61"""62Returns a function that applies phi to its argument.6364EXAMPLES::6566sage: from sage.modules.vector_symbolic_dense import apply_map67sage: v = vector([1,2,3])68sage: f = apply_map(lambda x: x+1)69sage: f(v)70(2, 3, 4)7172"""73def apply(self, *args, **kwds):74"""75Generic function used to implement common symbolic operations76elementwise as methods of a vector.7778EXAMPLES::7980sage: var('x,y')81(x, y)82sage: v = vector([sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), factorial(x+1)/factorial(x)])83sage: v.simplify_trig()84(1, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))85sage: v.simplify_radical()86(sin(x)^2 + cos(x)^2, log(x) + log(y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))87sage: v.simplify_rational()88(sin(x)^2 + cos(x)^2, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))89sage: v.simplify_factorial()90(sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), x + 1)91sage: v.simplify_full()92(1, log(x*y), sin(1/(x + 1)), x + 1)9394sage: v = vector([sin(2*x), sin(3*x)])95sage: v.simplify_trig()96(2*sin(x)*cos(x), (4*cos(x)^2 - 1)*sin(x))97sage: v.simplify_trig(False)98(sin(2*x), sin(3*x))99sage: v.simplify_trig(expand=False)100(sin(2*x), sin(3*x))101"""102return self.apply_map(lambda x: phi(x, *args, **kwds))103apply.__doc__ += "\nSee Expression." + phi.__name__ + "() for optional arguments."104return apply105106107class Vector_symbolic_dense(free_module_element.FreeModuleElement_generic_dense):108pass109110# Add elementwise methods.111for method in ['simplify', 'simplify_exp', 'simplify_factorial',112'simplify_log', 'simplify_radical', 'simplify_rational',113'simplify_trig', 'simplify_full', 'trig_expand', 'trig_reduce']:114setattr(Vector_symbolic_dense, method, apply_map(getattr(Expression, method)))115116117