forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvd.cpp
More file actions
121 lines (99 loc) · 3.76 KB
/
svd.cpp
File metadata and controls
121 lines (99 loc) · 3.76 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
/*******************************************************
* 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 <af/array.h>
#include <af/dim4.hpp>
#include <af/lapack.h>
#include <Array.hpp>
#include <backend.hpp>
#include <common/err_common.hpp>
#include <handle.hpp>
#include <svd.hpp>
#include <af/defines.h>
using namespace detail;
template<typename T>
static inline void svd(af_array *s, af_array *u, af_array *vt,
const af_array in) {
const ArrayInfo &info = getInfo(in); // ArrayInfo is the base class which
af::dim4 dims = info.dims();
int M = dims[0];
int N = dims[1];
typedef typename af::dtype_traits<T>::base_type Tr;
// Allocate output arrays
Array<Tr> sA = createEmptyArray<Tr>(af::dim4(min(M, N)));
Array<T> uA = createEmptyArray<T>(af::dim4(M, M));
Array<T> vtA = createEmptyArray<T>(af::dim4(N, N));
svd<T, Tr>(sA, uA, vtA, getArray<T>(in));
*s = getHandle(sA);
*u = getHandle(uA);
*vt = getHandle(vtA);
}
template<typename T>
static inline void svdInPlace(af_array *s, af_array *u, af_array *vt,
af_array in) {
const ArrayInfo &info = getInfo(in); // ArrayInfo is the base class which
af::dim4 dims = info.dims();
int M = dims[0];
int N = dims[1];
typedef typename af::dtype_traits<T>::base_type Tr;
// Allocate output arrays
Array<Tr> sA = createEmptyArray<Tr>(af::dim4(min(M, N)));
Array<T> uA = createEmptyArray<T>(af::dim4(M, M));
Array<T> vtA = createEmptyArray<T>(af::dim4(N, N));
svdInPlace<T, Tr>(sA, uA, vtA, getArray<T>(in));
*s = getHandle(sA);
*u = getHandle(uA);
*vt = getHandle(vtA);
}
af_err af_svd(af_array *u, af_array *s, af_array *vt, const af_array in) {
try {
const ArrayInfo &info = getInfo(in);
af::dim4 dims = info.dims();
ARG_ASSERT(3, (dims.ndims() >= 0 && dims.ndims() <= 3));
af_dtype type = info.getType();
if (dims.ndims() == 0) {
AF_CHECK(af_create_handle(u, 0, nullptr, type));
AF_CHECK(af_create_handle(s, 0, nullptr, type));
AF_CHECK(af_create_handle(vt, 0, nullptr, type));
return AF_SUCCESS;
}
switch (type) {
case f64: svd<double>(s, u, vt, in); break;
case f32: svd<float>(s, u, vt, in); break;
case c64: svd<cdouble>(s, u, vt, in); break;
case c32: svd<cfloat>(s, u, vt, in); break;
default: TYPE_ERROR(1, type);
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_svd_inplace(af_array *u, af_array *s, af_array *vt, af_array in) {
try {
const ArrayInfo &info = getInfo(in);
af::dim4 dims = info.dims();
ARG_ASSERT(3, (dims.ndims() >= 0 && dims.ndims() <= 3));
af_dtype type = info.getType();
if (dims.ndims() == 0) {
AF_CHECK(af_create_handle(u, 0, nullptr, type));
AF_CHECK(af_create_handle(s, 0, nullptr, type));
AF_CHECK(af_create_handle(vt, 0, nullptr, type));
return AF_SUCCESS;
}
DIM_ASSERT(3, dims[0] >= dims[1]);
switch (type) {
case f64: svdInPlace<double>(s, u, vt, in); break;
case f32: svdInPlace<float>(s, u, vt, in); break;
case c64: svdInPlace<cdouble>(s, u, vt, in); break;
case c32: svdInPlace<cfloat>(s, u, vt, in); break;
default: TYPE_ERROR(1, type);
}
}
CATCHALL;
return AF_SUCCESS;
}