source: src/main/java/agents/anac/y2019/harddealer/math3/linear/SparseFieldMatrix.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 5.9 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package agents.anac.y2019.harddealer.math3.linear;
18
19import agents.anac.y2019.harddealer.math3.Field;
20import agents.anac.y2019.harddealer.math3.FieldElement;
21import agents.anac.y2019.harddealer.math3.util.OpenIntToFieldHashMap;
22
23/**
24 * Sparse matrix implementation based on an open addressed map.
25 *
26 * <p>
27 * Caveat: This implementation assumes that, for any {@code x},
28 * the equality {@code x * 0d == 0d} holds. But it is is not true for
29 * {@code NaN}. Moreover, zero entries will lose their sign.
30 * Some operations (that involve {@code NaN} and/or infinities) may
31 * thus give incorrect results.
32 * </p>
33 * @param <T> the type of the field elements
34 * @since 2.0
35 */
36public class SparseFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMatrix<T> {
37
38 /** Storage for (sparse) matrix elements. */
39 private final OpenIntToFieldHashMap<T> entries;
40 /** Row dimension. */
41 private final int rows;
42 /** Column dimension. */
43 private final int columns;
44
45 /**
46 * Create a matrix with no data.
47 *
48 * @param field Field to which the elements belong.
49 */
50 public SparseFieldMatrix(final Field<T> field) {
51 super(field);
52 rows = 0;
53 columns= 0;
54 entries = new OpenIntToFieldHashMap<T>(field);
55 }
56
57 /**
58 * Create a new SparseFieldMatrix<T> with the supplied row and column
59 * dimensions.
60 *
61 * @param field Field to which the elements belong.
62 * @param rowDimension Number of rows in the new matrix.
63 * @param columnDimension Number of columns in the new matrix.
64 * @throws agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException
65 * if row or column dimension is not positive.
66 */
67 public SparseFieldMatrix(final Field<T> field,
68 final int rowDimension, final int columnDimension) {
69 super(field, rowDimension, columnDimension);
70 this.rows = rowDimension;
71 this.columns = columnDimension;
72 entries = new OpenIntToFieldHashMap<T>(field);
73 }
74
75 /**
76 * Copy constructor.
77 *
78 * @param other Instance to copy.
79 */
80 public SparseFieldMatrix(SparseFieldMatrix<T> other) {
81 super(other.getField(), other.getRowDimension(), other.getColumnDimension());
82 rows = other.getRowDimension();
83 columns = other.getColumnDimension();
84 entries = new OpenIntToFieldHashMap<T>(other.entries);
85 }
86
87 /**
88 * Generic copy constructor.
89 *
90 * @param other Instance to copy.
91 */
92 public SparseFieldMatrix(FieldMatrix<T> other){
93 super(other.getField(), other.getRowDimension(), other.getColumnDimension());
94 rows = other.getRowDimension();
95 columns = other.getColumnDimension();
96 entries = new OpenIntToFieldHashMap<T>(getField());
97 for (int i = 0; i < rows; i++) {
98 for (int j = 0; j < columns; j++) {
99 setEntry(i, j, other.getEntry(i, j));
100 }
101 }
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public void addToEntry(int row, int column, T increment) {
107 checkRowIndex(row);
108 checkColumnIndex(column);
109 final int key = computeKey(row, column);
110 final T value = entries.get(key).add(increment);
111 if (getField().getZero().equals(value)) {
112 entries.remove(key);
113 } else {
114 entries.put(key, value);
115 }
116 }
117
118 /** {@inheritDoc} */
119 @Override
120 public FieldMatrix<T> copy() {
121 return new SparseFieldMatrix<T>(this);
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 public FieldMatrix<T> createMatrix(int rowDimension, int columnDimension) {
127 return new SparseFieldMatrix<T>(getField(), rowDimension, columnDimension);
128 }
129
130 /** {@inheritDoc} */
131 @Override
132 public int getColumnDimension() {
133 return columns;
134 }
135
136 /** {@inheritDoc} */
137 @Override
138 public T getEntry(int row, int column) {
139 checkRowIndex(row);
140 checkColumnIndex(column);
141 return entries.get(computeKey(row, column));
142 }
143
144 /** {@inheritDoc} */
145 @Override
146 public int getRowDimension() {
147 return rows;
148 }
149
150 /** {@inheritDoc} */
151 @Override
152 public void multiplyEntry(int row, int column, T factor) {
153 checkRowIndex(row);
154 checkColumnIndex(column);
155 final int key = computeKey(row, column);
156 final T value = entries.get(key).multiply(factor);
157 if (getField().getZero().equals(value)) {
158 entries.remove(key);
159 } else {
160 entries.put(key, value);
161 }
162
163 }
164
165 /** {@inheritDoc} */
166 @Override
167 public void setEntry(int row, int column, T value) {
168 checkRowIndex(row);
169 checkColumnIndex(column);
170 if (getField().getZero().equals(value)) {
171 entries.remove(computeKey(row, column));
172 } else {
173 entries.put(computeKey(row, column), value);
174 }
175 }
176
177 /**
178 * Compute the key to access a matrix element.
179 *
180 * @param row Row index of the matrix element.
181 * @param column Column index of the matrix element.
182 * @return the key within the map to access the matrix element.
183 */
184 private int computeKey(int row, int column) {
185 return row * columns + column;
186 }
187}
Note: See TracBrowser for help on using the repository browser.