Mathematica solving complex equation - complex-numbers

If I have a complex equation in the form of
Solve[z^2 + 9 - 1.5 E^[-tz]==0, z]
where z = x + I y
where x , y and t are all assumed to be real.
How do I tell Mathematica 9 to separate the real and imaginary parts to make
two equations in the form of
Re[z]==0, Im[z]==0.
I try this
myConjugate[eqn_Equal] := myConjugate /# eqn
myConjugate[expr_] := expr /. {I -> -I, a_Complex :> Conjugate[a]}
myRe[eqn_Equal] := myRe /# eqn
myRe[expr_] := (expr + myConjugate[expr])/2
myIm[eqn_Equal] := myIm /# eqn
myIm[expr_] := (expr - myConjugate[expr])/(2 I)
and
{myRe[#], myIm[#]} &[(x + I y)^2 + 9 - 1.5 E[- t (x+ I y)] == 0]

Related

making Maxima code run faster (radius of convergence)

I am trying to find the radius of convergence of some Taylor series related to Newton's method applied to the Mandelbrot set. I have written some Maxima code to this end; the R computed is incorrect at least for the first commented case (1 / inf instead of 1 / 2), so I am trying to print some values to see how it behaves numerically. However Maxima takes too much time (1 second for the first value, 17 seconds for the second, and I gave up waiting for the third). The performance in the third commented case is even slower. How might I speed it up?
f(c, z) := z^2 + c;
/* h(c) := f(c, f(c, 0)); /* simpler case is much faster */ */
h(c) := ratsimp(f(c, f(c, f(c, 0))) / f(c, 0)); /* polynomial with roots at period 3 components */
/* h(c) := ratsimp(f(c, f(c, f(c, f(c, 0)))) / f(c, f(c, 0))); /* next case is much slower */ */
hh(c) := ratsimp(diff(h(c), c));
N(z) := ratsimp(z - h(z) / hh(z)); /* Newton's method */
M(z) := ratsimp(subst(c = c + z, N(c)) - c); /* perturbed */
for ct in allroots(h(c) = 0) do
(
print(ct),
/* coefficient of Taylor series */
a(n) := at(diff((subst(lhs(ct) = bfloat(rhs(ct)), M(z))), z, n) / factorial(n), z = 0),
R : 1 / limit(abs(a(n+1)/a(n)), n, infinity), /* radius of convergence */
print(R), /* prints 1/inf, but this is incorrect at least for the first commented case */
print(bfloat(1 / abs(a(11)/a(10)))), /* prints after 1 second */
print(bfloat(1 / abs(a(101)/a(100)))), /* prints after 17 seconds */
print(bfloat(1 / abs(a(1001)/a(1000)))) /* gave up waiting */
);
EDIT Correcting the infinity vs inf confusion, consider the first commented case h(c) = f(c, f(c, 0)) = c^2 + c. Then M(z) = (z^2 - h(c)) / (2 * z + 2 * c + 1) which for the root c = 0 gives M(z) = z^2 / (1 - (-2 * z)) which is the sum of the geometric series z^2 (1 + (-2) * z + (-2)^2 * z^2 + ... + (-2)^n z^n + ...). Now the coefficients a(n) = (-2)^n so 1 / limit(abs(a(n + 1)/a(n)), n, inf) should be 1/2, but Maxima reports a divide by zero:
f(c, z) := z^2 + c;
h(c) := f(c, f(c, 0));
hh(c) := ratsimp(diff(h(c), c));
N(z) := ratsimp(z - h(z) / hh(z));
M(z) := ratsimp(subst(c = c + z, N(c)) - c);
for ct in solve(h(c) = 0) do
(
print(ct),
a(n) := at(diff((subst(lhs(ct) = bfloat(rhs(ct)), M(z))), z, n) / factorial(n), z = 0),
invR : limit(abs(a(n+1)/a(n)), n, inf),
print(abs(a(10+1)/a(10))),
print(abs(a(100+1)/a(100))),
print(abs(a(1000+1)/a(1000))),
print(invR)
);
which prints
...
c = - 1
`rat' replaced -1.0B0 by -1/1 = -1.0B0
`rat' replaced -1.0B0 by -1/1 = -1.0B0
2.0b0
2.0b0
2.0b0
0
c = 0
`rat' replaced 1.0B0 by 1/1 = 1.0B0
`rat' replaced 1.0B0 by 1/1 = 1.0B0
2.0b0
2.0b0
2.0b0
0
I want to evaluate the high order terms of the other examples numerically to see how it behaves, because the limit does not seem to be calculated properly. But my Maxima code is too slow to make this practical, and while SymPy performs ok for the period 3 (cubic) case (see linked question), for the period 4 (degree 6) it also takes too long.

Two warnings "This local variable has the same name as a global variable" and "ELSE with no matching IF"

I'm getting two weird warnings.
One is "This local variable has the same name as a global variable",
referring to the wx, wy, ww, wh.
Another is "ELSE with no matching IF", referring to the two if-else statements.
Here's the whole script.
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
screen_scaling_factor := A_ScreenDPI/96
^p::mouse_move_win(200,300)
mouse_move_win(x,y,horizontal:="left",vertical:="top",mouse_click:=""){
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if horizontal="left"{
x1 := wx + x * screen_scaling_factor
}else{
x1 := wx + ww - x * screen_scaling_factor
}
if vertical="top"{
y1 := wy + y * screen_scaling_factor
}else{
y1 := wy + wh - y * screen_scaling_factor
}
DllCall("SetCursorPos", int, x1, int, y1)
}
When I add local as the first line of the function to enable the "force-local mode", however, the first warning disappears.
When I add a parentheses to the if condition or remove the curly braces following it, as shown below, the second warning disappears.
if (horizontal="left"){
x1 := wx + x * screen_scaling_factor
}else{
x1 := wx + ww - x * screen_scaling_factor
}
or
if horizontal="left"
x1 := wx + x * screen_scaling_factor
else
x1 := wx + ww - x * screen_scaling_factor
Any idea why this is happening?
If you want to use the legacy style if statement (please don't) you can't start your braces from the same line as the if statement. You'd have to drop the starting brace { down one line.
But please just use the modern expression style if-statement like so:
mouse_move_win(x,y,horizontal:="left",vertical:="top",mouse_click:=""){
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if (horizontal="left"){
x1 := wx + x * screen_scaling_factor
}else{
x1 := wx + ww - x * screen_scaling_factor
}
if (vertical="top"){
y1 := wy + y * screen_scaling_factor
}else{
y1 := wy + wh - y * screen_scaling_factor
}
DllCall("SetCursorPos", int, x1, int, y1)
}
Also, personally I'd call that brace style disgusting, but of course it's just personal preference haha.
But just in case you didn't know, you can omit braces from one liner if/else statements:
mouse_move_win(x,y,horizontal:="left",vertical:="top",mouse_click:="")
{
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if (horizontal="left")
x1 := wx + x * screen_scaling_factor
else
x1 := wx + ww - x * screen_scaling_factor
if (vertical="top")
y1 := wy + y * screen_scaling_factor
else
y1 := wy + wh - y * screen_scaling_factor
DllCall("SetCursorPos", int, x1, int, y1)
}
EDIT: oh seems you edited your post. I started typing this before you edited it, but then I had to go do something.
Anyway, my answer should answer your questions.
Use mouse_move_win(200, 300) instead of mouse_move(200,300)
When you define horizontal:="left", it means Left is its default value, meaning if no value is supplied it assumes it's left. So
if horizontal=left
is unnecessary.
So I guess your code should be other way around with the if and else combos.
^p::mouse_move_win(200, 300)
mouse_move_win(x, y, horizontal:="left", vertical:="top", mouse_click:="") {
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if horizontal=right
x1 := wx + ww - x * screen_scaling_factor
else
x1 := wx + x * screen_scaling_factor
if vertical=bottom
y1 := wy + wh - y * screen_scaling_factor
else
y1 := wy + y * screen_scaling_factor
DllCall("SetCursorPos", int, x1, int, y1)
}
This worked perfectly for me

How can I solve exponential equation in Maxima CAS

I have function in Maxima CAS :
f(t) := (2*exp(2*%i*%pi*t) - exp(4*%pi*t*%i))/4;
here:
t is a real number between 0 and 1
function should give a point on the boundary of main cardioid of Mandelbrot set
How can I solve equation :
eq1:c=f(t);
(where c is a complex number)
?
Solve doesn't work
solve( eq1,t);
result is empty list
[]
Result of this equation should give real number t ( internal angle or rotation number ) from complex point c
EDIT: Thx to comment by #JosehDoggie
I can draw initial equation using:
load(draw)$
f(t):=(2*exp(%i*t) - exp(2*t*%i))/4;
draw2d(
key="main cardioid",
nticks=200,
parametric( 0.5*cos(t) - 0.25*cos(2*t), 0.5*sin(t) - 0.25*sin(2*t), t,0,2*%pi),
title="main cardioid of M set "
)$
or
draw2d(polar(abs(exp(t*%i)/2 -exp(2*t*%i)/4),t,0,2*%pi));
Similar image ( cardioid) is here
Edit2:
(%i1) eq1:c = exp(%pi*t*%i)/2 - exp(2*%pi*t*%i)/4;
%i %pi t 2 %i %pi t
%e %e
(%o1) c = ---------- - ------------
2 4
(%i2) solve(eq1,t);
%i log(1 - sqrt(1 - 4 c)) %i log(sqrt(1 - 4 c) + 1)
(%o2) [t = - -------------------------, t = - -------------------------]
%pi %pi
So :
f1(c):=float(cabs( - %i* log(1 - sqrt(1 - 4* c))/%pi));
f2(c):=float(cabs( - %i* log(1 + sqrt(1 - 4* c))/%pi));
but the results are not good.
Edit 3 :
Maybe I shoud start from it.
I have:
complex numbers c ( = boundary of cardioid)
real numbers t ( from 0 to 1 or sometimes from 0 to 2*pi )
function f which computes c from t : c= f(t)
I want to find function which computes t from c: t = g(c)
testing values :
t = 0 , c= 1/4
t = 1/2 , c= -3/4
t = 1/3 , c = c = -0.125 +0.649519052838329*%i
t = 2/5 , c = -0.481762745781211 +0.531656755220025*%i
t = 0.118033988749895 c = 0.346828007859920 +0.088702386914555*%i
t = 0.618033988749895 , c = -0.390540870218399 -0.586787907346969*%i
t = 0.718033988749895 c = 0.130349371041523 -0.587693986342220*%i
load("to_poly_solve") $
e: (2*exp(2*%i*%pi*t) - exp(4*%pi*t*%i))/4 - c $
s: to_poly_solve(e, t) $
s: maplist(lambda([e], rhs(first(e))), s) $ /* unpack arguments of %union */
ratexpand(s);
Outputs
%i log(1 - sqrt(1 - 4 c)) %i log(sqrt(1 - 4 c) + 1)
(%o6) [%z7 - -------------------------, %z9 - -------------------------]
2 %pi 2 %pi

Nested "if" (AKA "switch") in Smalltalk (Pharo)

I need to populate the matrix (stored as an array of arrays) with some values. The matrix is a Jacobian for a simple diffusion problem and looks like this:
J(1,1) = 1, J(N,N)=0
and for 1<n<N:
J(n,n) = -2k/dx^2 - 2*c(n)
J(n,n-1)=J(n,n+1) = k/dx^2
the rest of the matrix entries are zeros.
So far I have this monstrosity:
(1 to: c size) collect: [ :n |
(1 to: c size) collect: [ :m |
n = 1 | (n = c size)
ifTrue: [ m = n ifTrue: [ 1.0 ] ifFalse: [ 0.0 ] ]
ifFalse: [ m = n
ifTrue: [ -2.0 * k / dx squared - (2.0 * (c at: n)) ]
ifFalse: [ m = (n-1) | (m = (n+1))
ifTrue: [ k / dx squared ]
ifFalse: [ 0.0 ] ] ]
] ]
Notice the nested "if-statements" (Smalltalk equivalents). This works. But, is there, perhaps, a more elegant way of doing the same thing? As it stands now, it is rather unreadable.
n := c size.
Matrix
new: n
tabulate: [:i :j | self jacobianAtRow: i column: j]
where
jacobianAtRow: i column: j
n := c size.
(i = 1 or: [i = n]) ifTrue: [^j = i ifTrue: [1.0] ifFalse [0.0]].
j = i ifTrue: [^-2.0 * k / dx squared - (2.0 * (c at: i))].
(j = (i - 1) or: [j = (i + 1)]) ifTrue: [^k / dx squared].
^0.0
Basically, the general idea is this: whenever you find nested ifs, factor out that piece of code to a method by itself and transform the nesting into a cases-like enumeration that returns a value at every possibility.
For readability's sake I would consider sacrificing the extra O(n) time and avoid IFs altogether (which just make it even faster...).
J(N,N) = 0
J(1,1) = 1
//and for 1<n<N:
J(n,n) = Y(n)
J(n,m-1) = J(n,m+1) = X
What this tells me is that the whole matrix looks something like this
( 1 X 0 0 0 )
( X Y X 0 0 )
( 0 X Y X 0 )
( 0 0 X Y X )
( 0 0 0 X 0 )
Which means that I can create the whole matrix with just zeros, and then change the diagonal and neighboring diagonals.
jNM := [ k / dx squared ].
jNN := [ :n | -2.0 * k / dx squared - (2.0 * (c at: n)) ].
n := c size.
m := Matrix
new: n
tabulate: [:i :j | 0 ].
(1 to: n - 1) do: [ :i |
m at: i at: i put: (jNN value: i).
m at: i + 1 at: i put: jnM value.
m at: i at: i + 1 put: jnM value.
].
m at: 1 at: 1 put: 1.
Note: I'm not familiar with the math behind this but the value for J(n,m-1) seems like a constant to me.
Note 2: I'm putting the values at i + 1 indexes, because I am starting at position 1;1, but you can start from the opposite direction and have i-1.

Haskell, function works when using numbers, but not with variables

I'm using ghci and I'm having a problem with a function for getting the factors of a number.
The code I would like to work is:
let factors n = [x | x <- [1..truncate (n/2)], mod n x == 0]
It doesn't complain when I then hit enter, but as soon as I try to use it (with 66 in this case) I get this error message:
Ambiguous type variable 't0' in the constraints:
(Integral t0)
arising from a use of 'factors' at <interactive>:30:1-10
(Num t0) arising from the literal '66' at <interactive>:30:12-13
(RealFrac t0)
arising from a use of 'factors' at <interactive:30:1-10
Probable fix: add a type signature that fixes these type variable(s)
In the expression: factors 66
In the equation for 'it': it = factors 66
The following code works perfectly:
let factorsOfSixtySix = [x | x <- [1..truncate (66/2)], mod 66 x == 0]
I'm new to haskell, and after looking up types and typeclasses, I'm still not sure what I'm meant to do.
Use div for integer division instead:
let factors n = [x | x <- [1.. n `div` 2], mod n x == 0]
The problem in your code is that / requires a RealFrac type for n while mod an Integral one. This is fine during definition, but then you can not choose a type which fits both constraints.
Another option could be to truncate n before using mod, but is more cumbersome. After all, you do not wish to call factors 6.5, do you? ;-)
let factors n = [x | x <- [1..truncate (n/2)], mod (truncate n) x == 0]
If you put a type annotation on this top-level bind (idiomatic Haskell), you get different, possibly more useful error messages.
GHCi> let factors n = [x | x <- [1..truncate (n/2)], mod n x == 0]
GHCi> :t factors
factors :: (Integral t, RealFrac t) => t -> [t]
GHCi> let { factors :: Double -> [Double]; factors n = [x | x <- [1..truncate (n/2)], mod n x == 0]; }
<interactive>:30:64:
No instance for (Integral Double) arising from a use of `truncate'
Possible fix: add an instance declaration for (Integral Double)
In the expression: truncate (n / 2)
In the expression: [1 .. truncate (n / 2)]
In a stmt of a list comprehension: x <- [1 .. truncate (n / 2)]
GHCi> let { factors :: Integer -> [Integer]; factors n = [x | x <- [1..truncate (n/2)], mod n x == 0]; }
<interactive>:31:66:
No instance for (RealFrac Integer) arising from a use of `truncate'
Possible fix: add an instance declaration for (RealFrac Integer)
In the expression: truncate (n / 2)
In the expression: [1 .. truncate (n / 2)]
In a stmt of a list comprehension: x <- [1 .. truncate (n / 2)]
<interactive>:31:77:
No instance for (Fractional Integer) arising from a use of `/'
Possible fix: add an instance declaration for (Fractional Integer)
In the first argument of `truncate', namely `(n / 2)'
In the expression: truncate (n / 2)
In the expression: [1 .. truncate (n / 2)]
I am new to Haskell so please forgive my courage to come up with an answer here but recently i have done this as follows;
factors :: Int -> [Int]
factors n = f' ++ [n `div` x | x <- tail f', x /= exc]
where lim = truncate (sqrt (fromIntegral n))
exc = ceiling (sqrt (fromIntegral n))
f' = [x | x <- [1..lim], n `mod` x == 0]
I believe it's more efficient. You will notice if you do like;
sum (factors 33550336)