math: added complex trig operations

pull/1082/head
Archan Patkar 2019-07-10 22:58:19 +05:30 committed by Alexander Medvednikov
parent 1b09e37a80
commit 69d2db0f1e
2 changed files with 168 additions and 0 deletions

View File

@ -158,6 +158,60 @@ pub fn (c Complex) ln() Complex {
}
}
// Complex Sin
// Based on
// http://www.milefoot.com/math/complex/functionsofi.htm
pub fn (c Complex) sin() Complex {
return Complex{
sin(c.re) * cosh(c.im),
cos(c.re) * sinh(c.im)
}
}
// Complex Cosine
// Based on
// http://www.milefoot.com/math/complex/functionsofi.htm
pub fn (c Complex) cos() Complex {
return Complex{
cos(c.re) * cosh(c.im),
-(sin(c.re) * sinh(c.im))
}
}
// Complex Tangent
// Based on
// http://www.milefoot.com/math/complex/functionsofi.htm
pub fn (c Complex) tan() Complex {
return c.sin().divide(c.cos())
}
// Complex Hyperbolic Sin
// Based on
// http://www.milefoot.com/math/complex/functionsofi.htm
pub fn (c Complex) sinh() Complex {
return Complex{
cos(c.im) * sinh(c.re),
sin(c.im) * cosh(c.re)
}
}
// Complex Hyperbolic Cosine
// Based on
// http://www.milefoot.com/math/complex/functionsofi.htm
pub fn (c Complex) cosh() Complex {
return Complex{
cos(c.im) * cosh(c.re),
sin(c.im) * sinh(c.re)
}
}
// Complex Hyperbolic Tangent
// Based on
// http://www.milefoot.com/math/complex/functionsofi.htm
pub fn (c Complex) tanh() Complex {
return c.sinh().divide(c.cosh())
}
// Complex Equals
pub fn (c1 Complex) equals(c2 Complex) bool {
return (c1.re == c2.re) && (c1.im == c2.im)

View File

@ -252,3 +252,117 @@ fn test_complex_ln() {
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_sin() {
// Tests were also verified on Wolfram Alpha
mut c1 := math.complex(5,7)
mut c2 := math.complex(-525.794515,155.536550)
mut result := c1.sin()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-3,4)
c2 = math.complex(-3.853738,-27.016813)
result = c1.sin()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-1,-2)
c2 = math.complex(-3.165779,-1.959601)
result = c1.sin()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_cos() {
// Tests were also verified on Wolfram Alpha
mut c1 := math.complex(5,7)
mut c2 := math.complex(155.536809,525.793641)
mut result := c1.cos()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-3,4)
c2 = math.complex(-27.034946,3.851153)
result = c1.cos()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-1,-2)
c2 = math.complex(2.032723,-3.051898)
result = c1.cos()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_tan() {
// Tests were also verified on Wolfram Alpha
mut c1 := math.complex(5,7)
mut c2 := math.complex(-0.000001,1.000001)
mut result := c1.tan()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-3,4)
c2 = math.complex(0.000187,0.999356)
result = c1.tan()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-1,-2)
c2 = math.complex(-0.033813,-1.014794)
result = c1.tan()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_sinh() {
// Tests were also verified on Wolfram Alpha
mut c1 := math.complex(5,7)
mut c2 := math.complex(55.941968,48.754942)
mut result := c1.sinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-3,4)
c2 = math.complex(6.548120,-7.619232)
result = c1.sinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-1,-2)
c2 = math.complex(0.489056,-1.403119)
result = c1.sinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_cosh() {
// Tests were also verified on Wolfram Alpha
mut c1 := math.complex(5,7)
mut c2 := math.complex(55.947047,48.750515)
mut result := c1.cosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-3,4)
c2 = math.complex(-6.580663,7.581553)
result = c1.cosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-1,-2)
c2 = math.complex(-0.642148,1.068607)
result = c1.cosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}
fn test_complex_tanh() {
// Tests were also verified on Wolfram Alpha
mut c1 := math.complex(5,7)
mut c2 := math.complex(0.999988,0.000090)
mut result := c1.tanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-3,4)
c2 = math.complex(-1.000710,0.004908)
result = c1.tanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
c1 = math.complex(-1,-2)
c2 = math.complex(-1.166736,0.243458)
result = c1.tanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str())
}