利用Math::Matrix做矩阵运算

返回
use Math::Matrix; $a = new Math::Matrix ([rand,rand,rand], [rand,rand,rand], [rand,rand,rand]); $a->print("A\n"); $b = $a->invert; $b->print("B\n"); $c= $a->multiply($b); $c->print("C\n"); ------------------------------------------ A 0.45645 0.44534 0.40323 0.32181 0.99142 0.79977 0.14343 0.64578 0.52075 B -0.06863 10.08344 -15.43308 -18.71506 63.66993 -83.29323 23.22738 -81.73415 109.46270 C 1.00000 0.00000 0.00000 -0.00000 1.00000 0.00000 0.00000 0.00000 1.00000
NAME Math::Matrix - Multiply and invert Matrices SYNOPSIS use Math::Matrix; DESCRIPTION The following methods are available: new Constructor arguments are a list of references to arrays of the same length. The arrays are copied. The method returns undef in case of error. $a = new Math::Matrix ([rand,rand,rand], [rand,rand,rand], [rand,rand,rand]); If you call "new" as method, a zero filled matrix with identical deminsions is returned. clone You can clone a matrix by calling: $b = $a->clone; size You can determine the dimensions of a matrix by calling: ($m, $n) = $a->size; concat Concatenates two matrices of same row count. The result is a new matrix or undef in case of error. $b = new Math::Matrix ([rand],[rand],[rand]); $c = $a->concat($b); transpose Returns the transposed matrix. This is the matrix where colums and rows of the argument matrix are swaped. multiply Multiplies two matrices where the length of the rows in the first matrix is the same as the length of the columns in the second matrix. Returns the product or undef in case of error. solve Solves a equation system given by the matrix. The number of colums must be greater than the number of rows. If variables are dependent from each other, the second and all further of the dependent coefficients are 0. This means the method can handle such systems. The method returns a matrix containing the solutions in its columns or undef in case of error. invert Invert a Matrix using "solve". multiply_scalar Multiplies a matrix and a scalar resulting in a matrix of the same dimensions with each element scaled with the scalar. $a->multiply_scalar(2); scale matrix by factor 2 add Add two matrices of the same dimensions. substract Shorthand for "add($other->negative)" equal Decide if two matrices are equal. The criterion is, that each pair of elements differs less than $Math::Matrix::eps. slice Extract columns: a->slice(1,3,5); determinant Compute the determinant of a matrix. dot_product Compute the dot product of two vectors. absolute Compute the absolute value of a vector. normalizing Normalize a vector. cross_product Compute the cross-product of vectors. print Prints the matrix on STDOUT. If the method has additional parameters, these are printed before the matrix is printed. pinvert Compute the pseudo-inverse of the matrix: ((A'A)^-1)A' EXAMPLE use Math::Matrix; srand(time); $a = new Math::Matrix ([rand,rand,rand], [rand,rand,rand], [rand,rand,rand]); $x = new Math::Matrix ([rand,rand,rand]); $a->print("A\n"); $E = $a->concat($x->transpose); $E->print("Equation system\n"); $s = $E->solve; $s->print("Solutions s\n"); $a->multiply($s)->print("A*s\n"); AUTHOR Ulrich Pfeifer <pfeifer@ls6.informatik.uni-dortmund.de> Brian J. Watson <bjbrew@power.net> Matthew Brett <matthew.brett@mrc-cbu.cam.ac.uk>
ppm> s math Searching in Active Repositories 1. Bundle-Math-Approx [1.00] Bundle related to interpolation and appr~ 2. Bundle-Math-Approx [1.00] Bundle-Math-Approx 3. Bundle-Math-Base [1.00] Bundle related to calculations between d~ 4. Bundle-Math-Base [1.00] Bundle-Math-Base 5. Chart-Math-Axis [0.3] An algorithm to find good values for cha~ 6. Chart-Math-Axis [0.3] Chart-Math-Axis 7. Image-Math-Constrain [1.01] Scaling math used in image size constrai~ 8. Image-Math-Constrain [1.01] Image-Math-Constrain 9. Math [0.5412] Perl class to represent colors 10. Math [0.5412] Math 11. Math-Algebra-Symbols [1.21] Symbolic Algebra in Pure Perl 12. Math-Algebra-Symbols [1.21] Math-Algebra-Symbols 13. Math-Amoeba [0.04] Multidimensional Function Minimisation 14. Math-Amoeba [0.04] Math-Amoeba 15. Math-Approx [0.220] 16. Math-Approx [0.220] Math-Approx 17. Math-Approx-Symbolic [0.100] Symbolic representation of interpolated ~ 18. Math-Approx-Symbolic [0.100] Math-Approx-Symbolic 19. Math-Base36 [0.05] Encoding and decoding of base36 strings 20. Math-Base36 [0.05] Math-Base36 21. Math-Base85 [0.2] Perl extension for base 85 numbers, as r~ 22. Math-Base85 [0.2] Math-Base85 23. Math-BaseArith [1.00] Perl extension for mixed-base number rep~ 24. Math-BaseArith [1.00] Math-BaseArith 25. Math-BaseCalc [1.011] Convert numbers between various bases 26. Math-BaseCalc [1.011] Math-BaseCalc 27. Math-BaseCnv [1.2.59I~ fast functions to convert between number~ 28. Math-BaseCnv [1.2.59I~ Math-BaseCnv 29. Math-Bezier [0.01] solution of Bezier curves 30. Math-Bezier [0.01] Math-Bezier 31. Math-Bezier-Convert [0.02] Convert cubic and quadratic bezier each ~ 32. Math-Bezier-Convert [0.02] Math-Bezier-Convert 33. Math-Big [1.09] routines (cos,sin,primes,hailstone,euler~ 34. Math-Big [1.09] Math-Big 35. Math-BigApprox [0.00100~ Fast and small way to closely approximat~ 36. Math-BigApprox [0.00100~ Math-BigApprox 37. Math-BigInt [1.64] Arbitrary size integer math package 38. Math-BigInt [1.64] Math-BigInt 39. Math-BigInt-BitVect [1.11] Use Bit::Vector for Math::BigInt routines 40. Math-BigInt-BitVect [1.11] Math-BigInt-BitVect 41. Math-BigInt-Constant [1.04] Arbitrary sized constant integers 42. Math-BigInt-Constant [1.04] Math-BigInt-Constant 43. Math-BigInt-Named [0.03] Math::BigInts that know their name in so~ 44. Math-BigInt-Named [0.03] Math-BigInt-Named 45. Math-BigInt-Random [0.03] Create random very large integers 46. Math-BigInt-Random [0.03] Math-BigInt-Random 47. Math-BigIntFast [6.0] Efficient big integer arithmetic (in C) 48. Math-BigIntFast [6] Math-BigIntFast 49. Math-BigRat [0.09] arbitrarily big rationales 50. Math-BigRat [0.09] Math-BigRat 51. Math-BigSimple [1.1a] 52. Math-BigSimple [1.1a] Math-BigSimple 53. Math-BooleanEval [1.00] Boolean expression parser 54. Math-BooleanEval [1.00] Math-BooleanEval 55. Math-Brent [0.01] Single Dimensional Function Minimisation 56. Math-Brent [0.01] Math-Brent 57. Math-Business-EMA [1.08] Perl extension for calculating EMAs 58. Math-Business-EMA [1.08] Math-Business-EMA 59. Math-Business-MACD [1.10] Perl extension for calculating MACDs 60. Math-Business-MACD [1.10] Math-Business-MACD 61. Math-Business-SMA [0.99] Perl extension for calculating simple mo~ 62. Math-Business-SMA [0.99] Math-Business-SMA 63. Math-Calc-Euro [0.02] convert between EUR and the old currenci~ 64. Math-Calc-Euro [0.02] Math-Calc-Euro 65. Math-Calc-Units [1.06] Human-readable unit-aware calculator 66. Math-Calc-Units [1.06] Math-Calc-Units 67. Math-Calculator [1.021] a multi-stack calculator class 68. Math-Calculator [1.021] Math-Calculator 69. Math-Calculus-Diffe~ [0.3] Algebraic Differentiation Engine 70. Math-Calculus-Diffe~ [0.3] Math-Calculus-Differentiate 71. Math-Calculus-Expre~ [0.1] Algebraic Calculus Tools Expression Class 72. Math-Calculus-Expre~ [0.1] Math-Calculus-Expression 73. Math-Calculus-Newto~ [0.1] Algebraic Newton Raphson Implementation 74. Math-Calculus-Newto~ [0.1] Math-Calculus-NewtonRaphson 75. Math-CatmullRom [0.00] Calculate Catmull-Rom splines 76. Math-CatmullRom [0.00] Math-CatmullRom 77. Math-Cephes [0.44] Perl interface to the math cephes library 78. Math-Cephes [0.44] Math-Cephes 79. Math-Combinatorics [0.09] Perform combinations and permutations on~ 80. Math-Combinatorics [0.09] Math-Combinatorics 81. Math-Complex [1.52] complex numbers and associated mathemati~ 82. Math-Complex [1.52] Math-Complex 83. Math-ConvexHull [1.02] Calculate convex hulls using Graham's sc~ 84. Math-ConvexHull [1.02] Math-ConvexHull 85. Math-Counting [0.0704] Combinatorial counting operations 86. Math-Counting [0.0704] Math-Counting 87. Math-Curve-Hilbert [0.04] Perl Implementation of Hilberts space fi~ 88. Math-Curve-Hilbert [0.04] Math-Curve-Hilbert 89. Math-Derivative [0.01] Numeric 1st and 2nd order differentiation 90. Math-Derivative [0.01] Math-Derivative 91. Math-ErrorPropagati~ [0.01] Computes the error of a function of stat~ 92. Math-ErrorPropagati~ [0.01] Math-ErrorPropagation 93. Math-ES [0.08] Evolution Strategy Optimizer 94. Math-ES [0.08] Math-ES 95. Math-Evol [1.06] Evolution Search Algorithm 96. Math-Evol [1.06] Math-Evol 97. Math-Expression [1.18] Evaluate arithmetic/string expressions 98. Math-Expression [1.18] Math-Expression 99. Math-Expression-Eva~ [0.0.4] parses and evaluates mathematic expressi~ 100. Math-Expression-Eva~ [0.4] Math-Expression-Evaluator 101. Math-Factor [0.28] Factorise numbers and calculate matching~ 102. Math-Factor [0.28] Math-Factor 103. Math-FFT [1.28] Perl module to calculate Fast Fourier Tr~ 104. Math-FFT [1.28] Math-FFT 105. Math-Fibonacci [1.5] Fibonacci numbers. 106. Math-Fibonacci [1.5] Math-Fibonacci 107. Math-Fibonacci-Phi [0.02] Perl extension for calculating Phi and p~ 108. Math-Fibonacci-Phi [0.02] Math-Fibonacci-Phi 109. Math-Financial [0.76] Calculates figures relating to loans and~ 110. Math-Financial [0.76] Math-Financial 111. Math-FitRect [0.02] Resize one rect in to another while pres~ 112. Math-FitRect [0.02] Math-FitRect 113. Math-FixedPrecision [0.21] Decimal Math without Floating Point Erro~ 114. Math-FixedPrecision [0.21] Math-FixedPrecision 115. Math-Fleximal [0.06] Integers with flexible representations. 116. Math-Fleximal [0.06] Math-Fleximal 117. Math-Fortran [0.01] Perl implimentations of Fortrans sign an~ 118. Math-Fortran [0.01] Math-Fortran 119. Math-Fractal-Curve [1.03] Generate fractal curves 120. Math-Fractal-Curve [1.03] Math-Fractal-Curve 121. Math-Fractal-Mandel~ [0.04] Calculate points in the mandelbrot fract~ 122. Math-Fractal-Mandel~ [0.04] Math-Fractal-Mandelbrot 123. Math-FractalCurve [1.00] Generate fractal curves 124. Math-FractalCurve [1.00] Math-FractalCurve 125. Math-FresnelZone [0.03] Perl extension for calculating the Fresn~ 126. Math-FresnelZone [0.03] Math-FresnelZone 127. Math-Function-Roots [0.03] Functions for finding roots of arbitrary~ 128. Math-Function-Roots [0.03] Math-Function-Roots 129. Math-Geometry [0.04] Geometry related functions 130. Math-Geometry [0.04] Math-Geometry 131. Math-Geometry-GPC [1.03] General Polygon Clipping library wrapper 132. Math-Geometry-GPC [1.03] Math-Geometry-GPC 133. Math-Geometry-Planar [1.10] Planar geometry package 134. Math-Geometry-Planar [1.10] Math-Geometry-Planar 135. Math-Geometry-Plana~ [1.04] General Polygon Clipping library wrapper 136. Math-Geometry-Plana~ [1.04] Math-Geometry-Planar-GPC 137. Math-Geometry-Plana~ [0.05] OO wrapper to gpc library 138. Math-Geometry-Plana~ [0.05] Math-Geometry-Planar-GPC-Polygon 139. Math-Geometry-Plana~ [1.02] Polygon offset package 140. Math-Geometry-Plana~ [1.02] Math-Geometry-Planar-Offset 141. Math-Geometry_2D [1.00] 2D geometry package 142. Math-Geometry_2D [1.00] Math-Geometry_2D 143. Math-GMatrix [0.2] Extension of Math::Matrix for (2D graphi~ 144. Math-GMatrix [0.2] Math-GMatrix 145. Math-Gradient [0.04] Perl extension for calculating gradients~ 146. Math-Gradient [0.04] Math-Gradient 147. Math-Group-Thompson [0.96] OO methods that calculates the cardinali~ 148. Math-Group-Thompson [0.96] Math-Group-Thompson 149. Math-HashSum [0.02] Sum a list of key-value pairs on a per-k~ 150. Math-HashSum [0.02] Math-HashSum 151. Math-Integral-Rombe~ [0_02.0] scalar numerical integration 152. Math-Integral-Rombe~ [0_02] Math-Integral-Romberg 153. Math-Interpolate [1.05] Interpolate the value Y from X using a l~ 154. Math-Interpolate [1.05] Math-Interpolate 155. Math-Interpolator [0.001] interpolate between lazily-evaluated poi~ 156. Math-Interpolator [0.001] Math-Interpolator 157. Math-Intersection-S~ [0.04] Calculate intersection point for two lin~ 158. Math-Intersection-S~ [0.04] Math-Intersection-StraightLine 159. Math-LinearCombinat~ [0.03] sum of variables with a numerical coeffi~ 160. Math-LinearCombinat~ [0.03] Math-LinearCombination 161. Math-Logic [1.19] 2, 3 and multi-value logic; with propaga~ 162. Math-Logic [1.19] Math-Logic 163. Math-Logic-Predicate [0.03] Query a database in predicate calculus f~ 164. Math-Logic-Predicate [0.03] Math-Logic-Predicate 165. Math-LogRand [0.04] Perl extension to return a random number~ 166. Math-LogRand [0.04] Math-LogRand 167. Math-MagicSquare [2.00] Magic Square Checker 168. Math-MagicSquare [2.00] Math-MagicSquare 169. Math-MagicSquare-Ge~ [0.01] Magic Square Generator 170. Math-MagicSquare-Ge~ [0.01] Math-MagicSquare-Generator 171. Math-Matrix [0.5] Multiply and invert Matrices 172. Math-Matrix [0.5] Math-Matrix 173. Math-MatrixBool [5.7] Matrix of Booleans 174. Math-MatrixBool [5.7] Math-MatrixBool 175. Math-MatrixReal [2.02] Manipulate Matrices of Real Numbers 176. Math-MatrixReal [2.02] Math-MatrixReal 177. Math-MatrixReal-Ext~ [07.0] Minor extensions to Math::MatrixReal 178. Math-MatrixReal-Ext~ [07] Math-MatrixReal-Ext1-0 179. Math-MatrixSparse [0.01] Perl extension for sparse matrices. 180. Math-MatrixSparse [0.01] Math-MatrixSparse 181. Math-MVPoly [0.8b] A simple (subset) of an algebraic Ideal. 182. Math-MVPoly [0.8b] Math-MVPoly 183. Math-NoCarry [1.11] Perl extension for no carry arithmetic 184. Math-NoCarry [1.11] Math-NoCarry 185. Math-NumberCruncher [5.00] Collection of useful math-related functi~ 186. Math-NumberCruncher [5.00] Math-NumberCruncher 187. Math-ODE [0.04] Solve N-th Order Ordinary Differential E~ 188. Math-ODE [0.04] Math-ODE 189. Math-Orthonormalize [1.00] Gram-Schmidt Orthonormalization of vecto~ 190. Math-Orthonormalize [1.00] Math-Orthonormalize 191. Math-Pari [2.01070~ Perl interface to PARI. 192. Math-Pari [2.01070~ Math-Pari ppm> install 171 Package 171: ==================== Install 'Math-Matrix' version 0.5 in ActivePerl 5.8.3.809. ==================== Downloaded 5231 bytes. Extracting 5/5: blib/arch/auto/Math/Matrix/.exists Installing C:\Perl\html\site\lib\Math\Matrix.html Installing C:\Perl\site\lib\Math\Matrix.pm Successfully installed Math-Matrix version 0.5 in ActivePerl 5.8.3.809. ppm>
sub est { # 最小二乘法计算 my ($pref) = @_; my ( %t,$n ); $t{f1} = $$pref{f1}; @{ $t{v1} } = @{ $$pref{est} }; # 形成v电压矩阵 $t{vT} = new Math::Matrix ([$t{v1}[0]]); for $n ( 1 .. 11 ) { $t{v2} = new Math::Matrix ([$t{v1}[$n]]); $t{vT} = $t{vT}->concat($t{v2}); } # $t{vT}->print("vT\n"); $t{v} = $t{vT}->transpose; $t{v}->print("v\n"); # 形成A系数矩阵 $t{AT} = new Math::Matrix ([1], [0]); for $n ( 1 .. 11 ) { $t{T1} = $n*$$pref{Tstep}; $t{wt1} = 2*$$pref{pi}*$t{f1}*$t{T1}; $t{coswt1} = cos($t{wt1}); $t{sinwt1} = sin($t{wt1}); $t{A1} = new Math::Matrix ([$t{coswt1}], [$t{sinwt1}]); $t{AT} = $t{AT}->concat($t{A1}); } $t{A} = $t{AT}->transpose; $t{A}->print("A\n"); # 计算P系数矩阵 $t{ATA} = $t{AT}->multiply($t{A}); $t{ATv} = $t{AT}->multiply($t{v}); $t{ATv}->print("ATv\n"); # 计算电压估计值矩阵(最后一个是当前值) $t{Av} = $t{ATA}->concat($t{ATv}); $t{Av}->print("Av\n"); $t{P} = $t{Av}->solve; $t{P}->print("P\n"); $t{P1} = $t{P}->[0]->[0]; $t{P2} = $t{P}->[1]->[0]; # 计算电压振幅(注意时间步长要用当前值) $t{V1} = 0; $t{Tstep} = 1/$t{f1}/12; for $n ( 0 .. 11 ) { $t{T1} = $n*$t{Tstep}; $t{wt1} = 2*$$pref{pi}*$t{f1}*$t{T1}; $t{coswt1} = cos($t{wt1}); $t{sinwt1} = sin($t{wt1}); $t{v31} = $t{P1}*$t{coswt1} + $t{P2}*$t{sinwt1}; $$pref{v1new} = $t{v31}; $t{V1} = $t{V1} + $t{v31}**2; push(@{ $t{v3} },$t{v31}); } $$pref{V1new} = sqrt($t{V1}/6); print "f1new=$t{f1}(Hz)\n"; print "V1new=$$pref{V1new}(V)\n"; exit; return($pref); } 计算结果 v 26.45034 7.03955 -13.90576 -31.81981 -42.79754 -44.44598 -36.40576 -20.42957 -0.00000 20.42957 36.40576 44.44598 A 1.00000 0.00000 0.89101 0.45399 0.58779 0.80902 0.15643 0.98769 -0.30902 0.95106 -0.70711 0.70711 -0.95106 0.30902 -0.98769 -0.15643 -0.80902 -0.58779 -0.45399 -0.89101 0.00000 -1.00000 0.45399 -0.89101 ATv 129.92973 -213.87751 Av 5.70611 0.57680 129.92973 0.57680 6.29389 -213.87751 P 26.45034 -36.40576 f1new=45(Hz) V1new=45(V)
返回