TRIQS/nda 2.0.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
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
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/cuBLAS dot, dotu and dotc routines.
Check if a given type satisfies the memory array concept.
Definition concepts.hpp:230
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:212
auto dotc(X const &x, Y const &y)
Interface to the BLAS/cuBLAS dotc routine.
Definition dot.hpp:72
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
Provides lazy, coefficient-wise array operations of standard mathematical functions together with ove...
Provides type traits for the nda library.