TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
Loading...
Searching...
No Matches
norm.hpp
Go to the documentation of this file.
1
// Copyright (c) 2023--present, The Simons Foundation
2
// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3
// SPDX-License-Identifier: Apache-2.0
4
// See LICENSE in the root of this distribution for details.
5
10
11
#pragma once
12
13
#include "
../algorithms.hpp
"
14
#include "
../basic_functions.hpp
"
15
#include "
../blas/dot.hpp
"
16
#include "
../concepts.hpp
"
17
#include "
../mapped_functions.hxx
"
18
#include "
../traits.hpp
"
19
20
#include <cmath>
21
#include <complex>
22
#include <limits>
23
24
namespace
nda::linalg {
25
44
template
<ArrayOfRank<1> A>
45
requires
(Scalar<get_value_t<A>>)
46
double
norm
(A
const
&a,
double
p = 2.0) {
47
if
(p == 2.0) [[likely]] {
48
if
constexpr
(
MemoryArray<A>
)
49
return
std::sqrt(std::real(
nda::blas::dotc
(a, a)));
50
else
51
return
norm
(
make_regular
(a));
52
}
else
if
(p == 1.0) {
53
return
sum
(
abs
(a));
54
}
else
if
(p == 0.0) {
55
long
count = 0;
56
for
(
long
i = 0; i < a.size(); ++i) {
57
if
(a(i) !=
get_value_t<A>
{0}) ++count;
58
}
59
return
double(count);
60
}
else
if
(p == std::numeric_limits<double>::infinity()) {
61
return
max_element
(
abs
(a));
62
}
else
if
(p == -std::numeric_limits<double>::infinity()) {
63
return
min_element
(
abs
(a));
64
}
else
{
65
return
std::pow(
sum
(
pow
(
abs
(a), p)), 1.0 / p);
66
}
67
}
68
69
}
// namespace nda::linalg
algorithms.hpp
Provides various algorithms to be used with nda::Array objects.
basic_functions.hpp
Provides basic functions to create and manipulate arrays and views.
dot.hpp
Provides a generic interface to the BLAS dot, dotu and dotc routine.
nda::MemoryArray
Check if a given type satisfies the memory array concept.
Definition
concepts.hpp:223
concepts.hpp
Provides concepts for the nda library.
nda::max_element
auto max_element(A const &a)
Find the maximum element of an array.
Definition
algorithms.hpp:125
nda::sum
auto sum(A const &a)
Sum all the elements of an nda::Array object.
Definition
algorithms.hpp:179
nda::min_element
auto min_element(A const &a)
Find the minimum element of an array.
Definition
algorithms.hpp:144
nda::make_regular
decltype(auto) make_regular(A &&a)
Make a given object regular.
Definition
basic_functions.hpp:225
nda::abs
auto abs(A &&a)
Function abs for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
Definition
mapped_functions.hxx:96
nda::pow
auto pow(A &&a, double p)
Function pow for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
Definition
mapped_functions.hpp:76
nda::get_value_t
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition
traits.hpp:191
nda::blas::dotc
auto dotc(X const &x, Y const &y)
Interface to the BLAS dotc routine.
Definition
dot.hpp:83
nda::linalg::norm
double norm(A const &a, double p=2.0)
Calculate the p-norm of an nda::ArrayOfRank<1> object with scalar values. The p-norm is defined as.
Definition
norm.hpp:46
mapped_functions.hxx
Provides lazy, coefficient-wise array operations of standard mathematical functions together with ove...
traits.hpp
Provides type traits for the nda library.
nda
linalg
norm.hpp
Generated by
1.17.0