Skip to content

Commit

Permalink
[LINALG] Adds Matrix and Vector operations and constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
fschueler authored and akunft committed Apr 29, 2016
1 parent f475c92 commit b8ef32b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ private[emma] class DenseMatrix[A: Numeric : ClassTag](
new DenseMatrix[A](numRows, that.numCols, values)
}

override
def %*%(that: Vector[A]): Vector[A] = {
val values: Array[A] = new Array[A](numRows)
for (i <- rRange) {
values(i) = row(i) dot that
}
new DenseVector[A](numRows, values)
}

//////////////////////////////////////////
// M operation
//////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eu.stratosphere.emma.api.lara
import spire.math.Numeric

import scala.reflect.ClassTag
import scala.util.Random

trait Matrix[A] {

Expand Down Expand Up @@ -52,8 +53,14 @@ trait Matrix[A] {

def /(that: Matrix[A]): Matrix[A]

//////////////////////////////////////////
// M x M -> M and M x V -> V
//////////////////////////////////////////

def %*%(that: Matrix[A]): Matrix[A]

def %*%(that: Vector[A]): Vector[A]

//////////////////////////////////////////
// M operation
//////////////////////////////////////////
Expand Down Expand Up @@ -125,4 +132,8 @@ object Matrix {
def eye[A: Numeric : ClassTag](rows: Int): Matrix[A] = {
Matrix.fill(rows, rows)((i, j) => if (i == j) implicitly[Numeric[A]].one else implicitly[Numeric[A]].zero)
}

def rand[A: Numeric : ClassTag](rows: Int, cols: Int): Matrix[A] = {
Matrix.fill(rows, cols)((i, j) => implicitly[Numeric[A]].fromDouble(Random.nextDouble()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package eu.stratosphere.emma.api.lara

import spire.math.Numeric

import scala.collection.immutable.NumericRange
import scala.reflect.ClassTag
import scala.util.Random

trait Vector[A] {

Expand Down Expand Up @@ -105,6 +107,18 @@ object Vector {
new DenseVector[A](values.length, values, rowVector = isRowVector)
}

def apply[A: Numeric : ClassTag](range: NumericRange[A]): Vector[A] = {
new DenseVector[A](range.length, range.toArray)
}

def apply[_ <: Int](range: Range): Vector[Int] = {
new DenseVector[Int](range.length, range.toArray)
}

def apply[A: Numeric : ClassTag](range: Range.Partial[Double, NumericRange[A]]): Vector[A] = {
apply[A](range.by(1.0))
}

//////////////////////////////////////////
// Generators
// TODO: THIS SHOULD BE REPLACED BY CTORS USING EITHER DENSE OR SPARSE IMPLEMENTATION
Expand All @@ -122,7 +136,28 @@ object Vector {
new DenseVector[A](size, Array.fill(size)(implicitly[Numeric[A]].zero), rowVector = isRowVector)
}

def zerosLike[A: Numeric: ClassTag](that: Vector[A]): Vector[A] = {
Vector.zeros[A](that.length)
}

def ones[A: Numeric : ClassTag](size: Int, isRowVector: Boolean = false): Vector[A] = {
new DenseVector[A](size, Array.fill(size)(implicitly[Numeric[A]].one), rowVector = isRowVector)
}

def rand[A: Numeric:ClassTag](size: Int, isRowVector: Boolean = false): Vector[A] = {
val rng = new Random()
new DenseVector[A](
size,
Array.fill(size)(implicitly[Numeric[A]].fromDouble(rng.nextDouble())),
rowVector = isRowVector
)
}

def rand[A: Numeric: ClassTag](size: Int, isRowVector: Boolean, seed: Long): Vector[A] = {
val rng = new Random(seed)
new DenseVector[A](size,
Array.fill(size)(implicitly[Numeric[A]].fromDouble(rng.nextDouble())),
rowVector = isRowVector
)
}
}

0 comments on commit b8ef32b

Please sign in to comment.