[SciPy-user] complex vector scalar product: wrong implementation
Robert Kern
rkern at ucsd.edu
Mon Jan 10 03:03:19 EST 2005
Vladimir A. Roudnev wrote:
> Dear All,
>
> I do not know whether the problem I've found is already known or not,
> but I think it makes no harm if I report it.
>
> As is known, for real vectors a matrix-vector multiplication and dot
> (aka scalar, aka inner) product can be treated as the same operation.
> But for complex vectors this not the case: scalar product in vector
> spaces over complex numbers must satisfy the condition
> innerproduct(z1,z2)==conjugate(innerproduct(z2,z1))
> Scipy implementation, however, does not satisfy this property, what can
> lead to serious complications when adapting real vector algorithms to
> complex arithmetics. In particular, the existing implementation breaks
> the complex vector space metric:
> >>> import scipy
> >>> a=scipy.array(range(3),'D')
> >>> print a
> [ 0.+0.j 1.+0.j 2.+0.j]
> >>> scipy.innerproduct(a,a)
> (5+0j)
> >>> import math
> >>> a=a*scipy.exp(math.pi*1.0j/4) # multiply by a unit scalar
> >>> print a
> [ 0. +0.j 0.70710678+0.70710678j 1.41421356+1.41421356j]
> >>> scipy.innerproduct(a,a) # wrong scalar product, PURE IMAGINARY
> RESULT
> (7.850978981510659e-16+5j)
> >>> scipy.innerproduct(scipy.conjugate(a),a) # the correct result, real
> (5+0j)
>
> I guess, that the problem is inhereted from Numeric.
Yes, it is indeed inherited from Numeric. If you are doing serious
linear algebra with complex matrices (and not everyone who uses
innerproduct on complex arrays does), I suggest you write a function
that does the appropriate conjugation.
def cdot(a, b):
return dot(conjugate(a), b)
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the SciPy-User
mailing list