Notes on Solving Systems of Linear Equations Using J

John E. Howland
Department of Computer Science
Trinity University
715 Stadium Drive
San Antonio, Texas 78212-7200
Voice: (210) 999-7364
Fax: (210) 999-7477
E-mail: jhowland@Trinity.Edu
Web: http://www.cs.trinity.edu/~jhowland/

Abstract:

Linear systems of equations are presented. Use of J for solutions of linear systems are given together with J primitives for related topics such as determinants.

Subject Areas: Linear Systems.
Keywords: Modeling, J Programming Language, Solving Linear Systems of Equations.

1 Introduction

In these notes, we consider the problem of solving linear systems of equations. A linear system of equations in $n$ unknowns, $\begin{array}{l} x_1, x_2, \ldots x_n \end{array}$ may be written as:


\begin{displaymath}
\begin{array}{l}
a_{11} \times x_1 + a_{12} \times x_2 + \ld...
...{n2} \times x_2 + \ldots + a_{nn} \times x_n = b_n
\end{array}\end{displaymath} (1)

Let $X = \left[ \begin{array}{l} x_1 \\ x_2 \\ \ldots \\ x_n \end{array} \right]$ be a matrix having $n$ rows and 1 column represent the $n$ unknowns, $B = \left[ \begin{array}{l} b_1 \\ b_2 \\ \ldots \\ b_n \end{array} \right]$ a matrix having $n$ rows and 1 column representing the right-hand sides of equations (1), and an $n$ by $n$ matrix of coefficients of each of the equations (1),


\begin{displaymath}
A = \left[
\begin{array}{l}
\begin{array}{llll} a_{11} & a_...
... & a_{n2} & \ldots & a_{nn} \end{array} \\
\end{array}\right]
\end{displaymath} (2)

Then, given the J definition of matrix product

   mp =: +/ . *

we can rewrite the equations (1) as the matrix equation:


\begin{displaymath}
A \ \texttt{mp} \ X = B
\end{displaymath} (3)

This linear system of equations can be solved by computing the inverse matrix, $A^{-1}$ of $A$ and multiplying both sides of equation (3) on the left (matrix multiplication is not commutative) by $A^{-1}$


\begin{displaymath}
(A^{-1} \ \texttt{mp} \ A) \ \texttt{mp} \ X = A^{-1} \ \texttt{mp} \ B
\end{displaymath} (4)

The inverse matrix, $A^{-1}$ exists when and only when the determinant of $A$ (discussed in Section 2) is non-zero. Simplifying equation (4) we have


\begin{displaymath}
\begin{array}{l}
I \ \texttt{mp} \ X = A^{-1} \ \texttt{mp} \ B \\
X = A^{-1} \ \texttt{mp} \ B
\end{array}\end{displaymath} (5)

Where $I$ is the $n$ by $n$ identitiy matrix ( 1's down the main diagonal, 0 elsewhere). The identity matrix satisfies the equation


\begin{displaymath}
I \ \texttt{mp} \ M = M \ \texttt{mp} \ I = M
\end{displaymath} (6)

for any $n$ by $n$ square matrix $M$.

The J monad %. computes the inverse matrix. Consider the following example:


\begin{displaymath}
\begin{array}{rrrrr}
2 \times x_1 & + \; 3 \times x_2 & - \;...
...x_1 & - \; 2 \times x_2 & + \; 3 \times x_3 & = & 5
\end{array}\end{displaymath} (7)

The coefficient matrix for this system is


\begin{displaymath}
\begin{array}{rrr}
2 & 3 & -4 \\
-4 & 2 & 5 \\
3 & -2 & 3
\end{array}\end{displaymath} (8)

In J we solve this system in the following manner. We first define mp and setup the coefficient matrix.

   mp =: +/ . *
   [a =: 3 3 $ 2 3 _4 _4 2 5 3 _2 3
 2  3 _4
_4  2  5
 3 _2  3
   [b =: 12 3 5
12 3 5

Next check the determinant of the coefficient matrix to insure that the system has a solution (the determinant must be non-zero).

   det =: -/ . *
   det a
105

The matrix inverse, %., is used to compute the solution of the system of 3 equations in 3 unknowns.

   (%.a) mp b
2.89524 3.88571 1.3619

Finally we check our answer by:

   a mp (%.a) mp b
12 3 5


2 Determinants

A linear system of $n$ equations in $n$ unknowns, as shown in equations (1), has a solution if and only if the determinant of the corresponding matrix of coefficients (2) is non-zero. Given an $n$ by $n$ matrix of real numbers such as (2), we define the determinant of $A$ recursively as the real number determined as:

The minor of an element $a_{ij}$ of an $n$ by $n$ matrix $A$ shown in (2), is the $n-1$ by $n-1$ matrix which is obtained by deleting row $i$ and column $j$ of A.

As an example, consider the following J matrix:

   [a =: 3 3 $ 2 3 _4 _4 2 5 3 _2 3
 2  3 _4
_4  2  5
 3 _2  3
   minors=: }."1 @ (1&([\.))
   minors a
 2  5
_2  3

 3 _4
_2  3

 3 _4
 2  5

We can see that minors computes the minors (first row to last) of the first column of a matrix. Using the definition of a determinant, we have


\begin{displaymath}
det(a) = 2 \times det(\left[ \begin{array}{rr}2 & 5\\ -2 & 3...
...et(\left[ \begin{array}{rr}3 & -4\\ 2 & 5 \end{array} \right])
\end{displaymath} (10)

Using the J definition for minors, we now compute the minors of each of the $2$ by $2$ matrices in minors a.

   minors "2 minors a
 3

 5


 3

_4


 5

_4

Hence, we can compute each of the determinants in equation (10).


\begin{displaymath}
\begin{array}{r}
2 \times det(\left[ \begin{array}{rr}2 & 5...
...\right]) = 3 \times ( 3 \times 5 +
2 \times 4) = 69
\end{array}\end{displaymath}

Hence $det(a)=105$. We can use the J primitive for determinant to check our answer.

   det =: -/ . *
   det a
105


2002-12-04