forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqr.cpp
More file actions
110 lines (90 loc) · 3.14 KB
/
qr.cpp
File metadata and controls
110 lines (90 loc) · 3.14 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
/*******************************************************
* 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 <backend.hpp>
#include <common/ArrayInfo.hpp>
#include <common/err_common.hpp>
#include <handle.hpp>
#include <qr.hpp>
#include <af/array.h>
#include <af/defines.h>
#include <af/lapack.h>
using af::dim4;
using detail::Array;
using detail::cdouble;
using detail::cfloat;
using detail::createEmptyArray;
using std::swap;
template<typename T>
static inline void qr(af_array *q, af_array *r, af_array *tau,
const af_array in) {
Array<T> qArray = createEmptyArray<T>(dim4());
Array<T> rArray = createEmptyArray<T>(dim4());
Array<T> tArray = createEmptyArray<T>(dim4());
qr<T>(qArray, rArray, tArray, getArray<T>(in));
*q = getHandle(qArray);
*r = getHandle(rArray);
*tau = getHandle(tArray);
}
template<typename T>
static inline af_array qr_inplace(af_array in) {
return getHandle(qr_inplace<T>(getArray<T>(in)));
}
af_err af_qr(af_array *q, af_array *r, af_array *tau, const af_array in) {
try {
const ArrayInfo &i_info = getInfo(in);
if (i_info.ndims() > 2) {
AF_ERROR("qr can not be used in batch mode", AF_ERR_BATCH);
}
af_dtype type = i_info.getType();
if (i_info.ndims() == 0) {
AF_CHECK(af_create_handle(q, 0, nullptr, type));
AF_CHECK(af_create_handle(r, 0, nullptr, type));
AF_CHECK(af_create_handle(tau, 0, nullptr, type));
return AF_SUCCESS;
}
ARG_ASSERT(0, q != nullptr);
ARG_ASSERT(1, r != nullptr);
ARG_ASSERT(2, tau != nullptr);
ARG_ASSERT(3, i_info.isFloating()); // Only floating and complex types
switch (type) {
case f32: qr<float>(q, r, tau, in); break;
case f64: qr<double>(q, r, tau, in); break;
case c32: qr<cfloat>(q, r, tau, in); break;
case c64: qr<cdouble>(q, r, tau, in); break;
default: TYPE_ERROR(1, type);
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_qr_inplace(af_array *tau, af_array in) {
try {
const ArrayInfo &i_info = getInfo(in);
if (i_info.ndims() > 2) {
AF_ERROR("qr can not be used in batch mode", AF_ERR_BATCH);
}
af_dtype type = i_info.getType();
ARG_ASSERT(1, i_info.isFloating()); // Only floating and complex types
ARG_ASSERT(0, tau != nullptr);
if (i_info.ndims() == 0) {
return af_create_handle(tau, 0, nullptr, type);
}
af_array out;
switch (type) {
case f32: out = qr_inplace<float>(in); break;
case f64: out = qr_inplace<double>(in); break;
case c32: out = qr_inplace<cfloat>(in); break;
case c64: out = qr_inplace<cdouble>(in); break;
default: TYPE_ERROR(1, type);
}
swap(*tau, out);
}
CATCHALL;
return AF_SUCCESS;
}