名稱空間
變體
操作

std::norm(std::complex)

來自 cppreference.com
< cpp‎ | 數值‎ | 複數
 
 
 
 
定義於標頭檔案 <complex>
(1)
template< class T >
T norm( const std::complex<T>& z );
(C++20 前)
template< class T >
constexpr T norm( const std::complex<T>& z );
(C++20 起)
額外過載 (自 C++11 起)
定義於標頭檔案 <complex>
(A)
float       norm( float f );

double      norm( double f );

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

constexpr double      norm( double f );

constexpr long double norm( long double f );
(C++20 起)
(直至 C++23)
template< class FloatingPoint >
constexpr FloatingPoint norm( FloatingPoint f );
(C++23 起)
(B)
template< class Integer >
double norm( Integer i );
(C++20 前)
template< class Integer >
constexpr double norm( Integer i );
(C++20 起)
1) 返回複數 z 的模的平方。
A,B) 為所有整數和浮點型別提供了額外的過載,這些型別被視為虛部為零的複數。
(C++11 起)

目錄

[edit] 引數

z - 複數型別的值
f - 浮點值
i - 整數值

[edit] 返回值

1) z 的模的平方。
A) f 的平方。
B) i 的平方。

[edit] 注意

此函式計算的範數也稱為域範數絕對平方

複數的歐幾里得範數std::abs 提供,其計算成本更高。在某些情況下,它可以被 std::norm 替換,例如,如果 abs(z1) > abs(z2)norm(z1) > norm(z2)

不需要完全按照 (A,B) 提供額外的過載。它們只需要足以確保對於它們的引數 num

  • 如果 num 具有標準(C++23 前)浮點型別 T,則 std::norm(num)std::norm(std::complex<T>(num)) 具有相同的效果。
  • 否則,如果 num 具有整數型別,則 std::norm(num)std::norm(std::complex<double>(num)) 具有相同的效果。

[edit] 示例

#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';
}

輸出

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

[edit] 參閱

返回複數的模
(函式模板) [編輯]
返回複共軛
(函式模板) [編輯]
從模和相角構造一個複數
(函式模板) [編輯]