Math_Vector
[ class tree: Math_Vector ] [ index: Math_Vector ] [ all elements ]

Source for file Vector.php

Documentation is available at Vector.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Vector.php 304047 2010-10-05 00:25:33Z clockwerx $
  20. //
  21.  
  22. require_once "PEAR.php";
  23. require_once "Math/Tuple.php";
  24. require_once "Math/VectorOp.php";
  25.  
  26. /**
  27.  * General Vector class
  28.  * Originally this class was part of NumPHP (Numeric PHP package)
  29.  *
  30.  * @author    Jesus M. Castagnetto <jmcastagnetto@php.net>
  31.  * @version    1.0
  32.  * @access    public
  33.  * @package    Math_Vector
  34.  */
  35.  
  36. class Math_Vector {
  37.  
  38.     /**
  39.      * Math_Tuple object
  40.      *
  41.      * @var        object    Math_Tuple 
  42.      * @access    private
  43.      */
  44.     var $_tuple = null;
  45.  
  46.     /**
  47.      * Constructor for Math_Vector
  48.      *
  49.      * @param    optional array|Math_Tuple|Math_Vector    $data    a Math_Tuple object, a Math_Vetctor object, or an array of numeric data
  50.      * @access    public
  51.      * @return    object    Math_Vector (or PEAR_Error on error)
  52.      * @see setData()
  53.      */
  54.     function Math_Vector($data=null/*{{{*/
  55.     {
  56.         if (!is_null($data)) {
  57.             $this->setData($data);
  58.         }
  59.     }/*}}}*/
  60.  
  61.     /**
  62.      * Initializes the vector
  63.      *
  64.      * @param    array|Math_Tuple|Math_Vector   $data    a Math_Tuple object, a Math_Vetctor object, or an array of numeric data
  65.      * @access    public
  66.      * @return    boolean|PEAR_ErrorTRUE on success, a PEAR_Error otherwise
  67.      */
  68.     function setData($data/*{{{*/
  69.     {
  70.         if (is_array($data)) {
  71.             $tuple = new Math_Tuple($data);
  72.         elseif (is_object($data&& strtolower(get_class($data)) == "math_tuple"{
  73.             $tuple $data;
  74.         else if (is_object($data&& strtolower(get_class($data)) == "math_vector"{
  75.             $tuple $data->getTuple();
  76.         else {
  77.             return PEAR::raiseError('Cannot initialize, expecting an array, tuple or vector');
  78.         }
  79.         $this->_tuple $tuple;
  80.         return true;
  81.     }/*}}}*/
  82.  
  83.  
  84.     /**
  85.      * Returns an array of numbers
  86.      *
  87.      * @access public
  88.      * @return array|PEAR_Errora numeric array on success, a PEAR_Error otherwise
  89.      */
  90.     function getData()/*{{{*/
  91.     {
  92.         if ($this->isValid()) {
  93.             return $this->_tuple->getData();
  94.         else {
  95.             return PEAR::raiseError('Vector has not been initialized');
  96.         }
  97.     }/*}}}*/
  98.  
  99.     /**
  100.      * Checks if the vector has been correctly initialized
  101.      *
  102.      * @access    public
  103.      * @return    boolean 
  104.      */
  105.     function isValid(/*{{{*/
  106.     {
  107.         return (!is_null($this->_tuple&& is_object($this->_tuple&&
  108.                 strtolower(get_class($this->_tuple)) == "math_tuple");
  109.     }/*}}}*/
  110.  
  111.     /**
  112.      * Returns the square of the vector's length
  113.      *
  114.      * @access    public
  115.      * @return    float 
  116.      */
  117.     function lengthSquared(/*{{{*/
  118.     {
  119.         $n $this->size();
  120.         $sum = 0;
  121.         for ($i=0; $i $n$i++)
  122.             $sum += pow($this->_tuple->getElement($i)2);
  123.         return $sum;
  124.     }/*}}}*/
  125.  
  126.     /**
  127.      * Returns the length of the vector
  128.      *
  129.      * @access    public
  130.      * @return    float 
  131.      */
  132.     function length(/*{{{*/
  133.     {
  134.         return sqrt($this->lengthSquared());
  135.     }/*}}}*/
  136.  
  137.     /**
  138.      * Returns the magnitude of the vector. Alias of length
  139.      *
  140.      * @access    public
  141.      * @return    float 
  142.      */
  143.     function magnitude()/*{{{*/
  144.     {
  145.         return $this->length();
  146.     }/*}}}*/
  147.  
  148.     /**
  149.      * Normalizes the vector, converting it to a unit vector
  150.      *
  151.      * @access    public
  152.      * @return    void 
  153.      */
  154.     function normalize(/*{{{*/
  155.     {
  156.         $n $this->size();
  157.         $length $this->length();
  158.         for ($i=0; $i $n$i++{
  159.             $this->_tuple->setElement($i$this->_tuple->getElement($i)/$length);
  160.         }
  161.     }/*}}}*/
  162.  
  163.     /**
  164.      * returns the Math_Tuple object corresponding to the vector
  165.      *
  166.      * @access    public
  167.      * @return    object    Math_Tuple 
  168.      */
  169.     function getTuple(/*{{{*/
  170.     {
  171.         return $this->_tuple;
  172.     }/*}}}*/
  173.  
  174.     /**
  175.      * Returns the number of elements (dimensions) of the vector
  176.      *
  177.      * @access    public
  178.      * @return    float 
  179.      */
  180.     function size(/*{{{*/
  181.     {
  182.         return $this->_tuple->getSize();
  183.     }/*}}}*/
  184.  
  185.     /**
  186.      * Reverses the direction of the vector negating each element
  187.      *
  188.      * @access    public
  189.      * @return    void 
  190.      */
  191.     function reverse(/*{{{*/
  192.     {
  193.         $n $this->size();
  194.         for ($i=0; $i $n$i++)
  195.             $this->_tuple->setElement($i-1 * $this->_tuple->getElement($i));
  196.     }/*}}}*/
  197.  
  198.     /**
  199.      * Conjugates the vector. Alias of reverse.
  200.      *
  201.      * @access    public
  202.      * @return    void 
  203.      *
  204.      * @see        reverse()
  205.      */
  206.     function conjugate()/*{{{*/
  207.     {
  208.         $this->reverse();
  209.     }/*}}}*/
  210.  
  211.     /**
  212.      * Scales the vector elements by the given factor
  213.      *
  214.      * @access    public
  215.      * @param    float    $f    scaling factor
  216.      * @return    mixed    void on success, a PEAR_Error object otherwise
  217.      */
  218.     function scale($f/*{{{*/
  219.     {
  220.         if (is_numeric($f)) {
  221.             $n $this->size();
  222.             $t $this->getTuple();
  223.             for ($i=0; $i $n$i++)
  224.                 $this->set($i$this->get($i$f);
  225.         else {
  226.             return PEAR::raiseError("Requires a numeric factor and a Math_Vector object");
  227.         }
  228.     }/*}}}*/
  229.  
  230.     /**
  231.      * Sets the value of a element
  232.      *
  233.      * @access    public
  234.      * @param    integer    $i    the index of the element
  235.      * @param    numeric    $value    the value to assign to the element
  236.      * @return    mixed    true on success, a PEAR_Error object otherwise
  237.      */
  238.     function set($i$value/*{{{*/
  239.     {
  240.         $res $this->_tuple->setElement($i$value);
  241.         if (PEAR::isError($res))
  242.             return $res;
  243.         else
  244.             return true;
  245.     }/*}}}*/
  246.  
  247.     /**
  248.      * Gets the value of a element
  249.      *
  250.      * @access    public
  251.      * @param    integer    $i    the index of the element
  252.      * @return    mixed    the element value (numeric) on success, a PEAR_Error object otherwise
  253.      */
  254.     function get($i{/*{{{*/
  255.         $res $this->_tuple->getElement($i);
  256.         return $res;
  257.     }/*}}}*/
  258.  
  259.     /**
  260.      * Returns the distance to another vector
  261.      *
  262.      * @access    public
  263.      * @param    object    $vector Math_Vector object
  264.      * @param   string    $type    distance type: cartesian (default), manhattan or chessboard
  265.      * @return    float on success, a PEAR_Error object on failure
  266.      */
  267.     function distance($vector$type='cartesian')/*{{{*/
  268.     {
  269.         switch ($type{
  270.             case 'manhattan' :
  271.             case 'city' :
  272.                 return $this->manhattanDistance($vector);
  273.                 break;
  274.             case 'chessboard' :
  275.                 return $this->chessboardDistance($vector);
  276.                 break;
  277.             case 'cartesian' :
  278.             default :
  279.                 return $this->cartesianDistance($vector);
  280.         }
  281.     }/*}}}*/
  282.  
  283.     /**
  284.      * Returns the cartesian distance to another vector
  285.      *
  286.      * @access    public
  287.      * @param    object    $vector Math_Vector object
  288.      * @return    float on success, a PEAR_Error object on failure
  289.      */
  290.     function cartesianDistance($vector/*{{{*/
  291.     {
  292.         $n $this->size();
  293.         $sum = 0;
  294.         if (Math_VectorOp::isVector($vector))
  295.             if ($vector->size(== $n{
  296.                 for($i=0; $i $n$i++)
  297.                     $sum += pow(($this->_tuple->getElement($i$vector->_tuple->getElement($i))2);
  298.                 return sqrt($sum);
  299.             else {
  300.                 return PEAR::raiseError("Vector has to be of the same size");
  301.             }
  302.         else
  303.             return PEAR::raiseError("Wrong parameter type, expecting a Math_Vector object");
  304.     }/*}}}*/
  305.  
  306.     /**
  307.      * Returns the Manhattan (aka City) distance to another vector
  308.      * Definition: manhattan dist. = |x1 - x2| + |y1 - y2| + ...
  309.      *
  310.      * @access    public
  311.      * @param    object    $vector Math_Vector object
  312.      * @return    float on success, a PEAR_Error object on failure
  313.      */
  314.     function manhattanDistance($vector/*{{{*/
  315.     {
  316.         $n $this->size();
  317.         $sum = 0;
  318.         if (Math_VectorOp::isVector($vector))
  319.             if ($vector->size(== $n{
  320.                 for($i=0; $i $n$i++)
  321.                     $sum += abs($this->_tuple->getElement($i$vector->_tuple->getElement($i));
  322.                 return $sum;
  323.             else {
  324.                 return PEAR::raiseError("Vector has to be of the same size");
  325.             }
  326.         else
  327.             return PEAR::raiseError("Wrong parameter type, expecting a Math_Vector object");
  328.     }/*}}}*/
  329.  
  330.     /**
  331.      * Returns the Chessboard distance to another vector
  332.      * Definition: chessboard dist. = max(|x1 - x2|, |y1 - y2|, ...)
  333.      *
  334.      * @access    public
  335.      * @param    object    $vector Math_Vector object
  336.      * @return    float on success, a PEAR_Error object on failure
  337.      */
  338.     function chessboardDistance($vector/*{{{*/
  339.     {
  340.         $n $this->size();
  341.         $sum = 0;
  342.         if (Math_VectorOp::isVector($vector))
  343.             if ($vector->size(== $n{
  344.                 $cdist = array();
  345.                 for($i=0; $i $n$i++)
  346.                     $cdist[abs($this->_tuple->getElement($i$vector->_tuple->getElement($i));
  347.                 return max($cdist);
  348.             else {
  349.                 return PEAR::raiseError("Vector has to be of the same size");
  350.             }
  351.         else
  352.             return PEAR::raiseError("Wrong parameter type, expecting a Math_Vector object");
  353.     }/*}}}*/
  354.  
  355.     /**
  356.      * Returns a simple string representation of the vector
  357.      *
  358.      * @access    public
  359.      * @return    string 
  360.      */
  361.     function toString(/*{{{*/
  362.     {
  363.         return "Vector: < ".implode(", ",$this->_tuple->getData())." >";
  364.     }/*}}}*/
  365. }
  366.  
  367. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.