-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathExample.scala
28 lines (21 loc) · 1.17 KB
/
Example.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
case class ComplexNumber(real: Double = 0, imaginary: Double = 0) {
def +(other: ComplexNumber): ComplexNumber =
ComplexNumber(real + other.real, imaginary + other.imaginary)
def -(other: ComplexNumber): ComplexNumber =
ComplexNumber(real - other.real, imaginary - other.imaginary)
def *(other: ComplexNumber): ComplexNumber =
ComplexNumber(real * other.real - imaginary * other.imaginary,
real * other.imaginary + imaginary * other.real)
def *(factor: Double): ComplexNumber =
ComplexNumber(factor * real, factor * imaginary)
def /(other: ComplexNumber): ComplexNumber =
ComplexNumber((real * other.real + imaginary * other.imaginary) / (other.real * other.real + other.imaginary * other.imaginary),
(imaginary * other.real - real * other.imaginary) / (other.real * other.real + other.imaginary * other.imaginary))
lazy val abs: Double =
math.sqrt(real * real + imaginary * imaginary)
lazy val conjugate: ComplexNumber = ComplexNumber(real, -1 * imaginary)
}
object ComplexNumber {
def exp(exponent: ComplexNumber): ComplexNumber =
ComplexNumber(Math.cos(exponent.imaginary), Math.sin(exponent.imaginary)) * Math.exp(exponent.real)
}