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.Mon, 25 Sep 2023 01:40:40 +0200Real and imaginary part of an expression with assumptions on variableshttps://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/ Hello,
I have this expression:
Exp[-(x+I*y)*sinh(t/(1-t)) - nu * t/(1-t)] * 1/(1-t)
All variables are real except nu which is a complex number. I want the real part and the imaginary part of this expression. I tried with Wolfram Alpha and either it assumes all variables are complex, or, of I specify some Assumptions, it does not return the result. Is it possible with Sage ? I'm very new to Sage. I'm new to GIAC too and I didn't found how to force a variable to be real.
Wed, 20 Sep 2023 17:45:17 +0200https://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/Answer by eric_g for <p>Hello,</p>
<p>I have this expression:</p>
<pre><code>Exp[-(x+I*y)*sinh(t/(1-t)) - nu * t/(1-t)] * 1/(1-t)
</code></pre>
<p>All variables are real except nu which is a complex number. I want the real part and the imaginary part of this expression. I tried with Wolfram Alpha and either it assumes all variables are complex, or, of I specify some Assumptions, it does not return the result. Is it possible with Sage ? I'm very new to Sage. I'm new to GIAC too and I didn't found how to force a variable to be real.</p>
https://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/?answer=73520#post-id-73520In Sage, you can specify the domains of the variables when you declare them. Your example becomes then
sage: x, y, t = var('x y t', domain='real')
sage: nu = var('nu', domain='complex')
sage: assumptions()
[x is real, y is real, t is real, nu is complex]
sage: s = exp(-(x+I*y)*sinh(t/(1-t)) - nu * t/(1-t)) * 1/(1-t)
sage: s.real_part()
-cos(-y*sinh(-t/(t - 1)) + t*imag_part(nu)/(t - 1))*e^(-x*sinh(-t/(t - 1)) + t*real_part(nu)/(t - 1))/(t - 1)
sage: s.imag_part()
-e^(-x*sinh(-t/(t - 1)) + t*real_part(nu)/(t - 1))*sin(-y*sinh(-t/(t - 1)) + t*imag_part(nu)/(t - 1))/(t - 1)
Wed, 20 Sep 2023 22:39:40 +0200https://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/?answer=73520#post-id-73520Comment by stla for <p>In Sage, you can specify the domains of the variables when you declare them. Your example becomes then</p>
<pre><code>sage: x, y, t = var('x y t', domain='real')
sage: nu = var('nu', domain='complex')
sage: assumptions()
[x is real, y is real, t is real, nu is complex]
sage: s = exp(-(x+I*y)*sinh(t/(1-t)) - nu * t/(1-t)) * 1/(1-t)
sage: s.real_part()
-cos(-y*sinh(-t/(t - 1)) + t*imag_part(nu)/(t - 1))*e^(-x*sinh(-t/(t - 1)) + t*real_part(nu)/(t - 1))/(t - 1)
sage: s.imag_part()
-e^(-x*sinh(-t/(t - 1)) + t*real_part(nu)/(t - 1))*sin(-y*sinh(-t/(t - 1)) + t*imag_part(nu)/(t - 1))/(t - 1)
</code></pre>
https://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/?comment=73581#post-id-73581Thanks. I found how to do with GIAC. It assumes all variables are real, and you can use `i` for the imaginary unit. So it suffices to write `nu = a + i*b` and to apply the function `re` and `im`.Mon, 25 Sep 2023 01:40:40 +0200https://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/?comment=73581#post-id-73581Comment by Emmanuel Charpentier for <p>In Sage, you can specify the domains of the variables when you declare them. Your example becomes then</p>
<pre><code>sage: x, y, t = var('x y t', domain='real')
sage: nu = var('nu', domain='complex')
sage: assumptions()
[x is real, y is real, t is real, nu is complex]
sage: s = exp(-(x+I*y)*sinh(t/(1-t)) - nu * t/(1-t)) * 1/(1-t)
sage: s.real_part()
-cos(-y*sinh(-t/(t - 1)) + t*imag_part(nu)/(t - 1))*e^(-x*sinh(-t/(t - 1)) + t*real_part(nu)/(t - 1))/(t - 1)
sage: s.imag_part()
-e^(-x*sinh(-t/(t - 1)) + t*real_part(nu)/(t - 1))*sin(-y*sinh(-t/(t - 1)) + t*imag_part(nu)/(t - 1))/(t - 1)
</code></pre>
https://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/?comment=73527#post-id-73527You can also *temporarily* add assumptions to the current global ones ; see `assuming?`. In your case :
sage: x, y, t, nu = var('x y t nu') # No assumptions
sage: s = exp(-(x+I*y)*sinh(t/(1-t)) - nu * t/(1-t)) * 1/(1-t)
sage: s.real_part().simplify() # Without assumptions
-t*cos(y*sinh(t^2/(t^2 - 2*t + 1) - t/(t^2 - 2*t + 1)))*e^(nu*t^2/(t^2 - 2*t + 1) + x*sinh(t^2/(t^2 - 2*t + 1) - t/(t^2 - 2*t + 1)) - nu*t/(t^2 - 2*t + 1))/(t^2 - 2*t + 1) + cos(y*sinh(t^2/(t^2 - 2*t + 1) - t/(t^2 - 2*t + 1)))*e^(nu*t^2/(t^2 - 2*t + 1) + x*sinh(t^2/(t^2 - 2*t + 1) - t/(t^2 - 2*t + 1)) - nu*t/(t^2 - 2*t + 1))/(t^2 - 2*t + 1)
sage: with assuming(x, y, t, "real"): s.real_part().simplify() # Temporary assumptions
-cos(y*sinh(t/(t - 1)))*e^(x*sinh(t/(t - 1)) + nu*t/(t - 1))/(t - 1)Thu, 21 Sep 2023 07:41:58 +0200https://ask.sagemath.org/question/73517/real-and-imaginary-part-of-an-expression-with-assumptions-on-variables/?comment=73527#post-id-73527