mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
complex, fraction: simplify and format source code
This commit is contained in:
parent
9907f07602
commit
00ea112b66
@ -9,8 +9,8 @@ struct Complex {
|
|||||||
im f64
|
im f64
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn complex(re f64,im f64) Complex {
|
pub fn complex(re f64, im f64) Complex {
|
||||||
return Complex{re,im}
|
return Complex{re, im}
|
||||||
}
|
}
|
||||||
|
|
||||||
// To String method
|
// To String method
|
||||||
@ -26,10 +26,15 @@ pub fn (c Complex) str() string {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Absolute value
|
// Complex Modulus value
|
||||||
|
// mod() and abs() return the same
|
||||||
pub fn (c Complex) abs() f64 {
|
pub fn (c Complex) abs() f64 {
|
||||||
return C.hypot(c.re,c.im)
|
return C.hypot(c.re, c.im)
|
||||||
}
|
}
|
||||||
|
pub fn (c Complex) mod() f64 {
|
||||||
|
return c.abs()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Complex Angle
|
// Complex Angle
|
||||||
pub fn (c Complex) angle() f64 {
|
pub fn (c Complex) angle() f64 {
|
||||||
@ -38,12 +43,12 @@ pub fn (c Complex) angle() f64 {
|
|||||||
|
|
||||||
// Complex Addition c1 + c2
|
// Complex Addition c1 + c2
|
||||||
pub fn (c1 Complex) + (c2 Complex) Complex {
|
pub fn (c1 Complex) + (c2 Complex) Complex {
|
||||||
return Complex{c1.re+c2.re,c1.im+c2.im}
|
return Complex{c1.re + c2.re, c1.im + c2.im}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Substraction c1 - c2
|
// Complex Substraction c1 - c2
|
||||||
pub fn (c1 Complex) - (c2 Complex) Complex {
|
pub fn (c1 Complex) - (c2 Complex) Complex {
|
||||||
return Complex{c1.re-c2.re,c1.im-c2.im}
|
return Complex{c1.re - c2.re, c1.im - c2.im}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Multiplication c1 * c2
|
// Complex Multiplication c1 * c2
|
||||||
@ -87,76 +92,69 @@ pub fn (c1 Complex) multiply(c2 Complex) Complex {
|
|||||||
pub fn (c1 Complex) divide(c2 Complex) Complex {
|
pub fn (c1 Complex) divide(c2 Complex) Complex {
|
||||||
denom := (c2.re * c2.re) + (c2.im * c2.im)
|
denom := (c2.re * c2.re) + (c2.im * c2.im)
|
||||||
return Complex {
|
return Complex {
|
||||||
((c1.re * c2.re) + ((c1.im * -c2.im) * -1))/denom,
|
((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom,
|
||||||
((c1.re * -c2.im) + (c1.im * c2.re))/denom
|
((c1.re * -c2.im) + (c1.im * c2.re)) / denom
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Conjugate
|
// Complex Conjugate
|
||||||
pub fn (c1 Complex) conjugate() Complex{
|
pub fn (c Complex) conjugate() Complex{
|
||||||
return Complex{c1.re,-c1.im}
|
return Complex{c.re, -c.im}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Additive Inverse
|
// Complex Additive Inverse
|
||||||
// Based on
|
// Based on
|
||||||
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx
|
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx
|
||||||
pub fn (c1 Complex) addinv() Complex {
|
pub fn (c Complex) addinv() Complex {
|
||||||
return Complex{-c1.re,-c1.im}
|
return Complex{-c.re, -c.im}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Multiplicative Inverse
|
// Complex Multiplicative Inverse
|
||||||
// Based on
|
// Based on
|
||||||
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx
|
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx
|
||||||
pub fn (c1 Complex) mulinv() Complex {
|
pub fn (c Complex) mulinv() Complex {
|
||||||
return Complex {
|
return Complex {
|
||||||
c1.re / (pow(c1.re,2) + pow(c1.im,2)),
|
c.re / (c.re * c.re + c.im * c.im),
|
||||||
-c1.im / (pow(c1.re,2) + pow(c1.im,2))
|
-c.im / (c.re * c.re + c.im * c.im)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Mod or Absolute
|
|
||||||
// Based on
|
|
||||||
// http://tutorial.math.lamar.edu/Extras/ComplexPrimer/ConjugateModulus.aspx
|
|
||||||
pub fn (c1 Complex) mod() f64 {
|
|
||||||
return sqrt(pow(c1.re,2)+pow(c1.im,2))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Complex Power
|
// Complex Power
|
||||||
// Based on
|
// 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
|
// https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers/multiplying-and-dividing-complex-numbers-in-polar-form/a/complex-number-polar-form-review
|
||||||
pub fn (c1 Complex) pow(n f64) Complex {
|
pub fn (c Complex) pow(n f64) Complex {
|
||||||
r := pow(c1.mod(),n)
|
r := pow(c.abs(), n)
|
||||||
angle := atan2(c1.im,c1.re)
|
angle := c.angle()
|
||||||
return Complex {
|
return Complex {
|
||||||
r * cos(n*angle),
|
r * cos(n * angle),
|
||||||
r * sin(n*angle)
|
r * sin(n * angle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex nth root
|
// Complex nth root
|
||||||
pub fn (c1 Complex) root(n f64) Complex {
|
pub fn (c Complex) root(n f64) Complex {
|
||||||
return c1.pow(1.0/n)
|
return c.pow(1.0 / n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Exponential
|
// Complex Exponential
|
||||||
// Using Euler's Identity
|
// Using Euler's Identity
|
||||||
// Based on
|
// Based on
|
||||||
// https://www.math.wisc.edu/~angenent/Free-Lecture-Notes/freecomplexnumbers.pdf
|
// https://www.math.wisc.edu/~angenent/Free-Lecture-Notes/freecomplexnumbers.pdf
|
||||||
pub fn (c1 Complex) exp() Complex {
|
pub fn (c Complex) exp() Complex {
|
||||||
a := exp(c1.re)
|
a := exp(c.re)
|
||||||
return Complex {
|
return Complex {
|
||||||
a * cos(c1.im),
|
a * cos(c.im),
|
||||||
a * sin(c1.im)
|
a * sin(c.im)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex Natural Logarithm
|
// Complex Natural Logarithm
|
||||||
// Based on
|
// Based on
|
||||||
// http://www.chemistrylearning.com/logarithm-of-complex-number/
|
// http://www.chemistrylearning.com/logarithm-of-complex-number/
|
||||||
pub fn (c1 Complex) ln() Complex {
|
pub fn (c Complex) ln() Complex {
|
||||||
return Complex {
|
return Complex {
|
||||||
log(c1.mod()),
|
log(c.abs()),
|
||||||
atan2(c1.im,c1.re)
|
c.angle()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,9 +11,9 @@ struct Fraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A factory function for creating a Fraction, adds a boundary condition
|
// A factory function for creating a Fraction, adds a boundary condition
|
||||||
pub fn fraction(n i64,d i64) Fraction{
|
pub fn fraction(n i64, d i64) Fraction{
|
||||||
if d != 0 {
|
if d != 0 {
|
||||||
return Fraction{n,d}
|
return Fraction{n, d}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
panic('Denominator cannot be zero')
|
panic('Denominator cannot be zero')
|
||||||
@ -28,20 +28,20 @@ pub fn (f Fraction) str() string {
|
|||||||
// Fraction add using operator overloading
|
// Fraction add using operator overloading
|
||||||
pub fn (f1 Fraction) + (f2 Fraction) Fraction {
|
pub fn (f1 Fraction) + (f2 Fraction) Fraction {
|
||||||
if f1.d == f2.d {
|
if f1.d == f2.d {
|
||||||
return Fraction{f1.n + f2.n,f1.d}
|
return Fraction{f1.n + f2.n, f1.d}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return Fraction{(f1.n * f2.d) + (f2.n * f1.d),f1.d * f2.d}
|
return Fraction{(f1.n * f2.d) + (f2.n * f1.d), f1.d * f2.d}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction substract using operator overloading
|
// Fraction substract using operator overloading
|
||||||
pub fn (f1 Fraction) - (f2 Fraction) Fraction {
|
pub fn (f1 Fraction) - (f2 Fraction) Fraction {
|
||||||
if f1.d == f2.d {
|
if f1.d == f2.d {
|
||||||
return Fraction{f1.n - f2.n,f1.d}
|
return Fraction{f1.n - f2.n, f1.d}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return Fraction{(f1.n * f2.d) - (f2.n * f1.d),f1.d * f2.d}
|
return Fraction{(f1.n * f2.d) - (f2.n * f1.d), f1.d * f2.d}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,33 +67,33 @@ pub fn (f1 Fraction) subtract(f2 Fraction) Fraction {
|
|||||||
|
|
||||||
// Fraction multiply method
|
// Fraction multiply method
|
||||||
pub fn (f1 Fraction) multiply(f2 Fraction) Fraction {
|
pub fn (f1 Fraction) multiply(f2 Fraction) Fraction {
|
||||||
return Fraction{f1.n * f2.n,f1.d * f2.d}
|
return Fraction{f1.n * f2.n, f1.d * f2.d}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction divide method
|
// Fraction divide method
|
||||||
pub fn (f1 Fraction) divide(f2 Fraction) Fraction {
|
pub fn (f1 Fraction) divide(f2 Fraction) Fraction {
|
||||||
return Fraction{f1.n * f2.d,f1.d * f2.n}
|
return Fraction{f1.n * f2.d, f1.d * f2.n}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction reciprocal method
|
// Fraction reciprocal method
|
||||||
pub fn (f1 Fraction) reciprocal() Fraction {
|
pub fn (f1 Fraction) reciprocal() Fraction {
|
||||||
return Fraction{f1.d,f1.n}
|
return Fraction{f1.d, f1.n}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction method which gives greatest common divisor of numerator and denominator
|
// Fraction method which gives greatest common divisor of numerator and denominator
|
||||||
pub fn (f1 Fraction) gcd() i64 {
|
pub fn (f1 Fraction) gcd() i64 {
|
||||||
return gcd(f1.n,f1.d)
|
return gcd(f1.n, f1.d)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction method which reduces the fraction
|
// Fraction method which reduces the fraction
|
||||||
pub fn (f1 Fraction) reduce() Fraction {
|
pub fn (f1 Fraction) reduce() Fraction {
|
||||||
cf := gcd(f1.n,f1.d)
|
cf := gcd(f1.n, f1.d)
|
||||||
return Fraction{f1.n/cf,f1.d/cf}
|
return Fraction{f1.n / cf, f1.d / cf}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts Fraction to decimal
|
// Converts Fraction to decimal
|
||||||
pub fn (f1 Fraction) f64() f64 {
|
pub fn (f1 Fraction) f64() f64 {
|
||||||
return f64(f1.n)/f64(f1.d)
|
return f64(f1.n) / f64(f1.d)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compares two Fractions
|
// Compares two Fractions
|
||||||
@ -101,4 +101,4 @@ pub fn (f1 Fraction) equals(f2 Fraction) bool {
|
|||||||
r1 := f1.reduce()
|
r1 := f1.reduce()
|
||||||
r2 := f2.reduce()
|
r2 := f2.reduce()
|
||||||
return (r1.n == r2.n) && (r1.d == r2.d)
|
return (r1.n == r2.n) && (r1.d == r2.d)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user