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.#]&
]
RowReduce
? How is it not satisfactory and what do you mean by "simpler" solution? $\endgroup$LinearSolve
, no need for implementing a specific method. If you need the specific method, then what R.M. said. $\endgroup$