use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.
the class VectorSerializer method serialize.
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
if (indArray.isView())
indArray = indArray.dup(indArray.ordering());
jsonGenerator.writeStartObject();
DataBuffer view = indArray.data();
jsonGenerator.writeArrayFieldStart("dataBuffer");
for (int i = 0; i < view.length(); i++) {
jsonGenerator.writeNumber(view.getDouble(i));
}
jsonGenerator.writeEndArray();
jsonGenerator.writeArrayFieldStart("shapeField");
for (int i = 0; i < indArray.rank(); i++) {
jsonGenerator.writeNumber(indArray.size(i));
}
jsonGenerator.writeEndArray();
jsonGenerator.writeArrayFieldStart("strideField");
for (int i = 0; i < indArray.rank(); i++) jsonGenerator.writeNumber(indArray.stride(i));
jsonGenerator.writeEndArray();
jsonGenerator.writeNumberField("offsetField", indArray.offset());
jsonGenerator.writeStringField("typeField", indArray instanceof IComplexNDArray ? "complex" : "real");
jsonGenerator.writeNumberField("rankField", indArray.rank());
jsonGenerator.writeNumberField("numElements", view.length());
jsonGenerator.writeStringField("orderingField", String.valueOf(indArray.ordering()));
jsonGenerator.writeEndObject();
}
use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.
the class NDArrayStrings method vectorToString.
private String vectorToString(INDArray arr, boolean summarize) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length(); i++) {
if (arr instanceof IComplexNDArray) {
sb.append(((IComplexNDArray) arr).getComplex(i).toString());
} else {
if (summarize && i > 2 && i < arr.length() - 3) {
if (i == 3)
sb.append(" ...");
} else {
double arrElement = arr.getDouble(i);
if (!dontOverrideFormat && ((Math.abs(arrElement) < this.minToPrintWithoutSwitching && arrElement != 0) || (Math.abs(arrElement) >= this.maxToPrintWithoutSwitching))) {
// switch to scientific notation
String asString = new DecimalFormat(scientificFormat).format(arrElement);
// from E to small e
asString = asString.replace('E', 'e');
sb.append(String.format("%1$" + padding + "s", asString));
} else {
if (arrElement == 0) {
sb.append(String.format("%1$" + padding + "s", 0));
} else {
sb.append(String.format("%1$" + padding + "s", decimalFormat.format(arrElement)));
}
}
}
}
if (i < arr.length() - 1) {
if (!summarize || i < 2 || i > arr.length() - 3 || (summarize && arr.length() == 6)) {
sb.append(colSep);
}
}
}
sb.append("]");
return sb.toString();
}
use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.
the class BaseLoader method doSave.
private void doSave(INDArray save, String id) throws SQLException, IOException {
Connection c = dataSource.getConnection();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
if (save instanceof IComplexNDArray) {
IComplexNDArray c2 = (IComplexNDArray) save;
Nd4j.writeComplex(c2, dos);
} else {
BinarySerde.writeArrayToOutputStream(save, bos);
}
byte[] bytes = bos.toByteArray();
PreparedStatement preparedStatement = c.prepareStatement(insertStatement());
preparedStatement.setString(1, id);
preparedStatement.setBytes(2, bytes);
preparedStatement.executeUpdate();
}
use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.
the class Eigen method eigenvectors.
/**
* Computes the eigenvalues and eigenvectors of a general matrix.
* <p/>
* For matlab users note the following from their documentation:
* The columns of V present eigenvectors of A. The diagonal matrix D contains eigenvalues.
* <p/>
* This is in reverse order of the matlab eig(A) call.
*
* @param A the ndarray to getFloat the eigen vectors for
* @return 2 arrays representing W (eigen vectors) and V (normalized eigen vectors)
*/
public static IComplexNDArray[] eigenvectors(INDArray A) {
assert A.columns() == A.rows();
// setting up result arrays
INDArray WR = Nd4j.create(A.rows());
INDArray WI = WR.dup();
INDArray VR = Nd4j.create(A.rows(), A.rows());
INDArray VL = Nd4j.create(A.rows(), A.rows());
Nd4j.getBlasWrapper().geev('v', 'v', A.dup(), WR, WI, VL, VR);
// transferring the result
IComplexNDArray E = Nd4j.createComplex(WR, WI);
IComplexNDArray V = Nd4j.createComplex(A.rows(), A.rows());
for (int i = 0; i < A.rows(); i++) {
if (E.getComplex(i).isReal()) {
IComplexNDArray column = Nd4j.createComplex(VR.getColumn(i));
V.putColumn(i, column);
} else {
IComplexNDArray v = Nd4j.createComplex(VR.getColumn(i), VR.getColumn(i + 1));
V.putColumn(i, v);
V.putColumn(i + 1, v.conji());
i += 1;
}
}
return new IComplexNDArray[] { Nd4j.diag(E), V };
}
use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.
the class Nd4j method sort.
/**
* Sort an ndarray along a particular dimension
*
* @param ndarray the ndarray to sort
* @param dimension the dimension to sort
* @return the sorted ndarray
*/
public static IComplexNDArray sort(IComplexNDArray ndarray, int dimension, boolean ascending) {
for (int i = 0; i < ndarray.vectorsAlongDimension(dimension); i++) {
IComplexNDArray vec = ndarray.vectorAlongDimension(i, dimension);
IComplexNumber[] data = new IComplexNumber[vec.length()];
for (int j = 0; j < vec.length(); j++) {
data[j] = vec.getComplex(j);
}
if (ascending)
Arrays.sort(data, new Comparator<IComplexNumber>() {
@Override
public int compare(IComplexNumber o1, IComplexNumber o2) {
return Double.compare(o1.asDouble().absoluteValue().doubleValue(), o2.asDouble().absoluteValue().doubleValue());
}
});
else
Arrays.sort(data, new Comparator<IComplexNumber>() {
@Override
public int compare(IComplexNumber o1, IComplexNumber o2) {
return -Double.compare(o1.asDouble().absoluteValue().doubleValue(), o2.asDouble().absoluteValue().doubleValue());
}
});
for (int j = 0; j < vec.length(); j++) vec.putScalar(j, data[j]);
}
return ndarray;
}
Aggregations