/
Matrix.kt
285 lines (243 loc) · 12.1 KB
/
Matrix.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
@file:Suppress("DuplicatedCode", "LocalVariableName", "UNUSED_PARAMETER", "NonAsciiCharacters", "FunctionName", "PropertyName", "MemberVisibilityCanBePrivate", "UNUSED_VARIABLE")
package ai.hypergraph.kotlingrad.api
import ai.hypergraph.kaliningraph.graphs.*
import ai.hypergraph.kaliningraph.tensor.*
import ai.hypergraph.kotlingrad.EAGER
import ai.hypergraph.kotlingrad.api.VFun.Companion.KG_IT
import ai.hypergraph.kotlingrad.shapes.D1
import ai.hypergraph.kotlingrad.shapes.DN
import ai.hypergraph.kotlingrad.shapes.Nat
import kotlin.reflect.KProperty
/**
* Matrix function.
*/
open class MFun<X, R, C>(override vararg val inputs: Fun<X>): Fun<X>
where X: SFun<X>, R: D1, C: D1 {
open val ᵀ: MFun<X, C, R> by lazy { MTranspose(this) }
override fun invoke(newBindings: Bindings<X>): MFun<X, R, C> =
MComposition(this, newBindings)
.run { if (bindings.complete || newBindings.readyToBind || EAGER) evaluate else this }
// Materializes the concrete matrix from the dataflow graph
override operator fun invoke(): Mat<X, R, C> =
MComposition(this).evaluate.let {
try {
it as Mat<X, R, C>
} catch (e: ClassCastException) {
//show("before"); it.show("after")
throw NumberFormatException("Matrix function has unbound free variables: ${bindings.allFreeVariables.keys}")
}
}
override operator fun invoke(vararg numbers: Number): MFun<X, R, C> =
invoke(bindings.zip(numbers.map { wrap(it) }))
override operator fun invoke(vararg funs: Fun<X>): MFun<X, R, C> =
invoke(bindings.zip(funs.toList()))
override operator fun invoke(vararg ps: FunToAny<X>): MFun<X, R, C> =
invoke(ps.toList().bind())
val mapInput = SVar<X>(KG_IT)
open fun map(ef: (SFun<X>) -> SFun<X>): MFun<X, R, C> = MMap(this, ef(mapInput), mapInput)
open fun d(sVar: SVar<X>): MFun<X, R, C> = MDerivative(this, sVar).let { if (EAGER) it.df() else it }
open operator fun unaryMinus(): MFun<X, R, C> = MNegative(this)
open operator fun plus(addend: MFun<X, R, C>): MFun<X, R, C> = MSum(this, addend)
open operator fun minus(addend: MFun<X, R, C>): MFun<X, R, C> = MSum(this, -addend)
open operator fun times(multiplicand: SFun<X>): MFun<X, R, C> = MSProd(this, multiplicand)
open operator fun times(multiplicand: VFun<X, C>): VFun<X, R> = MVProd(this, multiplicand)
open infix fun ʘ(multiplicand: MFun<X, R, C>): MFun<X, R, C> = HProd(this, multiplicand)
open operator fun <Q: D1> times(multiplicand: MFun<X, C, Q>): MFun<X, R, Q> = MMProd(this, multiplicand)
operator fun times(multiplicand: Number): MFun<X, R, C> = this * wrap(multiplicand)
open fun sum(): VFun<X, C> = MSumRows(this)
override fun toString() = when (this) {
is MNegative -> "-($input)"
is MTranspose -> "($input).T"
is MConst -> "${this::class.simpleName}($numRows x $numCols)"
is Mat -> "Mat(${rows.joinToString()})"
is MDerivative<X, *, *> -> "d($input) / d($vrb)"
is MVar -> "$name: Var${r}x${c}"
is MComposition -> "MComp($input)$bindings"
is Jacobian -> "Jacobian($vrb)$bindings"
else -> asString()
}
override val op: Op = when (this) {
is MNegative -> Ops.sub
is MTranspose -> Ops.transpose
is HProd<X, *, *> -> Ops.odot
is MMProd<X, *, *, *> -> Ops.prod
is MSum<X, *, *> -> Ops.sum
is SMProd<X, *, *> -> Ops.dot
is MComposition<X, *, *> -> Ops.λ
is VVMap<X, *, *> -> Ops.map
is MMap<X, *, *> -> Ops.map
else -> Ops.id
}
}
class MMap<X: SFun<X>, R: D1, C: D1>(
val input: MFun<X, R, C>,
val ssMap: SFun<X>,
placeholder: SVar<X>
): MFun<X, R, C>(input, ssMap), PolyFun<X> {
override val bindings: Bindings<X> = input.bindings + ssMap.bindings - placeholder
override val inputs: Array<out Fun<X>> = arrayOf(input, ssMap)
}
class MNegative<X: SFun<X>, R: D1, C: D1>(override val input: MFun<X, R, C>): MFun<X, R, C>(input), UnFun<X>
class MTranspose<X: SFun<X>, R: D1, C: D1>(override val input: MFun<X, R, C>): MFun<X, C, R>(input), UnFun<X>
class MSum<X: SFun<X>, R: D1, C: D1>(override val left: MFun<X, R, C>, override val right: MFun<X, R, C>): MFun<X, R, C>(left, right), BiFun<X>
class MMProd<X: SFun<X>, R: D1, C1: D1, C2: D1>(override val left: MFun<X, R, C1>, override val right: MFun<X, C1, C2>): MFun<X, R, C2>(left, right), BiFun<X>
class HProd<X: SFun<X>, R: D1, C: D1>(override val left: MFun<X, R, C>, override val right: MFun<X, R, C>): MFun<X, R, C>(left, right), BiFun<X>
class MSProd<X: SFun<X>, R: D1, C: D1>(override val left: MFun<X, R, C>, override val right: SFun<X>): MFun<X, R, C>(left, right), BiFun<X>
class SMProd<X: SFun<X>, R: D1, C: D1>(override val left: SFun<X>, override val right: MFun<X, R, C>): MFun<X, R, C>(right, right), BiFun<X>
class MComposition<X: SFun<X>, R: D1, C: D1>(
override val input: MFun<X, R, C>,
arguments: Bindings<X> = Bindings(input)
): MFun<X, R, C>(input), UnFun<X> {
override val bindings: Bindings<X> = input.bindings + arguments
val evaluate: MFun<X, R, C> by lazy { bind(bindings) }
@Suppress("UNCHECKED_CAST")
fun MFun<X, R, C>.bind(bnds: Bindings<X>): MFun<X, R, C> =
bnds[this@bind] ?: when (this@bind) {
is MConst -> this@bind
is MVar -> vMat
is Mat -> Mat(rows.map { it(bnds) })
is MNegative -> -input.bind(bnds)
is MTranspose -> input.ᵀ.bind(bnds)
is MSum -> left.bind(bnds) + right.bind(bnds)
is MMProd<X, R, *, C> -> left(bnds) as MFun<X, R, DN> * right(bnds) as MFun<X, DN, C>
is HProd -> left.bind(bnds) ʘ right.bind(bnds)
is MSProd -> left.bind(bnds) * right(bnds)
is SMProd -> left(bnds) * right.bind(bnds)
is MDerivative -> df().bind(bnds)
is MGradient<X, R, C> -> df().bind(bnds)
is MComposition -> input.bind(bnds)
is MMap<X, R, C> -> input.bind(bnds).map { ssMap(mapInput to it)(bnds) }
is Jacobian<X, R, C> -> df().bind(bnds)
is VVMap -> input(bnds).vMap { svMap(mapInput to it)(bnds) }
else -> TODO(this@bind.toString() + "/" + bnds)
}.also { result -> bnds.checkForUnpropagatedVariables(this@bind, result) }
}
// TODO: Generalize tensor derivatives? https://en.wikipedia.org/wiki/Tensor_derivative_(continuum_mechanics)
@Suppress("UNCHECKED_CAST")
class MDerivative<X: SFun<X>, R: D1, C: D1>(override val input: MFun<X, R, C>, override val vrb: SVar<X>): MFun<X, R, C>(input, vrb), Grad<X> {
fun df() = input.df()
fun MFun<X, R, C>.df(): MFun<X, R, C> = when (this@df) {
is MVar -> map { it.d(vrb) }
is Mat -> map { it.d(vrb) }
is MConst -> map { Zero() }
is MNegative -> -input.df()
is MTranspose -> (input as MFun<X, R, C>).df().ᵀ as MFun<X, R, C>
is MSum -> left.df() + right.df()
// Casting here is necessary because of type erasure (we loose the inner dimension when MMProd<X, R, C1, C2> is boxed as MFun<X, R, C2>)
is MMProd<X, R, *, C> ->
(left as MFun<X, R, C>).df() * (right as MFun<X, C, C>) +
left * ((right as MFun<X, R, C>).df() as MFun<X, C, C>)
is MSProd -> left.df() * right + left * right.d(vrb)
is SMProd -> left.d(vrb) * right + left * right.df()
is HProd -> left.df() ʘ right + left ʘ right.df()
is MMap -> input.df().map { it * ssMap.d(vrb) } // Chain rule
is VVMap -> input.d(vrb).vMap { it * svMap(mapInput to it).d(vrb) }
is MDerivative -> input.df()
is MComposition -> evaluate.df()
else -> TODO(this@df::class.simpleName!!)
}
}
class MGradient<X: SFun<X>, R: D1, C: D1>(override val input: SFun<X>, override val vrb: MVar<X, R, C>): MFun<X, R, C>(input, vrb), Grad<X> {
fun df() = input.df()
fun SFun<X>.df(): MFun<X, R, C> = when (this@df) {
is SVar -> vrb.vMat.map { if (it == this@df) One() else Zero() }
is SConst -> vrb.vMat.map { Zero() }
is Sum -> left.df() + right.df()
is Prod -> left.df() * right + left * right.df()
is Power ->
if (right.bindings.complete) right * left.pow(right - One()) * left.df()
else (left.df() * right * (One<X>() / left) + right.df() * left.ln())
is Negative -> -input.df()
is Log -> (left pow -One<X>()) * left.df()
is DProd -> vrb.map { q -> invoke().d(q as SVar<X>) }
is VSumAll<*, *> -> vrb.map { q -> invoke().d(q as SVar<X>) }
is SComposition -> evaluate.df()
else -> TODO(this@df::class.simpleName!!)
}
}
class Jacobian<X: SFun<X>, R: D1, C: D1>(override val input: VFun<X, R>, override val vrb: VVar<X, C>): MFun<X, R, C>(input, vrb), Grad<X> {
fun df() = input.df()
fun VFun<X, R>.df(): MFun<X, R, C> = when (this@df) {
is VSum -> left.df() + right.df()
is VNegative -> -input.df()
else -> input.vMap { it.d(vrb) }
}
}
open class MVar<X: SFun<X>, R: D1, C: D1> constructor(
val r: Nat<R>, val c: Nat<C>,
override val name: String = "",
val vVars: List<VVar<X, C>> = List(r.i) { VVar(c, "$name[$it]") },
val vMat: Mat<X, R, C> = Mat(List(r.i) { row -> vVars[row].sVars })
): Variable<X>, MFun<X, R, C>() {
override val bindings: Bindings<X> = Bindings(mapOf(this to vMat))
override fun equals(other: Any?) = other is MVar<*, *, *> && name == other.name
override fun hashCode(): Int = name.hashCode()
operator fun getValue(thisRef: Any?, property: KProperty<*>) =
MVar<X, R, C>(r, c, name.ifEmpty { property.name })
}
open class MConst<X: SFun<X>, R: D1, C: D1>(vararg val vConsts: VConst<X, C>): Mat<X, R, C>(vConsts.toList()), Constant<X> {
constructor(fVec: DoubleMatrix): this(*fVec.rows.map { VConst<X, C>(*it.toTypedArray()) }.toTypedArray())
val mat by lazy { DoubleMatrix(numRows, numCols) { row, col -> (vConsts[row][col] as SConst<X>).doubleValue } }
override fun times(multiplicand: SFun<X>): Mat<X, R, C> =
when (multiplicand) {
is SConst -> MConst(mat * multiplicand.doubleValue)
else -> super.times(multiplicand)
}
override fun times(multiplicand: VFun<X, C>): VFun<X, R> =
when (multiplicand) {
is VConst -> multiplicand * this
else -> super.times(multiplicand)
}
override operator fun <Q: D1> times(multiplicand: MFun<X, C, Q>): MFun<X, R, Q> = when (multiplicand) {
is MConst -> MConst(mat * multiplicand.mat)
else -> super.times(multiplicand)
}
}
open class Mat<X: SFun<X>, R: D1, C: D1>(open val rows: List<VFun<X, C>>):
MFun<X, R, C>(*rows.toTypedArray()), NilFun<X> {
constructor(vararg rows: Vec<X, C>): this(rows.asList())
// constructor(prototype: X, r: Nat<R>, c: Nat<C>, gen: (Int, Int) -> Any): this(
// (0 until r.i).map { i ->
// (0 until c.i).map { j ->
// prototype.wrapOrError(gen(i, j)) as SFun<X>
// }
// }.map { Vec<X, C>(it) }
// )
fun materialize() = rows.map { it.invoke() }.also { rows ->
rows.indices.zip(rows).filter { it.second.size != numCols }.run {
require(isEmpty()) { "Declared $numCols cols but row(s) ${map { it.first }} contain(s) ${map { it.second }} inputs, respectively" }
}
}
val flattened: List<SFun<X>> by lazy { materialize().flatMap { it.contents } }
val numRows: Int = rows.size
val numCols: Int by lazy { rows.first().invoke().size }
val indices: List<Int> = rows.indices.toList()
val cols: List<VFun<X, R>> by lazy { (0 until numCols).map { i -> Vec(materialize().map { it[i] }) } }
override fun sum() = rows.reduce { acc, it -> acc + it }
override val ᵀ: Mat<X, C, R> by lazy { Mat(cols) }
override fun map(ef: (SFun<X>) -> SFun<X>): Mat<X, R, C> =
Mat(rows.map { row -> row.map { ef(it)(mapInput to it) } })
override fun unaryMinus(): Mat<X, R, C> = map { -it }
override fun plus(addend: MFun<X, R, C>): MFun<X, R, C> =
when (addend) {
is Mat -> Mat(rows.mapIndexed { i, r -> r + addend[i] })
else -> super.plus(addend)
}
operator fun get(i: Int): VFun<X, C> = rows[i]
operator fun get(i: Int, j: Int): SFun<X> = (rows[i] as Vec<X, C>)[j]
override fun times(multiplicand: SFun<X>): Mat<X, R, C> = map { it * multiplicand }
override fun times(multiplicand: VFun<X, C>): VFun<X, R> =
when (multiplicand) {
is Vec -> Vec(rows.map { r -> r dot multiplicand })
else -> super.times(multiplicand)
}
override fun <Q: D1> times(multiplicand: MFun<X, C, Q>): MFun<X, R, Q> =
when (multiplicand) {
is Mat -> Mat(indices.map { i ->
Vec(multiplicand.cols.indices.map { j ->
rows[i] dot multiplicand.cols[j]
})
})
else -> super.times(multiplicand)
}
}