Can't create complex matrices in nd4j

97 Views Asked by At

I want to create a complex matrix in nd4j with the method `Nd4j.complexOnes(new int[]{1});`.
Sadly I get the following error:
Exception in thread "main" java.lang.UnsupportedOperationException
    at org.nd4j.linalg.api.complex.BaseComplexNDArray.<init>(BaseComplexNDArray.java:244)
    at org.nd4j.linalg.api.complex.BaseComplexNDArray.<init>(BaseComplexNDArray.java:532)
    at org.nd4j.linalg.cpu.nativecpu.complex.ComplexNDArray.<init>(ComplexNDArray.java:227)
    at org.nd4j.linalg.cpu.nativecpu.CpuNDArrayFactory.createComplex(CpuNDArrayFactory.java:257)
    at org.nd4j.linalg.factory.BaseNDArrayFactory.createComplex(BaseNDArrayFactory.java:1558)
    at org.nd4j.linalg.factory.BaseNDArrayFactory.createComplex(BaseNDArrayFactory.java:1672)
    at org.nd4j.linalg.factory.BaseNDArrayFactory.complexOnes(BaseNDArrayFactory.java:1281)
    at org.nd4j.linalg.factory.Nd4j.complexOnes(Nd4j.java:5809)
    at de.GameOfReal.Multipliers.<init>(Multipliers.java:34)
    at de.GameOfReal.Multipliers.<init>(Multipliers.java:20)
    at de.GameOfReal.GameOfReal.<init>(GameOfReal.java:33)
    at de.GameOfReal.Main.<init>(Main.java:12)
    at de.GameOfReal.Main.main(Main.java:26)

I am using the following versions:

implementation 'org.nd4j:nd4j-api:0.9.1'
implementation 'org.nd4j:nd4j-native:0.9.1'
implementation 'org.nd4j:nd4j-native-platform:0.9.1'

To give more context, I wanted to use Fourier Transformation on a normal NDArray, but any FFT function did not work and I found out that it was caused by not being able to create complex matrices.

I hope that I could give enough details, if you have questions, I'll answer right away.

with kind regards,
Felix

1

There are 1 best solutions below

1
Adam Gibson On

Nd4j does not support complex ndarrays anymore. We did at one point but a lot of our ops did not directly support it. If you want to use FFTs with nd4j you'll have to do some sort of an approxmation or use a different library and convert you nd4j array to a toDoubleMatrix()/toDoubleVector() and back.

Note that this will of course cause some performance penalties including copying data and a bit of memory overhead if you choose to go the copy route.

Apologies for the confusion there but hopefully that clears up why you don't see as many docs on it.

For the in memory representation a complex ndarray is usually just the components laid out as: real, imaginary, real, imaginary

in the in memory buffer. Nothing would stop you from creating that as well.