scalanlp / breeze

Breeze is a numerical processing library for Scala.
www.scalanlp.org
Apache License 2.0
3.44k stars 691 forks source link

single precision support added for LeastSquares calculation #857

Closed dineshdharme closed 7 months ago

dineshdharme commented 8 months ago

Hello David Hall,

This PR adds support for doing Least Squares calculation for Float datatype. Please review this.


  def NaiveMultiplicationFloat(n: Int, flag: Boolean): Unit = {

    val N_Dim = 4
    val rows = N_Dim + 5
    val columns = N_Dim
    val randomDoubleArray = Array.fill(rows * columns) {math.random}.map(ele => ele.toFloat)
    val sm: DenseMatrix[Float] = DenseMatrix.create(rows, columns, randomDoubleArray)

    val features2DMatrix = sm(::, 1 to -1)
    val targetPPNRVector = sm(::, 0)
   println("sm" +  sm)
    val result = leastSquares(features2DMatrix, targetPPNRVector)
    println("Breeze Linear Equation Solver results")

    println(s"intercept = ${result.coefficients.toArray.slice(1 , 5).mkString("[", ",", "]")}")
   println(s"r^2 = ${result.rSquared}")

  }

This code should allow you to test the Float datatype.

dineshdharme commented 7 months ago

Thank you, I will update the PR tomorrow and let you know.

dineshdharme commented 7 months ago

Hi David,

I made changes as you requested. Please take a look and let me know.

dineshdharme commented 7 months ago

I made the changes you requested. The only difference is for first apply I had to move the implicit can_dot to case class as scala didn't recognize it to be Function1's apply method.

Please review. thanks!