This commit is contained in:
Alexander Nozik 2021-03-14 09:55:24 +03:00
parent 0aa73cd48f
commit 8179be2f62
2 changed files with 3 additions and 5 deletions

View File

@ -9,7 +9,7 @@ import space.kscience.kmath.structures.VirtualBuffer
import space.kscience.kmath.structures.indices
public class LinearSpaceOverNd<T : Any, A : Ring<T>>(
public class BufferLinearSpace<T : Any, A : Ring<T>>(
override val elementAlgebra: A,
private val bufferFactory: BufferFactory<T>,
) : LinearSpace<T, A> {
@ -35,7 +35,7 @@ public class LinearSpaceOverNd<T : Any, A : Ring<T>>(
}
override fun Matrix<T>.minus(other: Matrix<T>): Matrix<T> = ndRing(rowNum, colNum).run {
require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::plus. Expected $shape but found ${other.shape}" }
require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::minus. Expected $shape but found ${other.shape}" }
unwrap().minus(other.unwrap()).as2D()
}
@ -50,7 +50,6 @@ public class LinearSpaceOverNd<T : Any, A : Ring<T>>(
return elementAlgebra {
val rows = this@dot.rows.map{it.linearize()}
val columns = other.columns.map { it.linearize() }
//TODO optimize buffers
buildMatrix(rowNum, other.colNum) { i, j ->
val r = rows[i]
val c = columns[j]
@ -67,7 +66,6 @@ public class LinearSpaceOverNd<T : Any, A : Ring<T>>(
require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" }
return elementAlgebra {
val rows = this@dot.rows
//TODO optimize buffers
buildVector(rowNum) { i ->
val r = rows[i]
var res = zero

View File

@ -171,7 +171,7 @@ public interface LinearSpace<T : Any, out A : Ring<T>> {
public fun <T : Any, A : Ring<T>> buffered(
algebra: A,
bufferFactory: BufferFactory<T> = Buffer.Companion::boxing,
): LinearSpace<T, A> = LinearSpaceOverNd(algebra,bufferFactory)
): LinearSpace<T, A> = BufferLinearSpace(algebra,bufferFactory)
public val real: LinearSpace<Double, RealField> = buffered(RealField, Buffer.Companion::real)