-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEx07Spec.scala
106 lines (89 loc) · 2.36 KB
/
Ex07Spec.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package matrix
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class Ex07Spec extends AnyFlatSpec with Matchers:
"Matrix.linearMap" should "return the same vector on identity matrix" in {
val matrix = Matrix.identity[2, Int]
val vector = Vector.of(4, 2)
matrix.linearMap(vector) shouldBe vector
}
it should "return multiplied vector on diagonal matrix" in {
val matrix = Matrix.diagonal[2, Int](2)
val vector = Vector.of(4, 2)
matrix.linearMap(vector) shouldBe (vector * 2)
}
it should "return correct vector on random square matrix" in {
val matrix = Matrix[2, 2, Int] {
Vector.of(
Vector.of(2, -2),
Vector.of(-2, 2),
)
}
val vector = Vector.of(4, 2)
matrix.linearMap(vector) shouldBe Vector.of(4, -4)
}
it should "return correct vector on random matrix" in {
val matrix = Matrix[2, 3, Int] {
Vector.of(
Vector.of(2, 1, -3),
Vector.of(0, -1, 4),
)
}
val vector = Vector.of(3, -2, 1)
matrix.linearMap(vector) shouldBe Vector.of(1, 6)
}
"Matrix.x" should "return identity matrix for two identity matrices" in {
val id = Matrix.identity[2, Int]
(id x id) shouldBe id
}
it should "return the second matrix if first is identity" in {
val id = Matrix.identity[2, Int]
val matrix = Matrix[2, 2, Int] {
Vector.of(
Vector.of(2, 1),
Vector.of(4, 2),
)
}
(id x matrix) shouldBe matrix
}
it should "return the correct matrix for two random matrices of the same size" in {
val m1 = Matrix[2, 2, Int] {
Vector.of(
Vector.of(3, -5),
Vector.of(6, 8),
)
}
val m2 = Matrix[2, 2, Int] {
Vector.of(
Vector.of(2, 1),
Vector.of(4, 2),
)
}
(m1 x m2) shouldBe Matrix[2, 2, Int] {
Vector.of(
Vector.of(-14, -7),
Vector.of(44, 22),
)
}
}
it should "return the correct matrix for two random matrices" in {
val m1 = Matrix[2, 3, Int] {
Vector.of(
Vector.of(1, 2, 3),
Vector.of(4, 5, 6),
)
}
val m2 = Matrix[3, 1, Int] {
Vector.of(
Vector.of(7),
Vector.of(8),
Vector.of(9),
)
}
(m1 x m2) shouldBe Matrix[2, 1, Int] {
Vector.of(
Vector.of(50),
Vector.of(122),
)
}
}