~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/site/xdoc/userguide/linear.xml

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-03-15 20:20:21 UTC
  • Revision ID: james.westby@ubuntu.com-20090315202021-zto3nmvqgcf3ami4
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0"?>
 
2
 
 
3
<!--
 
4
   Licensed to the Apache Software Foundation (ASF) under one or more
 
5
  contributor license agreements.  See the NOTICE file distributed with
 
6
  this work for additional information regarding copyright ownership.
 
7
  The ASF licenses this file to You under the Apache License, Version 2.0
 
8
  (the "License"); you may not use this file except in compliance with
 
9
  the License.  You may obtain a copy of the License at
 
10
 
 
11
       http://www.apache.org/licenses/LICENSE-2.0
 
12
 
 
13
   Unless required by applicable law or agreed to in writing, software
 
14
   distributed under the License is distributed on an "AS IS" BASIS,
 
15
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
16
   See the License for the specific language governing permissions and
 
17
   limitations under the License.
 
18
  -->
 
19
  
 
20
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
 
21
<!-- $Revision: 619641 $ $Date: 2008-02-07 14:17:35 -0700 (Thu, 07 Feb 2008) $ -->
 
22
<document url="linear.html">
 
23
 
 
24
  <properties>
 
25
    <title>The Commons Math User Guide - Linear Algebra</title>
 
26
  </properties>
 
27
 
 
28
  <body>
 
29
    <section name="3 Linear Algebra">
 
30
      <subsection name="3.1 Overview" href="overview">
 
31
        <p>
 
32
           Currently, numerical linear algebra support in commons-math is 
 
33
           limited to basic operations on real matrices and solving linear systems.
 
34
        </p>
 
35
      </subsection>
 
36
      <subsection name="3.2 Real matrices" href="real_matrices">
 
37
        <p>
 
38
          The <a href="../apidocs/org/apache/commons/math/linear/RealMatrix.html">
 
39
          RealMatrix</a> interface represents a matrix with real numbers as 
 
40
          entries.  The following basic matrix operations are supported:
 
41
          <ul>
 
42
          <li>Matrix addition, subtraction, mutiplication</li>
 
43
          <li>Scalar addition and multiplication</li>
 
44
          <li>Inverse and transpose</li>
 
45
          <li>Determinants and singularity testing</li>
 
46
          <li>LU decomposition</li>
 
47
          <li>Norm and Trace</li>
 
48
          <li>Operation on a vector</li>
 
49
          </ul>   
 
50
        </p>
 
51
        <p>
 
52
         Example:
 
53
         <source>
 
54
// Create a real matrix with two rows and three columns
 
55
double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
 
56
RealMatrix m = new RealMatrixImpl(matrixData);
 
57
 
 
58
// One more with three rows, two columns
 
59
double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
 
60
RealMatrix n = new RealMatrixImpl(matrixData2);
 
61
 
 
62
// Note: The constructor copies  the input double[][] array.
 
63
 
 
64
// Now multiply m by n
 
65
RealMatrix p = m.multiply(n);
 
66
System.out.println(p.getRowDimension()); // 2
 
67
System.out.println(p.getRowDimension()); // 2
 
68
 
 
69
// Invert p
 
70
RealMatrix pInverse = p.inverse();
 
71
         </source>
 
72
        </p>         
 
73
      </subsection>
 
74
      <subsection name="3.3 Solving linear systems" href="solve">
 
75
        <p>
 
76
          The <code>solve()</code> methods of the <code>RealMatrix</code> interface
 
77
          support solving linear systems of equations.  In each case, the 
 
78
          <code>RealMatrix</code> represents the coefficient matrix of the system.
 
79
          For example, to solve the linear system
 
80
          <pre>
 
81
           2x + 3y - 2z = 1
 
82
           -x + 7y + 6x = -2
 
83
           4x - 3y - 5z = 1
 
84
          </pre>
 
85
          Start by creating a RealMatrix to store the coefficients
 
86
          <source>
 
87
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
 
88
RealMatrix coefficients = new RealMatrixImpl(coefficientsData);
 
89
          </source>
 
90
          Next create a <code>double[]</code> array to represent the constant
 
91
          vector and use <code>solve(double[])</code> to solve the system
 
92
          <source>
 
93
double[] constants = {1, -2, 1};
 
94
double[] solution = coefficients.solve(constants);
 
95
          </source>
 
96
          The <code>solution</code> array will contain values for x 
 
97
          (<code>solution[0]</code>), y (<code>solution[1]</code>), 
 
98
          and z (<code>solution[2]</code>) that solve the system.
 
99
        </p>
 
100
        <p>
 
101
          If the coefficient matrix is not square or singular, an 
 
102
          <a href="../apidocs/org/apache/commons/math/linear/InvalidMatrixException.html">
 
103
          InvalidMatrixException</a> is thrown.
 
104
        </p>
 
105
        <p>
 
106
          It is possible to solve multiple systems with the same coefficient matrix 
 
107
          in one method call.  To do this, create a matrix whose column vectors correspond 
 
108
          to the constant vectors for the systems to be solved and use 
 
109
          <code>solve(RealMatrix),</code> which returns a matrix with column
 
110
          vectors representing the solutions.
 
111
        </p>
 
112
      </subsection>
 
113
    </section>
 
114
  </body>
 
115
</document>