Complex Number
Definition​
- Definition
- Explanation
- Guidance
- Tips
The Complex Number Algorithm is designed to perform arithmetic operations (addition, subtraction, multiplication, division) on complex numbers. Complex numbers consist of a real part and an imaginary part and are represented in the form a + bi, where 'a' is the real part, 'b' is the imaginary part, and 'i' is the imaginary unit (√-1
)
- Addition:
- add their real parts and imaginary parts separately
- Subtraction
- subtract the corresponding real and imaginary parts
- Multiplication
- use the distributive property and multiply each term of one number by each term of the other
- Division
- multiply the numerator and denominator by the complex conjugate of the denominator, then simplify
- Addition
- add the real parts of both complex numbers
- add the imaginary parts of both complex numbers
- combine the results to get the sum
- Subtraction
- subtract the real part of the second complex number from the real part of the first
- subtract the imaginary part of the second complex number from the imaginary part of the first
- combine the results to get the difference
- Multiplication
- multiply the real parts of both complex numbers
- multiply the imaginary parts of both complex numbers
- combine the results to get the product
- Division
- find the complex conjugate of the denominator
- multiply the numerator and denominator by the complex conjugate
- simplify the result to obtain the quotient
- ensure that the programming language you're using supports complex number arithmetic natively or through libraries
- pay attention to handling special cases, such as division by zero or complex numbers with zero imaginary parts
- use built-in functions or libraries for complex arithmetic operations to simplify implementation and improve efficiency
Practice​
- Practice
- Solution
addComplex(a, b):
return (a.real + b.real, a.imag + b.imag)
subtractComplex(a, b):
return (a.real - b.real, a.imag - b.imag)
multiplyComplex(a, b):
real = (a.real * b.real) - (a.imag * b.imag)
imag = (a.real * b.imag) + (a.imag * b.real)
return (real, imag)
divideComplex(a, b):
conjugate_b = (b.real, -b.imag)
numerator = multiplyComplex(a, conjugate_b)
denominator = multiplyComplex(b, conjugate_b)
return (numerator.real / denominator.real, numerator.imag / denominator.real)
package main
import (
"math"
)
type ComplexNumber struct {
re float64
im float64
}
func NewComplexNumber(re float64, im float64) *ComplexNumber {
return &ComplexNumber{re, im}
}
func (c *ComplexNumber) Add(addend *ComplexNumber) *ComplexNumber {
return NewComplexNumber(c.re+addend.re, c.im+addend.im)
}
func (c *ComplexNumber) Subtract(subtrahend *ComplexNumber) *ComplexNumber {
return NewComplexNumber(c.re-subtrahend.re, c.im-subtrahend.im)
}
func (c *ComplexNumber) Multiply(multiplicand *ComplexNumber) *ComplexNumber {
return NewComplexNumber(c.re*multiplicand.re-c.im*multiplicand.im, c.re*multiplicand.im+c.im*multiplicand.re)
}
func (c *ComplexNumber) Divide(divider *ComplexNumber) *ComplexNumber {
finalDivider := math.Pow(divider.re, 2) + math.Pow(divider.im, 2)
dividerConjugate := NewComplexNumber(divider.re, -divider.im)
finalDivident := c.Multiply(dividerConjugate)
return NewComplexNumber(finalDivident.re/finalDivider, finalDivident.im/finalDivider)
}
func (c *ComplexNumber) Conjugate() *ComplexNumber {
return NewComplexNumber(c.re, -c.im)
}
func (c *ComplexNumber) GetRadius() float64 {
return math.Sqrt(math.Pow(c.re, 2) + math.Pow(c.im, 2))
}
func (c *ComplexNumber) GetPhase(inRadians bool) float64 {
phase := math.Atan(math.Abs(c.im) / math.Abs(c.re))
if c.re < 0 && c.im > 0 {
phase = math.Pi - phase
} else if c.re < 0 && c.im < 0 {
phase = -(math.Pi - phase)
} else if c.re > 0 && c.im < 0 {
phase = -phase
} else if c.re == 0 && c.im > 0 {
phase = math.Pi / 2
} else if c.re == 0 && c.im < 0 {
phase = -math.Pi / 2
} else if c.re < 0 && c.im == 0 {
phase = math.Pi
} else if c.re > 0 && c.im == 0 {
phase = 0
} else if c.re == 0 && c.im == 0 {
phase = 0
}
if !inRadians {
phase = c.RadianToDegree(phase)
}
return phase
}
func (c *ComplexNumber) GetPolarForm(inRadians bool) (float64, float64) {
return c.GetRadius(), c.GetPhase(inRadians)
}
func (c *ComplexNumber) RadianToDegree(radian float64) float64 {
return radian * (180 / math.Pi)
}
import java.lang.Math;
public class ComplexNumber {
private double re;
private double im;
public ComplexNumber() {
this.re = 0;
this.im = 0;
}
public ComplexNumber(double re, double im) {
this.re = re;
this.im = im;
}
public ComplexNumber add(ComplexNumber addend) {
return new ComplexNumber(this.re + addend.re, this.im + addend.im);
}
public ComplexNumber subtract(ComplexNumber subtrahend) {
return new ComplexNumber(this.re - subtrahend.re, this.im - subtrahend.im);
}
public ComplexNumber multiply(ComplexNumber multiplicand) {
return new ComplexNumber(
this.re * multiplicand.re - this.im * multiplicand.im,
this.re * multiplicand.im + this.im * multiplicand.re
);
}
public ComplexNumber divide(ComplexNumber divider) {
double finalDivider = Math.pow(divider.re, 2) + Math.pow(divider.im, 2);
ComplexNumber dividerConjugate = this.conjugate(divider);
ComplexNumber finalDivident = this.multiply(dividerConjugate);
return new ComplexNumber(
finalDivident.re / finalDivider,
finalDivident.im / finalDivider
);
}
public ComplexNumber conjugate(ComplexNumber number) {
return new ComplexNumber(number.re, -1 * number.im);
}
public double getRadius() {
return Math.sqrt(Math.pow(this.re, 2) + Math.pow(this.im, 2));
}
public double getPhase(boolean inRadians) {
double phase = Math.atan(Math.abs(this.im) / Math.abs(this.re));
if (this.re < 0 && this.im > 0) {
phase = Math.PI - phase;
} else if (this.re < 0 && this.im < 0) {
phase = -(Math.PI - phase);
} else if (this.re > 0 && this.im < 0) {
phase = -phase;
} else if (this.re == 0 && this.im > 0) {
phase = Math.PI / 2;
} else if (this.re == 0 && this.im < 0) {
phase = -Math.PI / 2;
} else if (this.re < 0 && this.im == 0) {
phase = Math.PI;
} else if (this.re > 0 && this.im == 0) {
phase = 0;
} else if (this.re == 0 && this.im == 0) {
phase = 0;
}
if (!inRadians) {
phase = radianToDegree(phase);
}
return phase;
}
public double[] getPolarForm(boolean inRadians) {
double[] polarForm = new double[2];
polarForm[0] = this.getRadius();
polarForm[1] = this.getPhase(inRadians);
return polarForm;
}
private double radianToDegree(double radian) {
return radian * (180 / Math.PI);
}
}
class ComplexNumber {
constructor({ re = 0, im = 0 } = {}) {
this.re = re;
this.im = im;
}
radianToDegree(radian) {
return radian * (180 / Math.PI);
}
add(addend) {
const complexAddend = this.toComplexNumber(addend);
return new ComplexNumber({
re: this.re + complexAddend.re,
im: this.im + complexAddend.im,
});
}
subtract(subtrahend) {
const complexSubtrahend = this.toComplexNumber(subtrahend);
return new ComplexNumber({
re: this.re - complexSubtrahend.re,
im: this.im - complexSubtrahend.im,
});
}
multiply(multiplicand) {
const complexMultiplicand = this.toComplexNumber(multiplicand);
return new ComplexNumber({
re: this.re * complexMultiplicand.re - this.im * complexMultiplicand.im,
im: this.re * complexMultiplicand.im + this.im * complexMultiplicand.re,
});
}
divide(divider) {
const complexDivider = this.toComplexNumber(divider);
const dividerConjugate = this.conjugate(complexDivider);
const finalDivident = this.multiply(dividerConjugate);
const finalDivider = complexDivider.re ** 2 + complexDivider.im ** 2;
return new ComplexNumber({
re: finalDivident.re / finalDivider,
im: finalDivident.im / finalDivider,
});
}
conjugate(number) {
const complexNumber = this.toComplexNumber(number);
return new ComplexNumber({
re: complexNumber.re,
im: -1 * complexNumber.im,
});
}
getRadius() {
return Math.sqrt(this.re ** 2 + this.im ** 2);
}
getPhase(inRadians = true) {
let phase = Math.atan(Math.abs(this.im) / Math.abs(this.re));
if (this.re < 0 && this.im > 0) {
phase = Math.PI - phase;
} else if (this.re < 0 && this.im < 0) {
phase = -(Math.PI - phase);
} else if (this.re > 0 && this.im < 0) {
phase = -phase;
} else if (this.re === 0 && this.im > 0) {
phase = Math.PI / 2;
} else if (this.re === 0 && this.im < 0) {
phase = -Math.PI / 2;
} else if (this.re < 0 && this.im === 0) {
phase = Math.PI;
} else if (this.re > 0 && this.im === 0) {
phase = 0;
} else if (this.re === 0 && this.im === 0) {
phase = 0;
}
if (!inRadians) {
phase = radianToDegree(phase);
}
return phase;
}
getPolarForm(inRadians = true) {
return {
radius: this.getRadius(),
phase: this.getPhase(inRadians),
};
}
toComplexNumber(number) {
if (number instanceof ComplexNumber) {
return number;
}
return new ComplexNumber({ re: number });
}
}
import kotlin.math.*
class ComplexNumber(private var re: Double = 0.0, private var im: Double = 0.0) {
fun add(addend: ComplexNumber): ComplexNumber {
return ComplexNumber(re + addend.re, im + addend.im)
}
fun subtract(subtrahend: ComplexNumber): ComplexNumber {
return ComplexNumber(re - subtrahend.re, im - subtrahend.im)
}
fun multiply(multiplicand: ComplexNumber): ComplexNumber {
return ComplexNumber(
re * multiplicand.re - im * multiplicand.im,
re * multiplicand.im + im * multiplicand.re
)
}
fun divide(divider: ComplexNumber): ComplexNumber {
val finalDivider = divider.re.pow(2) + divider.im.pow(2)
val dividerConjugate = conjugate(divider)
val finalDividend = multiply(dividerConjugate)
return ComplexNumber(finalDividend.re / finalDivider, finalDividend.im / finalDivider)
}
fun conjugate(number: ComplexNumber): ComplexNumber {
return ComplexNumber(number.re, -1 * number.im)
}
fun getRadius(): Double {
return sqrt(re.pow(2) + im.pow(2))
}
fun getPhase(inRadians: Boolean = true): Double {
var phase = atan(abs(im) / abs(re))
if (re < 0 && im > 0) {
phase = PI - phase
} else if (re < 0 && im < 0) {
phase = -(PI - phase)
} else if (re > 0 && im < 0) {
phase = -phase
} else if (re == 0.0 && im > 0) {
phase = PI / 2
} else if (re == 0.0 && im < 0) {
phase = -PI / 2
} else if (re < 0 && im == 0.0) {
phase = PI
} else if (re > 0 && im == 0.0) {
phase = 0.0
} else if (re == 0.0 && im == 0.0) {
phase = 0.0
}
if (!inRadians) {
phase = Math.toDegrees(phase)
}
return phase
}
fun getPolarForm(inRadians: Boolean = true): Pair<Double, Double> {
return Pair(getRadius(), getPhase(inRadians))
}
}
import math
class ComplexNumber:
def __init__(self, re=0, im=0):
self.re = re
self.im = im
def add(self, addend):
return ComplexNumber(self.re + addend.re, self.im + addend.im)
def subtract(self, subtrahend):
return ComplexNumber(self.re - subtrahend.re, self.im - subtrahend.im)
def multiply(self, multiplicand):
return ComplexNumber(
self.re * multiplicand.re - self.im * multiplicand.im,
self.re * multiplicand.im + self.im * multiplicand.re
)
def divide(self, divider):
final_divider = divider.re ** 2 + divider.im ** 2
divider_conjugate = self.conjugate(divider)
final_dividend = self.multiply(divider_conjugate)
return ComplexNumber(final_dividend.re / final_divider, final_dividend.im / final_divider)
def conjugate(self, number):
return ComplexNumber(number.re, -1 * number.im)
def get_radius(self):
return math.sqrt(self.re ** 2 + self.im ** 2)
def get_phase(self, in_radians=True):
phase = math.atan(abs(self.im) / abs(self.re))
if self.re < 0 and self.im > 0:
phase = math.pi - phase
elif self.re < 0 and self.im < 0:
phase = -(math.pi - phase)
elif self.re > 0 and self.im < 0:
phase = -phase
elif self.re == 0 and self.im > 0:
phase = math.pi / 2
elif self.re == 0 and self.im < 0:
phase = -math.pi / 2
elif self.re < 0 and self.im == 0:
phase = math.pi
elif self.re > 0 and self.im == 0:
phase = 0
elif self.re == 0 and self.im == 0:
phase = 0
if not in_radians:
phase = math.degrees(phase)
return phase
def get_polar_form(self, in_radians=True):
return self.get_radius(), self.get_phase(in_radians)
use std::f64::consts::PI;
#[derive(Debug)]
struct ComplexNumber {
re: f64,
im: f64,
}
impl ComplexNumber {
fn new(re: f64, im: f64) -> ComplexNumber {
ComplexNumber { re, im }
}
fn add(&self, addend: &ComplexNumber) -> ComplexNumber {
ComplexNumber::new(self.re + addend.re, self.im + addend.im)
}
fn subtract(&self, subtrahend: &ComplexNumber) -> ComplexNumber {
ComplexNumber::new(self.re - subtrahend.re, self.im - subtrahend.im)
}
fn multiply(&self, multiplicand: &ComplexNumber) -> ComplexNumber {
ComplexNumber::new(
self.re * multiplicand.re - self.im * multiplicand.im,
self.re * multiplicand.im + self.im * multiplicand.re,
)
}
fn divide(&self, divider: &ComplexNumber) -> ComplexNumber {
let final_divider = divider.re.powf(2.0) + divider.im.powf(2.0);
let divider_conjugate = self.conjugate(divider);
let final_dividend = self.multiply(÷r_conjugate);
ComplexNumber::new(final_dividend.re / final_divider, final_dividend.im / final_divider)
}
fn conjugate(&self, number: &ComplexNumber) -> ComplexNumber {
ComplexNumber::new(number.re, -1.0 * number.im)
}
fn get_radius(&self) -> f64 {
(self.re.powf(2.0) + self.im.powf(2.0)).sqrt()
}
fn get_phase(&self, in_radians: bool) -> f64 {
let mut phase = (self.im / self.re).atan().abs();
if self.re < 0.0 && self.im > 0.0 {
phase = PI - phase;
} else if self.re < 0.0 && self.im < 0.0 {
phase = -(PI - phase);
} else if self.re > 0.0 && self.im < 0.0 {
phase = -phase;
} else if self.re == 0.0 && self.im > 0.0 {
phase = PI / 2.0;
} else if self.re == 0.0 && self.im < 0.0 {
phase = -PI / 2.0;
} else if self.re < 0.0 && self.im == 0.0 {
phase = PI;
} else if self.re > 0.0 && self.im == 0.0 {
phase = 0.0;
} else if self.re == 0.0 && self.im == 0.0 {
phase = 0.0;
}
if !in_radians {
phase = phase.to_degrees();
}
phase
}
fn get_polar_form(&self, in_radians: bool) -> (f64, f64) {
(self.get_radius(), self.get_phase(in_radians))
}
}
class ComplexNumber {
re: number;
im: number;
constructor({ re = 0, im = 0 }: { re?: number; im?: number } = {}) {
this.re = re;
this.im = im;
}
add(addend: ComplexNumber): ComplexNumber {
return new ComplexNumber({
re: this.re + addend.re,
im: this.im + addend.im,
});
}
subtract(subtrahend: ComplexNumber): ComplexNumber {
return new ComplexNumber({
re: this.re - subtrahend.re,
im: this.im - subtrahend.im,
});
}
multiply(multiplicand: ComplexNumber): ComplexNumber {
return new ComplexNumber({
re: this.re * multiplicand.re - this.im * multiplicand.im,
im: this.re * multiplicand.im + this.im * multiplicand.re,
});
}
divide(divider: ComplexNumber): ComplexNumber {
const finalDivider = divider.re ** 2 + divider.im ** 2;
const dividerConjugate = this.conjugate(divider);
const finalDividend = this.multiply(dividerConjugate);
return new ComplexNumber({
re: finalDividend.re / finalDivider,
im: finalDividend.im / finalDivider,
});
}
conjugate(number: ComplexNumber): ComplexNumber {
return new ComplexNumber({
re: number.re,
im: -1 * number.im,
});
}
getRadius(): number {
return Math.sqrt(this.re ** 2 + this.im ** 2);
}
getPhase(inRadians: boolean = true): number {
let phase = Math.atan(Math.abs(this.im) / Math.abs(this.re));
if (this.re < 0 && this.im > 0) {
phase = Math.PI - phase;
} else if (this.re < 0 && this.im < 0) {
phase = -(Math.PI - phase);
} else if (this.re > 0 && this.im < 0) {
phase = -phase;
} else if (this.re === 0 && this.im > 0) {
phase = Math.PI / 2;
} else if (this.re === 0 && this.im < 0) {
phase = -Math.PI / 2;
} else if (this.re < 0 && this.im === 0) {
phase = Math.PI;
} else if (this.re > 0 && this.im === 0) {
phase = 0;
} else if (this.re === 0 && this.im === 0) {
phase = 0;
}
if (!inRadians) {
phase = this.radianToDegree(phase);
}
return phase;
}
getPolarForm(inRadians: boolean = true): { radius: number; phase: number } {
return {
radius: this.getRadius(),
phase: this.getPhase(inRadians),
};
}
private radianToDegree(radian: number): number {
return radian * (180 / Math.PI);
}
}