#!/usr/bin/python3
"""
L-20 MCS 507 Fri 6 Oct 2023 : compute_det.py

This script is called by web_det.py to compute the determinant.
The dimension of the matrix is in the field "dim" of the form.
The (i,j)-th element of the matrix is in the field "i,j"
of the form, where i and j are the values of the indices.
"""

def determinant(form, dim):
    """
    Returns the determinant of the matrix
    available in the form.
    """
    from numpy import zeros
    from numpy.linalg import det
    mat = zeros((dim, dim), float)
    for i in range(0, dim):
        for j in range(0, dim):
            info = "%d,%d" % (i, j)
            if info in form:
                mat[i, j] = float(form[info].value)
    print("The matrix is\n", mat)
    return det(mat)

def main():
    """
    Computing the determinant of a matrix
    available via a CGI form.
    """
    import cgi
    print("Content-type: text/plain\n")
    form = cgi.FieldStorage()
    if "dim" in form:
        dim = int(form["dim"].value)
        detval = determinant(form, dim)
        print("The determinant :", detval)

main()
