forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdet.cpp
More file actions
101 lines (80 loc) · 2.44 KB
/
det.cpp
File metadata and controls
101 lines (80 loc) · 2.44 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
/*******************************************************
* 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/lapack.h>
#include <af/defines.h>
#include <err_common.hpp>
#include <handle.hpp>
#include <backend.hpp>
#include <ArrayInfo.hpp>
#include <math.hpp>
#include <lu.hpp>
#include <diagonal.hpp>
#include <copy.hpp>
using af::dim4;
using namespace detail;
template<typename T>
T det(const af_array a)
{
const Array<T> A = getArray<T>(a);
const int num = A.dims()[0];
std::vector<T> hD(num);
std::vector<int> hP(num);
Array<T> D = createEmptyArray<T>(dim4());
Array<int> pivot = createEmptyArray<int>(dim4());
// Free memory as soon as possible
{
Array<T> A_copy = copyArray<T>(A);
Array<int> pivot = lu_inplace(A_copy, false);
copyData(&hP[0], pivot);
Array<T> D = diagExtract(A_copy, 0);
copyData(&hD[0], D);
}
bool is_neg = false;
T res = scalar<T>(is_neg ? -1 : 1);
for (int i = 0; i < num; i++) {
res = res * hD[i];
is_neg ^= (hP[i] != (i+1));
}
if (is_neg) res = res * scalar<T>(-1);
return res;
}
af_err af_det(double *real_val, double *imag_val, const af_array in)
{
try {
ArrayInfo i_info = getInfo(in);
if (i_info.ndims() > 2) {
AF_ERROR("solve can not be used in batch mode", AF_ERR_BATCH);
}
af_dtype type = i_info.getType();
DIM_ASSERT(1, i_info.dims()[0] == i_info.dims()[1]); // Only square matrices
ARG_ASSERT(1, i_info.isFloating()); // Only floating and complex types
*real_val = 0;
*imag_val = 0;
cfloat cfval;
cdouble cdval;
switch(type) {
case f32: *real_val = det<float >(in); break;
case f64: *real_val = det<double >(in); break;
case c32:
cfval = det<cfloat >(in);
*real_val = real(cfval);
*imag_val = imag(cfval);
break;
case c64:
cdval = det<cdouble>(in);
*real_val = real(cdval);
*imag_val = imag(cdval);
break;
default: TYPE_ERROR(1, type);
}
}
CATCHALL;
return AF_SUCCESS;
}