Substitute multiplication of sine and cosine for a symbolic function

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 close merge delete

Sort by » oldest newest most voted

FWIW :

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)


HTH,

more

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.

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.

( 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.

( 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).

( 2021-11-22 17:13:08 +0100 )edit
2

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.

( 2021-11-22 21:20:39 +0100 )edit

Well,

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


does the trick in the current case.

more