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

Source for file Tuple.php

Documentation is available at Tuple.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: Tuple.php 304045 2010-10-05 00:16:53Z clockwerx $
  20. //
  21.  
  22. require_once "PEAR.php";
  23.  
  24. /**
  25.  * General Tuple class
  26.  * A Tuple represents a general unidimensional list of n numeric elements
  27.  * Originally this class was part of NumPHP (Numeric PHP package)
  28.  *
  29.  * @author    Jesus M. Castagnetto <jmcastagnetto@php.net>
  30.  * @version    1.0
  31.  * @access    public
  32.  * @package    Math_Vector
  33.  */
  34. class Math_Tuple {
  35.  
  36.     /**
  37.      * array of numeric elements
  38.      *
  39.      * @var        array 
  40.      * @access    private
  41.      */
  42.     var $data = null;
  43.     
  44.     /**
  45.      * Constructor of Math_Tuple
  46.      *
  47.      * @param    array    $data    array of numbers
  48.      * @access    public
  49.      * @return    object    Math_Tuple (or PEAR_Error on error)
  50.      */
  51.     function Math_Tuple ($data/*{{{*/
  52.     {
  53.         if (is_array($data|| !is_array($data[0])) {
  54.             $this->data $data;
  55.         else {
  56.             new PEAR_Error("An unidimensional array is needed to initialize a Tuple",
  57.                                 nullPEAR_ERROR_DIE);
  58.         }
  59.     }/*}}}*/
  60.  
  61.     /**
  62.      * Squeezes out holes in the tuple sequence
  63.      *
  64.      * @access    public
  65.      * @return    void 
  66.      */
  67.     function squeezeHoles ()/*{{{*/
  68.     {
  69.         $this->data explode(":"implode(":",$this->data));
  70.     }/*}}}*/
  71.  
  72.     /**
  73.      * Returns the size (number of elements) in the tuple
  74.      *
  75.      * @access    public
  76.      * @return    integer 
  77.      */
  78.     function getSize (/*{{{*/
  79.     {
  80.         return count($this->data);
  81.     }/*}}}*/
  82.  
  83.     /**
  84.      * Sets the value of an element
  85.      *
  86.      * @access    public
  87.      * @param    integer    $elindex    element index
  88.      * @param    numeric    $elvalue    element value
  89.      * @return    mixed    true if successful, PEAR_Error object otherwise
  90.      */
  91.     function setElement ($elindex$elvalue/*{{{*/
  92.     {
  93.         if ($elindex >= $this->getSize()) {
  94.             return PEAR::raiseError("Wrong index: $elindex for element: $elvalue");
  95.         }
  96.         $this->data[$elindex$elvalue;
  97.         return true;
  98.     }/*}}}*/
  99.  
  100.     /**
  101.      * Appends an element to the tuple
  102.      *
  103.      * @access    public
  104.      * @param    numeric    $elvalue    element value
  105.      * @return    mixed    index of appended element on success, PEAR_Error object otherwise
  106.      */
  107.     function addElement ($elvalue/*{{{*/
  108.     {
  109.         if (!is_numeric($elvalue)) {
  110.             return PEAR::raiseError("Error, a numeric value is needed. You used: $elvalue");
  111.         }
  112.         $this->data[$this->getSize()$elvalue;
  113.         return ($this->getSize(- 1);
  114.     }/*}}}*/
  115.  
  116.     /**
  117.      * Remove an element from the tuple
  118.      *
  119.      * @access public
  120.      * @param    integer $elindex    element index
  121.      * @return    mixed    true on success, PEAR_Error object otherwise
  122.      */
  123.     function delElement ($elindex/*{{{*/
  124.     {
  125.         if ($elindex >= $this->getSize()) {
  126.             return PEAR::raiseError("Wrong index: $elindex, element not deleted");
  127.         }
  128.         unset($this->data[$elindex]);
  129.         $this->squeezeHoles();
  130.         return true;
  131.     }/*}}}*/
  132.  
  133.     /**
  134.      * Returns the value of an element in the tuple
  135.      *
  136.      * @access    public
  137.      * @param    integer    $elindex    element index
  138.      * @return    mixed    numeric on success, PEAR_Error otherwise
  139.      */
  140.     function getElement($elindex/*{{{*/
  141.     {
  142.         if ($elindex >= $this->getSize()) {
  143.             return PEAR::raiseError("Wrong index: $elindex, Tuple size is: ".$this->getSize());
  144.         }
  145.         return $this->data[$elindex];
  146.     }/*}}}*/
  147.  
  148.     /**
  149.      * Returns an array with all the elements of the tuple
  150.      *
  151.      * @access    public
  152.      * @return    $array 
  153.      */
  154.     function getData (/*{{{*/
  155.     {
  156.         $this->squeezeHoles();
  157.         return $this->data;
  158.     }/*}}}*/
  159.     
  160.     /**
  161.      * Returns the minimum value of the tuple
  162.      *
  163.      * @access    public
  164.      * @return    numeric 
  165.      */
  166.     function getMin (/*{{{*/
  167.     {
  168.         return min($this->getData());
  169.     }/*}}}*/
  170.     
  171.     /**
  172.      * Returns the maximum value of the tuple
  173.      *
  174.      * @access    public
  175.      * @return    numeric 
  176.      */
  177.     function getMax (/*{{{*/
  178.     {
  179.         return max($this->getData());
  180.     }/*}}}*/
  181.  
  182.     /**
  183.      * Returns an array of the minimum and maximum values of the tuple
  184.      *
  185.      * @access    public
  186.      * @return    array of the minimum and maximum values
  187.      */
  188.     function getMinMax (/*{{{*/
  189.     {
  190.         return array ($this->getMin()$this->getMax());
  191.     }/*}}}*/
  192.     
  193.     /**
  194.      * Gets the position of the given value in the tuple
  195.      *
  196.      * @access    public
  197.      * @param    numeric    $val    value for which the index is requested
  198.      * @return    integer 
  199.      */
  200.     function getValueIndex ($val/*{{{*/
  201.     {
  202.         for ($i=0; $i $this->getSize()$i++)
  203.             if ($this->data[$i== $val)
  204.                 return $i;
  205.         return false;
  206.     }/*}}}*/
  207.  
  208.     /**
  209.      * Gets the position of the minimum value in the tuple
  210.      *
  211.      * @access    public
  212.      * @return    integer 
  213.      */
  214.     function getMinIndex (/*{{{*/
  215.     {
  216.         return $this->getValueIndex($this->getMin());
  217.     }/*}}}*/
  218.  
  219.     /**
  220.      * Gets the position of the maximum value in the tuple
  221.      *
  222.      * @access    public
  223.      * @return    integer 
  224.      */
  225.     function getMaxIndex (/*{{{*/
  226.     {
  227.         return $this->getValueIndex($this->getMax());
  228.     }/*}}}*/
  229.  
  230.     /**
  231.      * Gets an array of the positions of the minimum and maximum values in the tuple
  232.      *
  233.      * @access    public
  234.      * @return    array of integers indexes
  235.      */
  236.     function getMinMaxIndex (/*{{{*/
  237.     {
  238.         return array($this->getMinIndex()$this->getMaxIndex());
  239.     }/*}}}*/
  240.  
  241.     /**
  242.      * Checks if the tuple is a a Zero tuple
  243.      *
  244.      * @access    public
  245.      * @return    boolean 
  246.      */
  247.     function isZero (/*{{{*/
  248.     {
  249.         for ($i=0; $i $this->getSize()$i++)
  250.             if ($this->data[$i!= 0)
  251.                 return false;
  252.         return true;
  253.     }/*}}}*/
  254.     
  255.     /**
  256.      * Returns an string representation of the tuple
  257.      *
  258.      * @access    public
  259.      * @return    string 
  260.      */
  261.     function toString (/*{{{*/
  262.     {
  263.         return "{ ".implode(", ",$this->data)." }";
  264.     }/*}}}*/
  265.  
  266.     /**
  267.      * Returns an HTML representation of the tuple
  268.      *
  269.      * @access    public
  270.      * @return    string 
  271.      */
  272.     function toHTML(/*{{{*/
  273.     {
  274.         $out "<table border>\n\t<caption align=\"top\"><b>Vector</b></caption>\n";
  275.         $out .= "\t<tr align=\"center\">\n\t\t<th>i</th><th>value</th>\n\t</tr>\n";
  276.         for ($i=0; $i $this->getSize()$i++{
  277.             $out .= "\t<tr align=\"center\">\n\t\t<th>".$i."</th>";
  278.             $out .= "<td bgcolor=\"#dddddd\">".$this->data[$i]."</td>\n\t</tr>\n";
  279.         }
  280.         return $out."\n</table>\n";
  281.     }/*}}}*/
  282.     
  283. /* end of Tuple class */
  284.  
  285.  
  286. ?>

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