PHP Math Library

I’ve just finished my PHP Math Library and Abstraction Layer, I’ve been up all night in order to code it, but it was worth it. I am proud of it.

My PHP Math Library features the following:

Files & Methods:

  • Math
    • Absolute
    • Add
    • Average
    • Ceil
    • Clean_Decimals
    • Compare
    • Divide
    • Factorial
    • Float
    • Floor
    • Integer
    • Is_Float
    • Is_Integer
    • Is_Negative
    • Is_Positive
    • Math
    • Modulo
    • Multiply
    • Number_Format
    • Power
    • Random
    • Root
    • Round
    • Subtract
  • Number_Theory
    • Collatz_Trajectory
    • Divisors
    • Factor
    • Fibonacci
    • GCD
    • Is_Perfect
    • Is_Prime
    • Is_Prime_Wilson
    • LCM
    • Next_Prime
    • Pascal_Triangle
    • Perfect_Square
    • Powers_Of
    • Proth_Gilbreath_Triangle
    • Ratio
    • Square_Difference_Fermat
    • Sum_Gauss
  • Combinatorics
    • Combination
    • Disposition
    • Number_Combinations
    • Number_Permutations
    • Number_Dispositions
    • Permutation
  • Probability
    • Add
    • Clear
    • Get
    • Get_Weight
    • Remove

Here is a screenshot of the Fibonacci Sequence method:

Fibonacci Sequence - PHP Math Library

This allows you, for instance, to calculate the 1000th Fibonacci number (which is 43 466 557 686 937 456 435 688 527 675 040 625 802 564 660 517 371 780 402 481 729 089 536 555 417 949 051 890 403 879 840 079 255 169 295 922 593 080 322 634 775 209 689 623 239 873 322 471 161 642 996 440 906 533 187 938 298 969 649 928 516 003 704 476 137 795 166 849 228 875) in less than half of a eye blink. ;)

I can release the source code if anyone is interested. I’ve released the source code for the “Math” and “Number_Theory” libraries, just click on the list to get it.

PS: I’m exhausted.

13 Responses to “PHP Math Library”


  1. 1 Alix

    This post was reviewed at Nexen.net. :D

  2. 2 Alix

    Reviewed again at Portail PHP.

  3. 3 Alix

    I asked Paul Meagher to review this class on phpmath.com and he kindly accepted my request, you can see his thoughts about my library at http://www.phpmath.com/home?op=perm&nid=81

    To follow his suggestions, I’ve made available the source code for the Number_Theory class (click on the list to see it), that should provide some useful working examples on the implementation of the Math class.

    I don’t have any experience with unit testing yet, and I don’t have the time to learn it right now, so don’t expect this anytime soon.

    The third suggestion was “a non-trivial example of a math solution implemented using the library”. Well, I haven’t built anything that deserves to be mentioned so far (apart from my 45th Mersenne prime forecast :P ), however, I have plans in using it to develop a scheduler for BOINC (a distributed computing system), for RSA factoring, using the Fermat’s difference of squares factorization method.

    I am looking forward to see Paul’s port of his Gaussian elimination algorithm. ;) Just to close, I want to thank Paul once again for reviewing this library.

  4. 4 Alix

    Now also on http://www.numbertheory.org/ntw/N1.html thanks to Keith Matthews! :)

  5. 5 ed

    Forgive my relative ignorance of OO, but why doesn’t this test.php work?

    Factor(20));

    $bar = new Number_Theory();
    print_r($bar->Factor(10));
    ?>

    I get this unexpected result…

    Array
    (
    [2] => 2
    [5] => 1
    )
    Array
    (
    [2] => 3
    [5] => 2
    )

    Why does the factors array persist?

  6. 6 ed

    oops, darn wordpress. my code snippet was truncated.

    require(’Number_Theory.php’);

    $foo = new Number_Theory();
    print_r($foo->Factor(20));

    $bar = new Number_Theory();
    print_r($bar->Factor(10));

  7. 7 Alix

    Hi Ed, thanks for warning me about this unexpected behavior.

    The bug you presented seems to be caused by a misuse of the static variable $factors (line 81), which is needed to store the factorization made if the script needs to re-factorize a certain number (line 116).

    The problem is that the static variable $factors maintains its state until the script is finished and that causes it to also add the factors of previous factorizations. :(

    The good news is that this is the only method where I’ve used static variables, so as soon as I solve it I’ll post here the solution and I’ll update the source code.

    Again, many thanks for your info! ;)

  8. 8 Alix

    The Factor() bug it’s solved! :) Now it returns the correct result:

    print_r($Number_Theory->Factor(20));

    Array
    (
    [2] => 2
    [5] => 1
    )

    20 = 2^2 * 5^1 (correct)

    print_r($Number_Theory->Factor(10));

    Array
    (
    [2] => 1
    [5] => 1
    )

    10 = 2^1 * 5^1 (correct)

    I had to add an extra variable to reset the factors, I couldn’t find any other way to solve the bug… The source code has been updated, you can find the changes there. ;)

  9. 9 ed

    Wow, that was quick! I had forgotten the purpose of the static keyword. Any reason that factors[] should be a class variable instead of an instance variable?

    Another nit. Math()->$bcmath_enabled and $gmp_enabled should either be public or accessible via public get and set methods (I think). Here’s what happens if they are left private.

    PHP Fatal error: Cannot access private property Math::$gmp_enabled in C:\Development\www\math
    \Number_Theory.php on line 228

    Apparently, PHP doesn’t let you check the type (the “===” operator) of private data.

  10. 10 Alix

    Regarding the Math::$bcmath_enabled and Math::$gmp_enabled I’m aware of it, it would still produce the same error if I had used == or any other comparison operator. The reason for these variables being private is that this Math library is part of a much greater library framework I’m developing which runs on a base Singleton pattern class and therefore it doesn’t produce those fatal errors. I’ve forgot to change this when I released it to the public - I’ll do it now though.

  11. 11 P L Patodia

    The value of 1000th Fibonacci number provided by you is not correct. The correct value is

    4346655768693745643568852767504062580256466051737178040248172
    9089536555417949051890403879840079255169295922593080322634775
    2096896232398733224711616429964409065331879382989696499285160
    03704476137795166849228875

    This is computed using gp-pari.

  12. 12 Alix

    Thanks P L Patodia, I’ve double checked my code and the expected results are correct, the problem here resides in the number_format() PHP function that I’ve used for the presentational purpose of this blog (it wouldn’t word wrap it otherwise) - it seems like when you tell number_format() to format a really large number the outcome result is wrong. I’ll code my own Number_Format() method to handle big numbers and I’ll post it here as soon as I finish it.

    I’ve already reported the bug to the PHP team and had to manually add thousands separators to the real 1000th Fibonacci number, thank you very much for your feedback!

  13. 13 Alix

    Ok, I got so unhappy with the number_format behavior that I’ve made my own Number_Format() method for the Math class, I hope you like it.

Leave a Reply






Close
E-mail It