Hello.

The class "ComplexUtils" deal with multi-dimensional arrays that hold
instances of the "Complex" class.
I've recently encountered a use-case where it was pointed out that storing
many "Complex" instances (as seems the purpose of these utilities) is quite
inefficient memory-wise as each instance will take 32 bytes[1] while the
real and imaginary parts would only take 16 bytes as 2 primitive "double"s.
This is compounded by multi-dimensional array where each sub-dimensional
element is an array object (and thus takes another additional 16 bytes).
For example, a
    double[10][5][4]
would take
    16 * (1 + 10 * 5) + 10 * 5 * 4 * 8
  = 2416 bytes.
Assuming that in the above array, the last dimension holds 2 complex numbers,
the same data can be represented as
    Complex[10][5][2]
that would take
    16 * ((1 + 10 * 5) + (10 * 5 * 2)) + 10 * 5 * 2 * 2 * 8
  = 4016 bytes.
In both cases, the payload (10 * 5 * 2 complex numbers) is
    10 * 5 * 2 * 2 * 8
  = 1600 bytes.
If stored in a one-dimensional array, the size in memory would be 1616 bytes.
Thus in the case of a data cube holding 100 complex numbers, a 3D array
takes 1.5 (primitives) or 2.5 ("Complex" objects) more memory than a 1D array.
If this is correct, I wonder whether we should advertise such a "ComplexUtils"
class.  It would perhaps be preferable to propose a data cube abstraction.[2]

WDYT?

Regards,
Gilles

[1] https://www.baeldung.com/java-size-of-object
[2] Based on 
http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math4/util/MultidimensionalCounter.html

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to