2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-07-06 21:19:09 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-07-23 14:35:44 +03:00
|
|
|
module complex
|
2019-07-10 22:40:29 +03:00
|
|
|
|
2019-10-28 16:33:32 +03:00
|
|
|
import math
|
2019-07-06 21:19:09 +03:00
|
|
|
|
2019-10-28 16:33:32 +03:00
|
|
|
pub struct Complex {
|
2019-10-15 12:43:28 +03:00
|
|
|
pub:
|
2019-07-06 21:19:09 +03:00
|
|
|
re f64
|
|
|
|
im f64
|
|
|
|
}
|
|
|
|
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn complex(re f64, im f64) Complex {
|
|
|
|
return Complex{re, im}
|
2019-07-06 21:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// To String method
|
2019-10-28 16:33:32 +03:00
|
|
|
pub fn (c Complex) str() string {
|
2020-04-16 08:30:25 +03:00
|
|
|
mut out := '${c.re:f}'
|
2021-05-08 13:32:29 +03:00
|
|
|
out += if c.im >= 0 { '+${c.im:f}' } else { '${c.im:f}' }
|
2019-07-06 21:19:09 +03:00
|
|
|
out += 'i'
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-10 14:51:48 +03:00
|
|
|
// Complex Modulus value
|
|
|
|
// mod() and abs() return the same
|
2019-07-09 22:12:52 +03:00
|
|
|
pub fn (c Complex) abs() f64 {
|
2019-07-10 14:51:48 +03:00
|
|
|
return C.hypot(c.re, c.im)
|
2019-07-09 22:12:52 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn (c Complex) mod() f64 {
|
|
|
|
return c.abs()
|
|
|
|
}
|
|
|
|
|
2019-07-07 17:13:17 +03:00
|
|
|
// Complex Angle
|
2019-10-28 16:33:32 +03:00
|
|
|
pub fn (c Complex) angle() f64 {
|
2019-07-10 22:40:29 +03:00
|
|
|
return math.atan2(c.im, c.re)
|
2019-07-07 17:13:17 +03:00
|
|
|
}
|
|
|
|
|
2019-07-06 21:19:09 +03:00
|
|
|
// Complex Addition c1 + c2
|
|
|
|
pub fn (c1 Complex) + (c2 Complex) Complex {
|
2019-07-10 14:51:48 +03:00
|
|
|
return Complex{c1.re + c2.re, c1.im + c2.im}
|
2019-07-06 21:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Substraction c1 - c2
|
|
|
|
pub fn (c1 Complex) - (c2 Complex) Complex {
|
2019-07-10 14:51:48 +03:00
|
|
|
return Complex{c1.re - c2.re, c1.im - c2.im}
|
2019-07-06 21:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Multiplication c1 * c2
|
2019-12-30 07:24:30 +03:00
|
|
|
pub fn (c1 Complex) * (c2 Complex) Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{(c1.re * c2.re) + ((c1.im * c2.im) * -1), (c1.re * c2.im) + (c1.im * c2.re)}
|
2019-12-30 07:24:30 +03:00
|
|
|
}
|
2019-07-06 21:19:09 +03:00
|
|
|
|
|
|
|
// Complex Division c1 / c2
|
2019-12-30 07:24:30 +03:00
|
|
|
pub fn (c1 Complex) / (c2 Complex) Complex {
|
|
|
|
denom := (c2.re * c2.re) + (c2.im * c2.im)
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom, ((c1.re * -c2.im) +
|
|
|
|
(c1.im * c2.re)) / denom}
|
2019-12-30 07:24:30 +03:00
|
|
|
}
|
2019-07-06 21:19:09 +03:00
|
|
|
|
|
|
|
// Complex Addition c1.add(c2)
|
|
|
|
pub fn (c1 Complex) add(c2 Complex) Complex {
|
|
|
|
return c1 + c2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Subtraction c1.subtract(c2)
|
|
|
|
pub fn (c1 Complex) subtract(c2 Complex) Complex {
|
|
|
|
return c1 - c2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Multiplication c1.multiply(c2)
|
|
|
|
pub fn (c1 Complex) multiply(c2 Complex) Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{(c1.re * c2.re) + ((c1.im * c2.im) * -1), (c1.re * c2.im) + (c1.im * c2.re)}
|
2019-07-06 21:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Division c1.divide(c2)
|
|
|
|
pub fn (c1 Complex) divide(c2 Complex) Complex {
|
|
|
|
denom := (c2.re * c2.re) + (c2.im * c2.im)
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom, ((c1.re * -c2.im) +
|
|
|
|
(c1.im * c2.re)) / denom}
|
2019-07-06 21:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Conjugate
|
2021-05-08 13:32:29 +03:00
|
|
|
pub fn (c Complex) conjugate() Complex {
|
2019-07-10 14:51:48 +03:00
|
|
|
return Complex{c.re, -c.im}
|
2019-07-06 21:19:09 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 20:41:37 +03:00
|
|
|
// Complex Additive Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-08 20:41:37 +03:00
|
|
|
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn (c Complex) addinv() Complex {
|
|
|
|
return Complex{-c.re, -c.im}
|
2019-07-08 20:41:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Multiplicative Inverse
|
|
|
|
// Based on
|
|
|
|
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn (c Complex) mulinv() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{c.re / (c.re * c.re + c.im * c.im), -c.im / (c.re * c.re + c.im * c.im)}
|
2019-07-08 20:41:37 +03:00
|
|
|
}
|
2019-10-28 16:33:32 +03:00
|
|
|
|
2019-07-08 20:41:37 +03:00
|
|
|
// Complex Power
|
|
|
|
// Based on
|
|
|
|
// https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers/multiplying-and-dividing-complex-numbers-in-polar-form/a/complex-number-polar-form-review
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn (c Complex) pow(n f64) Complex {
|
2019-07-10 22:40:29 +03:00
|
|
|
r := math.pow(c.abs(), n)
|
2019-07-10 14:51:48 +03:00
|
|
|
angle := c.angle()
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{r * math.cos(n * angle), r * math.sin(n * angle)}
|
2019-07-08 20:41:37 +03:00
|
|
|
}
|
|
|
|
|
2019-10-28 16:33:32 +03:00
|
|
|
// Complex nth root
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn (c Complex) root(n f64) Complex {
|
|
|
|
return c.pow(1.0 / n)
|
2019-07-08 20:41:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Exponential
|
2019-10-28 16:33:32 +03:00
|
|
|
// Using Euler's Identity
|
2019-07-08 20:41:37 +03:00
|
|
|
// Based on
|
|
|
|
// https://www.math.wisc.edu/~angenent/Free-Lecture-Notes/freecomplexnumbers.pdf
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn (c Complex) exp() Complex {
|
2019-07-10 22:40:29 +03:00
|
|
|
a := math.exp(c.re)
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{a * math.cos(c.im), a * math.sin(c.im)}
|
2019-07-08 20:41:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Natural Logarithm
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-08 20:41:37 +03:00
|
|
|
// http://www.chemistrylearning.com/logarithm-of-complex-number/
|
2019-07-10 14:51:48 +03:00
|
|
|
pub fn (c Complex) ln() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{math.log(c.abs()), c.angle()}
|
2019-07-08 20:41:37 +03:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:14:20 +03:00
|
|
|
// Complex Log Base Complex
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-15 15:14:20 +03:00
|
|
|
// 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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
return math.atan2(c.im, c.re)
|
2019-07-15 15:14:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex raised to Complex Power
|
|
|
|
// Based on
|
|
|
|
// http://mathworld.wolfram.com/ComplexExponentiation.html
|
|
|
|
pub fn (c Complex) cpow(p Complex) Complex {
|
|
|
|
a := c.arg()
|
2021-05-08 13:32:29 +03:00
|
|
|
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)}
|
2019-07-15 15:14:20 +03:00
|
|
|
}
|
|
|
|
|
2019-07-10 20:28:19 +03:00
|
|
|
// Complex Sin
|
|
|
|
// Based on
|
|
|
|
// http://www.milefoot.com/math/complex/functionsofi.htm
|
|
|
|
pub fn (c Complex) sin() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{math.sin(c.re) * math.cosh(c.im), math.cos(c.re) * math.sinh(c.im)}
|
2019-07-10 20:28:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Cosine
|
|
|
|
// Based on
|
|
|
|
// http://www.milefoot.com/math/complex/functionsofi.htm
|
|
|
|
pub fn (c Complex) cos() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{math.cos(c.re) * math.cosh(c.im), -(math.sin(c.re) * math.sinh(c.im))}
|
2019-07-10 20:28:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Tangent
|
|
|
|
// Based on
|
|
|
|
// http://www.milefoot.com/math/complex/functionsofi.htm
|
|
|
|
pub fn (c Complex) tan() Complex {
|
|
|
|
return c.sin().divide(c.cos())
|
|
|
|
}
|
|
|
|
|
2019-07-12 13:56:21 +03:00
|
|
|
// Complex Cotangent
|
|
|
|
// Based on
|
|
|
|
// http://www.suitcaseofdreams.net/Trigonometric_Functions.htm
|
|
|
|
pub fn (c Complex) cot() Complex {
|
|
|
|
return c.cos().divide(c.sin())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Secant
|
|
|
|
// Based on
|
|
|
|
// http://www.suitcaseofdreams.net/Trigonometric_Functions.htm
|
|
|
|
pub fn (c Complex) sec() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1, 0).divide(c.cos())
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Cosecant
|
|
|
|
// Based on
|
|
|
|
// http://www.suitcaseofdreams.net/Trigonometric_Functions.htm
|
|
|
|
pub fn (c Complex) csc() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1, 0).divide(c.sin())
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
|
2019-07-11 16:35:06 +03:00
|
|
|
// Complex Arc Sin / Sin Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-11 16:35:06 +03:00
|
|
|
// http://www.milefoot.com/math/complex/summaryops.htm
|
|
|
|
pub fn (c Complex) asin() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(0, -1).multiply(complex(0, 1).multiply(c).add(complex(1, 0).subtract(c.pow(2)).root(2)).ln())
|
2019-07-11 16:35:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Arc Consine / Consine Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-11 16:35:06 +03:00
|
|
|
// http://www.milefoot.com/math/complex/summaryops.htm
|
|
|
|
pub fn (c Complex) acos() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(0, -1).multiply(c.add(complex(0, 1).multiply(complex(1, 0).subtract(c.pow(2)).root(2))).ln())
|
2019-07-11 16:35:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Arc Tangent / Tangent Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-11 16:35:06 +03:00
|
|
|
// http://www.milefoot.com/math/complex/summaryops.htm
|
|
|
|
pub fn (c Complex) atan() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
i := complex(0, 1)
|
|
|
|
return complex(0, 1.0 / 2).multiply(i.add(c).divide(i.subtract(c)).ln())
|
2019-07-11 16:35:06 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 13:56:21 +03:00
|
|
|
// Complex Arc Cotangent / Cotangent Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-12 13:56:21 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse_Functions.htm
|
|
|
|
pub fn (c Complex) acot() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1, 0).divide(c).atan()
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Arc Secant / Secant Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-12 13:56:21 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse_Functions.htm
|
|
|
|
pub fn (c Complex) asec() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1, 0).divide(c).acos()
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Arc Cosecant / Cosecant Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-12 13:56:21 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse_Functions.htm
|
|
|
|
pub fn (c Complex) acsc() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1, 0).divide(c).asin()
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
|
2019-07-10 20:28:19 +03:00
|
|
|
// Complex Hyperbolic Sin
|
|
|
|
// Based on
|
|
|
|
// http://www.milefoot.com/math/complex/functionsofi.htm
|
|
|
|
pub fn (c Complex) sinh() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{math.cos(c.im) * math.sinh(c.re), math.sin(c.im) * math.cosh(c.re)}
|
2019-07-10 20:28:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Hyperbolic Cosine
|
|
|
|
// Based on
|
|
|
|
// http://www.milefoot.com/math/complex/functionsofi.htm
|
|
|
|
pub fn (c Complex) cosh() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return Complex{math.cos(c.im) * math.cosh(c.re), math.sin(c.im) * math.sinh(c.re)}
|
2019-07-10 20:28:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2019-07-12 13:56:21 +03:00
|
|
|
// Complex Hyperbolic Cotangent
|
|
|
|
// Based on
|
|
|
|
// http://www.suitcaseofdreams.net/Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) coth() Complex {
|
|
|
|
return c.cosh().divide(c.sinh())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Hyperbolic Secant
|
|
|
|
// Based on
|
|
|
|
// http://www.suitcaseofdreams.net/Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) sech() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1, 0).divide(c.cosh())
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Hyperbolic Cosecant
|
|
|
|
// Based on
|
|
|
|
// http://www.suitcaseofdreams.net/Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) csch() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1, 0).divide(c.sinh())
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
|
2019-07-11 16:35:06 +03:00
|
|
|
// Complex Hyperbolic Arc Sin / Sin Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-11 16:35:06 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) asinh() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
return c.add(c.pow(2).add(complex(1, 0)).root(2)).ln()
|
2019-07-11 16:35:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Hyperbolic Arc Consine / Consine Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-11 16:35:06 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) acosh() Complex {
|
2020-04-08 23:12:42 +03:00
|
|
|
if c.re > 1 {
|
2021-05-08 13:32:29 +03:00
|
|
|
return c.add(c.pow(2).subtract(complex(1, 0)).root(2)).ln()
|
|
|
|
} else {
|
|
|
|
one := complex(1, 0)
|
|
|
|
return c.add(c.add(one).root(2).multiply(c.subtract(one).root(2))).ln()
|
2019-07-11 16:35:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Hyperbolic Arc Tangent / Tangent Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-11 16:35:06 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) atanh() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
one := complex(1, 0)
|
2020-04-08 23:12:42 +03:00
|
|
|
if c.re < 1 {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1.0 / 2, 0).multiply(one.add(c).divide(one.subtract(c)).ln())
|
|
|
|
} else {
|
|
|
|
return complex(1.0 / 2, 0).multiply(one.add(c).ln().subtract(one.subtract(c).ln()))
|
2019-07-11 16:35:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 13:56:21 +03:00
|
|
|
// Complex Hyperbolic Arc Cotangent / Cotangent Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-12 13:56:21 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) acoth() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
one := complex(1, 0)
|
2020-04-08 23:12:42 +03:00
|
|
|
if c.re < 0 || c.re > 1 {
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1.0 / 2, 0).multiply(c.add(one).divide(c.subtract(one)).ln())
|
|
|
|
} else {
|
2019-07-12 13:56:21 +03:00
|
|
|
div := one.divide(c)
|
2021-05-08 13:32:29 +03:00
|
|
|
return complex(1.0 / 2, 0).multiply(one.add(div).ln().subtract(one.subtract(div).ln()))
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Hyperbolic Arc Secant / Secant Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-12 13:56:21 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
|
|
|
// For certain scenarios, Result mismatch in crossverification with Wolfram Alpha - analysis pending
|
|
|
|
// pub fn (c Complex) asech() Complex {
|
|
|
|
// one := complex(1,0)
|
2021-05-08 13:32:29 +03:00
|
|
|
// if(c.re < -1.0) {
|
|
|
|
// return one.subtract(
|
|
|
|
// one.subtract(
|
|
|
|
// c.pow(2)
|
|
|
|
// )
|
|
|
|
// .root(2)
|
|
|
|
// )
|
|
|
|
// .divide(c)
|
|
|
|
// .ln()
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// return one.add(
|
|
|
|
// one.subtract(
|
|
|
|
// c.pow(2)
|
|
|
|
// )
|
|
|
|
// .root(2)
|
|
|
|
// )
|
|
|
|
// .divide(c)
|
|
|
|
// .ln()
|
|
|
|
// }
|
2019-07-12 13:56:21 +03:00
|
|
|
// }
|
|
|
|
|
|
|
|
// Complex Hyperbolic Arc Cosecant / Cosecant Inverse
|
2019-10-28 16:33:32 +03:00
|
|
|
// Based on
|
2019-07-12 13:56:21 +03:00
|
|
|
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
|
|
|
pub fn (c Complex) acsch() Complex {
|
2021-05-08 13:32:29 +03:00
|
|
|
one := complex(1, 0)
|
2020-04-08 23:12:42 +03:00
|
|
|
if c.re < 0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
return one.subtract(one.add(c.pow(2)).root(2)).divide(c).ln()
|
2019-08-08 10:49:56 +03:00
|
|
|
} else {
|
2021-05-08 13:32:29 +03:00
|
|
|
return one.add(one.add(c.pow(2)).root(2)).divide(c).ln()
|
2019-07-12 13:56:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 21:19:09 +03:00
|
|
|
// Complex Equals
|
|
|
|
pub fn (c1 Complex) equals(c2 Complex) bool {
|
|
|
|
return (c1.re == c2.re) && (c1.im == c2.im)
|
2019-07-16 18:59:07 +03:00
|
|
|
}
|