forked from breandan/galoisenne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTensor.kt
358 lines (301 loc) · 12.1 KB
/
Tensor.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package ai.hypergraph.kaliningraph.tensor
import ai.hypergraph.kaliningraph.allPairs
import ai.hypergraph.kaliningraph.types.*
import kotlin.math.*
import kotlin.random.Random
/**
* Generic matrix which supports overloadable addition and multiplication
* using an abstract algebra (e.g., tropical semiring). Useful for many
* problems in graph theory.
*
* @see [MatrixRing]
*/
interface Matrix<T, A : Ring<T>, M : Matrix<T, A, M>> : SparseTensor<Π3<Int, Int, T>> {
val algebra: A
val data: List<T>
// TODO: Tensor stuff
// TODO: create a constructor that takes a List<Pair<List<Int>, T>>
// for sparse tensors, e.g.: [([1, 2, 3], "a"), ([2, 4, 1], "b"), ...]
override val map: MutableMap<Π3<Int, Int, T>, Int> get() = TODO()
fun shape() = numRows cc numCols /** TODO: return [Π3] instead */
operator fun get(r: Any, c: Any): T = TODO("Implement support for named indexing")
val numRows: Int
val numCols: Int
operator fun plus(t: M): M = join(t) { i, j -> this@Matrix[i, j] + t[i, j] }
operator fun times(t: M): M = join(t) { i, j -> this@Matrix[i] dot t.transpose[j] }
fun <Y> map(f: (T) -> Y) = new(numRows, numCols, data.map(f) as List<T>)
fun getElements(filterBy: (Int, Int) -> Boolean) =
allPairs(numRows, numCols).mapNotNull { (r, c) -> if(filterBy(r, c)) this[r, c] else null }
infix fun List<T>.dot(es: List<T>): T =
with(algebra) { zip(es).map { (a, b) -> a * b }.reduce { a, b -> a + b } }
// Constructs a new instance with the same concrete matrix type
fun new(numRows: Int, numCols: Int, data: List<T>, alg: A = algebra): M
// TODO = this::class.primaryConstructor!!.call(algebra, numRows, numCols, data) as M
fun join(
that: M,
ids: Set<V2<Int>> = allPairs(numRows, that.numCols),
op: A.(Int, Int) -> T
): M = require(numCols == that.numRows) {
"Dimension mismatch: $numRows,$numCols . ${that.numRows},${that.numCols}"
}.let { new(numRows, that.numCols, ids.map { (i, j) -> algebra.op(i, j) }) }
operator fun get(r: Int, c: Int): T = data[r * numCols + c]
operator fun get(r: Int): List<T> = data.toList().subList(r * numCols, r * numCols + numCols)
}
// Only include nonzero indices for sparse matrices?
val <T, A : Ring<T>, M : Matrix<T, A, M>> Matrix<T, A, M>.idxs by cache { allPairs(numRows, numCols) }
val <T, A : Ring<T>, M : Matrix<T, A, M>> Matrix<T, A, M>.rows by cache { data.chunked(numCols) }
val <T, A : Ring<T>, M : Matrix<T, A, M>> Matrix<T, A, M>.cols by cache { (0 until numCols).map { c -> rows.map { it[c] } } }
val <T, A : Ring<T>, M : Matrix<T, A, M>> Matrix<T, A, M>.transpose by cache { new(numCols, numRows, cols.flatten()) }
// https://www.ijcai.org/Proceedings/2020/0685.pdf
val BOOLEAN_ALGEBRA: Ring<Boolean> =
Ring.of(
nil = false,
one = true,
plus = { a, b -> a || b },
times = { a, b -> a && b }
)
val XOR_ALGEBRA =
Ring.of(
nil = false,
one = true,
plus = { a, b -> a xor b },
times = { a, b -> a and b }
)
val INTEGER_FIELD: Field<Int> =
Field.of(
nil = 0,
one = 1,
plus = { a, b -> a + b },
minus = { a, b -> a - b },
times = { a, b -> a * b },
div = { _, _ -> throw NotImplementedError("Division not defined on integer field.") }
)
val DOUBLE_FIELD: Field<Double> =
Field.of(
nil = 0.0,
one = 1.0,
plus = { a, b -> a + b },
minus = { a, b -> a - b },
times = { a, b -> a * b },
div = { a, b -> a / b }
)
val MINPLUS_ALGEBRA: Ring<Int> =
Ring.of(
nil = Int.MAX_VALUE,
one = 0,
plus = { a, b -> min(a, b) },
times = { a, b -> a + b }
)
val MAXPLUS_ALGEBRA: Ring<Int> =
Ring.of(
nil = Int.MIN_VALUE,
one = 0,
plus = { a, b -> max(a, b) },
times = { a, b -> a + b }
)
val GF2_ALGEBRA: Ring<Int> =
Ring.of(
nil = 0,
one = 1,
plus = { a, b -> a + b % 2 },
times = { a, b -> a * b % 2 }
)
private fun <T> TODO_ALGEBRA(t: T): Ring<T> =
Ring.of(
nil = t,
plus = { a, b -> TODO() },
times = { a, b -> TODO() }
)
abstract class AbstractMatrix<T, A: Ring<T>, M: AbstractMatrix<T, A, M>> constructor(
override val algebra: A,
override val numRows: Int,
override val numCols: Int = numRows,
override val data: List<T>,
): Matrix<T, A, M> {
val values by lazy { data.toSet() }
override val map: MutableMap<Π3<Int, Int, T>, Int> by lazy {
idxs.fold(mutableMapOf()) { map, (r, c) ->
val element = get(r, c)
if (element != algebra.nil) map[Π(r, c, element)] = 1
map
}
}
override fun toString() =
data.maxOf { it.toString().length + 2 }.let { pad ->
data.foldIndexed("") { i, a, b ->
a + "$b".padEnd(pad, ' ') + " " + if (i > 0 && (i + 1) % numCols == 0) "\n" else ""
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as AbstractMatrix<*, *, *>
if (numRows != other.numRows) return false
if (numCols != other.numCols) return false
if (data != other.data) return false
// if (algebra != other.algebra) return false
return true
}
override fun hashCode(): Int {
var result = 1
result = 31 * result + numRows
result = 31 * result + numCols
result = 31 * result + data.hashCode()
result = 31 * result + algebra.hashCode()
return result
}
}
// A free matrix has no associated algebra by default. If you try to do math
// with the default implementation it will fail at runtime.
open class FreeMatrix<T> constructor(
override val numRows: Int,
override val numCols: Int = numRows,
override val data: List<T>,
override val algebra: Ring<T> = TODO_ALGEBRA(data.first()),
): AbstractMatrix<T, Ring<T>, FreeMatrix<T>>(algebra, numRows, numCols, data) {
constructor(elements: List<T>) : this(
numRows = sqrt(elements.size.toDouble()).toInt(),
data = elements
)
constructor(algebra: Ring<T>, elements: List<T>) : this(
algebra = algebra,
numRows = sqrt(elements.size.toDouble()).toInt(),
data = elements
)
constructor(numRows: Int, numCols: Int = numRows, f: (Int, Int) -> T) : this(
numRows = numRows,
numCols = numCols,
data = List(numRows * numCols) { f(it / numCols, it % numCols) }
)
constructor(
algebra: Ring<T>,
numRows: Int,
numCols: Int = numRows,
f: (Int, Int) -> T
) : this(
algebra = algebra,
numRows = numRows,
numCols = numCols,
data = List(numRows * numCols) { f(it / numCols, it % numCols) }
)
override fun new(numRows: Int, numCols: Int, data: List<T>, alg: Ring<T>) =
FreeMatrix(numRows, numCols, data, algebra)
override fun toString() =
"\n" + cols.map { it.maxOf { "$it".length } }.let { colWidth ->
rows.joinToString("\n") {
it.mapIndexed { i, c -> "$c".padEnd(colWidth[i]) }.joinToString(" ")
}
}
}
// Concrete subclasses
open class BooleanMatrix constructor(
override val numRows: Int,
override val numCols: Int = numRows,
override val data: List<Boolean>,
override val algebra: Ring<Boolean> = BOOLEAN_ALGEBRA,
): AbstractMatrix<Boolean, Ring<Boolean>, BooleanMatrix>(algebra, numRows, numCols, data) {
constructor(elements: List<Boolean>): this(
numRows = sqrt(elements.size.toDouble()).toInt(),
data = elements
)
constructor(algebra: Ring<Boolean>, elements: List<Boolean>): this(
algebra = algebra,
numRows = sqrt(elements.size.toDouble()).toInt(),
data = elements
)
constructor(numRows: Int, numCols: Int = numRows, f: (Int, Int) -> Boolean): this(
numRows = numRows,
numCols = numCols,
data = List(numRows * numCols) { f(it / numCols, it % numCols) }
)
constructor(vararg rows: Short): this(rows.fold("") { a, b -> a + b })
constructor(rows: String): this(
rows.filter { !it.isWhitespace() }.toCharArray().let { chars ->
val values = chars.distinct()
require(values.size <= 2) { "Expected two values or less" }
values.maxOrNull()!!.let { hi -> chars.map { it == hi } }
}
)
constructor(
numRows: Int,
numCols: Int = numRows,
values: List<Π2<Π2<Int, Int>, Boolean>>
): this(numRows, numCols,
values.toMap().let { map ->
List(numRows * numCols) { map[it / numCols to it % numCols] ?: false }
}
)
// TODO: Implement Four Russians for speedy Boolean matmuls https://arxiv.org/pdf/0811.1714.pdf#page=5
// override fun BooleanMatrix.times(t: BooleanMatrix): BooleanMatrix = TODO()
val isFull by lazy { data.all { it } }
companion object {
fun grayCode(size: Int): BooleanMatrix = TODO()
fun ones(size: Int) = BooleanMatrix(size) { _, _ -> true }
fun zeroes(size: Int) = BooleanMatrix(size) { _, _ -> false }
fun one(size: Int) = BooleanMatrix(size) { i, j -> i == j }
fun random(size: Int) = BooleanMatrix(size) { _, _ -> Random.nextBoolean() }
}
override fun toString() = "\n" + data.foldIndexed("") { i, a, b ->
a + (if (b) 1 else 0) + " " + if (i > 0 && (i + 1) % numCols == 0) "\n" else ""
}
override fun new(numRows: Int, numCols: Int, data: List<Boolean>, alg: Ring<Boolean>) =
BooleanMatrix(numRows, numCols, data, alg)
}
open class DoubleMatrix constructor(
override val numRows: Int,
override val numCols: Int = numRows,
override val data: List<Double>,
override val algebra: Field<Double> = DOUBLE_FIELD,
): AbstractMatrix<Double, Field<Double>, DoubleMatrix>(algebra, numRows, numCols, data) {
constructor(elements: List<Double>) : this(
numRows = sqrt(elements.size.toDouble()).toInt(),
data = elements
)
constructor(numRows: Int, numCols: Int = numRows, f: (Int, Int) -> Double) : this(
numRows = numRows,
numCols = numCols,
data = List(numRows * numCols) { f(it / numCols, it % numCols) }
)
constructor(vararg rows: Double) : this(rows.toList())
operator fun minus(that: DoubleMatrix): DoubleMatrix =
join(that) { i, j -> algebra.minus(this@DoubleMatrix[i, j], that[i][j]) }
companion object {
fun random(size: Int) = DoubleMatrix(size) { _, _ -> Random.nextDouble() }
}
override fun new(numRows: Int, numCols: Int, data: List<Double>, alg: Field<Double>) =
DoubleMatrix(numRows, numCols, data, alg)
}
operator fun Double.times(value: DoubleMatrix): DoubleMatrix = value * this
operator fun DoubleMatrix.times(value: Double): DoubleMatrix =
DoubleMatrix(numRows, numCols, data.map { it * value })
tailrec fun <T: FreeMatrix<S>, S> T.seekFixpoint(i: Int = 0, op: (T) -> T): T {
val next = op(this)
return if (this == next) next//.also { println("Converged in $i iterations") }
else next.seekFixpoint(i + 1, op)
}
fun DoubleMatrix.toBMat(
threshold: Double = (data.maxOf { it } + data.minOf { it }) / 2,
partitionFn: (Double) -> Boolean = { it > threshold }
) = BooleanMatrix(numRows, numCols) { i, j -> partitionFn(get(i, j)) }
operator fun BooleanMatrix.times(mat: DoubleMatrix): DoubleMatrix = toDoubleMatrix() * mat
operator fun BooleanMatrix.plus(mat: DoubleMatrix): DoubleMatrix = toDoubleMatrix() + mat
operator fun DoubleMatrix.minus(mat: BooleanMatrix): DoubleMatrix = this - mat.toDoubleMatrix()
fun BooleanMatrix.toDoubleMatrix() = DoubleMatrix(numRows, numCols) { i, j -> if (get(i, j)) 1.0 else 0.0 }
/**cf. [P]*/
// Alternatively: a length-2ⁿ array which can be "parsed" into a certain shape?
// See: http://conal.net/talks/can-tensor-programming-be-liberated.pdf
interface SparseTensor<T/*Should be a named tuple or dataclass of some kind*/> {
// TODO: Precompute specific [Borel] subsets of T's attributes that we expect to be queried at runtime
// e.g., (n-1)-D slices and 1D fibers
// https://mathoverflow.net/questions/393427/generalization-of-sinkhorn-s-theorem-to-stochastic-tensors
// private val marginals: MutableMap<List<T>, Int> = mutableMapOf()
val map: MutableMap<T, Int>
operator fun get(t: T) = map.getOrElse(t) { 0 }
// TODO: Support mutability but also map-reduce-ability/merge-ability for parallelism
// operator fun plus(other: SparseTensor<T>) = SparseTensor(map = this.map + other.map)
// operator fun MutableMap<T, Int>.plus(map: MutableMap<T, Int>): MutableMap<T, Int> =
// HashMap(this).apply { map.forEach { (k, v) -> merge(k, v, Int::plus) } }
operator fun set(index: T, i: Int) { map[index] = i }
fun count(selector: (T) -> Boolean) =
map.entries.sumOf { if(selector(it.key)) it.value else 0 }
}