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.Fri, 20 Dec 2019 19:48:54 +0100What 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 16:23:39 +0200https://ask.sagemath.org/question/9307/Hi, I have a fifth order polynomial in x whose coefficients depend on a certain variable k. Now, I want to find the real roots of that polynomial for each value of k.https://ask.sagemath.org/question/49103/hi-i-have-a-fifth-order-polynomial-in-x-whose-coefficients-depend-on-a-certain-variable-k-now-i-want-to-find-the-real-roots-of-that-polynomial-for/ Hi, I have a fifth order polynomial in x whose coefficients depend on a certain variable k. Now, I want to find the real roots of that polynomial for each value of k, say from 0.1 to 2 in steps of 0.05 and after finding the roots, I need to plot the corresponding root versus the k from which it was obtained. How can I do that? If anyone could help, that would be highly appreciable.
I have already tried to find the roots for each value of k and then got a set of roots. Then I chose the real root. I repeated this process 20 times to get 20 different roots. But this process is very lengthy. Suppose the value of one of the coefficients is changed, then I need to do the thing all over again. So i need to have a single program, where I can include all of that.SAGARFri, 20 Dec 2019 19:48:54 +0100https://ask.sagemath.org/question/49103/Intersection of polynomial Ideals over $\mathbb{R}$https://ask.sagemath.org/question/46258/intersection-of-polynomial-ideals-over-mathbbr/I am trying to compute the intersection of Ideals over $\mathbb{R}[x,y]$, but I get problems from the coefficient $\frac{1}{\sqrt{2}}$. This is my code:
R.<x,y>=PolynomialRing(RR,order='lex')
I=Ideal([(x^2+y^2-1),(x*y),(y^3-y)])
I5=Ideal([x-1/sqrt(2),y-1/sqrt(2)])
I6=Ideal([x+1/sqrt(2),y-1/sqrt(2)])
I7=Ideal([x+1/sqrt(2),y+1/sqrt(2)])
I8=Ideal([x-1/sqrt(2),y+1/sqrt(2)])
J=I.intersection(I5,I6,I7,I8)
and this is the error I get:
TypeError: Intersection is only available for ideals of the same ring.
So when I ask if
I5 in R
the answer is False. I also tried with QQbar but same result, can someone explain this?
Thanks!
EDIT: I also tried with $\frac{\sqrt{2}}{2}$ instead of $\frac{1}{\sqrt{2}}$ and I get the same error.LeghThu, 18 Apr 2019 17:29:09 +0200https://ask.sagemath.org/question/46258/Convert real matrix into Integer matrixhttps://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/Suppose I have a real matrix with integer entries
A = matrix(RR,[[1,2,3],[4,5,6]])
I would like to convert it to an Integer matrix (ZZ). I can do it element by element but is there a function that will do this for the entire matrix. Doing B=ZZ(A) will return an error message
unable to coerce <type 'sage.matrix.matrix_generic_dense.Matrix_generic_dense'> to an integer.
raykanSun, 27 Jan 2019 16:34:47 +0100https://ask.sagemath.org/question/45203/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 15:17:54 +0100https://ask.sagemath.org/question/44292/Is it possible to change Polynomial Ring in the middle of a computation?https://ask.sagemath.org/question/42341/is-it-possible-to-change-polynomial-ring-in-the-middle-of-a-computation/ Hi, I'm trying to invert the "Pollaczek-Khinchine" Laplace transform when it is rational
This works for me at degree 2:
var('x,s')
Fx = (1/6*exp(-2*x)+5/6*exp(-6*x));rho=2/3
print('Hyperexponential claims:',Fx)
R.<s> = PolynomialRing(QQbar)#when all coefficients are not integer, use CC
FF = R.fraction_field()
L_F=laplace(Fx,x,s)#Laplace transform of F
#Compute Pollackek-Khinchine (PK) formula L_rui for the Laplace transform (LT) of ruin probability
m1=L_F(s=0)
fe=L_F/m1
Fe=(1-fe)/s
L_rui=rho*Fe/(1-rho*fe)
show(L_rui.simplify_full())
inverse_laplace(SR(L_rui),s,u)
but not at degree 3, since I do not know how to use partial_fraction_decomposition, and then to switch to RR numbers and then invert .
If I start in R.<s> = PolynomialRing(RR), for an already known LT, everything is fine. But, a certain simplification by s in PK formula will become impossible due to rounding errors, so I am forced to start with R.<s> = PolynomialRing(QQbar)
After obtaining the partial_fraction_decomposition, I must apply RR to all numbers , but I do not manage to do it. Without that conversion, inverse_laplace won't workflorinMon, 14 May 2018 19:03:38 +0200https://ask.sagemath.org/question/42341/inverse_laplace of a fraction whose denominator has real roots (or complex)https://ask.sagemath.org/question/42331/inverse_laplace-of-a-fraction-whose-denominator-has-real-roots-or-complex/ Hi, this is the simplest and very important method of Laplace inversion . It can be applied to any Laplace transform
(LT), by starting with a Pade approximation of the LT, then partial fractions and inversion. For some reason, a program I had written last year stopped working
def Ruin(Fx, rho, x, u):#assumes rational survival function Fx, i.e. a hyperexponential density
var('s')
L_F=laplace(Fx,x,s)
m1=L_F(s=0) #some algebraic manipulations known as Pollaczek-Khinchine formula
fe=L_F/m1
Fe=factor((1-fe)/s)
L_rui=rho*Fe/(1-rho*fe)
C=ComplexField(53);
dec=Frac(C['s'])(L_rui).partial_fraction_decomposition();
n=len(dec)
par=[inverse_laplace(dec[1][i],s,u) for i in [0..n]];
rui=sum(par)
return rui, L_rui, fe
It's pretty easy to repair the program when roots are rational -- see question "partial_fraction_decomposition" with possibly "complex roots", again. For nonrational roots, I proposed there the following test case
var('s,u')
R.<s> = PolynomialRing(QQbar)
F = R.fraction_field()
L=3/4*(19*s^2 + 156*s + 284)/(19*s^3 + 174*s^2 + 422*s + 228)
whole,LL=L.partial_fraction_decomposition()
show(LL[0])
inverse_laplace(LL[0],s,u)
Thanks in advance :)florinMon, 14 May 2018 11:04:41 +0200https://ask.sagemath.org/question/42331/Why is sagemath real numbers arithmetic gives incorrect result?https://ask.sagemath.org/question/39824/why-is-sagemath-real-numbers-arithmetic-gives-incorrect-result/ I was using Sagemath for computation.
There I was input "152.8-152.43" and gets something like "0.370000000000005". I tried to use RLF and RR too but I could not eliminate the extra number "5" at the end of the result. This raises no problem, since I do not that level of precision, but the number "5" at the end of the result just drives me upset. Why is it so? Is there anyway I can do to eliminate this problem? Matlab gave me the right answer. Is this some kind of weakness of Sagemath? I love Sagemath and intend to stick to it lifelong. But did I go wrong somewhere?michaeldnguyen10Tue, 28 Nov 2017 08:24:06 +0100https://ask.sagemath.org/question/39824/assume(x, 'real') returns non-real values when solvinghttps://ask.sagemath.org/question/39283/assumex-real-returns-non-real-values-when-solving/ %sage
%var x, y
# our function
f(x,y) = x*y + 8/x + 1/y
# first order partial derivatives
fx(x,y) = diff(f(x,y), x)
fy(x,y) = diff(f(x,y), y)
# find where both are simultaneously equal to zero.
# the syntax is solve([system, of, equations, to, solve], variables, to, solve, for)
# To keep from finding any complex number solutions, we tell sage to assume that x and y are real numbers
assume(x, 'real')
assume(y, 'real')
solutions = solve([fx(x,y) == 0, fy(x,y) == 0], x, y)
solutions
returns
[[x == 4, y == (1/2)], [x == -2*I*sqrt(3) - 2, y == -1/4*I*sqrt(3) - 1/4], [x == 2*I*sqrt(3) - 2, y == 1/4*I*sqrt(3) - 1/4]]
Why is it giving non-real values?RealmicpandaThu, 26 Oct 2017 00:29:08 +0200https://ask.sagemath.org/question/39283/a problem with variables in real domainshttps://ask.sagemath.org/question/37766/a-problem-with-variables-in-real-domains/Hi,
I have a problem with setting the domains of definition of variables.
For example, typing
var("y") ; assume(y, "real")
conjugate(y + I)
I get the result `y + I`. The same with `var("y", domain="real")`.
Where I'm wrong?
thanks!danieleFri, 02 Jun 2017 19:12:44 +0200https://ask.sagemath.org/question/37766/Difference between RealNumber and RealLiteralhttps://ask.sagemath.org/question/36798/difference-between-realnumber-and-realliteral/I discovered this tonight.
sage: L = srange(-1,1,.01)
sage: type(L[50])
<type 'sage.rings.real_mpfr.RealNumber'>
sage: type(-.5)
<type 'sage.rings.real_mpfr.RealLiteral'>
and hence
sage: -.5 in L
False
In some sense I'm cool with that, but in another sense I'm very annoyed. (Especially since I probably should have known about it but missed this.) So ... what exactly is the difference between these types, and when should they each be used? You might see my confusion given this result:
sage: L[50] in RR
True
sage: -.5 in RR
True
Oh, and this is really awesome:
sage: type(RealNumber(-.5))
<type 'sage.rings.real_mpfr.RealLiteral'>
Also, perhaps if anyone ever makes the full list of real numbers types as in [Question 9950](https://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/), one could add these.kcrismanFri, 03 Mar 2017 03:21:57 +0100https://ask.sagemath.org/question/36798/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]]]TSchwennWed, 16 May 2012 02:25:20 +0200https://ask.sagemath.org/question/8973/Unable to simplify function outputhttps://ask.sagemath.org/question/30155/unable-to-simplify-function-output/ I am trying to make a tool that creates a large number of rectangular boxes approximating a 3 dimensional graph.
However when I enter in my function value as box height Sage says it is unable to simplify my height to a float approximation. I also do not what to display graph when the bounds are not real numbers. Does anyone know the solutions to my two problems.
if lower_x_bound in RR or upper_x_bound in RR or lower_y_bound in RR or lower_y_bound in RR:
#check if bounds are are real before graphing because I do nto know how to graph with x as a bound.
showGraph = True
html("Can only grah numerical bounds")
else:
showGraph = False
if showGraph == True :
graph = plot3d(function,(x,lower_x_bound,upper_x_bound),(y,lower_y_bound,upper_y_bound),fill=True,color = "orange",spin = 4)
html("<h3>Graph of Integrated Region</h3>")
delta = upper_x_bound - lower_x_bound
#func= function(x = lower_x_bound+(i-1)*delta/numrecs , y = .2)
#show(func)
B = sum([Box([1/numrecs,.5,function(x = lower_x_bound+(i-1)*delta/numrecs , y = .2)], color="orange").translate((lower_x_bound+delta*i/numrecs,0,function(lower_x_bound+i*delta/numrecs)/2)) for i in [0..numrecs]])
#makes a whole bunch of rectangels which approximate graph of function being integrated
show(graph+B)
This is the error I get when it tries to make the box heights.
Error in lines 1-1
Traceback (most recent call last):
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_server.py", line 881, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_server.py", line 1031, in _execute_interact
sage_salvus.interacts[id](vals)
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_salvus.py", line 216, in __call__
self._f(**dict([(k,self._last_vals[k]) for k in self._args]))
File "", line 26, in interplay
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_salvus.py", line 2429, in show
s = show0(objs, combine_all=True)
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_salvus.py", line 2414, in show0
b = show0(a)
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_salvus.py", line 2402, in show0
salvus.threed(obj, **kwds)
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_server.py", line 657, in threed
b = g.bounding_box()
File "sage/plot/plot3d/base.pyx", line 1826, in sage.plot.plot3d.base.Graphics3dGroup.bounding_box (/projects/sage/sage-6.9/src/build/cythonized/sage/plot/plot3d/base.c:21855)
v = [obj.bounding_box() for obj in self.all]
File "sage/plot/plot3d/base.pyx", line 2053, in sage.plot.plot3d.base.TransformGroup.bounding_box (/projects/sage/sage-6.9/src/build/cythonized/sage/plot/plot3d/base.c:24768)
cdef Transformation T = self.get_transformation()
File "sage/plot/plot3d/base.pyx", line 2174, in sage.plot.plot3d.base.TransformGroup.get_transformation (/projects/sage/sage-6.9/src/build/cythonized/sage/plot/plot3d/base.c:26552)
self.T = Transformation(self._scale, self._rot, self._trans)
File "sage/plot/plot3d/transform.pyx", line 49, in sage.plot.plot3d.transform.Transformation.__init__ (/projects/sage/sage-6.9/src/build/cythonized/sage/plot/plot3d/transform.c:2785)
self.matrix = m.augment(matrix(RDF, 3, 1, list(trans))) \
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/matrix/constructor.py", line 729, in _matrix_constructor
return matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries)
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/matrix/matrix_space.py", line 524, in __call__
return self.matrix(entries, coerce, copy)
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/matrix/matrix_space.py", line 1442, in matrix
return MC(self, x, copy=copy, coerce=coerce)
File "sage/matrix/matrix_double_dense.pyx", line 234, in sage.matrix.matrix_double_dense.Matrix_double_dense.__init__ (/projects/sage/sage-6.9/src/build/cythonized/sage/matrix/matrix_double_dense.c:3798)
self.set_unsafe(i,j,self._python_dtype(entries[i*self._ncols+j]))
File "sage/symbolic/expression.pyx", line 1368, in sage.symbolic.expression.Expression.__float__ (/projects/sage/sage-6.9/src/build/cythonized/sage/symbolic/expression.cpp:10216)
raise TypeError("unable to simplify to float approximation")
TypeError: unable to simplify to float approximation
This the error with non real bounds like x where I include the graph but not the boxes
Traceback (most recent call last):
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_server.py", line 881, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_server.py", line 1031, in _execute_interact
sage_salvus.interacts[id](vals)
File "/projects/454b81d2-ef23-4b95-bd57-5c718a468ea1/.sagemathcloud/sage_salvus.py", line 216, in __call__
self._f(**dict([(k,self._last_vals[k]) for k in self._args]))
File "", line 19, in interplay
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/plot/plot3d/plot3d.py", line 868, in plot3d
P=parametric_plot3d.parametric_plot3d((u,v,f), urange, vrange, **kwds)
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/plot/plot3d/parametric_plot3d.py", line 594, in parametric_plot3d
G = _parametric_plot3d_surface(f, urange, vrange, plot_points=plot_points, boundary_style=boundary_style, **kwds)
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/plot/plot3d/parametric_plot3d.py", line 704, in _parametric_plot3d_surface
g, ranges = setup_for_eval_on_grid(f, [urange,vrange], plot_points)
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/plot/misc.py", line 123, in setup_for_eval_on_grid
ranges = [[float(z) for z in r] for r in ranges]
File "sage/symbolic/expression.pyx", line 1368, in sage.symbolic.expression.Expression.__float__ (/projects/sage/sage-6.9/src/build/cythonized/sage/symbolic/expression.cpp:10216)
raise TypeError("unable to simplify to float approximation")
TypeError: unable to simplify to float approximation
collabmathTue, 20 Oct 2015 21:48:11 +0200https://ask.sagemath.org/question/30155/Problem with assumption (in RR)https://ask.sagemath.org/question/26854/problem-with-assumption-in-rr/ Hello,
The following code returns the value "False", and I don't understand why. Does 'assume' and 'in RR' work fine together ?
sage: var('alpha')
sage: assume(alpha,'real')
sage: alpha in RR
False
Thank you by advance
Please excuse me for the mistakes, I'm French.
gblotMon, 18 May 2015 07:55:15 +0200https://ask.sagemath.org/question/26854/complex, real, common ring, crash in 'vector' constructorhttps://ask.sagemath.org/question/26473/complex-real-common-ring-crash-in-vector-constructor/ (x,y,z) = var('x,y,z')
x = z.real_part()
y = z.imag_part()
test = vector((x,1,1))
test = vector((x,y,1))
test = vector((z.real_part,1,1))
Last line causes an error, can't find common ring for all elements.
These variables all have type "Expression"; but some expressions denote
complex numbers and some denote real numbers. How do I tell sage about that?
(Sage 6.5 on Mac OS 10).beesonWed, 08 Apr 2015 21:11:48 +0200https://ask.sagemath.org/question/26473/What are the differences between RealDoubleField() and RealField(53) ?https://ask.sagemath.org/question/9991/what-are-the-differences-between-realdoublefield-and-realfield53/Hi,
This question is related to
[question 2402](http://ask.sagemath.org/question/2402/what-are-the-different-real-numbers-in-sage)
(still open!) and tries to collect differences between RDF=RealDoubleField() and RR=RealField(53).
These are two floating point real number fields with both 53 bits of precision. The first one comes from the processor floating-point arithmetic, the second one is "emulated" by mpfr. They are assumed to follow the same rounding standards (to the nearest, according to the [sagebook](http://sagebook.gforge.inria.fr/), but i may be wrong).
However, we can see some differences between them:
sage: RDF(1/10)*10 == RDF(1)
False
sage: RDF(1/10)*10 - RDF(1)
-1.11022302463e-16
sage: RR(1/10)*10 == RR(1)
True
sage: sage: RR(1/10)*10 - RR(1)
0.000000000000000
Could you explain that ?
**EDIT: this was a bug and it is now fixed**, see [trac ticket 14416](http://trac.sagemath.org/ticket/14416).
There are also some specificities on which field should be used for some
methods.
- For example, it seems that the eignevalues are not well computed on RR, but are
correctly computed on RDF ([see trac #13660](http://trac.sagemath.org/sage_trac/ticket/13660)). What is the reason for that ?
- Also, it seems that when dealing with huge matrices, the fast atlas library in
only used when entries are in RDF, not in RR
([see trac #10815](http://trac.sagemath.org/sage_trac/ticket/10815)).
Are there other difference that should be known between both implementations of floating point numbers in Sage ?
ThierrytmonteilThu, 04 Apr 2013 20:34:40 +0200https://ask.sagemath.org/question/9991/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 15:07:48 +0200https://ask.sagemath.org/question/8393/What are the different real numbers in Sage ?https://ask.sagemath.org/question/9950/what-are-the-different-real-numbers-in-sage/No computer can grasp the genuine real numbers, so I am trying to understand
how sage handles them (and going to write a small tutorial about this).
Here is a list of approaching objects i found:
#### Exact subfields of the reals: ####
- QQ = RationalField() The genuine
[rationals](http://www.sagemath.org/doc/reference/rings_standard/sage/rings/rational_field.html).
Denominators can be arbitrary big, the computations are exact, so the rounding
error is only made when approaching a real by a rational, not during furher computations. Notice that the size of the denominators may grow along the computations, which can therefore become very slow.
- NumberField? [number fields](http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field.html) described by an irreducible polynomial.
- AA = AlgebraicField() The genuine [algebraic numbers](http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html). Computations are exact.
- SR = sage.symbolic.ring.SymbolicRing() This is the field made of expressions
like sqrt(2+pi). It handles a lot of real numbers but seems very slow.
Computations are not necessarily exact, since this field contains floating points
real numbers. Is there a way to express any algebraic number in SR ?
- CFF = ContinuedFractionField() reals are represented as a finite list of convergents. Claims it is exact but it actually approaches real numbers by rationals since the list of convergents are finite. However, we could imagine a future version using iterators instead of lists.
> sage: CFF(sqrt(2))^2
> [1, 1, 1491038293021224]
#### Floating point numbers: ####
- RDF = RealDoubleField() Those are the of [double precision floating numbers](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html) the processor. Definitely the fastest, but a rounding is done at each computation.
- RR = RealField() Floating number with arbitrary (still fixed) many bits of
precision. [http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfr.html](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfr.html)
Slower than RDF (in particular, atlas won't be used with them). Notice that
changing precision back and forth is a bad idea. Notice that NaN and Infinity belong to RDF and RR.
- RLF = RealLazyField() like RealField() but can wait that the user fixes the precision to get one. [http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_lazy.html](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_lazy.html)
> sage: a = RLF(pi)
> sage: RealField(100)(a+2)
> 5.1415926535897932384626433833
- RIF = RealIntervalField() A real number is represented as an interval
containing it, and whose extremities are RealFields elements [http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfi.html](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfi.html)
- RLF.interval_field() mixture of the two previous ones.
- sage.rings.real_lazy.LazyAlgebraic
#### Relations between those fields ####
- Two fields approaching the real numbers K and L can be compared using the
function composite_field(K,L), that finds the "best" field containing both K
and L.
> sage: composite_field(RDF,QQbar)
> Complex Double Field
- [Some](http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/number_field_morphisms.html)
[maps](http://www.sagemath.org/doc/reference/number_fields/sage/rings/number_field/maps.html)
are also defined to embed an abstract number field into the real line.
- The method [algebraic_dependency()](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_double.html) that allows to guess whether a floating number corresponds to some algebraic number of a given degree.
Is this list complete or am i missing other real representations ? Im i wrong
in the previous descriptions ? Could you order them by speed, by number of
methods available (without coercion) ? Do you have any remark that could help
the beginner to understand the subtilities in the use of those fields in Sage ?
Given an algebraic number, is it faster to work on AA or on a number field (of
smaller dimension) containing it ? Notice that one can find such an associated
number field with the method [as_number_field_element()](http://www.sagemath.org/doc/reference/number_fields/sage/rings/qqbar.html#sage.rings.qqbar.AlgebraicNumber_base.as_number_field_element)
Do you have any hint, striking example, recommandations of use in special cases
that one should like to learn in a tutorial about real numbers ?
tmonteilThu, 28 Mar 2013 12:36:28 +0100https://ask.sagemath.org/question/9950/check if the result is a real numberhttps://ask.sagemath.org/question/8950/check-if-the-result-is-a-real-number/I get several results and want to check, which one is a real number. From trial and error, I can see that maxSol[2][x] is the only plausable solution.
Is there something like isReal()?
my attempt (don't laugh) ;)
for i in maxSol:
print RealField(50)(i)
where I obviously get:
TypeError: Unable to convert x (='.....disiFri, 04 May 2012 12:39:24 +0200https://ask.sagemath.org/question/8950/How to format huge reals?https://ask.sagemath.org/question/8920/how-to-format-huge-reals/ print '{0:.3e}'.format(float(2.23799235765712699))
print '{0:.3e}'.format(float(2.23799235765712699e47675))
gives
2.238e+00
inf
Clearly what I want in the second case is 2.238e+47675.
How can I achieve this?petropolisSat, 28 Apr 2012 07:40:18 +0200https://ask.sagemath.org/question/8920/Finding Re() and Im() for complex numbershttps://ask.sagemath.org/question/7490/finding-re-and-im-for-complex-numbers/Hey guys,
Is there a way to find the real and imaginary parts of a complex number in Sage? If so, how is it done?
Thanks.SirFibonacciSun, 19 Feb 2012 00:39:49 +0100https://ask.sagemath.org/question/7490/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 20:10:09 +0100https://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 23:19:28 +0200https://ask.sagemath.org/question/8414/SAGE digits!!!https://ask.sagemath.org/question/8058/sage-digits/How can I make p.real_roots() in 6 digits??<br> .n(digits=6) DONT work!!!SagudTue, 05 Apr 2011 12:29:16 +0200https://ask.sagemath.org/question/8058/Additional conditions for expressionhttps://ask.sagemath.org/question/8009/additional-conditions-for-expression/Hi (sorry for bad eng.), i have next code
reset()
var('K1, T1, T2')
var('p')
# Transient function W(p)
W = K1 * p / ((T1 * p - 1) * (T2 * p - 1))
# Amplitude-phase-frequency characteristic W(jw)
Wa(omega) = W(p = I * omega)
Re(omega) = Wa(omega).real()
Im(omega) = Wa(omega).imag()
print(Re)
but last string gives loooooong result, because sagemath don't knows that K1, T1, T2 and omega are real, how to "tell" that to sagemath ?avi9526Sat, 19 Mar 2011 14:16:30 +0100https://ask.sagemath.org/question/8009/exponential equation real solutionhttps://ask.sagemath.org/question/7720/exponential-equation-real-solution/How can I solve the equation
exp(2*x)+exp(-2*x)==2
to get only the real solution x==0 ?
Thanks for help.
amaleaSun, 03 Oct 2010 06:24:29 +0200https://ask.sagemath.org/question/7720/