Numerical instability for complex dot product with Im=0.0 in native implementation
- Dominant language
- Kotlin
- Stars
- 734
- Forks
- 51
- Avg merge
- 9h 52m
- Merged PRs (30d)
- 6
Description
# Problem
When calculating the dot product of two matrices with all Im=0.0, the native (openblas) implementation introduces a lot of noise to the Im parts. As the Im values are varying highly, I cannot cleanly set them to 0.0.
Some of the oberserved values for Im at (1,1) of my test (see further down) are:
`(-7.89192892962405E207)i`, `(3.810313449527881E150)i`, `(2.782091336866409E154)i`, `(-2.0023513584615535E198)i`
The correct one would just be `(0.0)i`
As I understand, some noise like this is obviously expected due to how floating point numbers work in computers. However, I don't really expect it to be so highly variable, although I am not very experienced in this kind of programming. Correct me if I am wrong.
In about 50% of the runs, the result something very tiny like `(1.69472052672044E-264)i`, which can be easily corrected by applying some epsilon tolerance.
Changing from the native implementation with openblas to the kotlin implementation, the issue is gone. The results are always correctly Im=`(0.0)i`.
## My test case:
given
$`M=\begin{pmatrix}
\frac{1}{\sqrt{2}} + 0.0i & -\frac{1}{\sqrt{2}} + 0.0i\\
-\frac{1}{\sqrt{2}} + 0.0i & -\frac{1}{\sqrt{2}} + 0.0i
\end{pmatrix}`$
I want to calculate
$`X=MM^\dagger`$
where $`M^\dagger`$ is the conjugate transpose (Hermitian transpose)
which I write in Kotlin as
```
val ONE_OVER_SQRT_TWO = (1.0 / sqrt(2.0)) + i(0.0)
val M = mk.ndarray(mk[mk[ONE_OVER_SQRT_TWO, -ONE_OVER_SQRT_TWO], mk[-ONE_OVER_SQRT_TWO, -ONE_OVER_SQRT_TWO]])
val X = M.dot(M.transpose().conj()
```
Contributor guide
Research direction
Start by reproducing the supplied Kotlin matrix multiplication using the native OpenBLAS implementation, then compare it with the Kotlin implementation. Trace the native complex dot-product path and determine whether the imaginary output is unstable for zero-imaginary inputs; done means the behavior is corrected or its expected limitations are covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100