// this matrix_h09.h file has the file I/O subroutines.


#include <stdio.h>


#define MAX_SIZE 128

void matrix_print(FILE *outfile, MAT *array, char *title);


//	 writes a title.
//	 the file must be open for writing and pre-positioned.

void matrix_write_title(FILE *event_data_file, unsigned long code, char *title);


//	 reads a title.
//	 the file must be open for writing and pre-positioned.

int matrix_read_title(FILE *event_data_file, unsigned long code, char *title);


//	 writes an event data-file header.
//	 the file must be open for writing and pre-positioned.

void matrix_write_event_data_header(FILE *event_data_file, char *title, unsigned long left_size,
								   unsigned long right_size, unsigned long multiplier,
								   unsigned long amount);


//	 writes a single row of an event data-file data.
//	 the file must be open for writing and pre-positioned.

void matrix_write_event_data_row(FILE *event_data_file, unsigned long left_size,
								   unsigned long right_size, double *xx, double *yy,
								   unsigned long multiplier);


//	writes a pseudo-random event data-file.  the title string must not contain any spaces.
//	the file must be open for writing and pre-positioned.

int matrix_write_event_data(FILE *event_data_file, char *title,
							unsigned long left_size, unsigned long right_size,
							unsigned long multiplier, unsigned long amount);


//	reads an event data-file header.
//	the file must be open for reading and pre-positioned.

int matrix_read_event_data_header(FILE *event_data_file, char *title, unsigned long *left_size,
								   unsigned long *right_size, unsigned long *multiplier,
								   double *divisor, unsigned long *amount);


//	 reads a single row of an event data-file data.
//	 the file must be open for reading and pre-positioned.

void matrix_read_event_data_row(FILE *event_data_file, unsigned long size,
								double *vect_x, double divisor);


//	reads an event data-file.  yields the title, variance, and average.
//	the file must be open for reading and pre-positioned.

int matrix_read_event_data(FILE *event_data_file, char *title, MAT *array, MAT *average);


//	writes a matrix to a file, which must be open for writing and pre-positioned.
//	the title string must not contain any spaces.

int matrix_write_array(FILE *array_file, char *title, unsigned long multiplier,
					   MAT *array, MAT *scratch_1);


//	reads a matrix from a file, which must be open for reading and pre-positioned.
//	yields a title and an array.

int matrix_read_array(FILE *array_file, char *title, MAT *array);


//	writes a complex matrix to a file, which must be open for writing and pre-positioned.
//	the title string must not contain any spaces.

int matrix_write_complex_array(FILE *array_file, char *title, unsigned long multiplier,
					   matrix_cmplx *array, MAT *scratch_1, MAT *scratch_2,
					   MAT *scratch_3, MAT *scratch_4, MAT *scratch_5);


//	reads a complex matrix from a file, which must be open for reading and pre-positioned.
//	yields a title and an array.

int matrix_read_complex_array(FILE *array_file, char *title, matrix_cmplx *array,
							  MAT *scratch_1, MAT *scratch_2, MAT *scratch_3, MAT *scratch_4,
							  MAT *scratch_5, MAT *scratch_6, MAT *scratch_7);

//	reads an event_data_file_x.  yields the title_x.
//	performs the linear transformation y = (x - average_x) * array + average_y.
//	writes a new event_data_file_y, with the provided title_y.
//	within the scope of Linear Algebra, the array should be a positive definite
//		partitioned symmetric matrix; but, this subroutine will accept any array.
//		also, the partitioning should correspond to that of the event_data_file_x.
//	the file event_data_file_x must be open for reading and pre-positioned.
//	the file event_date_file_y must be open for writing and pre-popitioned.
//	will return an error-code of 64 if the left_size + right_size of the event_data_file_x
//		is not equal to the jmx (horizontal size) of the average_x.

int matrix_read_process_event_data1(FILE *event_data_file_x, char *title_x,
									MAT *average_x, MAT *array, MAT *average_y,
									FILE *event_data_file_y, char *title_y,
									MAT *scratch);


//	reads an event_data_file_x.  yields the title_x.
//	performs the linear transformation y = (x - average_x) * array + average_y.
//	writes the regression as a new event_data_file_y, with the provided title_y and
//		the residue as a new event_data_file_r, with the provided title_r.
//	within the scope of Linear Algebra, the array should be a positive definite
//		symmetric matrix; but, this subroutine will accept any array.
//	the file event_data_file_x must be open for reading and pre-positioned.
//	the file event_date_file_y must be open for writing and pre-popitioned.
//	the file event_date_file_r must be open for writing and pre-popitioned.
//	will return an error-code of 64 if either the left_size of the event_data_file_x
//		is not equal to the jmx (horizontal size) of the average_x or
//		the right_size of the event_data_file_x
//		is not equal to the jmx (horizontal size) of the average_y.
//	sw:  0=x to y, 1=y to x.
int matrix_read_process_event_data2(FILE *event_data_file_x, char *title_x,
									MAT *average_x, MAT *array, MAT *average_y,
									unsigned long sw,
									FILE *event_data_file_y, char *title_y,
									FILE *event_data_file_r, char *title_r,
									MAT *scratch);


//	from the positive-definite symmetric-matrix variance_in and horizontal-vector average_in,
//	generates or constructs the ellipsoidal events file.
//	then, computes its variance and average.
//	also, checks this variance matrix to be symmetric --> residue_symmetric,
//	this variance to be equal to the variance_in --> relative_var, and
//	this average to be equal to the average_in.
//	if construct is zero, will only generate; if one, will actually construct.
//	the average_in must be a horizontal-vector of the same size and partitioning as the variance_in.
//	to be usable directly as an event data-set for a channel, the partitioning must be in the middle.
int matrix_ellipsoidal_events(MAT *array_var_in, MAT *array_ave_in, unsigned long construct,
							  unsigned long multiplier, unsigned long amount,
							  char *ellipsoidal_events_name, char *ellipsoidal_events_title,
							  MAT *array_var, MAT *array_ave,
							  double *residue_symmetric,
							  double *relative_var, double *relative_ave,
							  char *scratch_events_name, char *scratch_events_title,
							  MAT *scratch_1, MAT *scratch_2, MAT *scratch_3, MAT *scratch_4,
							  MAT *scratch_5, MAT *scratch_6, MAT *scratch_7);


//	employing the resources of the channel, performs a linear-regression upon the
//		ellipsoidal_events.  writes the regression into the regress_events and
//		the residues into the residues.
//	then, analyzes this residue file, to obtain its variance and average.
//	also, checks the variance to be symmetric --> residue_symmetric and
//		the norm of the average.
//	finally, computes the channel-rate R (and the corresponding Theta and rho) of the variance.
//	sw:  0=forward x to y, 1=forward y to x, 2=inverse x to y, 3=inverse y to x.
//	for the results to be meaningful,
//		the channel should be that obtained by analyzing the events or a relative.
int matrix_events_regress(CHN *chan, char *ellipsoidal_events_name, char *regress_events_name,
						  char *regress_events_title, char *residues_name, char *residues_title,
						  unsigned long sw, MAT *array_var, double *residue_symmetric,
						  double *R, double *Theta, double *rho,
						  MAT *array_ave, double *ave_norm, MAT *scratch_1, MAT *scratch_2,
						  MAT *scratch_3, MAT *scratch_4, MAT *scratch_5, MAT *scratch_6,
						  MAT *scratch_7);




// copyright (c) 2004 by R.I. 'Scibor-Marchocki.  last modified Monday 03-rd May 2004.

// eof


