forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinverse.cpp
More file actions
369 lines (314 loc) · 11.2 KB
/
pinverse.cpp
File metadata and controls
369 lines (314 loc) · 11.2 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*******************************************************
* Copyright (c) 2018, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <complex>
#include <iostream>
#include <limits>
using af::array;
using af::cdouble;
using af::cfloat;
using af::constant;
using af::dim4;
using af::dtype;
using af::dtype_traits;
using af::exception;
using af::identity;
using af::matmul;
using af::max;
using af::pinverse;
using af::randu;
using af::span;
using std::abs;
using std::string;
using std::vector;
template<typename T>
array makeComplex(dim4 dims, const vector<T>& real, const vector<T>& imag) {
array realArr(dims, &real.front());
array imagArr(dims, &imag.front());
return af::complex(realArr, imagArr);
}
template<typename T>
array readTestInput(string testFilePath) {
typedef typename dtype_traits<T>::base_type InBaseType;
dtype outAfType = (dtype)dtype_traits<T>::af_type;
vector<dim4> dimsVec;
vector<vector<InBaseType> > inVec;
vector<vector<InBaseType> > goldVec;
readTestsFromFile<InBaseType, InBaseType>(testFilePath, dimsVec, inVec,
goldVec);
dim4 inDims = dimsVec[0];
if (outAfType == c32 || outAfType == c64) {
return makeComplex(inDims, inVec[1], inVec[2]);
} else {
return array(inDims, &inVec[0].front());
}
}
template<typename T>
array readTestGold(string testFilePath) {
typedef typename dtype_traits<T>::base_type InBaseType;
dtype outAfType = (dtype)dtype_traits<T>::af_type;
vector<dim4> dimsVec;
vector<vector<InBaseType> > inVec;
vector<vector<InBaseType> > goldVec;
readTestsFromFile<InBaseType, InBaseType>(testFilePath, dimsVec, inVec,
goldVec);
dim4 goldDims(dimsVec[0][1], dimsVec[0][0]);
if (outAfType == c32 || outAfType == c64) {
return makeComplex(goldDims, goldVec[1], goldVec[2]);
} else {
return array(goldDims, &goldVec[0].front());
}
}
template<typename T>
class Pinverse : public ::testing::Test {};
// Epsilons taken from test/inverse.cpp
template<typename T>
double eps();
template<>
double eps<float>() {
return 0.01f;
}
template<>
double eps<double>() {
return 1e-5;
}
template<>
double eps<cfloat>() {
return 0.01f;
}
template<>
double eps<cdouble>() {
return 1e-5;
}
template<typename T>
double relEps(array in) {
typedef typename af::dtype_traits<T>::base_type InBaseType;
double fixed_eps = eps<T>();
double calc_eps = std::numeric_limits<InBaseType>::epsilon() *
std::max(in.dims(0), in.dims(1)) * af::max<double>(in);
// Use the fixed values above if calculated error tolerance is unnecessarily
// too small
return std::max(fixed_eps, calc_eps);
}
typedef ::testing::Types<float, cfloat, double, cdouble> TestTypes;
TYPED_TEST_CASE(Pinverse, TestTypes);
// Test Moore-Penrose conditions in the following first 4 tests
// See https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse#Definition
TYPED_TEST(Pinverse, AApinvA_A) {
array in = readTestInput<TypeParam>(
string(TEST_DIR "/pinverse/pinverse10x8.test"));
array inpinv = pinverse(in);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, eps<TypeParam>());
}
TYPED_TEST(Pinverse, ApinvAApinv_Apinv) {
array in = readTestInput<TypeParam>(
string(TEST_DIR "/pinverse/pinverse10x8.test"));
array inpinv = pinverse(in);
array out = matmul(inpinv, in, inpinv);
ASSERT_ARRAYS_NEAR(inpinv, out, eps<TypeParam>());
}
TYPED_TEST(Pinverse, AApinv_IsHermitian) {
array in = readTestInput<TypeParam>(
string(TEST_DIR "/pinverse/pinverse10x8.test"));
array inpinv = pinverse(in);
array aapinv = matmul(in, inpinv);
array out = matmul(in, inpinv).H();
ASSERT_ARRAYS_NEAR(aapinv, out, eps<TypeParam>());
}
TYPED_TEST(Pinverse, ApinvA_IsHermitian) {
array in = readTestInput<TypeParam>(
string(TEST_DIR "/pinverse/pinverse10x8.test"));
array inpinv = pinverse(in);
array apinva = af::matmul(inpinv, in);
array out = af::matmul(inpinv, in).H();
ASSERT_ARRAYS_NEAR(apinva, out, eps<TypeParam>());
}
TYPED_TEST(Pinverse, Large) {
array in = readTestInput<TypeParam>(
string(TEST_DIR "/pinverse/pinverse640x480.test"));
array inpinv = pinverse(in);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, relEps<TypeParam>(in));
}
TYPED_TEST(Pinverse, LargeTall) {
array in = readTestInput<TypeParam>(
string(TEST_DIR "/pinverse/pinverse640x480.test"))
.T();
array inpinv = pinverse(in);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, relEps<TypeParam>(in));
}
TEST(Pinverse, Square) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x10.test"));
array inpinv = pinverse(in);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, eps<float>());
}
TEST(Pinverse, Dim1GtDim0) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse8x10.test"));
array inpinv = pinverse(in);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, eps<float>());
}
TEST(Pinverse, CompareWithNumpy) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
array gold =
readTestGold<float>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
array out = pinverse(in);
ASSERT_ARRAYS_NEAR(gold, out, relEps<float>(gold));
}
TEST(Pinverse, SmallSigValExistsFloat) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
const dim_t dim0 = in.dims(0);
const dim_t dim1 = in.dims(1);
// Generate sigma with small non-zero value
af::array u;
af::array vT;
af::array sVec;
af::svd(u, sVec, vT, in);
dim_t sSize = sVec.elements();
sVec(2) = 1e-12;
af::array s = af::diag(sVec, 0, false);
af::array zeros = af::constant(0, dim0 > sSize ? dim0 - sSize : sSize,
dim1 > sSize ? dim1 - sSize : sSize);
s = af::join(dim0 > dim1 ? 0 : 1, s, zeros);
// Make new input array that has a small non-zero value in its SVD sigma
in = af::matmul(u, s, vT);
array inpinv = pinverse(in);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, eps<float>());
}
TEST(Pinverse, SmallSigValExistsDouble) {
array in =
readTestInput<double>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
const dim_t dim0 = in.dims(0);
const dim_t dim1 = in.dims(1);
// Generate sigma with small non-zero value
array u;
array vT;
array sVec;
svd(u, sVec, vT, in);
dim_t sSize = sVec.elements();
sVec(2) = (double)1e-16;
array s = diag(sVec, 0, false);
array zeros = constant(0, dim0 > sSize ? dim0 - sSize : sSize,
dim1 > sSize ? dim1 - sSize : sSize, f64);
s = join(dim0 > dim1 ? 0 : 1, s, zeros);
// Make new input array that has a small non-zero value in its SVD sigma
in = matmul(u, s, vT);
array inpinv = pinverse(in, 1e-15);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, eps<double>());
}
TEST(Pinverse, Batching3D) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x8x2.test"));
array inpinv0 = pinverse(in(span, span, 0));
array inpinv1 = pinverse(in(span, span, 1));
array out = pinverse(in);
array out0 = out(span, span, 0);
array out1 = out(span, span, 1);
ASSERT_ARRAYS_NEAR(inpinv0, out0, relEps<float>(inpinv0));
ASSERT_ARRAYS_NEAR(inpinv1, out1, relEps<float>(inpinv1));
}
TEST(Pinverse, Batching4D) {
array in = readTestInput<float>(
string(TEST_DIR "/pinverse/pinverse10x8x2x2.test"));
array inpinv00 = pinverse(in(span, span, 0, 0));
array inpinv01 = pinverse(in(span, span, 0, 1));
array inpinv10 = pinverse(in(span, span, 1, 0));
array inpinv11 = pinverse(in(span, span, 1, 1));
array out = pinverse(in);
array out00 = out(span, span, 0, 0);
array out01 = out(span, span, 0, 1);
array out10 = out(span, span, 1, 0);
array out11 = out(span, span, 1, 1);
ASSERT_ARRAYS_NEAR(inpinv00, out00, relEps<float>(inpinv00));
ASSERT_ARRAYS_NEAR(inpinv01, out01, relEps<float>(inpinv01));
ASSERT_ARRAYS_NEAR(inpinv10, out10, relEps<float>(inpinv10));
ASSERT_ARRAYS_NEAR(inpinv11, out11, relEps<float>(inpinv11));
}
TEST(Pinverse, CustomTol) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
array inpinv = pinverse(in, 1e-12);
array out = matmul(in, inpinv, in);
ASSERT_ARRAYS_NEAR(in, out, eps<float>());
}
TEST(Pinverse, C) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
af_array inpinv = 0, identity = 0, out = 0;
ASSERT_SUCCESS(af_pinverse(&inpinv, in.get(), 1e-6, AF_MAT_NONE));
ASSERT_SUCCESS(
af_matmul(&identity, in.get(), inpinv, AF_MAT_NONE, AF_MAT_NONE));
ASSERT_SUCCESS(
af_matmul(&out, identity, in.get(), AF_MAT_NONE, AF_MAT_NONE));
ASSERT_ARRAYS_NEAR(in.get(), out, eps<float>());
ASSERT_SUCCESS(af_release_array(out));
ASSERT_SUCCESS(af_release_array(identity));
ASSERT_SUCCESS(af_release_array(inpinv));
}
TEST(Pinverse, C_CustomTol) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
af_array inpinv = 0, identity = 0, out = 0;
ASSERT_SUCCESS(af_pinverse(&inpinv, in.get(), 1e-12, AF_MAT_NONE));
ASSERT_SUCCESS(
af_matmul(&identity, in.get(), inpinv, AF_MAT_NONE, AF_MAT_NONE));
ASSERT_SUCCESS(
af_matmul(&out, identity, in.get(), AF_MAT_NONE, AF_MAT_NONE));
ASSERT_ARRAYS_NEAR(in.get(), out, eps<float>());
ASSERT_SUCCESS(af_release_array(out));
ASSERT_SUCCESS(af_release_array(identity));
ASSERT_SUCCESS(af_release_array(inpinv));
}
TEST(Pinverse, NegativeTol) {
array in =
readTestInput<float>(string(TEST_DIR "/pinverse/pinverse10x8.test"));
array out;
ASSERT_THROW(out = pinverse(in, -1.f), exception);
}
TEST(Pinverse, InvalidType) {
array in = constant(0, 10, 8, u8);
array out;
ASSERT_THROW(out = pinverse(in, -1.f), exception);
}
TEST(Pinverse, InvalidMatProp) {
array in = constant(0.f, 10, 8, f32);
array out;
ASSERT_THROW(out = pinverse(in, -1.f, AF_MAT_SYM), exception);
}
TEST(Pinverse, DocSnippet) {
//! [ex_pinverse]
float hA[] = {0, 1, 2, 3, 4, 5};
array A(3, 2, hA);
// 0.0000 3.0000
// 1.0000 4.0000
// 2.0000 5.0000
array Apinv = pinverse(A);
// -0.7778 -0.1111 0.5556
// 0.2778 0.1111 -0.0556
array MustBeA = matmul(A, Apinv, A);
// 0.0000 3.0000
// 1.0000 4.0000
// 2.0000 5.0000
//! [ex_pinverse]
ASSERT_ARRAYS_NEAR(A, MustBeA, eps<float>());
}