Skip to content

Commit c29e1a5

Browse files
committed
fix commutativity of addition
1 parent 333c135 commit c29e1a5

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

src/commonMain/kotlin/ai/hypergraph/kaliningraph/tensor/Tensor.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ interface Matrix<T, A : Ring<T>, M : Matrix<T, A, M>> : SparseTensor<Π3<Int, In
5050
fun getElements(filterBy: (Int, Int) -> Boolean) =
5151
allPairs(numRows, numCols).mapNotNull { (r, c) -> if (filterBy(r, c)) this[r, c] else null }
5252

53-
infix fun List<T>.dot(es: List<T>): T =
54-
require(size == es.size) { "Length mismatch: $size . ${es.size}" }
55-
// .run { with(algebra) { mapIndexed { i, a -> a * es[i] }.reduce { a, b -> a + b } } }
56-
.run { with(algebra) { zip(es).map { (a, b) -> a * b }.reduce { a, b -> a + b } } }
53+
infix fun List<T>.dot(es: List<T>): T = algebra.dot(this, es)
5754

5855
// Constructs a new instance with the same concrete matrix type
5956
fun new(rows: Int = numRows, cols: Int = numCols, data: List<T>, alg: A = algebra): M
@@ -403,7 +400,7 @@ open class UTMatrix<T> constructor(
403400
algebra = algebra
404401
)
405402
else carry.windowed(2, 1).map { window ->
406-
with(algebra) { dot(window[0].π2, window[1].π3) }
403+
algebra.dot(window[0].π2, window[1].π3)
407404
.let { it to (window[0].π2 + it) to (listOf(it) + window[1].π3) }
408405
}.let { next ->
409406
UTMatrix(

src/commonMain/kotlin/ai/hypergraph/kaliningraph/types/Types.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ interface Group<T>: Nat<T> {
8585
interface Ring<T>: Group<T> {
8686
override fun T.plus(t: T): T
8787
override fun T.times(t: T): T
88-
fun dot(l1: List<T>, l2: List<T>): T = l1.zip(l2).map { (l, r) -> l * r }.reduce { acc, t -> acc + t }
88+
fun dot(l1: List<T>, l2: List<T>): T =
89+
// n.b.: addition may not necessarily commute?
90+
l1.zip(l2).map { (l, r) -> l * r }.reduce { acc, t -> t + acc }
8991

9092
open class of<T>(
9193
override val nil: T, override val one: T = nil,

src/jvmMain/kotlin/ai/hypergraph/kaliningraph/parsing/JVMBarHillel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private fun CFG.jvmGenSym(
462462
val nextGenerating: MutableSet<Σᐩ> = from.toMutableSet()
463463
val TDEPS =
464464
ConcurrentHashMap<Σᐩ, MutableSet<Σᐩ>>(size).apply {
465-
this@jvmGenSym.toHashSet().asSequence().asStream().parallel()
465+
this@jvmGenSym.asSequence().asStream().parallel()
466466
.forEach { (l, r) -> r.forEach { getOrPut(it) { ConcurrentHashMap.newKeySet() }.add(l) } }
467467
}
468468
// [email protected]().asStream().parallel()

0 commit comments

Comments
 (0)