PHP code example of dwo / math

1. Go to this page and download the library: Download dwo/math 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/ */

    

dwo / math example snippets


use \Malenki\Math\Unit\Angle;

$a = new Angle(pi()/2);
var_dump($a->deg); // get degrees
var_dump($a->rad); // get radians
var_dump($a->gon); // get radians
var_dump($a->turn); // get turns

use \Malenki\Math\Unit\Angle;

$a = new Angle(34.53, Angle::TYPE_DEG);
var_dump($a->dms); // get DMS object
var_dump($a->dms->str); // get DMS string '34°31′48″'

use \Malenki\Math\Unit\Angle;

$a = new Angle(pi() / 2);
var_dump($a->isRight()); // should return TRUE

$b = new Angle(pi());
var_dump($b->isStraight()); // should return TRUE

$c = new Angle(2 * M_PI);
var_dump($c->isPerigon()); // should return TRUE

$d = new Angle(450, Angle::TYPE_DEG); //yes, ignore multiple turns :)
var_dump($d->isRight()); // should return TRUE

use \Malenki\Math\Unit\Angle;

$a = new Angle(M_PI / 3);
$b = new Angle(M_PI / 6);
var_dump($a->isComplementary($b)); // should be TRUE
var_dump($b->isComplementary($a)); // should be TRUE

$c = new Angle(M_PI / 2);
$d = new Angle(90, Angle::TYPE_DEG);
var_dump($c->isSupplementary($d)); // should be TRUE
var_dump($d->isSupplementary($c)); // should be TRUE

use \Malenki\Math\Matrix;

//instanciate
$m = new Matrix(3, 2);
//then populate
$m->populate(array(1,2,3,4,5,6));
//or
$m->addRow(array(1, 2));
$m->addRow(array(3, 4));
$m->addRow(array(5, 6));
//or
$m->addCol(array(1, 3, 5));
$m->addCol(array(2, 4, 6));

var_dump($m->cols); // amount of columns
var_dump($m->rows); // amount of rows

var_dump($m->getRow(0)); // get row having index 0
var_dump($m->getCol(1)); // get column having index 1

var_dump($m->getAll()); // Gets all data as array

// following will output that:
// 1  2
// 3  4
// 5  6
print($m);

echo $m->transpose();

use \Malenki\Math\Matrix;

$n = new Matrix(2, 3);
$n->populate(array(7, 8, 9, 10, 11, 12));

if($m->multiplyAllow($n))
{
    print($m->multiply($n));
}

use \Malenki\Math\Number\Complex;

$z = new Complex(2, -3);
$n->multiply(2);
$n->multiply($z);

try {
    echo $m->add($n);
}
catch(\Exception $e)
{
    echo $e->getMessage();
}

//or

if($m->sameSize($n))
{
    echo $m->add($n);
}
else
{
    echo "Cannot add M to N: not the same size!";
}

use \Malenki\Math\Matrix;

$m = new Matrix(2,2);
$m->populate(array(1,2,3,4));
var_dump($m->det()); // should be -2

use \Malenki\Math\Matrix;

$m = new Matrix(2,2);
$m->populate(array(1,2,3,4));
$i = $m->inverse();
echo $i;
// should be:
// -2   1
// 1.5  -0.5

use \Malenki\Math\Matrix;

$m = new Matrix(2,2);
$m->populate(array(1,2,3,4));
$c = $m->cofactor();
echo $c;

use \Malenki\Math\Number\Complex;

$z = new Complex(2, -3);

use \Malenki\Math\Number\Complex;
use \Malenki\Math\Unit\Angle;

$z = new Complex(1, pi(), Complex::TRIGONOMETRIC);
// or
$a = new Angle(M_PI);
$z = new Complex(1, $a); // 3rd argument is useless if Angle is used as second argumeent

use \Malenki\Math\Number\Complex;
use \Malenki\Math\Unit\Angle;

$z = new Complex(2, -3);
echo $z; // print "2-3i"
$zz = new Complex(1, new Angle(3 * M_PI / 2));
echo $zz; // print "cos 4.712389 + i⋅sin 4.712389"

use \Malenki\Math\Number\Complex;

$z = new Complex(1,2);
var_dump($z->real); // real part
var_dump($z->re); // real part
var_dump($z->r); // real part
var_dump($z->imaginary); // imaginary part
var_dump($z->im); //imaginary part 
var_dump($z->i); // imaginary part
var_dump($z->rho); // modulus aka norm
var_dump($z->theta); // argument (angle)

use \Malenki\Math\Number\Complex;

$z = new Complex(1,2);
$zz = new Complex(2,3);
echo $z->add($zz); // give new complex nulber
echo $z->multiply($zz); // give another complex number

use \Malenki\Math\Number\Complex;

$z = new Complex(1,2);
echo $z->conjugate();
echo $z->negative();

use \Malenki\Math\NormalDistribution;

// Normal Distribution with mean equals to 2 and has standard deviation of 0.3
$nd = new NormalDistribution(2, 0.3);

// you can get value of function:
$nd->f(3);

// you can generate fake sample following the current normal distribution:
$md->samples(100); // 100 elements into an array

use \Malenki\Math\Factorial;

$f = new Factorial(5);
$f->result; // should be 120
$f->n; // you can get rank as reminder too.

use \Malenki\Math\Factorial;

$f = new Factorial(5);
echo $f; // string '120'

use \Malenki\Math\Random;

$r = new Random(); // double form 0 to 1 only
var_dump($r->get());

$r = new Random(-5, 18); // integer range
var_dump($r->get());

use \Malenki\Math\Random;

$r = new Random(); // double form 0 to 1 only
var_dump($r->getMany(5));

$r = new Random(-5, 18); // integer range
var_dump($r->getMany(5));

use \Malenki\Math\Random;

$r = new Random(); // double form 0 to 1 only
var_dump($r->getManyWithoutReplacement(5));

$r = new Random(-5, 18); // integer range
var_dump($r->getManyWithoutReplacement(5));

use \Malenki\Math\RandomComplex;

$rc = new RandomComplex();
$rc->r(2, 6.5)->i(-2, 5)->get();

use \Malenki\Math\RandomComplex;

$rc = new RandomComplex();
$rc->rho(1, 5)->theta(M_PI / 4, M_PI /2)->getMany(10);

use \Malenki\Math\Stats\Stats;
$s = new Stats(array(1,2,4,2,6,4));

$s->merge(array(8,4,6,3)); // to add several values
$s->add(5); // to add one by one value

use \Malenki\Math\Stats\Stats;
$s = new Stats(array(1,2,4,2,6,4));
var_dump(count($s));

use \Malenki\Math\Stats\Stats;
$s = new Stats(array(1,2,4,2,6,4));

// arithmetic mean
var_dump($s->arithmeticMean());
var_dump($s->arithmetic_mean);
var_dump($s->mean);
var_dump($s->A);
var_dump($s->mu);

// harmonic mean
var_dump($s->harmonicMean());
var_dump($s->harmonic_mean);
var_dump($s->subcontrary_mean);
var_dump($s->H);

// geometric mean
var_dump($s->geometricMean());
var_dump($s->geometric_mean);
var_dump($s->G);

// root mean square aka RMS
var_dump($s->rootMeanSquare());
var_dump($s->root_mean_square);
var_dump($s->rms);
var_dump($s->quadratic_mean);
var_dump($s->Q);

use \Malenki\Math\Stats\Stats;
$s = new Stats(array(1,2,4,2,6,4));

// Variance (population)
var_dump($s->variance());
var_dump($s->var);
var_dump($s->variance);
var_dump($s->population_variance);

// Variance (sample)
var_dump($s->sampleVariance());
var_dump($s->sample_variance);
var_dump($s->s2);

// Standard deviation
var_dump($s->standardDeviation());
var_dump($s->standard_deviation);
var_dump($s->stdev);
var_dump($s->stddev);
var_dump($s->sigma);

use \Malenki\Math\Stats\Stats;
$s = new Stats(array(1,2,4,2,6,4));


var_dump($s->quartile(1));

var_dump($s->quartile(2));
//or
var_dump($s->mediane());

var_dump($s->quartile(3));

// or just by magic getters:

var_dump($s->first_quartile);
var_dump($s->second_quartile);
var_dump($s->third_quartile);
var_dump($s->last_quartile);
var_dump($s->mediane);

$s = new Stats(array(1,2,3,2,4,1,5));
var_dump($s->mode); // returned array has 2 values

$s = new Stats(array(1,3,2,4,1,5));
var_dump($s->mode); // returned array has 1 value

var_dump($s->frequency);
var_dump($s->relative_frequency);
var_dump($s->cumulative_frequency);
// or methods
var_dump($s->frequency());
var_dump($s->relativeFrequency());
var_dump($s->cumulativeFrequency());

use \Malenki\Math\Stats\Stats;
$s = new Stats(array(1,2,4,2,6,4));

var_dump($s->kurtosis);
var_dump($s->is_platykurtic);
var_dump($s->is_leptokurtic);
var_dump($s->is_mesokurtic);

// or method way:
var_dump($s->kurtosis());
var_dump($s->isPlatykurtic());
var_dump($s->isLeptokurtic());
var_dump($s->isMesokurtic());

use Malenki\Math\Stats\ParametricTest\Anova;
$a = new Anova();
$a->add(array(6, 8, 4, 5, 3, 4));
$a->add(array(8, 12, 9, 11, 6, 8));
$a->add(array(13, 9, 11, 8, 7, 12));

// degrees of freedom
echo $a->degrees_of_freedom;
echo $a->dof;
echo $a->degreesOfFreedom();
echo $a->dof();

// Within group degrees of freedom
echo $a->within_group_degrees_of_freedom;
echo $a->WithinGroupDegreesOfFreedom();
echo $a->wgdof();

echo $a->f;
//or
echo $a->f_ratio;
//or
echo $a->f();
//or
echo $a->fRatio();
// should be around 9.3

use Malenki\Math\Stats\ParametricTest\TTest;
$t = new Dependant();
$t->add(array(24, 17, 32, 14, 16, 22, 26, 19, 19, 22, 21, 25, 16, 24, 18));
$t->add(array(26, 24, 31, 17, 17, 25, 25, 24, 22, 23, 26, 28, 19, 23, 22));

// Degree Of Freedom
echo $t->dof(); // should be 14

// Sigma, the standard deviation
echo $t->sigma(); // Should be around 0.608

// The t-value
echo $t->t(); // Should be around -4.054