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