math: basic complex number support with tests
							parent
							
								
									758267254d
								
							
						
					
					
						commit
						818f8252f6
					
				|  | @ -0,0 +1,93 @@ | |||
| // Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
 | ||||
| // Use of this source code is governed by an MIT license
 | ||||
| // that can be found in the LICENSE file.
 | ||||
| 
 | ||||
| module math | ||||
| 
 | ||||
| struct Complex { | ||||
| 	re f64 | ||||
| 	im f64 | ||||
| } | ||||
| 
 | ||||
| pub fn complex(re f64,im f64) Complex { | ||||
| 	return Complex{re,im} | ||||
| } | ||||
| 
 | ||||
| // To String method
 | ||||
| pub fn (c Complex) str() string {  | ||||
| 	mut out := '$c.re' | ||||
| 	out += if c.im >= 0 { | ||||
| 		'+$c.im' | ||||
| 	} | ||||
| 	else { | ||||
| 		'$c.im' | ||||
| 	} | ||||
| 	out += 'i' | ||||
| 	return out | ||||
| } | ||||
| 
 | ||||
| // Complex Addition c1 + c2
 | ||||
| pub fn (c1 Complex) + (c2 Complex) Complex { | ||||
| 	return Complex{c1.re+c2.re,c1.im+c2.im} | ||||
| } | ||||
| 
 | ||||
| // Complex Substraction c1 - c2
 | ||||
| pub fn (c1 Complex) - (c2 Complex) Complex { | ||||
| 	return Complex{c1.re-c2.re,c1.im-c2.im} | ||||
| } | ||||
| 
 | ||||
| // 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)
 | ||||
| // 	}
 | ||||
| // }
 | ||||
| 
 | ||||
| // 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
 | ||||
| // 	}
 | ||||
| // }
 | ||||
| 
 | ||||
| // 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 { | ||||
| 	return Complex{ | ||||
| 		(c1.re * c2.re) + ((c1.im * c2.im) * -1),  | ||||
| 		(c1.re * c2.im) + (c1.im * c2.re) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // 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,  | ||||
| 		((c1.re * -c2.im) + (c1.im * c2.re))/denom | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // Complex Conjugate
 | ||||
| pub fn (c1 Complex) conjugate() Complex{ | ||||
| 	return Complex{c1.re,-c1.im} | ||||
| } | ||||
| 
 | ||||
| // Complex Equals
 | ||||
| pub fn (c1 Complex) equals(c2 Complex) bool { | ||||
| 	return (c1.re == c2.re) && (c1.im == c2.im) | ||||
| } | ||||
|  | @ -0,0 +1,104 @@ | |||
| import math | ||||
| 
 | ||||
| // Tests are based on and verified from practice examples of Khan Academy 
 | ||||
| // https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers
 | ||||
| 
 | ||||
| fn test_complex_addition() { | ||||
| 	mut c1 := math.complex(0,-10) | ||||
| 	mut c2 := math.complex(-40,8) | ||||
| 	mut result := c1 + c2 | ||||
| 	assert result.equals(math.complex(-40,-2)) | ||||
| 	c1 = math.complex(-71,2) | ||||
| 	c2 = math.complex(88,-12) | ||||
| 	result = c1 + c2 | ||||
| 	assert result.equals(math.complex(17,-10)) | ||||
| 	c1 = math.complex(0,-30) | ||||
| 	c2 = math.complex(52,-30) | ||||
| 	result = c1 + c2 | ||||
| 	assert result.equals(math.complex(52,-60)) | ||||
| 	c1 = math.complex(12,-9) | ||||
| 	c2 = math.complex(32,-6) | ||||
| 	result = c1 + c2 | ||||
| 	assert result.equals(math.complex(44,-15)) | ||||
| } | ||||
| 
 | ||||
| fn test_complex_subtraction() { | ||||
| 	mut c1 := math.complex(-8,0) | ||||
| 	mut c2 := math.complex(6,30) | ||||
| 	mut result := c1 - c2 | ||||
| 	assert result.equals(math.complex(-14,-30)) | ||||
| 	c1 = math.complex(-19,7) | ||||
| 	c2 = math.complex(29,32) | ||||
| 	result = c1 - c2 | ||||
| 	assert result.equals(math.complex(-48,-25)) | ||||
| 	c1 = math.complex(12,0) | ||||
| 	c2 = math.complex(23,13) | ||||
| 	result = c1 - c2 | ||||
| 	assert result.equals(math.complex(-11,-13)) | ||||
| 	c1 = math.complex(-14,3) | ||||
| 	c2 = math.complex(0,14) | ||||
| 	result = c1 - c2 | ||||
| 	assert result.equals(math.complex(-14,-11)) | ||||
| } | ||||
| 
 | ||||
| fn test_complex_multiplication() { | ||||
| 	mut c1 := math.complex(1,2) | ||||
| 	mut c2 := math.complex(1,-4) | ||||
| 	mut result := c1.multiply(c2) | ||||
| 	assert result.equals(math.complex(9,-2)) | ||||
| 	c1 = math.complex(-4,-4) | ||||
| 	c2 = math.complex(-5,-3) | ||||
| 	result = c1.multiply(c2) | ||||
| 	assert result.equals(math.complex(8,32)) | ||||
| 	c1 = math.complex(4,4) | ||||
| 	c2 = math.complex(-2,-5) | ||||
| 	result = c1.multiply(c2) | ||||
| 	assert result.equals(math.complex(12,-28)) | ||||
| 	c1 = math.complex(2,-2) | ||||
| 	c2 = math.complex(4,-4) | ||||
| 	result = c1.multiply(c2) | ||||
| 	assert result.equals(math.complex(0,-16)) | ||||
| } | ||||
| 
 | ||||
| fn test_complex_division() { | ||||
| 	mut c1 := math.complex(-9,-6) | ||||
| 	mut c2 := math.complex(-3,-2) | ||||
| 	mut result := c1.divide(c2) | ||||
| 	assert result.equals(math.complex(3,0)) | ||||
| 	c1 = math.complex(-23,11) | ||||
| 	c2 = math.complex(5,1) | ||||
| 	result = c1.divide(c2) | ||||
| 	assert result.equals(math.complex(-4,3)) | ||||
| 	c1 = math.complex(8,-2) | ||||
| 	c2 = math.complex(-4,1) | ||||
| 	result = c1.divide(c2) | ||||
| 	assert result.equals(math.complex(-2,0)) | ||||
| 	c1 = math.complex(11,24) | ||||
| 	c2 = math.complex(-4,-1) | ||||
| 	result = c1.divide(c2) | ||||
| 	assert result.equals(math.complex(-4,-5)) | ||||
| } | ||||
| 
 | ||||
| fn test_complex_conjugate() { | ||||
| 	mut c1 := math.complex(0,8) | ||||
| 	mut result := c1.conjugate() | ||||
| 	assert result.equals(math.complex(0,-8)) | ||||
| 	c1 = math.complex(7,3) | ||||
| 	result = c1.conjugate() | ||||
| 	assert result.equals(math.complex(7,-3)) | ||||
| 	c1 = math.complex(2,2) | ||||
| 	result = c1.conjugate() | ||||
| 	assert result.equals(math.complex(2,-2)) | ||||
| 	c1 = math.complex(7,0) | ||||
| 	result = c1.conjugate() | ||||
| 	assert result.equals(math.complex(7,0)) | ||||
| } | ||||
| 
 | ||||
| fn test_complex_equals() { | ||||
| 	mut c1 := math.complex(0,8) | ||||
| 	mut c2 := math.complex(0,8) | ||||
| 	assert c1.equals(c2) | ||||
| 	c1 = math.complex(-3,19) | ||||
| 	c2 = math.complex(-3,19) | ||||
| 	assert c1.equals(c2) | ||||
| } | ||||
		Loading…
	
		Reference in New Issue