Class TensorWriter
java.lang.Object
org.flag4j.io.TensorWriter
The TensorWriter
class provides static methods for serializing and saving tensors, matrices, and vectors to files.
Supported formats include:
- Binary serialization using
ObjectOutputStream
- Comma-separated values (CSV) format for dense real, complex, and field matrices
- Matrix Market Exchange Format for dense and sparse (COO or CSR) matrices (real or complex). Both COO and CSR matrices are written in the coordinate (COO) format. Symmetry is not considered and all matrices will be written as general matrices regardless of the symmetry.
Usage Example
// Write a real matrix to a Matrix Market Exchange Format file
Matrix matrix1 = new Matrix(...);
TensorWriter.toMatrixMarket("matrix1.mtx", matrix1);
// Write a complex COO matrix to a Matrix Market Exchange Format file
CooCMatrix cooMatrix = new CooCMatrix(...);
TensorWriter.toMatrixMarket("cooMatrix.mtx", cooMatrix, "this is a comment");
// Write a real matrix to a CSV file
Matrix matrix2 = new Matrix(...);
TensorWriter.toCsv("matrix2.csv", matrix2);
TensorWriter.toCsv("matrix2.csv", matrix2, ";"); // Specify delimiter.
// Serialize a tensor to a binary file
AbstractTensor tensor = new AbstractTensor(...);
TensorWriter.write("tensor.ser", tensor);
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
toCsv
(String fileName, MatrixMixin<?, ?, ?, ?> src) Writes a matrix to a CSV file.static void
toCsv
(String filePath, MatrixMixin<?, ?, ?, ?> src, String delimiter) Writes a matrix to a CSV file.static void
toMatrixMarket
(String fileName, MatrixMixin<?, ?, ?, ?> src, String... comments) Writes a matrix to a Matrix Market Exchange Format file.static void
write
(String fileName, AbstractTensor<?, ?, ?> src) Writes a tensor to a file using aObjectOutputStream
.
-
Method Details
-
write
Writes a tensor to a file using aObjectOutputStream
. If the write operation fails it will terminate but no exception will be thrown. To determine if the write was successful, see the return value: if the write was successful, true is returned; if the write fails,then false is returned. is returned.- Parameters:
fileName
- File name, including extension, of the file to write the serialized matrix to.src
- Source object to write to the specified file.- Throws:
IOException
- If any I/O error occurs when attempting to write to file.
-
toMatrixMarket
public static void toMatrixMarket(String fileName, MatrixMixin<?, ?, throws IOException?, ?> src, String... comments) Writes a matrix to a Matrix Market Exchange Format file.
If
src
is a CSR matrix, it will be converted to a COO matrix and saved as a coordinate matrix. All matrices are stored in general form regardless of the symmetry.Supported matrices:
Matrix
(real dense)CMatrix
(complex dense)CooMatrix
(real sparse COO)CsrMatrix
(real sparse CSR, converted to COO)CooCMatrix
(complex sparse COO)CsrCMatrix
(complex sparse CSR, converted to COO)
- Parameters:
fileName
- Path of the file to write to.src
- Matrix to write to file.comments
- Comments to prepend to file. Each comment will be written to its own line. May benull
or length zero (i.e. nothing passed); in this case the parameter will be ignored.- Throws:
IOException
- If an I/O error occurs.IllegalArgumentException
- Ifsrc
is not a supported matrix type for writing to a Matrix Market Exchange Format file.
-
toCsv
Writes a matrix to a CSV file. Matrix must be an instance of eitherMatrix
orCMatrix
. Uses "," as the default delimiter. To specify the delimiter usetoCsv(String, MatrixMixin, String)
.- Parameters:
fileName
- Name of the file to save matrix to.src
- Matrix to save to CSV file.- Throws:
IOException
- If any I/O error occurs when attempting to write to file.- See Also:
-
toCsv
public static void toCsv(String filePath, MatrixMixin<?, ?, throws IOException?, ?> src, String delimiter) - Parameters:
filePath
- Path of the file to save matrix to.src
- Matrix to save to CSV file.- Throws:
IOException
- If any I/O error occurs when attempting to write to file.IllegalArgumentException
- If!(src instanceof Matrix || src instanceof CMatrix)
.- See Also:
-