# Why unable to convert (sin(h(x)), cos(h(x))) to a symbolic expression?

f(x) = (sin(x), cos(x))
h = function('h', nargs=1)
f(h(x))


works fine and produces (sin(h(x)), cos(h(x))). However, if I try to assign that to a function:

g(x) = f(h(x))
TypeError: unable to convert (sin(h(x)), cos(h(x))) to a symbolic expression


How can I compose functions like this?

edit retag close merge delete

symbolic_expression(f(h(x))) fails, but symbolic_expression( (sin(h(x)), cos(h(x))) ) succeeds.

SageMath version 9.1, Release Date: 2020-05-20

Using Python 3.7.3.

Hello! By carefully checking the code for symbolic_expression(), I can see that the problem actually happens when that command sends its output to SR. Here is a simpler example:

f(x) = (x^2, sqrt(x))
h(x) = 2*x^3
SR(f(h(x)))


If $f$ is a $\mathbb{R}\rightarrow\mathbb{R}$, instead of $\mathbb{R}\rightarrow\mathbb{R}^2$, there is no error message.

I hope this helps to pinpoint the location of the problem!

A similar example, but using vector:

f(t) = vector([t, t*t])
TypeError: unable to convert (t, t^2) to a symbolic expression


Sort by » oldest newest most voted

I had the same problem, I don't know the reason why g(x) = f(h(x)) does not work. But this works (Sage 9.2):

g(x) = [*f(h(x))]


and for g, and g(x) you get:

x ↦ (sin(h(x)),cos(h(x)))
(sin(h(x)),cos(h(x)))


Here, we are basically unpacking and then repacking the array!

more

Nice trick!

Using SageManifolds might also help with such problems.

Following @slelievre 's comment, here is a possible answer using maps $\mathbb{R} \to \mathbb{R}^2$ and $\mathbb{R}\to\mathbb{R}$:

We define first $f$ as a differentiable map $\mathbb{R} \to \mathbb{R}^2$ :

sage: R.<x> = RealLine()
sage: R2.<X,Y> = EuclideanSpace(name='R^2')
sage: f = R.diff_map(R2, (sin(x), cos(x)))
sage: f.display()
R --> R^2
x |--> (X, Y) = (sin(x), cos(x))
sage: f(x)
Point on the Euclidean plane R^2
sage: f(x).coord()
(sin(x), cos(x))
sage: f(pi).coord()
(0, -1)


One can also access to the coordinate functions representing $f$:

sage: fc = f.coord_functions()
sage: fc
Coordinate functions (sin(x), cos(x)) on the Chart (R, (x,))
sage: fc(x)
(sin(x), cos(x))
sage: fc(pi)
(0, -1)


Then we define $H$ as the differentiable map $\mathbb{R}\to\mathbb{R}$ whose coordinate expression is $h(x)$:

sage: H = R.diff_map(R, function('h')(x))
sage: H.display()
R --> R
x |--> h(x)


and we compose $f$ by $H$ by means of the operator *:

sage: g = f * H
sage: g
Curve in the Euclidean plane R^2
sage: g.display()
R --> R^2
x |--> (X, Y) = (sin(h(x)), cos(h(x)))


Then

sage: g(x)
Point on the Euclidean plane R^2
sage: g(x).coord()
(sin(h(x)), cos(h(x)))
sage: g(pi).coord()
(sin(h(pi)), cos(h(pi)))
sage: gc = g.coord_functions(); gc
Coordinate functions (sin(h(x)), cos(h(x))) on the Chart (R, (x,))
sage: gc(pi)
(sin(h(pi)), cos(h(pi)))

more

Is this :

sage: def f(*args): return(sin(args),cos(args))
sage: h=function("h")
sage: f(h(x))
(sin(h(x)), cos(h(x)))


what you want ?

A symbolic function returns SR value. A list, a tuple, a vector are not in SR.

more