scalala/Scalala — Issue #47: Matrix multiplication gives incorrect results for matrix got from DenseMatrix.horzcat function
Author: tutysara
Created: 2012-01-23 04:46:40+00:00
State: open

I had to add bias terms to a matrix before multiplying, I used DenseMatrix.horzcat function to append the column 1's to the original matrix.
However there is vast difference between the result got by multiplying a matrix hard coded with the column of 1's and the one which is got as a result of applying the function DenseMatrix.horzcat.
I am using a DenseMatrix of Double, I know there could be some round off errors, but I think the difference is considerable.
Guys can you please try running it on your machines and see if it works fine or it is really a bug that needs a fix.

code
================

package org.tutysara
import scalala.scalar._;
import scalala.tensor.::;
import scalala.tensor.mutable._;
import scalala.tensor.dense._;
import scalala.tensor.sparse._;
import scalala.library.Library._;
import scalala.library.LinearAlgebra._;
import scalala.library.Statistics._;
import scalala.library.Plotting._;
import scalala.operators.Implicits._;
import scalala.generic.math.Library._
object test {

def main(args: Array[String]): Unit = {
val X_hc=DenseMatrix(
(1.000000 , 0.084147, -0.027942, -0.099999),
(1.000000 , 0.090930, 0.065699, -0.053657),
(1.000000 , 0.014112, 0.098936, 0.042017),
(1.000000 ,-0.075680, 0.041212, 0.099061),
(1.000000 ,-0.095892, -0.054402, 0.065029)
)

val X=DenseMatrix(
(0.841471, -0.279415, -0.999990 ),
(0.909297, 0.656987, -0.536573),
(0.141120, 0.989358, 0.420167),
(-0.756802, 0.412118, 0.990607),
(-0.958924, -0.544021, 0.650288 )
)
val X_new = DenseMatrix.horzcat( DenseMatrix.ones[Double](X.numRows,1),X); //add bias terms
val Theta1 =DenseMatrix(
(0.084147 , -0.027942, -0.099999, -0.028790),
(0.090930 , 0.065699 , -0.053657, -0.096140),
(0.014112 , 0.098936 , 0.042017, -0.075099),
(-0.075680 , 0.041212 , 0.099061, 0.014988),
(-0.095892 , -0.054402 , 0.065029, 0.091295)
)

println("X = \n"+X)
println("Theta1 = \n"+Theta1)
println("X_new = \n"+X_new)
println("X_hc = \n"+X_hc)
val res=Theta1*X_new.t
println("res = \n"+res)
val rec_x_hc=Theta1*X_hc.t
println("res_x_hc = \n"+rec_x_hc)
/*
* res should be
8.7469e-02 7.6581e-02 7.2650e-02 7.9289e-02 9.0394e-02
1.0757e-01 9.8537e-02 8.2509e-02 7.4223e-02 8.1297e-02
2.8773e-02 2.9898e-02 1.6510e-02 9.1676e-04 -2.5446e-03
-7.6479e-02 -6.6229e-02 -6.4668e-02 -7.3232e-02 -8.4047e-02
-1.1142e-01 -1.0147e-01 -8.6391e-02 -8.0052e-02 -8.8277e-02

*/
/*
Output
================================================================
X =
0.841471 -0.279415 -0.999990
0.909297 0.656987 -0.536573
0.141120 0.989358 0.420167
-0.756802 0.412118 0.990607
-0.958924 -0.544021 0.650288
Theta1 =
0.0841470 -0.0279420 -0.0999990 -0.0287900
0.0909300 0.0656990 -0.0536570 -0.0961400
0.0141120 0.0989360 0.0420170 -0.0750990
-0.0756800 0.0412120 0.0990610 0.0149880
-0.0958920 -0.0544020 0.0650290 0.0912950
X_new =
1.00000 0.841471 -0.279415 -0.999990
1.00000 0.909297 0.656987 -0.536573
1.00000 0.141120 0.989358 0.420167
1.00000 -0.756802 0.412118 0.990607
1.00000 -0.958924 -0.544021 0.650288
X_hc =
1.00000 0.0841470 -0.0279420 -0.0999990
1.00000 0.0909300 0.0656990 -0.0536570
1.00000 0.0141120 0.0989360 0.0420170
1.00000 -0.0756800 0.0412120 0.0990610
1.00000 -0.0958920 -0.0544020 0.0650290
res =
0.117366 0.00848932 -0.0308276 0.0355626 0.146621
0.257345 0.167004 0.00672061 -0.0761411 -0.00539850
0.160722 0.171975 0.0380896 -0.117841 -0.152454
-0.0836683 0.0188336 0.0344401 -0.0511973 -0.159344
-0.251134 -0.151623 -0.000873103 0.0625166 -0.0197337
res_x_hc =
0.0874689 0.0765812 0.0726495 0.0792885 0.0903944
0.107572 0.0985374 0.0825090 0.0742229 0.0812972
0.0287730 0.0298983 0.0165097 0.000916746 -0.00254459
-0.0764789 -0.0662286 -0.0646680 -0.0732317 -0.0840464
-0.111416 -0.101465 -0.0863901 -0.0800511 -0.0882762

*/
}

}