mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
math: update complex operators for multiplication and division
This commit is contained in:
parent
a0f32f5c29
commit
89d3075f88
@ -55,23 +55,21 @@ pub fn (c1 Complex) - (c2 Complex) Complex {
|
||||
}
|
||||
|
||||
// Complex Multiplication c1 * c2
|
||||
// Currently Not Supported
|
||||
// pub fn (c1 Complex) * (c2 Complex) Complex {
|
||||
// return Complex{
|
||||
// (c1.re * c2.re) + ((c1.im * c2.im) * -1),
|
||||
// (c1.re * c2.im) + (c1.im * c2.re)
|
||||
// }
|
||||
// }
|
||||
pub fn (c1 Complex) * (c2 Complex) Complex {
|
||||
return Complex{
|
||||
(c1.re * c2.re) + ((c1.im * c2.im) * -1),
|
||||
(c1.re * c2.im) + (c1.im * c2.re)
|
||||
}
|
||||
}
|
||||
|
||||
// Complex Division c1 / c2
|
||||
// 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,
|
||||
// ((c1.re * -c2.im) + (c1.im * c2.re))/denom
|
||||
// }
|
||||
// }
|
||||
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,
|
||||
((c1.re * -c2.im) + (c1.im * c2.re))/denom
|
||||
}
|
||||
}
|
||||
|
||||
// Complex Addition c1.add(c2)
|
||||
pub fn (c1 Complex) add(c2 Complex) Complex {
|
||||
|
@ -48,19 +48,19 @@ fn test_complex_multiplication() {
|
||||
// https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers
|
||||
mut c1 := cmplx.complex(1,2)
|
||||
mut c2 := cmplx.complex(1,-4)
|
||||
mut result := c1.multiply(c2)
|
||||
mut result := c1 * c2
|
||||
assert result.equals(cmplx.complex(9,-2))
|
||||
c1 = cmplx.complex(-4,-4)
|
||||
c2 = cmplx.complex(-5,-3)
|
||||
result = c1.multiply(c2)
|
||||
result = c1 * c2
|
||||
assert result.equals(cmplx.complex(8,32))
|
||||
c1 = cmplx.complex(4,4)
|
||||
c2 = cmplx.complex(-2,-5)
|
||||
result = c1.multiply(c2)
|
||||
result = c1 * c2
|
||||
assert result.equals(cmplx.complex(12,-28))
|
||||
c1 = cmplx.complex(2,-2)
|
||||
c2 = cmplx.complex(4,-4)
|
||||
result = c1.multiply(c2)
|
||||
result = c1 * c2
|
||||
assert result.equals(cmplx.complex(0,-16))
|
||||
}
|
||||
|
||||
@ -69,19 +69,19 @@ fn test_complex_division() {
|
||||
// https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers
|
||||
mut c1 := cmplx.complex(-9,-6)
|
||||
mut c2 := cmplx.complex(-3,-2)
|
||||
mut result := c1.divide(c2)
|
||||
mut result := c1 / c2
|
||||
assert result.equals(cmplx.complex(3,0))
|
||||
c1 = cmplx.complex(-23,11)
|
||||
c2 = cmplx.complex(5,1)
|
||||
result = c1.divide(c2)
|
||||
result = c1 / c2
|
||||
assert result.equals(cmplx.complex(-4,3))
|
||||
c1 = cmplx.complex(8,-2)
|
||||
c2 = cmplx.complex(-4,1)
|
||||
result = c1.divide(c2)
|
||||
result = c1 / c2
|
||||
assert result.equals(cmplx.complex(-2,0))
|
||||
c1 = cmplx.complex(11,24)
|
||||
c2 = cmplx.complex(-4,-1)
|
||||
result = c1.divide(c2)
|
||||
result = c1 / c2
|
||||
assert result.equals(cmplx.complex(-4,-5))
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user