Source for file Vector.php
Documentation is available at Vector.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> |
// +----------------------------------------------------------------------+
// $Id: Vector.php 304047 2010-10-05 00:25:33Z clockwerx $
require_once "Math/Tuple.php";
require_once "Math/VectorOp.php";
* Originally this class was part of NumPHP (Numeric PHP package)
* @author Jesus M. Castagnetto <jmcastagnetto@php.net>
* Constructor for Math_Vector
* @param optional array|Math_Tuple|Math_Vector $data a Math_Tuple object, a Math_Vetctor object, or an array of numeric data
* @return object Math_Vector (or PEAR_Error on error)
* @param array|Math_Tuple|Math_Vector $data a Math_Tuple object, a Math_Vetctor object, or an array of numeric data
* @return boolean|PEAR_ErrorTRUE on success, a PEAR_Error otherwise
$tuple = $data->getTuple ();
return PEAR ::raiseError ('Cannot initialize, expecting an array, tuple or vector');
* Returns an array of numbers
* @return array|PEAR_Errora numeric array on success, a PEAR_Error otherwise
return PEAR ::raiseError ('Vector has not been initialized');
* Checks if the vector has been correctly initialized
* Returns the square of the vector's length
for ($i=0; $i < $n; $i++ )
* Returns the length of the vector
* Returns the magnitude of the vector. Alias of length
* Normalizes the vector, converting it to a unit vector
for ($i=0; $i < $n; $i++ ) {
* returns the Math_Tuple object corresponding to the vector
* @return object Math_Tuple
* Returns the number of elements (dimensions) of the vector
* Reverses the direction of the vector negating each element
for ($i=0; $i < $n; $i++ )
* Conjugates the vector. Alias of reverse.
* Scales the vector elements by the given factor
* @param float $f scaling factor
* @return mixed void on success, a PEAR_Error object otherwise
function scale($f) /*{{{*/
for ($i=0; $i < $n; $i++ )
$this->set($i, $this->get($i) * $f);
return PEAR ::raiseError ("Requires a numeric factor and a Math_Vector object");
* Sets the value of a element
* @param integer $i the index of the element
* @param numeric $value the value to assign to the element
* @return mixed true on success, a PEAR_Error object otherwise
function set($i, $value) /*{{{*/
* Gets the value of a element
* @param integer $i the index of the element
* @return mixed the element value (numeric) on success, a PEAR_Error object otherwise
function get($i) {/*{{{*/
* Returns the distance to another vector
* @param object $vector Math_Vector object
* @param string $type distance type: cartesian (default), manhattan or chessboard
* @return float on success, a PEAR_Error object on failure
function distance($vector, $type= 'cartesian')/*{{{*/
* Returns the cartesian distance to another vector
* @param object $vector Math_Vector object
* @return float on success, a PEAR_Error object on failure
if ($vector->size () == $n) {
$sum += pow(($this->_tuple->getElement($i) - $vector->_tuple ->getElement ($i)), 2 );
return PEAR ::raiseError ("Vector has to be of the same size");
return PEAR ::raiseError ("Wrong parameter type, expecting a Math_Vector object");
* Returns the Manhattan (aka City) distance to another vector
* Definition: manhattan dist. = |x1 - x2| + |y1 - y2| + ...
* @param object $vector Math_Vector object
* @return float on success, a PEAR_Error object on failure
if ($vector->size () == $n) {
$sum += abs($this->_tuple->getElement($i) - $vector->_tuple ->getElement ($i));
return PEAR ::raiseError ("Vector has to be of the same size");
return PEAR ::raiseError ("Wrong parameter type, expecting a Math_Vector object");
* Returns the Chessboard distance to another vector
* Definition: chessboard dist. = max(|x1 - x2|, |y1 - y2|, ...)
* @param object $vector Math_Vector object
* @return float on success, a PEAR_Error object on failure
if ($vector->size () == $n) {
$cdist[] = abs($this->_tuple->getElement($i) - $vector->_tuple ->getElement ($i));
return PEAR ::raiseError ("Vector has to be of the same size");
return PEAR ::raiseError ("Wrong parameter type, expecting a Math_Vector object");
* Returns a simple string representation of the vector
Documentation generated on Mon, 11 Mar 2019 15:39:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|