Matrix

open class Matrix<R : Nat, C : Nat>(source)

A dimensionally type-safe matrix of doubles.

The dimensions R (rows) and C (columns) are encoded at the type level using Nat types, allowing the compiler to catch dimension mismatches at compile time.

Example:

val a: Matrix<N2, N3> = Matrix.zero(N2, N3)  // 2x3 matrix
val b: Matrix<N3, N4> = Matrix.zero(N3, N4) // 3x4 matrix
val c: Matrix<N2, N4> = a * b // 2x4 matrix - compiles!
// val d = a * a // Would not compile - N3 != N2

Type Parameters

R

The row dimension type

C

The column dimension type

Inheritors

Constructors

Link copied to clipboard
constructor(data: Array<DoubleArray>)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
@get:JvmName(name = "inverse")
val inverse: Matrix<R, C>

The inverse of this matrix. Only valid for square matrices.

Link copied to clipboard

Natural number representing the number of columns.

Link copied to clipboard

Natural number representing the number of rows.

Link copied to clipboard
@get:JvmName(name = "norm")
val norm: Double

The Frobenius norm of this matrix.

Link copied to clipboard

The number of columns in the matrix.

Link copied to clipboard

The number of rows in the matrix.

Link copied to clipboard
@get:JvmName(name = "pseudoInverse")
val pseudoInverse: Matrix<C, R>

The pseudo-inverse of this matrix.

Link copied to clipboard

The size of the matrix as (rows, columns).

Link copied to clipboard
@get:JvmName(name = "transpose")
val transpose: Matrix<C, R>

The transpose of this matrix, with swapped dimension types.

Functions

Link copied to clipboard
open fun copy(): Matrix<R, C>

Returns a copy of this matrix.

Link copied to clipboard
open operator fun div(scalar: Double): Matrix<R, C>
open operator fun div(scalar: Number): Matrix<R, C>

Divides this matrix by a scalar.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
fun <N : Nat> Matrix<N, N>.exp(): Matrix<N, N>

Computes the matrix exponential of this matrix, using the Padé approximant.

Link copied to clipboard
operator fun get(i: Int, j: Int): Double

Returns the element at the given indices.

Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
operator fun minus(other: Matrix<R, C>): Matrix<R, C>

Subtracts another matrix with the same dimensions.

Link copied to clipboard
operator fun plus(other: Matrix<R, C>): Matrix<R, C>

Adds another matrix with the same dimensions.

Link copied to clipboard
operator fun set(i: Int, j: Int, value: Double)

Sets the element at the given indices.

Link copied to clipboard
fun <K : Nat> solve(other: Matrix<R, K>): Matrix<C, K>

Solves for X in the equation AX = B, where A is this matrix and B is other.

Link copied to clipboard
operator fun <K : Nat> times(other: Matrix<C, K>): Matrix<R, K>

Multiplies this matrix by another matrix. The inner dimensions must match: (R x C) * (C x K) = (R x K)

operator fun times(other: Vector<C>): Vector<R>

Multiplies this matrix by a vector. The vector's length must be equal to C.

open operator fun times(scalar: Double): Matrix<R, C>
open operator fun times(scalar: Number): Matrix<R, C>

Multiplies this matrix by a scalar.

Link copied to clipboard
Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
open operator fun unaryMinus(): Matrix<R, C>

Negates all elements of this matrix.