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

"modes"   missing 
IF 
    "mmath.v" source 
THEN

"ceigSSL" missing 
IF 
    "tops not compiled with LAPACK; cannot run eigen" nl . 
    halt 
THEN

define: n_ceig_tests ( nIter nSize  --- )
    \ Invokes chk_ceig_sover nIter times for a nSize x nSize sized system.
    into nSize
    1 DO nSize 
        I "%5.0f.  " format .
        chk_ceig_solver 
    LOOP
    ;

define: chk_ceig_solver ( n --- )
    \ Creates an n x n random matrix, then computes its complex
    \ eigensolution with ceigSSL.  Checks the accuracy of the
    \ solution  by computing [A][Phi] - [Phi][lambda].
    dup random into A
    A yes ceigSSL              \ (hA -1 --- hLr hLi hAr hAi)
    into Phii                  \ Phii <- hAi      ( hLr hLi hAr )
    into Phir                  \ Phir <- hAr      ( hLr hLi )
    A 0i Phir Phii *c          \ [A]*[Phi]        ( hLr hLi -- Lr Li Cr Ci )
    Phir Phii 5 pick 5 pick    \ bring eigenvalue array to TOS
    diagpost_complex           \ [Phi]*[Lamba]
    negate swap negate swap +c \ [A]*[Phi] - [Phi]*[Lamba]  ( Dr Di Lr Li )
    abs max maxfetch drop drop \ ( err Lr Li )
    rot rot drop drop dup      \ ( err err )
    " ceigSSL " .
    1.0e-10
    > IF 
        . " ERROR " . nl halt 
    ELSE 
        . " OK " . nl 
    THEN
    ;

define: n_sort_tests ( nIter nSize  --- )
    \ Invokes sort_seq and reorder nIter times for random nSize arrays
    into nSize
    1 DO 
        I "%5.0f.  " format .
        nSize 1 random into A 
        A sort_seq 
        A reorder
        " sort_seq " .
        ascending 
        IF 
            " OK " . nl 
        ELSE 
            " ERROR " . nl halt 
        THEN
        drop drop
    LOOP
    ;

# Iter Size
   30    50 n_sort_tests   nl 
   30    50 n_ceig_tests   nl 
