1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Mean, Median, Probability and Other Miscellaneous Math functions

Discussion in 'PHP' started by pein87, Aug 9, 2011.

  1. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    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



    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;
        }
    }
    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;
                }
            }
        }
    }
    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;
        }
    }
    PHP:
    // evaluates if a number is odd or not
    function isOddNum($n)
    {
        
    $odd = ($n 2) ? 0;
        return 
    $odd;
    }
    PHP:
    // evaluates if a number even not
    function isEvenNum($n)
    {
        
    $even = ($n 2) ? 1;
        return 
    $even;
    }
    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;    
            }    
        }
    }
     
  2. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Pretty nice, but you could add some comments.
     

Share This Page