6
$\begingroup$

Is there a simpler way of performing Gaussian Elimination other than using RowReduce? Such as a single built in function?

Edit:
Look at the example from our simulation class. Not too difficult, but using this method to solve problems of the sort is new to most of us. We are solving for P# of course.

Example

Also, to those asking... While I can see what is going on below, I really don't understand what it all means. Asking why not RowReduce? I guess not everyone is at that level of use yet, and I don't like just cutting and pasting internet code without understanding it. I simply wondered if there was a function that would do what the code did, but be built in.

  GaussianElimination[m_?MatrixQ, v_?VectorQ] :=
    Last /@ RowReduce[Flatten /@ Transpose[{m, v}]]
$\endgroup$
7
  • 5
    $\begingroup$ What's wrong with RowReduce? How is it not satisfactory and what do you mean by "simpler" solution? $\endgroup$
    – rm -rf
    Feb 20, 2012 at 7:36
  • 1
    $\begingroup$ Do you mean LinearSolve? $\endgroup$
    – user21
    Feb 20, 2012 at 8:31
  • 2
    $\begingroup$ If you just need to solve an equation, use LinearSolve, no need for implementing a specific method. If you need the specific method, then what R.M. said. $\endgroup$
    – Szabolcs
    Feb 20, 2012 at 8:31
  • $\begingroup$ Simpler, like a function versus having to device my own. I will look at LinearSolve, but might just use RowReduce. $\endgroup$ Feb 20, 2012 at 8:42
  • 1
    $\begingroup$ Could you fill in some of the mathematical details in you question, then we could better direct you to the function you should use? $\endgroup$
    – rcollyer
    Feb 21, 2012 at 2:26

1 Answer 1

18
$\begingroup$

Based upon your update, you are trying to solve the system

$$\mathbf{A}\vec{x} = \vec{b}$$

for $\vec{x}$, so LinearSolve is exactly what you want. Also, it has the exact form

LinearSolve[A, b]

that you're asking for. Internally it uses a form of Gaussian elimination to solve such systems; this is most likely a variant of LU decomposition, but other methods are available. If you have more than one $\vec{b}$, you can use the form

solv = LinearSolve[A]

which returns a LinearSolveFunction which you can apply to each $\vec{x}$ in turn via

solv[b]

Edit: In the case of your example, RowReduce will return the identity matrix as your matrix is invertible (non-singular), so it would not be immediately useful. You could make it "useful" and create an augmented matrix, via

 augA = ArrayFlatten[{{#, IdentityMatrix[Length@#]}}]& @ A

which creates $$\left(\mathbf{A}\, |\, \mathbf{I} \right).$$ Then,

 redAugA = RowReduce[augA]

gives a matrix of the form $$\left(\mathbf{I}\, |\, \mathbf{A}^{-1} \right),$$ and the inverse is extractable via

redAugA[[All, Length@A + 1 ;; ]]

which uses the shorthand form of Part and Span to extract only the columns you want. But, if your going to go to the trouble of getting the inverse, you might as well use Inverse[A] directly.

However, if your matrix is singular, i.e. MatrixRank[A] < Length[A], then you need to use LeastSquares which returns the vector, $\vec{x}$, that minimizes $\lVert\mathbf{A}\vec{x} - \vec{b}\rVert_2$ where $\lVert\cdot\rVert_2$ refers to the standard Euclidean norm. Which has the same calling convention

LeastSquares[A, b]

but it lacks the pre-calculation capabilities of LinearSolve. If you need those, then you would first decompose the matrix using QRDecomposition and then LinearSolve is used, as follows

{q,r} = QRDecomposition[A];
LinearSolve[r, q.b]

Or, if you want a single function that operates like the second form of LinearSolve but with the least squares minimization,

savedLeastSquares[m_?MatrixQ]:= 
 Module[{q,r}, 
  {q,r} = QRDecomposition[m];
  LinearSolve[r, q.#]&
 ]
$\endgroup$
3
  • $\begingroup$ One could also use SingularValueDecomposition[] instead of QRDecomposition[]... $\endgroup$ May 4, 2012 at 5:17
  • $\begingroup$ Your same idea of augmentation can be used to solve for A.x=b: instead of augmenting by an identity matrix, put b in a new (rightmost) column. Example: In[50]:= A = RandomInteger[{-10, 10}, {4, 4}]; b = RandomInteger[{-10, 10}, 4]; LinearSolve[A, b] - RowReduce[ArrayFlatten[{{#, Transpose[{b}]}}] &@A][[All, -1]] Out[52]= {0, 0, 0, 0} $\endgroup$ May 31, 2013 at 16:59
  • $\begingroup$ @DanielLichtblau absolutely, where do you think I got the idea? $\endgroup$
    – rcollyer
    May 31, 2013 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.