ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 15 Nov 2018 08:17:54 -0600What is the best way to return only real solutions?https://ask.sagemath.org/question/9307/what-is-the-best-way-to-return-only-real-solutions/I'm trying to write a function that filters out non-real solutions returned by solve. For now, I'm using
def select_real(xs):
return map(lambda eq: eq.lhs() == eq.rhs().real(), filter(lambda eq: not bool(eq.rhs().imag()), xs))
and it works for some simple cases like this
sage: select_real(solve(x^3+8==0, x))
[x == -2]
but this assumes multiple things, like fact that the solve retuns list in the form of actual solutions and imaginary part can be calculated. Generally it is only an ugly hack that I don't like.
I looked at assume(x, 'real') but found out that it does not work ( [Ticket #11941](http://trac.sagemath.org/sage_trac/ticket/11941) ). I also tried to do check using "in RR", but in above case all 3 solutions gave False. Also at first I tried using eq.full_simplify() in map step above, but it turns out that simplifying returns different root than using real/imag:
sage: ((-8)^(1/3)).full_simplify()
-2^(1/3)
sage: ((-8)^(1/3)).real()
1
After all that, I'm out of ideas. What is considered the best way to obtain only real solutions? Thanks in advance.
----------
edit:
I need such functionality because actually I'm preparing materials for high-schoolers who yet do not know about complex numbers, and shouldn't be introduced to them at that moment. My current version (above+extra full_simplify and uniq which are not really part of question) allows me to do for example this:
sage: var('x,a,b,c')
(x, a, b, c)
sage: assume(b^2-4*a*c>0)
sage: select_real(solve(a*x^2+b*x+c==0, x))
[x == -1/2*(b - sqrt(-4*a*c + b^2))/a, x == -1/2*(b + sqrt(-4*a*c + b^2))/a]
sage: forget(assumptions())
sage: assume(b^2-4*a*c==0)
sage: select_real(solve(a*x^2+b*x+c==0, x))
[x == -1/2*b/a]
sage: forget(assumptions())
sage: assume(b^2-4*a*c<0)
sage: select_real(solve(a*x^2+b*x+c==0, x))
[]giniuSun, 09 Sep 2012 09:23:39 -0500https://ask.sagemath.org/question/9307/solve() is not giving the right solutionshttps://ask.sagemath.org/question/44292/solve-is-not-giving-the-right-solutions/Hey there,
i need your help on this one. The following sage code is giving me a headache:
- f(x) = x^3 + x^2 - 0.1
- solve(f==0, x)
- -->
- [x == -1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(I*sqrt(3) + 1) - 1/18*(-I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3, x == -1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(-I*sqrt(3) + 1) - 1/18*(I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3, x == (1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) + 1/9/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3]
So i'm getting these 3 solutions, but they are not in R (real numbers), which they should be.
Im figuring there is some problem with really long terms or something? Is there an easy solution for this? Is the solve()-method here working as intended?
Regards,
BenbentheteacherThu, 15 Nov 2018 08:17:54 -0600https://ask.sagemath.org/question/44292/Using 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]]]TSchwennTue, 15 May 2012 19:25:20 -0500https://ask.sagemath.org/question/8973/Real Solution of x^3+8 == 0?https://ask.sagemath.org/question/8393/real-solution-of-x38-0/I do not understand the following:
sage: assume(x,'real')
sage: solve(x^3+8==0,x)
[]
Why does this equation have no solution?
But -2 is a solution!
Thanks for help!
amaleaTue, 18 Oct 2011 08:07:48 -0500https://ask.sagemath.org/question/8393/solve gives 1, 2, or 3 answers depending if one value in my equation is a real, rational, or integerhttps://ask.sagemath.org/question/7493/solve-gives-1-2-or-3-answers-depending-if-one-value-in-my-equation-is-a-real-rational-or-integer/Any idea why solve() is giving me different answers depending on what the value of 'g' is in my equations (see code below)? If g is a variable, an integer, or 1.0 then I get the two correct solutions (i.e. +&- a sqrt). If g is 1/4 or 1.1 I get an incomplete answer which I can get the two correct solutions by doing a solve on the solved solution. If g is a real whole number like 2,3 then I get 3 solutions: the two correct ones and a spurious one. I only discovered this behaviour when I tried `eqs=[0==f(x).diff(x)(x=0),0==f(x)(x=0)]; solve(eqs,T_1,x_0)` and sage hanged just calculating forever. Is there any general rules about how I should enter variables in equations for use with solve()? I want to know beforehand if something like `/4` will cause problems in which case I could use `v*` instead and later, after using solve(), substitute with `.subs(v=1/4)`?
reset()
forget()
var('T_1,T_2,x_0,h,d,g')
def f(x):
return B_0 + B_1 * (x - x_0) + B_2 * sqrt((x - x_0) ^ 2 + g*d)
B_0 = T_2 * (x_0 - h)
B_1 = (T_1 + T_2) / 2
B_2 = (T_2 - T_1) / 2
def check_solutions(gval):
print("g = {0}".format(gval))
eqs=[0==f(x).diff(x)(x=0,g=gval),0==f(x)(x=0,g=gval)]
print("******eqs:")
print(eqs)
sol1 = solve(eqs[0],T_1)
print("******Solution for T_1:")
print(sol1)
print("******sub T_1 solution into 2nd equation:")
print(eqs[1].subs(sol1[0]))
assume(h>0)
sol2 = solve(eqs[1].subs(sol1[0]),x_0)
print("******Solutions for x_0")
print(sol2)
print("******Solve again for x_0")
print([solve(sol2[i],x_0) for i in range(len(sol2))])
print("*****************************************")
gvals=[g,1,1.0,2.0,2,1/4,1.1]
for i in range(len(gvals)):
check_solutions(gvals[i])
running this I get:
g = g
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(d*g + x_0^2) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(d*g + x_0^2)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(d*g + x_0^2))*T_2/(x_0 + sqrt(d*g + x_0^2))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(d*g + x_0^2))*T_2/(x_0 + sqrt(d*g + x_0^2)) + T_2)*x_0 - 1/2*((x_0 - sqrt(d*g + x_0^2))*T_2/(x_0 + sqrt(d*g + x_0^2)) - T_2)*sqrt(d*g + x_0^2)
******Solutions for x_0
[
x_0 == -sqrt(-d*g + h^2),
x_0 == sqrt(-d*g + h^2)
]
******Solve again for x_0
[[x_0 == -sqrt(-d*g + h^2)], [x_0 == sqrt(-d*g + h^2)]]
*****************************************
g = 1
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) - T_2)*sqrt(x_0^2 + d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - d),
x_0 == sqrt(h^2 - d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - d)], [x_0 == sqrt(h^2 - d)]]
*****************************************
g = 1.00000000000000
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) - T_2)*sqrt(x_0^2 + d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - d),
x_0 == sqrt(h^2 - d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - d)], [x_0 == sqrt(h^2 - d)]]
*****************************************
g = 2.00000000000000
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 2.00000000000000*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 2.00000000000000*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) - T_2)*sqrt(x_0^2 + 2.00000000000000*d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - 2*d),
x_0 == sqrt(h^2 - 2*d),
x_0 == -sqrt(x_0^2 + 2*d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - 2*d)], [x_0 == sqrt(h^2 - 2*d)], [x_0 == -sqrt(x_0^2 + 2*d)]]
*****************************************
g = 2
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 2*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 2*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) - T_2)*sqrt(x_0^2 + 2*d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - 2*d),
x_0 == sqrt(h^2 - 2*d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - 2*d)], [x_0 == sqrt(h^2 - 2*d)]]
*****************************************
g = 1/4
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 1/4*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 1/4*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 1/4*d))*T_2/(x_0 + sqrt(x_0^2 + 1/4*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 1/4*d))*T_2/(x_0 + sqrt(x_0^2 + 1/4*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 1/4*d))*T_2/(x_0 + sqrt(x_0^2 + 1/4*d)) - T_2)*sqrt(x_0^2 + 1/4*d)
******Solutions for x_0
[
1/2*sqrt(4*x_0^2 + d) == h
]
******Solve again for x_0
[[x_0 == -1/2*sqrt(4*h^2 - d), x_0 == 1/2*sqrt(4*h^2 - d)]]
*****************************************
g = 1.10000000000000
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 1.10000000000000*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 1.10000000000000*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 11/10*d))*T_2/(x_0 + sqrt(x_0^2 + 11/10*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 11/10*d))*T_2/(x_0 + sqrt(x_0^2 + 11/10*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 11/10*d))*T_2/(x_0 + sqrt(x_0^2 + 11/10*d)) - T_2)*sqrt(x_0^2 + 1.10000000000000*d)
******Solutions for x_0
[
1/10*sqrt(10*x_0^2 + 11*d)*sqrt(10) == h
]
******Solve again for x_0
[[x_0 == -1/10*sqrt(10*h^2 - 11*d)*sqrt(10), x_0 == 1/10*sqrt(10*h^2 - 11*d)*sqrt(10)]]
*****************************************rtrwalkerFri, 25 Nov 2011 13:10:09 -0600https://ask.sagemath.org/question/7493/Problem with solvehttps://ask.sagemath.org/question/8414/problem-with-solve/Hi, I'm very very new in Sage. I have a problem trying this:
x=var('x')
f=10*(1-exp(-0.23*(x)))^2
g=20*(1-exp(0.23*(x-14)))^2-10
I'm want to get the intersection point(s). From Maple, I know there is three, but when I try to solve for x:
solve(f==g,x)
[ ]
Sage gives me that [ ]! What does it mean?
Thanks in advance
saradocbrandiThu, 27 Oct 2011 16:19:28 -0500https://ask.sagemath.org/question/8414/