Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Mean, Median, Probability and Other Miscellaneous Math functions (http://www.go4expert.com/articles/mean-median-probability-miscellaneous-t26459/)

pein87 9Aug2011 11:44

Mean, Median, Probability and Other Miscellaneous Math functions
 
These functions allow you to get the mean, the median, tell if a number is even or odd, strip an array of all strings and characters, and provide probability. The mean function works well if your trying to implement a rate system. just filter the input values of the array and it will work perfectly.

To get the mean just input an array. All non numbers will be stripped from the array and it will be re-ordered. The result is not rounded.

To get the median input an array. Like mean non numbers will be stripped. It will return the median as either a decimal or whole number depending on the length of the numbers range.

The even and odd functions take a number and return 0 or 1 if its not or is even or odd.

The non-numeric stripper function strips all non-numeric characters from and array and re-orders them so there are no blank spaces.

The probability function takes in either an array or single number and returns a ration of the probability of that number occurring.

The Code



Code: PHP

// returns the mean of a number list
function mean($vals,$debug = 0)
{
       
    if(!is_array($vals))
    {
        return 0;
    }
    else
    {
   
        $val = arrayStripChar($vals);
        $num = count($val);
        $i = 0;
        $m = 0;
        for($i = 0; $i < $num; $i++)
        {
            $m += $val[$i];
        }
       
        $mean = $m / $num;
       
        return $mean;
    }
}

Code: PHP

// returns the median of a number list
function median($vals)
{
    if(!is_array($vals))
    {
        return 0;
    }
    else
    {
        $val = arrayStripChar($vals);
        $num = count($val);
        rsort($val);
        $midNum = ($num + 1) / 2;
        if(is_float($midNum))
        {
            $M = round($midNum);
           
            if(isEvenNum($num - $midNum))
            {
                $mn = $M - 1;
                $mm = $mn - 1;
                $median = ($val[$mn] + $val[$mm]) / 2;
                return $median;
            }
            else
            {
                $median = $val[$M -1];
                return $median;
            }
        }
        else
        {
            if(isEvenNum($num - $midNum))
            {
                $mn = $midNum - 1;
                $mm = $mn - 1;
                $median = array('a' => $val[$mn],'b' => $val[$mm]);
                return $mm;
            }
            else
            {
                $median = $val[$midNum -1];
                return $median;
            }
        }
    }
}

Code: PHP

// function strips all non numeric values from an array and returns a new array that is clipped of all strings and character indexes
function arrayStripChar($a)
{
    if(!is_array($a))
    {
        return 0;
    }
    else
    {
        $num = count($a);
        $i = 0;
        $j = 0;
        $rtn = 0;
        for ($i = 0; $i < $num; $i++)
        {
            if(!is_numeric($a[$i]))
            {
                unset($a[$i]);
            }
        }
        $n = array_values($a);
       
        return $n;
    }
}

Code: PHP

// evaluates if a number is odd or not
function isOddNum($n)
{
    $odd = ($n % 2) ? 1 : 0;
    return $odd;
}

Code: PHP

// evaluates if a number even not
function isEvenNum($n)
{
    $even = ($n % 2) ? 0 : 1;
    return $even;
}

Code: PHP

function probability($n,$nR)
{
    if(!is_array($n))
    {
        if(!is_array($nR))
        {
            $prob = $n . ":" . $nR;
           
            return $prob;
        }
        else
        {
            $val = arrayStripChar($nR);
            $num = count($val);
            $i = 0;
            $p = 0;
            for($i = 0; $i < $num; $i++)
            {
                $p += $val[$i];
            }
            $prob = $n . ":" . $p;
            return $prob;
        }
    }
    else
    {
   
        if(!is_array($nR))
        {
            $nam = arrayStripChar($n);
            $nem = count($nam);
            $k = 0;
            $p = array();
            for($k = 0; $k < $nem; $k++)
            {
                $p[$k] = $nam[$k] . ":" . $nR;
            }
            return $p;
        }
        else
        {
            $nam = arrayStripChar($n);
            $nem = count($nam);
            $k = 0;
            $val = arrayStripChar($nR);
            $num = count($val);
            $i = 0;
            $p = 0;
            $c = array();
            for($i = 0; $i < $num; $i++)
            {
                $p += $val[$i];
            }
            for($k = 0; $k < $nem; $k++)
            {
                $c[$k] = $nam[$k] . ":" . $p;
            }
            return $c
        }   
    }
}


Scripting 23Aug2011 01:32

Re: Mean, Median, Probability and Other Miscellaneous Math functions
 
Pretty nice, but you could add some comments.


All times are GMT +5.5. The time now is 21:40.