-
-
Save dduan/b931c8f3f5443e0bb43c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Complex<T: FloatLiteralConvertible> { | |
var real: T | |
var imaginary: T | |
} | |
func +(lhs: Complex<Double>, rhs: Complex<Double>) -> Complex<Double> { | |
return Complex<Double>(real: lhs.real + rhs.real, imaginary: lhs.imaginary + rhs.imaginary) | |
} | |
func -(lhs: Complex<Double>, rhs: Complex<Double>) -> Complex<Double> { | |
return Complex<Double>(real: lhs.real - rhs.real, imaginary: lhs.imaginary - rhs.imaginary) | |
} | |
func *(lhs: Complex<Double>, rhs: Complex<Double>) -> Complex<Double> { | |
return Complex<Double>( | |
real: lhs.real * rhs.real - lhs.imaginary * rhs.imaginary, | |
imaginary: lhs.imaginary * rhs.real + lhs.real * rhs.imaginary | |
) | |
} | |
func /(lhs: Complex<Double>, rhs: Complex<Double>) -> Complex<Double> { | |
let temp = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary | |
return Complex<Double>( | |
real: (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / temp, | |
imaginary: (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / temp | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment