/*
 *  Copyright 2002 by Soos, Antal 
 *  All rights reserved. Property of Soos, Antal. 
 *  Restricted rights to use, duplicate or disclose this code are 
 *  granted through contract.   
 */ 
/***************************************************************************/ 
/*                                                                         */ 
/*     MatrixCal . hpp                                                     */ 
/*                                                                         */ 
/*     Basic C++ standard matrix calculations.                             */ 
/*                                                                         */ 
/*                                                                         */ 
/***************************************************************************/ 
 
#ifndef _MATRIX_CAL_HPP_ 
#define _MATRIX_CAL_HPP_    
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
// 
//		The used include files: 
//-----------------------------------------------------------------------------
#include "mmaccfg.h"  // Generated by the TI_DSP



#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <ctype.h> 
#include <cstdlib>

#include <math.h> 
#include <cmath> // For the sqrt() function  
 

#include <cstdlib> 


#include <std.h>  // for TI_DSP
#include <log.h> // for the LOG_printf()

#include <rtdx.h> // for RTDX functions 
#include <tsk.h>   //for task functions

#include "dsk_lib.h"

//----------------------------------------------------------------------------- 
// 
//		All the locally used definitions 
//----------------------------------------------------------------------------- 
 
#define DEBUG_PRINT_ON 1  //Prints the matrices if 1, for print_matrix() 
 
#define NR_END 1 
#define FREE_ARG char* 
 
#define DIMENSION_MISMACH 10 
#define SMALL_MATRIX_ALOCATION 11 
 
 
#define POSITIV_ZERO (float)1e-15 
#define NEGATIV_ZERO -POSITIV_ZERO 

#define TINY_NUM 1.0e-20;    // A small number.
#define HUGE_NUM 1.0e+20;  // A big number

//----------------------------------------------------------------------------- 
// 
//		The nrutil.h definitions from the Numerical Recepies 
//----------------------------------------------------------------------------- 
 
// ----------------------------------------------------------------------------------------
// nrutil.h::

static float sqrarg;
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)

static float maxarg1,maxarg2;
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ? (maxarg1) : (maxarg2))

static float minarg1,minarg2;
#define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ? (minarg1) : (minarg2))

static long lmaxarg1,lmaxarg2;
#define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ? (lmaxarg1) : (lmaxarg2))

static long lminarg1,lminarg2;
#define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ? (lminarg1) : (lminarg2))

static int imaxarg1,imaxarg2;
#define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ? (imaxarg1) : (imaxarg2))

static int iminarg1,iminarg2;
#define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ? (iminarg1) : (iminarg2))

#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))


//----------------------------------------------------------------------------- 
// 
//		The used Matrix structure definition 
//----------------------------------------------------------------------------- 
 
typedef struct {  // definies the Matrix structure: 
					int	 row;	// number of rowes {1...row} 
					int	 col;	//number of coloms {1...col} 
					float  *mtx;   // the alocated matrix [row x col] 
					int   dim;   // allocated size 
				} Matrix;  
                  		 
//----------------------------------------------------------------------------- 

//----------------------------------------------------------------------------- 
// 
//		The function defined in MatrixCal.cpp 
//----------------------------------------------------------------------------- 
 

void addmat(Matrix *r,Matrix *x,Matrix *y); /* Addition of two matrices */ 
void submat(Matrix *r,Matrix *x,Matrix *y); /*Substraction of two matrices*/ 
void mulmat(Matrix *r,Matrix *x,Matrix *y); /*Multiplixation of two matrices*/ 
void mulmat_t1(Matrix *r, Matrix *x, Matrix *y);/*Multiplixation two matrices*/ 
void mulmat_t2(Matrix *r, Matrix *x, Matrix *y);/*Multiplixation two matrices*/ 
void cmulmat(Matrix *r,float a, Matrix *x); /* Multiplixation with constant*/ 
int  minv(Matrix *r,Matrix *x); /* Matrix inverse calculation r = 1/x  */
int  ludcmp(float *a, int n, int *indx, float *d); /* for minv() LU decomposition */
void lubksb(float *a, int n, int *indx, float b[]); /* for minv() */
void transpose(Matrix *r, Matrix *x );	/* Transpose  of matrice */ 
void squaremat(Matrix *r, Matrix *x); /* Power of 2::  r = x'*I*x */ 
void sqrtmat(Matrix *r, Matrix *x);  /* sqrt of a matrix */ 
void zeromat(Matrix *r);	    /* Zeroes the matrix */ 
void eyemat(Matrix *r);              /* Unity matrix */
void diagn(Matrix *r,int n,float x); /* Diagonal  matrix nxn <- x*/ 


void copymat(Matrix *r, Matrix *x); /* Copy matrix x to r:: r=x; */ 

void concat_row(Matrix *r,Matrix *x,Matrix *y);/*Concatenation of a row matrice*/ 
void concat_col(Matrix *r,Matrix *x,Matrix *y);/*Concatenation of a colum matrice*/ 
void extract_row(Matrix *r,int rs,int re,Matrix *x);/* extract from rs to re rews*/ 
void extract_col(Matrix *r,int cs,int ce,Matrix *x);/* extract from rs to re lolums*/ 

//float random_1(long int *idum);		/* random number generation < (+/- 1) */  

int  ptest(Matrix *x);  /*Positivity test for a square matrix */

float sign(float x); /* return the signum of x:(-1,0,1) */ 

void print_matrix(Matrix *x,char s[80]); /*  usage : printmat(&x,"x"); */ 
void printlog(char s[80]);	/* Mesage printing::usage: printlog("mesage");*/ 


 
//***************************************************************************** 
 
 #endif   // _MATRIX_CAL_HPP_ 




