mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
math: make consts and structs public
This commit is contained in:
@ -4,9 +4,9 @@
|
||||
|
||||
module complex
|
||||
|
||||
import math
|
||||
import math
|
||||
|
||||
struct Complex {
|
||||
pub struct Complex {
|
||||
pub:
|
||||
re f64
|
||||
im f64
|
||||
@ -17,7 +17,7 @@ pub fn complex(re f64, im f64) Complex {
|
||||
}
|
||||
|
||||
// To String method
|
||||
pub fn (c Complex) str() string {
|
||||
pub fn (c Complex) str() string {
|
||||
mut out := '$c.re'
|
||||
out += if c.im >= 0 {
|
||||
'+$c.im'
|
||||
@ -40,7 +40,7 @@ pub fn (c Complex) mod() f64 {
|
||||
|
||||
|
||||
// Complex Angle
|
||||
pub fn (c Complex) angle() f64 {
|
||||
pub fn (c Complex) angle() f64 {
|
||||
return math.atan2(c.im, c.re)
|
||||
}
|
||||
|
||||
@ -58,7 +58,7 @@ pub fn (c1 Complex) - (c2 Complex) Complex {
|
||||
// Currently Not Supported
|
||||
// pub fn (c1 Complex) * (c2 Complex) Complex {
|
||||
// return Complex{
|
||||
// (c1.re * c2.re) + ((c1.im * c2.im) * -1),
|
||||
// (c1.re * c2.re) + ((c1.im * c2.im) * -1),
|
||||
// (c1.re * c2.im) + (c1.im * c2.re)
|
||||
// }
|
||||
// }
|
||||
@ -67,8 +67,8 @@ pub fn (c1 Complex) - (c2 Complex) Complex {
|
||||
// Currently Not Supported
|
||||
// pub fn (c1 Complex) / (c2 Complex) Complex {
|
||||
// denom := (c2.re * c2.re) + (c2.im * c2.im)
|
||||
// return Complex {
|
||||
// ((c1.re * c2.re) + ((c1.im * -c2.im) * -1))/denom,
|
||||
// return Complex {
|
||||
// ((c1.re * c2.re) + ((c1.im * -c2.im) * -1))/denom,
|
||||
// ((c1.re * -c2.im) + (c1.im * c2.re))/denom
|
||||
// }
|
||||
// }
|
||||
@ -86,7 +86,7 @@ pub fn (c1 Complex) subtract(c2 Complex) Complex {
|
||||
// Complex Multiplication c1.multiply(c2)
|
||||
pub fn (c1 Complex) multiply(c2 Complex) Complex {
|
||||
return Complex{
|
||||
(c1.re * c2.re) + ((c1.im * c2.im) * -1),
|
||||
(c1.re * c2.re) + ((c1.im * c2.im) * -1),
|
||||
(c1.re * c2.im) + (c1.im * c2.re)
|
||||
}
|
||||
}
|
||||
@ -94,8 +94,8 @@ pub fn (c1 Complex) multiply(c2 Complex) Complex {
|
||||
// Complex Division c1.divide(c2)
|
||||
pub fn (c1 Complex) divide(c2 Complex) Complex {
|
||||
denom := (c2.re * c2.re) + (c2.im * c2.im)
|
||||
return Complex {
|
||||
((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom,
|
||||
return Complex {
|
||||
((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom,
|
||||
((c1.re * -c2.im) + (c1.im * c2.re)) / denom
|
||||
}
|
||||
}
|
||||
@ -106,7 +106,7 @@ pub fn (c Complex) conjugate() Complex{
|
||||
}
|
||||
|
||||
// Complex Additive Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx
|
||||
pub fn (c Complex) addinv() Complex {
|
||||
return Complex{-c.re, -c.im}
|
||||
@ -121,7 +121,7 @@ pub fn (c Complex) mulinv() Complex {
|
||||
-c.im / (c.re * c.re + c.im * c.im)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
@ -134,25 +134,25 @@ pub fn (c Complex) pow(n f64) Complex {
|
||||
}
|
||||
}
|
||||
|
||||
// Complex nth root
|
||||
// Complex nth root
|
||||
pub fn (c Complex) root(n f64) Complex {
|
||||
return c.pow(1.0 / n)
|
||||
}
|
||||
|
||||
// Complex Exponential
|
||||
// Using Euler's Identity
|
||||
// Using Euler's Identity
|
||||
// Based on
|
||||
// https://www.math.wisc.edu/~angenent/Free-Lecture-Notes/freecomplexnumbers.pdf
|
||||
pub fn (c Complex) exp() Complex {
|
||||
a := math.exp(c.re)
|
||||
return Complex {
|
||||
a * math.cos(c.im),
|
||||
a * math.cos(c.im),
|
||||
a * math.sin(c.im)
|
||||
}
|
||||
}
|
||||
|
||||
// Complex Natural Logarithm
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.chemistrylearning.com/logarithm-of-complex-number/
|
||||
pub fn (c Complex) ln() Complex {
|
||||
return Complex {
|
||||
@ -162,7 +162,7 @@ pub fn (c Complex) ln() Complex {
|
||||
}
|
||||
|
||||
// Complex Log Base Complex
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.milefoot.com/math/complex/summaryops.htm
|
||||
pub fn (c Complex) log(base Complex) Complex {
|
||||
return base.ln().divide(c.ln())
|
||||
@ -184,7 +184,7 @@ pub fn (c Complex) cpow(p Complex) Complex {
|
||||
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.cos(d),
|
||||
t1 * math.sin(d)
|
||||
}
|
||||
}
|
||||
@ -238,7 +238,7 @@ pub fn (c Complex) csc() Complex {
|
||||
}
|
||||
|
||||
// Complex Arc Sin / Sin Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.milefoot.com/math/complex/summaryops.htm
|
||||
pub fn (c Complex) asin() Complex {
|
||||
return complex(0,-1).multiply(
|
||||
@ -254,7 +254,7 @@ pub fn (c Complex) asin() Complex {
|
||||
}
|
||||
|
||||
// Complex Arc Consine / Consine Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.milefoot.com/math/complex/summaryops.htm
|
||||
pub fn (c Complex) acos() Complex {
|
||||
return complex(0,-1).multiply(
|
||||
@ -271,7 +271,7 @@ pub fn (c Complex) acos() Complex {
|
||||
}
|
||||
|
||||
// Complex Arc Tangent / Tangent Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.milefoot.com/math/complex/summaryops.htm
|
||||
pub fn (c Complex) atan() Complex {
|
||||
i := complex(0,1)
|
||||
@ -285,21 +285,21 @@ pub fn (c Complex) atan() Complex {
|
||||
}
|
||||
|
||||
// Complex Arc Cotangent / Cotangent Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse_Functions.htm
|
||||
pub fn (c Complex) acot() Complex {
|
||||
return complex(1,0).divide(c).atan()
|
||||
}
|
||||
|
||||
// Complex Arc Secant / Secant Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse_Functions.htm
|
||||
pub fn (c Complex) asec() Complex {
|
||||
return complex(1,0).divide(c).acos()
|
||||
}
|
||||
|
||||
// Complex Arc Cosecant / Cosecant Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse_Functions.htm
|
||||
pub fn (c Complex) acsc() Complex {
|
||||
return complex(1,0).divide(c).asin()
|
||||
@ -354,7 +354,7 @@ pub fn (c Complex) csch() Complex {
|
||||
}
|
||||
|
||||
// Complex Hyperbolic Arc Sin / Sin Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
||||
pub fn (c Complex) asinh() Complex {
|
||||
return c.add(
|
||||
@ -365,7 +365,7 @@ pub fn (c Complex) asinh() Complex {
|
||||
}
|
||||
|
||||
// Complex Hyperbolic Arc Consine / Consine Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
||||
pub fn (c Complex) acosh() Complex {
|
||||
if(c.re > 1) {
|
||||
@ -389,7 +389,7 @@ pub fn (c Complex) acosh() Complex {
|
||||
}
|
||||
|
||||
// Complex Hyperbolic Arc Tangent / Tangent Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
||||
pub fn (c Complex) atanh() Complex {
|
||||
one := complex(1,0)
|
||||
@ -419,7 +419,7 @@ pub fn (c Complex) atanh() Complex {
|
||||
}
|
||||
|
||||
// Complex Hyperbolic Arc Cotangent / Cotangent Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
||||
pub fn (c Complex) acoth() Complex {
|
||||
one := complex(1,0)
|
||||
@ -449,7 +449,7 @@ pub fn (c Complex) acoth() Complex {
|
||||
}
|
||||
|
||||
// Complex Hyperbolic Arc Secant / Secant Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// 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 {
|
||||
@ -457,7 +457,7 @@ pub fn (c Complex) acoth() Complex {
|
||||
// if(c.re < -1.0) {
|
||||
// return one.subtract(
|
||||
// one.subtract(
|
||||
// c.pow(2)
|
||||
// c.pow(2)
|
||||
// )
|
||||
// .root(2)
|
||||
// )
|
||||
@ -467,7 +467,7 @@ pub fn (c Complex) acoth() Complex {
|
||||
// else {
|
||||
// return one.add(
|
||||
// one.subtract(
|
||||
// c.pow(2)
|
||||
// c.pow(2)
|
||||
// )
|
||||
// .root(2)
|
||||
// )
|
||||
@ -477,14 +477,14 @@ pub fn (c Complex) acoth() Complex {
|
||||
// }
|
||||
|
||||
// Complex Hyperbolic Arc Cosecant / Cosecant Inverse
|
||||
// Based on
|
||||
// Based on
|
||||
// http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm
|
||||
pub fn (c Complex) acsch() Complex {
|
||||
one := complex(1,0)
|
||||
if(c.re < 0) {
|
||||
return one.subtract(
|
||||
one.add(
|
||||
c.pow(2)
|
||||
c.pow(2)
|
||||
)
|
||||
.root(2)
|
||||
)
|
||||
@ -493,7 +493,7 @@ pub fn (c Complex) acsch() Complex {
|
||||
} else {
|
||||
return one.add(
|
||||
one.add(
|
||||
c.pow(2)
|
||||
c.pow(2)
|
||||
)
|
||||
.root(2)
|
||||
)
|
||||
|
Reference in New Issue
Block a user