TRIQS/nda 1.3.0
Multi-dimensional array library for C++
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"
15#include "../blas/dot.hpp"
16#include "../concepts.hpp"
18#include "../traits.hpp"
19
20#include <cmath>
21#include <complex>
22#include <limits>
23
24namespace 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
Provides various algorithms to be used with nda::Array objects.
Provides basic functions to create and manipulate arrays and views.
Provides a generic interface to the BLAS dot, dotu and dotc routine.
Check if a given type satisfies the memory array concept.
Definition concepts.hpp:223
Provides concepts for the nda library.
auto max_element(A const &a)
Find the maximum element of an array.
auto sum(A const &a)
Sum all the elements of an nda::Array object.
auto min_element(A const &a)
Find the minimum element of an array.
decltype(auto) make_regular(A &&a)
Make a given object regular.
auto abs(A &&a)
Function abs for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
auto pow(A &&a, double p)
Function pow for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
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:182
auto dotc(X const &x, Y const &y)
Interface to the BLAS dotc routine.
Definition dot.hpp:83
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
Provides lazy, coefficient-wise array operations of standard mathematical functions together with ove...
Provides type traits for the nda library.