How to resolve the algorithm LU decomposition step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm LU decomposition step by step in the Nim programming language

Table of Contents

Problem Statement

Every square matrix

A

{\displaystyle A}

can be decomposed into a product of a lower triangular matrix

L

{\displaystyle L}

and a upper triangular matrix

U

{\displaystyle U}

, as described in LU decomposition. It is a modified form of Gaussian elimination. While the Cholesky decomposition only works for symmetric, positive definite matrices, the more general LU decomposition works for any square matrix. There are several algorithms for calculating L and U. To derive Crout's algorithm for a 3x3 example, we have to solve the following system: We now would have to solve 9 equations with 12 unknowns. To make the system uniquely solvable, usually the diagonal elements of

L

{\displaystyle L}

are set to 1 so we get a solvable system of 9 unknowns and 9 equations. Solving for the other

l

{\displaystyle l}

and

u

{\displaystyle u}

, we get the following equations: and for

l

{\displaystyle l}

: We see that there is a calculation pattern, which can be expressed as the following formulas, first for

U

{\displaystyle U}

and then for

L

{\displaystyle L}

We see in the second formula that to get the

l

i j

{\displaystyle l_{ij}}

below the diagonal, we have to divide by the diagonal element (pivot)

u

j j

{\displaystyle u_{jj}}

, so we get problems when

u

j j

{\displaystyle u_{jj}}

is either 0 or very small, which leads to numerical instability. The solution to this problem is pivoting

A

{\displaystyle A}

, which means rearranging the rows of

A

{\displaystyle A}

, prior to the

L U

{\displaystyle LU}

decomposition, in a way that the largest element of each column gets onto the diagonal of

A

{\displaystyle A}

. Rearranging the rows means to multiply

A

{\displaystyle A}

by a permutation matrix

P

{\displaystyle P}

: Example: The decomposition algorithm is then applied on the rearranged matrix so that

The task is to implement a routine which will take a square nxn matrix

A

{\displaystyle A}

and return a lower triangular matrix

L

{\displaystyle L}

, a upper triangular matrix

U

{\displaystyle U}

and a permutation matrix

P

{\displaystyle P}

, so that the above equation is fulfilled. You should then test it on the following two examples and include your output.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm LU decomposition step by step in the Nim programming language

Source code in the nim programming language

import macros, strutils
import strfmt

type

  Matrix[M, N: static int] = array[1..M, array[1..N, float]]
  SquareMatrix[N: static int] = Matrix[N, N]


# Templates to allow to use more natural notation for indexing.
template `[]`(m: Matrix; i, j: int): float = m[i][j]
template `[]=`(m: Matrix; i, j: int; val: float) = m[i][j] = val


func `*`[M, N, P: static int](a: Matrix[M, N]; b: Matrix[N, P]): Matrix[M, P] =
  ## Matrix multiplication.
  for i in 1..M:
    for j in 1..P:
      for k in 1..N:
        result[i, j] += a[i, k] * b[k, j]


func pivotize[N: static int](m: SquareMatrix[N]): SquareMatrix[N] =

  for i in 1..N: result[i, i] = 1

  for i in 1..N:
    var max = m[i, i]
    var row = i
    for j in i..N:
      if m[j, i] > max:
        max = m[j, i]
        row = j
    if i != row:
      swap result[i], result[row]


func lu[N: static int](m: SquareMatrix[N]): tuple[l, u, p: SquareMatrix[N]] =

  result.p = m.pivotize()
  let m2 = result.p * m

  for j in 1..N:
    result.l[j, j] = 1
    for i in 1..j:
      var sum = 0.0
      for k in 1..<i: sum += result.u[k, j] * result.l[i, k]
      result.u[i, j] = m2[i, j] - sum
    for i in j..N:
      var sum = 0.0
      for k in 1..<j: sum += result.u[k, j] * result.l[i, k]
      result.l[i, j] = (m2[i, j] - sum) / result.u[j, j]


proc print(m: Matrix; title, f: string) =
  echo '\n', title
  for i in 1..m.N:
    for j in 1..m.N:
      stdout.write m[i, j].format(f), "  "
    stdout.write '\n'


when isMainModule:

  const A1: SquareMatrix[3] = [[1.0, 3.0, 5.0],
                               [2.0, 4.0, 7.0],
                               [1.0, 1.0, 0.0]]

  let (l1, u1, p1) = A1.lu()
  echo "\nExample 2:"
  A1.print("A:", "1.0f")
  l1.print("L:", "8.5f")
  u1.print("U:", "8.5f")
  p1.print("P:", "1.0f")


  const A2: SquareMatrix[4] = [[11.0,  9.0, 24.0,  2.0],
                               [ 1.0,  5.0,  2.0,  6.0],
                               [ 3.0, 17.0, 18.0,  1.0],
                               [ 2.0,  5.0,  7.0,  1.0]]

  let (l2, u2, p2) = A2.lu()
  echo "Example 1:"
  A2.print("A:", "2.0f")
  l2.print("L:", "8.5f")
  u2.print("U:", "8.5f")
  p2.print("P:", "1.0f")


  

You may also check:How to resolve the algorithm Bitmap/Histogram step by step in the Racket programming language
You may also check:How to resolve the algorithm Call a function step by step in the Pascal programming language
You may also check:How to resolve the algorithm Jordan-Pólya numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm Cistercian numerals step by step in the C programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the jq programming language