Scala 具有自定义数据类型的Breeze线性代数
我正在尝试使用Breeze线性代数包()和我自己表示GF2字段的数据类型来实现Humming编码。到目前为止,我成功地实现了我的GF2:Scala 具有自定义数据类型的Breeze线性代数,scala,types,linear-algebra,scala-breeze,Scala,Types,Linear Algebra,Scala Breeze,我正在尝试使用Breeze线性代数包()和我自己表示GF2字段的数据类型来实现Humming编码。到目前为止,我成功地实现了我的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")
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 }
}
以及创建GF2值矩阵所需的类型类:
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)
但当我尝试将矩阵添加到自身时,我得到以下错误:
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]
在这一点上,我被卡住了,因为我不太明白我现在应该实现什么类型的类。查看breeze.linalg.operators
并没有多大帮助。所以问题是,我应该怎么做才能将我的GF2与Breeze结合起来,以支持所有矩阵/向量运算
===================上述问题在0.6版中得到解决
但是,我仍然不能将矩阵乘以向量或值:
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
我想我必须提供一个
UFunc
实现。它应该是什么样子?这在微风的最新快照中起作用。这个周末我将发布0.6
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
这在Breeze的最新快照中起作用。这个周末我将发布0.6
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
谢谢也许你也可以帮我解决后续问题(矩阵向量乘法)?谢谢!也许你也可以帮我解决后续问题(矩阵向量乘法)?