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

Vectorizing basic arithmetic operations [Regression
Posted on January 23, 2012 @ 12:45:37 AM by Paul Meagher

Continuing on with my research into adding better matrix/vector capabilities to PHP, I decided to implement the basic arithmetic functions (addition, subtraction, division, multiplication) as vectorized functions. To make these functions concise and easy to remember, I used a system of function naming that involves prefixing the function call with a "v" (for "vector") and then using the first three letters of the arithmetic operation name (add, sub, div, mul) as the suffix:

  1. vadd($v, $addend): vector addition
  2. vsub($v, $subtrahend): vector subtaction
  3. vdiv($v, $divisor): vector division
  4. vmul($v, $factor): vector multiplication

In all cases, the first argument is a vector of numbers, followed by the value to apply to each element of the vector. The code below is proof-of-concept of how to vectorize these functions and is not meant as production-level code:

* Set of functions that vectorize the basic arithmetic functions:
* addition, subtraction, division, multiplication.  No error 
* checking.

// function for vectorized addition

function _add($addend1$addend2) {
  return (
$addend1 $addend2);

vadd($v$addend) {
$size      count($v);
$add_array array_fill(0$size$addend);

// function for vectorized subtraction

function _sub($minuend$subtrahend) {
  return (
$minuend $subtrahend);

vsub($v$subtrahend) {
$size      count($v);
$sub_array array_fill(0$size$subtrahend);

// function for vectorized multiplication

function _mul($factor1$factor2) {
  return (
$factor1 $factor2);

vmul($v$factor) {
$size      count($v);
$mul_array array_fill(0$size$factor);

// function for vectorized division

function _div($numerator$divisor) {
  return (
$numerator $divisor);

vdiv($v$divisor) {
$size      count($v);
$div_array array_fill(0$size$divisor);

$v   = array(51015);
$arg 5;

$b vadd($v$arg);
"<br />";

$b vsub($v$arg);
"<br />";

$b vmul($v$arg);
"<br />";

$b vdiv($v$arg);
"<br />";

// Output:

// Array ( [0] => 10 [1] => 15 [2] => 20 )
// Array ( [0] => 0 [1] => 5 [2] => 10 )
// Array ( [0] => 25 [1] => 50 [2] => 75 )
// Array ( [0] => 1 [1] => 2 [2] => 3 ) 


The alternative to vectorized functions is the ad-hoc creation of loops to perform similar vectorized operations in your code. Think how much programming time and effort has been wasted coding loops to do vectorized arithmetic when there could be vectorized arithmetic functions available to do it all in one simple command.

This only touches the surface of the number of mathematical functions that should be vectorized in PHP (e.g., vsin, vcos, vexp, etc...) to make PHP a much more efficient language to program numeric-based algorithms. We could easily add a "v" suffix to all these function to indicate that they are all vector-based.

Another option to consider would be to go even further and have these functions handle matrix operations, with vectors being just the special case of a one-dimensional matrix. In that case, we might want to instead prefix all the functions with an "m" instead of a "v". I had considered this, but was too lazy to implement functions to handle the matrix case. This is a good exercise for the reader.

At the end of the day we should look at languages like Matlab/Octave to give us guidance on how to proceed with respect to the range of functions to vectorize and whether we should handle vector or matrix arguments to these functions. While we may not be able to use Matlab/Octave's concise syntax to code vectorized algorithms, we can develop slightly more verbose functions like vadd, vsub, vdiv, and vmul to efficiently code numeric algorithms.


No comments entered ...


php/Math Project
© 2011. All rights reserved.