Exemple #1
0
/**
 @param[in] vetrices array of vetrices (so far only for 3)
 @param[in] E material constatn E
 @param[in] mi material constatn mi
 @param[in] fs volume force vector
 @param[out] stifMat stifness matrix (array 36 long)
 @param[out]
 */
void elastLoc(Point *vetrices[], PetscReal E, PetscReal mi, PetscReal *fs,
              PetscReal *stifMat, PetscReal *bL) {

    PetscReal
    T[] =
    { 1, 1, 1, vetrices[0]->x, vetrices[1]->x, vetrices[2]->x, vetrices[0]->y, vetrices[1]->y, vetrices[2]->y };

    PetscReal phiGrad[] = { vetrices[1]->y - vetrices[2]->y, vetrices[2]->x
                            - vetrices[1]->x, vetrices[2]->y - vetrices[0]->y, vetrices[0]->x
                            - vetrices[2]->x, vetrices[0]->y - vetrices[1]->y, vetrices[1]->x
                            - vetrices[0]->x
                          };

    //PetscPrintf(PETSC_COMM_SELF, "DET:%e \n", matrixDet(T, 3));

    PetscReal detT = fabs(matrixDet(T, 3));

    PetscReal phiGradT[6];
    matrixTranspose(phiGrad, 3, 2, phiGradT);

    double Cmu[] = { 2, 0, 0, 0, 2, 0, 0, 0, 1 };
    //double Clm[] = { 1, 1, 0, 1, 1, 0, 0, 0, 0 }; Why is this never used?
    PetscReal C[] = { 1 - mi, mi, 0, mi, 1 - mi, 0, 0, 0, (1 - 2 * mi) / 2 };

    matrixSum(C, E / ((1 + mi) * (1 - 2 * mi)), Cmu, 0, C, 3, 3);

    double R[18], RT[18];
    for (int i = 0; i < 18; i++)
        R[i] = 0;

    int idxR1[] = { 0, 2 };
    int idxC1[] = { 0, 2, 4 };
    int idxR2[] = { 2, 1 };
    int idxC2[] = { 1, 3, 5 };

    matrixSetSub(R, 3, 6, idxR1, 2, idxC1, 3, phiGradT);
    matrixSetSub(R, 3, 6, idxR2, 2, idxC2, 3, phiGradT);

    matrixTranspose(R, 3, 6, RT);

    PetscReal temp[18];

    matrixMult(C, 3, 3, R, 3, 6, temp);
    matrixMult(RT, 6, 3, temp, 3, 6, stifMat);

    matrixSum(stifMat, 1 / (2 * detT), stifMat, 0, stifMat, 6, 6);
    //PetscPrintf(PETSC_COMM_SELF, "%e %e \n", fs[0], fs[1]);

    //Volume Force
    for (int i = 0; i < 3; i++) {
        bL[i * 2] = detT / 6 * fs[0];
        bL[i * 2 + 1] = detT / 6 * fs[1];
    }

    //@TODO Edge Force!!!
}
Exemple #2
0
void testMatrixSum(void)
{
	int **matrixA, **matrixB;
	int	**matrixResult;
	int	rowNum, colNum;

	rowNum = colNum = 3;

	matrixA = generateMatrix(rowNum, colNum);
	matrixB = generateMatrix(rowNum, colNum);

	matrixResult = make2DArray(rowNum, colNum);

	matrixSum(matrixResult, matrixA, matrixB, rowNum, colNum);

	printf("----------- test: matrix addition ------------------\n");

	printf("matrixA\n");
	printMatrix(matrixA, rowNum, colNum);

	printf("matrixB\n");
	printMatrix(matrixB, rowNum, colNum);

	printf("matrixResult = matrixA + matrixB\n");
	printMatrix(matrixResult, rowNum, colNum);

}
Exemple #3
0
void DCTLabDialog::updateStats()
{
	Matrix D = buildDCT(8);
	Matrix C = D.Tr() * A * D;
	Matrix rec = clampMatrix(D * (C^M) * D.Tr(), 0, 255);
	Matrix E = abs(A-rec);
	
	double max = E.getElement(0, 0);
	for(int i = 0 ; i < 8 ; i++)
		for(int j = 0 ; j < 8 ; j++)
			if(max < E.getElement(i, j))
				max = E.getElement(i, j); 
	
	stats.setText(
		"Statistics:\n\n"
		"PSNR: " + QString::number(10*log10(matrixPSNR(A,rec)), 'g', 3) + " dB\n"
		"SNR : " + QString::number(10*log10(matrixSNR(A,rec)), 'g', 3) + " dB\n"
		"MAE : " + QString::number(matrixSum(E)/64.0, 'g', 3) + "\n"
		"MaxE: " + QString::number(max, 'g', 3)
		);
}
Exemple #4
0
void test_eigen2_sum()
{
  for(int i = 0; i < g_repeat; i++) {
    CALL_SUBTEST_1( matrixSum(Matrix<float, 1, 1>()) );
    CALL_SUBTEST_2( matrixSum(Matrix2f()) );
    CALL_SUBTEST_3( matrixSum(Matrix4d()) );
    CALL_SUBTEST_4( matrixSum(MatrixXcf(3, 3)) );
    CALL_SUBTEST_5( matrixSum(MatrixXf(8, 12)) );
    CALL_SUBTEST_6( matrixSum(MatrixXi(8, 12)) );
  }
  for(int i = 0; i < g_repeat; i++) {
    CALL_SUBTEST_5( vectorSum(VectorXf(5)) );
    CALL_SUBTEST_7( vectorSum(VectorXd(10)) );
    CALL_SUBTEST_5( vectorSum(VectorXf(33)) );
  }
}
Exemple #5
0
  //NNFF performs a feedforward pass
  double FBNN::nnff(const FMatrix& x, const FMatrix& y)
  {
//     std::cout << "start nnff x = (" << x.rows() << "," << x.columns() << ")" << std::endl;
    double L = 0;
    if(m_oAs.empty())
    {
      for(int i = 0; i < m_iN; ++i)
	m_oAs.push_back(std::make_shared<FMatrix>(FMatrix()));
    }
    *m_oAs[0] = addPreColumn(x,1);    
    if(m_fDropoutFraction > 0 && !m_fTesting)
    {
        if(m_odOMs.empty())//clear dropOutMask
        {
            for(int i = 0; i < m_iN - 1; ++i)
                m_odOMs.push_back(std::make_shared<FMatrix>(FMatrix()));
        }
    }
    
//     std::cout << "start feedforward" << std::endl;
    //feedforward pass
    for(int i = 1; i < m_iN - 1; ++i)
    {
//       std::cout << "activation function" << std::endl;
      //activation function
      if(m_strActivationFunction == "sigm")
      {	
	//Calculate the unit's outputs (including the bias term)
	*m_oAs[i] = sigm((*m_oAs[i-1]) * blaze::trans(*m_oWs[i-1]));
      }
      else if(m_strActivationFunction == "tanh_opt")
      {
	*m_oAs[i] = tanh_opt((*m_oAs[i-1]) * blaze::trans(*m_oWs[i-1]));
      }
      
//       std::cout << "dropout" << std::endl;
      //dropout
      if(m_fDropoutFraction > 0)
      {
	if(m_fTesting)
	  *m_oAs[i] = (*m_oAs[i]) * (1 - m_fDropoutFraction);
	else
	{
	  *m_odOMs[i] = rand(m_oAs[i]->rows(),m_oAs[i]->columns()) > m_fDropoutFraction;
	  *m_oAs[i] = bitWiseMul(*m_oAs[i],*m_odOMs[i]);
	}
      }
      
//       std::cout << "sparsity" << std::endl;
      //calculate running exponential activations for use with sparsity
      if(m_fNonSparsityPenalty > 0)
	*m_oPs[i] =  (*m_oPs[i]) * 0.99 + columnMean(*m_oAs[i]);
      
//       std::cout << "Add the bias term" << std::endl;
      //Add the bias term
      *m_oAs[i] = addPreColumn(*m_oAs[i],1);    
    }
    
//     std::cout << "start calculate output" << std::endl;
    if(m_strOutput == "sigm")
    {
      *m_oAs[m_iN -1] = sigm((*m_oAs[m_iN-2]) * blaze::trans(*m_oWs[m_iN-2]));
    }
    else if(m_strOutput == "linear")
    {
      *m_oAs[m_iN -1] = (*m_oAs[m_iN-2]) * blaze::trans(*m_oWs[m_iN-2]);
    }
    else if(m_strOutput == "softmax")
    {
      *m_oAs[m_iN -1] = softmax((*m_oAs[m_iN-2]) * blaze::trans(*m_oWs[m_iN-2]));
    }
    
//     std::cout << "start error and loss" << std::endl;
    //error and loss
    m_oEp = std::make_shared<FMatrix>(y - (*m_oAs[m_iN-1]));
    
    if(m_strOutput == "sigm" || m_strOutput == "linear")
    {
      L = 0.5 * matrixSum(bitWiseSquare(*m_oEp)) / x.rows();
    }
    else if(m_strOutput == "softmax")
    {
      L = -matrixSum(bitWiseMul(y,bitWiseLog(*m_oAs[m_iN-1]))) / x.rows();
    }    
//     std::cout << "end nnff" << std::endl;
    return L;
  }
Exemple #6
0
void strassen(Matrices *m){
  int seuil = 0;
  int *a11,*a12,*a21,*a22;
  int *b11,*b12,*b21,*b22;

  a11 = malloc((m->size*m->size*sizeof(int))/4);
  a12 = malloc((m->size*m->size*sizeof(int))/4);
  a21 = malloc((m->size*m->size*sizeof(int))/4);
  a22 = malloc((m->size*m->size*sizeof(int))/4);

  b11 = malloc((m->size*m->size*sizeof(int))/4);
  b12 = malloc((m->size*m->size*sizeof(int))/4);
  b21 = malloc((m->size*m->size*sizeof(int))/4);
  b22 = malloc((m->size*m->size*sizeof(int))/4);

  int index1=0;
  int index2=0;
  int index3=0;
  int index4=0;
  int i; 
  for(i = 0 ; i < m->size*m->size ; i++){
    if(i%m->size < m->size/2 ){
      if((i/m->size) < m->size/2){
        a11[index1]=m->a[i];
        b11[index1++]=m->b[i];
      }else{
        a21[index2]=m->a[i];
        b21[index2++]=m->b[i];
      }
    }else{
      if((i/m->size) < m->size/2){
        a12[index3]=m->a[i];
        b12[index3++]=m->b[i];
      }else{
        a22[index4]=m->a[i];
        b22[index4++]=m->b[i];
      }
    }
  }

  Matrices temp;
  temp.size = m->size/2;

  int* p1;
  p1 = malloc(m->size*sizeof(int));
  temp.result = p1;
  matrixSum(a11,a22,temp.size,temp.a);
  matrixSum(b11,b22,temp.size,temp.b);
  strassen(&temp);
    
  int* p2;
  p2 = malloc(m->size*sizeof(int));
  temp.result = p2;
  matrixSum(a21,a22,temp.size,temp.a);
  temp.b = b11;
  strassen(&temp);

  int* p3;
  p3 = malloc(m->size*sizeof(int));
  temp.result = p3;
  matrixSub(b12,b22,temp.size,temp.b);
  temp.a = a11;
  strassen(&temp);
  
  
}
Exemple #7
0
int main()
{
	FILE* fin = NULL;
	char c, fileName[20];
	int i;

	do
	{
		printf ("\n    <<<<<<<<<<<<<<<<<<<<<<   MENU   >>>>>>>>>>>>>>>>>>>>>>\n\n");
		printf ("\n    1. Generate a new test file AND display it;");
		printf ("\n    2. Generate a new test file WITHOUT displaying it;\n");
		printf ("\n    3. Load an existing test file AND display it;");
		printf ("\n    4. Load an existing test file WITHOUT displaying it;\n");
		printf ("\n    5. Exit the program.\n");

		printf ("\n Choose an option ('c' to clear screen): ");
		scanf (" %c", &c);

		if (c == 'c')
		{
			for (i = 0; i < 100; i++)
			{
				printf ("\n");
			}
		}

		else if (c == '1')
		{
			printf ("\nGenerate a new test file and display it. Input its name:\n");
			scanf ("%s", fileName);
			graphGenerator (fileName);
			displayGraph (fin, fileName);
		}

		else if (c == '2')
		{
			printf ("\nGenerate a new test file. Input its name:\n");
			scanf ("%s", fileName);
			graphGenerator (fileName);
		}

		else if (c == '3')
		{
			printf ("\nLoad an existing test file and display it. Input its name:\n");
			scanf ("%s", fileName);
			displayGraph (fin, fileName);
		}

		else if (c == '4')
		{
			printf ("\nLoad an existing test file. Input its name:\n");
			scanf ("%s", fileName);
		}

		else if (c == '5')
		{
			exit(1);
		}

		else
		{
			printf ("\nThe key you pressed is not a valid option. Please press 1, 2, 3, 4 or 5.\n\n");
		}

		if (c == '1' || c == '2' || c == '3' || c == '4')
		{
			fin = fopen (fileName, "r");

			if (fin == NULL) 
			{
				perror ("\n\tError opening file\n");
			}
			else
			{
				int nodes;
				double beta;

				printf ("\nDefine the damping factor (best between 0.8 and 0.9): ");
				scanf ("%lf", &beta);

				double **M = loadGraph (fin, nodes);
				M = createMatrix_M (M, nodes, beta);

				double **B = createMatrix_B (nodes, beta);
				M = matrixSum (M, B, nodes);
				deallocMatrix <double> (B, nodes);

				//
				printf("\n Matrix A = M + B \n");
				displayMatrix (M, nodes, nodes);
				//

				powerIteration (M, nodes, beta);

				fclose (fin);
			}
		}

	} while (true);

	return 0;
}
Exemple #8
0
void coalMarkovChainTopologyMatrix_pthread(afsStateSpace *S,gsl_matrix *topol, gsl_matrix_int *moveType, int *dim1, int *dim2, int start, int stop, int *nnz){
	int i,j,k,steps,x,y;
	double top,bottom;
	afsObject *delta;
	int **activePos1, **activePos2; //first dimension here relates to 2 popns
	int nlin1,nlin2,compat;
	int nonZeroCount, tot, tCount;
	int size = 200;
	nlin1 = nlin2 = 0;
	nonZeroCount = 0;
	if(S->nstates != topol->size1 || S->nstates != moveType->size1){
		fprintf(stderr,"Error: StateSpace and matrices are not of equal size\n");
		exit(1);
	}
	*nnz = 0;
	//arrays for finding positions of entries
	activePos1 = malloc(size*sizeof(int *));
	activePos2 = malloc(size*sizeof(int *));
	for(i=0;i<size;i++){
		activePos1[i] = malloc(2*sizeof(int));
		activePos1[i][0] = activePos1[i][1] = 0;
		activePos2[i] = malloc(2*sizeof(int));
		activePos2[i][0] = activePos2[i][1] = 0;

	}

	tot = S->nstates * S->nstates;
	tCount = 0;
	delta = afsObjectNewFrom(S->states[0]);
	for(i=start;i<stop;i++){
		for(j=0;j<S->nstates;j++){
			gsl_matrix_int_set(moveType,i,j,666);

			//is ith state root state?
			if(S->states[i]->nalleles == 1){
				//set correct absorbing conditions
				if(i==j){
					gsl_matrix_set(topol,i,j,1.0);
					gsl_matrix_int_set(moveType,i,j,-1);
					dim1[nonZeroCount] = i;
					dim2[nonZeroCount] = j;
					nonZeroCount += 1;
				}
				else{
					gsl_matrix_set(topol,i,j,0.0);
					gsl_matrix_int_set(moveType,i,j,-1);
				}		
			}
			else{ //ith not root; what is the move?
				steps = S->states[i]->nalleles - S->states[j]->nalleles ;
				if(steps < 2){
				//	delta = afsObjectDelta(S->states[i],S->states[j]);
					afsObjectDeltaPre(S->states[i],S->states[j],delta);
					switch(steps){
						case 0:
						// 0 "steps", so no changes in nalleles between two
						//check counts and positions	
						if(abs(matrixSum(delta->popMats[0])) == 1){
							nonZeroEntries(delta->popMats[0], activePos1, &nlin1);
							nonZeroEntries(delta->popMats[1], activePos2, &nlin2);

							//migration?
							if(nlin1 == nlin2 && activePos1[0][0] == activePos2[0][0] &&
								(gsl_matrix_int_min(delta->popMats[0]) >= 0 || gsl_matrix_int_min(delta->popMats[1]) >= 0))
							{

								//migration popn1?
								if(matrixSum(delta->popMats[0]) == 1){
									gsl_matrix_set(topol,i,j,
										(double) gsl_matrix_int_get(S->states[i]->popMats[0],
										activePos1[0][0], activePos1[0][1]) / S->states[i]->aCounts[0]);
									gsl_matrix_int_set(moveType,i,j,2);
									dim1[nonZeroCount] = i;
									dim2[nonZeroCount] = j;
									nonZeroCount += 1;
								}
								else{ //migration popn2
									gsl_matrix_set(topol,i,j,
										(double) gsl_matrix_int_get(S->states[i]->popMats[1],
										activePos2[0][0],activePos2[0][1]) / S->states[i]->aCounts[1]);
									gsl_matrix_int_set(moveType,i,j,3);
									dim1[nonZeroCount] = i;
									dim2[nonZeroCount] = j;
									nonZeroCount += 1;
								}
							}
						}
						break;

						case 1://coalescent?
						//coal popn1?
						if(matrixSum(delta->popMats[0]) == 1 && countNegativeEntries(delta->popMats[1]) == 0 && S->states[j]->aCounts[0] >= 1 ){
							entriesGreaterThan(delta->popMats[0], activePos1, &nlin1,0);//old lineages
							entriesLessThan(delta->popMats[0], activePos2, &nlin2,0);//new lineages
							if(nlin2 != 0 && nlin2 < 2){

								compat=0;
								if(nlin1 == 1){
									if(2*activePos1[0][0] == activePos2[0][0] && 2*activePos1[0][1] == activePos2[0][1])
										compat = 1;
								}
								if(nlin1==2){
									if(activePos1[0][0] + activePos1[1][0] == activePos2[0][0] &&
										activePos1[0][1] + activePos1[1][1] == activePos2[0][1])
										compat = 1;
								}
								if(compat != 1){
									gsl_matrix_set(topol,i,j,0.0);
									gsl_matrix_int_set(moveType,i,j,666);
								}
								else{

									top = 1.0;
									bottom = S->states[i]->aCounts[0] * (S->states[i]->aCounts[0]-1) /2.0 ;
									for(k=0;k<nlin1;k++){
										x = activePos1[k][0];
										y = activePos1[k][1];
										top *= (float) xchoosey(gsl_matrix_int_get(S->states[i]->popMats[0],x,y),
											gsl_matrix_int_get(delta->popMats[0],x,y));
									}
									gsl_matrix_set(topol,i,j,top/bottom);
									gsl_matrix_int_set(moveType,i,j,0);
									dim1[nonZeroCount] = i;
									dim2[nonZeroCount] = j;
									nonZeroCount += 1;
								}
							}
						} 
						else{
							if(matrixSum(delta->popMats[1]) == 1 && countNegativeEntries(delta->popMats[0]) == 0 && S->states[j]->aCounts[1] >= 1 ){
								entriesGreaterThan(delta->popMats[1], activePos1, &nlin1,0);//old lineages
								entriesLessThan(delta->popMats[1], activePos2, &nlin2,0);//new lineages
								if(nlin2 != 0 && nlin2 < 2){								
									compat=0;
									if(nlin1 == 1){
										if(2*activePos1[0][0] == activePos2[0][0] && 2*activePos1[0][1] == activePos2[0][1])
											compat = 1;
									}
									if(nlin1==2){
										if(activePos1[0][0] + activePos1[1][0] == activePos2[0][0] &&
											activePos1[0][1] + activePos1[1][1] == activePos2[0][1])
											compat = 1;
									}
									if(compat != 1){

										gsl_matrix_set(topol,i,j,0.0);
										gsl_matrix_int_set(moveType,i,j,666);
									}
									else{
										top = 1.0;

										bottom = S->states[i]->aCounts[1] * (S->states[i]->aCounts[1]-1) /2.0 ;
										for(k=0;k<nlin1;k++){
											x = activePos1[k][0];
											y = activePos1[k][1];

											top *= xchoosey(gsl_matrix_int_get(S->states[i]->popMats[1],x,y),
												gsl_matrix_int_get(delta->popMats[1],x,y));
										}

										gsl_matrix_set(topol,i,j,top/bottom);
										gsl_matrix_int_set(moveType,i,j,1);
										dim1[nonZeroCount] = i;
										dim2[nonZeroCount] = j;
										nonZeroCount += 1;
									}
								}
							}
						}
						break;
					}
					//afsObjectFree(delta);
				}
			}
		}

	}
	//cleanup and free
	for(i=0;i<size;i++){
		free(activePos1[i]); 
		free(activePos2[i]); 
	}
	free(activePos1);
	free(activePos2);
	*nnz = nonZeroCount;
}