//================================================== file = qmat2pmat.c =====
//=  Convert a text matrix input in Q type to P type                        =
//===========================================================================
//=  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) Matrix is a vector where matrix element [i,j] is (i*dim+j)        =
//=    5) 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:                                                  =
//=                                                                         =
//=  -3.0   3.0   0.0                                                       =
//=   6.0 -10.0   4.0                                                       =
//=   0.0   5.0  -5.0                                                       =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat"):                                    =
//=                                                                         =
//=   0.700000  0.300000  0.000000                                          =
//=   0.600000  0.000000  0.400000                                          =
//=   0.000000  0.500000  0.500000                                          =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 qmat2pmat.c                                               =
//=-------------------------------------------------------------------------=
//=  Execute: qmat2pmat < in.dat                                            =
//=-------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (06/16/05) - Genesis (from iter.c)                        =
//===========================================================================
//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf()
#include <stdlib.h>                // Needed for exit() and atof()
#include <math.h>                  // Needed for sqrt()

//----- Defines -------------------------------------------------------------
#define    MAX         100         // Maximum number of states

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  double  p[MAX * MAX];            // Array of P matrix values
  double  q[MAX * MAX];            // Array of Q matrix values
  char    in_string[1024];         // Input string value
  int     dim;                     // Matrix dimension (number of states)
  double  max;                     // Maximum value
  double  sum;                     // Sum value
  int     i, j;                    // Counters

  // Read in values to q[]
  i = 0;
  while(1)
  {
    scanf("%s", in_string);
    if (feof(stdin)) break;
    q[i] = atof(in_string);
    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);
  }

  // *** Convert the Q matrix to a P matrix ***

  // Find max flow for a Q matrix row
  max = 0.0;
  for (i=0; i<dim; i++)
    for (j=0; j<dim; j++)
      if (max < -q[i*dim+j]) max = -q[i*dim+j];
    if (max <= 0.0)
  {
    printf("*** ERROR - max flow is zero or less \n");
    exit(1);
  }

  // Divide all terms by max
  for (i=0; i<dim; i++)
    for (j=0; j<dim; j++)
      p[i*dim+j] = q[i*dim+j] / max;

  // Sum-up row probabilites except diagonal, subtract from one and put
  //   difference in diagonal
  for (i=0; i<dim; i++)
  {
    sum = 0.0;
    for (j=0; j<dim; j++)
      if (i != j) sum = sum + p[i*dim+j];
    p[i*dim+i] = 1.0 - sum;
  }

  // Output the P matrix
  for (i=0; i<dim; i++)
  {
    for (j=0; j<dim; j++)
      printf("%f  ", p[i*dim+j]);
    printf("\n");
  }
}

