49 | | from sage.symbolic.ring import SR |
| 48 | from sage.symbolic.all import SR, Expression |
| 49 | |
| 50 | |
| 51 | def apply_map(phi): |
| 52 | """ |
| 53 | Returns a function that applies phi to its argument. |
| 54 | |
| 55 | EXAMPLES:: |
| 56 | |
| 57 | sage: from sage.modules.vector_symbolic_dense import apply_map |
| 58 | sage: v = vector([1,2,3]) |
| 59 | sage: f = apply_map(lambda x: x+1) |
| 60 | sage: f(v) |
| 61 | (2, 3, 4) |
| 62 | |
| 63 | """ |
| 64 | def apply(self, *args, **kwds): |
| 65 | """ |
| 66 | Generic function used to implement common symbolic operations |
| 67 | elementwise as methods of a vector. |
| 68 | |
| 69 | EXAMPLES:: |
| 70 | |
| 71 | sage: var('x,y') |
| 72 | (x, y) |
| 73 | sage: v = vector([sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), factorial(x+1)/factorial(x)]) |
| 74 | sage: v.simplify_trig() |
| 75 | (1, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x)) |
| 76 | sage: v.simplify_radical() |
| 77 | (sin(x)^2 + cos(x)^2, log(x) + log(y), sin(1/(x + 1)), factorial(x + 1)/factorial(x)) |
| 78 | sage: v.simplify_rational() |
| 79 | (sin(x)^2 + cos(x)^2, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x)) |
| 80 | sage: v.simplify_factorial() |
| 81 | (sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), x + 1) |
| 82 | sage: v.simplify_full() |
| 83 | (1, log(x*y), sin(1/(x + 1)), x + 1) |
| 84 | |
| 85 | sage: v = vector([sin(2*x), sin(3*x)]) |
| 86 | sage: v.simplify_trig() |
| 87 | (2*sin(x)*cos(x), (4*cos(x)^2 - 1)*sin(x)) |
| 88 | sage: v.simplify_trig(False) |
| 89 | (sin(2*x), sin(3*x)) |
| 90 | sage: v.simplify_trig(expand=False) |
| 91 | (sin(2*x), sin(3*x)) |
| 92 | """ |
| 93 | return self.apply_map(lambda x: phi(x, *args, **kwds)) |
| 94 | apply.__doc__ += "\nSee Expression." + phi.__name__ + "() for optional arguments." |
| 95 | return apply |
54 | | def simplify_full(self): |
55 | | """ |
56 | | Applies :meth:`simplify_full` to the entries of self. |
57 | | |
58 | | EXAMPLES:: |
59 | | |
60 | | sage: u = vector([sin(x)^2 + cos(x)^2, 1]) |
61 | | sage: u.simplify_full() |
62 | | (1, 1) |
63 | | sage: v = vector([log(exp(x))]) |
64 | | sage: v.simplify_full() |
65 | | (x) |
66 | | |
67 | | """ |
68 | | return (SR**len(self))([fun.simplify_full() for fun in self]) |
| 101 | # Add elementwise methods. |
| 102 | for method in ['simplify', 'simplify_exp', 'simplify_factorial', |
| 103 | 'simplify_log', 'simplify_radical', 'simplify_rational', |
| 104 | 'simplify_trig', 'simplify_full', 'trig_expand', 'trig_reduce']: |
| 105 | setattr(Vector_symbolic_dense, method, apply_map(getattr(Expression, method))) |