markrogoyski / math-php

Powerful modern math library for PHP: Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra
MIT License
2.33k stars 239 forks source link

Math PHP Graphing Utility #168

Open jakobsandberg opened 8 years ago

jakobsandberg commented 8 years ago

I've been playing around with GD, and am looking into building some graphing functionality for this repo.

Before I dive deep into this, as I imagine it will be an extensive project, does anyone have any major objections to this? The description states that this is a "self-contained library in pure PHP with no external dependencies" and thus it would make sense to have our own graphing capabilities.

My idea is to provide a developer experience similar to MathPlotLib (I'll look at their documentation and try to build a similar interface). However, I also think MathPlotLib isn't the prettiest graphing utility I've used, so I'm sure we could improve on that front as well.

I'm considering these steps:

  1. Building an extremely minimial MVP line graph for math (callback) functions
  2. Building a set of customizations that can be shared among most (if not all) types of graphs
    • Graph title
    • Axis titles
    • Color/shape of lines
    • Grid options (turn off, frequency, thickness, color, etc.)
    • Etc.
  3. Build more types of graphs (histograms, pie charts, etc.)
jakobsandberg commented 8 years ago

Here is a really simple example of what I have so far plotting a modified sin function:

$function = function ($x) {
    return 100 + 100*sin(0.1*$x);
};

simpletext

markrogoyski commented 8 years ago

Wow, that would be awesome. I'm not familiar with any PHP library that can graph a mathematical function. Most of the charting libraries just take an array of points and connect those for line graphs.

R has mathematical graphing capabilities built in, so there is precedence to include this in a math library. It could also very well be a separate, standalone library. I think it would be a great addition if you choose to include it within this project.

Graphs of mathematical functions and histograms seem like great features to have.

When you start getting into other types of charts, like pie charts, etc., there are a lot of existing charting libraries that do this. You may want to check out some of the popular PHP charting libraries to see what exists, how things are done, and what they are missing in relation to what would be useful for math-related charts.

Here are some of the PHP charting libraries that are worth checking out:

One thing to consider is that GD is an extension so it may not be enabled by default, so you'd want to check for it. PHP has a way to do this: http://php.net/manual/en/function.extension-loaded.php Interestingly enough, their example is checking for the GD extension =).

jakobsandberg commented 8 years ago

Thanks for the links! And I will definitely add in a check for the GD extension.

I'm making a lot of progress, and hoping to put in a pull request for a basic version in the next few days. Here is a quick teaser for where I'm currently at:

image-1517200116

Note: this is a graph for the following polynomial function on the interval [0, 7]. Currently, the utility will find the min and max on [0, 7] and use those as the bounds for the y-axis.

$function = function ($x) {
        return $x**3 - 7*$x**2 + 10*$x;
};
jakobsandberg commented 8 years ago

Here are four plots generated to show how Cubic Spline Interpolation gets very effective at larger n. For the last case (n=100) the resulting graph is practically identical to the original function. However, the resulting output of Cubic Spline Interpolation is a polynomial, and thus we can (precisely) differentiate and integrate this function, which is something we CANNOT do with the original function f(x) = x*sin(x) in the general case.

I really want to add functionality so we can add all four of these plots/functions to the same canvas/image. That is definitely my next step. I also want to add functionality so we can instead put all four of these plots as individual graphs (as they are here) but smaller and in the same output image (rather than 4 separate images, as they are below).

function

n 5

n 10

n 100