Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Haifengl Smile Complex Number Arithmetic

From Leeroopedia


Knowledge Sources
Domains Mathematics, Complex Arithmetic
Last Updated 2026-02-08 22:00 GMT

Overview

Complex is an immutable Java record representing a complex number with real and imaginary parts, providing arithmetic operations and transcendental functions.

Description

The Complex record in the smile.math package provides a full-featured immutable complex number type. It implements Serializable and supports:

  • Arithmetic -- addition (add), subtraction (sub), multiplication (mul), division (div), and scalar multiplication (scale)
  • Properties -- modulus (abs), phase/argument (phase), conjugate (conjugate), reciprocal (reciprocal)
  • Transcendental functions -- exp, sin, cos, tan

The class also includes an inner Complex.Array class that stores complex numbers in a packed double[] array for memory-efficient storage of large complex arrays.

Usage

Use Complex when working with complex arithmetic, such as in signal processing (FFT), control systems, or any mathematical computation involving complex numbers.

Code Reference

Source Location

Signature

public record Complex(double re, double im) implements Serializable {
    // Factory methods
    public static Complex of(double real);
    public static Complex of(double real, double imag);

    // Arithmetic operations
    public Complex add(Complex b);
    public Complex sub(Complex b);
    public Complex mul(Complex b);
    public Complex div(Complex b);
    public Complex scale(double b);

    // Properties
    public double abs();
    public double phase();
    public Complex conjugate();
    public Complex reciprocal();

    // Transcendental functions
    public Complex exp();
    public Complex sin();
    public Complex cos();
    public Complex tan();

    // Inner class for packed arrays
    public static class Array {
        public final int length;
        public Array(int length);
        public Complex get(int i);
        public void set(int i, Complex c);
        public void set(int i, double re);
        public static Array of(Complex... x);
    }
}

Import

import smile.math.Complex;

I/O Contract

Inputs

Name Type Required Description
re double Yes The real part of the complex number
im double Yes The imaginary part of the complex number
b Complex or double Varies The operand for arithmetic operations

Outputs

Name Type Description
(Complex) Complex A new Complex instance resulting from arithmetic or transcendental operations
abs double The modulus (magnitude) of the complex number
phase double The argument (angle) of the complex number in radians, between -pi and pi

Usage Examples

Basic Complex Arithmetic

Complex a = Complex.of(3.0, 4.0);  // 3 + 4i
Complex b = Complex.of(1.0, -2.0); // 1 - 2i

Complex sum = a.add(b);    // 4 + 2i
Complex diff = a.sub(b);   // 2 + 6i
Complex prod = a.mul(b);   // 11 - 2i
Complex quot = a.div(b);   // -1.0 + 2.0i
Complex scaled = a.scale(2.0); // 6 + 8i

Complex Properties

Complex z = Complex.of(3.0, 4.0);
double modulus = z.abs();       // 5.0
double angle = z.phase();      // ~0.9273 radians
Complex conj = z.conjugate();  // 3 - 4i
Complex inv = z.reciprocal();  // 0.12 - 0.16i

Transcendental Functions

Complex z = Complex.of(1.0, 1.0);
Complex expZ = z.exp();
Complex sinZ = z.sin();
Complex cosZ = z.cos();
Complex tanZ = z.tan();

Packed Array for Memory Efficiency

Complex.Array arr = new Complex.Array(1024);
arr.set(0, Complex.of(1.0, 2.0));
Complex c = arr.get(0); // 1.0 + 2.0i

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment