Ask Your Question

Substitute multiplication of sine and cosine for a symbolic function

asked 2021-11-22 16:00:40 +0100

keko gravatar image

updated 2021-11-23 07:18:36 +0100

I have one variable and two functions:

th = var('th')
f = function('f')(th)
omega = function('omega')(th)

and the following equation (this is an example, my actual equation has more terms such as +cos(th)+sin(th)):

eq = 2*pi*cos(th)*sin(th)*diff(omega,th,th)

Now, I want cos(th)*sin(th) to be substituted for f. I have tried the following:

eq = eq.substitute(cos(th)*sin(th)==f)

and also,

expression = cos(th)*sin(th)
eq = eq.substitute_function(expression,f)

but when I print eq, I still get:

2 * pi * cos(th) * sin(th) * diff(omega(th), th)

How can I do it? It should also work for different permutations of the individual components of the equation. For example:

eq = 2*pi*sin(th)*diff(omega,th,th)*cos(th)

I have found that a similar question was asked a long time ago, where the author commented that maxima.fullratsubst did the job. However, I am not capable of working with it; I have run

eq = maxima.fullratsubst(f,cos(th)*sin(th),eq)

directly on SageMath, but I guess I am doing something wrong since it does not work.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2021-11-23 08:45:02 +0100

Emmanuel Charpentier gravatar image


sage: th = var('th')
sage: f = function('f')(th)
sage: omega = function('omega')(th)
sage: eq = 2*pi*cos(th)*sin(th)*diff(omega,th,th)
sage: w0 = SR.wild(0)
sage: eq.subs([w0*sin(th)*cos(th)==w0*f])
2*pi*f(th)*diff(omega(th), th, th)

The use of a wildcard allows for partial substitution...

As for Maxima :

sage: f.maxima_methods().ratsubst(cos(th)*sin(th),eq)
2*pi*f(th)*diff(omega(th), th, th)

Also :

sage: maxima_calculus.lratsubst([cos(th)*sin(th)==f],eq)._sage_()
2*pi*f(th)*diff(omega(th), th, th)


edit flag offensive delete link more

answered 2021-11-22 16:27:44 +0100

Max Alekseyev gravatar image

updated 2021-11-22 16:28:20 +0100

cos(th)*sin(th)==f is equivalent to th==arcsin(2*f)/2, and so substitution eq.subs({th:arcsin(2*f)/2}) should do the job.

edit flag offensive delete link more


Yes, for the example I have given this would work. However, in my actual expression I have other terms, say:

eq = 2*pi*cos(th)*sin(th)*diff(omega,th,th) + cos(th) - omega*sin(th)^2

but I only want to change the term cos(th)*sin(th), so I am looking for a more general solution, which can also be applied to other functions of sines and cosines.

keko gravatar imagekeko ( 2021-11-22 16:35:24 +0100 )edit

Then the problem is ill posed. You want some partial substitution but it's unclear how partial it should be. For example, do you want to substitute sin(th)*cos(th) (where the order of sin/cos is different) as well, do you want to substitute sin(2*th)/2 as well, or do you want to express any trigonometric function of th in terms of f? If it is just plain cos(th)*sin(th) in that fixed order and nothing in between of cos/sin, I'd convert eq to a string and perform substitution there.

Max Alekseyev gravatar imageMax Alekseyev ( 2021-11-22 16:56:50 +0100 )edit

I didn't think about sin(2*th)/2, so I am not worried about it now. What I want is to substitute cos(th)*sin(th) or sin(th)*cos(th), even if there is something in between, say sin(th)*diff(omega,th,th)*cos(th).

keko gravatar imagekeko ( 2021-11-22 17:13:08 +0100 )edit

You may want to translate the problem into algebraic language by introducing a variable, say s, for sin(th) and a variable, say c, for cos(th) and then reduce the resulting polynomial in s and c modulo polynomial s*c-f.

Max Alekseyev gravatar imageMax Alekseyev ( 2021-11-22 21:20:39 +0100 )edit

answered 2021-11-22 23:24:13 +0100

eric_g gravatar image


eq.subs({cos(th): f/sin(th)})

does the trick in the current case.

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


Asked: 2021-11-22 16:00:40 +0100

Seen: 73 times

Last updated: Nov 23