Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help



Complex Number Operations

Topic: advanced.complex_numbers

Work with complex numbers in MathHook including imaginary unit i, complex arithmetic, polar form, Euler's formula, and operations like conjugate, magnitude, and argument.

Mathematical Definition

Complex number: where and

Polar form:

where and

Complex Number Operations

MathHook provides comprehensive support for complex number arithmetic, conversions between rectangular and polar forms, and complex functions.

Creating Complex Numbers

#![allow(unused)]
fn main() {
use mathhook::Expression;

// Imaginary unit
let i = Expression::i();

// Complex number: 3 + 4i
let z = expr!(3 + 4*i);

// Pure imaginary: 5i
let w = expr!(5*i);
}

Operations

Addition/Subtraction

Component-wise: (a + bi) ± (c + di) = (a ± c) + (b ± d)i

Multiplication

(a + bi)(c + di) = (ac - bd) + (ad + bc)i

Division

Division by conjugate multiplication

Conjugate

conj(a + bi) = a - bi

Magnitude

|a + bi| = √(a² + b²)

Argument

arg(a + bi) = arctan(b/a)

Examples

Basic Complex Arithmetic

Rust
#![allow(unused)]
fn main() {
let i = Expression::i();
let z1 = expr!(3 + 4*i);
let z2 = expr!(1 - 2*i);

let sum = expr!(z1 + z2);      // 4 + 2i
let product = expr!(z1 * z2);  // 11 - 2i

}
Python
i = expr('I')
z1 = expr('3 + 4*I')
z2 = expr('1 - 2*I')

sum_z = z1 + z2       # 4 + 2*I
product = z1 * z2     # 11 - 2*I

JavaScript
const i = expr('I');
const z1 = expr('3 + 4*I');
const z2 = expr('1 - 2*I');

const sum = z1.add(z2);      // 4 + 2*I
const product = z1.mul(z2);  // 11 - 2*I

Euler's Formula

Rust
#![allow(unused)]
fn main() {
let theta = symbol!(theta);
let euler = expr!(exp(i * theta));

// Expands to: cos(theta) + i*sin(theta)
let expanded = euler.expand();

}
Python
theta = symbol('theta')
euler = exp(I * theta)

# Expands to: cos(theta) + I*sin(theta)
expanded = expand(euler)

JavaScript
const theta = symbol('theta');
const euler = exp(mul(I, theta));

// Expands to: cos(theta) + I*sin(theta)
const expanded = euler.expand();

Polar Form Conversion

Rust
#![allow(unused)]
fn main() {
let z = expr!(3 + 4*i);

let magnitude = expr!(abs(z));  // 5
let angle = expr!(arg(z));      // arctan(4/3)

// Polar form: r*exp(i*theta)
let polar = expr!(magnitude * exp(i * angle));

}
Python
z = expr('3 + 4*I')

magnitude = abs(z)  # 5
angle = arg(z)      # atan(4/3)

# Polar form
polar = magnitude * exp(I * angle)

JavaScript
const z = expr('3 + 4*I');

const magnitude = abs(z);  // 5
const angle = arg(z);      // atan(4/3)

// Polar form
const polar = magnitude.mul(exp(I.mul(angle)));

Performance

Time Complexity: O(1)

API Reference

  • Rust: mathhook_core::complex
  • Python: mathhook.complex
  • JavaScript: mathhook.complex

See Also