This module defines a specialized array class for representing adjacency of items of a single type. This is e.g. used in mesh models, to store the adjacent elements.
Classes defined in module adjacency
A class for storing and handling adjacency tables.
An adjacency table defines a neighbouring relation between elements of a single collection. The nature of the relation is not important, but should be a binary relation: two elements are either related or they are not.
Typical applications in pyFormex are the adjacency tables for storing elements connected by a node, or by an edge, or by a node but not by an edge, etcetera.
Conceptually the adjacency table corresponds with a graph. In graph theory however the data are usually stored as a set of tuples (a,b) indicating a connection between the elements a and b. In pyFormex elements are numbered consecutively from 0 to nelems-1, where nelems is the number of elements. If the user wants another numbering, he can always keep an array with the actual numbers himself. Connections between elements are stored in an efficient two-dimensional array, holding a row for each element. This row contains the numbers of the connected elements. Because the number of connections can be different for each element, the rows are padded with an invalid elements number (-1).
A normalized Adjacency is one where all rows do not contain duplicate nonnegative entries and are sorted in ascending order and where no column contains only -1 values. Also, since the adjacency is defined within a single collection, no row should contain a value higher than the maximum row index.
A new Adjacency table is created with the following syntax
Adjacency(data=[],dtyp=None,copy=False,ncon=0,normalize=True)
Parameters:
Example:
>>> print Adjacency([[1,2,-1],
... [3,2,0],
... [1,-1,3],
... [1,2,-1],
... [-1,-1,-1]])
[[-1 1 2]
[ 0 2 3]
[-1 1 3]
[-1 1 2]
[-1 -1 -1]]
Return the number of elements in the Adjacency table.
Return the maximum number of connections for any element.
This returns the row count of the Adjacency.
Normalize an adjacency table.
A normalized adjacency table is one where each row:
and that has at least one row without -1 value.
By default, an Adjacency is normalized when it is constructed. Performing operations on an Adjacency may however leave it in a non-normalized state. Calling this method will normalize it again. This can obviously also be obtained by creating a new Adjacency with self as data.
Returns: an integer array with shape (adj.shape[0],maxc), with maxc <= adj.shape[1], where row i retains the unique non-negative numbers of the original array except the value i, and is possibly padded with -1 values.
Example:
>>> a = Adjacency([[ 0, 0, 0, 1, 2, 5],
... [-1, 0, 1, -1, 1, 3],
... [-1, -1, 0, -1, -1, 2],
... [-1, -1, 1, -1, -1, 3],
... [-1, -1, -1, -1, -1, -1],
... [-1, -1, 0, -1, -1, 5]])
>>> a.normalize()
Adjacency([[ 1, 2, 5],
[-1, 0, 3],
[-1, -1, 0],
[-1, -1, 1],
[-1, -1, -1],
[-1, -1, 0]])
Return all pairs of adjacent element.
Returns an integer array with two columns, where each row contains a pair of adjacent elements. The element number in the first columne is always the smaller of the two element numbers.
Return the symmetric difference of two adjacency tables.
Parameters:
Returns an adjacency table of the same length, where each row contains all the (nonnegative) numbers of the corresponding rows of self and adj, except those that occur in both.
Generator function returning the frontal elements.
This is a generator function and is normally not used directly, but via the frontWalk() method.
It returns an int array with a value for each element. On the initial call, all values are -1, except for the elements in the initial front, which get a value 0. At each call a new front is created with all the elements that are connected to any of the current front and which have not yet been visited. The new front elements get a value equal to the last front’s value plus the frontinc. If the front becomes empty and a new starting front is created, the front value is extra incremented with partinc.
Parameters: see frontWalk().
Example:
>>> A = Adjacency([[1,2,-1],
... [3,2,0],
... [1,-1,3],
... [1,2,-1],
... [-1,-1,-1]])
>>> for p in A.frontFactory(): print p
[ 0 -1 -1 -1 -1]
[ 0 1 1 -1 -1]
[ 0 1 1 2 -1]
[0 1 1 2 4]
Walks through the elements by their node front.
A frontal walk is executed starting from the given element(s). A number of steps is executed, each step advancing the front over a given number of single pass increments. The step number at which an element is reached is recorded and returned.
Parameters:
Returns: an array of integers specifying for each element in which step the element was reached by the walker.
Example:
>>> A = Adjacency([
... [-1, 1, 2, 3],
... [-1, 0, 2, 3],
... [ 0, 1, 4, 5],
... [-1, -1, 0, 1],
... [-1, -1, 2, 5],
... [-1, -1, 2, 4]])
>>> print A.frontWalk()
[0 1 1 1 2 2]
Functions defined in module adjacency
Sort an adjacency table.
An adjacency table is an integer array where each row lists the numbers of the items that are connected to the item with number equal to the row index. Rows are padded with -1 value to create rows of equal length.
This function sorts the rows of the adjacency table in ascending order and removes all columns containing only -1 values.
Paramaters:
Returns: an integer array with shape (adj.shape[0],maxc), with maxc <= adj.shape[1], where the rows are sorted in ascending order and where columns with only -1 values are removed.
Example:
>>> a = array([[ 0, 2, 1, -1],
... [-1, 3, 1, -1],
... [ 3, -1, 0, 1],
... [-1, -1, -1, -1]])
>>> sortAdjacency(a)
array([[ 0, 1, 2],
[-1, 1, 3],
[ 0, 1, 3],
[-1, -1, -1]])
Reduce an adjacency table.
An adjacency table is an integer array where each row lists the numbers of the items that are connected to the item with number equal to the row index. Rows are padded with -1 values to create rows of equal length.
A reduced adjacency table is one where each row:
and that has at least one row without -1 value.
Paramaters:
Returns: an integer array with shape (adj.shape[0],maxc), with maxc <= adj.shape[1], where row i retains the unique non-negative numbers of the original array except the value i, and is possibly padded with -1 values.
Example:
>>> a = array([[ 0, 0, 0, 1, 2, 5],
... [-1, 0, 1, -1, 1, 3],
... [-1, -1, 0, -1, -1, 2],
... [-1, -1, 1, -1, -1, 3],
... [-1, -1, -1, -1, -1, -1],
... [-1, -1, 0, -1, -1, 5]])
>>> reduceAdjacency(a)
array([[ 1, 2, 5],
[-1, 0, 3],
[-1, -1, 0],
[-1, -1, 1],
[-1, -1, -1],
[-1, -1, 0]])