# Weird matrix multiplication results from MKL

**URL:** <https://discourse.julialang.org/t/weird-matrix-multiplication-results-from-mkl/104623>\
**Category:** New to Julia\
**Tags:** question, package, mkl, linearalgebra\
**Created:** [October 5, 2023, 5:50pm UTC](https://discourse.julialang.org/t/weird-matrix-multiplication-results-from-mkl/104623 "2023-10-05T17:50:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacques1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacques1/32/45780_2.png) [@jacques1](https://discourse.julialang.org/u/jacques1)\
**Post date:** [October 5, 2023, 5:50pm UTC](https://discourse.julialang.org/t/weird-matrix-multiplication-results-from-mkl/104623/1 "2023-10-05T17:50:41Z")

</div>

Hi, I’m trying to use a simple OLS solution to an overdetermined linear system using MKL and LinearAlgebra, with coefficient matrix

‘’’  
M = [200000 0 0 (2e5)^2 0; 0 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0; 0 2e5 0 0 (2e5)^2; 0 0 3e5 0 (3e5)^2]  
‘’’  
and observation

‘’’  
y = [2e5 0 0 0 4e5 9e5]  
‘’’  
When I try to get the ols solution of the system with  
‘’’  
(M’ \* M)^(-1) \* M’ \* y’  
‘’’  
the output is  
‘’’  
5×1 Matrix{Float64}:  
1.9780612128670327  
3.969978473845913  
2.0318402149168833  
9.652545605920437e-6  
5.091301176166074e-6  
‘’’  
but this is incorrect. In particular, if I first store the result  
‘’’  
u = (M’ \* M)^(-1) \* M’  
‘’’  
and then compute

‘’’  
u \* y’  
‘’’  
the output is instead  
‘’’  
5×1 Matrix{Float64}:  
1.3066744941170327  
0.645027301970913  
1.7964886524168833  
1.436876312377322e-5  
-5.193294015312655e-6  
‘’’  
Any pointers on what I am doing wrong/why this is happening?

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [October 5, 2023, 6:21pm UTC](https://discourse.julialang.org/t/weird-matrix-multiplication-results-from-mkl/104623/2 "2023-10-05T18:21:57Z")

</div>

Perhaps,

```julia
julia> det(M'M)
0.0

```

Might be the issue. Why is MKL allowing the inverse to be taken? (The column 4 is twice that of column 1.)

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [October 5, 2023, 6:32pm UTC](https://discourse.julialang.org/t/weird-matrix-multiplication-results-from-mkl/104623/3 "2023-10-05T18:32:29Z")

</div>

Aside from the issue with the singular matrix, you almost never want to use the normal form solution `(M'M)^-1 M' y`, and you should use `M \ y` instead which uses better algorithms. The normal form doubles the condition number of the matrix to be inverted.
