Skip to content

etconsilium/php-complex-number

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Complex numbers

Classes that define complex numbers and their operations, based on PEAR::Math_Complex

How to use

Imperative Procedural Approach

use Math\Complex;
use Math\Complex\Operation;

$a = complex(0.3, 0.5);
$b = complex(1.0, -M_PI_2);

echo "a = " . $a->toString() . PHP_EOL;
echo "b = " . $b->toString() . PHP_EOL;
$z = Complex\sqrt($a);          //  namespace
echo "sqrt(a) = " . $z->toString() . PHP_EOL;
$z = Operation::log10($b);      //  or static call
echo "log10(a) = " . $z->toString() . PHP_EOL;

$z = Operation::add($a, $b);
echo "add(a, b) = " . $z->toString() . PHP_EOL;
$z = Complex\div($a, $b);
echo "div(a, b) = " . $z->toString() . PHP_EOL;

@see also using_example.php

Object Oriented Approach

use Math\Complex;
use Math\Complex\Operation;

$a = Complex::new(0.3, 0.5);    //  factory method contains syntactic sugar 
$b = new Complex(["real" => 1, "imaginary" => -M_PI_2]); //  constructor does not 

$op = new Operation($a);        //  operands' FILO inside the class
//  Operation::add($a, $b);
$c = $op->add($b)->fetch();     //  first Complex element shift from heap's top and return
print $c->asString() . PHP_EOL;

print $op->mul($c,$a)() . PHP_EOL;  //  php double magic: __call alias fetch() & __toString

print $op->atan(complex(1,2))
	->mul(complex(3,4))         //  can be chained
	->add(complex(5,6))
	->abs();

Enjoy!

About

Classes that define complex numbers and their operations (PEAR::Math_Complex cover ft. PHP7)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages