The conj function in C++ is present in the standard library. C++ provides a wide range of inbuilt functions for complex numbers. It is also a built-in function that deals with complex numbers. This function is provided by the <comple> header file. The main intention of this function is to return the conjugate of the complex number when we give a complex number to it. In this article, we will learn about the syntax of the conj function, programs related to this function and an explanation of the function.
Example:
conjugate of complex number (a + bi) is (a-bi)
where a and b are the real numbers
Syntax of the conj function:
It has the following syntax:
#include <complex>
Using namespace std;
complex<T> conj(const complex<T>& x);
Here, "T" represents the data types of the real and imaginary parts of the complex numbers. This function takes the complex number "x" as argument, and it returns the complex conjugate of the complex number "x" .
Example:
Let us take a C++ program to illustrate the conj function:
#include <iostream>
#include <complex>
using namespace std;
int main() {
double real_part;
cout << "Enter the real part of the complex number" << endl;
cin >> real_part;
double imaginary_part;
cout << "Enter the imaginary part of the complex number" << endl;
cin >> imaginary_part;
complex<double> complexNumber(real_part, imaginary_part);
complex<double> conjugate = conj(complexNumber);
cout << "Complex Number: " << complexNumber << endl;
cout << "Complex Conjugate: " << conjugate << endl;
return 0;
}
Output:
Explanation:
In this example, we include the <complex> header file in the program. Two variables, realpart and imaginarypart , are declared and given the values taken from the user input. Here, complexNumber<> will represent the complex number with a real part and an imaginary part, and it creates the complex object. After that, this complex number will be given to the conj function so that it returns the conjugate of the complex number, and then the complex and its conjugate will also printed.
Example 2:
Let us take an example to find the magnitude of the complex number using the conj function in C++:
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
double magnitude(const complex<double>& complexNumber) {
complex<double> conjugate = conj(complexNumber);
return sqrt(complexNumber.real() * complexNumber.real() + conjugate.imag() * conjugate.imag());
}
int main() {
complex<double> complexNumber(3.0, 4.0); // 3 + 4i
double mag = magnitude(complexNumber);
cout << "Magnitude of Complex Number: " << mag << endl;
return 0;
}
Output:
Explanation:
In this example, a static complex number is taken, and its magnitude is calculated and then printed in the console. In this program, we also included headers like <complex> and <cmath> for finding the magnitude.
Example:
Let us take a C++ program to illustrate the conj function and its usage:
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
int main() {
complex<double> complexPower(120, 80);
complex<double> complexConjugate = conj(complexPower);
double apparentPower = abs(complexPower);
double realPower = complexPower.real();
double reactivePower = abs(complexPower.imag());
cout << "Complex Power: " << complexPower << " VA" << endl;
cout << "Complex Conjugate: " << complexConjugate << " VA" << endl;
cout << "Apparent Power: " << apparentPower << " VA" << endl;
cout << "Real Power: " << realPower << " watts" << endl;
cout << "Reactive Power: " << reactivePower << " VAR" << endl;
return 0;
}
Output:
Explanation:
In this example, AC circuit power of 120 + 80i volts amps is a complex number. Its real part represents the actual power consumed by the circuit, and the imaginary part represents the reactive power.
We first calculate the complex conjugate of the complex number by using the inbuilt function conj . After that, we calculate the magnitude of the complex number, which is equivalent to the apparent power consumed by the circuit.
The real part of the complex power represents the actual power consumed in watts.
The imaginary part of the complex power represents the reactive power consumed in VAR (volt-amperes reactive) .
This example demonstrates how complex numbers and the std::conj function can be used to solve real-world problems in electrical engineering, helping engineers analyze and optimize AC circuits.
Conclusion:
The conj function in C++ provides a convenient way to calculate the complex conjugate of a complex number. Its simplicity and efficiency make it a valuable tool for working with complex numbers in various applications, ranging from mathematics and physics to engineering and signal processing. By understanding the syntax and usage of the conj function, programmers can enhance their ability to manipulate complex numbers effectively, opening doors to a wide array of computational possibilities.