1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.linear;
18
19 import org.apache.commons.math4.legacy.core.Field;
20 import org.apache.commons.math4.legacy.core.FieldElement;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class SparseFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMatrix<T> {
36
37
38 private final OpenIntToFieldHashMap<T> entries;
39
40 private final int rows;
41
42 private final int columns;
43
44
45
46
47
48
49 public SparseFieldMatrix(final Field<T> field) {
50 super(field);
51 rows = 0;
52 columns= 0;
53 entries = new OpenIntToFieldHashMap<>(field);
54 }
55
56
57
58
59
60
61
62
63
64
65
66 public SparseFieldMatrix(final Field<T> field,
67 final int rowDimension, final int columnDimension) {
68 super(field, rowDimension, columnDimension);
69 this.rows = rowDimension;
70 this.columns = columnDimension;
71 entries = new OpenIntToFieldHashMap<>(field);
72 }
73
74
75
76
77
78
79 public SparseFieldMatrix(SparseFieldMatrix<T> other) {
80 super(other.getField(), other.getRowDimension(), other.getColumnDimension());
81 rows = other.getRowDimension();
82 columns = other.getColumnDimension();
83 entries = new OpenIntToFieldHashMap<>(other.entries);
84 }
85
86
87
88
89
90
91 public SparseFieldMatrix(FieldMatrix<T> other){
92 super(other.getField(), other.getRowDimension(), other.getColumnDimension());
93 rows = other.getRowDimension();
94 columns = other.getColumnDimension();
95 entries = new OpenIntToFieldHashMap<>(getField());
96 for (int i = 0; i < rows; i++) {
97 for (int j = 0; j < columns; j++) {
98 setEntry(i, j, other.getEntry(i, j));
99 }
100 }
101 }
102
103
104 @Override
105 public void addToEntry(int row, int column, T increment) {
106 checkRowIndex(row);
107 checkColumnIndex(column);
108 final int key = computeKey(row, column);
109 final T value = entries.get(key).add(increment);
110 if (getField().getZero().equals(value)) {
111 entries.remove(key);
112 } else {
113 entries.put(key, value);
114 }
115 }
116
117
118 @Override
119 public FieldMatrix<T> copy() {
120 return new SparseFieldMatrix<>(this);
121 }
122
123
124 @Override
125 public FieldMatrix<T> createMatrix(int rowDimension, int columnDimension) {
126 return new SparseFieldMatrix<>(getField(), rowDimension, columnDimension);
127 }
128
129
130 @Override
131 public int getColumnDimension() {
132 return columns;
133 }
134
135
136 @Override
137 public T getEntry(int row, int column) {
138 checkRowIndex(row);
139 checkColumnIndex(column);
140 return entries.get(computeKey(row, column));
141 }
142
143
144 @Override
145 public int getRowDimension() {
146 return rows;
147 }
148
149
150 @Override
151 public void multiplyEntry(int row, int column, T factor) {
152 checkRowIndex(row);
153 checkColumnIndex(column);
154 final int key = computeKey(row, column);
155 final T value = entries.get(key).multiply(factor);
156 if (getField().getZero().equals(value)) {
157 entries.remove(key);
158 } else {
159 entries.put(key, value);
160 }
161 }
162
163
164 @Override
165 public void setEntry(int row, int column, T value) {
166 checkRowIndex(row);
167 checkColumnIndex(column);
168 if (getField().getZero().equals(value)) {
169 entries.remove(computeKey(row, column));
170 } else {
171 entries.put(computeKey(row, column), value);
172 }
173 }
174
175
176
177
178
179
180
181
182 private int computeKey(int row, int column) {
183 return row * columns + column;
184 }
185 }