001 package numal.lowprecision;
002
003 import static java.lang.Math.*;
004
005 /** A library of basic operations between matrices and vectrors.
006 * These methods make use of lower and upper indices; which allows
007 * vectors to be 0-index or 1-index (i.e. a[0...n-1] or a[1...n])
008 */
009
010 public class Basic extends Object {
011
012 static final int BASE = 100;
013
014 /** Compute a constant multiple (<i>x</i) of part of a vector <i>b</i>.
015 * Method originally from class numal.LinearAlgebra - Lau (2004)
016 * section 1.2.A Real vector and matrix - Duplication p.6.
017 *
018 * @param l lower row bound index
019 * @param u upper row bound index
020 * @param shift index shifting parameter
021 * @param a rectangular output matrix
022 * @param b rectangular input matrix
023 */
024
025 public static void dupvec(int l, int u, int shift, float a[], float b[]) {
026 for (int k = l; k <= u; k++) a[k] = b[k + shift];
027 }
028
029 /** Replace a column sequence of elements of a rectangular matrix <i>a</i>
030 * by a constant multiple (<i>x</i) of a column sequence of elements of a rectangular matrix <i>b</i>.
031 * Method originally from class numal.LinearAlgebra - Lau (2004)
032 * section 1.3.C Real vector and matrix - Multiplication p.9.
033 *
034 * @param l lower row bound
035 * @param u upper row bound
036 * @param i column index of <i>a</i>
037 * @param j column index of <i>b</i>
038 * @param a rectangular matrix
039 * @param b rectangular matrix
040 * @param x multiplication factor
041 */
042 public static void mulcol(int l, int u, int i, int j, float a[][], float b[][], float x) {
043 for (int k = l; k <= u; k++) a[k][i] = x * b[k][j];
044 }
045
046 /** Compute the inner product of part of a vector <i>a</i> and part of a vector <i>b</i>
047 * A subset of the inner product
048 * can be computed by choosing suitable lower and upper bounds.
049 * Method originally from class numal.Basic - Lau (2004)
050 * section 1.4.A Real vector vector products p.10.
051 *
052 * @param l lower bound
053 * @param u upper bound
054 * @param shift index-shifting parameter of vector <i>b</i>
055 * @param a vector
056 * @param b vector
057 * @return the inner product of vector <i>a</i> and vector <i>b</i>
058 */
059 public static float vecvec(int l, int u, int shift, float a[], float b[]) {
060 float s = 0f;
061 for (int k = l; k <= u; k++) s += a[k] * b[k + shift];
062 return s;
063 }
064
065 /** Compute the inner product of part of a row of a rectangular matrix <i>a</i>
066 * and the corresponding part of a vector <i>b</i>. A subset of the inner product
067 * can be computed by choosing suitable lower and upper column bounds.
068 * Method originally from class numal.Basic - Lau (2004)
069 * section 1.4.B Real Vector Vector products p.11.
070 *
071 * @param l lower bound
072 * @param u upper bound
073 * @param i row index of <i>a</i>
074 * @param a rectangular matrix
075 * @param b vector
076 * @return inner product of row <i>i</i> of matrix <i>a</i> with vector <i>b</i>
077 */
078 public static float matvec(int l, int u, int i, float a[][], float b[]) {
079 float s = 0f;
080 for (int k = l; k <= u; k++) s += a[i][k] * b[k];
081 return s;
082 }
083 /**
084 * Method originally from class numal.Basic - Lau (2004)
085 * section 1.4.B Real Vector Vector products p.12.
086 * @param l lower bound
087 * @param u upper bound
088 * @param i column index of <i>a</i>
089 * @param a rectangular matrix with column [l-u][j] defined
090 * @param b vector with entries [l-u] defined
091 */
092
093 public static float tamvec(int l, int u, int i, float a[][], float b[]) {
094 float s = 0f;
095 for (int k = l; k <= u; k++) s += a[k][i] * b[k];
096 return s;
097 }
098
099 /** Get the inner product of part of a row of a rectangular matrix <i>a</i>
100 * and the corresponding part of a column of a rectangular matrix <i>b</i>.
101 *
102 * @param l lower row bound
103 * @param u upper row bound
104 * @param i row index of <i>a</i>
105 * @param j column index of <i>b</i>
106 * @param a rectangular matrix with row [i][l-u] defined
107 * @param b rectangular matrix with column [l-u][j] defined
108 *
109 * @return inner product of <i>a</i> and <i>b</i>
110 */
111 public static float matmat(int l, int u, int i, int j, float a[][], float b[][]) {
112 float s = 0f;
113 for (int k = l; k <= u; k++) s += a[i][k] * b[k][j];
114 return s;
115 }
116
117 /** Compute the inner product of part of a column of a rectangular matrix <i>a</i>
118 * and the corresponding part of a column of a rectangular matrix <i>b</i>.
119 * Method originally from class numal.Basic - Lau (2004)
120 * section 1.4.E Real Vector Vector products p.13.
121 *
122 * @param l lower row bound
123 * @param u upper row bound
124 * @param i column index of <i>a</i>
125 * @param j column index of <i>b</i>
126 * @param a rectangular matrix with column [l-u][i] defined
127 * @param b rectangular matrix with column [l-u][j] defined
128 */
129 public static float tammat(int l, int u, int i, int j, float a[][], float b[][]) {
130 float s = 0f;
131 for (int k = l; k <= u; k++) s += a[k][i] * b[k][j];
132 return s;
133 }
134
135 /** Compute the inner product of part of a row of a rectangular matrix <i>a</i> and the
136 * corresponding part of a row of a rectangular matrix <i>b</i>.
137 * Method originally from class numal.LinearAlgebra - Lau (2004)
138 * section 1.4.F Real Vector Vector products p.13
139 *
140 * @param l lower column bound
141 * @param u upper column bound
142 * @param i row index of <i>a</i>
143 * @param j row index of <i>b</i>
144 * @param a rectangular matrix
145 * @param b rectangular matrix
146 * @return inner product of row <i>i</i> of <i>a</i> and row <i>j</i> of <i>b</i>
147 */
148 public static float mattam(int l, int u, int i, int j, float a[][], float b[][]) {
149 float s = 0f;
150 for (int k = l; k <= u; k++) s += a[i][k] * b[j][k];
151 return s;
152 }
153
154 /** Adds a constant multiple (<i>x</i>) of part of a column of a rectangular matrix
155 * <i>b</i> to part of a column of a rectangular matrix <i>a</i>
156 * Method originally from class numal.Basic - Lau (2004)
157 * section 1.7.B Real vector and matrix - Elimination p.22.
158 *
159 * @param l lower row bound
160 * @param u upper row bound
161 * @param i column index of <i>a</i>
162 * @param j column index of <i>b</i>
163 * @param a rectangular matrix
164 * @param b rectangular matrix
165 * @param x multiplication factor
166 */
167 public static void elmcol(int l, int u, int i, int j, float a[][], float b[][], float x) {
168 for (int k = l; k <= u; k++) a[k][i] += x * b[k][j];
169 }
170
171 /** Adds a constant multiple (<i>x</i>) of part of a row of a rectangular matrix
172 * <i>b</i> to part of a row of a rectangular matrix <i>a</i>
173 * Method originally from class numal.Basic - Lau (2004)
174 * section 1.7.B Real vector and matrix - Elimination p.22.
175 *
176 * @param l lower row bound
177 * @param u upper row bound
178 * @param i column index of <i>a</i>
179 * @param j column index of <i>b</i>
180 * @param a rectangular matrix
181 * @param b rectangular matrix
182 * @param x multiplication factor
183 */
184 public static void elmrow(int l, int u, int i, int j, float a[][], float b[][], float x) {
185 for (int k = l; k <= u; k++) a[i][k] += x * b[j][k];
186 }
187
188 /** Rotates two columns of a rectangular matrix using two parameters
189 * <ul>
190 * <li>a[k][i] = c * a[k][i] + s * a[k][j] (where k = l to u)</li>
191 * <li>a[k][j] = c * a[k][j] - s * a[k][i]</li>
192 * </ul>
193 * Method originally from class numal.Basic - Lau (2004)
194 * section 1.9.A Real vector and matrix - Rotation p.30.
195 *
196 * @param l lower row bound
197 * @param u upper row bound
198 * @param i column index of <i>a</i>
199 * @param j column index of <i>a</i>
200 * @param a rectangular matrix
201 * @param c cosine multiplication parameter
202 * @param s sine multiplication parameter
203 */
204 public static void rotcol(int l, int u, int i, int j, float a[][], float c, float s) {
205 for (int k = l; k <= u; k++) {
206 float x = a[k][i];
207 float y = a[k][j];
208 a[k][i] = x * c + y * s;
209 a[k][j] = y * c - x * s;
210 }
211 }
212
213 }