J
jzakiya
I thought I would extract some of the issues
and ideas raised from the thread 'Math errors'
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/75cad8ec7b137fd8#
that dealt with taking the roots of negative reals,
like (-27)^(1/3) and extend the math to cover roots
of all real values, positive and negative.
Mathematical foundations
1) i = (-1)^(1/2)
2) i^1 = i
3) i^2 = -1
4) i^3 = -i
5) i^4 = 1
6) Then it repeats, e.g.: i^5 = i*(i^4) = i
7) e^(i*x) = cos(x) + i*sin(x)
8) when x = PI/2 then e^(PI*i/2) = i
For roots (-a)^(1/n) of negative real values:
x = |a^(1/n)|*(-1)^(1/n)
from 2) above
x = |a^(1/n)|*(i^2)^(1/n)
x = |a^(1/n)|*(i)^(2/n)
apply 8) from above
x = |a^{1/n)|*e^(PI*i/2)^(2/n)
x = |a^(1/n)|*e^(PI*i/n)
9) x = |a^(1/n)|*(cos(PI/n) + i*sin(PI/n))
For roots (a)^(1/n) of positive real values:
x = (a)^(1/n)
x = (a*1)^(1/n)
x = |a^(1/n)|*(1)^(1/n)
from 5) above
x = |a^(1/n)|*(i^4))^(1/n)
x = |a^(1/n)|*(i)^(4/n)
apply 8) from above
x = |a^{1/n)|*e^(PI*i/2)^(4/n)
x = |a^(1/n)|*e^(2*PI*i/n)
10) x = |a^(1/n)|*(cos(2*PI/n) + i*sin(2*PI/n))
There are n distinct roots for any real value:
9) and 10) say, the n roots have magnitude |a^(1/n)|
and dstributed counter clockwise (ccw) on unit circle
2*PI/n (360/n) degrees apart, first root at angle PI/n.
To find the n roots of neg real values the ccw spacing is:
11) roots(n,k)=cos(PI/n+2*k*PI/n) + i*sin(PI/n+2*k*PI/n)
=cos((2*k+1)*PI/n) + i*sin((2*k+1)PI/n)
To find the n roots of pos real values the ccw spacing is:
12) roots(n,k)=cos(PI/n+(2*k+1)*PI/n)+i*sin(PI/n+(2*k+1)*PI/n)
=cos(2*(k+1)*PI/n) + i*sin(2*(k+1)*PI/n)
where n is the root number and k = 0..n-1 is the kth root
In Ruby these can be coded as:
require 'complex'
include Math
13)
def roots-neg-reals(a,n,k) # a real value, n root, kth root
(a.abs**n**-1)*Complex(cos((2*k+1)*PI/n),sin((2*k+1)*PI/n))
end
14)
def roots-pos-reals(a,n,k) # a real value, n root, kth root
(a.abs**n**-1)*Complex(cos(2*(k+1)*PI/n),sin(2*(k+1)*PI/n))
end
When a is negative, n odd, like (-27)^(1/3), only one real root
and (n-1)/2 complex conjugate pairs (CCP), and the real root is
the middle kth ccw root, which is at [cos(PI)+i*sin(PI)].
When n is even there are n/2 CCPs.
When a is positive, n odd, like (32)^(1/5), only (+) real root is
the last ccw root at cos(2*PI)+i*sin(2*PI). Again, (n-1)/2 CCPs.
When n even there are 2 real roots (+ and -) and the rest CCPs.
Ex: In ccw order: (1)^(1/2) = -1,1; (1)^(1/4)= i,-1,-i,1
Checke with 14) with a=1, and n=2, k=0,1 and n=4,k=0,1,2,3
Thus, using 13), 14) as methods in Complex module, a syntax like
Complex(a).root(n,k), etc, can correctly find all the roots.
def Complex.root(a,n,k)
if a < 0
roots-neg-reals(a,n,k)
else
roots-pos-reals(a,n,k)
end
end
This is just illustrative of the concept, that could be
incorporatied in the Complex, et al, modules.
This will correct some of the math errors observed in the
previous thread and allow for more features to be correctly added to
the numerical classes.
and ideas raised from the thread 'Math errors'
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/75cad8ec7b137fd8#
that dealt with taking the roots of negative reals,
like (-27)^(1/3) and extend the math to cover roots
of all real values, positive and negative.
Mathematical foundations
1) i = (-1)^(1/2)
2) i^1 = i
3) i^2 = -1
4) i^3 = -i
5) i^4 = 1
6) Then it repeats, e.g.: i^5 = i*(i^4) = i
7) e^(i*x) = cos(x) + i*sin(x)
8) when x = PI/2 then e^(PI*i/2) = i
For roots (-a)^(1/n) of negative real values:
x = |a^(1/n)|*(-1)^(1/n)
from 2) above
x = |a^(1/n)|*(i^2)^(1/n)
x = |a^(1/n)|*(i)^(2/n)
apply 8) from above
x = |a^{1/n)|*e^(PI*i/2)^(2/n)
x = |a^(1/n)|*e^(PI*i/n)
9) x = |a^(1/n)|*(cos(PI/n) + i*sin(PI/n))
For roots (a)^(1/n) of positive real values:
x = (a)^(1/n)
x = (a*1)^(1/n)
x = |a^(1/n)|*(1)^(1/n)
from 5) above
x = |a^(1/n)|*(i^4))^(1/n)
x = |a^(1/n)|*(i)^(4/n)
apply 8) from above
x = |a^{1/n)|*e^(PI*i/2)^(4/n)
x = |a^(1/n)|*e^(2*PI*i/n)
10) x = |a^(1/n)|*(cos(2*PI/n) + i*sin(2*PI/n))
There are n distinct roots for any real value:
9) and 10) say, the n roots have magnitude |a^(1/n)|
and dstributed counter clockwise (ccw) on unit circle
2*PI/n (360/n) degrees apart, first root at angle PI/n.
To find the n roots of neg real values the ccw spacing is:
11) roots(n,k)=cos(PI/n+2*k*PI/n) + i*sin(PI/n+2*k*PI/n)
=cos((2*k+1)*PI/n) + i*sin((2*k+1)PI/n)
To find the n roots of pos real values the ccw spacing is:
12) roots(n,k)=cos(PI/n+(2*k+1)*PI/n)+i*sin(PI/n+(2*k+1)*PI/n)
=cos(2*(k+1)*PI/n) + i*sin(2*(k+1)*PI/n)
where n is the root number and k = 0..n-1 is the kth root
In Ruby these can be coded as:
require 'complex'
include Math
13)
def roots-neg-reals(a,n,k) # a real value, n root, kth root
(a.abs**n**-1)*Complex(cos((2*k+1)*PI/n),sin((2*k+1)*PI/n))
end
14)
def roots-pos-reals(a,n,k) # a real value, n root, kth root
(a.abs**n**-1)*Complex(cos(2*(k+1)*PI/n),sin(2*(k+1)*PI/n))
end
When a is negative, n odd, like (-27)^(1/3), only one real root
and (n-1)/2 complex conjugate pairs (CCP), and the real root is
the middle kth ccw root, which is at [cos(PI)+i*sin(PI)].
When n is even there are n/2 CCPs.
When a is positive, n odd, like (32)^(1/5), only (+) real root is
the last ccw root at cos(2*PI)+i*sin(2*PI). Again, (n-1)/2 CCPs.
When n even there are 2 real roots (+ and -) and the rest CCPs.
Ex: In ccw order: (1)^(1/2) = -1,1; (1)^(1/4)= i,-1,-i,1
Checke with 14) with a=1, and n=2, k=0,1 and n=4,k=0,1,2,3
Thus, using 13), 14) as methods in Complex module, a syntax like
Complex(a).root(n,k), etc, can correctly find all the roots.
def Complex.root(a,n,k)
if a < 0
roots-neg-reals(a,n,k)
else
roots-pos-reals(a,n,k)
end
end
This is just illustrative of the concept, that could be
incorporatied in the Complex, et al, modules.
This will correct some of the math errors observed in the
previous thread and allow for more features to be correctly added to
the numerical classes.