C++-Standardbibliothek<complex>
In Mathematik und Ingenieurberechnungen müssen wir oft mit komplexen Zahlen umgehen, beispielsweise in der Signalverarbeitung, bei Fourier-Transformationen, der Schaltungsanalyse usw.
Die C++-Standardbibliothek bietet<complex>Die Header-Datei ermöglicht es Ihnen, mit komplexen Zahlen so einfach wie mit gewöhnlichen Zahlen zu arbeiten.
Eine komplexe Zahl besteht aus einem Realteil und einem Imaginärteil, in der Form:
z = a + bi
Eine komplexe Zahl ist eine Kombination aus einer reellen Zahl und einer imaginären Zahl, üblicherweise dargestellt alsa + bi, wobeiaist der Realteil,bist der Imaginärteil,iist die imaginäre Einheit, für die gilti^2 = -1。
In C++ ist der Typ complexstd::complex<T>dargestellt, wobeiTein beliebiger arithmetischer Typ sein kann, wie z. B.float、doubleoderlong double。
Um<complex>Für die Bibliothek müssen Sie zuerst diese Header-Datei in Ihr C++-Programm einbinden:
#include <iostream> #include <complex>
Beispiel
#include <complex> // Header-Datei für komplexe Zahlen
int main() {
std::complex<double> z1(3.0, 4.0); // 3 + 4i
std::complex<double> z2(1.0, -2.0); // 1 - 2i
std::cout << "z1 = " << z1 << std::endl;
std::cout << "z2 = " << z2 << std::endl;
}
Ausgabe:
z1 = (3,4) z2 = (1,-2)
Grundlegende Syntax
Komplexe Zahlen erstellen
std::complex<double> c(5.0, 3.0); // 创建一个复数 5 + 3i
Zugriff auf Real- und Imaginärteile
double realPart = c.real(); // 获取实部 double imagPart = c.imag(); // 获取虚部
Grundlegende Operationen mit komplexen Zahlen
C++-Standardbibliothek<complex>unterstützt die folgenden grundlegenden Operationen:
- Addition:
operator+ - Subtraktion:
operator- - Multiplikation:
operator* - Division:
operator/ - Konjugierte:
conj - Betrag:
abs - Argument:
arg
1. Ermitteln der Real- und Imaginärteile
std::cout << "实部: " << z1.real() << std::endl; // 3 std::cout << "虚部: " << z1.imag() << std::endl; // 4
2. Vier Grundrechenarten:
Beispiel
auto z4 = z1 - z2; // (2, 6)
auto z5 = z1 * z2; // (11, -2)
auto z6 = z1 / z2; // (-1, 2)
std::cout << "z1 + z2 = " << z3 << std::endl;
std::cout << "z1 * z2 = " << z5 << std::endl;
3. Häufige Funktionen
Header-Datei<complex>bietet viele mathematische Funktionen im Zusammenhang mit komplexen Zahlen.
Beispiel
std::cout << "Betrag |z1| = " << std::abs(z1) << std::endl; // 5
std::cout << "Argument arg(z1) = " << std::arg(z1) << std::endl; // 0.927 (radians)
std::cout << "Konjugierte conjugate(z1) = " << std::conj(z1) << std::endl; // (3,-4)
std::cout << "exp(z1) = " << std::exp(z1) << std::endl; // e^(3+4i)
std::cout << "sin(z1) = " << std::sin(z1) << std::endl;
std::cout << "cos(z1) = " << std::cos(z1) << std::endl;
4. Polarkoordinatendarstellung
Manchmal müssen wir komplexe Zahlen in Polarkoordinaten darstellen (Betrag + Phasenwinkel).
Beispiel
double r = 5.0; // Betrag
double theta = M_PI / 4; // 45 degrees
std::complex<double> z = std::polar(r, theta);
std::cout << "Polarform der komplexen Zahl z = " << z << std::endl; // (3.53553,3.53553)
5. Template-Parameter
std::complex<T>Es ist eine Template-Klasse, die verschiedene numerische Typen unterstützt:
std::complex<float>std::complex<double>(häufig verwendet)std::complex<long double>
Beispiel
std::complex<double> zd(1.0, 2.0);
Beispiel
Nachfolgend ein Beispiel unter Verwendung von<complex>Ein einfaches Beispiel für die Header-Datei, einschließlich des Erstellens komplexer Zahlen, grundlegender Operationen und der Ausgabe von Ergebnissen.
Beispiel
#include <complex>
int main() {
// Zwei komplexe Zahlen erstellen
std::complex<double> c1(5.0, 3.0); // 5 + 3i
std::complex<double> c2(2.0, -4.0); // 2 - 4i
// Komplexe Zahlen ausgeben
std::cout << "c1: " << c1 << std::endl; // (5,3)
std::cout << "c2: " << c2 << std::endl; // (2,-4)
// Addition komplexer Zahlen
std::complex<double> sum = c1 + c2;
std::cout << "Summe: " << sum << std::endl; // 7 - i
// Subtraktion komplexer Zahlen
std::complex<double> diff = c1 - c2;
std::cout << "Differenz: " << diff << std::endl; // 3 + 7i
// Multiplikation komplexer Zahlen
std::complex<double> product = c1 * c2;
std::cout << "Produkt: " << product << std::endl; // 22 - 14i
// Division komplexer Zahlen
std::complex<double> quotient = c1 / c2;
std::cout << "Quotient: " << quotient << std::endl; // -0.1 + 1.3i
// Komplexe Konjugation
std::complex<double> conjugate = std::conj(c1);
std::cout << "Konjugiertes von c1: " << conjugate << std::endl; // 5 - 3i
// Komplexer Betrag
double modulus = std::abs(c1);
std::cout << "Betrag von c1: " << modulus << std::endl; // sqrt(34) ≈ 5.83095
// Komplexes Argument (Bogenmaß)
double argument = std::arg(c1);
std::cout << "Argument von c1: " << argument << std::endl; // atan(3/5) ≈ 0.54042 rad
return 0;
}
Wenn Sie das obige Programm ausführen, erhalten Sie die folgende Ausgabe:
c1: (5,3) c2: (2,-4) Sum: (7,-1) Difference: (3,7) Product: (22,-14) Quotient: (-0.1,1.3) Conjugate of c1: (5,-3) Modulus of c1: 5.83095 Argument of c1: 0.54042Weitere Erweiterungen