Reputation: 23
Given a function from R into R^n, I'd like to define a new function by precomposition, for example as follows
alpha(x) = [e^x,e^(-x)]
beta(x) = alpha(-x+2)
However attempting to do so in this way throws an error "unable to convert (e^(-x + 2), e^(x - 2)) to a symbolic expression"
Now the similar but simpler version of the code
alpha(x) = e^x
beta(x) = alpha(-x+2)
works perfectly, so the issue arrises from the fact that alpha is multivalued.
The following variant of the original code does exactly what I want
alpha(x) = [e^x,e^(-x)]
beta(x) = [alpha[0](-x+2),alpha[1](-x+2)]
but requires me to assume the length of alpha, which is undesirable. And the obvious solution to that problem
alpha(x) = [e^x,e^(-x)]
for i in range(0,len(alpha)):
beta[i](x) = alpha[i](x)
or any variant thereupon throws the error "can't assign to function call"
My question is as follows:
Is there any way to do this precomposition? In particular without assuming the length of alpha. I control how the functions alpha and beta are defined, so if theres another way of defining them (for example using lambda notation or something like that) that lets me do this, that's acceptable too. But note that I would like to do some equivalent of the following at some point in my code
... + beta.derivative(x).dot_product( ...
Upvotes: 2
Views: 754
Reputation: 3453
Defined as in the question, alpha is not a symbolic function returning vectors, but a vector of callable functions.
Below we describe two other ways of defining alpha and beta, either defining alpha as a vector over the symbolic ring, and defining beta by substitution, or defining alpha and beta as Python functions.
Original approach in the question:
sage: alpha(x) = [e^x, e^-x]
sage: alpha
x |--> (e^x, e^(-x))
sage: alpha.parent()
Vector space of dimension 2 over Callable function ring with argument x
Using a vector over the symbolic ring
sage: alpha = vector([e^x, e^-x])
sage: alpha
(e^x, e^(-x))
sage: alpha.parent()
Vector space of dimension 2 over Symbolic Ring
sage: beta = alpha.subs({x: -x + 2})
sage: beta
(e^(-x + 2), e^(x - 2))
Using Python functions
sage: def alpha(x):
....: return vector([e^x, e^-x])
....:
sage: def beta(x):
....: return alpha(-x + 2)
....:
sage: beta(x)
(e^(-x + 2), e^(x - 2))
Some related resources.
Query:
Questions:
Tickets:
Upvotes: 2