Search in sources :

Example 1 with IComplexNumber

use of org.nd4j.linalg.api.complex.IComplexNumber in project nd4j by deeplearning4j.

the class Nd4j method sortWithIndices.

/**
 * Sort an ndarray along a particular dimension
 *
 * @param ndarray   the ndarray to sort
 * @param dimension the dimension to sort
 * @return an array with indices and the sorted ndarray
 */
public static INDArray[] sortWithIndices(IComplexNDArray ndarray, int dimension, boolean ascending) {
    INDArray indices = Nd4j.create(ndarray.shape());
    INDArray[] ret = new INDArray[2];
    for (int i = 0; i < ndarray.vectorsAlongDimension(dimension); i++) {
        IComplexNDArray vec = ndarray.vectorAlongDimension(i, dimension);
        INDArray indexVector = indices.vectorAlongDimension(i, dimension);
        final IComplexNumber[] data = new IComplexNumber[vec.length()];
        final Double[] index = new Double[vec.length()];
        for (int j = 0; j < vec.length(); j++) {
            data[j] = vec.getComplex(j);
            index[j] = (double) j;
        }
        if (ascending)
            Arrays.sort(index, new Comparator<Double>() {

                @Override
                public int compare(Double o1, Double o2) {
                    int idx1 = (int) o1.doubleValue();
                    int idx2 = (int) o2.doubleValue();
                    return Double.compare(data[idx1].absoluteValue().doubleValue(), data[idx2].absoluteValue().doubleValue());
                }
            });
        else
            Arrays.sort(index, new Comparator<Double>() {

                @Override
                public int compare(Double o1, Double o2) {
                    int idx1 = (int) o1.doubleValue();
                    int idx2 = (int) o2.doubleValue();
                    return -Double.compare(data[idx1].absoluteValue().doubleValue(), data[idx2].absoluteValue().doubleValue());
                }
            });
        for (int j = 0; j < vec.length(); j++) {
            vec.putScalar(j, data[(int) index[j].doubleValue()]);
            indexVector.putScalar(j, index[j]);
        }
    }
    ret[0] = indices;
    ret[1] = ndarray;
    return ret;
}
Also used : IComplexNumber(org.nd4j.linalg.api.complex.IComplexNumber) IComplexNDArray(org.nd4j.linalg.api.complex.IComplexNDArray) IComplexDouble(org.nd4j.linalg.api.complex.IComplexDouble)

Example 2 with IComplexNumber

use of org.nd4j.linalg.api.complex.IComplexNumber in project nd4j by deeplearning4j.

the class ComplexNumberTests method testLogarithmFloat.

@Test
public void testLogarithmFloat() {
    IComplexDouble test = Nd4j.createDouble(1, 1);
    IComplexDouble test2 = Nd4j.createDouble(1, 1);
    IComplexNumber result = test.pow(test2);
    assertEquals(result.realComponent(), 0.3465736);
    assertEquals(result.imaginaryComponent(), 0.7853982);
}
Also used : IComplexNumber(org.nd4j.linalg.api.complex.IComplexNumber) IComplexDouble(org.nd4j.linalg.api.complex.IComplexDouble) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 3 with IComplexNumber

use of org.nd4j.linalg.api.complex.IComplexNumber in project nd4j by deeplearning4j.

the class ComplexNDArrayUtil method expi.

/**
 * Returns the exponential of a complex ndarray
 *
 * @param toExp the ndarray to convert
 * @return the exponential of the specified
 * ndarray
 */
public static IComplexNDArray expi(IComplexNDArray toExp) {
    IComplexNDArray flattened = toExp.ravel();
    for (int i = 0; i < flattened.length(); i++) {
        IComplexNumber n = flattened.getComplex(i);
        flattened.put(i, Nd4j.scalar(ComplexUtil.exp(n)));
    }
    return flattened.reshape(toExp.shape());
}
Also used : IComplexNumber(org.nd4j.linalg.api.complex.IComplexNumber) IComplexNDArray(org.nd4j.linalg.api.complex.IComplexNDArray)

Example 4 with IComplexNumber

use of org.nd4j.linalg.api.complex.IComplexNumber in project nd4j by deeplearning4j.

the class Nd4jTestsC method testParseComplexNumber.

@Test
@Ignore
public void testParseComplexNumber() {
    IComplexNumber assertion = Nd4j.createComplexNumber(1, 1);
    String parse = "1 + 1i";
    IComplexNumber parsed = Nd4j.parseComplexNumber(parse);
    assertEquals(assertion, parsed);
}
Also used : IComplexNumber(org.nd4j.linalg.api.complex.IComplexNumber) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with IComplexNumber

use of org.nd4j.linalg.api.complex.IComplexNumber in project nd4j by deeplearning4j.

the class ComplexNumberTests method testLogarithmDouble.

@Test
public void testLogarithmDouble() {
    IComplexDouble test = Nd4j.createDouble(1, 1);
    IComplexDouble test2 = Nd4j.createDouble(1, 1);
    IComplexNumber result = test.pow(test2);
    assertEquals(result.realComponent(), 0.3465735902799727);
    assertEquals(result.imaginaryComponent(), 0.7853981633974483);
}
Also used : IComplexNumber(org.nd4j.linalg.api.complex.IComplexNumber) IComplexDouble(org.nd4j.linalg.api.complex.IComplexDouble) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

IComplexNumber (org.nd4j.linalg.api.complex.IComplexNumber)8 Test (org.junit.Test)5 IComplexDouble (org.nd4j.linalg.api.complex.IComplexDouble)5 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)4 IComplexNDArray (org.nd4j.linalg.api.complex.IComplexNDArray)3 Ignore (org.junit.Ignore)1