1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.util;
import java.util.Arrays;
/**
* A class that contains utility methods related to matrices.
*
* @hide
*/
public class MatrixUtils {
private MatrixUtils() {
// This class is non-instantiable.
}
/**
* Sets a matrix to the identity matrix.
*
* @param out output matrix of size n*m
* @param n number of rows in the output matrix
* @param m number of columns in the output matrix
*/
public static void setIdentityM(float[] out, int n, int m) {
final int size = n * m;
if (out.length != size) {
throw new IllegalArgumentException("Invalid dimensions for output matrix");
}
Arrays.fill(out, 0, size, 0);
for (int i = Math.min(m,n) - 1; i >= 0; i--) {
out[i * (m + 1)] = 1;
}
}
/**
* Add two matrices. May be used in-place by specifying the same value for
* the out matrix and one of the input matrices.
*
* @param ab output matrix of size n*m
* @param a left-hand matrix of size n*m
* @param n number of rows in the left-hand matrix
* @param m number of columns in the left-hand matrix
* @param b right-hand matrix of size n*m
*/
public static void addMM(float[] ab, float[] a, int n, int m, float[] b) {
final int size = n * m;
if (a.length != size) {
throw new IllegalArgumentException("Invalid dimensions for matrix a");
} else if (b.length != size) {
throw new IllegalArgumentException("Invalid dimensions for matrix b");
} else if (ab.length != size) {
throw new IllegalArgumentException("Invalid dimensions for matrix ab");
}
for (int i = 0; i < size; i++) {
ab[i] = a[i] + b[i];
}
}
/**
* Multiply two matrices.
*
* @param ab output matrix of size n*p
* @param a left-hand matrix of size n*m
* @param n number of rows in the left-hand matrix
* @param m number of columns in the left-hand matrix
* @param b right-hand matrix of size m*p
* @param p number of columns in the right-hand matrix
*/
public static void multiplyMM(float[] ab, float[] a, int n, int m, float[] b, int p) {
if (a.length != n * m) {
throw new IllegalArgumentException("Invalid dimensions for matrix a");
} else if (b.length != m * p) {
throw new IllegalArgumentException("Invalid dimensions for matrix b");
} else if (ab.length != n * p) {
throw new IllegalArgumentException("Invalid dimensions for matrix ab");
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
float sum = 0;
for (int k = 0; k < m; k++) {
sum += a[i * m + k] * b[k * p + j];
}
ab[i * p + j] = sum;
}
}
}
/**
* Multiply a matrix by a scalar value. May be used in-place by specifying
* the same value for the input and output matrices.
*
* @param out output matrix
* @param in input matrix
* @param scalar scalar value
*/
public static void multiplyMS(float[] out, float[] in, float scalar) {
final int n = out.length;
for (int i = 0; i < n; i++) {
out[i] = in[i] * scalar;
}
}
}
|