Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.

Mathematical Functions #35

@dantleech

Description

@dantleech

Currently this library includes no mathematical functions. For PHPBench I needed access to basic statistic functions, e.g. average, stddev, variance, etc. and later I needed to port a Kernel Distribution estimator class, it would be great to move these over to this library.

Reference libraries / projects:

I would like to start with porting the minimum required for PHPBench:

Functions or static methods?

Should we use functions or static methods? i.e.

$mean = Stats::mean(Core::linspace(1, 10, 10));

or

$mean = Stats\mean(Core\linspace(1, 10, 10));

I tend to prefer the functional approach, but it is not possible to "autoload" functions, so we would have to include the whole library everytime.

{
    "autoload": {
        "files": [
            "lib/core.php",
            "lib/stats.php",
            "lib/financial.php",
        ]
    }
}

Perhaps the overhead would be insignificant, but maybe it would be better to use static methods anyway, thoughts?

Package Organisation

Currently the source code for this package is located in the root directory, along with some "non-code" files. Which I am not sure is a great idea, f.e. the Bin directory, which (I think) this case stands for "binary" but this also has a mathematical significance. Which could block us in the future.

Equally the Context and Arithmetic.pp files share the same concern as the Bin\Calc.php, so I would rather these were all in a sub-namespace:

Parser/ // or whatever this could be named
   Calc.php
   Arithimetic.php
   Context.php
Combinatronics/
Sampler/
Stats/
    Kde.php
    GaussianBlah.php
    GammaBlah.php
    Stats.php // file containing functions (if that is what we do)
Financial/
LinearAlgebra/
Etc../

Options or Arguments?

PHP does not currently have named parameters, which really sucks.

Taking an example from R:

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
       dimnames = NULL)

So we have the choice of either:

function matrix($data = null, $nrow = 1, $ncol = 1, $byrow = false, $dimnames = null)

or

function matrix($data, array $options = array())
{
    $options = array_merge([
        'nrow' => 1,
        'ncol' => 1,
        'byrow' => false,
        'dimnames' => null
    ], $options);
}

The first is pretty bad IMO, and the second really requires a utility to validate the options. In this case a class would probably be a better solution, but this sort of thing is quite frequent, so it would be good to have a stragtegy to deal with it.

Vectorized Operations

R and numpy support vectorized operations, e.g.

$x  = array(1, 2, 3);
$y = array(2,3,4);
$x + $y; // array(3, 5, 7)

I think it would be good to support this:

Vector::add($x, $y); // array(3, 5, 7)
Vector::multiple($x, $y);
Vector::div($x, $y); 
// etc

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions