1

在下面的代码中,如何将 DenseVector 的值四舍五入到小数点后一位。

DenseVector 值应为:

DenseVector(0.4, 0.4, 0.2)

代替 :

DenseVector(0.36000000003540067, 0.35999999990731946, 0.18000000005727962)

我曾尝试使用圆形方法,但不支持:

     var nv : DenseVector[Double] = ((m * v).round)
value round is not a member of breeze.linalg.DenseVector[Double]

将 Matrix 乘以 Vector 的完整代码:

 val m = DenseMatrix((0.5, 0.5, 0.0) , (0.5 , 0.0 , 1.0) , (0.0 , 0.5 , 0.0))
                                                  //> m  : breeze.linalg.DenseMatrix[Double] = 0.5  0.5  0.0  
                                                  //| 0.5  0.0  1.0  
                                                  //| 0.0  0.5  0.0  
 var v = DenseVector(0.3 , 0.3 , 0.3)             //> v  : breeze.linalg.DenseVector[Double] = DenseVector(0.3, 0.3, 0.3)
for (i <- 1 to 100) {
 var nv : DenseVector[Double] = (m * v)
 v = nv
 if(i > 90){
     println(nv)
 }
}                                                 //> Oct 06, 2014 9:55:00 PM com.github.fommil.netlib.BLAS <clinit>
                                                  //| WARNING: Failed to load implementation from: com.github.fommil.netlib.Native
                                                  //| SystemBLAS
                                                  //| Oct 06, 2014 9:55:00 PM com.github.fommil.netlib.BLAS <clinit>
                                                  //| WARNING: Failed to load implementation from: com.github.fommil.netlib.Native
                                                  //| RefBLAS
                                                  //| DenseVector(0.3599999998439336, 0.36000000040858693, 0.17999999974747932)
                                                  //| DenseVector(0.36000000012626027, 0.3599999996694461, 0.18000000020429346)
                                                  //| DenseVector(0.3599999998978532, 0.3600000002674236, 0.17999999983472306)
                                                  //| DenseVector(0.36000000008263844, 0.35999999978364966, 0.1800000001337118)
                                                  //| DenseVector(0.359999999933144, 0.36000000017503103, 0.17999999989182483)
                                                  //| DenseVector(0.3600000000540875, 0.3599999998583968, 0.18000000008751552)
                                                  //| DenseVector(0.35999999995624216, 0.36000000011455924, 0.1799999999291984)
                                                  //| DenseVector(0.36000000003540067, 0.35999999990731946, 0.18000000005727962)
                                                  //| DenseVector(0.35999999997136006, 0.3600
                                                  //| Output exceeds cutoff limit.
}
4

1 回答 1

3

大多数“转换”倾向于作为独立函数存在于微风.numerics._ 中。其中之一是 rint,它四舍五入到最接近的整数。

所以:

scala> import breeze.linalg._
import breeze.linalg._

scala> DenseVector(0.36000000003540067, 0.35999999990731946, 0.18000000005727962)
res1: breeze.linalg.DenseVector[Double] = DenseVector(0.36000000003540067, 0.35999999990731946, 0.18000000005727962)

scala> breeze.numerics.rint(res1 * 10.0)/10.0
res2: breeze.linalg.DenseVector[Double] = DenseVector(0.4, 0.4, 0.2)
于 2014-10-06T22:28:16.737 回答