#!../src/tops  -i -s ../sys  -u ../usr
/*
Program Tops - a stack-based computing environment
Copyright (C) 1999-2006  Dale R. Williamson

Author: Dale R. Williamson <dale.williamson@prodigy.net>

File test/put4  March 2006
To run interactively, see below.

----------------------------------------------------------------------*/

   nl(dot(nl, " Testing put4, get4, get4c, copy4 and toc4"));

/* Real matrix to test, with null and partially null columns: */
   B = [0, 2, 3, 0, 0, 6, 0, 8, 0 ;
        1, 0, 3, 0, 5, 6, 0, 8, 0 ;
        0, 0, 3, 0, 5, 6, 0, 0, 0 ;
        0, 0, 3, 0, 5, 6, 7, 8, 0 ;
        0, 2, 3, 0, 0, 0, 7, 8, 0 ];

/* Complex matrix to test: */
   C = complex(B, pi*B);

/* Make name FILE and delete existing: */
   deleteif((FILE = "/tmp/" + runid + "_test_put.op4"));

/* Open FILE handle called OP4 after closing existing: */
   closeif("OP4");
   open(FILE, new, binary, "OP4");

/* Put matrices on FILE: */
   put4(B, OP4);
   put4(C, OP4);
   put4(naming([10*C], "D"), OP4);
   put4(naming([C*20], "E"), OP4);

/* Make name CFILE and delete existing: */
   deleteif((CFILE = "/tmp/" + runid + "_test_copy.op4"));

/* Close existing, then open file handle CP4 for copies on CFILE: */
   open(closeif("CP4"), CFILE, new, binary, "CP4");

/* Rewind OP4 and copy real and complex to CP4 (CFILE): */
   copy4(rewind(OP4), OP4, "B", CP4, "F");
   copy4(OP4, "C", CP4, "G");

/* Show FILE properties and contents of FILE and CFILE: */
   nl(fprops(nl, OP4)); /* FILE */
   nl(toc4(dot(" File " + FILE + ":"), nl, OP4));
   nl(toc4(dot(" File " + CFILE + ":"), nl, CP4));

/* Fetch real matrix: */
   NULL_A = B - get4(rewind(OP4), OP4, "B");

/* Fetch parts of real matrix: */
   r = [5, 3, 1];
   c = [5, 4];
   NULL_B = B[r, c] - get4c(rewind(OP4), OP4, "B", c)[r, *];

/* Fetch complex matrix: */
   NULL_C = C - get4(OP4, "C");

/* Fetch parts of complex matrices: */
   NULL_D = (C*10)[*, 2:=4] - get4c(OP4, "D", 2:5);
   NULL_E = [20*C][, [9; 1; 4]] - get4c(OP4, "E", [9, 1, 4]);

/* Fetch real and complex copied matrices: */
   NULL_F = B - get4(rewind(CP4), CP4, "F");
   NULL_G = C - get4(CP4, "G");

/* Show results for 7 tests: */
   ALL_NULL = null?(NULL_A) + null?(NULL_B) + null?(NULL_C) +
              null?(NULL_D) + null?(NULL_E) + null?(NULL_F) +
              null?(NULL_G);

   if(ALL_NULL == 7*true) dot(" Ok put4, get4, get4c, copy4 and toc4");
   else dot(" Error in put4, get4, get4c or copy4");

   (nl, idle(2), dot(cats("*",72)), nl);

/* Close handles and delete files: */
   deleteif(fclose(OP4), FILE);
   deleteif(fclose(CP4), CFILE);

/* End of test */

/*----------------------------------------------------------------------

Running this file interactively.

Copy this file to one named work.n, the name that word nn sources, and 
in file work.n, comment-out or remove the last two lines of the test 
that close handles and delete files:
   % /opt/tops/tops/test > cp put4 work.n
   % /opt/tops/tops/test > vi work.n [remove lines]

Save work.n and start the program in infix mode (using -i switch):
   % /opt/tops/tops/test > tops -i

At the program prompt run the following to get started:

   >> nn
   >> whos
    Variables added to the main library:
     Name     Rows Cols Bytes Type  Description
     CFILE    1    24   24    STR   string
     FILE     1    23   23    STR   string
     ALL_NULL 0    0    8     NUM   -7
     B        5    9    360   MAT   dense
     C        5    9    720   MAT   dense:complex
     c        1    2    16    MAT   dense
     CP4      9    1    72    MAT   dense
     NULL_A   5    9    360   MAT   dense
     NULL_B   3    2    48    MAT   dense
     NULL_C   5    9    720   MAT   dense:complex
     NULL_D   5    4    320   MAT   dense:complex
     NULL_E   5    3    240   MAT   dense:complex
     NULL_F   5    9    360   MAT   dense
     NULL_G   5    9    720   MAT   dense:complex
     OP4      9    1    72    MAT   dense
     r        1    3    24    MAT   dense
                     4087  total

   >> toc4(OP4)
    B  5 by 9  form 2  type 2
    C  5 by 9  form 2  type 4
    D  5 by 9  form 2  type 4
    E  5 by 9  form 2  type 4

   >> CP4 toc4
    F  5 by 9  form 2  type 2
    G  5 by 9  form 2  type 4

   >> C' .m
    Row 1: 0,0      1,3.142  0,0      0,0      0,0
    Row 2: 2,6.283  0,0      0,0      0,0      2,6.283
    Row 3: 3,9.425  3,9.425  3,9.425  3,9.425  3,9.425
    Row 4: 0,0      0,0      0,0      0,0      0,0
    Row 5: 0,0      5,15.71  5,15.71  5,15.71  0,0
    Row 6: 6,18.85  6,18.85  6,18.85  6,18.85  0,0
    Row 7: 0,0      0,0      0,0      7,21.99  7,21.99
    Row 8: 8,25.13  8,25.13  0,0      8,25.13  8,25.13
    Row 9: 0,0      0,0      0,0      0,0      0,0

   >> B[*,2:6] .m
    Row 1:        2        3        0        0        6
    Row 2:        0        3        0        5        6
    Row 3:        0        3        0        5        6
    Row 4:        0        3        0        5        6
    Row 5:        2        3        0        0        0

   >> nl(.m(B[1:4,2:=6])); 
    Row 1:        2        3        0        0        6        0
    Row 2:        0        3        0        5        6        0
    Row 3:        0        3        0        5        6        0
    Row 4:        0        3        0        5        6        7

/* Compute C*D' and fetch and display the upper left 3-by-3: */
   >> M = [C * get4(rewind(OP4), OP4, "D")'][1:3,1:3]; .m(M);
    Row 1: -1.002e+04,7100       -9668,6849  -3991,2827
    Row 2:      -9668,6849  -1.197e+04,8482  -6209,4398
    Row 3:      -3991,2827       -6209,4398  -6209,4398


/* Read the file into vi and edit it and run it again: */
   >> vi work.n [editing and saving]
   >> nn

----------------------------------------------------------------------*/
