以 Breeze 向量或 Breeze 矩阵作为输入的通用方法
Generic method that take as input Breeze Vector or Breeze Matrix
我正在尝试实现一种可以接受 Breeze 向量或 Breeze 矩阵作为输入的方法。像
private def updateExponentialMovingAverage[T](tau: Double, initTheta: T, theta: T): T = {
tau * theta + (1 - tau) * initTheta
}
但是这会引发重载问题,我找不到合适的类型来限制 T。你有什么建议吗?
Breezes 使用类型 classes 来处理大多数这类事情。最简单的方法是使用 VectorSpace
类型 class,但不幸的是,对于 DenseMatrices,VectorSpace 类型 class 被另一个导入门控,我很遗憾。
import breeze.linalg._
import breeze.math._
import breeze.linalg.DenseMatrix.FrobeniusInnerProductDenseMatrixSpace.space
def updateExponentialMovingAverage[T](tau: Double, initTheta: T, theta: T)(implicit vs: VectorSpace[T, Double]): T = {
import vs._
theta * tau + initTheta * (1 - tau)
}
scala> val dv = DenseVector.rand(3)
val dv: breeze.linalg.DenseVector[Double] = DenseVector(0.21025028035007942, 0.6257503866073217, 0.8304592391242225)
scala> updateExponentialMovingAverage(0.8, dv, dv)
val res0: breeze.linalg.DenseVector[Double] = DenseVector(0.21025028035007942, 0.6257503866073217, 0.8304592391242225)
scala> val dm = DenseMatrix.rand(3, 3)
val dm: breeze.linalg.DenseMatrix[Double] = 0.6848513069340505 0.8995141354384266 0.3889904836678608
0.4554398871938874 0.03297082723969558 0.6708501327709948
0.4456539828672945 0.04289112062985678 0.9679002485942578
updateExponentialMovingAverage(0.8, dm, dm)
val res1: breeze.linalg.DenseMatrix[Double] = 0.6848513069340505 0.8995141354384266 0.3889904836678608
0.4554398871938874 0.03297082723969558 0.6708501327709948
0.4456539828672945 0.04289112062985678 0.9679002485942578
我正在尝试实现一种可以接受 Breeze 向量或 Breeze 矩阵作为输入的方法。像
private def updateExponentialMovingAverage[T](tau: Double, initTheta: T, theta: T): T = {
tau * theta + (1 - tau) * initTheta
}
但是这会引发重载问题,我找不到合适的类型来限制 T。你有什么建议吗?
Breezes 使用类型 classes 来处理大多数这类事情。最简单的方法是使用 VectorSpace
类型 class,但不幸的是,对于 DenseMatrices,VectorSpace 类型 class 被另一个导入门控,我很遗憾。
import breeze.linalg._
import breeze.math._
import breeze.linalg.DenseMatrix.FrobeniusInnerProductDenseMatrixSpace.space
def updateExponentialMovingAverage[T](tau: Double, initTheta: T, theta: T)(implicit vs: VectorSpace[T, Double]): T = {
import vs._
theta * tau + initTheta * (1 - tau)
}
scala> val dv = DenseVector.rand(3)
val dv: breeze.linalg.DenseVector[Double] = DenseVector(0.21025028035007942, 0.6257503866073217, 0.8304592391242225)
scala> updateExponentialMovingAverage(0.8, dv, dv)
val res0: breeze.linalg.DenseVector[Double] = DenseVector(0.21025028035007942, 0.6257503866073217, 0.8304592391242225)
scala> val dm = DenseMatrix.rand(3, 3)
val dm: breeze.linalg.DenseMatrix[Double] = 0.6848513069340505 0.8995141354384266 0.3889904836678608
0.4554398871938874 0.03297082723969558 0.6708501327709948
0.4456539828672945 0.04289112062985678 0.9679002485942578
updateExponentialMovingAverage(0.8, dm, dm)
val res1: breeze.linalg.DenseMatrix[Double] = 0.6848513069340505 0.8995141354384266 0.3889904836678608
0.4554398871938874 0.03297082723969558 0.6708501327709948
0.4456539828672945 0.04289112062985678 0.9679002485942578