https://github.com/dieproht/matr
A Scala 3 matrix library
https://github.com/dieproht/matr
matrix-library scala
Last synced: 9 months ago
JSON representation
A Scala 3 matrix library
- Host: GitHub
- URL: https://github.com/dieproht/matr
- Owner: dieproht
- License: apache-2.0
- Created: 2021-02-19T07:59:05.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-07-18T07:03:00.000Z (about 1 year ago)
- Last Synced: 2025-08-16T19:34:27.959Z (11 months ago)
- Topics: matrix-library, scala
- Language: Scala
- Homepage:
- Size: 262 KB
- Stars: 9
- Watchers: 1
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# matr [ˈmætə]

[](https://gitter.im/dieproht/matr?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://scala-steward.org)

## A Scala 3 matrix library.
Matr is an attempt to bring together safety, flexibility and simplicity for matrix calculations.
* Safety through typed shapes and immutable data
* Flexibility through loose coupling between interface, data and operations
* Simplicity through an easily accessible API and "batteries included" default implementations, both with zero dependencies
## Getting Started
Add the following dependency to your SBT build configuration:
```scala
libraryDependencies += "io.github.dieproht" %% "matr-bundle" % ""
```
## Basic Usage
```scala
// Bring API interfaces in scope
import matr.{Matrix, MatrixFactory}
// Bring bundled implementations in scope
import matr.MatrBundle.given
// Create a Matrix from tuples
val a: Matrix[3, 4, Int] =
MatrixFactory[3, 4, Int].fromTuple(
(0, 8, 15, 0),
(4, 7, 1, 1),
(1, 2, 3, 4)
)
// Create a Matrix of ones
val b: Matrix[3, 4, Int] = MatrixFactory[3, 4, Int].ones
// Add two matrices
val c: Matrix[3, 4, Int] = a + b
// Create a Matrix of random numbers
val d: Matrix[4, 2, Int] =
MatrixFactory[4, 2, Int].tabulate((_, _) => scala.util.Random.nextInt(20))
// Calculate the dot product of two Matrices
val e: Matrix[3, 2, Int] = c dot d
// Transpose a Matrix
val f: Matrix[2, 3, Int] = e.transpose
// Pretty print a Matrix
println(f.mkString)
// ... it outputs something like this:
// ⎛ 73, 77, 116⎞
// ⎝430, 253, 179⎠
```
Matr checks the shape of a Matrix at compile-time, thus the following code does *not* compile:
```scala
// Element (2, 3) is missing when creating the Matrix
val ae =
MatrixFactory[3, 4, Int].fromTuple(
(0, 8, 15, 0),
(4, 7, 1, 1),
(1, 2, 3)
)
// Adding two Matrices with different dimensions is not possible
val be = MatrixFactory[2, 5, Int].ones
val ce = a + be
// Column dimension of left-hande side and row dimension of right-hand side
// must be equal when calculating dot product
val de = MatrixFactory[1, 2, Int].tabulate((_, _) => scala.util.Random.nextInt(20))
val ee = c dot de
```
## Status of the Project
This project is currently in an early phase. If you are looking for a mature tool for matrix calculations in Scala, you should definitely consider [Breeze](https://github.com/scalanlp/breeze)!
## Copyright and License
All code is available to you under the Apache 2 license, available at
https://opensource.org/licenses/Apache-2.0.
Copyright Karl F Walkow, 2020 - 2021.