Linear Algebra (Lec03)

yukita@k.hosei.ac.jp

•System of Linear Equations

sol1 = Solve[ {x + y + z == 0, x - y + 2 z == 0, 3 x + y + 4 z == 0}, {x, y, z}]

Solve :: svars :  方程式はすべての \"solve\" 変数に対しては解を与えない可能性があります.

{{x -> -(3 z)/2, y -> z/2}}

Solve[ {x + y + z == 1, x - y + 2 z == 1, 3 x + y + 4 z == 1}, {x, y, z}]

{}

•Matrix

The following command will change the default output format so that we get matrix output in a conventional manner.

$PrePrint = MatrixForm

MatrixForm

A = {{1, 1, 1}, {1, -1, 2}, {3, 1, 4}}

( 1    1    1  )    1    -1   2    3    1    4

A . {x, y, z} /. sol1[[1]]

( 0 )    0    0

If we input a 1-dim list, the system automatically convert it to a column vector.

{0, 0, 0}

( 0 )    0    0

I do not like this feature. There is no way to distinguish a column vector from a row vector in Mathematica. For example, let us evaluate the following expression.

{x, y, z} . A

( x + y + 3 z   )    x - y + z    x + 2 y + 4 z

We see that the system treated the list {x,y,z} as a row vector, performed the usual matrix multiplication, and finally converted the result to a column vector. How confusing!

•Elementary Row Operations

The RowReduce function performs Gaussian elimination.

RowReduce[A]

(           3  )             -   1    0    2               1             --   0    1     2      0    0    0

Let us do this step by step.

Which of the following definitions is better?

multRow[X_, row_, scale_] := Join[Take[X, row - 1], {scale * Part[X, row]}, Drop[X, row]]

If you want to use the above definition, do not evaluate the next cell.

multRow[X_, row_, scale_] := ReplacePart[X, scale * Part[X, row], row]

A

( 1    1    1  )    1    -1   2    3    1    4

multRow[A, 1, 10]

( 10   10   10 )    1    -1   2    3    1    4

addRow[X_, dest_, src_, scale_] := ReplacePart[X, Part[X, dest] + scale * Part[X, src], dest]

A

( 1    1    1  )    1    -1   2    3    1    4

addRow[A, 1, 2, 10]

( 11   -9   21 )    1    -1   2    3    1    4

A

( 1    1    1  )    1    -1   2    3    1    4

A1 = addRow[A, 2, 1, -1]

( 1    1    1  )    0    -2   1    3    1    4

A2 = addRow[A1, 3, 1, -3]

( 1    1    1  )    0    -2   1    0    -2   1

A3 = addRow[A2, 3, 2, -1]

( 1    1    1  )    0    -2   1    0    0    0

A4 = multRow[A3, 2, -1/2]

(              )    1    1    1               1             --   0    1     2      0    0    0

A5 = addRow[A4, 1, 2, -1]

(           3  )             -   1    0    2               1             --   0    1     2      0    0    0

•Add-on package

<< LinearAlgebra`MatrixManipulation`

mat = Array[m, {4, 4}]

( m[1, 1]   m[1, 2]   m[1, 3]   m[1, 4] )    m[2, 1]   m[2, 2]   m[2, 3]   m[2, 4]    m[3, 1]   m[3, 2]   m[3, 3]   m[3, 4]    m[4, 1]   m[4, 2]   m[4, 3]   m[4, 4]

TakeRows[mat, -2]

( m[3, 1]   m[3, 2]   m[3, 3]   m[3, 4] )    m[4, 1]   m[4, 2]   m[4, 3]   m[4, 4]

TakeColumns[mat, {2, 3}]

( m[1, 2]   m[1, 3] )    m[2, 2]   m[2, 3]    m[3, 2]   m[3, 3]    m[4, 2]   m[4, 3]

TakeColumns[mat, 3]

( m[1, 1]   m[1, 2]   m[1, 3] )    m[2, 1]   m[2, 2]   m[2, 3]    m[3, 1]   m[3, 2]   m[3, 3]    m[4, 1]   m[4, 2]   m[4, 3]

This takes the submatrix between the element at position{2, 3}and the element at position{4, 4}.The same result is obtained withTake[mat, {2, 4}, {3, 4}]ormat[[{2, 3, 4}, {3, 4}]].

TakeMatrix[mat, {2, 3}, {4, 4}]

( m[2, 3]   m[2, 4] )    m[3, 3]   m[3, 4]    m[4, 3]   m[4, 4]

Here is the same submatrix,specified as a3×2matrix starting with the element at position{2, 3}.

SubMatrix[mat, {2, 3}, {3, 2}]

( m[2, 3]   m[2, 4] )    m[3, 3]   m[3, 4]    m[4, 3]   m[4, 4]

UpperDiagonalMatrix[f, 3]

( f[1, 1]   f[1, 2]   f[1, 3] )    0         f[2, 2]   f[2, 3]    0         0         f[3, 3]

LowerDiagonalMatrix[#1 + #2 &, 3]

( 2   0   0 )    3   4   0    4   5   6

•Exercises

•Problem 1

Define the multCol function which does the same task as multRow except that the operation is on the columns rather than rows.  Define also the addCol function.

•Solution

Converted by Mathematica  (April 21, 2003)