forked from HebiRobotics/MFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EjmlDMatrixWrapper.java
81 lines (67 loc) · 2.35 KB
/
EjmlDMatrixWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*-
* #%L
* Mat-File IO
* %%
* Copyright (C) 2018 HEBI Robotics
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package us.hebi.matlab.mat.tests.serialization;
import org.ejml.data.DMatrix;
import us.hebi.matlab.mat.format.Mat5;
import us.hebi.matlab.mat.format.Mat5Serializable;
import us.hebi.matlab.mat.types.AbstractArray;
import us.hebi.matlab.mat.types.MatlabType;
import us.hebi.matlab.mat.types.Sink;
import java.io.IOException;
import static us.hebi.matlab.mat.format.Mat5Type.Double;
import static us.hebi.matlab.mat.format.Mat5WriteUtil.*;
/**
* Serializes an EJML double matrix into a MAT 5 file that can be read by MATLAB
*
* @author Florian Enner
*/
public class EjmlDMatrixWrapper extends AbstractArray implements Mat5Serializable {
@Override
public int getMat5Size(String name) {
return Mat5.MATRIX_TAG_SIZE
+ computeArrayHeaderSize(name, this)
+ Double.computeSerializedSize(matrix.getNumElements());
}
@Override
public void writeMat5(String name, Sink sink) throws IOException {
writeMatrixTag(name, this, sink);
writeArrayHeader(name, this, sink);
// Data in column major format
Double.writeTag(matrix.getNumElements(), sink);
for (int col = 0; col < matrix.getNumCols(); col++) {
for (int row = 0; row < matrix.getNumRows(); row++) {
sink.writeDouble(matrix.unsafe_get(row, col));
}
}
Double.writePadding(matrix.getNumElements(), sink);
}
@Override
public MatlabType getType() {
return MatlabType.Double;
}
public EjmlDMatrixWrapper(DMatrix matrix) {
super(Mat5.dims(matrix.getNumRows(), matrix.getNumCols()), false);
this.matrix = matrix;
}
@Override
public void close() throws IOException {
}
final DMatrix matrix;
}