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;
}
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);
}
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());
}
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);
}
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);
}
Aggregations