- complex[meta header]
- std[meta namespace]
- class template[meta id-type]
namespace std {
template <class T>
class complex;
}
<complex>
ヘッダでは、複素数を扱うクラスおよび関数を定義する。
complex
クラステンプレートは、複素数を表すクラスである。このクラスは、実部(real part)と虚部(imaginary part)を、それぞれ型T
の値として保持し、演算に使用する。
complex
クラステンプレートはCV修飾されていない浮動小数点数型 (C++23以降は拡張浮動小数点数型を含む) で特殊化され、特化した実装が行われる:
これ以外の型がテンプレート引数として与えられた場合、その動作は未規定である。
complex
クラスの左辺値オブジェクトc
は、reinterpret_cast<cv修飾 T(&)[2]>(c)
もしくはreinterpret_cast<cv修飾 T*>(&c)
で浮動小数点数型の配列にキャスト可能である。その場合、配列の0
番目は実部を表し、1
番目は虚部を表す。
名前 |
説明 |
対応バージョン |
value_type |
実部と虚部およびそれらの演算に使用する浮動小数点数型。テンプレートパラメータの型T 。 |
|
名前 |
説明 |
対応バージョン |
i |
complex<double> のリテラル |
C++14 |
if |
complex<float> のリテラル |
C++14 |
il |
complex<long double> のリテラル |
C++14 |
名前 |
説明 |
対応バージョン |
acos |
複素数の逆余弦を求める |
C++11 |
asin |
複素数の逆正弦を求める |
C++11 |
atan |
複素数の逆正接を求める |
C++11 |
acosh |
複素数の双曲線逆余弦を求める |
C++11 |
asinh |
複素数の双曲線逆正弦を求める |
C++11 |
atanh |
複素数の双曲線逆正接を求める |
C++11 |
cos |
複素数の余弦を求める |
|
cosh |
複素数の双曲線余弦を求める |
|
exp |
自然対数の底 e の累乗(複素数)を求める。 |
|
log |
複素数の自然対数を求める |
|
log10 |
複素数の常用対数を求める |
|
pow |
複素数の累乗を求める |
|
sin |
複素数の正弦を求める |
|
sinh |
複素数の双曲線正弦を求める |
|
sqrt |
複素数の平方根を求める |
|
tan |
複素数の正接を求める |
|
tanh |
複素数の双曲線正接を求める |
|
#include <iostream>
#include <complex>
float pi()
{
return 3.141593f;
}
int main()
{
// 実部1.0f、虚部2.0fの複素数オブジェクトを作る
std::complex<float> c(1.0f, 2.0f);
// ストリーム出力
std::cout << "output : " << c << std::endl;
// 各要素の取得
float real = c.real(); // 実部
float imag = c.imag(); // 虚部
std::cout << "real : " << real << std::endl;
std::cout << "imag : " << imag << std::endl;
// 演算
std::complex<float> a(1.0f, 2.0f);
std::complex<float> b(2.0f, 3.0f);
std::cout << "a + b : " << a + b << std::endl;
std::cout << "a - b : " << a - b << std::endl;
std::cout << "a * b : " << a * b << std::endl;
std::cout << "a / b : " << a / b << std::endl;
// 各複素数の値を取得する
std::cout << "abs : " << std::abs(c) << std::endl; // 絶対値
std::cout << "arg : " << std::arg(c) << std::endl; // 偏角
std::cout << "norm : " << std::norm(c) << std::endl; // ノルム
std::cout << "conj : " << std::conj(c) << std::endl; // 共役複素数
std::cout << "proj : " << std::proj(c) << std::endl; // リーマン球面への射影
std::cout << "polar : " << std::polar(1.0f, pi() / 2.0f); // 極座標(絶対値:1.0、偏角:円周率÷2.0)から複素数を作る
}
- std::complex[color ff0000]
- c.real()[link complex/real.md]
- c.imag()[link complex/imag.md]
- std::abs[link complex/abs.md]
- std::arg[link complex/arg.md]
- std::norm[link complex/norm.md]
- std::conj[link complex/conj.md]
- std::proj[link complex/proj.md]
- std::polar[link complex/polar.md]
output : (1,2)
real : 1
imag : 2
a + b : (3,5)
a - b : (-1,-1)
a * b : (-4,7)
a / b : (0.615385,0.0769231)
abs : 2.23607
arg : 1.10715
norm : 5
conj : (1,-2)
proj : (1,2)
polar : (-1.62921e-07,1)