Search code examples
javapseudocode

How to implement argmax from gaussian problem in Java


I'm working on Gauss Elimination (precisely im trying to transform a matrix into row echelon form) and immediately research some source which may help me. I found one of this pseudocode here in Wikipedia related to gaussian elimination function.

h := 1 /* Initialization of the pivot row */
k := 1 /* Initialization of the pivot column */

while h ≤ m and k ≤ n
    /* Find the k-th pivot: */
    i_max := argmax (i = h ... m, abs(A[i, k]))
    if A[i_max, k] = 0
        /* No pivot in this column, pass to next column */
        k := k+1
    else
         swap rows(h, i_max)
         /* Do for all rows below pivot: */
         for i = h + 1 ... m:
             f := A[i, k] / A[h, k]
             /* Fill with zeros the lower part of pivot column: */
             A[i, k] := 0
             /* Do for all remaining elements in current row: */
             for j = k + 1 ... n:
                 A[i, j] := A[i, j] - A[h, j] * f
         /* Increase pivot row and column */
         h := h + 1
         k := k + 1

I'm not sure whether how to implement the argmax function which is stated on line 6 of the pseudocode.


Solution

  • I interpret the expression argmax(i = h ... m, abs(A[i, k])) as

    "find the index i which maximizes the expression abs(A[i,k]) over the range h..m".

    So in other words. loop on i (from h .. m) and find the maximum value of abs(A[i,k]) where k is a constant value (from the containing loop) and return the index 'i'.

    // inline implementation of `argmax_abs_a_ik` inside while loop
    int i_max = h;
    for (int i = h; i <= m; i++) {
        if (Math.abs(A[i,k]) > Math.abs(A[i_max,k])) {
           i_max = i;
        }
    }
    // i_max contains result