Complex numbers support in array module

I know about numpy, but I would really like to see it in the built-in python module, but I was wondering if this could be a thing.

Any thoughts about this?

It’s an open ended can of worms. Where do you stop after complex?
Tuples, coordinates, matrixes? Decimals?

Personally I’d be inclined to write a class which used an array
underneath and assembled pairs of values as complex numbers. Publish it
for reuse if the outcoming has pleasing heft and feels easy for others
to use.

Cheers,
Cameron Simpson cs@cskk.id.au

Why? The array module “defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers”. (And each type has multiple machine representations/encodings. Complex numbers are not a basic (machine) type.

Is there any way to make an array of structs? If so, a complex could be seen as a struct of two floats, and you’d have what you want.

Not really big on the array module myself though, so I’ve no idea.

Is there any way to make an array of structs? If so, a complex could be
seen as a struct of two floats, and you’d have what you want.

Not as such - it is basicly an efficient storage for basic C types, and
IIRC the basis for the numpy arrays, at least of types like that.

But you could certainly pair up adjacent values to express complex
values with little work.

Not really big on the array module myself though, so I’ve no idea.

It’s pretty basic, but also quite cool for the bulk access/processing
side of basic values.

I ended up using it to good effect in my cs.timeseries modules, which
I’m using to store data from my solar inverter.

Cheers,
Cameron Simpson cs@cskk.id.au

Maybe you are overthinking it, I’m just talking about to having support for the standard library complex.h which essentially will introduce 3 new types float complex, double comple, long double complex. I mean, the operations required for complex numbers are implemented on their own in cmath, maybe they should be using the already existing complex.h library in C.

They are not basic to the machine, but they could add a lot of new use cases using pure-python

If you wanted to do that, you could stay with fixed length tuples and call it a day, but the array structure, has the added benefit of “little metadata” when allocating.

And an array would be just an array, and users can add an extra layer on top of it to make it multidimensional, as part of a use case, or pure python library with no C extensions.


I really think this could enable a lot or cool things, without the bulk of numerical libraries on top.


Just as a reference, the man page for complex.h

That would be a occasionally very useful addition. One still relevant use of the standard array module is to return results from numerical computation in a C extension when e.g. numpy is not available. The three complex cases are the only ones that need special treatment.

I have always treated the C complex types as equivalent to an array of two floating point numbers of the same width. That’s how I understand the standard:

Each complex type has the same object representation and alignment requirements as an array of two elements of the corresponding real type (float for float complex, double for double complex, long double for long double complex). The first element of the array holds the real part, and the second element of the array holds the imaginary component.

That would mean you can implement the complex types in array without even using <complex.h> by creating arrays of adjacent real and imaginary components. If your C has complex type support, it will be able to read the array natively, otherwise you can rely on the real representation.

I do agree that It is possible to implement it as two adjacent arrays without the need for complex.h, but I believe that the “appealing” part of having the array mechanism is to have a warranty of a consecutive homogenous sequence of values, leaving no place for interpretation, except for the positional access to the value in memory, having it that way as you say it will introduce a layer of interpretation that will be “bloating” the array object. By using the standard complex from C the array implementation should just be essentially extending the types supported.

I would think that if python has cmath module and a complex type, it would be using complex.h instead of re-inventing the wheel, but after exploring the source code, it looks like they’ve implemented their own methods.

Not sure why, I would really like to see a rational about that.