Skip to content

About

Immutable complex numbers in Java: arithmetic, transcendental functions, parsing, Javadoc and JUnit 5 tests

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Complex Number – Java

An immutable ComplexNumber class in Java, written from scratch: arithmetic, powers and roots, elementary transcendental functions, tolerance-based equality, formatting in four notations and parsing from strings. It comes with full Javadoc and 43 JUnit 5 tests.

Features

  • Arithmetic: sum, difference, product, quotient, opposite, conjugate, inverse, with both complex and real operands
  • Powers and roots: integer, real and complex exponents (principal branch), all n-th roots
  • Transcendental functions: exp, principal logarithm, sin, cos, tan, sinh, cosh, tanh, asin, acos, atan
  • Four notations for construction and display: algebraic, trigonometric, exponential and 2×2 real matrix
  • Parsing of algebraic and exponential strings, including scientific notation ("1e3+2.5e-2i", "2exp(1.5i)")
  • Equality at a fixed precision, consistent with hashCode and compareTo, so the class works correctly in HashSet, TreeSet and as a map key

Design

The class keeps three levels of precision separate:

  1. Raw data – real part, imaginary part, norm and angle store the exact floating-point results; every calculation works on these values, with no implicit rounding.
  2. Equivalence classes – equals, hashCode and the classifiers (isZero, isReal, isImaginary) compare the parts rounded to 9 decimal digits, so that numbers differing only by floating-point noise are equal. Rounding never produces -0.0, so the sign of a negligible part does not matter.
  3. Representation – toString rounds to a display precision (3 digits by default) that can never exceed the internal one.

All fields are final and there is no mutable static state, so instances are thread-safe.

Usage

import io.github.sebian26.complex.ComplexFormatEnum;
import io.github.sebian26.complex.ComplexNumber;

ComplexNumber z = new ComplexNumber(3, 4);
ComplexNumber w = ComplexNumber.valueOf("1 - 2i");

z.plus(w);                                    // 4.0 + 2.0i
z.times(w);                                   // 11.0 - 2.0i
z.dividedBy(w);                               // -1.0 + 2.0i
z.getNorm();                                  // 5.0
z.toString(ComplexFormatEnum.EXP);            // 5.0exp(0.927i)
ComplexNumber.exp(new ComplexNumber(0, Math.PI));  // -1.0   (Euler's identity)
new ComplexNumber(-8).rootsOfOrder(3);        // [1.0 + 1.732i, -2.0, 1.0 - 1.732i]
ComplexNumber.I.raisedTo(ComplexNumber.I);    // 0.208   (i^i = e^(-pi/2))
new ComplexNumber(1, 1e-12).equals(ComplexNumber.ONE);  // true

z.toString(ComplexFormatEnum.MATR) gives the matrix form:

[       3.000       -4.000
        4.000        3.000]

Tests

ComplexNumberTest contains 43 JUnit 5 tests, each tied to a statement of the documentation. They cover every public method, numbers in all four quadrants and on the axes, zero, very small and very large values, and invalid input. They also check these identities on sample points:

Identity
z · z⁻¹ = 1 z · z̄ = |z|²
e^(iπ) = −1 e^(Ln z) = z
sin²z + cos²z = 1 cosh²z − sinh²z = 1
sin(asin z) = z, cos(acos z) = z, tan(atan z) = z asin z + acos z = π/2
i² = −1 product of the n-th roots of z = (−1)ⁿ⁺¹ z

Known limitations

  • raisedTo(int) loses angular precision for huge exponents: for |n| around 10⁹ the error can reach about 1e-7.
  • Equality at 9 decimal digits becomes less meaningful for magnitudes above about 10⁶, where a double no longer carries 9 decimals.
  • On the branch cut of real numbers greater than 1, asin and acos return the complex conjugate of the C99 convention.
  • intValue, longValue, floatValue and doubleValue return the norm, not the real part.

Documentation

The API documentation (Javadoc) is available online at https://sebian-26.github.io/complex-number-java/ and in the docs folder.

Project structure

src/io/github/sebian26/complex/
    ComplexNumber.java
    ComplexFormatEnum.java
    package-info.java
test/io/github/sebian26/complex/
    ComplexNumberTest.java
docs/
    Javadoc HTML pages

Requirements

  • Java 17 or later (developed with Java 25), JUnit 5 for the tests.

  • To regenerate the documentation:

    javadoc -d docs -sourcepath src io.github.sebian26.complex
    

    or, in Eclipse, Project → Generate Javadoc with docs as destination.

About

Immutable complex numbers in Java: arithmetic, transcendental functions, parsing, Javadoc and JUnit 5 tests

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages