package com.intel.daal.examples.datasource;
import java.nio.DoubleBuffer;
import com.intel.daal.data_management.data.PackedSymmetricMatrix;
import com.intel.daal.data_management.data.NumericTable;
import com.intel.daal.examples.utils.Service;
import com.intel.daal.services.DaalContext;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
class DataStructuresPackedSymmetric {
private static final int nDim = 5;
private static final int nRead = 5;
private static final int firstReadRow = 0;
private static DaalContext context = new DaalContext();
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
System.out.println("Packed symmetric matrix example");
int readFeatureIdx;
double[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
PackedSymmetricMatrix dataTable = new PackedSymmetricMatrix(context, data, nDim, NumericTable.StorageLayout.lowerPackedSymmetricMatrix);
DoubleBuffer dataDouble = DoubleBuffer.allocate(nRead * nDim);
dataDouble = dataTable.getBlockOfRows(firstReadRow, nRead, dataDouble);
printDoubleBuffer(dataDouble, nDim, nRead, "Print " + nRead + " rows from packed data array as double:");
dataTable.releaseBlockOfRows(firstReadRow, nRead, dataDouble);
DoubleBuffer dataDoubleFeatures = DoubleBuffer.allocate((int) nRead);
readFeatureIdx = 2;
dataDoubleFeatures = dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nRead, dataDoubleFeatures);
printDoubleBuffer(dataDoubleFeatures, 1, nRead, "Print the third feature of homogeneous data:");
dataDoubleFeatures.put(1, 239);
dataTable.releaseBlockOfColumnValues(readFeatureIdx, firstReadRow, nRead, dataDoubleFeatures);
dataDouble = dataTable.getBlockOfRows(firstReadRow, nRead, dataDouble);
printDoubleBuffer(dataDouble, nDim, nRead, "Print " + nRead + " rows from packed data array as double:");
dataTable.releaseBlockOfRows(firstReadRow, nRead, dataDouble);
double[] dataFromNumericTable = (double[])dataTable.getDataObject();
Service.printMatrix(dataFromNumericTable, nDim * (nDim + 1) / 2, 1, "Data from getDataObject:");
context.dispose();
}
private static void printDoubleBuffer(DoubleBuffer buf, long nColumns, long nRows, String message) {
int step = (int) nColumns;
System.out.println(message);
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nColumns; j++) {
System.out.format("%6.3f ", buf.get(i * step + j));
}
System.out.println("");
}
System.out.println("");
}
}