[SciPy-user] Real Array Expressed as Complex Array

Anne Archibald peridot.faceted@gmail....
Thu Jul 3 15:57:00 CDT 2008


2008/7/3 Lorenzo Isella <lorenzo.isella@gmail.com>:
> Hello,
>> 814v3268b9e5rca34b98bb7b84b57@mail.gmail.com> Content-Type:
>> text/plain; charset=ISO-8859-1 On Thu, Jul 3, 2008 at 9:39 AM, Lorenzo
>> Isella <lorenzo.isella@gmail.com> wrote:
>>> > Since the result was that the array pot_ext_dimensionless is real, how
>>> > comes that it is expressed as a complex array (though the imaginary
>>> > part is always zero)?
>>>
>>
>> It all depends on how you're calculating the pot_ext_dimensionless
>> array; clearly somewhere in there an operation makes it complex.
>> You'll have to show us how it's calculated.
>>
> I think I solved the problem: I introduced some real elements taken from
> an array that has also some complex entries into pot_ext_dimensionless;
> although all the elements of pot_ext_dimensionless are all real, somehow
> scipy retains memory of these, once-existing, complex entries.

The key idea is that arrays have a data type: that is, each numpy
array, upon creation, specifies the type of all its contents. So your
numpy arrays are marked as containing complex numbers. The fact that
the imaginary part of these complex numbers is approximately or
exactly zero isn't relevant; they are still stored as a pair of
floating-point numbers. If you prefer, you can think of their type as
"potentially complex numbers". In any case, such numbers are printed
as a+bj even if a or b is zero, and various arithmetic operations
treat them as complex numbers. If you know that the answer should be
real, and you want to represent them more conveniently and
efficiently, you can simply take the real part.

>> You can always access the (real,imaginary) part of a complex array
>> with (pot_ext_dimensionless.real, pot_ext_dimensionless.imag)
>>
>> But be careful, these arrays are not contiguous (they're a view into
>> the complex array). That wrinkle has bitten me before, but I can't
>> quite recall the circumstances.  You can always make them contiguous
>> with numpy.ascontiguousarray().
>>
>
> This sounds important and not 100% clear to me. Do you mean that if I
> have a complex array z and call real.z, I do not get in general an array
> with the same length as z, since purely imaginary entries are "skipped"
> rather than appearing as entries with zero real part, as one would expect?

No. Taking "X.real" is the same as the mathematical operation of
taking the real part:it throws away any imaginary part and interprets
what's left as real, whether it's zero or not. "Contiguous", in this
context, is a technical feature of numpy arrays that you should almost
never need to care about. (Numpy arrays can be homogeneous blocks of
memory, but they can also be homogeneous elements "strided" through
memory with other data in between. A few functions cannot deal with
this striding.)

Anne


More information about the SciPy-user mailing list