Ejemplo n.º 1
0
NT2_TEST_CASE_TPL ( rref, NT2_REAL_TYPES)
{
  typedef typename nt2::meta::as_integer<T, signed>::type  itype_t;
  typedef nt2::table<itype_t> it_t;
  typedef nt2::table<T> table_t;
  T A[16] = {
   16,    2,    3,   13,
    5,   11,   10,    8,
    9,    7,    6,   12,
   4,   14,   15,    1};
  table_t b = nt2::ones(4, 4, nt2::meta::as_<T>())
                + T(10)*nt2::eye(4, 4, nt2::meta::as_<T>());
  table_t rref, l, u, p;
  table_t a(nt2::of_size(4, 4));
  int k = 0;
  for(int i=1; i <= 4; ++i)
    {
      for(int j=1; j <= 4; ++j)
        {
          a(i, j) = A[k++];
        }
    }
  NT2_DISPLAY(a);
  nt2::tie(rref) = nt2::rref(a);
  NT2_DISPLAY(rref);
  it_t jb;

  nt2::tie(rref, jb) = nt2::rref(a);
  NT2_DISPLAY(rref);
  NT2_DISPLAY(jb);
  NT2_TEST(nt2::isulpequal(rref(nt2::_(1, nt2::numel(jb)), jb), nt2::eye(nt2::numel(jb), nt2::meta::as_<T>())));

}
/*
  Column-reduced echelon form.
*/
void cref(int *mat, int rows, int cols, int w)
{
  int* mT = (int*)malloc(sizeof(int) * rows * cols);
  transpose(mat, mT, rows, cols);
  rref(mT, cols, rows, w);
  transpose(mT, mat, cols, rows);
  free(mT);
}
Ejemplo n.º 3
0
void main(){
	double **mx;
	int m,n;
	printf("Enter m and n ");
	scanf("%i %i", &m, &n);
	
	//get space
	mx = malloc(m*sizeof(double *));
	for(int i=0; i<m; i++)
		mx[i] = malloc(n*sizeof(double));
		
	scanMatrix(mx, m, n);
	rref(mx, m, n);
	printMatrix(mx , m, n);
}
Ejemplo n.º 4
0
void cGetRREFCommand::Execute()
{
	if((1 != m_Expression.terms.size()) || (1 != m_Expression.terms[0].factors.size()))
		throw std::runtime_error("Invalid input. Provide a valid matrix to compute the RREF.");
	cMatrix *mat = boost::get<cMatrix >(&(m_Expression.terms[0].factors[0].factor));
	if(nullptr == mat)
		throw std::runtime_error("Invalid input. Provide a valid matrix to compute the RREF.");

	double double_cols_no = mat->elements.size()/mat->rows_no;
	std::size_t cols_no = mat->elements.size()/mat->rows_no;
	if(double_cols_no != std::floor(double_cols_no))
		throw std::runtime_error("Invalid input. Provide a valid matrix to compute the RREF.");

	//add the matrix stored in row major order
	m_Result.resize(mat->rows_no, cols_no);
	for(std::size_t rows_idx = 0; rows_idx < mat->rows_no; rows_idx++)
		for(std::size_t cols_idx = 0; cols_idx < cols_no; cols_idx++)
			m_Result(rows_idx, cols_idx) = mat->elements[cols_idx + rows_idx*cols_no];

	rref(m_Result);
}
Ejemplo n.º 5
0
void crank(double ***matrix, double ***matrix_np1, int nx, int ny, int nz, double alpha, double dx, double C, int border, int NT){
    int i, j, k, l, f, g, h;
    double d;
    int b0;
    int bn;
    int when = 1;
    double ***temp;
    double *tempb;
    double *mat_b = (double*) malloc(sizeof(double)* nx*ny*nz);
    double *mat_newb = (double*) malloc(sizeof(double)* nx*ny*nz);
    double **mat_A = (double**) malloc(sizeof(double*) *nx*ny*nz);
    double **augmented = (double**) malloc(sizeof(double*) *nx*ny*nz);
    double *data = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz);
    double *augdata = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz+nx*ny*nz);/*care for last column - b*/
	    /*now need to make augmented matrix to pass into rref*/
	    for(h=0;h<nx*ny*nz;h++){
		*(augmented+h) = augdata + h*nx*ny*nz+1;/*care for augmented with b*/
	    }
    switch(border){
	case 0:
	    b0 = 0;
	    bn = 0;
	    break;
	case 1:
	    b0 = 3;
	    bn = 3;
	    break;
	case 2:
	    b0 = nx;
	    bn = 0;
	    break;
	default :
	    b0 = 0;
	    bn = 0;
    }
    print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
    when++;
    /*malloc space for A*/
    for(i=0;i<nx*ny*nz;i++){
	*(mat_A+i) = data + i*nx*ny*nz;
    }
    /*make matrix b which is 1-D from init 3-D matrix --flattened*/
    for(i = 0 ; i< nx ; i++){
	for(j = 0 ; j < ny ; j++){
	    for(k = 0 ; k < nz ; k++){
		*(mat_b + i*ny*nz + j*nz + k) = *(*(*(matrix + i) + j) +k);
	    }
	}
    }

    for(k=0;k<NT;k++){


	for(i = 0; i< nx*ny*nz ; i++){
	    for(j = 0;j< nx*ny*nz ; j++){
		if( i == j){
		    *(*(mat_A+i)+j) = /*mat_b[i]**/1+6.0*C;
		}	
		else if (i == j-1 ){
		    if( i != 0%nz){
			*(*(mat_A+i)+j) = /**(mat_b + i-1)**/(-1.0*C);
		    }
		    else if(i ==0%nz && border== 2){
			*(*(mat_A+i)+j) = /**(mat_b + (i-1)%nz)**/(-1.0*C);

		    }

		}
		else if (i == j+1){
		    if( i != (nz-1)%(nz-1)){
			*(*(mat_A+i)+j) =/* *(mat_b + i+1)**/(-1.0*C);
		    }
		    else if(i == (nz-1)%(nz-1) && border == 2){
			*(*(mat_A+i)+j) =/* *(mat_b + (i+1)%nz)**/(-1.0*C);

		    }

		}
		else if(i == j-ny*nz){
		    if( i > ny*nz){
			*(*(mat_A+i)+j) =/* *(mat_b + i-ny*nz)**/(-1.0*C);
		    }
		    else if(i <= ny*nz && border == 2){
			*(*(mat_A+i)+j) =/* *(mat_b + (i-ny*nz)%(nx*ny*nz))**/(-1.0*C);

		    }
		}
		else if(i == j+ny*nz){
		    if( i < ny*nz){
			*(*(mat_A+i)+j) = /**(mat_b + i+ny*nz)**/(-1.0*C);
		    }
		    else if(i >= ny*nz && border == 2){
			*(*(mat_A+i)+j) = /**(mat_b + (i+ny*nz)%(nx*ny*nz))**/(-1.0*C);

		    }

		}
		else if(i == j+ny){
		    if( i != (ny-1)%(ny-1)){
			*(*(mat_A+i)+j) =/* *(mat_b + i+ny)**/(-1.0*C);
		    }
		    else if(i ==(ny-1)%(ny-1) && border == 2){
			*(*(mat_A+i)+j) =/* *(mat_b + (i+ny)%ny)**/(-1.0*C);

		    }

		}
		else if(i == j-ny){
		    if( i != 0%ny){
			*(*(mat_A+i)+j) =/* *(mat_b + i-ny)**/(-1.0*C);
		    }
		    else if(i == 0%ny && border == 2){
			*(*(mat_A+i)+j) =/* *(mat_b + (i-ny)%ny)**/(-1.0*C);

		    }

		}
		else{
		    *(*(mat_A+i)+j) = 0.0;

		}
	    }

	    /*at this point A is created*/
	    for(g = 0; g< nx*ny*nz ; g++){
		for(h = 0;h< nx*ny*nz ; h++){
		    if(h == nx*ny*nz-1){
			*(*(augmented+g)+h) = *(mat_b+g);
		    }
		    else{
			*(*(augmented+g)+h) = *(*(mat_A+g)+h);
		    }
		}
	    }
	    /*at this point i have Ax=b in augmented matrix form to use rref on {augmented | b}  */
	    rref(augmented, nx*ny*nz,  nx*ny*nz+1);
	    /*now i need to extract the nx*ny*nz+1 column to cpy back to my old mat_b, mat_A*/
	    for(g = 0; g< nx*ny*nz ; g++){
		for(h = 0;h< nx*ny*nz ; h++){
		    if(h == nx*ny*nz-1){
			*(mat_newb+g)  = *(*(augmented+g)+h);
		    }
	/*	    else{
			*(*(mat_A+g)+h) = *(*(augmented+g)+h);
		    }*/
		}
	    }
	    d = distanceFormula(mat_newb, mat_b, nx*ny*nz);
	    //printf("%f \n",d);
	    tempb = mat_b;
	    mat_b = mat_newb;
	    mat_newb = tempb;

	    for(f = 0 ; f< nx ; f++){
		for(g = 0 ; g < ny ; g++){
		    for( h = 0 ; h < nz ; h++){
			*(*(*(matrix + f) + g) +h) =  *(mat_b + f*ny*nz + g*nz + h);
		    }
		}
	    }
	}
	if( k==0){
	    print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
	    when++;
	}
    }
}
Ejemplo n.º 6
0
void adi(double ***matrix, double ***matrix_np1, int nx, int ny, int nz, double alpha, double dx, double C, int border, int NT){
    int i, j, k, l, f, g, h;
    int dir;
    int when =1;
    double d;
    double ***temp;
    double *tempb;
    double *mat_b = (double*) malloc(sizeof(double)* nx*ny*nz);
    double *mat_newb = (double*) malloc(sizeof(double)* nx*ny*nz);
    double **mat_A = (double**) malloc(sizeof(double*) *nx*ny*nz);
    double **augmented = (double**) malloc(sizeof(double*) *nx*ny*nz);
    double *data = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz);
    double *augdata = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz+nx*ny*nz);/*care for last column - b*/
    print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
    when++;
	    /*now need to make augmented matrix to pass into rref*/
	    for(h=0;h<nx*ny*nz;h++){
		*(augmented+h) = augdata + h*nx*ny*nz+1;/*care for augmented with b*/
	    }

    /*malloc space for A*/
    for(i=0;i<nx*ny*nz;i++){
	*(mat_A+i) = data + i*nx*ny*nz;
    }
    /*make matrix b which is 1-D from init 3-D matrix --flattened*/
    for(i = 0 ; i< nx ; i++){
	for(j = 0 ; j < ny ; j++){
	    for(k = 0 ; k < nz ; k++){
		*(mat_b + i*ny*nz + j*nz + k) = *(*(*(matrix + i) + j) +k);
	    }
	}
    }
    /*run in each direction, without X, then without Y, then without Z, so A is made up of 3 different 2 D variations*/
    for(k=0;k<NT;k++){
	for(dir=0;dir<3;dir++){
	    switch(dir){
		case 0:
		    for(i = 0; i< nx*ny*nz ; i++){
			for(j = 0;j< nx*ny*nz ; j++){
			    if( i == j){
				*(*(mat_A+i)+j) = /*mat_b[i]**/1+4.0*C;
			    }	
			    else if (i == j-1){
				if( i != 0%nz){
				    *(*(mat_A+i)+j) = /**(mat_b + i-1)**/(-1.0*C);
				}
				else if(i ==0%nz && border== 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i-1)%nz)**/(-1.0*C);

				}

			    }
			    else if (i == j+1){
				if( i != (nz-1)%(nz-1)){
				    *(*(mat_A+i)+j) = /**(mat_b + i+1)**/(-1.0*C);
				}
				else if(i == (nz-1)%(nz-1) && border == 2){
				    *(*(mat_A+i)+j) =/* *(mat_b + (i+1)%nz)**/(-1.0*C);

				}

			    }
			    else if(i == j+ny){
				if( i != (ny-1)%(ny-1)){
				    *(*(mat_A+i)+j) = /**(mat_b + i+ny)**/(-1.0*C);
				}
				else if(i ==(ny-1)%(ny-1) && border == 2){
				    *(*(mat_A+i)+j) =/* *(mat_b + (i+ny)%ny)**/(-1.0*C);

				}

			    }
			    else if(i == j-ny){
				if( i != 0%ny){
				    *(*(mat_A+i)+j) = /**(mat_b + i-ny)**/(-1.0*C);
				}
				else if(i == 0%ny && border == 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i-ny)%ny)**/(-1.0*C);

				}

			    }
			    else{
				*(*(mat_A+i)+j) = 0.0;

			    }
			}
		    }
		    break;
		case 1:
		    for(i = 0; i< nx*ny*nz ; i++){
			for(j = 0;j< nx*ny*nz ; j++){
			    if( i == j){
				*(*(mat_A+i)+j) =/* mat_b[i]**/1+4.0*C;
			    }	
			    else if (i == j-1){
				if( i != 0%nz){
				    *(*(mat_A+i)+j) = /**(mat_b + i-1)**/(-1.0*C);
				}
				else if(i ==0%nz && border== 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i-1)%nz)**/(-1.0*C);

				}

			    }
			    else if (i == j+1){
				if( i != (nz-1)%(nz-1)){
				    *(*(mat_A+i)+j) = /**(mat_b + i+1)**/(-1.0*C);
				}
				else if(i == (nz-1)%(nz-1) && border == 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i+1)%nz)**/(-1.0*C);

				}

			    }
			    else if(i == j-ny*nz){
				if( i > ny*nz){
				    *(*(mat_A+i)+j) = /**(mat_b + i-ny*nz)**/(-1.0*C);
				}
				else if(i <= ny*nz && border == 2){
				    *(*(mat_A+i)+j) =/* *(mat_b + (i-ny*nz)%(nx*ny*nz))**/(-1.0*C);

				}

			    }
			    else if(i == j+ny*nz){
				if( i < ny*nz){
				    *(*(mat_A+i)+j) = /**(mat_b + i+ny*nz)**/(-1.0*C);
				}
				else if(i >= ny*nz && border == 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i+ny*nz)%(nx*ny*nz))**/(-1.0*C);

				}

			    }
			    else{
				*(*(mat_A+i)+j) = 0.0;

			    }
			}
		    }
		    break;
		case 2:
		    for(i = 0; i< nx*ny*nz ; i++){
			for(j = 0;j< nx*ny*nz ; j++){
			    if( i == j){
				*(*(mat_A+i)+j) = /*mat_b[i]**/1+4.0*C;
			    }	
			    else if(i == j-ny*nz){
				if( i > ny*nz){
				    *(*(mat_A+i)+j) = /**(mat_b + i-ny*nz)**/(-1.0*C);
				}
				else if(i <= ny*nz && border == 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i-ny*nz)%(nx*ny*nz))**/(-1.0*C);

				}

			    }
			    else if(i == j+ny*nz){
				if( i < ny*nz){
				    *(*(mat_A+i)+j) = /**(mat_b + i+ny*nz)**/(-1.0*C);
				}
				else if(i >= ny*nz && border == 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i+ny*nz)%(nx*ny*nz))**/(-1.0*C);

				}

			    }
			    else if(i == j+ny){
				if( i != (ny-1)%(ny-1)){
				    *(*(mat_A+i)+j) = /**(mat_b + i+ny)**/(-1.0*C);
				}
				else if(i ==(ny-1)%(ny-1) && border == 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i+ny)%ny)**/(-1.0*C);

				}

			    }
			    else if(i == j-ny){
				if( i != 0%ny){
				    *(*(mat_A+i)+j) = /**(mat_b + i-ny)**/(-1.0*C);
				}
				else if(i == 0%ny && border == 2){
				    *(*(mat_A+i)+j) = /**(mat_b + (i-ny)%ny)**/(-1.0*C);

				}

			    }
			    else{
				*(*(mat_A+i)+j) = 0.0;

			    }
			}
		    }
		    break;

	    }



	    /*at this point A is created*/
	    for(g = 0; g< nx*ny*nz ; g++){
		for(h = 0;h< nx*ny*nz ; h++){
		    if(h == nx*ny*nz-1){
			*(*(augmented+g)+h) = *(mat_b+g);
		    }
		    else{
			*(*(augmented+g)+h) = *(*(mat_A+g)+h);
		    }
		}
	    }
	    /*at this point i have Ax=b in augmented matrix form to use rref on {augmented | b}  */
	    rref(augmented, nx*ny*nz,  nx*ny*nz+1);
	    /*now i need to extract the nx*ny*nz+1 column to cpy back to my old mat_b, mat_A*/
	    for(g = 0; g< nx*ny*nz ; g++){
		for(h = 0;h< nx*ny*nz ; h++){
		    if(h == nx*ny*nz-1){
			*(mat_newb+g)  = *(*(augmented+g)+h);
		    }
		 /*   else{
			*(*(mat_A+g)+h) = *(*(augmented+g)+h);
		    }*/
		}
	    }
	    tempb = mat_b;
	    mat_b = mat_newb;
	    mat_newb = tempb;

	}

	for(f = 0 ; f< nx ; f++){
	    for(g = 0 ; g < ny ; g++){
		for( h = 0 ; h < nz ; h++){
		    *(*(*(matrix + f) + g) +h) =  *(mat_b + f*ny*nz + g*nz + h);
		}
	    }
	}
	if(k == 1){
	    print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
	    when++;
	}
    }

}