-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathnorm.cpp
More file actions
151 lines (131 loc) · 5.09 KB
/
norm.cpp
File metadata and controls
151 lines (131 loc) · 5.09 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
/*******************************************************
* Copyright (c) 2025, 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 <arith.hpp>
#include <backend.hpp>
#include <common/ArrayInfo.hpp>
#include <common/cast.hpp>
#include <common/err_common.hpp>
#include <complex.hpp>
#include <copy.hpp>
#include <handle.hpp>
#include <lu.hpp>
#include <math.hpp>
#include <reduce.hpp>
#include <af/array.h>
#include <af/constants.h>
#include <af/defines.h>
#include <af/lapack.h>
#include <af/traits.hpp>
using af::dim4;
using arrayfire::common::cast;
using detail::arithOp;
using detail::Array;
using detail::cdouble;
using detail::cfloat;
using detail::createEmptyArray;
using detail::createValueArray;
using detail::getScalar;
using detail::reduce;
using detail::reduce_all;
using detail::scalar;
template<typename T>
using normReductionResult =
typename std::conditional<std::is_same<T, arrayfire::common::half>::value, float,
T>::type;
template<typename T>
double matrixNorm(const Array<T> &A, double p) {
using RT = normReductionResult<T>;
if (p == 1) {
Array<RT> colSum = reduce<af_add_t, T, normReductionResult<T>>(A, 0);
return getScalar<RT>(reduce_all<af_max_t, RT, RT>(colSum));
}
if (p == af::Inf) {
Array<RT> rowSum = reduce<af_add_t, T, RT>(A, 1);
return getScalar<RT>(reduce_all<af_max_t, RT, RT>(rowSum));
}
AF_ERROR("This type of norm is not supported in ArrayFire\n",
AF_ERR_NOT_SUPPORTED);
}
template<typename T>
double vectorNorm(const Array<T> &A, double p) {
using RT = normReductionResult<T>;
if (p == 1) { return getScalar<RT>(reduce_all<af_add_t, T, RT>(A)); }
if (p == af::Inf) {
return getScalar<RT>(reduce_all<af_max_t, RT, RT>(cast<RT>(A)));
} else if (p == 2) {
Array<T> A_sq = arithOp<T, af_mul_t>(A, A, A.dims());
return std::sqrt(getScalar<RT>(reduce_all<af_add_t, T, RT>(A_sq)));
}
Array<T> P = createValueArray<T>(A.dims(), scalar<T>(p));
Array<T> A_p = arithOp<T, af_pow_t>(A, P, A.dims());
return std::pow(getScalar<RT>(reduce_all<af_add_t, T, RT>(A_p)), (1.0 / p));
}
template<typename T>
double LPQNorm(const Array<T> &A, double p, double q) {
using RT = normReductionResult<T>;
Array<RT> A_p_norm = createEmptyArray<RT>(dim4());
if (p == 1) {
A_p_norm = reduce<af_add_t, T, RT>(A, 0);
} else {
Array<T> P = createValueArray<T>(A.dims(), scalar<T>(p));
Array<RT> invP = createValueArray<RT>(A.dims(), scalar<RT>(1.0 / p));
Array<T> A_p = arithOp<T, af_pow_t>(A, P, A.dims());
Array<RT> A_p_sum = reduce<af_add_t, T, RT>(A_p, 0);
A_p_norm = arithOp<RT, af_pow_t>(A_p_sum, invP, invP.dims());
}
if (q == 1) {
return getScalar<RT>(reduce_all<af_add_t, RT, RT>(A_p_norm));
}
Array<RT> Q = createValueArray<RT>(A_p_norm.dims(), scalar<RT>(q));
Array<RT> A_p_norm_q = arithOp<RT, af_pow_t>(A_p_norm, Q, Q.dims());
return std::pow(getScalar<RT>(reduce_all<af_add_t, RT, RT>(A_p_norm_q)),
(1.0 / q));
}
template<typename T>
double norm(const af_array a, const af_norm_type type, const double p,
const double q) {
using BT = typename af::dtype_traits<T>::base_type;
const Array<BT> A = detail::abs<BT, T>(getArray<T>(a));
switch (type) {
case AF_NORM_EUCLID: return vectorNorm(A, 2);
case AF_NORM_VECTOR_1: return vectorNorm(A, 1);
case AF_NORM_VECTOR_INF: return vectorNorm(A, af::Inf);
case AF_NORM_VECTOR_P: return vectorNorm(A, p);
case AF_NORM_MATRIX_1: return matrixNorm(A, 1);
case AF_NORM_MATRIX_INF: return matrixNorm(A, af::Inf);
case AF_NORM_MATRIX_2: return matrixNorm(A, 2);
case AF_NORM_MATRIX_L_PQ: return LPQNorm(A, p, q);
default:
AF_ERROR("This type of norm is not supported in ArrayFire\n",
AF_ERR_NOT_SUPPORTED);
}
}
af_err af_norm(double *out, const af_array in, const af_norm_type type,
const double p, const double q) {
try {
const 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 i_type = i_info.getType();
ARG_ASSERT(1, i_info.isFloating()); // Only floating and complex types
*out = 0;
if (i_info.ndims() == 0) { return AF_SUCCESS; }
switch (i_type) {
case f32: *out = norm<float>(in, type, p, q); break;
case f64: *out = norm<double>(in, type, p, q); break;
case c32: *out = norm<cfloat>(in, type, p, q); break;
case c64: *out = norm<cdouble>(in, type, p, q); break;
case f16: *out = norm<arrayfire::common::half>(in, type, p, q); break;
default: TYPE_ERROR(1, i_type);
}
}
CATCHALL;
return AF_SUCCESS;
}