GraphMatrix API Reference¶
Type: groggy.GraphMatrix
Overview¶
Matrix representation of graph data (adjacency, Laplacian, embeddings).
Primary Use Cases: - Matrix-based graph algorithms - Spectral analysis - Graph embeddings
Related Objects:
- Graph
- Subgraph
Complete Method Reference¶
The following methods are available on GraphMatrix objects. This reference is generated from comprehensive API testing and shows all empirically validated methods.
| Method | Returns | Status |
|---|---|---|
abs() |
GraphMatrix |
✓ |
apply() |
GraphMatrix |
✓ |
backward() |
? |
✗ |
cholesky_decomposition() |
? |
✗ |
columns() |
list |
✓ |
concatenate() |
? |
✗ |
data() |
list |
✓ |
dense() |
GraphMatrix |
✓ |
dense_html_repr() |
str |
✓ |
determinant() |
? |
✗ |
dropout() |
? |
✗ |
dtype() |
str |
✓ |
eigenvalue_decomposition() |
? |
✗ |
elementwise_multiply() |
? |
✗ |
elu() |
GraphMatrix |
✓ |
exp() |
GraphMatrix |
✓ |
filter() |
? |
✗ |
flatten() |
NumArray |
✓ |
from_base_array() |
? |
✗ |
from_data() |
? |
✗ |
from_flattened() |
? |
✗ |
from_graph_attributes() |
? |
✗ |
gelu() |
GraphMatrix |
✓ |
get() |
? |
✗ |
get_cell() |
? |
✗ |
get_column() |
? |
✗ |
get_column_by_name() |
? |
✗ |
get_row() |
? |
✗ |
grad() |
? |
✓ |
identity() |
GraphMatrix |
✓ |
inverse() |
? |
✗ |
is_empty() |
bool |
✓ |
is_numeric() |
bool |
✓ |
is_sparse() |
bool |
✓ |
is_square() |
bool |
✓ |
is_symmetric() |
bool |
✓ |
iter_columns() |
list |
✓ |
iter_rows() |
list |
✓ |
leaky_relu() |
GraphMatrix |
✓ |
log() |
GraphMatrix |
✓ |
lu_decomposition() |
? |
✗ |
map() |
GraphMatrix |
✓ |
max() |
float |
✓ |
max_axis() |
? |
✗ |
mean() |
float |
✓ |
mean_axis() |
? |
✗ |
min() |
float |
✓ |
min_axis() |
? |
✗ |
multiply() |
? |
✗ |
norm() |
float |
✓ |
norm_inf() |
float |
✓ |
norm_l1() |
float |
✓ |
ones() |
? |
✗ |
power() |
? |
✗ |
preview() |
list |
✓ |
qr_decomposition() |
tuple |
✓ |
rank() |
int |
✓ |
relu() |
GraphMatrix |
✓ |
repeat() |
? |
✗ |
requires_grad() |
bool |
✓ |
requires_grad_() |
? |
✗ |
reshape() |
? |
✗ |
rich_display() |
str |
✓ |
scalar_multiply() |
? |
✗ |
set() |
? |
✗ |
shape() |
tuple |
✓ |
sigmoid() |
GraphMatrix |
✓ |
softmax() |
GraphMatrix |
✓ |
solve() |
? |
✗ |
split() |
? |
✗ |
sqrt() |
GraphMatrix |
✓ |
stack() |
? |
✗ |
std_axis() |
? |
✗ |
sum() |
float |
✓ |
sum_axis() |
? |
✗ |
summary() |
str |
✓ |
svd() |
tuple |
✓ |
tanh() |
GraphMatrix |
✓ |
tile() |
? |
✗ |
to_base_array() |
BaseArray |
✓ |
to_degree_matrix() |
? |
✗ |
to_dict() |
dict |
✓ |
to_laplacian() |
? |
✗ |
to_list() |
list |
✓ |
to_normalized_laplacian() |
? |
✗ |
to_numpy() |
ndarray |
✓ |
to_pandas() |
DataFrame |
✓ |
to_table_for_streaming() |
BaseTable |
✓ |
trace() |
? |
✗ |
transpose() |
GraphMatrix |
✓ |
var_axis() |
? |
✗ |
zero_grad() |
? |
✗ |
zeros() |
? |
✗ |
Legend:
- ✓ = Method tested and working
- ✗ = Method failed in testing or not yet validated
- ? = Return type not yet determined
Detailed Method Reference¶
Creating GraphMatrix¶
GraphMatrices are typically created from graphs:
import groggy as gr
g = gr.generators.karate_club()
# From graph structure
A = g.adjacency_matrix() # Binary adjacency
L = g.laplacian_matrix() # Graph Laplacian
W = g.edges.weight_matrix() # Weighted adjacency
# Manual creation
data = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
M = gr.matrix(data)
# Identity matrix
I = gr.GraphMatrix.identity(5) # 5x5 identity
Properties¶
shape()¶
Get matrix dimensions.
Returns:
- tuple[int, int]: (rows, columns)
Example:
Performance: O(1)
dtype()¶
Get data type.
Returns:
- str: Type name ("float", "int", etc.)
Example:
is_sparse()¶
Check if stored as sparse matrix.
Returns:
- bool: True if sparse
Example:
A = g.adjacency_matrix()
if A.is_sparse():
print("Sparse representation (efficient for large graphs)")
else:
print("Dense representation")
Notes: - Groggy automatically chooses representation based on sparsity - Sparse storage saves memory for graphs with few edges
is_square()¶
Check if matrix is square.
Returns:
- bool: True if n×n
Example:
is_symmetric()¶
Check if matrix is symmetric.
Returns:
- bool: True if A = A^T
Example:
A = g.adjacency_matrix()
if A.is_symmetric():
print("Undirected graph")
else:
print("Directed graph")
is_numeric()¶
Check if contains numeric values.
Returns:
- bool: True if numeric
Example:
is_empty()¶
Check if matrix has no elements.
Returns:
- bool: True if empty
Example:
Statistical Methods¶
sum()¶
Sum of all elements.
Returns:
- float: Sum
Example:
A = g.adjacency_matrix()
total_edges = A.sum() / 2 # Divide by 2 for undirected
print(f"{total_edges} edges")
mean()¶
Mean of all elements.
Returns:
- float: Mean value
Example:
min() / max()¶
Minimum/maximum values.
Returns:
- float: Min or max value
Example:
W = g.edges.weight_matrix()
min_weight = W.min()
max_weight = W.max()
print(f"Weight range: [{min_weight}, {max_weight}]")
Norms¶
norm()¶
Frobenius norm.
Returns:
- float: ||A||_F = sqrt(sum(A_ij^2))
Example:
norm_l1()¶
L1 norm (max column sum).
Returns:
- float: L1 norm
Example:
norm_inf()¶
Infinity norm (max row sum).
Returns:
- float: L∞ norm
Example:
Activation Functions (for GNNs)¶
relu()¶
ReLU activation: max(0, x).
Returns:
- GraphMatrix: Activated matrix
Example:
leaky_relu()¶
Leaky ReLU: max(αx, x) where α is small.
Returns:
- GraphMatrix: Activated matrix
Example:
elu()¶
Exponential Linear Unit.
Returns:
- GraphMatrix: Activated matrix
Example:
gelu()¶
Gaussian Error Linear Unit.
Returns:
- GraphMatrix: Activated matrix
Example:
sigmoid()¶
Sigmoid: 1/(1 + e^(-x)).
Returns:
- GraphMatrix: Values in (0, 1)
Example:
tanh()¶
Hyperbolic tangent.
Returns:
- GraphMatrix: Values in (-1, 1)
Example:
softmax()¶
Softmax normalization.
Returns:
- GraphMatrix: Rows sum to 1
Example:
Element-wise Operations¶
abs()¶
Absolute values.
Returns:
- GraphMatrix: |A_ij|
Example:
exp()¶
Exponential: e^A_ij.
Returns:
- GraphMatrix: Exponential of each element
Example:
log()¶
Natural logarithm.
Returns:
- GraphMatrix: ln(A_ij)
Example:
sqrt()¶
Square root.
Returns:
- GraphMatrix: sqrt(A_ij)
Example:
map(func) / apply(func)¶
Apply function to each element.
Parameters:
- func: Function to apply
Returns:
- GraphMatrix: Transformed matrix
Example:
# Square each element
squared = A.map(lambda x: x ** 2)
# Threshold
thresholded = A.map(lambda x: x if x > 0.5 else 0)
Matrix Decompositions¶
svd()¶
Singular Value Decomposition.
Returns:
- tuple: (U, S, V) matrices
Example:
A = g.adjacency_matrix()
U, S, V = A.svd()
# Use for dimensionality reduction
k = 10
U_k = U[:, :k] # Conceptual - actual indexing may differ
qr_decomposition()¶
QR decomposition.
Returns:
- tuple[GraphMatrix, GraphMatrix]: (Q, R) matrices
Example:
rank()¶
Matrix rank.
Returns:
- int: Rank
Example:
Data Access¶
data()¶
Get raw data as list of lists.
Returns:
- list[list]: Row-major data
Example:
flatten()¶
Flatten to 1D array.
Returns:
- NumArray: Flattened values
Example:
to_list()¶
Convert to nested list.
Returns:
- list: Same as data()
Example:
Iteration¶
iter_rows()¶
Iterate over rows.
Returns: - Iterator over rows
Example:
iter_columns()¶
Iterate over columns.
Returns: - Iterator over columns
Example:
columns()¶
Get column names (if applicable).
Returns:
- list[str]: Column names
Example:
Conversion¶
dense()¶
Convert to dense representation.
Returns:
- GraphMatrix: Dense matrix
Example:
Notes: Only needed if you require dense storage
to_numpy()¶
Convert to NumPy array.
Returns:
- numpy.ndarray: NumPy array
Example:
import numpy as np
A = g.adjacency_matrix()
np_array = A.to_numpy()
print(type(np_array)) # numpy.ndarray
to_pandas()¶
Convert to pandas DataFrame.
Returns:
- pandas.DataFrame: DataFrame
Example:
to_dict()¶
Convert to dictionary.
Returns:
- dict: Matrix as dict
Example:
Transformations¶
transpose()¶
Matrix transpose.
Returns:
- GraphMatrix: A^T
Example:
A = g.adjacency_matrix()
AT = A.transpose()
# Check symmetry
if (A.data() == AT.data()):
print("Symmetric (undirected graph)")
Display¶
summary()¶
Get text summary.
Returns:
- str: Summary string
Example:
preview(n=5)¶
Preview first n rows/cols.
Returns: - Display output
Example:
Automatic Differentiation¶
requires_grad()¶
Check if gradients are tracked.
Returns:
- bool: True if tracking gradients
Example:
grad()¶
Get gradient (after backward pass).
Returns:
- GraphMatrix or None: Gradient
Example:
Usage Patterns¶
Pattern 1: Spectral Analysis¶
# Get Laplacian
L = g.laplacian_matrix()
# Convert to NumPy for eigenvalue decomposition
import numpy as np
L_np = L.to_numpy()
# Compute eigenvectors
eigenvalues, eigenvectors = np.linalg.eigh(L_np)
# Use for spectral clustering
Pattern 2: GNN Forward Pass¶
# Adjacency matrix
A = g.adjacency_matrix()
# Feature matrix (from node attributes)
ages = g.nodes["age"].to_list()
scores = g.nodes["score"].to_list()
X = np.column_stack([ages, scores])
# GNN layer: H = σ(A @ X @ W)
# (Simplified - actual GNN more complex)
H = A.to_numpy() @ X # Message passing
H_activated = gr.matrix(H.tolist()).relu() # Activation
Pattern 3: Sparse Matrix Operations¶
A = g.adjacency_matrix()
if A.is_sparse():
print(f"Sparse: {A.shape()} with {A.sum()} nonzeros")
# Efficient operations on sparse
stats = {
'mean': A.mean(),
'max': A.max(),
'norm': A.norm()
}
Pattern 4: Matrix Normalization¶
W = g.edges.weight_matrix()
# Min-max normalization
min_w = W.min()
max_w = W.max()
W_norm = W.map(lambda x: (x - min_w) / (max_w - min_w) if max_w > min_w else x)
# Or standardization
mean_w = W.mean()
# std_w = W.std() # If available
Pattern 5: Attention Mechanism¶
# Compute attention scores
A = g.adjacency_matrix()
# Apply softmax for attention weights
attention = A.softmax()
# Weighted aggregation
# features_aggregated = attention @ node_features
Quick Reference¶
Properties¶
| Method | Returns | Description |
|---|---|---|
shape() |
tuple |
(rows, cols) |
dtype() |
str |
Data type |
is_sparse() |
bool |
Sparse storage? |
is_square() |
bool |
n×n matrix? |
is_symmetric() |
bool |
A = A^T? |
Statistics¶
| Method | Returns | Description |
|---|---|---|
sum() |
float |
Sum of elements |
mean() |
float |
Mean value |
min() / max() |
float |
Min/max value |
norm() |
float |
Frobenius norm |
norm_l1() |
float |
L1 norm |
norm_inf() |
float |
L∞ norm |
Activations (GNN)¶
| Method | Description |
|---|---|
relu() |
max(0, x) |
leaky_relu() |
Leaky ReLU |
elu() |
Exponential Linear Unit |
gelu() |
Gaussian Error Linear Unit |
sigmoid() |
1/(1 + e^(-x)) |
tanh() |
Hyperbolic tangent |
softmax() |
Row normalization |
Element-wise¶
| Method | Description |
|---|---|
abs() |
Absolute values |
exp() |
e^x |
log() |
Natural log |
sqrt() |
Square root |
map(func) |
Apply function |
Decompositions¶
| Method | Returns | Description |
|---|---|---|
svd() |
tuple |
U, S, V matrices |
qr_decomposition() |
tuple |
Q, R matrices |
rank() |
int |
Matrix rank |
Conversion¶
| Method | Returns | Description |
|---|---|---|
to_numpy() |
ndarray |
NumPy array |
to_pandas() |
DataFrame |
Pandas DataFrame |
data() |
list |
List of lists |
flatten() |
NumArray |
1D array |
Performance Considerations¶
Sparse vs Dense: - Large graphs (>1000 nodes, sparse): Sparse saves memory - Small/dense graphs: Dense may be faster - Groggy chooses automatically based on sparsity
Efficient Operations:
- Element-wise: abs(), exp(), relu() - O(nnz) for sparse
- Statistics: sum(), mean(), norm() - Single pass
- Conversion: to_numpy() - O(nnz) or O(n²)
Less Efficient:
- dense() on large sparse - Forces materialization
- Repeated conversions - Cache the NumPy/pandas result
- Element access in loops - Use bulk operations
Best Practices:
# ✅ Good: check sparsity first
if A.is_sparse():
# Work with sparse methods
nnz = A.sum()
else:
# Dense operations
A_np = A.to_numpy()
# ✅ Good: batch operations
activated = A.relu().sigmoid()
# ❌ Avoid: repeated conversions
for i in range(100):
np_array = A.to_numpy() # Reconverts each time
# ✅ Good: convert once
A_np = A.to_numpy()
for i in range(100):
# Use A_np
Integration with SciPy¶
For advanced matrix operations:
from scipy.sparse import csr_matrix
from scipy.sparse.linalg import eigsh
# Convert to scipy sparse
A = g.adjacency_matrix()
A_scipy = csr_matrix(A.to_numpy())
# Use scipy algorithms
eigenvalues, eigenvectors = eigsh(A_scipy, k=10)
See Also¶
- Matrices Guide: Comprehensive tutorial
- Neural Guide: GNN usage patterns
- Graph API: Creating matrices from graphs
- NumArray API: Working with flattened matrices
Additional Methods¶
apply()¶
Apply.
Returns:
- GraphMatrix: Return value
Example:
backward()¶
Backward.
Returns:
- None: Return value
Example:
cholesky_decomposition()¶
Cholesky Decomposition.
Returns:
- None: Return value
Example:
concatenate(other, axis)¶
Concatenate.
Parameters:
- other: other
- axis: axis
Returns:
- None: Return value
Example:
dense_html_repr()¶
Dense Html Repr.
Returns:
- str: Return value
Example:
determinant()¶
Determinant.
Returns:
- None: Return value
Example:
dropout(p)¶
Dropout.
Parameters:
- p: p
Returns:
- None: Return value
Example:
eigenvalue_decomposition()¶
Eigenvalue Decomposition.
Returns:
- None: Return value
Example:
elementwise_multiply(other)¶
Elementwise Multiply.
Parameters:
- other: other
Returns:
- None: Return value
Example:
filter(condition)¶
Filter.
Parameters:
- condition: condition
Returns:
- None: Return value
Example:
from_base_array(base_array, rows, cols)¶
From Base Array.
Parameters:
- base_array: base array
- rows: rows
- cols: cols
Returns:
- None: Return value
Example:
from_data(data)¶
From Data.
Parameters:
- data: data
Returns:
- None: Return value
Example:
from_flattened(num_array, rows, cols)¶
From Flattened.
Parameters:
- num_array: num array
- rows: rows
- cols: cols
Returns:
- None: Return value
Example:
from_graph_attributes(_graph, _attrs, _entities)¶
From Graph Attributes.
Parameters:
- _graph: graph
- _attrs: attrs
- _entities: entities
Returns:
- None: Return value
Example:
get(col)¶
Get.
Parameters:
- col: col
Returns:
- None: Return value
Example:
get_cell(col)¶
Get Cell.
Parameters:
- col: col
Returns:
- None: Return value
Example:
get_column()¶
Get Column.
Returns:
- None: Return value
Example:
get_column_by_name()¶
Get Column By Name.
Returns:
- None: Return value
Example:
get_row(row)¶
Get Row.
Parameters:
- row: row
Returns:
- None: Return value
Example:
identity()¶
Identity.
Returns:
- GraphMatrix: Return value
Example:
inverse()¶
Inverse.
Returns:
- None: Return value
Example:
lu_decomposition()¶
Lu Decomposition.
Returns:
- None: Return value
Example:
max()¶
Max.
Returns:
- float: Return value
Example:
max_axis(axis)¶
Max Axis.
Parameters:
- axis: axis
Returns:
- None: Return value
Example:
mean_axis(axis)¶
Mean Axis.
Parameters:
- axis: axis
Returns:
- None: Return value
Example:
min_axis(axis)¶
Min Axis.
Parameters:
- axis: axis
Returns:
- None: Return value
Example:
multiply(operand)¶
Multiply.
Parameters:
- operand: operand
Returns:
- None: Return value
Example:
ones(rows, cols)¶
Ones.
Parameters:
- rows: rows
- cols: cols
Returns:
- None: Return value
Example:
power()¶
Power.
Returns:
- None: Return value
Example:
repeat(repeats, axis)¶
Repeat.
Parameters:
- repeats: repeats
- axis: axis
Returns:
- None: Return value
Example:
requires_grad_(requires_grad)¶
Requires Grad .
Parameters:
- requires_grad: requires grad
Returns:
- None: Return value
Example:
reshape(new_rows, new_cols)¶
Reshape.
Parameters:
- new_rows: new rows
- new_cols: new cols
Returns:
- None: Return value
Example:
rich_display()¶
Rich Display.
Returns:
- str: Return value
Example:
scalar_multiply(scalar)¶
Scalar Multiply.
Parameters:
- scalar: scalar
Returns:
- None: Return value
Example:
set(col, value)¶
Set.
Parameters:
- col: col
- value: value
Returns:
- None: Return value
Example:
solve(b)¶
Solve.
Parameters:
- b: b
Returns:
- None: Return value
Example:
split(split_points, axis)¶
Split.
Parameters:
- split_points: split points
- axis: axis
Returns:
- None: Return value
Example:
stack(other, axis)¶
Stack.
Parameters:
- other: other
- axis: axis
Returns:
- None: Return value
Example:
std_axis(axis)¶
Std Axis.
Parameters:
- axis: axis
Returns:
- None: Return value
Example:
sum_axis(axis)¶
Sum Axis.
Parameters:
- axis: axis
Returns:
- None: Return value
Example:
tile(reps)¶
Tile.
Parameters:
- reps: reps
Returns:
- None: Return value
Example:
to_base_array()¶
To Base Array.
Returns:
- BaseArray: Return value
Example:
to_degree_matrix()¶
To Degree Matrix.
Returns:
- None: Return value
Example:
to_laplacian()¶
To Laplacian.
Returns:
- None: Return value
Example:
to_normalized_laplacian()¶
To Normalized Laplacian.
Returns:
- None: Return value
Example:
to_table_for_streaming()¶
To Table For Streaming.
Returns:
- BaseTable: Return value
Example:
trace()¶
Trace.
Returns:
- None: Return value
Example:
var_axis(axis)¶
Var Axis.
Parameters:
- axis: axis
Returns:
- None: Return value
Example:
zero_grad()¶
Zero Grad.
Returns:
- None: Return value
Example:
zeros(rows, cols)¶
Zeros.
Parameters:
- rows: rows
- cols: cols
Returns:
- None: Return value
Example: