Debian Codegraph @

src/dr/math/matrixAlgebra/WrappedMatrix.java

raw symbols SCIP
1 /*
2 * WrappedMatrix.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.matrixAlgebra;
27
28 import java.util.Arrays;
29
30 /**
31 * Created by msuchard on 1/27/17.
32 */
33
34 public interface WrappedMatrix {
35
36 double get(final int i);
37
38 void set(final int i, final double x);
39
40 double get(final int i, final int j);
41
42 void set(final int i, final int j, final double x);
43
44 int getMajorDim();
45
46 int getMinorDim();
47
48 double[] getBuffer();
49
50 int getOffset();
51
52 double[] data = null;
53
54 abstract class Abstract implements WrappedMatrix {
55 final protected double[] buffer;
56 final protected int offset;
57 final protected int dimMajor;
58 final protected int dimMinor;
59
60 public Abstract(final double[] buffer, final int offset, final int dimMajor, final int dimMinor) {
61 this.buffer = buffer;
62 this.offset = offset;
63 this.dimMajor = dimMajor;
64 this.dimMinor = dimMinor;
65 }
66
67 final public double[] getBuffer() {
68 return buffer;
69 }
70
71 final public int getOffset() { return offset; }
72
73 final public int getMajorDim() {
74 return dimMajor;
75 }
76
77 final public int getMinorDim() {
78 return dimMinor;
79 }
80
81 final public String toString() {
82 StringBuilder sb = new StringBuilder("[ ");
83 // TODO
84 // if (dim > 0) {
85 // sb.append(get(0));
86 // }
87 // for (int i = 1; i < dim; ++i) {
88 // sb.append(", ").append(get(i));
89 // }
90 // sb.append(" ]");
91
92 return sb.toString();
93 }
94 }
95
96 final class Raw extends Abstract {
97
98 public Raw(double[] buffer, int offset, int dimMajor, int dimMinor) {
99 super(buffer, offset, dimMajor, dimMinor);
100 }
101
102 @Override
103 final public double get(final int i) {
104 return buffer[offset + i];
105 }
106
107 @Override
108 final public void set(final int i, final double x) {
109 buffer[offset + i] = x;
110 }
111
112 @Override
113 final public double get(final int i, final int j) {
114 return buffer[offset + i * dimMajor + j];
115 }
116
117 @Override
118 final public void set(final int i, final int j, final double x) {
119 buffer[offset + i * dimMajor + j] = x;
120 }
121 }
122
123 final class Indexed extends Abstract {
124
125 final private int[] indicesMajor;
126 final private int[] indicesMinor;
127
128 public Indexed(double[] buffer, int offset, int[] indicesMajor, int[] indicesMinor, int dimMajor, int dimMinor) {
129 super(buffer, offset, dimMajor, dimMinor);
130 this.indicesMajor = indicesMajor;
131 this.indicesMinor = indicesMinor;
132 }
133
134 @Override
135 final public double get(int i, int j) { return buffer[getIndex(i, j)]; }
136
137 @Override
138 final public void set(int i, int j, double x) { buffer[getIndex(i, j)] = x; }
139
140 @Override
141 final public double get(int i) { throw new RuntimeException("Not yet implemented"); }
142
143 @Override
144 final public void set(int i, double x) { throw new RuntimeException("Not yet implemented"); }
145
146 private final int getIndex(final int i, final int j) {
147 return offset + indicesMajor[i] * dimMajor + indicesMinor[j];
148 }
149 }
150
151 final class Utils {
152
153 public static void gatherRowsAndColumns(final WrappedMatrix source, final WrappedMatrix destination,
154 final int[] rowIndices, final int[] colIndices) {
155
156 final int rowLength = rowIndices.length;
157 final int colLength = colIndices.length;
158
159 for (int i = 0; i < rowLength; ++i) {
160 final int rowIndex = rowIndices[i];
161 for (int j = 0; j < colLength; ++j) {
162 destination.set(i, j, source.get(rowIndex, colIndices[j]) );
163 }
164 }
165 }
166
167 public static void scatterRowsAndColumns(final WrappedMatrix source, final WrappedMatrix destination,
168 final int[] rowIndices, final int[] colIndices) {
169 scatterRowsAndColumns(source, destination, rowIndices, colIndices, true);
170 }
171
172 public static void scatterRowsAndColumns(final WrappedMatrix source, final WrappedMatrix destination,
173 final int[] rowIndices, final int[] colIndices, final boolean clear) {
174 if (clear) {
175 double[] data = destination.getBuffer();
176 final int start = destination.getOffset();
177 final int end = start + destination.getMajorDim() * destination.getMinorDim();
178 Arrays.fill(data, start, end, 0.0);
179 }
180
181 final int rowLength = rowIndices.length;
182 final int colLength = colIndices.length;
183
184 for (int i = 0; i < rowLength; ++i) {
185 final int rowIndex = rowIndices[i];
186 for (int j = 0; j < colLength; ++j) {
187 destination.set(rowIndex, colIndices[i], source.get(i, j));
188 }
189 }
190 }
191 }
192 }