Seppo Mustonen : Programming Survo in C

8. Libraries

The ready-made tools for programming Survo modules have been collected in the following libraries:
SURVO.LIB Functions for general system control, management of the edit field, data management, management of matrix files, specifications, sucros and prompts
SURVOMAT.LIB Routines for matrix management and algebra
DISTRIB.LIB Continuous statistical distributions (by T.Patovaara)

All the functions in these libraries have been compiled in the large memory model. The functions are described separately for each library.

8.1 Library SURVO.LIB

Functions in library SURVO.LIB

[ activated | conditions | create_newvar | data_alpha_load | data_alpha_save | data_close | data_load | data_open | data_open2 | data_open3 | data_read_open | data_save | data_to_write | edline2 | edread | edwrite | empty_line | fconv | fnconv | fi_create | fi_increase_n | hae_apu | init_remarks | lastline2 | mask | matrix_format | matrix_load | matrix_print | matrix_save | nextch | output_open | output_line | output_close | prompt | rem_pr | scales | scale_ok | shadow_create | shadow_test | spec_init | spfind | split | sp_init | survoid | sur_print | sur_wait | s_end | s_init | tut_init | tut_end | unsuitable | varfind | varfind2 | wait_remarks | wfind | write_string ]

8.2 Library SURVOMAT.LIB

Matrix functions are tools for making SURVO 84C operations for linear models, multivariate analysis, etc. For example, the matrix interpreter employs the library functions through the MAT operations.

The matrix operands and results are referred to by pointers of the double type. To enable dynamic space allocation, the matrices are always stored as one-dimensional arrays. The elements of the matrices (of type double) are saved columnwise. All computations are carried out in double precision.

For example, the function mat_transp is written as

     mat_transp(T,X,m,n)
     double *T,*X;
     int m,n;
         {
         register int i,j;

         for (i=0; i<m; ++i) for (j=0; j<n; ++j)
             T[j+n*i]=X[i+m*j];
         return(1);
         }

It transposes a m*n matrix X and gives the result as a n*m matrix T. The elements of X are X[i+m*j] with row indices i=0,1,...,m-1 and column indices j=0,1,...,n-1.

The matrix functions do not allocate space for result matrices. For example, if the function above is called, space for the result T must have been reserved by

     T=(double *)malloc(m*n*sizeof(double));
     if (T==NULL) { not_enough_space(); return(-1); }

If the matrix operation is successful, 1 is returned. Otherwise 0 or a negative integer is returned. In many cases the return value -i indicates that the operation has failed on row/column i of the matrix.

Matrix input and output

The SURVOMAT.LIB library does not support the matrix input and output directly. The SURVO.LIB library, however, includes the functions matrix_load and matrix_save functions for matrix files of the type used in MAT operations, for example. These functions should be used in all SURVO 84C operations which read and write matrices.

For example, the following SURVO 84C module transposes the matrix in matrix file A and saves the result in matrix file B, when the command

    MTRANSP A TO B
is given in SURVO 84C. The operation is equivalent to
    MAT B=A'

  1 /* !mtransp.c 30.10.1986/SM  (24.6.1989) */
  2 #include "survo.h"
  3 #include "survoext.h"
  4 #include "survomat.h"
  5 #include <stdio.h> 
  6 #include <string.h> 
  7 #include <malloc.h> 
  8 double *A,*B;        /* pointers to matrices */
  9 int m,n;             /* dimensions of A */
 10 char *rlab,*clab;    /* row and column labels */
 11 int rlen,clen;       /* lengths of labels */
 12 int type;            /* type of matrix (not used) */
 13 char expr[LLENGTH];  /* internal matrix name */
 14 char newexpr[LLENGTH];
 15
 16 main(argc,argv)
 17 int argc; char *argv[];
 18     {
 19     int i;
 20
 21     if (argc==1) return;
 22     s_init(argv[1]);
 23     if (g<4) {
 24         sur_print("\nUsage: MTRANSP A TO B");
 25         WAIT; return;
 26         }
 27     i=matrix_load(word[1],&A,&m,&n,&rlab,&clab,&rlen,&clen,&type,expr);
 28     B=(double *)malloc(m*n*sizeof(double));
 29     if (B==NULL) {
 30         sur_print("\nNot enough memory!");
 31         WAIT; return;
 32         }
 33     mat_transp(B,A,m,n);
 34     strcpy(newexpr,"("); strcat(newexpr,expr);
 35     strcat(newexpr,")'");
 36     matrix_save(word[3],B,n,m,clab,rlab,clen,rlen,0,newexpr,0,0);
 37     }

The matrix_load call (27) also allocates space for matrix A and its row and column labels rlab,clab. Space is allocated for the transpose B on lines 28-32 and after transposing the label of the matrix is updated on lines 34-35. Finally the matrix_save call (36) saves B in a matrix file. For additional information on matrix_load and matrix_save functions, see their descriptions in the SURVO.LIB library.

Functions in library SURVOMAT.LIB

[ mat_add | mat_sub | mat_mlt | mat_inv | mat_transp | mat_mtm | mat_mmt | mat_2mtm | mat_2mmt | mat_dmlt | mat_mltd | mat_center | mat_nrm | mat_sum | mat_p | mat_chol | mat_cholinv | mat_cholmove | mat_gram_schmidt | mat_svd | mat_tred2 | mat_tql2 | solve_upper | solve_lower | solve_diag | solve_symm | ortholin1 | sis_tulo ]

8.3 Library DISTRIB.LIB

Many statistical operations give test statistics with appropriate P values obtained from standard distributions. To provide such P values and other numerical characteristics related to theoretical distributions, a set of C routines for density, distribution and inverse distribution functions of the common continuous distributions have been written by T.Patovaara.
These functions are presented in the DISTRIB.LIB library.

The sources for the algorithms used are:

Abramowitz and Stegun: Handbook of Mathematical Functions with Formulas, Graphs and Mathematical Tables, Dover 1970.
Griffiths and Hill: Applied Statistical Algorithms, Horwood 1985.
Kennedy and Gentle: Statistical Computing, Dekker 1980.

Functions in library DISTRIB.LIB

[ cdf_std | inv_std | pdf_t | cdf_t | inv_t | pdf_chi2 | cdf_chi2 | inv_chi2 | pdf_beta | cdf_beta | inv_beta | pdf_f | cdf_f | inv_f | lg_gamma ]


Front page of Programming Survo in C