Reputation: 2847
I'm trying to implement Humming coding using Breeze Linear Algebra package (https://github.com/scalanlp/breeze) and my own data type that represents GF2 field. So far I was successfully able to implement my GF2:
package humming
trait GF2 {
def + (that: GF2): GF2
def * (that: GF2): GF2
def / (that: GF2) = that match {
case Zero => throw new IllegalArgumentException("Div by 0")
case _ => this
}
}
object Zero extends GF2 {
override def toString = "Zero"
def + (that: GF2) = that
def * (that: GF2) = this
}
object One extends GF2 {
override def toString = "One"
def + (that: GF2) = that match { case One => Zero ; case _ => this }
def * (that: GF2) = that match { case One => this ; case _ => that }
}
and needed type classes to create a matrix of GF2 values:
import breeze.linalg._
import breeze.numerics._
import breeze.storage._
import scala.reflect._
import breeze.math._
import humming._
implicit object GF2DefaultArrayValue extends DefaultArrayValue[GF2] {
override def value = Zero
}
implicit object GF2Semiring extends Semiring[GF2] {
def defaultArrayValue = GF2DefaultArrayValue
def manifest = classTag[GF2]
def zero = Zero
def one = One
def ==(a: GF2, b: GF2) = a == b
def !=(a: GF2, b: GF2) = a != b
def +(a: GF2, b: GF2) = a + b
def *(a: GF2, b: GF2) = a * b
}
val a = DenseMatrix.eye[GF2](5)
But when I try to add matrix to itself I get the following error:
a + a
// could not find implicit value for parameter op:
// breeze.linalg.operators.BinaryOp[breeze.linalg.DenseMatrix[humming.GF2],
// breeze.linalg.DenseMatrix[humming.GF2],breeze.linalg.operators.OpAdd,That]
At this point I'm stuck because I don't quite understand what type class I should implement now. Looking at breeze.linalg.operators
did not help much. So the question is what should I do to marry my GF2 with Breeze in a way that all matrix/vector operations could be supported?
==============
The problem above is solved in version 0.6
However, I still can't multiply matrix by vector or by value:
val a = DenseMatrix.eye[GF2](5)
val b = DenseVector.ones[GF2](5)
a + a // OK
a * b
// Could not find an implicit implementation for this UFunc with arguments
// breeze.linalg.DenseMatrix[humming.GF2], breeze.linalg.DenseVector[humming.GF2]
a + One
// Could not find an implicit implementation for this UFunc with arguments
// breeze.linalg.DenseMatrix[humming.GF2], humming.One.type
I suppose I have to provide a UFunc
implementation. What should it look like?
Upvotes: 1
Views: 680
Reputation: 2293
This works in the latest snapshot of Breeze. I'll be releasing 0.6 this weekend.
scala> val a = DenseMatrix.eye[GF2](5)
a: breeze.linalg.DenseMatrix[X.GF2] =
One Zero Zero Zero Zero
Zero One Zero Zero Zero
Zero Zero One Zero Zero
Zero Zero Zero One Zero
Zero Zero Zero Zero One
scala> a + a
res0: breeze.linalg.DenseMatrix[X.GF2] =
Zero Zero Zero Zero Zero
Zero Zero Zero Zero Zero
Zero Zero Zero Zero Zero
Zero Zero Zero Zero Zero
Zero Zero Zero Zero Zero
Upvotes: 4