/*
 * Copyright (c) 2005, University of Massachusetts
 * All Rights Reserved.
 * 
 * Name:           gausselim_support.c
 * Last modified:  30 Mar 2005
 * Author:         Stephen Siegel <siegel@cs.umass.edu>
 *
 * This file supplies C support functions for the composite
 * model for the Gaussian elimination algorithm.
 */

#include "seqpar.c"

extern void prettyprintMatrix();

int widths[NUM_ENTRIES];
int column_widths[NUM_COLS];


void initializeSystem() {
  int i;

  init_seqpar();
  for (i=0; i<NUM_ENTRIES; i++) {
    now.matrix_seq[i] = SYM_symbolicConstant_0(i);
  }
  printf("Starting verification of gausselim.\n\n");
  printf("Original Matrix: \n\n");
  prettyprintMatrix();
  printf("-------------------------------");
  printf("--------------------------------------\n\n");
}



void printSolution() {
  update_seqpar();
#if MPI_VERBOSE>0
  printf("\nMatrix in reduced row-echelon form:\n\n");
  prettyprintMatrix();
#endif
}

    

void calculateWidths() {
  int col, row, expr, width;
  
  for (col = 0; col < NUM_COLS; col++) {
    column_widths[col] = 0;
    for (row = 0; row < NUM_ROWS; row++) {
      expr = now.matrix_seq[row*NUM_COLS+col];
      width = SYM_exprLength(expr);
      widths[row*NUM_COLS+col] = width;
      if (width>column_widths[col]) {
	column_widths[col] = width;
      }
    }
    column_widths[col]++;
  }
}
 
   
void prettyprintMatrix() {
  int row, col, spaces, i;
  
  calculateWidths();
  for (row = 0; row < NUM_ROWS; row++) {
    for (col = 0; col < NUM_COLS; col++) {
      SYM_printExpr(now.matrix_seq[row*NUM_COLS+col]);
      if (col < NUM_COLS-1) {
	spaces = column_widths[col]-widths[row*NUM_COLS+col];
	for (i = 0; i < spaces; i++) printf(" ");
      }
    }
    printf("\n");
  }
  printf("\n");
}


void report_par_error(int row, int col, int seqExpr, int parExpr) {
  incr_error();
  printf("\nParralel solution matrix differs in row %d column %d:\n\n",
         row, col);
  SYM_printExpr(seqExpr);
  printf("\n\n");
  SYM_printExpr(parExpr);
  printf("\n");
}


void report_seq_error() {
  printf("\nSequential solution matrix may NOT be in reduced\n");
  printf("row echelon form:\n\n");
  SYM_printPathCondition(now.pc);
  printf("\n");
  prettyprintMatrix();
}
