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
47
template
<ArrayOfRank<1> X>
48
requires
(Scalar<get_value_t<X>>)
49
double
norm
(X
const
&x,
double
p = 2.0) {
50
if
(p == 2.0) [[likely]] {
51
if
constexpr
(
MemoryArray<X>
)
52
return
std::sqrt(std::real(
nda::blas::dotc
(x, x)));
53
else
54
return
norm
(
make_regular
(x));
55
}
else
if
(p == 1.0) {
56
return
sum
(
abs
(x));
57
}
else
if
(p == 0.0) {
58
long
count = 0;
59
for
(
long
i = 0; i < x.size(); ++i) {
60
if
(x(i) !=
get_value_t<X>
{0}) ++count;
61
}
62
return
double(count);
63
}
else
if
(p == std::numeric_limits<double>::infinity()) {
64
return
max_element
(
abs
(x));
65
}
else
if
(p == -std::numeric_limits<double>::infinity()) {
66
return
min_element
(
abs
(x));
67
}
else
{
68
return
std::pow(
sum
(
pow
(
abs
(x), p)), 1.0 / p);
69
}
70
}
71
72
}
// 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/cuBLAS dot, dotu and dotc routines.
nda::MemoryArray
Check if a given type satisfies the memory array concept.
Definition
concepts.hpp:230
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:77
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:212
nda::blas::dotc
auto dotc(X const &x, Y const &y)
Interface to the BLAS/cuBLAS dotc routine.
Definition
dot.hpp:72
nda::linalg::norm
double norm(X const &x, double p=2.0)
Calculate the -norm of a 1-dimensional array/view with scalar elements.
Definition
norm.hpp:49
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