Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/complex/norm"

From cppreference.com
< cpp‎ | numeric‎ | complex
m (std::)
m (http -> https)
 
(8 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
{{cpp/numeric/complex/navbar}}
 
{{cpp/numeric/complex/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | complex}}
+
{{dcl header|complex}}
{{dcl rev multi| num=1 | until1=c++20|dcl1=
+
{{dcl rev multi|num=1|dcl1=
 
template< class T >  
 
template< class T >  
 
T norm( const std::complex<T>& z );
 
T norm( const std::complex<T>& z );
Line 10: Line 10:
 
constexpr T norm( const std::complex<T>& z );
 
constexpr T norm( const std::complex<T>& z );
 
}}
 
}}
{{dcl rev multi | num=2 | since1=c++11 | dcl1=
+
{{dcl h|[[#Notes|Additional overloads]] {{mark since c++11}}}}
float norm( float z );
+
{{dcl header|complex}}
template< class DoubleOrInteger >
+
{{dcl rev multi|num=A|dcl1=
double norm( DoubleOrInteger z );
+
float       norm( float f );
long double norm( long double z );
+
double     norm( double f );
 +
long double norm( long double f );
 
|since2=c++20|dcl2=
 
|since2=c++20|dcl2=
constexpr float norm( float z );
+
constexpr float       norm( float f );
template< class DoubleOrInteger >
+
constexpr double      norm( double f );
constexpr double norm( DoubleOrInteger z );
+
constexpr long double norm( long double f );
constexpr long double norm( long double z );
+
|since3=c++23|dcl3=
 +
template< class FloatingPoint >
 +
constexpr FloatingPoint norm( FloatingPoint f );
 +
}}
 +
{{dcl rev multi|num=B|dcl1=
 +
template< class Integer >
 +
double norm( Integer i );
 +
|since2=c++20|dcl2=
 +
template< class Integer >
 +
constexpr double norm( Integer i );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Returns the squared magnitude of the complex number {{tt|z}}.
+
@1@ Returns the squared magnitude of the complex number {{c|z}}.
  
 
{{rrev|since=c++11|
 
{{rrev|since=c++11|
@2@ Additional overloads are provided for {{c|float}}, {{c|double}}, {{c|long double}}, and all integer types, which are treated as complex numbers with zero imaginary component.
+
@A,B@ Additional overloads are provided for all integer and floating-point types, which are treated as complex numbers with zero imaginary component.
 
}}
 
}}
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | z | complex value}}
+
{{par|z|complex value}}
 +
{{par|f|floating-point value}}
 +
{{par|i|integer value}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
 
+
@1@ The squared magnitude of {{c|z}}.
the squared magnitude of {{tt|z}}
+
@A@ The square of {{c|f}}.
 +
@B@ The square of {{c|i}}.
  
 
===Notes===
 
===Notes===
The norm calculated by this function is also known as [[enwiki:Field norm|field norm]] or [http://mathworld.wolfram.com/AbsoluteSquare.html absolute square].
+
The norm calculated by this function is also known as {{enwiki|Field norm|field norm}} or [https://mathworld.wolfram.com/AbsoluteSquare.html absolute square].
  
The ''Euclidean norm'' of a complex number is provided by {{ltt|cpp/numeric/complex/abs|std::abs}}, which is more costly to compute. In some situations, it may be replaced by {{tt|std::norm}}, for example, if {{c|abs(z1) > abs(z2)}} then {{c|norm(z1) > norm(z2)}}.
+
The {{enwiki|Euclidean space#Euclidean norm|Euclidean norm}} of a complex number is provided by {{ltt std|cpp/numeric/complex/abs}}, which is more costly to compute. In some situations, it may be replaced by {{tt|std::norm}}, for example, if {{c|abs(z1) > abs(z2)}} then {{c|norm(z1) > norm(z2)}}.
 +
 
 +
{{cpp/numeric/complex/additional overload note|norm}}
 +
 
 +
===Example===
 +
{{example
 +
|code=
 +
#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===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/complex/dsc abs}}
+
{{dsc inc|cpp/numeric/complex/dsc abs}}
{{dsc inc | cpp/numeric/complex/dsc conj}}
+
{{dsc inc|cpp/numeric/complex/dsc conj}}
{{dsc inc | cpp/numeric/complex/dsc polar}}
+
{{dsc inc|cpp/numeric/complex/dsc polar}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/numeric/complex/norm]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/numeric/complex/norm]]
+
[[fr:cpp/numeric/complex/norm]]
+
[[it:cpp/numeric/complex/norm]]
+
[[ja:cpp/numeric/complex/norm]]
+
[[pt:cpp/numeric/complex/norm]]
+
[[ru:cpp/numeric/complex/norm]]
+
[[zh:cpp/numeric/complex/norm]]
+

Latest revision as of 07:45, 14 September 2023

 
 
 
 
Defined in header <complex>
(1)
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)
Defined in header <complex>
(A)
float       norm( float f );

double      norm( double f );

long double norm( long double f );
(until C++20)
constexpr float       norm( float f );

constexpr double      norm( double f );

constexpr long double norm( long double f );
(since C++20)
(until C++23)
template< class FloatingPoint >
constexpr FloatingPoint norm( FloatingPoint f );
(since C++23)
(B)
template< class Integer >
double norm( Integer i );
(until C++20)
template< class Integer >
constexpr double norm( Integer i );
(since C++20)
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)

Contents

[edit] Parameters

z - complex value
f - floating-point value
i - integer value

[edit] Return value

1) The squared magnitude of z.
A) The square of f.
B) The square of i.

[edit] Notes

The norm calculated by this function is also known as field norm or absolute square.

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).

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)).

[edit] Example

#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

[edit] See also

returns the magnitude of a complex number
(function template) [edit]
returns the complex conjugate
(function template) [edit]
constructs a complex number from magnitude and phase angle
(function template) [edit]