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.Sun, 11 Oct 2020 14:39:24 -0500plotted real intersection but solve only shows imaginaryhttps://ask.sagemath.org/question/53835/plotted-real-intersection-but-solve-only-shows-imaginary/I plotted `x^3 - x` and its derivative `3*x^2 - 1` to get two intersections in the real plane.
plot([x^3-x,3*x^2 - 1],-3,3,color=['blue','green'],legend_label=["f","derivative"])
![intersection](/upfiles/16024461625093012.png)
However, when I solved the two with `solve(x^3-x == 3*x^2 - 1, x)`, all I get are imaginary values.
solve(x^3-x == 3x^2 - 1,x)
[x == -1/2*(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3)*(I*sqrt(3) + 1) - 2/3*(-I*sqrt(3) + 1)/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1, x == -1/2*(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3)*(-I*sqrt(3) + 1) - 2/3*(I*sqrt(3) + 1)/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1, x == (1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 4/3/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1]
Shouldn't `solve` show me the two intersections or am I disastrously confused?cybervigilanteSun, 11 Oct 2020 14:39:24 -0500https://ask.sagemath.org/question/53835/Imaginary result for cube root of -1https://ask.sagemath.org/question/53481/imaginary-result-for-cube-root-of-1/I get -1 as the cube root of -1,which is correct.
But if I put -1 in parentheses I get an imaginary result.
I was hoping the imaginary result, if cubed, would give
me -1, but it got worse.
-1^(1/3).n()
-1.00000000000000
(-1)^(1/3).n()
0.500000000000000 + 0.866025403784439*I
cybervigilanteThu, 17 Sep 2020 16:44:47 -0500https://ask.sagemath.org/question/53481/How can I calculate only the imaginary part of a complex number?https://ask.sagemath.org/question/49031/how-can-i-calculate-only-the-imaginary-part-of-a-complex-number/ So I have the following function which the x will be the difference between two point:
def complexDis(x): return sqrt((x.real()^2)+((x.imag()*-i)^2))
But it seems like i can’t actually operate the .real or .imag number or at least I can’t print it out with N(conplexDis(b-a)) so im asking how can I achieve my goal?andrewygFri, 13 Dec 2019 22:02:05 -0600https://ask.sagemath.org/question/49031/Imaginary part of a ratio of polynomialshttps://ask.sagemath.org/question/42609/imaginary-part-of-a-ratio-of-polynomials/When working with polynomial rings with complex coefficients, how to pass the assumption that the unknown variable belongs to the Reals set?
I am trying to recover and plot the imaginary part of the the ratio of polynomials *arg2*, but as sage assumes the independent variable is complex, it takes a long time and returns a huge answer. But I think the answer could be much simpler if it assumes that the independent variable belongs to the Reals set. The *assume()* function doesn't seems to work in this case.
*arg* has complex terms in the numerator and denominator. *arg2* has complex terms only on the numerator. So, it should be easy to obtain the imaginary part of *arg2*.
s = polygen(CC, "s")
omega = polygen(CC, "omega")
I = CC(I)
# assume(omega, "real") # this does not work
s21 = 0.894292331092066/(s^4 + 2.14081977623463*s^3 + 3.15237117897600*s^2 + 2.31898630138664*s + 0.902488008823108)
s21_omega = s21.subs(s=I*omega)
s21_omega_diff = s21_omega.derivative()
arg = 1/s21_omega*s21_omega_diff
num = arg.numerator()
den = arg.denominator()
den_conj = den.map_coefficients(lambda z: z.conjugate())
num2 = (num*den_conj).map_coefficients(lambda z: 0 if abs(z.real())<1e-10 and abs(z.imag())<1e-10 else z.imag()*I if abs(z.real())<1e-10 and abs(z.imag())>=1e-10 else z.real() if abs(z.real())>=1e-10 and abs(z.imag())<1e-10 else z)
den2 = (den*den_conj).map_coefficients(lambda z: 0 if abs(z.real())<1e-10 and abs(z.imag())<1e-10 else z.imag()*I if abs(z.real())<1e-10 and abs(z.imag())>=1e-10 else z.real() if abs(z.real())>=1e-10 and abs(z.imag())<1e-10 else z)
arg2 = num2/den2
print(arg2)
print(imag(arg2))
# plot(-imag(arg2), (-1.5, 1.5)).show()
Output:
(-3.19903509380033*omega^15 - 1.71213939841908*I*omega^14 + 9.63823791915898*omega^13 + 3.11426578979509*I*omega^12 - 15.8129908994689*omega^11 - 4.60245136409720*I*omega^10 + 13.7326241083798*omega^9 + 1.24770230131999*I*omega^8 - 9.58497293293397*omega^7 - 0.760732566992820*I*omega^6 + 4.72291963120453*omega^5 - 2.52135706735033*I*omega^4 - 2.44038907746050*omega^3 - 0.463630243025939*I*omega^2 + 0.203401406783201*omega - 1.36326886734867*I)/(0.799758773450081*omega^16 - 2.75378226261685*omega^14 + 5.27099696648963*omega^12 - 5.49304964335183*omega^10 + 4.79248646646703*omega^8 - 3.14861308746971*omega^6 + 2.44038907746050*omega^4 - 0.406802813566401*omega^2 + 0.530548112702672)
2.55845638284151*imag_part(omega)^31/(0.639614095710378*imag_part(omega)^32 + 10.2338255313661*imag_part(omega)^30*real_part(omega)^2 + 76.7536914852462*imag_part(omega)^28*real_part(omega)^4 + 358.183893597743*imag_part(omega)^26*real_part(omega)^6 + 1164.09765419317*imag_part(omega)^24*real_part(omega)^8 + 2793.83437005430*imag_part(omega)^22*real_part(omega)^10 + 5122.02967846394*imag_part(omega)^20*real_part(omega)^12 + 7317.18525490165*imag_part(omega)^18*real_part(omega)^14 + 8231.83341181278*imag_part(omega)^16*real_part(omega)^16 + 7317.18525493145*imag_part(omega)^14*real_part(omega)^18 + 5122.02967846394*imag_part(omega)^12*real_part(omega)^20 + 2793.83437005430*imag_part(omega)^10*real_part(omega)^22 + 1164.09765419317*imag_part(omega)^8*real_part(omega)^24 + 358.183893597743*imag_part(omega)^6*real_part(omega)^26 + ... (huge answer)
Thank you!joaoffFri, 15 Jun 2018 09:23:09 -0500https://ask.sagemath.org/question/42609/Elliptic Curves and Isogenies in Sagehttps://ask.sagemath.org/question/40624/elliptic-curves-and-isogenies-in-sage/I have the following Sage code which throws an error:
p = 10354717741769305252977768237866805321427389645549071170116189679054678940682478846502882896561066713624553211618840202385203911976522554393044160468771151816976706840078913334358399730952774926980235086850991501872665651576831
Fp = GF(p)
R.<x> = PolynomialRing(Fp)
# The quadratic extension via x^2 + 1 since p = 3 mod 4
Fp2 = Fp.extension(x^2 + 1, 'i')
# E0 is the starting curve E0/Fp2: y^2=x^3+x (the A=0 Montgomery curve)
E0 = EllipticCurve(Fp2, [1,0])
assert E0.is_supersingular()
phiP = E0([params[2], params[3]])
print "phiP =", phiP
phiQ = E0([-params[2], I*params[3]])
print "phiQ =", phiQ
`param` is a list of some big values. The `phiP` part executes sucessfully, and I get the correct values that I need, but the next line `phiQ`, throws and error. I believe because I want to have an imaginary number and multiply by `I`. The error that it throws is: `TypeError: positive characteristic not allowed in symbolic computations` . Any ideas what the problem is and how to workaround it?
Additionally, I also have a problem with the `EllipcticCurveIsogeny` function. Using the above prime and fields, I have something like this:
E = EllipticCurve(Fp2, [1,0])
ker = x^3 + 10354717741769305252977768237866805321427389645549071170116189679054678940682478846502882896561066713624553211618840202385203911976522554393044160468771151816976706840078913334358399730952774926980235086850991501872665651576829*x^2 + x
phi = EllipticCurveIsogeny(E, ker)
But, this throws also an error, the error is: `NotImplementedError: For basic Kohel's algorithm, if the kernel degree is even then the kernel must be contained in the two torsion.`. Any idea how to call the isogeny function using a kernel?
**EDIT:**
params = [
578430703315757416239167247452252298383230451121890570770496205879957246271\
94741927699803619225371873099605244752411865273005490885339418654128746\
61143122262830946833377212881592965099601886901183961091839303261748866\
970694633,
552894179318461736451145230096269508494216546007889788158066655273655541827\
34966458946743147740010723538169667646894930981225566627558420019697816\
87909521301233517912821073526079191975713749455487083964491867894271185\
073160661,
435991739684910123105333676370030089291509670001370421019478145780141273164\
39883673898708868843364532451567754543362491991856542501590519299756008\
57047173121187832546031604804277991148436536445770452624367894371450077\
315674371,
106866937607440797536385002617766720826944674650271400721039514250889186719\
92313304948796673051468229664303969453105267287375412800684443463681956\
65543642579133322371232938607676833959588179836843700655987261910882390\
28762772
]ninhoMon, 15 Jan 2018 11:43:52 -0600https://ask.sagemath.org/question/40624/Forcing Symbolic Variables to Always be Realhttps://ask.sagemath.org/question/29422/forcing-symbolic-variables-to-always-be-real/I am having a problem, I am trying to do algebra with a function of multiple variables, but Sage is assuming the variables could be complex numbers, which I do not want to do. I have a minimal working example that shows the problem. I am using the method discussed at [this Google thread on the issue](https://groups.google.com/forum/#!topic/sage-support/e_RRz4IC4IU). I actually want to use the commented out equation, but I can't even get the behaviour right for a purely real (at least conceptually) function.
R, r, L, omega, k, s = var('R r L omega k s')
assume(R, 'real')
assume(r, 'real')
assume(L, 'real')
assume(omega, 'real')
assume(k, 'real')
assume(s, 'real')
R.conjugate().simplify()
r.conjugate().simplify()
L.conjugate().simplify()
omega.conjugate().simplify()
k.conjugate().simplify()
s.conjugate().simplify()
#Z(R,r,L,k,omega,s) = R + I * omega * L + ((s * omega**2 * L**2 * k**2) * (r - I * omega * L)) / (r**2 + omega**2 * s **2 * L**2)
Z(s,L) = s * L
show(Z.diff(s))
Z
show(Z)
(s, L) |--> L*s
(s, L) |--> -(imag_part(L)*D[0](imag_part)(s) - real_part(L)*D[0](real_part)(s))/abs(L*s) + (imag_part(L)*imag_part(s) - real_part(L)*real_part(s))*L/(abs(L*s)*conjugate(L)*conjugate(s))
The derivative of the argument of `s*L` with respect to `s` should be zero by definition, since both `s` and `L` are purely real variables. Why is Sage assuming the following two things: that real variables all of a sudden become complex, and that `s` and `L` are related to each other. I want them to be real independent real variables. **Always**. How do I 'enforce' this?bjem00Tue, 08 Sep 2015 23:37:53 -0500https://ask.sagemath.org/question/29422/Number field containing real/imaginary part of algebraic numberhttps://ask.sagemath.org/question/25822/number-field-containing-realimaginary-part-of-algebraic-number/I have a number field N with an embedding into C (e.g., NumberField(x^3+3, 'z', 0.7+1.2j)).
I would like the smallest number field N' that contains all Re(z) and Im(z) for z in N.
I would also like a mapping from x in N to (Re(x), Im(x) in N'xN' - or at least know what Re(z) and Im(z) is in N' for the generator z of N.
What is a good way of doing this?
I was thinking along the lines of N.composite_fields(NumberField(N.defining_polynomial, 'z', embedding=ComplexField()(N.gen_embedding()).conjugate()),both_maps=True). But I ran into http://trac.sagemath.org/ticket/14164 with this occasionally. Is there a better way of doing it?matthiasFri, 13 Feb 2015 12:47:13 -0600https://ask.sagemath.org/question/25822/Solve: 1-4i/4i(1+4i)^-1https://ask.sagemath.org/question/10356/solve-1-4i4i14i-1/Solve: 1-4i/4i(1+4i)^-1...not sure how to do denominatorBurleyTue, 16 Jul 2013 14:15:49 -0500https://ask.sagemath.org/question/10356/Using "j" for Imaginary Unit?https://ask.sagemath.org/question/10039/using-j-for-imaginary-unit/This is probably trivial, but it's a minor annoyance that I'd like to fix.
Is there a way to define "j" to be the imaginary unit so that it not only represents "i" (in all expressions,etc.) but also appears in output as "j"?
sage: var('j,w,t')
(j, w, t)
sage: j=i
sage: j
I
sage: exp(j*w*t)
e^(I*t*w)
sage:
I'd like "j" to have the value of "i" but still appear as "j" in all output (LaTeX too!).
rickhg12hsThu, 18 Apr 2013 19:17:35 -0500https://ask.sagemath.org/question/10039/numerically integrating an expression containing 'i'https://ask.sagemath.org/question/9884/numerically-integrating-an-expression-containing-i/This is example of an integral that can't be evaluated using
sage: numerical_integral(i*sin(i*x), x, 0, 0.2)
Traceback (click to the left of this block for traceback)
...
TypeError: unable to coerce to a real number
even though the integrand is real (in disguse). How can I get around this problem?ebsWed, 06 Mar 2013 05:45:59 -0600https://ask.sagemath.org/question/9884/roots() returns no real solutions for cubic functionhttps://ask.sagemath.org/question/9437/roots-returns-no-real-solutions-for-cubic-function/Hi there
I have a pretty complicated third-order polynomial which I use Sage to solve. However, somehow roots() only returns imaginary solutions. This should not be possible as a cubic function always has at least one root, so naturally I'm puzzled by this.
I could't seem to include all my code (formatting gets messed up).
First I checked that the equation really is a polynomial in m:
print dldm.degree(m)
Outputs 3, so that's OK. Next, I obtain the roots, which I have printed below:
-0.000119434160296805 + 2.27373675443232e-12*I
-5395.14738658974 - 7.95807864051312e-13*I
2560.33896686341 - 1.36424205265939e-12*I
As you can see, they're all imaginary, which should not be possible. The imaginary part is very small on all the solutions. My guess is that Sage uses some numerical approximations to obtain the solutions which is why it seemingly violates mathematics. But what do you think? Thanks!TheoTue, 16 Oct 2012 19:21:52 -0500https://ask.sagemath.org/question/9437/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 05:39:24 -0500https://ask.sagemath.org/question/8950/TypeError doing a contour plot of imag_part(I*log(x+I*y))https://ask.sagemath.org/question/7500/typeerror-doing-a-contour-plot-of-imag_partilogxiy/Hi. I'm trying to do a contour plot of the imaginary part of i*log(z).
z = var("z")
x,y = var("x,y",domain="real")
u = I*log(z)
f = imag_part(u(z=(x+I*y)))
contour_plot(f,(x,-3,3),(y,-3,3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_30.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("eiA9IHZhcigieiIpCngseSA9IHZhcigieCx5Iixkb21haW49InJlYWwiKQp1ID0gSSpsb2coeikKZiA9IGltYWdfcGFydCh1KHo9KHgrSSp5KSkpCmNvbnRvdXJfcGxvdChmLCh4LC0zLDMpLCh5LC0zLDMpKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpk8YRSf/___code___.py", line 7, in <module>
exec compile(u'contour_plot(f,(x,-_sage_const_3 ,_sage_const_3 ),(y,-_sage_const_3 ,_sage_const_3 ))
File "", line 1, in <module>
File "/cosas/sage/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 456, in wrapper
return func(*args, **kwds)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 456, in wrapper
return func(*args, **kwds)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 534, in wrapper
return func(*args, **options)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/plot/contour_plot.py", line 470, in contour_plot
g, ranges = setup_for_eval_on_grid([f], [xrange, yrange], options['plot_points'])
File "/cosas/sage/local/lib/python2.6/site-packages/sage/plot/misc.py", line 144, in setup_for_eval_on_grid
return fast_float(funcs, *vars,**options), [tuple(range+[range_step]) for range,range_step in zip(ranges, range_steps)]
File "fast_eval.pyx", line 1357, in sage.ext.fast_eval.fast_float (sage/ext/fast_eval.c:8418)
File "fast_eval.pyx", line 1372, in sage.ext.fast_eval.fast_float (sage/ext/fast_eval.c:8627)
File "fast_callable.pyx", line 420, in sage.ext.fast_callable.fast_callable (sage/ext/fast_callable.c:3173)
File "expression.pyx", line 8108, in sage.symbolic.expression.Expression._fast_callable_ (sage/symbolic/expression.cpp:30640)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1392, in fast_callable
return FastCallableConverter(ex, etb)()
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 220, in __call__
return self.composition(ex, operator)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1370, in composition
return self.etb.call(function, *ex.operands())
File "fast_callable.pyx", line 685, in sage.ext.fast_callable.ExpressionTreeBuilder.call (sage/ext/fast_callable.c:5024)
File "fast_callable.pyx", line 565, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (sage/ext/fast_callable.c:4378)
File "expression.pyx", line 8108, in sage.symbolic.expression.Expression._fast_callable_ (sage/symbolic/expression.cpp:30640)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1392, in fast_callable
return FastCallableConverter(ex, etb)()
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 220, in __call__
return self.composition(ex, operator)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1370, in composition
return self.etb.call(function, *ex.operands())
File "fast_callable.pyx", line 685, in sage.ext.fast_callable.ExpressionTreeBuilder.call (sage/ext/fast_callable.c:5024)
File "fast_callable.pyx", line 565, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (sage/ext/fast_callable.c:4378)
File "expression.pyx", line 8108, in sage.symbolic.expression.Expression._fast_callable_ (sage/symbolic/expression.cpp:30640)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1392, in fast_callable
return FastCallableConverter(ex, etb)()
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 214, in __call__
return self.arithmetic(ex, operator)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1332, in arithmetic
return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1332, in <lambda>
return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
File "fast_callable.pyx", line 685, in sage.ext.fast_callable.ExpressionTreeBuilder.call (sage/ext/fast_callable.c:5024)
File "fast_callable.pyx", line 565, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (sage/ext/fast_callable.c:4378)
File "expression.pyx", line 8108, in sage.symbolic.expression.Expression._fast_callable_ (sage/symbolic/expression.cpp:30640)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1392, in fast_callable
return FastCallableConverter(ex, etb)()
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 213, in __call__
return self.arithmetic(div, div.operator())
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1332, in arithmetic
return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1332, in <lambda>
return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
File "fast_callable.pyx", line 685, in sage.ext.fast_callable.ExpressionTreeBuilder.call (sage/ext/fast_callable.c:5024)
File "fast_callable.pyx", line 565, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (sage/ext/fast_callable.c:4378)
File "expression.pyx", line 8108, in sage.symbolic.expression.Expression._fast_callable_ (sage/symbolic/expression.cpp:30640)
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 1392, in fast_callable
return FastCallableConverter(ex, etb)()
File "/cosas/sage/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py", line 204, in __call__
raise err
TypeError: cannot convert I to real number
I get a similar error when using `1j`, except it's
TypeError: Unable to convert 1.00000000000000*I to float; use abs() or
real_part() as desired
One work-around I've found is defining `f` as a regular python function:
z = var("z")
x,y = var("x,y",domain="real")
u = I*log(z)
def f(x,y): return imag_part(u(z=(x+I*y)))
contour_plot(f,(x,-3,3),(y,-3,3))
However it is much slower than ordinary plots.ignamvFri, 13 Apr 2012 02:48:28 -0500https://ask.sagemath.org/question/7500/solving sqrt(-1) to a real numberhttps://ask.sagemath.org/question/8762/solving-sqrt-1-to-a-real-number/Here is what I am trying to do:
var('x')
A(x)=x/2+(4-x*x)^(1/2)
assume(0<x<2)
maximum = (derivative(A)==0).maxima_methods().rootscontract().simplify()
view(maximum)
view(solve(maximum,x))
A.plot(A,0,2)
and get i multiplied by the root of x^2... if I put the same equation into wolfram, it gives me a real number 2/sqrt(5) (which is correct and makes sense).
How can I make SageMath solve those? I tried simplify() and full_simplify(), maxima_methods() and rootscontract() gives an error in combination with solve().
I guess it's just some syntax error, sorry for that :(
For now I do most in the Sage - Cell Server, which is great.disiThu, 01 Mar 2012 02:26:06 -0600https://ask.sagemath.org/question/8762/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.SirFibonacciSat, 18 Feb 2012 17:39:49 -0600https://ask.sagemath.org/question/7490/