Пример #1
0
 /**
  * Returns the Chessboard distance to another vector
  * Definition: chessboard dist. = max(|x1 - x2|, |y1 - y2|, ...)
  *
  * @param Math_Vector $vector Math_Vector object
  *
  * @return  float on success
  * @throws InvalidArgumentException
  */
 public function chessboardDistance(Math_Vector $vector)
 {
     if (!Math_VectorOp::isVector($vector)) {
         throw new InvalidArgumentException("Wrong parameter type, expecting a Math_Vector object");
     }
     $n = $this->size();
     if ($vector->size() != $n) {
         throw new InvalidArgumentException("Vector has to be of the same size");
     }
     $sum = 0;
     $cdist = array();
     for ($i = 0; $i < $n; $i++) {
         $cdist[] = abs($this->_tuple->getElement($i) - $vector->_tuple->getElement($i));
     }
     return max($cdist);
 }
Пример #2
0
 /**
  * Solves a system of linear equations: Ax = b
  *
  * A system such as:
  * <pre>
  *     a11*x1 + a12*x2 + ... + a1n*xn = b1
  *     a21*x1 + a22*x2 + ... + a2n*xn = b2
  *     ...
  *     ak1*x1 + ak2*x2 + ... + akn*xn = bk
  * </pre>
  * can be rewritten as:
  * <pre>
  *     Ax = b
  * </pre>
  * where: 
  * - A is matrix of coefficients (aij, i=1..k, j=1..n), 
  * - b a vector of values (bi, i=1..k),
  * - x the vector of unkowns (xi, i=1..n)
  * Using: x = (Ainv)*b
  * where:
  * - Ainv is the inverse of A
  *
  * @static
  * @access public
  * @param object Math_Matrix $a the matrix of coefficients
  * @param object Math_Vector $b the vector of values
  * @return mixed a Math_Vector object on succcess, PEAR_Error otherwise
  * @see vectorMultiply()
  */
 function solve($a, $b)
 {
     // check that the vector classes are defined
     if (!Math_Matrix::isMatrix($a) && !Math_VectorOp::isVector($b)) {
         return PEAR::raiseError('Incorrect parameters, expecting a Math_Matrix and a Math_Vector');
     }
     $e = $a->invert();
     if (PEAR::isError($e)) {
         return $e;
     }
     return $a->vectorMultiply($b);
 }
Пример #3
0
 /**
  * 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 PEAR::raiseError("Wrong parameter type, expecting a Math_Vector object");
     }
 }
 /**
  * Vector division
  * v / w = <v1 / w1, v2 / w2, ..., vk / wk>
  *
  * @access	public
  * @param	object	Math_Vector (or subclass)	$v1
  * @param	object	Math_Vector (or subclass)	$v2
  * @return	object	Math_Vector (or subclass) on success, PEAR_Error otherwise
  *
  * @see 	isVector()
  */
 function divide($v1, $v2)
 {
     if (Math_VectorOp::isVector($v1) && Math_VectorOp::isVector($v2)) {
         $n = $v1->size();
         if ($v2->size() != $n) {
             return PEAR::raiseError("Vectors must of the same size");
         }
         for ($i = 0; $i < $n; $i++) {
             $d = $v2->get($i);
             if ($d == 0) {
                 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 PEAR::raiseError("V1 and V2 must be Math_Vector objects");
     }
 }
Пример #5
0
 function testFromVector()
 {
     $this->assertTrue(Math_VectorOp::isVector($this->z) && $this->z->isValid());
 }
Пример #6
0
 /**
  * Vector division
  * v / w = <v1 / w1, v2 / w2, ..., vk / wk>
  *
  * @param   object  Math_Vector (or subclass)   $v1
  * @param   object  Math_Vector (or subclass)   $v2
  * @return  object  Math_Vector (or subclass) on success
  * @throws InvalidArgumentException
  *
  * @see     isVector()
  */
 public static function divide($v1, $v2)
 {
     if (Math_VectorOp::isVector($v1) && Math_VectorOp::isVector($v2)) {
         $n = $v1->size();
         if ($v2->size() != $n) {
             throw new InvalidArgumentException("Vectors must of the same size");
         }
         for ($i = 0; $i < $n; $i++) {
             $d = $v2->get($i);
             if ($d == 0) {
                 throw new Math_Vector_Exception("Division by zero: Element {$i} in V2 is zero");
             }
             $arr[$i] = $v1->get($i) / $d;
         }
         return new Math_Vector($arr);
     } else {
         throw new InvalidArgumentException("V1 and V2 must be Math_Vector objects");
     }
 }