cmath: added arg, log and complex pow operations

pull/1161/head
Archan Patkar 2019-07-15 17:44:20 +05:30 committed by Alexander Medvednikov
parent 48c06df5f5
commit 4af58e0925
2 changed files with 91 additions and 0 deletions

View File

@ -160,6 +160,34 @@ pub fn (c Complex) ln() Complex {
} }
} }
// Complex Log Base Complex
// Based on
// http://www.milefoot.com/math/complex/summaryops.htm
pub fn (c Complex) log(base Complex) Complex {
return base.ln().divide(c.ln())
}
// Complex Argument
// Based on
// http://mathworld.wolfram.com/ComplexArgument.html
pub fn (c Complex) arg() f64 {
return math.atan2(c.im,c.re)
}
// Complex raised to Complex Power
// Based on
// http://mathworld.wolfram.com/ComplexExponentiation.html
pub fn (c Complex) cpow(p Complex) Complex {
a := c.arg()
b := math.pow(c.re,2) + math.pow(c.im,2)
d := p.re * a + (1.0/2) * p.im * math.log(b)
t1 := math.pow(b,p.re/2) * math.exp(-p.im*a)
return Complex{
t1 * math.cos(d),
t1 * math.sin(d)
}
}
// Complex Sin // Complex Sin
// Based on // Based on
// http://www.milefoot.com/math/complex/functionsofi.htm // http://www.milefoot.com/math/complex/functionsofi.htm

View File

@ -254,6 +254,69 @@ fn test_complex_ln() {
assert result.str().eq(c2.str()) assert result.str().eq(c2.str())
} }
fn test_complex_arg() {
// Tests were also verified on Wolfram Alpha
mut c1 := cmath.complex(5,7)
mut c2 := cmath.complex(2.152033,0.950547)
mut result := c1.arg()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq('0.950547')
c1 = cmath.complex(-3,4)
c2 = cmath.complex(1.609438,2.214297)
result = c1.arg()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq('2.214297')
c1 = cmath.complex(-1,-2)
c2 = cmath.complex(0.804719,-2.034444)
result = c1.arg()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq('-2.034444')
}
fn test_complex_log() {
// Tests were also verified on Wolfram Alpha
mut c1 := cmath.complex(5,7)
mut b1 := cmath.complex(-6,-2)
mut c2 := cmath.complex(0.232873,-1.413175)
mut result := c1.log(b1)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = cmath.complex(-3,4)
b1 = cmath.complex(3,-1)
c2 = cmath.complex(0.152198,-0.409312)
result = c1.log(b1)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = cmath.complex(-1,-2)
b1 = cmath.complex(0,9)
c2 = cmath.complex(-0.298243,1.197981)
result = c1.log(b1)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_cpow() {
// Tests were also verified on Wolfram Alpha
mut c1 := cmath.complex(5,7)
mut r1 := cmath.complex(2,2)
mut c2 := cmath.complex(11.022341,-0.861785)
mut result := c1.cpow(r1)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = cmath.complex(-3,4)
r1 = cmath.complex(-4,-2)
c2 = cmath.complex(0.118303,0.063148)
result = c1.cpow(r1)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = cmath.complex(-1,-2)
r1 = cmath.complex(8,-9)
c2 = cmath.complex(-0.000000,0.000007)
result = c1.cpow(r1)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_sin() { fn test_complex_sin() {
// Tests were also verified on Wolfram Alpha // Tests were also verified on Wolfram Alpha
mut c1 := cmath.complex(5,7) mut c1 := cmath.complex(5,7)