forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinverse.cpp
More file actions
98 lines (74 loc) · 2.27 KB
/
inverse.cpp
File metadata and controls
98 lines (74 loc) · 2.27 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
/*******************************************************
* Copyright (c) 2014, 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 <common/err_common.hpp>
#include <inverse.hpp>
#if defined(WITH_LINEAR_ALGEBRA)
#include <err_cpu.hpp>
#include <handle.hpp>
#include <range.hpp>
#include <af/dim4.hpp>
#include <cassert>
#include <identity.hpp>
#include <lapack_helper.hpp>
#include <lu.hpp>
#include <platform.hpp>
#include <queue.hpp>
#include <solve.hpp>
namespace cpu {
template<typename T>
using getri_func_def = int (*)(ORDER_TYPE, int, T *, int, const int *);
#define INV_FUNC_DEF(FUNC) \
template<typename T> \
FUNC##_func_def<T> FUNC##_func();
#define INV_FUNC(FUNC, TYPE, PREFIX) \
template<> \
FUNC##_func_def<TYPE> FUNC##_func<TYPE>() { \
return &LAPACK_NAME(PREFIX##FUNC); \
}
INV_FUNC_DEF(getri)
INV_FUNC(getri, float, s)
INV_FUNC(getri, double, d)
INV_FUNC(getri, cfloat, c)
INV_FUNC(getri, cdouble, z)
template<typename T>
Array<T> inverse(const Array<T> &in) {
int M = in.dims()[0];
int N = in.dims()[1];
if (M != N) {
Array<T> I = identity<T>(in.dims());
return solve(in, I);
}
Array<T> A = copyArray<T>(in);
Array<int> pivot = lu_inplace<T>(A, false);
auto func = [=](Param<T> A, Param<int> pivot, int M) {
getri_func<T>()(AF_LAPACK_COL_MAJOR, M, A.get(), A.strides(1),
pivot.get());
};
getQueue().enqueue(func, A, pivot, M);
return A;
}
#define INSTANTIATE(T) template Array<T> inverse<T>(const Array<T> &in);
INSTANTIATE(float)
INSTANTIATE(cfloat)
INSTANTIATE(double)
INSTANTIATE(cdouble)
} // namespace cpu
#else // WITH_LINEAR_ALGEBRA
namespace cpu {
template<typename T>
Array<T> inverse(const Array<T> &in) {
AF_ERROR("Linear Algebra is disabled on CPU", AF_ERR_NOT_CONFIGURED);
}
#define INSTANTIATE(T) template Array<T> inverse<T>(const Array<T> &in);
INSTANTIATE(float)
INSTANTIATE(cfloat)
INSTANTIATE(double)
INSTANTIATE(cdouble)
} // namespace cpu
#endif // WITH_LINEAR_ALGEBRA