From 8d6e55fa9a8273efa02d78de936fa6151c42fb8e Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Mon, 17 May 2004 05:57:38 +0000 Subject: [PATCH] Added missing sections. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141224 13f79535-47bb-0310-9956-ffa450edef68 --- xdocs/userguide/linear.xml | 78 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/xdocs/userguide/linear.xml b/xdocs/userguide/linear.xml index 46393e199..9491e5432 100644 --- a/xdocs/userguide/linear.xml +++ b/xdocs/userguide/linear.xml @@ -17,7 +17,7 @@ --> - + @@ -28,17 +28,87 @@

- This is yet to be written. Any contributions will be gratefully accepted! + Currently, numerical linear algebra support in commons-math is + limited to basic operations on real matrices and solving linear systems.

- This is yet to be written. Any contributions will be gratefully accepted! + The + RealMatrix interface represents a matrix with real numbers as + entries. The following basic matrix operations are supported: +

    +
  • Matrix addition, subtraction, mutiplication
  • +
  • Scalar addition and multiplication
  • +
  • Inverse and transpose
  • +
  • Determinants and singularity testing
  • +
  • LU decomposition
  • +
  • Norm and Trace
  • +
  • Operation on a vector
  • +

+

+ Example: + +// Create a real matrix with two rows and three columns +double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}}; +RealMatrix m = new RealMatrixImpl(matrixData); + +// One more with three rows, two columns +double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}}; +RealMatrix n = new RealMatixImpl(); +n.setData(matrixData2); + +// Note: both constructor and setData make +// Fresh copies of input double[][] arrays + +// Now multiply m by n +RealMatrix p = m.multiply(n); +System.out.println(p.getRowDimension()); // 2 +System.out.println(p.getRowDimension()); // 2 + +// Invert p +RealMatrix pInverse = p.inverse(); + +

- This is yet to be written. Any contributions will be gratefully accepted! + The solve() methods of the RealMatrix interface + support solving linear systems of equations. In each case, the + RealMatrix represents the coefficient matrix of the system. + For example, to solve the linear system +

+           2x + 3y - 2z = 1
+           -x + 7y + 6x = -2
+           4x - 3y - 5z = 1
+          
+ Start by creating a RealMatrix to store the coefficients + +double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}}; +RealMatrix coefficients = new RealMatrixImpl(coefficientsData); + + Next create a double[] array to represent the constant + vector and use solve(double[]) to solve the system + +double[] constants = {1, -2, 1}; +double[] solution = coefficients.solve(constants); + + The solution array will contain values for x + (solution[0]), y (solution[1]), + and z (solution[2]) that solve the system. +

+

+ If the coefficient matrix is not square or singular, an + + InvalidMatrixException is thrown. +

+

+ It is possible to solve multiple systems with the same coefficient matrix + in one method call. To do this, create a matrix whose column vectors correspond + to the constant vectors for the systems to be solved and use + solve(RealMatrix), which returns a matrix with column + vectors representing the solutions.