0

I am new to this language and I find this a bit confusing. Can you help me with this please? I just have a pseudocode so far. Will you help with the syntax please?

The each row of the matrix would be a list and the argument would be list of lists

function (list of lists) = 
    if (matrix is null) then
        false
    else if (unit matrix with element 1) then
        true
    else
        add all the lists and get a list with the lenght of number of lists
        check if each element is 2
        if (2) then
            true
        else false
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
  • May be back later in the day (European time) for an answer, but for now, what about using an appropriate type for matrix? Two‑dimensional array could be a match: [The Array2 structure](http://sml-family.org/Basis/array2.html). Then check the matrix is a square prior to anything. – Hibou57 Sep 27 '14 at 05:47
  • write a function for the Kronecker delta: http://en.wikipedia.org/wiki/Kronecker_delta and check whether M_i,j equals kronecker(i,j) for all i,j – Gergely Oct 13 '14 at 13:12

1 Answers1

0

Lists of lists are not optimal for dealing with matrices in SML, but it is still a good exercise to write a function for checking if a list of lists is an identity matrix.

A recursive approach is natural. In the following picture:

enter image description here

Note that:

  1. The first element of the first row is a 1
  2. The rest of that row is all 0
  3. The rest of the first column is all 0
  4. The rest of the matrix (below and to the right of the shaded region) is itself an identity matrix.

As a preliminary, write a function which tests if a list is all 0. You could use the standard basis function all to do so, though it is easy enough to roll your own:

fun all_zeros [] = true
|   all_zeros (x::xs) = x = 0 andalso all_zeros xs

The main function checks the 4 points given above. Some care must be given to various basis cases. The empty list is not an identity matrix. Nor is a list of empty lists. This last case is easy to overlook, but if you pass the function a matrix which has more rows than columns you run out of columns before rows. I am mapping hd across the bottom rows to extract the rest of column 1 (below the element in the upper left) and am mapping tl across the bottom rows to get the lower right part of the matrix to feed back into the function recursively. This might produce a nonempty lists of empty lists, so that must be checked:

fun id_matrix [] = false
|   id_matrix (row::[]) = (row = [1])
|   id_matrix ([]::rows) = false
|   id_matrix (row::rows) = 
    let
       val x :: xs = row
    in
       x = 1 andalso 
       all_zeros(xs) andalso 
       all_zeros(map hd rows) andalso 
       id_matrix(map tl rows)
    end

As a final remark -- my code assumes that the lists of lists represents a valid array and merely checks if it is the identity. Passing it a jagged array rather than a matrix could crash it. It would be easy enough to add a check, though it would make the code more cumbersome.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • I know this is an old question but I thought it would be fun to find some old but interesting unanswered questions in some of my favorite SO tags. – John Coleman Jul 31 '15 at 00:45