Exemple #1
0
int main(int argc, char* argv[])
{
	double** A;
	A = AllocateMatrixMemory(5, 3);
	A[0][1] = 2.0;
	A[2][4] = 4.0;

	FreeMatrixMemory(5, A);

	return 0;
}
Exemple #2
0
void  CSSGraph::ReleaseEdges()  {
int i;

  FreeMatrixMemory ( graph,nGAlloc,1,1 );
  nGAlloc = 0;

  for (i=0;i<nEAlloc;i++)
    if (Edge[i])  delete Edge[i];
  if (Edge)  delete[] Edge;
  Edge    = NULL;
  nEdges  = 0;
  nEAlloc = 0;

}
Exemple #3
0
realtype CSSGraph::CalcCombinations ( ivector F, int nm )  {
//
//   F contains list of nm graph vertices (e.g. those matched),
// the function returns the number of combinations these
// vertices may be picked from the graph, taking into account
// their type, length and sequence order.
//
rmatrix  C;
realtype nCombs;
int      i,j,k,k0;

  if ((nm<=0) || (nVertices<nm))  return 1.0;

  GetMatrixMemory ( C,nm,nVertices,1,1 );
  for (i=1;i<=nm;i++)
    for (j=1;j<=nVertices;j++)
      C[i][j] = 0.0;

  k0 = 0;
  for (i=1;i<=nm;i++)  {
    k = MaxInt4;
    for (j=1;j<=nm;j++)
      if ((F[j]>k0) && (F[j]<k))  k = F[j];
    if (k<MaxInt4)  {
      k0 = k;
      k--;
      for (j=i;j<=nVertices-nm+i;j++)
        if (Vertex[k]->Compare(Vertex[j-1]))  C[i][j] = 1.0;
    }
  }

  for (j=nVertices-1;j>=nm;j--)
    C[nm][j] += C[nm][j+1];

  for (i=nm-1;i>=1;i--)
    for (j=nVertices-nm+i;j>=i;j--)
      if (C[i+1][j+1]<=0.01)  C[i][j] = 0.0;
      else if (C[i][j]<=0.01) C[i][j] = C[i][j+1];
                         else C[i][j] = C[i][j+1] + C[i+1][j+1];

  nCombs = C[1][1];
  FreeMatrixMemory ( C,nm,1,1 );

  return nCombs;

}
int main(int argc, char * argv[])
{
    if(argc !=2 ) { Usage(argv[0]); return 1; } //has the user provided the data filename in the command line?
    std::ifstream read_file(argv[1]);  //...then open it for reading
    assert(read_file.is_open());
    double ** A;
    int rows,cols;
    read_file >> rows >> cols;  //the format of the file is that first the size is given...
    std::cout << "Dynamically allocating memory for a matrix "<<rows<<"x"<<cols<<"\n";
    A = AllocateMatrixMemory(rows, cols);
    for(int r=0;r<rows; r++)  //... and then the elements are in the file
    {
        for(int c=0;c<cols;c++)
          read_file >> A[r][c];
    }
    read_file.close();    //do not forget to close the file
    std::cout << "Lets see if we read correct,by printing the matrix on the console:\n";
    for(int r=0;r<rows; r++) {
        for(int c=0;c<cols;c++) { std::cout << A[r][c]<< "  ";}
        std::cout << "\n";
    }
    FreeMatrixMemory(rows, A);  //do not forget to release the memory allocated from new
}
Exemple #5
0
void CalcCombinations ( rvector & combs, int & vlen,
                        PCSSGraph G1, PCSSGraph G2 )  {
//  combs[i], i=1..vlen, returns the number of common
//  substructures of size i of graphs G1 and G2. The
//  sequential order of graph vertices is taken into
//  account, however the actual edges are completely
//  neglected.
PPCSSVertex V1,V2;
rmatrix3    P;
imatrix     C;
realtype    q;
int         n,m, i,j,k;

  n = G1->GetNofVertices();
  m = G2->GetNofVertices();
  if (n<=m)  {
    V1 = G1->GetVertices();
    V2 = G2->GetVertices();
  } else  {
    m  = G1->GetNofVertices();
    n  = G2->GetNofVertices();
    V2 = G1->GetVertices();
    V1 = G2->GetVertices();
  }

  vlen = 0;
  FreeVectorMemory ( combs,1 );
  if (n<=0)  return;

  GetMatrix3Memory ( P,n,m,n,1,1,1 );
  GetMatrixMemory  ( C,n,m,1,1 );
  for (i=1;i<=n;i++)
    for (j=1;j<=m;j++)  {
      if (V1[i-1]->Compare(V2[j-1]))  C[i][j] = 1;
                                else  C[i][j] = 0;
      for (k=1;k<=n;k++)
        P[i][j][k] = 0.0;
    }

  q = 0.0;
  for (j=1;j<=m;j++)  {
    q += C[1][j];
    P[1][j][1] = q;
  }

  for (i=2;i<=n;i++)  {

    q = 0.0;
    for (j=1;j<=m;j++)  {
      q += C[i][j];
      P[i][j][1] = P[i-1][j][1] + q;
    }

    for (k=2;k<=i;k++)  {
      for (j=k;j<=m;j++)
        if (C[i][j]==0)  P[i][j][k] = P[i][j-1][k];
                   else  P[i][j][k] = P[i][j-1][k] + P[i-1][j-1][k-1];
      for (j=k;j<=m;j++)
        P[i][j][k] += P[i-1][j][k];
    }

  }

  vlen = n;
  GetVectorMemory ( combs,n,1 );
  for (k=1;k<=n;k++)
    combs[k] = P[n][m][k];

  FreeMatrix3Memory ( P,n,m,1,1,1 );
  FreeMatrixMemory  ( C,n,1,1 );

}
Exemple #6
0
int  SuperposeSSGraphs ( PCSSGraph G1, ivector F1,
                         PCSSGraph G2, ivector F2,
                         int     matchlen,
                         mat44 & TMatrix )  {
PCSSVertex Vx;
rmatrix    A,U,V;
rvector    W,RV1;
vect3      v1,v2;
realtype   det,B, x01,y01,z01, x02,y02,z02, mass,mass1,mass2;
int        i,j,k,l, nE1,nE2;

  nE1 = G1->GetNofEdges();
  if (!nE1)  G1->BuildGraph();

  nE2 = G2->GetNofEdges();
  if (!nE2)  G2->BuildGraph();

  GetMatrixMemory ( A,3,3,1,1 );
  GetMatrixMemory ( U,3,3,1,1 );
  GetMatrixMemory ( V,3,3,1,1 );
  GetVectorMemory ( W,3,1 );
  GetVectorMemory ( RV1,3,1 );

  for (j=1;j<=3;j++)
    for (k=1;k<=3;k++)
      A[j][k] = 0.0;

  for (i=1;i<=matchlen;i++)  {
    Vx = G1->GetGraphVertex ( F1[i] );
    Vx->GetDirection ( v1 );
    Vx = G2->GetGraphVertex ( F2[i] );
    Vx->GetDirection ( v2 );
    for (j=1;j<=3;j++)
      for (k=1;k<=3;k++)
        A[j][k] += v1[k-1]*v2[j-1];
  }

  for (i=1;i<matchlen;i++)
    for (l=i+1;l<=matchlen;l++)
      if (G1->GetEdgeDirection(F1[i],F1[l],v1) &&
          G2->GetEdgeDirection(F2[i],F2[l],v2))
        for (j=1;j<=3;j++)
          for (k=1;k<=3;k++)
            A[j][k] += v1[k-1]*v2[j-1];

  det = A[1][1]*A[2][2]*A[3][3] + 
        A[1][2]*A[2][3]*A[3][1] +
        A[2][1]*A[3][2]*A[1][3] -
        A[1][3]*A[2][2]*A[3][1] -
        A[1][1]*A[2][3]*A[3][2] -
        A[3][3]*A[1][2]*A[2][1];

  SVD ( 3,3,3,A,U,V,W,RV1,True,True,i );

  if (i!=0) {
    for (j=0;j<4;j++)  {
      for (k=0;k<4;k++)
        TMatrix[j][k] = 0.0;
      TMatrix[j][j] = 1.0;
    }
    return 1;
  }

  if (det<0.0)  {
    k = 0;
    B = MaxReal;
    for (j=1;j<=3;j++)
      if (W[j]<B)  {
        B = W[j];
        k = j;
      }
    for (j=1;j<=3;j++)
      V[k][j] = -V[k][j];
  }

  for (j=1;j<=3;j++)
    for (k=1;k<=3;k++)  {
      B = 0.0;
      for (i=1;i<=3;i++)
        B += U[j][i]*V[k][i];
      TMatrix[j-1][k-1] = B;
    }


  //  9. Add translation
  x01 = 0.0;   y01 = 0.0;   z01 = 0.0;  mass1 = 0.0;
  x02 = 0.0;   y02 = 0.0;   z02 = 0.0;  mass2 = 0.0;
  for (i=1;i<=matchlen;i++)  {
    Vx     = G1->GetGraphVertex ( F1[i] );
    mass   = Vx->GetMass();
    Vx->GetPosition ( v1 );
    x01   += v1[0]*mass;
    y01   += v1[1]*mass;
    z01   += v1[2]*mass;
    mass1 += mass;
    Vx     = G2->GetGraphVertex ( F2[i] );
    mass   = Vx->GetMass();
    Vx->GetPosition ( v2 );
    x02   += v2[0]*mass;
    y02   += v2[1]*mass;
    z02   += v2[2]*mass;
    mass2 += mass;
  }
  x01 /= mass1;   y01 /= mass1;  z01 /= mass1;
  x02 /= mass2;   y02 /= mass2;  z02 /= mass2;
  TMatrix[0][3] = x02 - TMatrix[0][0]*x01 - TMatrix[0][1]*y01 -
                        TMatrix[0][2]*z01;
  TMatrix[1][3] = y02 - TMatrix[1][0]*x01 - TMatrix[1][1]*y01 -
                        TMatrix[1][2]*z01;
  TMatrix[2][3] = z02 - TMatrix[2][0]*x01 - TMatrix[2][1]*y01 -
                        TMatrix[2][2]*z01;

  FreeMatrixMemory ( A,1,1 );
  FreeMatrixMemory ( U,1,1 );
  FreeMatrixMemory ( V,1,1 );
  FreeVectorMemory ( W,1 );
  FreeVectorMemory ( RV1,1 );

  if (!nE1)  G1->ReleaseEdges();
  if (!nE2)  G2->ReleaseEdges();

  return 0;

}
static POSITION_ARRAY StepFive(double ** ppMatrix, POSITION posPivot)
{
	POSITION_ARRAY pivots = {NULL, 0};
	int iRowsSave = iRows;
	int iColumnsSave = iColumns;

	if(iRows - 1 == 0) {
		return pivots; // Fertig
	}

	if(iColumns - posPivot.iColumn == 0) {
		return pivots; // Fertig
	}
	
	double ** ppSmallerMatrix = AllocateMatrixMemory(iColumns - posPivot.iColumn - 1, iRows - 1);

	for(int i = posPivot.iColumn + 1; i < iColumns; i++) {
		for(int g = 1; g < iRows; g++) {
			ppSmallerMatrix[i - posPivot.iColumn - 1][g - 1] = ppMatrix[i][g];
		}
	}

	iRows--; 
	iRowsOffset++;
	iColumns -= posPivot.iColumn + 1;
	iColumnsOffset += posPivot.iColumn + 1;

	POSITION posNewPivot = StepOne(ppSmallerMatrix);
	if(posNewPivot.iColumn == -1) {
		// Rücksetzen
		iRowsOffset -= (iRowsSave - iRows);
		iRows = iRowsSave;
		iRowsOffset -= (iColumnsSave - iColumns);
		iColumns = iColumnsSave;
		return pivots; // Fertig
	}
	
	StepTwo(ppSmallerMatrix, &posNewPivot);
	StepThree(ppSmallerMatrix, posNewPivot);
	StepFour(ppSmallerMatrix, posNewPivot);
	pivots = StepFive(ppSmallerMatrix, posNewPivot);

	AddPosition(&pivots, posNewPivot);

	for(int i = 0; i < pivots.iCount; i++) {
		pivots.pPositions[i].iColumn += posPivot.iColumn + 1; // Auf neue Grösse anpassen
		pivots.pPositions[i].iRow += 1;
	}

	// Rücksetzen
	iRowsOffset -= (iRowsSave - iRows);
	iRows = iRowsSave;
	iColumnsOffset -= (iColumnsSave - iColumns);
	iColumns = iColumnsSave;

	// Eingruppieren der neuen Matrix
	for(int i = posPivot.iColumn + 1; i < iColumns; i++) {
		for(int g = 1; g < iRows; g++) {
			ppMatrix[i][g] = ppSmallerMatrix[i - posPivot.iColumn - 1][g - 1];
		}
	}

	// Freigeben des Speichers
	FreeMatrixMemory(ppSmallerMatrix, iColumns - posPivot.iColumn - 1);

	return pivots;
}