ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 23 Nov 2021 08:45:02 +0100Substitute multiplication of sine and cosine for a symbolic functionhttps://ask.sagemath.org/question/59881/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](https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/) 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.Mon, 22 Nov 2021 16:00:40 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/Answer by Emmanuel Charpentier for <p>I have one variable and two functions:</p>
<pre><code>th = var('th')
f = function('f')(th)
omega = function('omega')(th)
</code></pre>
<p>and the following equation (this is an example, my actual equation has more terms such as +cos(th)+sin(th)):</p>
<pre><code>eq = 2*pi*cos(th)*sin(th)*diff(omega,th,th)
</code></pre>
<p>Now, I want cos(th)*sin(th) to be substituted for f. I have tried the following:</p>
<pre><code>eq = eq.substitute(cos(th)*sin(th)==f)
</code></pre>
<p>and also,</p>
<pre><code>expression = cos(th)*sin(th)
eq = eq.substitute_function(expression,f)
</code></pre>
<p>but when I print eq, I still get:</p>
<blockquote>
<p>2 * pi * cos(th) * sin(th) * diff(omega(th), th)</p>
</blockquote>
<p>How can I do it? It should also work for different permutations of the individual components of the equation. For example:</p>
<pre><code>eq = 2*pi*sin(th)*diff(omega,th,th)*cos(th)
</code></pre>
<p>I have found that a <a href="https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/">similar question</a> 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</p>
<pre><code>eq = maxima.fullratsubst(f,cos(th)*sin(th),eq)
</code></pre>
<p>directly on SageMath, but I guess I am doing something wrong since it does not work.</p>
https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?answer=59901#post-id-59901FWIW :
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, Tue, 23 Nov 2021 08:45:02 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?answer=59901#post-id-59901Answer by eric_g for <p>I have one variable and two functions:</p>
<pre><code>th = var('th')
f = function('f')(th)
omega = function('omega')(th)
</code></pre>
<p>and the following equation (this is an example, my actual equation has more terms such as +cos(th)+sin(th)):</p>
<pre><code>eq = 2*pi*cos(th)*sin(th)*diff(omega,th,th)
</code></pre>
<p>Now, I want cos(th)*sin(th) to be substituted for f. I have tried the following:</p>
<pre><code>eq = eq.substitute(cos(th)*sin(th)==f)
</code></pre>
<p>and also,</p>
<pre><code>expression = cos(th)*sin(th)
eq = eq.substitute_function(expression,f)
</code></pre>
<p>but when I print eq, I still get:</p>
<blockquote>
<p>2 * pi * cos(th) * sin(th) * diff(omega(th), th)</p>
</blockquote>
<p>How can I do it? It should also work for different permutations of the individual components of the equation. For example:</p>
<pre><code>eq = 2*pi*sin(th)*diff(omega,th,th)*cos(th)
</code></pre>
<p>I have found that a <a href="https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/">similar question</a> 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</p>
<pre><code>eq = maxima.fullratsubst(f,cos(th)*sin(th),eq)
</code></pre>
<p>directly on SageMath, but I guess I am doing something wrong since it does not work.</p>
https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?answer=59894#post-id-59894Well,
eq.subs({cos(th): f/sin(th)})
does the trick in the current case. Mon, 22 Nov 2021 23:24:13 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?answer=59894#post-id-59894Answer by Max Alekseyev for <p>I have one variable and two functions:</p>
<pre><code>th = var('th')
f = function('f')(th)
omega = function('omega')(th)
</code></pre>
<p>and the following equation (this is an example, my actual equation has more terms such as +cos(th)+sin(th)):</p>
<pre><code>eq = 2*pi*cos(th)*sin(th)*diff(omega,th,th)
</code></pre>
<p>Now, I want cos(th)*sin(th) to be substituted for f. I have tried the following:</p>
<pre><code>eq = eq.substitute(cos(th)*sin(th)==f)
</code></pre>
<p>and also,</p>
<pre><code>expression = cos(th)*sin(th)
eq = eq.substitute_function(expression,f)
</code></pre>
<p>but when I print eq, I still get:</p>
<blockquote>
<p>2 * pi * cos(th) * sin(th) * diff(omega(th), th)</p>
</blockquote>
<p>How can I do it? It should also work for different permutations of the individual components of the equation. For example:</p>
<pre><code>eq = 2*pi*sin(th)*diff(omega,th,th)*cos(th)
</code></pre>
<p>I have found that a <a href="https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/">similar question</a> 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</p>
<pre><code>eq = maxima.fullratsubst(f,cos(th)*sin(th),eq)
</code></pre>
<p>directly on SageMath, but I guess I am doing something wrong since it does not work.</p>
https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?answer=59882#post-id-59882`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.Mon, 22 Nov 2021 16:27:44 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?answer=59882#post-id-59882Comment by Max Alekseyev for <p><code>cos(th)*sin(th)==f</code> is equivalent to <code>th==arcsin(2*f)/2</code>, and so substitution <code>eq.subs({th:arcsin(2*f)/2})</code> should do the job.</p>
https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59893#post-id-59893You 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`.Mon, 22 Nov 2021 21:20:39 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59893#post-id-59893Comment by keko for <p><code>cos(th)*sin(th)==f</code> is equivalent to <code>th==arcsin(2*f)/2</code>, and so substitution <code>eq.subs({th:arcsin(2*f)/2})</code> should do the job.</p>
https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59886#post-id-59886I 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)`.Mon, 22 Nov 2021 17:13:08 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59886#post-id-59886Comment by keko for <p><code>cos(th)*sin(th)==f</code> is equivalent to <code>th==arcsin(2*f)/2</code>, and so substitution <code>eq.subs({th:arcsin(2*f)/2})</code> should do the job.</p>
https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59883#post-id-59883Yes, 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.Mon, 22 Nov 2021 16:35:24 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59883#post-id-59883Comment by Max Alekseyev for <p><code>cos(th)*sin(th)==f</code> is equivalent to <code>th==arcsin(2*f)/2</code>, and so substitution <code>eq.subs({th:arcsin(2*f)/2})</code> should do the job.</p>
https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59885#post-id-59885Then 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.Mon, 22 Nov 2021 16:56:50 +0100https://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/?comment=59885#post-id-59885