ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 07 Jun 2016 21:35:16 -0500Using solve() to find positive real solutions to a complex equationhttps://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/I need `solve()` to return only positive real solutions to a symbolic complex equation, but it fails to find a useful solution:
sage: var('x, y', domain='positive')
sage: z = 1/(i*x + 1/(i*y + 1))
sage: Equation = z == 2
sage: solve(Equation, x, y)
([x == (-I*y + 1)/(2*y - 2*I)], [1])
I want solutions in the form `[x == foo, y == bar]`. Splitting Equation into real and imaginary components does the trick:
sage: Equation = [z.real() == 2, z.imag() == 0]
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
But why is that reformulation necessary to produce useful results? Isn't it equivalent to the original Equation?
Also, why does `solve()` ignore my variables' `domain='positive'` clause? Solve also ignores positive x and y assumptions:
sage: assume(x>0)
sage: assume(y>0)
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
And it bugs out when positive x & y clauses are added to Equation:
sage: Equation.append(x>0, y>0)
sage: solve([Equation, x>0], x, y)
[[0 < x, [1/((y^2 + 1)*(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2)) == 2, -(x - y/(y^2 + 1))/(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2) == 0, x > 0, y > 0]]]Tue, 15 May 2012 19:25:20 -0500https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/Comment by kcrisman for <p>I need <code>solve()</code> to return only positive real solutions to a symbolic complex equation, but it fails to find a useful solution:</p>
<pre><code>sage: var('x, y', domain='positive')
sage: z = 1/(i*x + 1/(i*y + 1))
sage: Equation = z == 2
sage: solve(Equation, x, y)
([x == (-I*y + 1)/(2*y - 2*I)], [1])
</code></pre>
<p>I want solutions in the form <code>[x == foo, y == bar]</code>. Splitting Equation into real and imaginary components does the trick:</p>
<pre><code>sage: Equation = [z.real() == 2, z.imag() == 0]
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>But why is that reformulation necessary to produce useful results? Isn't it equivalent to the original Equation? </p>
<p>Also, why does <code>solve()</code> ignore my variables' <code>domain='positive'</code> clause? Solve also ignores positive x and y assumptions:</p>
<pre><code>sage: assume(x>0)
sage: assume(y>0)
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>And it bugs out when positive x & y clauses are added to Equation:</p>
<pre><code>sage: Equation.append(x>0, y>0)
sage: solve([Equation, x>0], x, y)
[[0 < x, [1/((y^2 + 1)*(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2)) == 2, -(x - y/(y^2 + 1))/(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2) == 0, x > 0, y > 0]]]
</code></pre>
https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19767#post-id-19767So like when you do the integral from 0 to x of t^2 dt, it didn't really matter what the name of the variable t was, and one shouldn't take any outside info into account because it is just a dummy because I was lazy. I could even do the integral from 0 to x of x^2 dx, though we tell our students not to because of the ambiguity. Similarly, Maxima treats variables in "solve" that way. It's not really a big deal, but at least makes it more clear why it's not a big priority for them to change the behavior. We would like to do more of it in Sage, but there are only limited resources of people who know enough about how Maxima and Sage work in this area, and they are more focused on extending our capabilities to more symbolic functions or fixing other types of bugs.Fri, 18 May 2012 07:18:39 -0500https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19767#post-id-19767Comment by TSchwenn for <p>I need <code>solve()</code> to return only positive real solutions to a symbolic complex equation, but it fails to find a useful solution:</p>
<pre><code>sage: var('x, y', domain='positive')
sage: z = 1/(i*x + 1/(i*y + 1))
sage: Equation = z == 2
sage: solve(Equation, x, y)
([x == (-I*y + 1)/(2*y - 2*I)], [1])
</code></pre>
<p>I want solutions in the form <code>[x == foo, y == bar]</code>. Splitting Equation into real and imaginary components does the trick:</p>
<pre><code>sage: Equation = [z.real() == 2, z.imag() == 0]
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>But why is that reformulation necessary to produce useful results? Isn't it equivalent to the original Equation? </p>
<p>Also, why does <code>solve()</code> ignore my variables' <code>domain='positive'</code> clause? Solve also ignores positive x and y assumptions:</p>
<pre><code>sage: assume(x>0)
sage: assume(y>0)
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>And it bugs out when positive x & y clauses are added to Equation:</p>
<pre><code>sage: Equation.append(x>0, y>0)
sage: solve([Equation, x>0], x, y)
[[0 < x, [1/((y^2 + 1)*(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2)) == 2, -(x - y/(y^2 + 1))/(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2) == 0, x > 0, y > 0]]]
</code></pre>
https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19769#post-id-19769Please clarify the "should not do this" part. What's the _this_?Thu, 17 May 2012 18:59:38 -0500https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19769#post-id-19769Comment by kcrisman for <p>I need <code>solve()</code> to return only positive real solutions to a symbolic complex equation, but it fails to find a useful solution:</p>
<pre><code>sage: var('x, y', domain='positive')
sage: z = 1/(i*x + 1/(i*y + 1))
sage: Equation = z == 2
sage: solve(Equation, x, y)
([x == (-I*y + 1)/(2*y - 2*I)], [1])
</code></pre>
<p>I want solutions in the form <code>[x == foo, y == bar]</code>. Splitting Equation into real and imaginary components does the trick:</p>
<pre><code>sage: Equation = [z.real() == 2, z.imag() == 0]
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>But why is that reformulation necessary to produce useful results? Isn't it equivalent to the original Equation? </p>
<p>Also, why does <code>solve()</code> ignore my variables' <code>domain='positive'</code> clause? Solve also ignores positive x and y assumptions:</p>
<pre><code>sage: assume(x>0)
sage: assume(y>0)
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>And it bugs out when positive x & y clauses are added to Equation:</p>
<pre><code>sage: Equation.append(x>0, y>0)
sage: solve([Equation, x>0], x, y)
[[0 < x, [1/((y^2 + 1)*(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2)) == 2, -(x - y/(y^2 + 1))/(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2) == 0, x > 0, y > 0]]]
</code></pre>
https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19416#post-id-19416You could postprocess your list of solutions by trying to do `float(x)` for each of them. If that raised an error, then I guess it wasn't real! We do this for the `exclude` option for plotting, for instance.Thu, 12 Jul 2012 14:26:38 -0500https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19416#post-id-19416Comment by Cosmos for <p>I need <code>solve()</code> to return only positive real solutions to a symbolic complex equation, but it fails to find a useful solution:</p>
<pre><code>sage: var('x, y', domain='positive')
sage: z = 1/(i*x + 1/(i*y + 1))
sage: Equation = z == 2
sage: solve(Equation, x, y)
([x == (-I*y + 1)/(2*y - 2*I)], [1])
</code></pre>
<p>I want solutions in the form <code>[x == foo, y == bar]</code>. Splitting Equation into real and imaginary components does the trick:</p>
<pre><code>sage: Equation = [z.real() == 2, z.imag() == 0]
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>But why is that reformulation necessary to produce useful results? Isn't it equivalent to the original Equation? </p>
<p>Also, why does <code>solve()</code> ignore my variables' <code>domain='positive'</code> clause? Solve also ignores positive x and y assumptions:</p>
<pre><code>sage: assume(x>0)
sage: assume(y>0)
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>And it bugs out when positive x & y clauses are added to Equation:</p>
<pre><code>sage: Equation.append(x>0, y>0)
sage: solve([Equation, x>0], x, y)
[[0 < x, [1/((y^2 + 1)*(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2)) == 2, -(x - y/(y^2 + 1))/(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2) == 0, x > 0, y > 0]]]
</code></pre>
https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19433#post-id-19433Is there any kind of workaround to at least remove automatically the complex solutions from the solutions so the don't mix with real roots? For example i was trying to make a list of real solutions of an equation and half of the list finally came out to be complex numbers.Sat, 07 Jul 2012 11:06:57 -0500https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19433#post-id-19433Comment by kcrisman for <p>I need <code>solve()</code> to return only positive real solutions to a symbolic complex equation, but it fails to find a useful solution:</p>
<pre><code>sage: var('x, y', domain='positive')
sage: z = 1/(i*x + 1/(i*y + 1))
sage: Equation = z == 2
sage: solve(Equation, x, y)
([x == (-I*y + 1)/(2*y - 2*I)], [1])
</code></pre>
<p>I want solutions in the form <code>[x == foo, y == bar]</code>. Splitting Equation into real and imaginary components does the trick:</p>
<pre><code>sage: Equation = [z.real() == 2, z.imag() == 0]
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>But why is that reformulation necessary to produce useful results? Isn't it equivalent to the original Equation? </p>
<p>Also, why does <code>solve()</code> ignore my variables' <code>domain='positive'</code> clause? Solve also ignores positive x and y assumptions:</p>
<pre><code>sage: assume(x>0)
sage: assume(y>0)
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>And it bugs out when positive x & y clauses are added to Equation:</p>
<pre><code>sage: Equation.append(x>0, y>0)
sage: solve([Equation, x>0], x, y)
[[0 < x, [1/((y^2 + 1)*(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2)) == 2, -(x - y/(y^2 + 1))/(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2) == 0, x > 0, y > 0]]]
</code></pre>
https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19770#post-id-19770To address one of your questions, but not the main one: Maxima's solve, as is probably mentioned elsewhere on this site, explicitly does not take assumptions into account. The idea is that the variables in solve are like dummy variables in integration, and so should not do this. In Sage we try very weakly to take this into account, but it's a process that has a way to go.Thu, 17 May 2012 17:16:14 -0500https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?comment=19770#post-id-19770Answer by paulmasson for <p>I need <code>solve()</code> to return only positive real solutions to a symbolic complex equation, but it fails to find a useful solution:</p>
<pre><code>sage: var('x, y', domain='positive')
sage: z = 1/(i*x + 1/(i*y + 1))
sage: Equation = z == 2
sage: solve(Equation, x, y)
([x == (-I*y + 1)/(2*y - 2*I)], [1])
</code></pre>
<p>I want solutions in the form <code>[x == foo, y == bar]</code>. Splitting Equation into real and imaginary components does the trick:</p>
<pre><code>sage: Equation = [z.real() == 2, z.imag() == 0]
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>But why is that reformulation necessary to produce useful results? Isn't it equivalent to the original Equation? </p>
<p>Also, why does <code>solve()</code> ignore my variables' <code>domain='positive'</code> clause? Solve also ignores positive x and y assumptions:</p>
<pre><code>sage: assume(x>0)
sage: assume(y>0)
sage: solve(Equation, x, y)
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
</code></pre>
<p>And it bugs out when positive x & y clauses are added to Equation:</p>
<pre><code>sage: Equation.append(x>0, y>0)
sage: solve([Equation, x>0], x, y)
[[0 < x, [1/((y^2 + 1)*(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2)) == 2, -(x - y/(y^2 + 1))/(1/(y^2 + 1)^2 + (x - y/(y^2 + 1))^2) == 0, x > 0, y > 0]]]
</code></pre>
https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?answer=33691#post-id-33691Here's a way to see why the explicit split into real and imaginary parts is necessary. If the variables are given to `solve` as a list,
var('x, y')
z = 1/(i*x + 1/(i*y + 1))
Equation = z == 2
solve(Equation, [x, y])
[[x == (-I*r1 + 1)/(2*r1 - 2*I), y == r1]]
then it's clear that `solve` is returning a fully complex answer with a dummy variable `r1`. With references to this dummy variable and the result of solve, one can then find solutions that make the imaginary part of `x` zero:
var('x, y, r1')
z = 1/(i*x + 1/(i*y + 1))
Equation = z == 2
f = solve(Equation, [x, y])
solve( imaginary(f[0][0].rhs()), r1 )
[r1 == -1, r1 == 1]
With an additional reference to this second result, the desired solution form can be achieved with nested lists:
var('x, y, r1')
z = 1/(i*x + 1/(i*y + 1))
Equation = z == 2
f = solve(Equation, [x, y])
g = solve( imaginary(f[0][0].rhs()), r1 )
[[f[0][i].subs(g[j]) for i in (0,1)] for j in (0,1)]
[[x == (-1/2), y == -1], [x == (1/2), y == 1]]
Here's a [link](http://sagecell.sagemath.org/?z=eJxFjcEOgjAQRO8k_Ye5sdUGqXeOfEXTQ00ElyjEVgj49S4QNbuHmZ2X2SlEymeDxSDaXKvsjQr2RHyYcdzFsgotUf0cw4uHXgihKpxV1ohOw3260jc0cGudF779heBHaLkPcaHGlV62iLdEWq9fIahz2519kcZLotZ1XqMZIhjcg0pjtd989_cq2-cDOO80eA==&lang=sage) to a live example.Tue, 07 Jun 2016 21:35:16 -0500https://ask.sagemath.org/question/8973/using-solve-to-find-positive-real-solutions-to-a-complex-equation/?answer=33691#post-id-33691