# [SciPy-User] problem with dot for complex matrices

Ryan Krauss ryanlists@gmail....
Tue Mar 27 14:14:57 CDT 2012

```Thanks to Nils and Josef for responding so quickly.

I don't know if I feel more or less confused:

If I copy and paste the code from my email, I can't reproduce my own problem:

In [5]: A = array([[-0.22740265-1.63857451j, -0.07740957-0.55847886j],
...:       [-3.20602957-4.93959054j, -0.36746252-1.68352465j]])

In [6]: B = array([[ -3.74729148e-05-0.0005937j ],
...:       [  7.96025801e-04+0.01137658j]])

In [7]: -1*dot(A,B)
Out[7]:
array([[-0.00532766+0.00112881j],
[-0.01604775+0.00343208j]])

But if I use the matrices returned by my function I get the wrong result:

In [27]: -1*numpy.dot(submat_inv_num, augcol_num)
Out[27]:
array([[  5.30985737e-05+0.00038316j],
[  1.72370377e-04+0.00115503j]])

Even though they seem to be very nearly the same arrays:

In [11]: submat_inv_num
Out[11]:
array([[-0.22740265-1.63857451j, -0.07740957-0.55847886j],
[-3.20602957-4.93959054j, -0.36746252-1.68352465j]])

In [12]: A
Out[12]:
array([[-0.22740265-1.63857451j, -0.07740957-0.55847886j],
[-3.20602957-4.93959054j, -0.36746252-1.68352465j]])

In [13]: submat_inv_num.dtype
Out[13]: dtype('complex128')

In [14]: A.dtype
Out[14]: dtype('complex128')

In [15]: A.shape
Out[15]: (2, 2)

In [16]: submat_inv_num.shape
Out[16]: (2, 2)

In [17]: submat_inv_num - A
Out[17]:
array([[  1.18593824e-09 +3.83908949e-09j,
1.45239888e-09 +4.30272740e-09j],
[ -2.42228770e-09 -2.12942108e-09j,
-4.36657455e-09 +2.14619789e-09j]])

In [20]: augcol_num.dtype
Out[20]: dtype('complex128')

In [21]: augcol_num.shape
Out[21]: (2, 1)

In [22]: B.dtype
Out[22]: dtype('complex128')

In [23]: B.shape
Out[23]: (2, 1)

In [18]: augcol_num - B
Out[18]:
array([[ -2.57355850e-14 -5.09694849e-11j],
[ -1.51298895e-13 +2.85492891e-09j]])

Any ideas as to what might be going on?

FYI,

In [24]: numpy.__version__
Out[24]: '1.6.1'

In [25]: scipy.__version__
Out[25]: '0.10.0'

Thanks again,

Ryan

On Tue, Mar 27, 2012 at 1:58 PM, Nils Wagner
<nwagner@iam.uni-stuttgart.de> wrote:
> On Tue, 27 Mar 2012 13:48:14 -0500
>  Ryan Krauss <ryanlists@gmail.com> wrote:
>>
>> I am loosing my mind while trying to debug some code.  I am trying to
>> find the cause of some differences between numpy analysis and analysis
>> done first in maxima and then converted to python code. The maxima
>> approach is more difficult to do, but seems to lead to the correct
>> answers.  The core issue seems to be one dot product of a 2x2 and a
>> 2x1 that are both complex numbers:
>>
>> here is the 2x2:
>>
>> ipdb> submatinv
>> array([[-0.22740265-1.63857451j, -0.07740957-0.55847886j],
>>      [-3.20602957-4.93959054j, -0.36746252-1.68352465j]])
>>
>> here is the 2x1:
>>
>> ipdb> augcol
>> array([[ -3.74729148e-05-0.0005937j ],
>>      [  7.96025801e-04+0.01137658j]])
>>
>> verifying their shapes and data types:
>>
>> ipdb> submatinv.shape
>> (2, 2)
>> ipdb> submatinv.dtype
>> dtype('complex128')
>> ipdb> augcol.shape
>> (2, 1)
>> ipdb> augcol.dtype
>> dtype('complex128')
>>
>> I need to compute this result:
>>
>> ipdb> -1*numpy.dot(submatinv,augcol)
>> array([[  5.30985737e-05+0.00038316j],
>>      [  1.72370377e-04+0.00115503j]])
>>
>> If I hard code how to do the matrix multiplication, I get the correct
>> answer (it agrees with Maxima):
>>
>> For the first element:
>> ipdb> -1*(submatinv[0,0]*augcol[0,0]+submatinv[0,1]*augcol[1,0])
>> (-0.005327660633034575+0.0011288088216130766j)
>>
>> For the second
>> ipdb> -1*(submatinv[1,0]*augcol[0,0]+submatinv[1,1]*augcol[1,0])
>> (-0.016047752110848554+0.003432076134378004j)
>>
>> What is the dot product doing if it isn't dotting row by column?
>>
>> I am not seeing something.
>>
>> Thanks,
>>
>> Ryan
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
> Hi Ryan,
>
> I cannot reproduce your np.dot results, here.
>
> python -i ryan_1.py
> [[-0.00532766+0.00112881j]
>  [-0.01604775+0.00343208j]]
> 1.7.0.dev-3503c5f
>
> Nils
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```