Recreational Mathematics   
   home  |  library  |  contact
 Math Notes
 Math Programming [25]
 Regression [3]
 Data Mining [17]
 Notation [6]
 Linear Algebra [9]
 Stats & Prob [15]
 Math Cognition [5]
 Space & Physics [6]
 Formulas [5]
 Fun & Games [2]
 Haskell [1]
 Bayes Theory [1]
 Site News [0]
 Math Projects [5]
 Polynomials [1]
 Calculus [9]
 Number Theory [3]
 Optimization [2]
 Financial [1]

 Math Links
 Andrew Gelman
 Chance Wiki
 Daniel Lemire
 KD Knuggets
 Social Stats
 MySQL Performance
 Matthew Hurst
 Hal Daume III
 Math Notes >> Permanent Link

Matrix manipulation functions for PHP: Part 3 [Regression
Posted on January 14, 2012 @ 05:28:18 PM by Paul Meagher

Another useful function that matlab/octave supports is the linspace(start, end, num_points) function. The linspace function returns an array of n numbers that are equally spaced between the start and end points. A function like this is very useful when graphing an axis and when testing vectorized functions. You will see it used quite frequently in matlab/octave examples.

Below is an implementation that captures the behavior of Octave's linspace function. It also includes some code to test the output of the linspace function.



linspace($start$end$num_points) {

$format "%01.".PRECISION."f";

$start      = (float)$start;
$end        = (float)$end;  
$num_points = (int)$num_points 1;
$difference abs($end-$start);
  if (
$num_points <= 1)
    return array(
  if (
$num_points == 2
    return array(
  if (
$num_points >= 3) {

    if (
$difference 0) {

$increment sprintf($format$difference/$num_points);  
$increment $difference/$num_points;        
$A[0] = sprintf($format$start);
      for (
$i=1$i $num_points$i++) {
$A[$i] = sprintf($format, ($A[$i-1] + $increment));
$next_num $A[$i];
$A[] = sprintf($format$end);


    } else {



// Testing the output of the linspace function.

echo "<pre>";
"linspace(1, 5, 4) \n";
"linspace(1, 1.8, 4) \n";
"linspace(1, 1, 4) \n";
"linspace(5, 7, 0)\n";

The output of this script looks like this:

linspace(1, 5, 4) 
    [0] => 1.0000
    [1] => 2.3333
    [2] => 3.6666
    [3] => 5.0000

linspace(1, 1.8, 4) 
    [0] => 1.0000
    [1] => 1.2667
    [2] => 1.5334
    [3] => 1.8000

linspace(1, 1, 4) 
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 1

linspace(5, 7, 0)
    [0] => 7

An interesting aspect of the behavior of the linspace function is that it handles odd cases in ways that one might think should throw an error. Instead of throwing an error when you enter, for example, linspace(5, 7, 0) - because you can't return 0 points - it instead tries to do something sensible and returns the end point (i.e., 7). Whether this is sensible or not I will leave you to decide, however, the larger point is that Octave seems biased towards returning a numeric result of some sort rather than an error result. If you don't know the detailed behavior of these functions you could end up with some nasty surprises in terms of the result returned; perhaps it would be better if an error gets returned rather than a numeric result in some cases. Nevertheless, when doing mathematical programming you should consider whether it is better to throw an error result in response to odd inputs or try to generate a sensible numeric answer by, for example, anticipating the type of mistakes a user might make when passing arguments to a function and handling this mistakes appropriately (e.g., the low and hi values might be in reverse order from what is expected, so reverse them, and give the answer based upon the corrected ordering of arguments). A good way to master Octave is to port some of it's matrix manipulation functions to PHP. To do so you will need to observe how the Octave function behaves with boundary cases; a type of study you probably would not have engaged in otherwise.

On another note, it is my hope that I can begin to revive this blog again in 2012. I don't have time to blog as frequently as I did in the past, however, I would like to try for at least one blog post per week - morely like on weekends when I have more time for recreational math, than on weekdays. This is one of my resolutions in 2012 so stay tuned to see if this pans out :-)


No comments entered ...


php/Math Project
© 2011. All rights reserved.