示例#1
0
    def testNonzero(self):
        try:
            from pysparse import spmatrix
            from apgl.util.PySparseUtils import PySparseUtils
        except ImportError as error:
            return

        n = 10
        X = spmatrix.ll_mat(n, n)

        self.assertTrue(
            (PySparseUtils.nonzero(X)[0] == numpy.array([], numpy.int)).all())
        self.assertTrue(
            (PySparseUtils.nonzero(X)[0] == numpy.array([], numpy.int)).all())

        X[1, 1] = 5
        X[2, 4] = 6.1
        X[3, 1] = 2.5

        self.assertTrue(
            (PySparseUtils.nonzero(X)[0] == numpy.array([1, 2, 3],
                                                        numpy.int)).all())
        self.assertTrue(
            (PySparseUtils.nonzero(X)[1] == numpy.array([1, 4, 1],
                                                        numpy.int)).all())
    def multiply(self, graph):
        """
        Multiply the edge weights of the input graph to the current one. Results in an
        intersection of the edges.

        :param graph: the input graph.
        :type graph: :class:`apgl.graph.PySparseGraph`

        :returns: A new graph with edge weights which are multiples of the current and graph
        """
        Parameter.checkClass(graph, PySparseGraph)
        if graph.getNumVertices() != self.getNumVertices():
            raise ValueError(
                "Can only add edges from graph with same number of vertices")
        if self.undirected != graph.undirected:
            raise ValueError(
                "Both graphs must be either undirected or directed")

        if self.W.nnz < graph.W.nnz:
            (rows, cols) = PySparseUtils.nonzero(self.W)
        else:
            (rows, cols) = PySparseUtils.nonzero(graph.W)

        arr1 = numpy.zeros(len(rows))
        arr2 = numpy.zeros(len(rows))
        self.W.take(arr1, rows, cols)
        graph.W.take(arr2, rows, cols)

        arr1 = arr1 * arr2

        newGraph = PySparseGraph(self.vList, self.undirected)
        newGraph.W.put(arr1, rows, cols)
        return newGraph
    def multiply(self, graph):
        """
        Multiply the edge weights of the input graph to the current one. Results in an
        intersection of the edges.

        :param graph: the input graph.
        :type graph: :class:`apgl.graph.PySparseGraph`

        :returns: A new graph with edge weights which are multiples of the current and graph
        """
        Parameter.checkClass(graph, PySparseGraph)
        if graph.getNumVertices() != self.getNumVertices():
            raise ValueError("Can only add edges from graph with same number of vertices")
        if self.undirected != graph.undirected:
            raise ValueError("Both graphs must be either undirected or directed")

        if self.W.nnz < graph.W.nnz:
            (rows, cols) = PySparseUtils.nonzero(self.W)
        else:
            (rows, cols) = PySparseUtils.nonzero(graph.W)

        arr1 = numpy.zeros(len(rows))
        arr2 = numpy.zeros(len(rows))
        self.W.take(arr1, rows, cols)
        graph.W.take(arr2, rows, cols)

        arr1 = arr1 * arr2

        newGraph = PySparseGraph(self.vList, self.undirected)
        newGraph.W.put(arr1, rows, cols)
        return newGraph
    def setDiff(self, graph):
        """
        Find the edges in the current graph which are not present in the input
        graph.

        :param graph: the input graph.
        :type graph: :class:`apgl.graph.PySparseGraph`

        :returns: A new graph with edges from the current graph and not in the input graph.
        """
        Parameter.checkClass(graph, PySparseGraph)
        if graph.getNumVertices() != self.getNumVertices():
            raise ValueError("Can only add edges from graph with same number of vertices")
        if self.undirected != graph.undirected:
            raise ValueError("Both graphs must be either undirected or directed")

        A1 = self.nativeAdjacencyMatrix()
        A2 = graph.nativeAdjacencyMatrix()

        (rows, cols) = PySparseUtils.nonzero(A1)
        arr1 = numpy.zeros(len(rows))
        arr2 = numpy.zeros(len(rows))

        A1.take(arr1, rows, cols)
        A2.take(arr2, rows, cols)
        arr1 = arr1 - arr2

        A1.put(arr1, rows, cols)

        newGraph = PySparseGraph(self.vList, self.undirected)
        newGraph.W = A1
        return newGraph
    def setDiff(self, graph):
        """
        Find the edges in the current graph which are not present in the input
        graph.

        :param graph: the input graph.
        :type graph: :class:`apgl.graph.PySparseGraph`

        :returns: A new graph with edges from the current graph and not in the input graph.
        """
        Parameter.checkClass(graph, PySparseGraph)
        if graph.getNumVertices() != self.getNumVertices():
            raise ValueError(
                "Can only add edges from graph with same number of vertices")
        if self.undirected != graph.undirected:
            raise ValueError(
                "Both graphs must be either undirected or directed")

        A1 = self.nativeAdjacencyMatrix()
        A2 = graph.nativeAdjacencyMatrix()

        (rows, cols) = PySparseUtils.nonzero(A1)
        arr1 = numpy.zeros(len(rows))
        arr2 = numpy.zeros(len(rows))

        A1.take(arr1, rows, cols)
        A2.take(arr2, rows, cols)
        arr1 = arr1 - arr2

        A1.put(arr1, rows, cols)

        newGraph = PySparseGraph(self.vList, self.undirected)
        newGraph.W = A1
        return newGraph
示例#6
0
    def testSum(self):
        try:
            from pysparse import spmatrix
            from apgl.util.PySparseUtils import PySparseUtils
        except ImportError as error:
            return

        n = 10
        X = spmatrix.ll_mat(n, n)

        self.assertEquals(PySparseUtils.sum(X), 0.0)

        X[1, 1] = 5
        X[2, 4] = 6.1
        X[3, 1] = 2.5

        self.assertEquals(PySparseUtils.sum(X), 13.6)
示例#7
0
    def testSum(self):
        try:
            from pysparse import spmatrix
            from apgl.util.PySparseUtils import PySparseUtils
        except ImportError as error:
            return

        n = 10
        X = spmatrix.ll_mat(n, n)

        self.assertEquals(PySparseUtils.sum(X), 0.0)

        X[1, 1] = 5
        X[2, 4] = 6.1
        X[3, 1] = 2.5

        self.assertEquals(PySparseUtils.sum(X), 13.6)
示例#8
0
    def testNonzero(self):
        try:
            from pysparse import spmatrix
            from apgl.util.PySparseUtils import PySparseUtils
        except ImportError as error:
            return

        n = 10
        X = spmatrix.ll_mat(n, n)

        self.assertTrue((PySparseUtils.nonzero(X)[0]==numpy.array([], numpy.int)).all())
        self.assertTrue((PySparseUtils.nonzero(X)[0]==numpy.array([], numpy.int)).all())

        X[1, 1] = 5
        X[2, 4] = 6.1
        X[3, 1] = 2.5

        self.assertTrue((PySparseUtils.nonzero(X)[0]==numpy.array([1,2,3], numpy.int)).all())
        self.assertTrue((PySparseUtils.nonzero(X)[1]==numpy.array([1,4,1], numpy.int)).all())
    def nativeAdjacencyMatrix(self):
        """
        Return the adjacency matrix in sparse format.
        """

        A = spmatrix.ll_mat(self.vList.getNumVertices(), self.vList.getNumVertices())

        nonzeros = PySparseUtils.nonzero(self.W)
        A.put(1, nonzeros[0], nonzeros[1])
        return A
    def nativeAdjacencyMatrix(self):
        """
        Return the adjacency matrix in sparse format.
        """

        A = spmatrix.ll_mat(self.vList.getNumVertices(),
                            self.vList.getNumVertices())

        nonzeros = PySparseUtils.nonzero(self.W)
        A.put(1, nonzeros[0], nonzeros[1])
        return A
    def getAllDirEdges(self):
        """
        Returns the set of directed edges of the current graph as a matrix in which each
        row corresponds to an edge. For an undirected graph, there is an edge from
        v1 to v2 and from v2 to v1 if v2!=v1.

        :returns: A matrix with 2 columns, and each row corresponding to an edge.
        """
        (rows, cols) = PySparseUtils.nonzero(self.W)
        edges = numpy.c_[rows, cols]

        return edges
    def getAllDirEdges(self):
        """
        Returns the set of directed edges of the current graph as a matrix in which each
        row corresponds to an edge. For an undirected graph, there is an edge from
        v1 to v2 and from v2 to v1 if v2!=v1.

        :returns: A matrix with 2 columns, and each row corresponding to an edge.
        """
        (rows, cols) = PySparseUtils.nonzero(self.W)
        edges = numpy.c_[rows, cols]

        return edges
    def neighbourOf(self, vertexIndex):
        """
        Return an array of the indices of vertices than have an edge going to the input
        vertex.

        :param vertexIndex: the index of a vertex.
        :type vertexIndex: :class:`int`

        :returns: An array of the indices of all vertices with an edge towards the input vertex.
        """
        Parameter.checkIndex(vertexIndex, 0, self.vList.getNumVertices())
        neighbours = PySparseUtils.nonzero(self.W[:, vertexIndex])[0]

        return numpy.array(neighbours)
    def neighbourOf(self, vertexIndex):
        """
        Return an array of the indices of vertices than have an edge going to the input
        vertex.

        :param vertexIndex: the index of a vertex.
        :type vertexIndex: :class:`int`

        :returns: An array of the indices of all vertices with an edge towards the input vertex.
        """
        Parameter.checkIndex(vertexIndex, 0, self.vList.getNumVertices())
        neighbours = PySparseUtils.nonzero(self.W[:, vertexIndex])[0]

        return numpy.array(neighbours)
    def neighbours(self, vertexIndex):
        """
        Return an array of the indices of neighbours. In the case of a directed
        graph it is an array of those vertices connected by an edge from the current
        one.

        :param vertexIndex: the index of a vertex.
        :type vertexIndex: :class:`int`

        :returns: An array of the indices of all neigbours of the input vertex.
        """
        Parameter.checkIndex(vertexIndex, 0, self.vList.getNumVertices())
        neighbours = PySparseUtils.nonzero(self.W[int(vertexIndex), :])[1]

        return numpy.array(neighbours)
    def neighbours(self, vertexIndex):
        """
        Return an array of the indices of neighbours. In the case of a directed
        graph it is an array of those vertices connected by an edge from the current
        one.

        :param vertexIndex: the index of a vertex.
        :type vertexIndex: :class:`int`

        :returns: An array of the indices of all neigbours of the input vertex.
        """
        Parameter.checkIndex(vertexIndex, 0, self.vList.getNumVertices())
        neighbours = PySparseUtils.nonzero(self.W[int(vertexIndex), :])[1]

        return numpy.array(neighbours)
示例#17
0
 def runSum():     
     for i in range(1000):  
          i = PySparseUtils.sum(A)
     print(i)
示例#18
0
 def runNonZeros():
     for i in range(1000):
         (rows, cols) = PySparseUtils.nonzero(A)
         nzVals = numpy.zeros(len(rows))
         A.take(nzVals, rows, cols)
示例#19
0
 def runNonZeros(): 
     for i in range(1000):
         (rows, cols) = PySparseUtils.nonzero(A)
         nzVals = numpy.zeros(len(rows))
         A.take(nzVals, rows, cols)
示例#20
0
 def runSum():
     for i in range(1000):
         i = PySparseUtils.sum(A)
     print(i)