forked from HebiRobotics/MFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEjmlSparseWrapper.java
123 lines (102 loc) · 3.67 KB
/
EjmlSparseWrapper.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*-
* #%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.DMatrixSparseCSC;
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.Mat5Type.*;
import static us.hebi.matlab.mat.format.Mat5WriteUtil.*;
/**
* Serializes an EJML Sparse CSC matrix into a MAT 5 file that can be read by MATLAB.
* <p>
* The data is stored almost identically, so there isn't much conversion required.
* Implementing 'Mat5Attributes' lets us get around the overhead of implementing
* the entire Sparse interface, or alternatively manually writing the header.
*
* @author Florian Enner
*/
public class EjmlSparseWrapper extends AbstractArray implements Mat5Serializable, Mat5Serializable.Mat5Attributes {
@Override
public int getMat5Size(String name) {
return Mat5.MATRIX_TAG_SIZE
+ computeArrayHeaderSize(name, this)
+ Int32.computeSerializedSize(getNumRowIndices())
+ Int32.computeSerializedSize(getNumColIndices())
+ Double.computeSerializedSize(getNzMax());
}
@Override
public void writeMat5(String name, Sink sink) throws IOException {
writeMatrixTag(name, this, sink);
writeArrayHeader(name, this, sink);
// Row indices (MATLAB requires at least 1 entry)
Int32.writeTag(getNumRowIndices(), sink);
if (sparse.getNonZeroLength() == 0) {
sink.writeInt(0);
} else {
sink.writeInts(sparse.nz_rows, 0, getNumRowIndices());
}
Int32.writePadding(getNumRowIndices(), sink);
// Column indices
Int32.writeTag(getNumColIndices(), sink);
sink.writeInts(sparse.col_idx, 0, getNumColIndices());
Int32.writePadding(getNumColIndices(), sink);
// Non-zero values
Double.writeTag(getNzMax(), sink);
sink.writeDoubles(sparse.nz_values, 0, getNzMax());
Double.writePadding(getNzMax(), sink);
}
@Override
public MatlabType getType() {
return MatlabType.Sparse;
}
private int getNumRowIndices() {
return Math.max(1, sparse.getNonZeroLength());
}
private int getNumColIndices() {
return sparse.getNumCols() + 1;
}
@Override
public int getNzMax() {
return sparse.getNonZeroLength();
}
@Override
public boolean isLogical() {
return false;
}
@Override
public boolean isComplex() {
return false;
}
public EjmlSparseWrapper(DMatrixSparseCSC sparse) {
super(Mat5.dims(sparse.getNumRows(), sparse.getNumCols()), false);
if (!sparse.indicesSorted)
throw new IllegalArgumentException("Indices must be sorted!");
this.sparse = sparse;
}
@Override
public void close() throws IOException {
}
final DMatrixSparseCSC sparse;
}