//=================================================== file = mat2iter.c =====
//=  Convert a text matrix input to the column format needed for iter.c     =
//=   - P or Q type is automatically determined                             =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=        * No comments allowed in input matrix                            =
//=    2) Output is to stdout                                               =
//=    3) Input matrix must be square                                       =
//=    4) No validity checking is done (beyond checking for squareness).    =
//=       Can use check.c to check for valid P or Q matrix.                 =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=      0  0.75  0.25                                                      =
//=   0.25     0  0.75                                                      =
//=   0.25  0.25  0.50                                                      =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat"):                                    =
//=                                                                         =
//=   P 3                                                                   =
//=   & ---------------- column #0    &                                     =
//=   1   0.250000                                                          =
//=   2   0.250000                                                          =
//=   -1      zero                                                          =
//=   & ---------------- column #1    &                                     =
//=   0   0.750000                                                          =
//=   2   0.250000                                                          =
//=   -1      zero                                                          =
//=   & ---------------- column #2    &                                     =
//=   0   0.250000                                                          =
//=   1   0.750000                                                          =
//=   2   0.500000                                                          =
//=   -1      zero                                                          =
//=   & ---------------- END FLAG     &                                     =
//=   -999    zero                                                          =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 mat2iter.c                                                =
//=-------------------------------------------------------------------------=
//=  Execute: mat2iter < in.dat                                             =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (05/19/05) - Genesis                                      =
//===========================================================================
//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf()
#include <stdlib.h>                // Needed for exit() and atof()
#include <math.h>                  // Needed for pow()

//----- Defines -------------------------------------------------------------
#define P_TYPE           0         // P matrix type is 0
#define Q_TYPE           1         // Q matrix type is 1
#define    MAX         100         // Maximum number of states

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  double  in_array[MAX * MAX];     // Input array of matrix values
  char    in_string[1024];         // Input string value
  double  value;                   // Input value
  int     matrix_type;             // Matrix is P_TYPE or Q_TYPE
  int     dim;                     // Matrix dimension (number of states)
  int     i, j;                    // Counters

  // Read in values to in_array[] and determine matrix type
  matrix_type = P_TYPE;
  i = 0;
  while(1)
  {
    scanf("%s", in_string);
    if (feof(stdin)) break;
    in_array[i] = atof(in_string);
    if (in_array[i] < 0.0) matrix_type = Q_TYPE;
    i++;
    if (i > (MAX*MAX))
    {
      printf("*** ERROR - matrix is too large to be processed \n");
      exit(1);
    }
  }

  // Check that this is a square matrix (as it must be)
  dim = (int) sqrt(i);
  if (i != (dim*dim))
  {
    printf("*** ERROR - not a square matrix \n");
    exit(1);
  }

  // Output the matrix type and size per iter.c format rules
  if (matrix_type == P_TYPE)
    printf("P %d \n", dim);
  else
    printf("Q %d \n", dim);

  // Output values in column format per iter.c format rules
  for (i=0; i<dim; i++)
  {
    printf("& ---------------- column #%d    & \n", i);
    for (j=i; j<(dim*dim); j=j+dim)
      if (in_array[j] != 0.0)
        printf("%d   %f \n", (j/dim), in_array[j]);
    printf("-1      zero \n");
  }
  printf("& ---------------- END FLAG     & \n");
  printf("-999    zero \n");
}

