/
MatrixDemo.kt
179 lines (147 loc) · 3.67 KB
/
MatrixDemo.kt
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package ai.hypergraph.kotlingrad.samples
import ai.hypergraph.kotlingrad.api.*
import ai.hypergraph.kotlingrad.shapes.*
fun main() {
val f = x pow 2
println(f(x to 3.0))
println("f(x) = $f")
val df_dx = f.d(x)
println("f'(x) = $df_dx")
val g = x pow x
println("g(x) = $g")
val dg_dx = g.d(x)
println("g'(x) = $dg_dx")
val h = x + y
println("h(x) = $h")
val dh_dx = h.d(x)
println("h'(x) = $dh_dx")
val vf1 = Vec(y + x, y * 2)
val bh = x * vf1
val vf2 = Vec(x, y)
val q = vf1 + vf2
val r = q(x to 1.0, y to 2.0)
println("r: $r")
val mf1 = Mat2x1(y * y, x * y)
val mf2 = Mat1x2(vf2)
val qr = mf2 * Vec(x, y)
val mf3 = Mat3x2(x, x, y, x, x, x)
val mf4 = Mat2x2(vf2, vf2)
val mf5 = Mat2x2(
y * y, x * x,
x * y, y * y
)
val mf6 = mf4 * mf5 * mf1
println(mf1 * mf2) // 2*1 x 1*2
// println(mf1 * vf1) // 2*1 x 2
println(mf2 * vf1) // 1*2 x 2
println(mf3 * vf1) // 3*2 x 2
// println(mf3 * mf3) // 3*2 x 3*2
oldMatDemo()
}
fun oldMatDemo() {
// Inferred type: Mat<Int, D1, D3>
val a = DReal.Mat1x3(1.0, 2.0, 3.0)
println("a = $a")
// Inferred type: Mat<Int, D3, D1>
val b = a.ᵀ
println("b = $b")
// Inferred type: Mat<Int, D1, D1>
val c = a * b
println("c = ab = $c")
// Does not compile, inner dimension mismatch
// a * a
// b * b
// Does not compile, incompatible shape
// val b_ = Mat(D3, D1, 1, 2)
// val c_ = Mat(D3, D1, 1, 2, 3, 4)
// Does not compile, incompatible shape
// val b_ = Mat(D2, D1, 1)
// Inferred type: Mat<Int, D2, D3>
val d = DReal.Mat2x3(
1.0, 2.0, 3.0,
4.0, 5.0, 6.0
)
println("d = $d}")
// Inferred type: Mat<Int, D3, D2>
val e = DReal.Mat3x2(
1.0, 2.0,
3.0, 4.0,
5.0, 6.0
)
println("e = $e")
// Inferred type: Mat<Int, D2, D2>
val f = d * e
println("f = de = $f")
// Does not compile, inner dimension mismatch
// e * b
// f * b
// Does not compile, incompatible size
// val d_: Mat<DReal, D2, D3> = Mat2x3(
// 1, 2, 3,
// 4, 5
// )
// Inferred type: Mat<Int, D3, D3>
val g = DReal.Mat3x3(
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0
)
println("g = $g")
// Inferred type: Mat<Int, D3, D3>
val h = DReal.Mat3x3(
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0
)
println("h = $g")
// Inferred type: Mat<Int, D3, D3>
val i = g * h
println("i = gh = $i")
val j = i * i
println("j = ii = $j")
val k = i * b
println("k = ib = $j")
// Does not compile, inner dimension mismatch
// i * f
// i * d
// Inferred type: Mat<Int, D4, D4>
val l = DReal.Mat4x4(
1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0,
9.0, 0.0, 0.0, 0.0,
9.0, 0.0, 0.0, 0.0
)
println("l = $l")
// Inferred type: Mat<Int, D4, D3>
val m = DReal.Mat4x3(
1.0, 1.0, 1.0, 2.0,
2.0, 2.0, 3.0, 3.0,
3.0, 4.0, 4.0, 4.0
)
// Inferred type: Mat<Int, D4, D3>
val lm = l * m
println("lm = $lm")
// Does not compile, inner dimension mismatch
// lm * f
// ln * d
val o = DReal.Mat9x9(
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0
)
println(o * o - (o + o))
// val p = Mat(D1, D100){ 0.0 }
// // Type-checked matrix operations are still possible after unsafe construction
// val q = p.transpose()
// q * p
// Does not compile, inner dimension mismatch
// q * f
val t = DReal.Mat1x1(1.0)
println("t: $t")
}