Search code examples
scalascala-breeze

How to round DenseVector in Scala breeze?


In below code how can I round values of DenseVector to one decimal place.

DenseVector value should be :

DenseVector(0.4, 0.4, 0.2)

instead of :

DenseVector(0.36000000003540067, 0.35999999990731946, 0.18000000005727962)

I have tried using round method, but its not supported :

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

entire code for multiplying a Matrix by a 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.
}

Solution

  • Most "transformations" tend to live in breeze.numerics._ as standalone functions. One of them is rint, which rounds to the nearest integer.

    So:

    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)