Java Code Examples for cern.colt.matrix.impl.SparseDoubleMatrix2D#assign()

The following examples show how to use cern.colt.matrix.impl.SparseDoubleMatrix2D#assign() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScanningAlgorithm.java    From CFGScanDroid with GNU General Public License v2.0 4 votes vote down vote up
public static boolean scanMethodSubgraph(SparseDoubleMatrix2D signatureAdjacencyMatrix, SparseDoubleMatrix2D functionAdjacencyMatrix){
	// System.out.println("DEPTH: " + 0);
	// System.out.println(signatureAdjacencyMatrix);
	// System.out.println(functionAdjacencyMatrix);
	int signatureNodeCount = signatureAdjacencyMatrix.rows();
	int functionNodeCount = functionAdjacencyMatrix.rows();
	SparseDoubleMatrix2D candidateList = new SparseDoubleMatrix2D(signatureNodeCount, functionNodeCount);
	
	int[] signatureDepths = new int[signatureNodeCount];
	int[] functionDepths = new int[functionNodeCount];

	for(int functionEntry=0; functionEntry<functionNodeCount; ++functionEntry) {
		// arrays for tracking depths
		// function finds all nodes with no branches to them
		// first node (entry point) is always set at depth 0
		// System.out.println(functionEntry);
		candidateList.assign(0);

		Arrays.fill(signatureDepths, -1);
		Arrays.fill(functionDepths, -1);
	
		signatureDepths[0] = 0;
		functionDepths[functionEntry] = 0;

		// all nodes at depth 0 go into this vector
		SparseDoubleMatrix2D signatureVector = new SparseDoubleMatrix2D(1, signatureNodeCount);
		SparseDoubleMatrix2D functionVector = new SparseDoubleMatrix2D(1, functionNodeCount);

		// First node should be always be an entry point
		signatureVector.set(0, 0, 1.0);
		functionVector.set(0, functionEntry, 1.0);

		// get total children at depth 0+1
		// check function has sufficient nodes to satisfy sig at this depth
		// System.out.println("\t" + Algebra.DEFAULT.mult(signatureVector, signatureAdjacencyMatrix).cardinality());
		// System.out.println("\t" + Algebra.DEFAULT.mult(functionVector, functionAdjacencyMatrix).cardinality());
		if(Algebra.DEFAULT.mult(signatureVector, signatureAdjacencyMatrix).cardinality() > 
			Algebra.DEFAULT.mult(functionVector, functionAdjacencyMatrix).cardinality()) {
			// System.out.println("Insufficient function basic blocks at depth 1 to satisfy signature!");
			continue;
		}

		// build candidate list 
		candidateList.set(0, functionEntry, 1.0);

		// check bipartite match
		if(!bipartiteMatchingDepth(candidateList, signatureDepths, functionDepths, 0)) {
			// System.out.println("BIPARTITE MATCHING FAILED!");
			continue;
		}

		// check children recursively
		if(bfsCompare(candidateList, signatureAdjacencyMatrix, functionAdjacencyMatrix, signatureDepths, functionDepths, 1)) {
			return true;
		}
		// System.out.println();
	}

	return false;
}
 
Example 2
Source File: ScanningAlgorithm.java    From CFGScanDroid with GNU General Public License v2.0 4 votes vote down vote up
public static SparseDoubleMatrix2D checkIncestConditionParents(	DoubleMatrix2D signatureNodeIncestVector,
																DoubleMatrix2D functionNodeIncestVector,
																SparseDoubleMatrix2D signatureAdjacencyMatrix, 
																SparseDoubleMatrix2D functionAdjacencyMatrix,
																int[] signatureDepths,
																int[] functionDepths,
																int depth,
																SparseDoubleMatrix2D candidateList) {

	SparseDoubleMatrix2D signatureSelector = new SparseDoubleMatrix2D(1, signatureDepths.length);
	SparseDoubleMatrix2D functionSelector = new SparseDoubleMatrix2D(1, functionDepths.length);

	for(int i=0; i<signatureNodeIncestVector.size(); ++i) {
		if(signatureNodeIncestVector.get(0, i) == 0.0)
			continue;

		signatureSelector.assign(0.0);
		signatureSelector.set(0, i, 1.0);

		DoubleMatrix2D signatureNodeParentVector = Algebra.DEFAULT.mult(signatureSelector, Algebra.DEFAULT.transpose(signatureAdjacencyMatrix));

		// only bipartite match on incest parents
		for(int iParent = 0; iParent < signatureNodeParentVector.columns(); ++iParent) {
			if(signatureNodeParentVector.get(0, iParent) > 0 && signatureDepths[iParent] != depth) {
				signatureNodeParentVector.set(0, iParent, 0.0);
			}
		}

		int signatureIncestParentCount = signatureNodeParentVector.cardinality();

		for(int j=0; j<functionNodeIncestVector.size(); ++j) {
			if(candidateList.get(i, j) == 0.0)
				continue;

			functionSelector.assign(0.0);
			functionSelector.set(0, j, 1.0);

			DoubleMatrix2D functionNodeParentVector = Algebra.DEFAULT.mult(functionSelector, Algebra.DEFAULT.transpose(functionAdjacencyMatrix));

			// only bipartite match on incest parents
			for(int iParent = 0; iParent < functionNodeParentVector.columns(); ++iParent) {
				if(functionNodeParentVector.get(0, iParent) > 0 && functionDepths[iParent] != depth) {
					functionNodeParentVector.set(0, iParent, 0.0);
				}
			}

			int functionIncestParentCount = functionNodeParentVector.cardinality();

			// bipartite matching, if !sufficient
			if(	candidateList.get(i, j) > 0 && (signatureIncestParentCount > functionIncestParentCount ||
				!bipartiteMatchingVector(candidateList, signatureNodeParentVector, functionNodeParentVector, signatureDepths, functionDepths))) {
				// remove candidacy
				candidateList.set(i, j, 0.0);
			}
		}
	}

	return candidateList;
}
 
Example 3
Source File: ScanningAlgorithm.java    From CFGScanDroid with GNU General Public License v2.0 4 votes vote down vote up
public static SparseDoubleMatrix2D checkPreviouslyVisitedChildren(	IntArrayList signatureNonZeros, 
																	IntArrayList functionNonZeros,
																	SparseDoubleMatrix2D signatureAdjacencyMatrix, 
																	SparseDoubleMatrix2D functionAdjacencyMatrix,
																	int[] signatureDepths,
																	int[] functionDepths,
 																	SparseDoubleMatrix2D candidateList) {
	
	SparseDoubleMatrix2D signatureSelector = new SparseDoubleMatrix2D(1, signatureDepths.length);
	SparseDoubleMatrix2D functionSelector = new SparseDoubleMatrix2D(1, functionDepths.length);

	// reduce candidacy based on previously visited children
	for(int i=0; i<signatureNonZeros.size(); ++i) {
		// for node N, this sets the Nth bit of the vector to 1.0
		signatureSelector.assign(0.0);
		signatureSelector.set(0, signatureNonZeros.get(i), 1.0);

		// get children at depth <= current
		DoubleMatrix2D signatureNodeChildVector = Algebra.DEFAULT.mult(signatureSelector, signatureAdjacencyMatrix);

		// remove signature node's unvisited children
		for(int iChild = 0; iChild < signatureNodeChildVector.columns(); ++iChild) {
			if(signatureNodeChildVector.get(0, iChild) > 0 && signatureDepths[iChild] == -1) { // for the oposite conditional && signatureDepths[iChild] <= depth) {
				signatureNodeChildVector.set(0, iChild, 0.0);
			}
		}

		int signatureVisitedChildCount = signatureNodeChildVector.cardinality();

		for(int j=0; j<functionNonZeros.size(); ++j) {
			functionSelector.assign(0.0);
			functionSelector.set(0, functionNonZeros.get(j), 1.0);
			// get children at depth <= current
			DoubleMatrix2D functionNodeChildVector = Algebra.DEFAULT.mult(functionSelector, functionAdjacencyMatrix);

			// remove function node's unvisited children
			for(int iChild = 0; iChild < functionNodeChildVector.columns(); ++iChild) {
				if(functionNodeChildVector.get(0, iChild) > 0 && functionDepths[iChild] == -1) { // for the oposite conditional && functionDepths[iChild] <= depth) {
					functionNodeChildVector.set(0, iChild, 0.0);
				}
			}

			int functionVisitedChildCount = functionNodeChildVector.cardinality();
			
			// bipartite matching, if !sufficient
			if(	candidateList.get(signatureNonZeros.get(i), functionNonZeros.get(j)) > 0 && 
				(signatureVisitedChildCount > functionVisitedChildCount ||
				!bipartiteMatchingVector(candidateList, signatureNodeChildVector, functionNodeChildVector, signatureDepths, functionDepths))) {
				// remove candidacy
				candidateList.set(signatureNonZeros.get(i), functionNonZeros.get(j), 0.0);
			}
		}
	}

	return candidateList;
}