Contents Previous Next Subchapters

C Structure That Defines A Matrix
See Also: linkdll

Description
The details of a matrix structure are important when communicating between C or C++ and O-Matrix. The file matrix.h defines a matrix and other types listed below which are necessary for this communication.

Matrix Type
The matrix types LOGICAL_mat through COMPLEX_mat are in coercion order with COMPLEX_mat the highest coercion type. Matrices with type greater than COMPLEX_mat are not automatically coerced to other types by binary operations or intrinsic functions. Matrices with type greater than or equal to UNDEF_mat do not use extra memory for their matrix elements. Matrices with type greater than or equal to INDIRECT_mat, actually point to other matrices using their adr.m field.
enum type_mat
{    // coercion types in order with coercion to higher type
     LOGICAL_mat,      // a logical matrix
     CHAR_mat,         // a character matrix
     INT_mat,          // an integer matrix
     REAL_mat,         // a real matrix
     DOUBLE_mat,       // a double precision matrix
     COMPLEX_mat,      // a complex matrix

     // other types that require memory to be allocated for elements
     FUN_mat,          // a function

     // types of matrices that do not require memory allocate for elements
     UNDEF_mat,        // an undefined value
     INDIRECT_mat,     // points to a matrix that can change
     CONSTANT_mat      // points to a matrix that cannot change
};
typedef enum type_mat mtype;

Matrix Type Name
The following list is in the same order as the types above. Except for INDIRECT and CONSTANT, it is the value of the O-Matrix type function that corresponds to each of the matrix types above.
extern char *name_type_mat[];
# ifdef NAME_TYPE_MAT
char *name_type_mat[]= {
     "logical",
     "char",
     "int",
     "real",
     "double",
     "complex",
     "function",
     "novalue",
     "indirect",
     "constant"
};
# endif 

Element Type
The following are the C types that currently correspond to each of the O-Matrix element types. (Note that a complex element is represented as a sequence of two double precision elements with the real part coming first.)
typedef unsigned char   mbool;  // logical element
typedef unsigned char   mchar;  // character element
typedef int             mint;   // integer element
typedef float           mreal;  // real element
typedef double          mdbl;   // double precision element

typedef void            *vptr;  // pointer to a void
typedef mbool           *bptr;  // pointer to a logical element
typedef mchar           *cptr;  // pointer to a character element
typedef mint            *iptr;  // pointer to an integer element
typedef mreal           *rptr;  // pointer to a real element
typedef mdbl            *dptr;  // pointer to a double precision element

Matrix Element Pointer
Each matrix contains an address field which is a pointer to an arbitrary matrix element. The following is the definition of and address:
typedef union
{    bptr b;         // Logical
     cptr c;         // Character 
     iptr i;         // Integer 
     rptr r;         // Real 
     dptr d;         // Double 
     vptr v;         // Void 
     int m;          // For indirect addressing 
} address;

A Matrix Structure
The following is the definition of a matrix
typedef unsigned char mflag;         // type used to hold all the flags
typedef struct 
{    
     int nr;         // number of rows
     int nc;         // number of columns
     int br;         // base index for rows
     int bc;         // base index for columns
     int name;       // index in constant_char table of name
     address adr;    // points to the elements in the matrix
     mtype type;     // type of the matrix
     mflag sflag;    // small memory allocation flag
} matrix;

Example
The following C code segment assigns zero to the (i, j) element of the matrix mat
     k = i + j * mat->nr;
     switch(mat->type)
     {
          case LOGICAL_mat:
          mat->adr.b[k] = 0;
          break;

          case CHAR_mat:
          mat->adr.c[k] = 0;
          break;

          case INT_mat:
          mat->adr.i[k] = 0;
          break;

          case REAL_mat:
          mat->adr.r[k] = 0;
          break;

          case DOUBLE_mat:
          mat->adr.d[k] = 0;
          break;

          case COMPLEX_mat:
          mat->adr.d[2 * k]     = 0;
          mat->adr.d[2 * k + 1] = 0;
          break;
     }
Note that the (i, j) element in C corresponds to the (i + mat->br, j + mat->bc) element in O-Matrix.