std::norm(std::complex)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ std::norm(std::complex) πŸ’…

okay so bestie, welcome to the C++ Standard Libary page for std::norm(std::complex) β€” yes, the name says it twice, it’s giving main-character self-title energy ✨. This one is all about the squared-magnitude side of complex numbers, and honestly? it ate. Let’s get into it πŸ’–.


✨ NAME ✨

std::norm(std::complex) - std::norm(std::complex)

✨ Synopsis ✨

Defined in header <complex>

The main overload, aka the (1) one πŸ‘‘:

template< class T >
T norm( const std::complex<T>& z );

(until C++20)

template< class T >
constexpr T norm( const std::complex<T>& z );

(since C++20)

Additional overloads (since C++11) β€” also defined in header <complex> πŸ¦‹:

Until C++20, these non-constexpr floating-point besties:

float       norm( float f );
double      norm( double f );
long double norm( long double f );

(until C++20)

Since C++20 and until C++23, the constexpr float/double/long double squad β€” label (A) energy πŸ”₯:

constexpr float       norm( float f );
constexpr double      norm( double f );
constexpr long double norm( long double f );

(since C++20) (until C++23) (A)

Since C++23, the template floating-point one enters the chat 🌈:

template< class FloatingPoint >
constexpr FloatingPoint norm( FloatingPoint f );

(since C++23)

Integer overloads, because inclusivity πŸ’œ:

template< class Integer >
double norm( Integer i );

(until C++20)

template< class Integer >
constexpr double norm( Integer i );

(since C++20) (B)

(1) Returns the squared magnitude of the complex number z ✨.

A,B) Additional overloads are provided for all integer and floating-point types, which are treated as complex numbers with zero imaginary component. (since C++11) 🫢

✨ Parameters ✨

  • z β€” complex value πŸ’…
  • f β€” floating-point value 🌸
  • i β€” integer value πŸŽ€

✨ Return value ✨

  • (1) The squared magnitude of z πŸ‘‘
  • (A) The square of f πŸ”₯
  • (B) The square of i ⭐

✨ Notes ✨

The norm calculated by this function is also known as field norm or absolute square β€” iconic terminology, no cap 🧚.

The Euclidean norm of a complex number is provided by std::abs, which is more costly to compute. In some situations, it may be replaced by std::norm, for example, if abs(z1) > abs(z2) then norm(z1) > norm(z2). lowkey a useful swap πŸ’‘.

The additional overloads are not required to be provided exactly as (A,B). They only need to be sufficient to ensure that for their argument num:

  • If num has a standard (until C++23) floating-point type T, then std::norm(num) has the same effect as std::norm(std::complex<T>(num)) 🌟.
  • Otherwise, if num has an integer type, then std::norm(num) has the same effect as std::norm(std::complex<double>(num)) πŸ’–.

✨ Example ✨

run this, bestie πŸƒβ€β™€οΈπŸ’¨:

        #include <cassert>
        #include <complex>
        #include <iostream>

        int main()
        {
            constexpr std::complex<double> z {3.0, 4.0};
            static_assert(std::norm(z) == (z.real() * z.real() + z.imag() * z.imag()));
            static_assert(std::norm(z) == (z * std::conj(z)));
                   assert(std::norm(z) == (std::abs(z) * std::abs(z)));
            std::cout << "std::norm(" << z << ") = " << std::norm(z) << '\n';
        }

✨ Output ✨

std::norm((3,4)) = 25

✨ See also ✨

  • abs(std::complex) β€” returns the magnitude of a complex number (function template) 🌸
  • conj β€” returns the complex conjugate (function template) πŸ¦‹
  • polar β€” constructs a complex number from magnitude and phase angle (function template) 🌈

xoxo, your C++ bestie πŸ’– β€” source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::norm(std::complex)(3)

πŸ“¦ where this page comes from