...
 
Commits (2)
......@@ -305,46 +305,67 @@ void copy_imat_0(int **tab1, int **tab2, int x , int y)
void print_ivec_0(int *vec, int x)
{
int i;
mbs_msg("[");
for(i=0; i<x; i++)
if (vec == NULL || x ==0)
{
mbs_msg("%d ",vec[i]);
mbs_msg("[ ]\n");
}
else
{
int i;
mbs_msg("[");
mbs_msg("]\n");
for (i = 0; i < x; i++)
{
mbs_msg("%d ", vec[i]);
}
mbs_msg("]\n");
}
}
void print_ivec_1(int *vec)
{
int i;
mbs_msg("[");
for(i=1; i<=vec[0]; i++)
if (vec == NULL)
{
mbs_msg("%d ",vec[i]);
mbs_msg("[ ]\n");
}
else
{
int i;
mbs_msg("[");
mbs_msg("]\n");
for (i = 1; i <= vec[0]; i++)
{
mbs_msg("%d ", vec[i]);
}
mbs_msg("]\n");
}
}
void print_imat_0(int **mat, int x , int y)
{
int i,j;
for(i=0; i<x; i++)
if (mat == NULL)
{
mbs_msg("| |\n");
}
else
{
mbs_msg("|");
int i, j;
for(j=0; j<y; j++)
for (i = 0; i < x; i++)
{
mbs_msg("%d ", mat[i][j]);
}
mbs_msg("|");
mbs_msg("|\n");
for (j = 0; j < y; j++)
{
mbs_msg("%d ", mat[i][j]);
}
mbs_msg("|\n");
}
}
}
......@@ -673,53 +694,74 @@ void transpose_dmat_0(double **tab1, double **tab2, int x, int y)
void print_dvec_0(double *vec, int x)
{
int i;
mbs_msg("[");
for(i=0; i<x; i++)
if (vec == NULL || x == 0)
{
mbs_msg("%f ",vec[i]);
mbs_msg("[ ]\n");
}
else
{
int i;
mbs_msg("[");
for (i = 0; i < x; i++)
{
mbs_msg("%f ", vec[i]);
}
mbs_msg("]\n");
mbs_msg("]\n");
}
}
void print_dvec_1(double *vec)
{
int i;
int vec_size;
vec_size = (int) vec[0];
if (vec_size <= 0)
if (vec == NULL)
{
mbs_msg("print_dvec_1 warning: vec[0] is %d !\n", vec_size);
return;
mbs_msg("[ ]\n");
}
else
{
int i;
int vec_size;
mbs_msg("[");
vec_size = (int)vec[0];
for(i=1; i<=vec_size; i++)
{
mbs_msg("%f ",vec[i]);
}
if (vec_size <= 0)
{
mbs_msg("print_dvec_1 warning: vec[0] is %d !\n", vec_size);
return;
}
mbs_msg("[");
for (i = 1; i <= vec_size; i++)
{
mbs_msg("%f ", vec[i]);
}
mbs_msg("]\n");
mbs_msg("]\n");
}
}
void print_dmat_0(double **mat, int x , int y)
{
int i,j;
for(i=0; i<x; i++)
if (mat == NULL)
{
mbs_msg("|");
for(j=0; j<y; j++)
mbs_msg("| |\n");
}
else
{
int i, j;
for (i = 0; i < x; i++)
{
mbs_msg("%f ", mat[i][j]);
mbs_msg("|");
for (j = 0; j < y; j++)
{
mbs_msg("%f ", mat[i][j]);
}
mbs_msg("|\n");
}
mbs_msg("|\n");
}
}
......@@ -763,15 +805,15 @@ void read_dmat_0(double **mat, int x, int y, char *name)
}
i = 0;
// read the file line by line
while (fgets(line, 100 * y + 1, file_in) != NULL)
while (i<x && fgets(line, 100 * y + 1, file_in) != NULL)
{
// for each line, split the string in several tokens.
token = strtok(line, "\t");
token = strtok(line, " \t");
j = 0;
while (token != NULL)
{
mat[i][j] = atof(token);
token = strtok(NULL, "\t");
token = strtok(NULL, " \t");
j++;
}
i++;
......@@ -955,3 +997,196 @@ void zeros_dmat_0(double **mat, int x, int y)
}
//----------------------------------------------------------------
// Function definition to read a matrix
//----------------------------------------------------------------
void readmatrix(double** mat, int* n)
{
int i, j;
int err = 0;
mbs_msg("\tEnter the no: rows in the matrix : ");
err = scanf("%d", &n[0]);
mbs_msg("\tEnter the no: columns in the matrix : ");
err = scanf("%d", &n[1]);
mbs_msg("\tEnter The Matrix Elements Row wise ");
err = scanf("\n");
for (i = 0; i < n[0]; ++i)
{
for (j = 0; j < n[1]; ++j)
{
err = scanf("%lf", &mat[i][j]);
}
}
}
//------------------------------------------------------------------
// Function definition to Update initzeros array
//-----------------------------------------------------------------
void update_initzeros(int* initzeros, double** matrix, int* n)
{
int zcount, i, j;
for (i = 0; i < n[0]; ++i)
{
zcount = 0;
for (j = 0; (matrix[i][j] == 0) && (j < n[1]); ++j)
{
++zcount;
}
initzeros[i] = zcount;
}
}
//-------------------------------------------------------------------
// Function definition to arrange matrix
//-------------------------------------------------------------------
void arrange_matrix(double** matrix, int* n, int* initzeros)
{
int l, reqrow, i, k, lastrow, tempvar, large;
double* rowtemp;
rowtemp = get_dvec_0(n[1]);
lastrow = n[0] - 1;
for (l = 0; l < n[0]; ++l)
{
large = initzeros[0];
for (i = 0; i < n[0]; ++i)
{
if (large <= initzeros[i])
{
large = initzeros[i];
reqrow = i;
}
}
initzeros[reqrow] = -1;
tempvar = initzeros[reqrow];
initzeros[reqrow] = initzeros[lastrow];
initzeros[lastrow] = tempvar;
for (k = 0; k < n[1]; ++k)
{
rowtemp[k] = matrix[lastrow][k];
}
for (k = 0; k < n[1]; ++k)
{
matrix[lastrow][k] = matrix[reqrow][k];
}
for (k = 0; k < n[1]; ++k)
{
matrix[reqrow][k] = rowtemp[k];
}
lastrow = lastrow - 1;
}
free_dvec_0(rowtemp);
}
//---------------------------------------------------------------------------
// Function definition to scale a matrix
//---------------------------------------------------------------------------
void scale_matrix(double** matrix, int* n, int* initzeros)
{
int i, j;
double divisor;
for (i = 0; i < n[0]; ++i)
{
divisor = matrix[i][initzeros[i]];
for (j = initzeros[i]; j < n[1]; ++j)
{
matrix[i][j] = matrix[i][j] / divisor;
}
}
}
//---------------------------------------------------------------------------
// Function definition to obtain a sub matrix without ligne 1 and colum x
//---------------------------------------------------------------------------
void sub_matrix(double** a, int n, int x, int y, double** a1) {
int i, j;
int k = 0;
int m = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i != x && j != y) {
a1[m][k] = a[i][j];
k++;
}
}
k = 0;
if (i != x) {
m++;
}
}
}
//---------------------------------------------------------------------------
// Function definition to compute the determinant of a matrix
//---------------------------------------------------------------------------
/*
* a : matrix
* n : matrix's size
*/
double det_dmatrix_0(double** a, int n) {
int i, j, k, index;
double ratio, swap, det = 1.0, p = 1.0;
for (i = 0; i < n - 1; i++)
{
index = i;
while (a[index][i] == 0.0) {
index++;
if (index == n) {
index = 0;
// the determinat of the matrix is zero
while (a[index][i] == 0.0 && index < i) {
index++;
}
// if all the colone is null det(a) = 0
if (index == i) {
return 0.0;
}
// if not nothing to do with this i
else {
continue;
}
}
}
if (index != i)
{
//loop for swaping the diagonal element row and index row
for (j = 0; j < n; j++)
{
swap = a[index][j];
a[index][j] = a[i][j];
a[i][j] = swap;
}
//determinant sign changes when we shift rows
//go through determinant properties
p = -p;
}
for (j = i + 1; j < n; j++)
{
ratio = a[j][i] / a[i][i];
for (k = i; k < n; k++)
{
a[j][k] = a[j][k] - ratio * a[i][k];
}
}
det *= a[i][i];
}
return p * det*a[n - 1][n - 1];
}
......@@ -366,10 +366,12 @@ void print_dmat_0(double **mat, int x, int y);
void save_dvec_0(double *vec, int x,char *name);
/*! \brief read a [x times y] matrix of doubles from a file starting with index 0 and store the values in a matrix provided by the user.
* The first x line of the file are loaded, other lines are ignored.
*
* \param[in] x nb of rows
* \param[in] y nb of columns
* \param[in] name the path an name of the file in which reading the matrix
* \param[in,out] mat allocated array for storing the file
*/
void read_dmat_0(double **mat, int x, int y, char *name);
......@@ -483,6 +485,56 @@ void zeros_dvec_0(double *vec, int x);
*/
void zeros_dvec_1(double *vec);
/*! \brief function to read a matrix (obsolete?)
*
* \param[in,out] the matrix
* \param[in] the vector n
*/
void readmatrix(double** matrix, int* n);
/*! \brief function to update the initzeros array (obsolete?)
*
* \param[in,out] the vector to initialize
* \param[in,out] the matrix
* \param[in] the vector n
*/
void update_initzeros(int* initzeros, double** matrix, int* n);
/*! \brief function to arrange matrix (obsolete?)
*
* \param[in,out] the matrix
* \param[in] the vector n
* \param[in,out] the vector to arrange
*/
void arrange_matrix(double** matrix, int* n, int* initzeros);
/*! \brief function to scale matrix (obsolete?)
*
* \param[in,out] the matrix
* \param[in] the vector n
* \param[in,out] the vector to arrange
*/
void scale_matrix(double** matrix, int* n, int* initzeros);
/*! \brief function to obtain a sub matrix without line x and colum y
*
* \param[in,out] the matrix
* \param[in] the size of the matrix nxn
* \param[in] the number of the line to skip
* \param[in] the number of the colum to skip
* \param[in, out] sub-matrix
*/
void sub_matrix(double** a, int n, int x, int y, double** a1);
/*! \brief function to compute the determinant of a matrix
*
* \param[in,out] the matrix
* \param[in] the size of the matrix nxn
* \return the determinant of this matrix
*/
double det_dmatrix_0(double** a, int n);
/*
* Print values of mbs_data (used for debug)
*/
......@@ -490,9 +542,6 @@ void zeros_dvec_1(double *vec);
extern "C" {
#endif
void mbs_print_data(MbsData *mbs_data);
void print_vector(char* prefix, int n, double *vec);
void print_intvector(char* prefix, int n, int *vec);
void print_matrix(char* prefix, int r, int c, double **mat);
void mbs_copy_data(MbsData* s, MbsData* s_copy);
/*! \brief Reset all fields of a MbsData structure to initial values
*
......