#!../src/tops  -s ../sys  -u ../usr

no catmsg
"ranint" missing
IF
    "math.v" source
THEN
"*c" missing
IF
    "mmath.v" source
THEN

"splu" missing
IF
    "tops not compiled with UMFPACK; cannot run spsolve" . nl
    halt
THEN

time seedset \ getting different random sequence every time

define: sp_write ( qFile hSp --- ) # {{{1
    # Writes the sparse matrix on tos to the specified file
    swap forn binary "SP" file SP fwrite drop drop SP fclose
    ; # 1}}}
define: sp_read ( qFile --- hSp ) # {{{1
    # Loads the sparse matrix from the specified file to tos.
    old binary "SP" file SP SP file.size pry fget SP fclose
    ; # 1}}}
define: chk_spsolve ( n --- ) # {{{1
    # Word tested:  spmult_sd_rr      Real sparse * Real dense
    1.0e-12 is TOLERANCE

    dup
    dup
    rand * integer 1 + into nR_A    # rows, cols of [A], rows of [B]
    rand * integer 1 + into nC_B    # columns  [B]
    rand               into rhoA    # density  [A]
    nR_A "[%3.0f x " format . nR_A " %3.0f " format . rhoA " ,%5.3f]\{" format . nR_A " %3.0f x " format . nC_B "%3.0f} " format .

    nR_A nR_A rhoA sprand nR_A speye + into A
    nR_A nC_B random                   into B
    A splu B spfbs                     into C
    A C spmult B -
    abs maxfetch drop drop 
    dup .
    TOLERANCE > 
    IF  
        " spsolve ERROR " . 
        "A.sp" A sp_write
        " spsolve ERROR; Wrote A to A.sp " . nl
        halt
    ELSE  
        " spsolve  OK " . 
    THEN

    nl
    xx

    ; # 1}}}
define: n_spsolve_tests ( nIter nSize  --- ) # {{{1
    # Invokes chk_spmult_sd_rr nIter times for a random sized systems (up to nSize)
    into nSize
    1 DO I nSize 
        I "%5.0f.  " format .
        chk_spsolve
        xx
    LOOP
    ; # 1}}}

time seedset
# Iter Size
  100    50  n_spsolve_tests  nl
