Developer

10 PEAR classes you need to know to code complex math in your PHP applications

PEAR has a number of ready-made packages for working with complex mathematical units. These packages provide a robust, well-tested code base, and also save you the time and effort you would otherwise spend in "rolling your own" math algorithms. This document will list ten PEAR packages of specific interest to developers working with complex math in their projects.

This article is also available as a TechRepublic download.

If you have a mathematics background, you'll already be well aware that PHP doesn't come with any special functions for working with advanced mathematical concepts — complex numbers, fractions, polynomials, statistics and matrices. However, this deficiency is more than made up by PEAR, the PHP Extension and Application Repository, which has a number of ready-made packages for working with complex mathematical units. These packages provide a robust, well-tested code base for application developers, and also save you the time and effort you would otherwise spend in "rolling your own" math algorithms.

This document will list ten PEAR packages (Table A) of specific interest to developers working with complex math in their projects. Take a look, and try them out — you never know you might find something that solves a problem you're currently facing. One warning, though: if you didn't like math class in school, what follows may bring back some unpleasant memories!

Note: You can install PEAR packages directly from the Web, by following the instructions provided.

Table A


Package Name

Description

Math_Complex

This package lets you perform basic and advanced arithmetic operations on complex numbers (numbers containing both real and imaginary parts). It includes an object-oriented interface that supports methods for trigonometric and logarithmic operations on these numbers.

Use this package when you need to perform trigonometric, hyperbolic or exponential operations on complex numbers.

Math_Fraction

This package lets you generate "fraction objects" by passing the fraction's numerator and denominator as arguments to the class. It includes methods to add, subtract, divide and multiply fractions, as well as calculate reciprocals and compare two or more fractions to find out which is greater.

Use this package when you need to work with fractional values in PHP.

Math_Derivative

A derivative is the rate of change of a given function with respect to its components, and one of the most important concepts in calculus. This package provides an object-oriented framework to find the derivative of any mathematical expression to a given level. It includes support for basic rules (addition, subtraction, division, multiplication) and a cache to speed up derivation.

Use this package when working with derivatives.

Math_Polynomial

A polynomial is a multi-component expression involving variables raised to various powers. This package provides the tools required to solve polynomial equations, including the ability to perform basic arithmetic operations on polynomials; calculate polynomial derivatives and degrees; and evaluate the polynomial for a particular value of its variables.

Use this package to simplify the solving of complex polynomial equations.

Math_Matrix

This package provides an object-oriented framework to mathematical operations involving matrices. Here, a matrix is represented by an array of arrays, and the class provides functions to add, subtract, multiply and divide these arrays; determine if the matrix is square; and calculate important matrix values like the determinant and norm.

Use this package when you need to work with matrices of numbers.

Math_Fibonacci

The Fibonacci sequence is a connected sequence of numbers first discovered by an Italian mathematician. This package provides methods to calculate one or more members of the sequence, a task often required in applications like pseudorandom number generators, visual arts and music. It can also be used to test if a particular integer belongs to the Fibonacci sequence.

Use this package when you need to calculate and manipulate Fibonacci numbers.

Math_Vector

This package provides an object-oriented framework for working with vectors. It includes support for important calculations such as calculating the distance (Cartesian, Manhattan and chessboard) between vectors; reversing, conjugating, scaling and normalizing vectors; and finding vector lengths and magnitudes.

Use this package when you need to perform vector calculations in 2D or 3D space.

Math_BinaryUtils

This package provides a framework for working with binary data, including conversions to big endian, little endian, decimal, integer and hexadecimal format; pack and unpack options; and the ability to easily add and subtract binary numbers.

Use this package when you need to perform calculations with binary data.

Math_Stats

This package provides a diverse array of utility functions to statistically analyze a data set. Some of the things you can do with it include calculating the means (standard, inter-quartile, harmonic and geometric), median and mode; finding the standard, absolute and inter-quartile deviation; calculating the coefficient of variation; finding raw moments; and calculating skewness and kurtosis of the distribution.

Use this package when you need to quickly perform basic statistical analysis on a data set.

Math_Histogram

This package provides an easy way to statistically analyze a set of numerical data by categorizing them and plotting a graphical frequency distribution. It includes the ability to define "bins", assign data points to bins, and generate a histogram distribution of the data set, either numerically or graphically in 2D or 3D space

Use this package to understand the frequency distribution of a data set.

About Mark Kaelin

Mark W. Kaelin has been writing and editing stories about the IT industry, gadgets, finance, accounting, and tech-life for more than 25 years. Most recently, he has been a regular contributor to BreakingModern.com, aNewDomain.net, and TechRepublic.

Editor's Picks