matrix.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,490 行 · 第 1/4 页
PHP
1,490 行
<?php//// +----------------------------------------------------------------------+// | PHP version 4.0 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2001 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.02 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> |// +----------------------------------------------------------------------+// // Matrix definition and manipulation package// // $Id: Matrix.php,v 1.6 2003/04/14 11:27:52 jmcastagnetto Exp $//require_once 'PEAR.php';require_once 'Math/Vector/Vector.php';/** * Defines a matrix object, conceptualized as an array of arrays such that: * * <pre> * [0][0] [0][1] [0][2] ... [0][M]<br> * [1][0] [1][1] [1][2] ... [1][M]<br> * ...<br> * [N][0] [n][1] [n][2] ... [n][M]<br> * </pre> * * i.e. N rows, M colums * * Originally this class was part of NumPHP (Numeric PHP package) * * @author Jesus M. Castagnetto <jmcastagnetto@php.net> * @access public * @version 1.0 * @package Math_Matrix */class Math_Matrix {/*{{{*/ // Properties /*{{{*/ /** * Contains the array of arrays defining the matrix * * @access private * @var array * @see getData() */ var $_data = null; /** * The number of rows in the matrix * * @access private * @var integer * @see getSize() */ var $_num_rows = null; /** * The number of columns in the matrix * * @access private * @var integer * @see getSize() */ var $_num_cols = null; /** * The smallest value of all matrix cells * * @access private * @var float * @see getMin() * @see getMinMax() */ var $_min = null; /** * The biggest value of all matrix cells * * @access private * @var float * @see getMax() * @see getMinMax() */ var $_max = null; /** * A flag indicating if the matrix is square * i.e. if $this->_num_cols == $this->_num_rows * * @access private * @var boolean * @see isSquare() */ var $_square = false; /** * The Euclidean norm for the matrix: sqrt(sum(e[i][j]^2)) * * @access private * @var float * @see norm() */ var $_norm = null; /** * The matrix determinant * * @access private * @var float * @see determinant() */ var $_det = null; /** * Cutoff error used to test for singular or ill-conditioned matrices * * @access private * @var float * @see determinant(); * @see invert() */ var $_epsilon = 1E-18; /*}}}*/ /** * Constructor for the matrix object * * @access public * @param array $data * @return object Math_Matrix * @see $_data * @see setData() */ function Math_Matrix ($data=null) {/*{{{*/ if (!is_null($data)) $this->setData($data); }/*}}}*/ /** * Validates the data and initializes the internal variables (except for the determinant). * * The validation is performed by by checking that * each row (first dimension in the array of arrays) * contains the same number of colums (e.g. arrays of the * same size) * * @access public * @param array $data array of arrays to create a matrix object * @return mixed true on success, a PEAR_Error object otherwise */ function setData($data) {/*{{{*/ $errorObj = PEAR::raiseError('Invalid data, cannot create/modify matrix'); if (!is_array($data) || !is_array($data[0])) { return $errorObj; } // check that we got a numeric bidimensional array // and that all rows are of the same size $nc = count($data[0]); $nr = count($data); $eucnorm = 0; for ($i=0; $i < $nr; $i++) { if (count($data[$i]) != $nc) { return $errorObj; } for ($j=0; $j < $nc; $j++) { if (!is_numeric($data[$i][$j])) { return $errorObj; } $data[$i][$j] = (float) $data[$i][$j]; $tmp[] = $data[$i][$j]; $eucnorm += $data[$i][$j] * $data[$i][$j]; } } $this->_num_rows = $nr; $this->_num_cols = $nc; $this->_square = ($nr == $nc); $this->_min = min($tmp); $this->_max = max($tmp); $this->_norm = sqrt($eucnorm); $this->_data = $data; $this->_det = null; // lazy initialization ;-) return true; }/*}}}*/ /** * Returns the array of arrays. * * @access public * @return mixed an array of array of numbers on success, a PEAR_Error otherwise */ function getData () {/*{{{*/ if ($this->isEmpty()) { return PEAR::raiseError('Matrix has not been populated'); } else { return $this->_data; } }/*}}}*/ /** * Checks if the matrix has been initialized. * * @access public * @return boolean TRUE on success, FALSE otherwise */ function isEmpty() {/*{{{*/ return ( empty($this->_data) || is_null($this->_data) ); }/*}}}*/ /** * Returns an array with the number of rows and columns in the matrix * * @access public * @return mixed an array of integers on success, a PEAR_Error object otherwise */ function getSize() {/*{{{*/ if ($this->isEmpty()) return PEAR::raiseError('Matrix has not been populated'); else return array($this->_num_rows, $this->_num_cols); }/*}}}*/ /** * Checks if it is a square matrix (i.e. num rows == num cols) * * @access public * @return boolean TRUE on success, FALSE otherwise */ function isSquare () {/*{{{*/ if ($this->isEmpty()) { return PEAR::raiseError('Matrix has not been populated'); } else { return $this->_square; } }/*}}}*/ /** * Returns the Euclidean norm of the matrix. * * Euclidean norm = sqrt( sum( e[i][j]^2 ) ) * * @access public * @return mixed a number on success, a PEAR_Error otherwise */ function norm() {/*{{{*/ if (!is_null($this->_norm)) { return $this->_norm; } else { return PEAR::raiseError('Uninitialized Math_Matrix object'); } }/*}}}*/ /** * Returns a new Math_Matrix object with the same data as the current one * * @access public * @return object Math_Matrix */ function &clone() {/*{{{*/ return new Math_Matrix($this->_data); }/*}}}*/ /** * Sets the value of the element at (row,col) * * @param integer $row * @param integer $col * @param numeric $value * @access public * @return mixed TRUE on success, a PEAR_Error otherwise */ function setElement($row, $col, $value) {/*{{{*/ if ($this->isEmpty()) { return PEAR::raiseError('Matrix has not been populated'); } if ($row >= $this->_num_rows && $col >= $this->_num_cols) { return PEAR::raiseError('Incorrect row and column values'); } if (!is_numeric($value)) { return PEAR::raiseError('Incorrect value, expecting a number'); } $this->_data[$row][$col] = $value; return true; }/*}}}*/ /** * Returns the value of the element at (row,col) * * @param integer $row * @param integer $col * @access public * @return mixed a number on success, a PEAR_Error otherwise */ function getElement($row, $col) {/*{{{*/ if ($this->isEmpty()) { return PEAR::raiseError('Matrix has not been populated'); } if ($row >= $this->_num_rows && $col >= $this->_num_cols) { return PEAR::raiseError('Incorrect row and column values'); } return $this->_data[$row][$col]; }/*}}}*/ /** * Returns the row with the given index * * This method checks that matrix has been initialized and that the * row requested is not outside the range of rows. * * @param integer $row * @param optional boolean $asVector whether to return a Math_Vector or a simple array. Default = false. * @access public * @return mixed an array number on success, a PEAR_Error otherwise */ function getRow ($row, $asVector = false) {/*{{{*/ if ($this->isEmpty()) { return PEAR::raiseError('Matrix has not been populated'); } if (is_integer($row) && $row >= $this->_num_rows) { return PEAR::raiseError('Incorrect row value'); } if ($asVector) { $classes = get_declared_classes(); if (!in_array("math_vector", $classes) || !in_array("math_vectopop", $classes)) { return PEAR::raiseError ("Classes Math_Vector and Math_VectorOp undefined". " add \"require_once 'Math/Vector/Vector.php'\" to your script"); } return new Math_Vector($this->_data[$row]); } else { return $this->_data[$row]; } }/*}}}*/ /** * Sets the row with the given index to the array * * This method checks that the row is less than the size of the matrix * rows, and that the array size equals the number of columns in the * matrix. * * @param integer $row index of the row * @param array $arr array of numbers * @access public * @return mixed TRUE on success, a PEAR_Error otherwise */ function setRow ($row, $arr) {/*{{{*/ if ($this->isEmpty()) { return PEAR::raiseError('Matrix has not been populated'); } if ($row >= $this->_num_rows) { return PEAR::raiseError('Row index out of bounds'); } if (count($arr) != $this->_num_cols) { return PEAR::raiseError('Incorrect size for matrix row: expecting '.$this->_num_cols .' columns, got '.count($arr).' columns'); } for ($i=0; $i < $this->_num_cols; $i++) { if (!is_numeric($arr[$i])) { return PEAR::raiseError('Incorrect values, expecting numbers');
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?