|
| 1 | +package org.matheclipse.core.convert.matlab; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.nio.ByteBuffer; |
| 6 | +import org.hipparchus.linear.AnyMatrix; |
| 7 | +import org.hipparchus.linear.Array2DRowRealMatrix; |
| 8 | +import org.hipparchus.linear.RealMatrix; |
| 9 | +import org.matheclipse.core.expression.ASTRealMatrix; |
| 10 | +import org.matheclipse.core.expression.F; |
| 11 | +import org.matheclipse.core.interfaces.IAST; |
| 12 | +import org.matheclipse.core.interfaces.IASTAppendable; |
| 13 | +import org.matheclipse.core.interfaces.IExpr; |
| 14 | +import us.hebi.matlab.mat.format.Mat5; |
| 15 | +import us.hebi.matlab.mat.format.Mat5File; |
| 16 | +import us.hebi.matlab.mat.types.AbstractMatrixBase; |
| 17 | +import us.hebi.matlab.mat.types.Array; |
| 18 | +import us.hebi.matlab.mat.types.MatFile; |
| 19 | +import us.hebi.matlab.mat.types.MatlabType; |
| 20 | +import us.hebi.matlab.mat.types.Matrix; |
| 21 | +import us.hebi.matlab.mat.types.Source; |
| 22 | +import us.hebi.matlab.mat.types.Sources; |
| 23 | + |
| 24 | +/** |
| 25 | + * Matlab file format conversion to Symja |
| 26 | + */ |
| 27 | +public class Mat5Symja { |
| 28 | + |
| 29 | + private static AnyMatrix convertToAnyMatrix(AbstractMatrixBase input, |
| 30 | + Class<? extends AnyMatrix> clazz) { |
| 31 | + final int rows = input.getNumRows(); |
| 32 | + final int cols = input.getNumCols(); |
| 33 | + if (clazz.isAssignableFrom(ASTRealMatrix.class)) { |
| 34 | + RealMatrix realMatrix = convertToArray2DRowRealMatrix(input, rows, cols); |
| 35 | + return new ASTRealMatrix(realMatrix, false); |
| 36 | + } else if (clazz.isAssignableFrom(IAST.class)) { |
| 37 | + IASTAppendable astMatrix = F.ListAlloc(rows); |
| 38 | + for (int i = 0; i < rows; i++) { |
| 39 | + astMatrix.append(F.ListAlloc(cols)); |
| 40 | + } |
| 41 | + for (int col = 0; col < cols; col++) { |
| 42 | + for (int row = 0; row < rows; row++) { |
| 43 | + astMatrix.setPart(input.getDouble(row, col), row, col); |
| 44 | + } |
| 45 | + } |
| 46 | + } else if (clazz.isAssignableFrom(RealMatrix.class)) { |
| 47 | + return convertToArray2DRowRealMatrix(input, rows, cols); |
| 48 | + } |
| 49 | + return F.NIL; |
| 50 | + } |
| 51 | + |
| 52 | + private static RealMatrix convertToArray2DRowRealMatrix(Matrix input, final int rows, |
| 53 | + final int cols) { |
| 54 | + RealMatrix realMatrix = new Array2DRowRealMatrix(rows, cols); |
| 55 | + for (int col = 0; col < cols; col++) { |
| 56 | + for (int row = 0; row < rows; row++) { |
| 57 | + realMatrix.setEntry(row, col, input.getDouble(row, col)); |
| 58 | + } |
| 59 | + } |
| 60 | + return realMatrix; |
| 61 | + } |
| 62 | + |
| 63 | + public static IAST getTensor(AbstractMatrixBase baseMatrix) { |
| 64 | + int[] dimensions = baseMatrix.getDimensions(); |
| 65 | + if (dimensions.length == 2) { |
| 66 | + if (baseMatrix.getType() == MatlabType.Double) { |
| 67 | + return (ASTRealMatrix) Mat5Symja.convertToAnyMatrix(baseMatrix, ASTRealMatrix.class); |
| 68 | + } |
| 69 | + if (baseMatrix.getType() == MatlabType.Sparse) { |
| 70 | + return (IAST) Mat5Symja.convertToAnyMatrix(baseMatrix, IAST.class); |
| 71 | + } |
| 72 | + } |
| 73 | + if (baseMatrix.getType() == MatlabType.Sparse) { |
| 74 | + return F.NIL; |
| 75 | + // int[] indices = new int[dimensions.length]; |
| 76 | + // final int size = dimensions[0]; |
| 77 | + // ISparseArray sparse = |
| 78 | + // F.sparseArray(F.List(F.Rule(F.List(1, 2, 3), F.b), F.Rule(F.List(1, 4, 5), F.a))); |
| 79 | + // IASTAppendable result = F.ListAlloc(); |
| 80 | + // for (int i = 0; i < size; i++) { |
| 81 | + // indices[0] = i; |
| 82 | + // getSparseRecursive(baseMatrix, dimensions, indices, 1, result); |
| 83 | + // } |
| 84 | + // return result; |
| 85 | + } |
| 86 | + int[] indices = new int[dimensions.length]; |
| 87 | + final int size = dimensions[0]; |
| 88 | + IASTAppendable result = F.ListAlloc(size); |
| 89 | + for (int i = 0; i < size; i++) { |
| 90 | + indices[0] = i; |
| 91 | + getTensorRecursive(baseMatrix, dimensions, indices, 1, result); |
| 92 | + } |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + private static void getSparseRecursive(AbstractMatrixBase baseMatrix, int[] dimensions, |
| 97 | + int[] indices, int indexCounter, IASTAppendable result) { |
| 98 | + int newCounter = indexCounter + 1; |
| 99 | + if (indexCounter == dimensions.length) { |
| 100 | + MatlabType type = baseMatrix.getType(); |
| 101 | + switch (type) { |
| 102 | + case UInt8: |
| 103 | + result.append(baseMatrix.getBoolean(indices)); |
| 104 | + return; |
| 105 | + case Double: |
| 106 | + result.append(baseMatrix.getDouble(indices)); |
| 107 | + return; |
| 108 | + case Single: |
| 109 | + result.append(baseMatrix.getFloat(indices)); |
| 110 | + return; |
| 111 | + case Sparse: |
| 112 | + double d = baseMatrix.getDouble(indices); |
| 113 | + result.append(d); |
| 114 | + return; |
| 115 | + } |
| 116 | + return; |
| 117 | + } |
| 118 | + final int size = dimensions[indexCounter]; |
| 119 | + IASTAppendable subRow = F.ListAlloc(); |
| 120 | + for (int i = 0; i < size; i++) { |
| 121 | + indices[indexCounter] = i; |
| 122 | + getTensorRecursive(baseMatrix, dimensions, indices, newCounter, subRow); |
| 123 | + } |
| 124 | + result.append(subRow); |
| 125 | + } |
| 126 | + |
| 127 | + private static void getTensorRecursive(AbstractMatrixBase baseMatrix, int[] dimensions, |
| 128 | + int[] indices, int indexCounter, IASTAppendable result) { |
| 129 | + int newCounter = indexCounter + 1; |
| 130 | + if (indexCounter == dimensions.length) { |
| 131 | + MatlabType type = baseMatrix.getType(); |
| 132 | + switch (type) { |
| 133 | + case UInt8: |
| 134 | + result.append(baseMatrix.getBoolean(indices)); |
| 135 | + return; |
| 136 | + case Double: |
| 137 | + result.append(baseMatrix.getDouble(indices)); |
| 138 | + return; |
| 139 | + case Single: |
| 140 | + result.append(baseMatrix.getFloat(indices)); |
| 141 | + return; |
| 142 | + } |
| 143 | + return; |
| 144 | + } |
| 145 | + final int size = dimensions[indexCounter]; |
| 146 | + IASTAppendable subRow = F.ListAlloc(size); |
| 147 | + for (int i = 0; i < size; i++) { |
| 148 | + indices[indexCounter] = i; |
| 149 | + getTensorRecursive(baseMatrix, dimensions, indices, newCounter, subRow); |
| 150 | + } |
| 151 | + result.append(subRow); |
| 152 | + } |
| 153 | + |
| 154 | + public static IExpr importMAT(InputStream inputStream, String inputName) |
| 155 | + throws IOException, AssertionError { |
| 156 | + ByteBuffer buffer = ByteBuffer.allocate(inputStream.available()); |
| 157 | + int bytes = inputStream.read(buffer.array()); |
| 158 | + if (bytes != buffer.array().length) { |
| 159 | + throw new AssertionError("Could not read full contents of " + inputName); |
| 160 | + } |
| 161 | + try (Source source = Sources.wrap(buffer)) { |
| 162 | + Mat5File mat = Mat5.newReader(source)// |
| 163 | + .setReducedHeader(false)// |
| 164 | + .readMat(); |
| 165 | + System.out.println(mat.toString()); |
| 166 | + for (MatFile.Entry entry : mat.getEntries()) { |
| 167 | + // String name = entry.getName(); |
| 168 | + Array value = entry.getValue(); |
| 169 | + if (value instanceof AbstractMatrixBase) { |
| 170 | + return getTensor((AbstractMatrixBase) value); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + return F.NIL; |
| 175 | + } |
| 176 | +} |
0 commit comments