Ask Your Question
2

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

asked 2020-07-01 22:42:06 +0200

martincmartin gravatar image
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 flag offensive close merge delete

Comments

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

martincmartin gravatar imagemartincmartin ( 2020-07-01 23:12:54 +0200 )edit

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

Using Python 3.7.3.

martincmartin gravatar imagemartincmartin ( 2020-07-01 23:13:39 +0200 )edit

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!

dsejas gravatar imagedsejas ( 2020-07-02 01:07:24 +0200 )edit

A similar example, but using vector:

f(t) = vector([t, t*t])
TypeError: unable to convert (t, t^2) to a symbolic expression
martincmartin gravatar imagemartincmartin ( 2020-07-08 17:26:51 +0200 )edit

3 Answers

Sort by » oldest newest most voted
2

answered 2021-04-14 00:09:25 +0200

RozaTh gravatar image

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!

edit flag offensive delete link more

Comments

Nice trick!

Using SageManifolds might also help with such problems.

slelievre gravatar imageslelievre ( 2021-04-14 00:43:22 +0200 )edit
1

answered 2021-04-15 13:23:06 +0200

eric_g gravatar image

updated 2021-04-15 21:51:03 +0200

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)))
edit flag offensive delete link more
1

answered 2021-04-14 08:06:17 +0200

Emmanuel Charpentier gravatar image

Is this :

sage: def f(*args): return(sin(args[0]),cos(args[0]))
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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-07-01 22:42:06 +0200

Seen: 212 times

Last updated: Apr 15