Skip to content

Commit 7f24745

Browse files
committed
Added scala test
1 parent eb522b6 commit 7f24745

File tree

4 files changed

+293
-12
lines changed

4 files changed

+293
-12
lines changed

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ libraryDependencies += "io.github.hexagonnico" % "vecmatlib" % "2.2"
77
lazy val root = (project in file(".")).settings(
88
name := "CmplxLib"
99
)
10+
11+
// Scala test dependency
12+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.15" % Test

src/main/scala/io/github/hexagonnico/cmplxlib/Complex.scala

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,20 @@ case class Complex(a: Double, b: Double) extends Double2 {
146146
/**
147147
* Returns the product between this complex number and the given real number.
148148
*
149-
* @param k The real number to multiply
149+
* @param r The real number to multiply
150150
* @return The product between this complex number and the given real number
151151
*/
152-
def *(k: Double): Complex = Complex(this.a * k, this.b * k)
152+
def *(r: Double): Complex = Complex(this.a * r, this.b * r)
153153

154154
/**
155155
* Returns the product between this complex number and the given real number.
156156
*
157157
* This method can be used in place of the '*' operator for better interoperability with Java.
158158
*
159-
* @param k The real number to multiply
159+
* @param r The real number to multiply
160160
* @return The product between this complex number and the given real number
161161
*/
162-
def multiply(k: Double): Complex = this * k
162+
def multiply(r: Double): Complex = this * r
163163

164164
/**
165165
* Returns the product between this complex number and the number `a + ib`.
@@ -202,20 +202,20 @@ case class Complex(a: Double, b: Double) extends Double2 {
202202
/**
203203
* Returns the result of the division between this complex number and the given real number.
204204
*
205-
* @param k The real number by which this complex number is divided
205+
* @param r The real number by which this complex number is divided
206206
* @return The result of the division between this complex number and the given real number
207207
*/
208-
def /(k: Double): Complex = Complex(this.a / k, this.b / k)
208+
def /(r: Double): Complex = Complex(this.a / r, this.b / r)
209209

210210
/**
211211
* Returns the result of the division between this complex number and the given real number.
212212
*
213213
* This method can be used in place of the '/' operator for better interoperability with Java.
214214
*
215-
* @param k The real number by which this complex number is divided
215+
* @param r The real number by which this complex number is divided
216216
* @return The result of the division between this complex number and the given real number
217217
*/
218-
def divide(k: Double): Complex = this / k
218+
def divide(r: Double): Complex = this / r
219219

220220
/**
221221
* Returns the complex conjugate of this complex number.
@@ -328,12 +328,16 @@ case class Complex(a: Double, b: Double) extends Double2 {
328328
* @return A string representation of this complex number
329329
*/
330330
override def toString: String = {
331-
if(this.a == 0.0 && this.b == 0.0) {
331+
if (this.a == 0.0 && this.b == 0.0) {
332332
"0.0"
333-
} else if(this.b == 0.0) {
333+
} else if (this.b == 0.0) {
334334
this.a.toString
335-
} else if(this.a == 0.0) {
336-
f"${b}i"
335+
} else if (this.a == 0.0) {
336+
if (this.b == 1.0) {
337+
"i"
338+
} else {
339+
f"${b}i"
340+
}
337341
} else {
338342
f"$a ${if(b > 0.0) "+" else "-"} ${b.abs}i"
339343
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package io.github.hexagonnico.cmplxlib
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class ComplexSuite extends AnyFunSuite {
6+
7+
test("Sum of a complex number and two real numbers") {
8+
val z = Complex(2.0, 1.0)
9+
val res = z + (1.5, 2.0)
10+
assert(res == Complex(3.5, 3.0))
11+
}
12+
13+
test("Sum of a complex number and a real number") {
14+
val z = Complex(2.0, 1.0)
15+
val res = z + 1.5
16+
assert(res == Complex(3.5, 1.0))
17+
}
18+
19+
test("Sum of a real number and a complex number") {
20+
val z = Complex(2.0, 1.0)
21+
val res = 1.5 + z
22+
assert(res == Complex(3.5, 1.0))
23+
}
24+
25+
test("Sum of two complex numbers") {
26+
val z = Complex(2.0, 1.0)
27+
val w = Complex(1.5, 2.0)
28+
assert(z + w == Complex(3.5, 3.0))
29+
}
30+
31+
test("Additive inverse") {
32+
val z = Complex(2.0, 1.0)
33+
assert(-z == Complex(-2.0, -1.0))
34+
}
35+
36+
test("Subtraction of two real numbers from a complex number") {
37+
val z = Complex(2.0, 1.0)
38+
val res = z - (1.5, 2.0)
39+
assert(res == Complex(0.5, -1.0))
40+
}
41+
42+
test("Subtraction of a real number from a complex number") {
43+
val z = Complex(2.0, 1.0)
44+
val res = z - 1.5
45+
assert(res == Complex(0.5, 1.0))
46+
}
47+
48+
test("Subtraction of a complex number from a real number") {
49+
val z = Complex(2.0, 1.0)
50+
val res = 1.5 - z
51+
assert(res == Complex(-0.5, -1.0))
52+
}
53+
54+
test("Subtraction of two complex numbers") {
55+
val z = Complex(2.0, 1.0)
56+
val w = Complex(1.5, 2.0)
57+
assert(z - w == Complex(0.5, -1.0))
58+
}
59+
60+
test("Product between a complex number and a real number") {
61+
val z = Complex(2.0, 1.0)
62+
val res = z * 1.5
63+
assert(res == Complex(3.0, 1.5))
64+
}
65+
66+
test("Product between a real number and a complex number") {
67+
val z = Complex(2.0, 1.0)
68+
val res = 1.5 * z
69+
assert(res == Complex(3.0, 1.5))
70+
}
71+
72+
test("Product between a complex number and two real numbers") {
73+
val z = Complex(2.0, 1.0)
74+
val res = z * (1.5, 2.0)
75+
assert(res == Complex(1.0, 5.5))
76+
}
77+
78+
test("Product between two complex numbers") {
79+
val z = Complex(2.0, 1.0)
80+
val w = Complex(1.5, 2.0)
81+
assert(z * w == Complex(1.0, 5.5))
82+
}
83+
84+
test("Complex number divided by a real number") {
85+
val z = Complex(2.0, 1.0)
86+
val res = z / 2.0
87+
assert(res == Complex(1.0, 0.5))
88+
}
89+
90+
test("Conjugate of a complex number") {
91+
val z = Complex(2.0, 1.0)
92+
assert(z.conjugate == Complex(2.0, -1.0))
93+
}
94+
95+
test("Modulus of a complex number") {
96+
val z = Complex(1.0, 1.0)
97+
assert(z.abs == math.sqrt(2.0))
98+
}
99+
100+
test("Reciprocal of a complex number") {
101+
val z = Complex(2.0, 1.0)
102+
assert(z.reciprocal == Complex(0.4, -0.2))
103+
}
104+
105+
test("Complex number divided by a complex number") {
106+
val z = Complex(2.0, 2.0)
107+
val w = Complex(3.0, 2.0)
108+
assert(w / z == Complex(1.25, -0.25))
109+
}
110+
111+
test("Complex number divided by two real numbers") {
112+
val z = Complex(3.0, 2.0)
113+
val res = z / (2.0, 2.0)
114+
assert(res == Complex(1.25, -0.25))
115+
}
116+
117+
test("Argument of a complex number") {
118+
val z = Complex(4.0, 2.0)
119+
assert(z.arg == math.atan(0.5))
120+
}
121+
122+
test("Real and imaginary part of a complex number") {
123+
val z = Complex(3.0, 2.0)
124+
assert(z.real == 3.0)
125+
assert(z.imaginary == 2.0)
126+
}
127+
128+
test("Complex number to string") {
129+
val z = Complex(3.0, 2.0)
130+
assert(z.toString == "3.0 + 2.0i")
131+
}
132+
133+
test("Real number to string") {
134+
assert(Complex.One.toString == "1.0")
135+
}
136+
137+
test("Imaginary unit to string") {
138+
assert(Complex.I.toString == "i")
139+
}
140+
141+
test("Imaginary number to string") {
142+
val z = Complex(0.0, 2.0)
143+
assert(z.toString == "2.0i")
144+
}
145+
146+
test("Zero to string") {
147+
assert(Complex.Zero.toString == "0.0")
148+
}
149+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package io.github.hexagonnico.cmplxlib.vector
2+
3+
import io.github.hexagonnico.cmplxlib.Complex
4+
import org.scalactic.Tolerance.convertNumericToPlusOrMinusWrapper
5+
import org.scalatest.funsuite.AnyFunSuite
6+
7+
class Vec2cSuite extends AnyFunSuite {
8+
9+
test("Sum of a vector and two values") {
10+
val vec = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
11+
val res = vec + (Complex(2.0, 2.5), Complex(3.0, 0.5))
12+
assert(res == Vec2c(Complex(3.0, 4.5), Complex(4.5, 1.5)))
13+
}
14+
15+
test("Sum of two vectors") {
16+
val a = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
17+
val b = Vec2c(Complex(2.0, 2.5), Complex(3.0, 0.5))
18+
assert(a + b == Vec2c(Complex(3.0, 4.5), Complex(4.5, 1.5)))
19+
}
20+
21+
test("Additive inverse") {
22+
val a = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
23+
assert(-a == Vec2c(Complex(-1.0, -2.0), Complex(-1.5, -1.0)))
24+
}
25+
26+
test("Subtraction of two values from a vector") {
27+
val vec = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
28+
val res = vec - (Complex(2.0, 2.5), Complex(3.0, 0.5))
29+
assert(res == Vec2c(Complex(-1.0, -0.5), Complex(-1.5, 0.5)))
30+
}
31+
32+
test("Subtraction of two vectors") {
33+
val a = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
34+
val b = Vec2c(Complex(2.0, 2.5), Complex(3.0, 0.5))
35+
assert(a - b == Vec2c(Complex(-1.0, -0.5), Complex(-1.5, 0.5)))
36+
}
37+
38+
test("Vector multiplied by a scalar") {
39+
val vec = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
40+
val res = vec * 1.5
41+
assert(res == Vec2c(Complex(1.5, 3.0), Complex(2.25, 1.5)))
42+
}
43+
44+
test("Vector multiplied by a scalar commutativity") {
45+
val vec = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
46+
val res = 1.5 * vec
47+
assert(res == Vec2c(Complex(1.5, 3.0), Complex(2.25, 1.5)))
48+
}
49+
50+
test("Vector divided by a scalar") {
51+
val vec = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
52+
val res = vec / 2.0
53+
assert(res == Vec2c(Complex(0.5, 1.0), Complex(0.75, 0.5)))
54+
}
55+
56+
test("Component-wise multiplication of a vector and two values") {
57+
val vec = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
58+
val res = vec * (Complex(2.0, 2.5), Complex(3.0, 0.5))
59+
assert(res == Vec2c(Complex(-3.0, 6.5), Complex(4.0, 3.75)))
60+
}
61+
62+
test("Component-wise multiplication of two vectors") {
63+
val a = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
64+
val b = Vec2c(Complex(2.0, 2.5), Complex(3.0, 0.5))
65+
assert(a * b == Vec2c(Complex(-3.0, 6.5), Complex(4.0, 3.75)))
66+
}
67+
68+
test("Dot product with two values") {
69+
val vec = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
70+
val res = vec.dot(Complex(2.0, 2.5), Complex(3.0, 0.5))
71+
assert(res == Complex(12.0, 3.75))
72+
}
73+
74+
test("Dot product of two vectors") {
75+
val a = Vec2c(Complex(1.0, 2.0), Complex(1.5, 1.0))
76+
val b = Vec2c(Complex(2.0, 2.5), Complex(3.0, 0.5))
77+
assert((a dot b) == Complex(12.0, 3.75))
78+
}
79+
80+
test("Length squared") {
81+
assert(Vec2c(Complex(1.0, 1.0), Complex(1.0, 1.0)).lengthSquared == 4.0)
82+
}
83+
84+
test("Length") {
85+
assert(Vec2c(Complex(1.0, 1.0), Complex(1.0, 1.0)).length == 2.0)
86+
}
87+
88+
test("Normalized vector") {
89+
val vec = Vec2c(Complex(1.0, 1.0), Complex(1.0, 1.0))
90+
assert(vec.normalized == Vec2c(Complex(0.5, 0.5), Complex(0.5, 0.5)))
91+
}
92+
93+
test("Angle between two vectors") {
94+
val a = Vec2c(Complex(1.0, 1.0), Complex(2.0, 1.0))
95+
val b = Vec2c(Complex(2.0, 1.0), Complex(1.0, 1.0))
96+
assert(a.angle(b) === math.acos(6.0 / 7.0) +- 1e-9)
97+
}
98+
99+
test("Conjugate of a vector") {
100+
val vec = Vec2c(Complex(2.0, 4.0), Complex(8.0, 6.0))
101+
assert(vec.conjugate == Vec2c(Complex(2.0, -4.0), Complex(8.0, -6.0)))
102+
}
103+
104+
test("Reciprocal of a vector") {
105+
val vec = Vec2c(Complex(2.0, 4.0), Complex(8.0, 6.0))
106+
assert(vec.reciprocal == Vec2c(Complex(0.1, -0.2), Complex(0.08, -0.06)))
107+
}
108+
109+
test("Component-wise division of two vectors") {
110+
val a = Vec2c(Complex(8.0, 6.0), Complex(6.0, 8.0))
111+
val b = Vec2c(Complex(2.0, 2.0), Complex(4.0, 2.0))
112+
assert(a / b == Vec2c(Complex(3.5, -0.5), Complex(2.0, 1.0)))
113+
}
114+
115+
test("Component-wise division of a vector and two values") {
116+
val vec = Vec2c(Complex(8.0, 6.0), Complex(6.0, 8.0))
117+
val res = vec / (Complex(2.0, 2.0), Complex(4.0, 2.0))
118+
assert(res == Vec2c(Complex(3.5, -0.5), Complex(2.0, 1.0)))
119+
}
120+
121+
test("Vector absolute value") {
122+
val vec = Vec2c(Complex(1.0, 1.0), Complex(1.0, 1.0))
123+
assert(vec.abs == Vec2c(Complex(math.sqrt(2.0), 0.0), Complex(math.sqrt(2.0), 0.0)))
124+
}
125+
}

0 commit comments

Comments
 (0)