Using complex numbers

Dart has no built-in type for complex numbers, but it is easy to build your own. The complex_numbers library (based on similar libraries by Tiago de Jesus and Adam Singer) provides constructors, utility methods, and four arithmetic operations both defined as operators and static methods.

How to do it...

We now define a ComplexNumber class, containing all utility methods for normal usage:

library complex_numbers;

import 'dart:math' as math;

class ComplexNumber {
  num _real;
  num _imag;

// 1- Here we define different ways to build a complex number:
  // constructors:
     ComplexNumber([this._real = 0, this._imag = 0]);
    ComplexNumber.im(num imag) : this(0, imag);
     ComplexNumber.re(num real) : this(real, 0);

// 2- The normal utility methods to get and set the real and         // imaginary part, to get the absolute value and the angle, to      // compare two complex numbers:
  num get real => _real;
  set real(num value) => _real = value;

  num get imag => _imag;
  set imag(num value) => _imag = value;

  num get abs => math.sqrt(real * real + imag * imag);

  num get angle => math.atan2(imag, real);

  bool operator ==(other) {
    if (!(other is ComplexNumber)) {
      return false;
    }
    return this.real == other.real && this.imag == other.imag;
  }

  String toString() {
    if (_imag >= 0) {
      return '${_real} + ${_imag}i';
    }
    return '${_real} - ${_imag.abs()}i';
  }
// 3- operator overloading:
// The basic operations for adding, multiplying, subtraction and    // division are defined as overloading of the operators +, *, - and / 
  ComplexNumber operator +(ComplexNumber x) {
    return new ComplexNumber(_real + x.real, _imag + x.imag);
  }

  ComplexNumber operator -(var x) {
    if (x is ComplexNumber) {
return new ComplexNumber(this.real - x.real, this.imag - x.imag);
    } else if (x is num) {
      _real -= x;
      return this;
    }
    throw 'Not a number';
  }

  ComplexNumber operator *(var x) {
    if (x is ComplexNumber) {
      num realAux = (this.real * x.real - this.imag * x.imag);
      num imagAux = (this.imag * x.real + this.real * x.imag);

      return new ComplexNumber(realAux, imagAux);
    } else if (x is num) {
      return new ComplexNumber(this.real * x, this.imag * x);
    }
    throw 'Not a number';
  }

  ComplexNumber operator /(var x) {
    if (x is ComplexNumber) {
num realAux = (this.real * x.real + this.imag * x.imag) / (x.real * x.real + x.imag * x.imag);
num imagAux = (this.imag * x.real - this.real * x.imag) / (x.real * x.real + x.imag * x.imag);
      return new ComplexNumber(realAux, imagAux);
    } else if (x is num) {
      return new ComplexNumber(this.real / x, this.imag / x);
    }
    throw 'Not a number';
  }

// 4- Here we define the same operations as methods:
 static ComplexNumber add(ComplexNumber c1, ComplexNumber c2)     {
    num rr = c1.real + c2.real;
    num ii = c1.imag + c2.imag;
    return new ComplexNumber(rr, ii);
 }

 static ComplexNumber subtract(ComplexNumber c1, ComplexNumber c2) 
 {
    num rr = c1.real - c2.real;
    num ii = c1.imag - c2.imag;
    return new ComplexNumber(rr, ii);
 }

 static ComplexNumber multiply(ComplexNumber c1, ComplexNumber c2) 
 {
    num rr = c1.real * c2.real - c1.imag * c2.imag;
    num ii = c1.real * c2.imag + c1.imag * c2.real;
    return new ComplexNumber(rr, ii);
 }

 static ComplexNumber divide(ComplexNumber c1, ComplexNumber c2) 
 {
num real = (c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag);
num imag = (c1.imag * c2.real - c1.real * c2.imag) /  (c2.real * c2.real + c2.imag * c2.imag);
    return new ComplexNumber(real, imag);
 }
}

How it works...

The ComplexNumber class is built using standard Dart functionalities:

  • Private getters for real and imaginary parts to return their values and setters to change them
  • A constructor with two optional arguments and two named constructors for a complex number, respectively, without real or imaginary parts
  • Some utility methods such as toString() and overloading of ==
  • Operator overloading for +, -, *, and /
  • The same operations implemented as static methods taking two complex numbers

There's more...

Keep an eye on the pub package math-expressions by Frederik Leonhardt, as evaluation of expressions with complex numbers is one of its goals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset