Complex Numbers in Dart

A robust, dependency-free library for working with complex numbers in Dart and Flutter. It supports Cartesian, Polar/Exponential forms, fundamental arithmetic, powers, roots, and essential transcendental functions.

Complex_Number_dart

A robust, dependency-free library for working with complex numbers in Dart and Flutter. It supports Cartesian, Polar/Exponential forms, fundamental arithmetic, powers, roots, and essential transcendental functions.

Features

The core Complex class stores a number z=a+bi and pre-calculates its key properties for efficient operations:

Property Description Range
re (_re) Real part ($a$) $\mathbb{R}$
im (_im) Imaginary part ($b$) $\mathbb{R}$
modulus (_modulus) Magnitude ($r = \z $)
theta1 (_theta1) Principal argument ($\arg(z)$) (−π,π]
theta2 (_theta2) Full argument [0,2π)

Getting started

Installation

Add complex_numbers_dart (or your chosen package name) to your pubspec.yaml dependencies:

dependencies: complex_numbers_dart: ^1.0.0 # Use the latest version

Basic Usage

Import the package and use the Complex class

import 'package:complex_numbers_dart/complex_numbers_dart.dart';
import 'dart:math' as math;

void main() {
  final z1 = Complex(3.0, 4.0); // z = 3 + 4i
  final z2 = Complex.polar(5.0, math.pi / 2); // z = 5 * e^(iπ/2) = 5i
  
  // Arithmetic operations are supported
  final sum = z1 + z2; // 3 + 9i
  print('z1 + z2 = ${sum}'); 
}

Constructors

The library provides four ways to create a Complex number:

1. Complex(double re, double im)

The default Cartesian form **z = re + im⋅i.**

- The constructor accepts mathematical expressions or functions from dart:math for its parameters:

```dart
    Complex z = Complex(math.sqrt(2)/2, 1 + math.log(math.sqrt(2)));
```

2. Complex.polar(double modulus, double theta)

Creates a complex number from its modulus (r) and its argument (θ) expressed in radians:  

    1. Exponential form:  z=r⋅e^iθ

    - Trigonometric form: z = r(cos( θ) + i sin(θ))

3. Complex.polarDegrees(double modulus, double degrees)

Same as Complex.polar, but accepts the angle (θ) expressed in *degrees*.

4. Complex.fromString(String input)

Parses a complex number from a string. Only pure Cartesian forms are allowed (expressions or functions are not permitted):
    // Examples accepted:
    final z1 = Complex.fromString("3.5-4i");
    final z2 = Complex.fromString("-3.5+i");
    final z3 = Complex.fromString("5i");
    final z4 = Complex.fromString("8");

Supported Methods

Arithmetic & Properties

Method Description Example
+, -, *. / Standard arithmetic operators. z1 * z2
== Checks for equality of real and imaginary parts. z == Complex(1, 0)
conjugate (getter) Returns the complex conjugate (zˉ). z.conjugate
opposite (getter) Returns the additive inverse (−z). z.opposite
inverse() Returns the multiplicative inverse (1/z). z.inverse()
pow(int n) Calculates z^n using De Moivre's Theorem. z.pow(6)
nthRoots(int n) Calculates and returns a list of the n-th roots of z. z.nthRoots(3)
rotate(double angleRadians) Rotates the complex number by a given angle (in radians) in the complex plane. z.rotate(math.pi/4)

Transcendental Functions

Method Description
log() Returns the natural logarithm (ln(z)).
exp() Returns the exponential (e^z ).
sin(), cos(), tan() Calculate the complex trigonometric functions.
sinh(), cosh(), tanh() Calculate the complex hyperbolic functions.

Additional information

Contribution and Issues

Contributions are welcome! If you find a bug or have a feature request, please file an issue on the GitHub Issue Tracker.

-   Bugs/Issues: [Link to Your GitHub Issues Page]

-   Contribution: Feel free to fork the repository and submit pull requests.

"# Complex_Numbers_dart"