Exemplo n.º 1
0
int main(int argc, char const *argv[]) {
  unsigned D;             // Numero de paginas. (Dimensão da matriz)
  double dampingFactor;   // Fator de Damping
  double **matriz = NULL; // Matriz a ser alocada.
  double **saida = NULL;  // Matriz de saida.
  unsigned i, j;          // Posições onde a matriz sera setada para 1

  scanf("%u %lf", &D, &dampingFactor);
  matrixAlloc(&matriz, D, D);

  while (scanf("%u %u", &i, &j) == 2) {
    matriz[i][j] = 1;
  }

  stochasticMatrix(matriz, D, D);
  dampMatrix(matriz, dampingFactor, D, D);

  copyMatrix(&saida, matriz, D, D);
  expontiationUntilConverge(&saida, matriz, D, 0);
  printIndexedVector(saida[0], D);

  matrixFree(saida, D);
  matrixFree(matriz, D);

  return 0;
}
Exemplo n.º 2
0
static int PCA( Mat input, Mat output, Vec ev )
{
    Mat     u;
    double  *m1, *m2;
    int     row, clm, min;
    int     i, j;

    row = input.row;
    clm = input.clm;
    min = (clm < row)? clm: row;
    if( row < 2 || clm < 2 )      return(-1);
    if( output.clm != input.clm ) return(-1);
    if( output.row != min )       return(-1);
    if( ev.clm != min )           return(-1);

    u = matrixAlloc( min, min );
    if( u.row != min || u.clm != min ) return(-1);
    if( row < clm ) {
        if( x_by_xt( input, u ) < 0 ) { matrixFree(u); return(-1); }
    }
    else {
        if( xt_by_x( input, u ) < 0 ) { matrixFree(u); return(-1); }
    }

    if( QRM( u, ev ) < 0 ) { matrixFree(u); return(-1); }

    if( row < clm ) {
        if( EV_create( input, u, output, ev ) < 0 ) {
            matrixFree(u);
            return(-1);
        }
    }
    else{
        m1 = u.m;
        m2 = output.m;
	for( i = 0; i < min; i++){
	    if( ev.v[i] < VZERO ) break;
            for( j = 0; j < min; j++ ) *(m2++) = *(m1++);
        }
	for( ; i < min; i++){
            ev.v[i] = 0.0;
            for( j = 0; j < min; j++ ) *(m2++) = 0.0;
        }
    }

    matrixFree(u);

    return( 0 );
}
Exemplo n.º 3
0
int matrixPCAnoMean( Mat input, Mat evec, Vec ev )
{
    Mat     work;
    double  srow, sum;
    int     row, clm;
    int     check, rval;
    int     i;

    row = input.row;
    clm = input.clm;
    check = (row < clm)? row: clm;
    if( row < 2 || clm < 2 ) return(-1);
    if( evec.clm != input.clm || evec.row != check ) return(-1);
    if( ev.clm   != check )     return(-1);

    work = matrixAllocDup( input );
    if( work.row != row || work.clm != clm ) return(-1);

    srow = sqrt((double)row);
    for(i=0; i<row*clm; i++) work.m[i] /= srow;

    rval = PCA( work, evec, ev );
    matrixFree( work );

    sum = 0.0;
    for( i = 0; i < ev.clm; i++ ) sum += ev.v[i];
    for( i = 0; i < ev.clm; i++ ) ev.v[i] /= sum;

    return( rval );
}
Exemplo n.º 4
0
void object::applyTrans(float **result, float **vertex, long vertCnt, bool doProj, float **proj)
{
	long i;
	float **matrixA;
	float *tempA;

	if (doProj)
	{
		matrixA = matrixAlloc();
		tempA = new float[4];
		matrixMult(matrixA, mTrans, proj);
		for (i = 0; i < vertCnt; i++)
		{
			vectorMatrixMultEx(tempA, vertex[i], matrixA);
			result[i][0] = tempA[0] / tempA[3];
			result[i][1] = tempA[1] / tempA[3];
			result[i][2] = tempA[2] / tempA[3];
		}
		matrixFree(matrixA);
		delete []tempA;
	}
	else
		for (i = 0; i < vertCnt; i++)
			vectorMatrixMult(result[i], vertex[i], mTrans);
}
Exemplo n.º 5
0
mesh::~mesh()
{
	/* data */
	update(NULL, NULL, 0, 0, true, false);
	matrixFree(mBox, 8);
	matrixFree(mBoxEx, 8);
	matrixFree(mAttrib, 4);
	if (mTreeNode != NULL)
		delete mTreeNode;
	if (mName != NULL)
		delete []mName;
	if (mTag != NULL)
		delete []mTag;

	/* default */
	for (mChildList.moveFirst(); !mChildList.eof(); mChildList.remove());
}
Exemplo n.º 6
0
/**
 * Essa função checa se a exponenciação de matriz convergiu à um valor
 * específico. Esse valor é definido como uma constante no cabeçalho da
 * biblioteca e aqui é definido por 1E-12.
 * Parte-se do pré-suposto que os expoentes seguem a seguinte regra M > N.
 * @param  matrixM Matriz exponenciada a M.
 * @param  matrixN Matriz exponenciada a N.
 * @param  size    Tamanho da matriz.
 * @return         Inteiro que indentifica se a matriz convergiu ou não.
 */
int matrixHasConverged(double **matrixM, double **matrixN, unsigned size) {
  double **matrixD = NULL, norm;

  matrixDifference(matrixM, matrixN, &matrixD, size, size);
  norm = matrixNorm(matrixD, size, size);
  matrixFree(matrixD, size);

  return (norm <= convergeValue);
}
Exemplo n.º 7
0
float *vectorFromAngles(float *angles)
{
	float *result;
	float **tempA;
	float **tempB;
	float **applyTrans;

	tempA = matrixRotation('x', angles[1]);
	tempB = matrixRotation('y', angles[2]);
	applyTrans = matrixIdent(4);
	applyTrans = matrixUpdate(applyTrans, matrixMult(applyTrans, tempA, 4), 4);
	applyTrans = matrixUpdate(applyTrans, matrixMult(applyTrans, tempB, 4), 4);
	result = vectorNull(4);
	result[2] = 1;
	result[3] = 1;
	result = vectorUpdate(result, vectorMatrixMult(result, applyTrans, 4));
	matrixFree(tempA, 4);
	matrixFree(tempB, 4);
	matrixFree(applyTrans, 4);
	return result;
}
Exemplo n.º 8
0
/**
 * Exponecia a matriz até a norma da matriz ^ m ser semelhante à norma
 * da matriz ^ n, determinado por um valor de convergência.
 * @param matrixExp  [description]
 * @param matrixBase [description]
 * @param size       [description]
 * @param iteration  [description]
 */
void expontiationUntilConverge(double ***matrixExp, double **matrixBase,
                               unsigned size, unsigned iteration) {
  double **matrixExpPlusOne = NULL;
  matrixAlloc(&matrixExpPlusOne, size, size);

  squareMatrixMultiplication(*matrixExp, matrixBase, matrixExpPlusOne, size);

  if (!matrixHasConverged(matrixExpPlusOne, *matrixExp, size) &&
      iteration <= maximumExponent) {
    expontiationUntilConverge(&matrixExpPlusOne, matrixBase, size, ++iteration);
  }

  matrixFree(*matrixExp, size);
  *matrixExp = matrixExpPlusOne;
}
Exemplo n.º 9
0
/**
 * Faz uma cópia de uma matriz para uma segunda matriz.
 * @param output  Ponteiro para a matriz que vai receber a cópia.
 * @param input   Ponteiro para a matriz que vai ser copiada.
 * @param lines   Linhas da matriz principal.
 * @param columms Colunas da matriz principal.
 */
void copyMatrix(double ***output, double **input, unsigned lines,
                unsigned columms) {
  unsigned i, j;
  double **newPointer;
  if (*output != NULL)
    matrixFree(*output, lines);

  matrixAlloc(&newPointer, lines, columms);
  for (i = 0; i < lines; i++) {
    for (j = 0; j < columms; j++) {
      newPointer[i][j] = input[i][j];
    }
  }

  *output = newPointer;
}
Exemplo n.º 10
0
object::~object()
{
	/* data */
	matrixFree(mTrans);
	delete []mOrigin;
	delete []mOriginEx;
	delete []mAngle;
	delete []mScale;

	/* physics */
	delete []mPosVel;
	delete []mAngVel;
	delete []mPosAcc;
	delete []mAngAcc;
	if (mTarget != NULL)
		delete []mTarget;
}
Exemplo n.º 11
0
IMG* deblur(const IMG* src, 
	    const IMG* psfBase,
	    const IMG* disparityMap,
	    double param[])
{
  int h,w;
  int maxDisparity = MAX_DISPARITY;
  int BlockRows = ceil( (double)src->height / (double)BLOCK_SIZE );
  int BlockCols = ceil( (double)src->width / (double)BLOCK_SIZE );

  
  //psf
  Complex psf[MAX_PSF_SIZE][CUT_OFF_SIZE][CUT_OFF_SIZE];
  createPSF(psf, psfBase, 1, MAX_PSF_SIZE-1);

  //窓関数
  Mat window = createWindowFunction();
  
  //最終的な結果を保存しておく場所
  Mat dstMat = matrixAlloc( src->height, src->width);
  Mat wegithMat = matrixAlloc( src->height, src->width);

  //0で初期化
  for( h = 0; h < dstMat.row; ++h){
    for( w = 0; w < dstMat.clm; ++w){
      ELEM0(dstMat, h, w) = 0.0;
      ELEM0(wegithMat, h, w) = 0.0;
    }
  }


  //作業用領域
  double srcIn[CUT_OFF_SIZE][CUT_OFF_SIZE];
  double dstIn[CUT_OFF_SIZE][CUT_OFF_SIZE];
  
  Complex srcF[CUT_OFF_SIZE][CUT_OFF_SIZE];
  Complex dstF[CUT_OFF_SIZE][CUT_OFF_SIZE];


  
  for(int row = 0; row < BlockRows; ++row){
    for(int col = 0 ; col < BlockCols; ++col){

      //copy & window function
      for( h = 0; h < CUT_OFF_SIZE; ++h){
	for( w = 0; w < CUT_OFF_SIZE; ++w){
	  srcIn[h][w] = 0.0;
	  
	  int y = h + row * BLOCK_SIZE + ( BLOCK_SIZE - CUT_OFF_SIZE ) / 2;
	  int x = w + col * BLOCK_SIZE + ( BLOCK_SIZE - CUT_OFF_SIZE ) / 2;

	  if( y < 0 || y >= src->height || w < 0 || w >= src->width){
	    continue;
	  }else{
	    srcIn[h][w] = (double)IMG_ELEM(src, y, x) * ELEM0(window, h, w);
	  }
	}
      }
      //copy done

      //kernel sizeの決定
      int disparity = (int)IMG_ELEM(disparityMap, row*BLOCK_SIZE + BLOCK_SIZE/2, col*BLOCK_SIZE + BLOCK_SIZE/2);
      int kernelSize =  param[0]*(double)disparity + param[1] ;
      
      //printf("disprity = %d, kernelSize = %d\n",disparity, kernelSize);

      //srcをDFT
      fourier(srcF, srcIn);

      //wiener deconvolution
      wienerdeconvolution(srcF, psf[kernelSize], dstF, SNR);

      //IDFT
      inverseFourier(dstIn, dstF);
      
      //copy to dstMat
      for(h=0;h<CUT_OFF_SIZE;++h){
	for(w=0;w<CUT_OFF_SIZE;++w){
	  int y = h + row * BLOCK_SIZE + (BLOCK_SIZE-CUT_OFF_SIZE)/2;
	  int x = w + col * BLOCK_SIZE + (BLOCK_SIZE-CUT_OFF_SIZE)/2;

	  if( y < 0 || y >= src->height || x < 0 || x >= src->width){
	    continue;
	  }else{
	    ELEM0(dstMat, y, x) += dstIn[h][w];
	    ELEM0(wegithMat, y, x) += ELEM0(window, h, w);
	  }

	}//w
      }//h
      


    }//col
  }//row

  printPassedTime();


  //最終的に返す構造体
  IMG* dst = createImage( src->height, src->width);

  //weright mean
  for(h=0;h<dstMat.row;++h){
    for(w=0;w<dstMat.clm;++w){
      ELEM0(dstMat, h, w) /= ELEM0(wegithMat, h, w);
    }
  }

  for( h = 0 ; h < dst->height ; ++h ){
    for( w = 0 ; w < dst->width ; ++w ){
      IMG_ELEM( dst, h, w) = fabs( ELEM0( dstMat, h, w) ) / 3.0;
    }
  }


  //後片付け
  matrixFree(dstMat);
  matrixFree(wegithMat);
  matrixFree(window);

  return dst;
}