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
6#pragma once
7
12
13#pragma once
14
15#include "../algorithms.hpp"
17#include "../blas/dot.hpp"
18#include "../concepts.hpp"
20#include "../traits.hpp"
21
22#include <cmath>
23#include <complex>
24#include <limits>
25
26namespace nda {
27
46 template <ArrayOfRank<1> A>
47 double norm(A const &a, double p = 2.0) {
48 static_assert(Scalar<get_value_t<A>>, "Error in nda::norm: Only scalar value types are allowed");
49
50 if (p == 2.0) [[likely]] {
51 if constexpr (MemoryArray<A>)
52 return std::sqrt(std::real(nda::blas::dotc(a, a)));
53 else
54 return norm(make_regular(a));
55 } else if (p == 1.0) {
56 return sum(abs(a));
57 } else if (p == 0.0) {
58 long count = 0;
59 for (long i = 0; i < a.size(); ++i) {
60 if (a(i) != get_value_t<A>{0}) ++count;
61 }
62 return double(count);
63 } else if (p == std::numeric_limits<double>::infinity()) {
64 return max_element(abs(a));
65 } else if (p == -std::numeric_limits<double>::infinity()) {
66 return min_element(abs(a));
67 } else {
68 return std::pow(sum(pow(abs(a), p)), 1.0 / p);
69 }
70 }
71
72} // namespace nda
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 routine.
Check if a given type satisfies the memory array concept.
Definition concepts.hpp:248
Check if a given type is either an arithmetic or complex type.
Definition concepts.hpp:108
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:181
auto dotc(X const &x, Y const &y)
Interface to the BLAS dotc routine.
Definition dot.hpp:92
Provides lazy, coefficient-wise array operations of standard mathematical functions together with ove...
Provides type traits for the nda library.