cvs: pear /Math_Vector CompactedTuple.php Tuple.php Vector.php VectorOp.php package.xml /Math_Vector/examples vector_creation.php

From: Date: Mon, 19 Aug 2002 19:30:23 +0000
Subject: cvs: pear /Math_Vector CompactedTuple.php Tuple.php Vector.php VectorOp.php package.xml /Math_Vector/examples vector_creation.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-5816@lists.php.net to get a copy of this message
jmcastagnetto Mon Aug 19 15:30:23 2002 EDT Modified files: /pear/Math_Vector CompactedTuple.php Tuple.php Vector.php VectorOp.php package.xml /pear/Math_Vector/examples vector_creation.php Log: More class name capitalization fixes. Using PEAR::raiseError() instead of creating a new PEAR_Error object Added new distance metrics between vectors: Manhattan (aka city) distance and Chessboard distance. Changed examples/vector_creation.php to show the use of the new distance metrics.

Index: pear/Math_Vector/CompactedTuple.php diff -u pear/Math_Vector/CompactedTuple.php:1.2 pear/Math_Vector/CompactedTuple.php:1.3 --- pear/Math_Vector/CompactedTuple.php:1.2 Wed Apr 24 19:37:19 2002 +++ pear/Math_Vector/CompactedTuple.php Mon Aug 19 15:30:22 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: CompactedTuple.php,v 1.2 2002/04/24 23:37:19 jmcastagnetto Exp $ +// $Id: CompactedTuple.php,v 1.3 2002/08/19 19:30:22 jmcastagnetto Exp $ // require_once "PEAR.php"; @@ -35,7 +35,7 @@ $msg = "Incorrect parameter for Math_CompactedTuple constructor. ". "Expecting an unidimensional array or a Math_Tuple object,". " got '$arg'\n"; - new PEAR_Error($msg, null, PEAR_ERROR_DIE); + PEAR::raiseError($msg); } return true; } @@ -67,7 +67,7 @@ unset ($this->data[$value]); return true; } - return new PEAR_Error("value does not exist in compacted tuple"); + return PEAR::raiseError("value does not exist in compacted tuple"); } function hasElement($value) { Index: pear/Math_Vector/Tuple.php diff -u pear/Math_Vector/Tuple.php:1.3 pear/Math_Vector/Tuple.php:1.4 --- pear/Math_Vector/Tuple.php:1.3 Tue Jul 30 20:57:24 2002 +++ pear/Math_Vector/Tuple.php Mon Aug 19 15:30:23 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Tuple.php,v 1.3 2002/07/31 00:57:24 jmcastagnetto Exp $ +// $Id: Tuple.php,v 1.4 2002/08/19 19:30:23 jmcastagnetto Exp $ // require_once "PEAR.php"; @@ -91,7 +91,7 @@ function setElement ($elindex, $elvalue) /*{{{*/ { if ($elindex >= $this->getSize()) { - return new PEAR_Error("Wrong index: $elindex for element: $elvalue"); + return PEAR::raiseError("Wrong index: $elindex for element: $elvalue"); } $this->data[$elindex] = $elvalue; return true; @@ -107,7 +107,7 @@ function addElement ($elvalue) /*{{{*/ { if (!is_numeric($elvalue)) { - return new PEAR_Error("Error, a numeric value is needed. You used: $elvalue"); + return PEAR::raiseError("Error, a numeric value is needed. You used: $elvalue"); } $this->data[$this->getSize()] = $elvalue; return ($this->getSize() - 1); @@ -123,7 +123,7 @@ function delElement ($elindex) /*{{{*/ { if ($elindex >= $this->getSize()) { - return new PEAR_Error("Wrong index: $elindex, element not deleted"); + return PEAR::raiseError("Wrong index: $elindex, element not deleted"); } unset($this->data[$elindex]); $this->squeezeHoles(); @@ -140,7 +140,7 @@ function getElement($elindex) /*{{{*/ { if ($elindex >= $this->getSize()) { - return new PEAR_Error("Wrong index: $elindex, Tuple size is: ".$this->getSize()); + return PEAR::raiseError("Wrong index: $elindex, Tuple size is: ".$this->getSize()); } return $this->data[$elindex]; }/*}}}*/ Index: pear/Math_Vector/Vector.php diff -u pear/Math_Vector/Vector.php:1.2 pear/Math_Vector/Vector.php:1.3 --- pear/Math_Vector/Vector.php:1.2 Tue Jul 30 20:57:24 2002 +++ pear/Math_Vector/Vector.php Mon Aug 19 15:30:23 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Vector.php,v 1.2 2002/07/31 00:57:24 jmcastagnetto Exp $ +// $Id: Vector.php,v 1.3 2002/08/19 19:30:23 jmcastagnetto Exp $ // require_once "PEAR.php"; @@ -190,7 +190,7 @@ for ($i=0; $i < $n; $i++) $this->set($i, $this->get($i) * $f); } else { - return new PEAR_Error("Requires a numeric factor and a Math_Vector object"); + return PEAR::raiseError("Requires a numeric factor and a Math_Vector object"); } }/*}}}*/ @@ -205,7 +205,7 @@ function set($i, $value) /*{{{*/ { $res = $this->tuple->setElement($i, $value); - if (Pear::isError($res)) + if (PEAR::isError($res)) return $res; else return true; @@ -227,24 +227,96 @@ * Returns the distance to another vector * * @access public - * @param object Math_Vector - * @return float + * @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')/*{{{*/ + { + switch ($type) { + case 'manhattan' : + case 'city' : + return $this->manhattanDistance($vector); + break; + case 'chessboard' : + return $this->chessboardDistance($vector); + break; + case 'cartesian' : + default : + return $this->cartesianDistance($vector); + } + }/*}}}*/ + + /** + * Returns the cartesian distance to another vector + * + * @access public + * @param object $vector Math_Vector object + * @return float on success, a PEAR_Error object on failure */ - function distance($vector)/*{{{*/ + function cartesianDistance($vector) /*{{{*/ { $n = $this->size(); $sum = 0; if (Math_VectorOp::isVector($vector)) if ($vector->size() == $n) { for($i=0; $i < $n; $i++) - $sum += pow(($this->tuple->getElement($i) - - $vector->tuple->getElement($i)), 2); + $sum += pow(($this->tuple->getElement($i) - $vector->tuple->getElement($i)), 2); return sqrt($sum); } else { - return new PEAR_Error("Vector has to be of the same size"); + return PEAR::raiseError("Vector has to be of the same size"); + } + else + 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| + ... + * + * @access public + * @param object $vector Math_Vector object + * @return float on success, a PEAR_Error object on failure + */ + function manhattanDistance($vector) /*{{{*/ + { + $n = $this->size(); + $sum = 0; + if (Math_VectorOp::isVector($vector)) + if ($vector->size() == $n) { + for($i=0; $i < $n; $i++) + $sum += abs($this->tuple->getElement($i) - $vector->tuple->getElement($i)); + return $sum; + } else { + return PEAR::raiseError("Vector has to be of the same size"); + } + else + 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|, ...) + * + * @access public + * @param object $vector Math_Vector object + * @return float on success, a PEAR_Error object on failure + */ + function chessboardDistance($vector) /*{{{*/ + { + $n = $this->size(); + $sum = 0; + if (Math_VectorOp::isVector($vector)) + if ($vector->size() == $n) { + $cdist = array(); + for($i=0; $i < $n; $i++) + $cdist[] = abs($this->tuple->getElement($i) - $vector->tuple->getElement($i)); + return max($cdist); + } else { + return PEAR::raiseError("Vector has to be of the same size"); } else - return new PEAR_Error("Wrong parameter type, expecting a Math_Vector object"); + return PEAR::raiseError("Wrong parameter type, expecting a Math_Vector object"); }/*}}}*/ /** Index: pear/Math_Vector/VectorOp.php diff -u pear/Math_Vector/VectorOp.php:1.2 pear/Math_Vector/VectorOp.php:1.3 --- pear/Math_Vector/VectorOp.php:1.2 Tue Jul 30 20:57:24 2002 +++ pear/Math_Vector/VectorOp.php Mon Aug 19 15:30:23 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: VectorOp.php,v 1.2 2002/07/31 00:57:24 jmcastagnetto Exp $ +// $Id: VectorOp.php,v 1.3 2002/08/19 19:30:23 jmcastagnetto Exp $ // /** @@ -142,7 +142,7 @@ function createBasis ($size, $index) /*{{{*/ { if ($index >= $size) - return new PEAR_Error("Incorrect index for size: $index >= $size"); + return PEAR::raiseError("Incorrect index for size: $index >= $size"); $v = Math_VectorOp::createZero($size); $res =$v->set($index, 1); if (PEAR::isError($res)) @@ -167,12 +167,12 @@ if (Math_VectorOp::isVector($v1) && Math_VectorOp::isVector($v2)) { $n = $v1->size(); if ($v2->size() != $n) - return new PEAR_Error("Vectors must of the same size"); + return PEAR::raiseError("Vectors must of the same size"); for ($i=0; $i < $n; $i++) $arr[$i] = $v1->get($i) + $v2->get($i); return new Math_Vector($arr); } else { - return new PEAR_Error("V1 and V2 must be Math_Vector objects"); + return PEAR::raiseError("V1 and V2 must be Math_Vector objects"); } }/*}}}*/ @@ -192,12 +192,12 @@ if (Math_VectorOp::isVector($v1) && Math_VectorOp::isVector($v2)) { $n = $v1->size(); if ($v2->size() != $n) - return new PEAR_Error("Vectors must of the same size"); + return PEAR::raiseError("Vectors must of the same size"); for ($i=0; $i < $n; $i++) $arr[$i] = $v1->get($i) - $v2->get($i); return new Math_Vector($arr); } else { - return new PEAR_Error("V1 and V2 must be Math_Vector objects"); + return PEAR::raiseError("V1 and V2 must be Math_Vector objects"); } }/*}}}*/ @@ -217,12 +217,12 @@ if (Math_VectorOp::isVector($v1) && Math_VectorOp::isVector($v2)) { $n = $v1->size(); if ($v2->size() != $n) - return new PEAR_Error("Vectors must of the same size"); + return PEAR::raiseError("Vectors must of the same size"); for ($i=0; $i < $n; $i++) $arr[$i] = $v1->get($i) * $v2->get($i); return new Math_Vector($arr); } else { - return new PEAR_Error("V1 and V2 must be Math_Vector objects"); + return PEAR::raiseError("V1 and V2 must be Math_Vector objects"); } }/*}}}*/ @@ -245,7 +245,7 @@ $arr[$i] = $v->get($i) * $f; return new Math_Vector($arr); } else { - return new PEAR_Error("Requires a numeric factor and a Math_Vector object"); + return PEAR::raiseError("Requires a numeric factor and a Math_Vector object"); } }/*}}}*/ @@ -265,16 +265,16 @@ if (Math_VectorOp::isVector($v1) && Math_VectorOp::isVector($v2)) { $n = $v1->size(); if ($v2->size() != $n) - return new PEAR_Error("Vectors must of the same size"); + return PEAR::raiseError("Vectors must of the same size"); for ($i=0; $i < $n; $i++) { $d = $v2->get($i); if ($d == 0) - return new PEAR_Error("Division by zero: Element $i in V2 is zero"); + return PEAR::raiseError("Division by zero: Element $i in V2 is zero"); $arr[$i] = $v1->get($i) / $d; } return new Math_Vector($arr); } else { - return new PEAR_Error("V1 and V2 must be Math_Vector objects"); + return PEAR::raiseError("V1 and V2 must be Math_Vector objects"); } }/*}}}*/ @@ -299,7 +299,7 @@ $v1->getY() * $v2->getY() + $v1->getZ() * $v2->getZ() ); else - return new PEAR_Error("Vectors must be both of the same type"); + return PEAR::raiseError("Vectors must be both of the same type"); }/*}}}*/ /** @@ -320,7 +320,7 @@ $arr[2] = $v1->getX() * $v2->getY() - $v1->getY() * $v2->getX(); return new Math_Vector3($arr); } else { - return new PEAR_Error("Vectors must be both of the same type"); + return PEAR::raiseError("Vectors must be both of the same type"); } } @@ -367,7 +367,7 @@ $v2->normalize(); return acos( Math_VectorOp::dotProduct($v1,$v2) ); } else { - return new PEAR_Error("Vectors must be both of the same type"); + return PEAR::raiseError("Vectors must be both of the same type"); } }/*}}}*/ Index: pear/Math_Vector/package.xml diff -u pear/Math_Vector/package.xml:1.3 pear/Math_Vector/package.xml:1.4 --- pear/Math_Vector/package.xml:1.3 Tue Jul 30 21:01:46 2002 +++ pear/Math_Vector/package.xml Mon Aug 19 15:30:23 2002 @@ -22,11 +22,16 @@ <release> <license>PHP</license> - <version>0.5.1</version> - <date>2002-07-30</date> + <version>0.6.0</version> + <date>2002-08-19</date> <state>beta</state> <notes> - Fixed capitalization of PEAR_Error + Some other class name capitalization fixes. + Using PEAR::raiseError() instead of creating a new PEAR_Error object + Added new distance metrics between vectors: Manhattan (aka city) distance + and Chessboard distance. + Changed examples/vector_creation.php to show the use of the new distance + metrics. </notes> <filelist> <dir name="/" baseinstalldir="Math/Vector"> Index: pear/Math_Vector/examples/vector_creation.php diff -u pear/Math_Vector/examples/vector_creation.php:1.1 pear/Math_Vector/examples/vector_creation.php:1.2 --- pear/Math_Vector/examples/vector_creation.php:1.1 Wed Apr 24 19:26:02 2002 +++ pear/Math_Vector/examples/vector_creation.php Mon Aug 19 15:30:23 2002 @@ -22,8 +22,11 @@ echo "==\nVector from tuple\n"; $w = new Math_Vector($t); -echo "Distance(v,w) = ".$v->distance($w)."\n"; -echo $w->toString()."\n"; +echo "Cartesian distance(v,w) = ".$v->distance($w)."\n"; +echo "Manhattan distance(v,w) = ".$v->distance($w, 'manhattan')."\n"; +echo "Chessboard distance(v,w) = ".$v->distance($w, 'chessboard')."\n"; +echo "Vector v: ".$v->toString()."\n"; +echo "Vector w: ".$w->toString()."\n"; echo "==\nVector from another vector\n"; $z = new Math_Vector(new Math_Vector(range(2,5)));
« previous php.pear.cvs (#5816) next »