PHP code example of irrevion / science

1. Go to this page and download the library: Download irrevion/science library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

irrevion / science example snippets




use irrevion\science\Helpers\Utils;
use irrevion\science\Math\Operations\Delegator;
use irrevion\science\Math\Math;
use irrevion\science\Math\Entities\{Scalar, Fraction, Imaginary, Complex};

use irrevion\science\Math\Entities\Scalar;

$n = new Scalar(5);
print("Scalar to string is {$n}\nType of n is ".($n::class)."\n");
// Outputs:
// Scalar to string is 5
// Type of n is irrevion\science\Math\Entities\Scalar

$sum = $n->add(new Scalar(3));
$diff = $n->subtract(new Scalar(2));
$prod = $n->multiply(new Scalar(10));
$quotient = $n->divide(new Scalar(3));

use irrevion\science\Helpers\Utils;
use irrevion\science\Math\Math;
use irrevion\science\Math\Operations\Delegator;
use irrevion\science\Math\Entities\{Scalar, Fraction, Imaginary, Complex};

$x = 2;
$y = 3;
$z = Math::pow($x, $y);
print "{$x}**{$y} is {$z} ( ".Delegator::getType($z)." ) \n";
// Output: 2**3 is 8 ( integer )

$x = new Scalar(25);
$y = new Scalar(2);
$z = Math::pow($x, $y);
print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n";
// Output: 25**2 is 625 ( irrevion\science\Math\Entities\Scalar )

$x = new Scalar(25);
$y = new Scalar(-2);
$z = Math::pow($x, $y);
print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n";
// Output: 25**-2 is 0.0016 ( irrevion\science\Math\Entities\Scalar )

$x = new Scalar(-1.678903);
$y = new Scalar(-2.5);
$z = Math::pow($x, $y);
print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n";
// Output: -1.678903**-2.5 is [8.3827564317097E-17 + -0.27380160345158i] ( irrevion\science\Math\Entities\Complex )
// Vanilla PHP will give you: NAN
// Python will give you: (8.382756431709652e-17-0.2738016034515765j)

$x = new Scalar(Math::E);
$y = new Imaginary(Math::PI);
$z = Math::pow($x, $y);
print "e**πi is {$z} ( ".($z::class)." ) \n";
// Output: e**πi is [-1 + 1.2246467991474E-16i] ( irrevion\science\Math\Entities\Complex )
// Python gives: (-1+1.2246467991473532e-16j)
// Expected: -1 by formulae e**πi+1=0

$x = new Scalar(125);
$y = new Fraction('1/3');
$z = Math::pow($x, $y);
print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n";
// Output: 125**1/3 is 5 ( irrevion\science\Math\Entities\Scalar )

$x = new Scalar(4);
$y = new Complex(2.5, 2);
$z = Math::pow($x, $y);
print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n";
// Output: 4**(2.5+2i) is [-29.845986458754 + 11.541970902054i] ( irrevion\science\Math\Entities\Complex )
// Python gives: (-29.845986458754275+11.541970902053793j)

$roots = (new Complex(-64))->roots(3);
print Utils::printR($roots)." \n";
// Output: [[2 + 3.4641016151378i], [-4 + 4.8985871965894E-16i], [2 + -3.4641016151378i]]