All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.space.kscience.kmath.linear.RealMatrixContext.kt Maven / Gradle / Ivy

package space.kscience.kmath.linear

import space.kscience.kmath.structures.RealBuffer

public object RealMatrixContext : MatrixContext> {

    public override fun produce(
        rows: Int,
        columns: Int,
        initializer: (i: Int, j: Int) -> Double,
    ): BufferMatrix {
        val buffer = RealBuffer(rows * columns) { offset -> initializer(offset / columns, offset % columns) }
        return BufferMatrix(rows, columns, buffer)
    }

    public fun Matrix.toBufferMatrix(): BufferMatrix = if (this is BufferMatrix) this else {
        produce(rowNum, colNum) { i, j -> get(i, j) }
    }

    public fun one(rows: Int, columns: Int): Matrix = VirtualMatrix(rows, columns) { i, j ->
        if (i == j) 1.0 else 0.0
    } + DiagonalFeature

    public override infix fun Matrix.dot(other: Matrix): BufferMatrix {
        require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" }
        val bufferMatrix = toBufferMatrix()
        val otherBufferMatrix = other.toBufferMatrix()
        return produce(rowNum, other.colNum) { i, j ->
            var res = 0.0
            for (l in 0 until colNum) {
                res += bufferMatrix[i, l] * otherBufferMatrix[l, j]
            }
            res
        }
    }

    public override infix fun Matrix.dot(vector: Point): Point {
        require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" }
        val bufferMatrix = toBufferMatrix()
        return RealBuffer(rowNum) { i ->
            var res = 0.0
            for (j in 0 until colNum) {
                res += bufferMatrix[i, j] * vector[j]
            }
            res
        }
    }

    override fun add(a: Matrix, b: Matrix): BufferMatrix {
        require(a.rowNum == b.rowNum) { "Row number mismatch in matrix addition. Left side: ${a.rowNum}, right side: ${b.rowNum}" }
        require(a.colNum == b.colNum) { "Column number mismatch in matrix addition. Left side: ${a.colNum}, right side: ${b.colNum}" }
        val aBufferMatrix = a.toBufferMatrix()
        val bBufferMatrix = b.toBufferMatrix()
        return produce(a.rowNum, a.colNum) { i, j ->
            aBufferMatrix[i, j] + bBufferMatrix[i, j]
        }
    }

    override fun Matrix.times(value: Double): BufferMatrix {
        val bufferMatrix = toBufferMatrix()
        return produce(rowNum, colNum) { i, j -> bufferMatrix[i, j] * value }
    }


    override fun multiply(a: Matrix, k: Number): BufferMatrix {
        val aBufferMatrix = a.toBufferMatrix()
        return produce(a.rowNum, a.colNum) { i, j -> aBufferMatrix[i, j] * k.toDouble() }
    }
}


/**
 * Partially optimized real-valued matrix
 */
public val MatrixContext.Companion.real: RealMatrixContext get() = RealMatrixContext




© 2015 - 2025 Weber Informatics LLC | Privacy Policy