Complex Number
A guide to complex numbers in mathematics, covering basic operations and calculations.
Complex Number
A class representing complex numbers and their operations.
Overview
The ComplexNumber
class provides a comprehensive implementation for working with complex numbers in mathematics. It supports basic arithmetic operations, conjugate calculations, and magnitude computations.
Constructors
The ComplexNumber
class provides three constructors for creating complex numbers in different formats:
ComplexNumber()
ComplexNumber(double real, double imaginary)
Creates a complex number with the given real and imaginary parts.
Parameters:
real
: The real part (a) of the complex numberimaginary
: The imaginary part (b) of the complex number
Example:
// Create a complex number 3 + 2i
var z1 = ComplexNumber(3, 2);
print(z1); // Output: 3 + 2i
ComplexNumber.fromImaginary()
const ComplexNumber.fromImaginary(double imaginary)
Creates a complex number from an imaginary number (real part is 0).
Parameters:
imaginary
: The imaginary part (b) of the complex number
Example:
// Create a pure imaginary number 5i
var z2 = ComplexNumber.fromImaginary(5);
print(z2); // Output: 0 + 5i
ComplexNumber.fromReal()
factory ComplexNumber.fromReal(double real)
Creates a complex number from a real number (imaginary part is 0).
Parameters:
real
: The real part (a) of the complex number
Example:
// Create a real number 4
var z3 = ComplexNumber.fromReal(4);
print(z3); // Output: 4 + 0i
Properties
real
final double real
The real part of the complex number.
Example:
var z = ComplexNumber(3, 2);
print(z.real); // Output: 3.0
imaginary
final double imaginary
The imaginary part of the complex number.
Example:
var z = ComplexNumber(3, 2);
print(z.imaginary); // Output: 2.0
Methods
conjugate()
ComplexNumber conjugate()
Returns the conjugate of this complex number. The conjugate of a complex number a + bi is a - bi.
Example:
var z = ComplexNumber(3, 2);
var conj = z.conjugate();
print(conj); // Output: 3 - 2i
magnitude()
double magnitude()
Returns the magnitude (absolute value) of this complex number. The magnitude is calculated as √(a² + b²) where a is the real part and b is the imaginary part.
Example:
var z = ComplexNumber(3, 4);
print(z.magnitude()); // Output: 5.0 (as √(3² + 4²) = 5)
@override
toString() String toString()
Returns a string representation of this complex number. This method overrides the default toString() implementation.
Example:
var z = ComplexNumber(3, -2);
print(z.toString()); // Output: 3 - 2i
Operators
Addition (+)
ComplexNumber operator +(ComplexNumber other)
Adds two complex numbers. For complex numbers a + bi and c + di, the result is (a + c) + (b + d)i.
Example:
var z1 = ComplexNumber(3, 2);
var z2 = ComplexNumber(1, 4);
var sum = z1 + z2;
print(sum); // Output: 4 + 6i
Subtraction (-)
ComplexNumber operator -(ComplexNumber other)
Subtracts two complex numbers. For complex numbers a + bi and c + di, the result is (a - c) + (b - d)i.
Example:
var z1 = ComplexNumber(3, 2);
var z2 = ComplexNumber(1, 4);
var diff = z1 - z2;
print(diff); // Output: 2 - 2i
Multiplication (*)
ComplexNumber operator *(ComplexNumber other)
Multiplies two complex numbers. For complex numbers a + bi and c + di, the result is (ac - bd) + (ad + bc)i.
Example:
var z1 = ComplexNumber(3, 2);
var z2 = ComplexNumber(1, 4);
var product = z1 * z2;
print(product); // Output: -5 + 14i
Division (/)
ComplexNumber operator /(ComplexNumber other)
Divides two complex numbers. For complex numbers a + bi and c + di, the result is: ((ac + bd)/(c² + d²)) + ((bc - ad)/(c² + d²))i
Example:
var z1 = ComplexNumber(3, 2);
var z2 = ComplexNumber(1, 1);
var quotient = z1 / z2;
print(quotient); // Output: 2.5 + 0.5i
@override
Equality (==) bool operator ==(Object other)
Checks if two complex numbers are equal. This operator overrides the default implementation.
Example:
var z1 = ComplexNumber(3, 2);
var z2 = ComplexNumber(3, 2);
var z3 = ComplexNumber(3, 1);
print(z1 == z2); // Output: true
print(z1 == z3); // Output: false