/*
 * test3300.det - test determinants of a matrix containing objects for test 31dd
 *
 * Copyright (C) 1999,2023  Ernest Bowen and Landon Curt Noll
 *
 * Primary author:  Ernest Bowen
 *
 * Calc is open software; you can redistribute it and/or modify it under
 * the terms of the version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * Calc is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
 * Public License for more details.
 *
 * A copy of version 2.1 of the GNU Lesser General Public License is
 * distributed with calc under the filename COPYING-LGPL.  You should have
 * received a copy with calc; if not, write to Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Under source code control:   1995/12/02 04:27:41
 * File existed as early as:    1995
 *
 * Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/
 */


defaultverbose = 1;     /* default verbose value */

define testi(str, n, N, verbose)
{
        local A, t, i, j, d1, d2;
        local m;

        if (isnull(verbose)) verbose = defaultverbose;
        if (verbose > 0) {
                print str:":",:;
        }
        if (isnull(N))
                N = 1e6;
        mat A[n,n];
        for (i = 0; i < n; i++)
                for (j = 0; j < n; j++)
                        A[i,j] = rand(-N, N);
        t = runtime();
        d1 = det(A);
        t = runtime() - t;
        d2 = det(A^2);
        if (d2 != d1^2) {
                if (verbose > 0) {
                        printf("*** Failure for n=%d, N=%d, d1=%d\n", n, N, d1);
                }
                return 1;       /* error */
        } else {
                if (verbose > 0) {
                        printf("no errors\n");
                }
                if (verbose > 1) {
                        printf("ok: n=%d, N=%d, d1=%d, t=%d\n", n, N, d1, t);
                }
        }
        return 0;       /* ok */
}

define testr(str, n, N, verbose)
{
        local A, t, i, j, d1, d2;

        if (isnull(verbose)) verbose = defaultverbose;
        if (verbose > 0) {
                print str:":",:;
        }
        if (isnull(N))
                N = 1e6;
        mat A[n,n];
        for (i = 0; i < n; i++)
                for (j = 0; j < n; j++)
                        A[i,j] = rand(-(N^2), N^2)/rand(1, N);
        t = usertime();
        d1 = det(A);
        t = usertime() - t;
        d2 = det(A^2);
        if (d2 != d1^2) {
                if (verbose > 0) {
                        printf("*** Failure for n=%d, N=%d, d1=%d\n", n, N, d1);
                }
                return 1;       /* error */
        } else {
                if (verbose > 0) {
                        printf("no errors\n");
                }
                if (verbose > 1) {
                        printf("ok: n=%d, N=%d, d1=%d, t=%d\n", n, N, d1, t);
                }
        }
        return 0;       /* ok */
}

/*
 * test3300 - perform all of the above tests a bunch of times
 */
define test3300(verbose, tnum)
{
        local N;        /* test parameter */
        local i;

        /*
         * set test parameters
         */
        if (isnull(verbose)) {
                verbose = defaultverbose;
        }
        N = 1e6;
        srand(3300e3300);

        /*
         * test a lot of stuff
         */
        for (i=0; i < 19; ++i) {
                err += testi(strcat(str(tnum++), ": testi(", str(i), ")"), \
                             i, N, verbose);
        }
        for (i=0; i < 9; ++i) {
                err += testr(strcat(str(tnum++), ": testr(", str(i), ")"), \
                             i, N, verbose);
        }

        /*
         * test results
         */
        if (verbose > 1) {
                if (err) {
                        print "***", err, "error(s) found in testall";
                } else {
                        print "no errors in testall";
                }
        }
        return tnum;
}
