集合#

矩阵#

class graphblas.Matrix(dtype=FP64, nrows=0, ncols=0, *, name=None)#

创建一个新的GraphBLAS稀疏矩阵。

Parameters
dtype

矩阵中元素的数据类型。

nrowsint

行数。

ncolsint

列数。

namestr, optional

给矩阵命名。这将在__repr__中显示。

__contains__(index)#

指示 (row, col) 索引是否有值存在。

Examples

(10, 15) in M
__delitem__(keys, **opts)#

删除单个元素、行/列或子矩阵。

Examples

>>> del M[1, 5]
__getitem__(keys)#

提取单个元素、行/列或子矩阵。

请参阅用户指南中的提取部分以获取更多详细信息。

Examples

subM = M[[1, 3, 5], :].new()
__iter__()#

遍历矩阵中存在的(行,列)索引。

__setitem__(keys, expr, **opts)#

为单个元素、行/列或子矩阵赋值。

请参阅用户指南中的分配部分以获取更多详细信息。

Examples

M[0, 0:3] = 17
apply(op, right=None, *, left=None)#

通过将 op 应用于矩阵的每个元素来创建一个新矩阵。

请参阅用户指南中的Apply部分以获取更多详细信息。

常见用法是传递一个UnaryOp, 在这种情况下,rightleft 可能不会被定义。

也可以使用BinaryOp,在这种情况下,必须将标量作为leftright传递。

一个 IndexUnaryOp 也可以使用 将 thunk 作为 right 传入。

Parameters
opUnaryOp or BinaryOp or IndexUnaryOp

要应用的运算符

right

与BinaryOp或IndexUnaryOp一起使用的标量

left

与BinaryOp一起使用的标量

Returns
MatrixExpression

Examples

# Method syntax
C << A.apply(op.abs)

# Functional syntax
C << op.abs(A)
build(rows, columns, values, *, dup_op=None, clear=False, nrows=None, ncols=None)#

很少使用的方法,用于将值插入到现有的矩阵中。

典型的用例是创建一个新的矩阵,并使用 from_coo() 同时插入值。

所有参数在from_coo()中的使用方式相同,除了clear,它表示在添加新值之前是否清除矩阵。

clear()#

原地操作,清除矩阵中的所有值。

调用后,nvals 将返回 0。shape 不会改变。

diag(k=0, dtype=None, *, name=None, **opts)#

返回一个由矩阵的对角线值构建的向量。

Parameters
kint

非对角线偏移。

dtype

新向量的数据类型。适用正常的类型转换规则。

namestr, optional

为新向量指定的名称。

Returns
Vector
dup(dtype=None, *, clear=False, mask=None, name=None, **opts)#

创建矩阵的副本。

这是一个完整的副本,而不是对原件的视图。

Parameters
dtype

新矩阵的数据类型。适用正常的类型转换规则。

clearbool, default=False

如果为True,返回的矩阵将为空。

maskMask, optional

控制原始元素中哪些元素包含在副本中的掩码。

namestr, optional

为矩阵指定的名称。

Returns
Matrix
ewise_add(other, op=monoid.plus)#

对稀疏值的并集执行元素级计算,类似于人们期望稀疏矩阵的加法如何工作。

请参阅用户指南中的元素级联合部分以获取更多详细信息,特别是关于ewise_add和ewise_union()之间的区别。

Parameters
otherMatrix

计算中的另一个矩阵

opMonoid or BinaryOp

用于相交值的操作符

Returns
MatrixExpression with a structure formed as the union of the input structures

Examples

# Method syntax
C << A.ewise_add(B, op=monoid.max)

# Functional syntax
C << monoid.max(A | B)
ewise_mult(other, op=binary.times)#

对稀疏值的交集执行元素级计算,类似于人们期望稀疏矩阵的乘法如何工作。

请参阅用户指南中的 Element-wise Intersection 部分以获取更多详细信息。

Parameters
otherMatrix

计算中的另一个矩阵

opMonoid or BinaryOp

用于相交值的操作符

Returns
MatrixExpression with a structure formed as the intersection of the input structures

Examples

# Method syntax
C << A.ewise_mult(B, op=binary.gt)

# Functional syntax
C << binary.gt(A & B)
ewise_union(other, op, left_default, right_default)#

对稀疏值的并集执行元素级计算,类似于人们期望稀疏矩阵的减法如何工作。

请参阅用户指南中的元素级联合部分以获取更多详细信息,特别是关于ewise_union和ewise_add()之间的区别。

Parameters
otherMatrix

计算中的另一个矩阵

opMonoid or BinaryOp

要使用的操作符

left_default

当左侧索引缺失时使用的标量值

right_default

当右侧索引缺失时使用的标量值

Returns
MatrixExpression with a structure formed as the union of the input structures

Examples

# Method syntax
C << A.ewise_union(B, op=binary.div, left_default=1, right_default=1)

# Functional syntax
C << binary.div(A | B, left_default=1, right_default=1)
classmethod from_coo(rows, columns, values=1.0, dtype=None, *, nrows=None, ncols=None, dup_op=None, name=None)#

从行和列索引以及值创建一个新的矩阵。

Parameters
rowslist or np.ndarray

行索引。

columnslist or np.ndarray

列索引。

valueslist or np.ndarray or scalar, default 1.0

值列表。如果提供了标量,则所有值都将设置为该单一值。

dtype

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

nrowsint, optional

矩阵中的行数。如果未提供,nrows 将从 rows 中找到的最大行索引计算得出。

ncolsint, optional

矩阵中的列数。如果未提供,ncols 将从 columns 中找到的最大列索引计算得出。

dup_opBinaryOp, optional

用于在发现重复索引时合并值的函数。 如果发现重复项,保留 dup_op=None 将会引发错误。

namestr, optional

为矩阵指定的名称。

Returns
Matrix
classmethod from_csc(indptr, row_indices, values=1.0, dtype=None, *, nrows=None, ncols=None, name=None)#

从数据的标准CSC表示创建一个新的矩阵。

在CSC中,列i的行索引存储在row_indices[indptr[i]:indptr[i+1]]中,值存储在values[indptr[i]:indptr[i+1]]中。列数推断为ncols = len(indptr) - 1

这总是复制数据。对于具有移动语义的零拷贝导入,请参见 Matrix.ss.import_csc

Parameters
indptrlist or np.ndarray

每列的指针指向行索引和值;indptr.size == ncols + 1

col_indiceslist or np.ndarray

列索引。

valueslist or np.ndarray or scalar, default 1.0

值列表。如果提供了标量,则所有值都将设置为该单一值。

dtype

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

nrowsint, optional

矩阵中的行数。如果未提供,ncols 将从 row_indices 中找到的最大行索引计算得出。

ncolsint, optional

矩阵中的列数。ncols 计算为 len(indptr) - 1。 如果提供,它必须等于 len(indptr) - 1

namestr, optional

为矩阵指定的名称。

Returns
Matrix

另请参阅

from_coo
from_csr
from_dcsc
to_csc
Matrix.ss.import_csc
io.from_scipy_sparse
classmethod from_csr(indptr, col_indices, values=1.0, dtype=None, *, nrows=None, ncols=None, name=None)#

从数据的标准CSR表示创建一个新的矩阵。

在CSR中,行i的列索引存储在col_indices[indptr[i]:indptr[i+1]]中,值存储在values[indptr[i]:indptr[i+1]]中。行数推断为nrows = len(indptr) - 1

这总是复制数据。对于具有移动语义的零拷贝导入,请参见 Matrix.ss.import_csr

Parameters
indptrlist or np.ndarray

每行指向col_indices和values的指针;indptr.size == nrows + 1

col_indiceslist or np.ndarray

列索引。

valueslist or np.ndarray or scalar, default 1.0

值列表。如果提供了标量,则所有值都将设置为该单一值。

dtype

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

nrowsint, optional

矩阵中的行数。nrows 计算为 len(indptr) - 1。 如果提供,它必须等于 len(indptr) - 1

ncolsint, optional

矩阵中的列数。如果未提供,ncols 将从 col_indices 中找到的最大列索引计算得出。

namestr, optional

为矩阵指定的名称。

Returns
Matrix

另请参阅

from_coo
from_csc
from_dcsr
to_csr
Matrix.ss.import_csr
io.from_scipy_sparse
classmethod from_dcsc(compressed_cols, indptr, row_indices, values=1.0, dtype=None, *, nrows=None, ncols=None, name=None)#

从数据的DCSC(也称为HyperCSC)表示创建一个新的矩阵。

在DCSC中,我们将每个非空列的索引存储在compressed_cols中。 列compressed_cols[i]的行索引存储在 col_indices[indptr[compressed_cols[i]]:indptr[compressed_cols[i]+1]]中,而值 存储在values[indptr[compressed_cols[i]]:indptr[compressed_cols[i]+1]]中。

这总是复制数据。对于具有移动语义的零拷贝导入,请参见 Matrix.ss.import_hypercsc。

Parameters
compressed_colslist or np.ndarray

非空列的索引;唯一且已排序。

indptrlist or np.ndarray

指向每个非空列在row_indices和values中的指针。

row_indiceslist or np.ndarray

行索引。

valueslist or np.ndarray or scalar, default 1.0

值列表。如果提供了标量,则所有值都将设置为该单一值。

dtype

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

nrowsint, optional

矩阵中的行数。如果未提供,nrows 将从 row_indices 中找到的最大行索引计算得出。

ncolsint, optional

矩阵中的列数。如果未提供,ncols 将从 compressed_cols 中找到的最大列索引计算得出。

namestr, optional

为矩阵指定的名称。

Returns
Matrix

另请参阅

from_coo
from_csc
from_dcsr
to_dcsc
Matrix.ss.import_hypercsc
io.from_scipy_sparse
classmethod from_dcsr(compressed_rows, indptr, col_indices, values=1.0, dtype=None, *, nrows=None, ncols=None, name=None)#

从DCSR(也称为HyperCSR)数据表示创建一个新的矩阵。

在DCSR中,我们将每个非空行的索引存储在compressed_rows中。 行compressed_rows[i]的列索引存储在 col_indices[indptr[compressed_row[i]]:indptr[compressed_row[i]+1]]中,而值 存储在values[indptr[compressed_row[i]]:indptr[compressed_row[i]+1]]中。

这总是复制数据。对于具有移动语义的零拷贝导入,请参见 Matrix.ss.import_hypercsr。

Parameters
compressed_rowslist or np.ndarray

非空行的索引;唯一且已排序。

indptrlist or np.ndarray

指向每个非空行的指针,指向col_indices和values。

col_indiceslist or np.ndarray

列索引。

valueslist or np.ndarray or scalar, default 1.0

值列表。如果提供了标量,则所有值都将设置为该单一值。

dtype

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

nrowsint, optional

矩阵中的行数。如果未提供,nrows 将从 compressed_rows 中找到的最大行索引计算得出。

ncolsint, optional

矩阵中的列数。如果未提供,ncols 将从 col_indices 中找到的最大列索引计算得出。

namestr, optional

为矩阵指定的名称。

Returns
Matrix

另请参阅

from_coo
from_csr
from_dcsc
to_dcsr
Matrix.ss.import_hypercsr
io.from_scipy_sparse
classmethod from_dense(values, missing_value=None, *, dtype=None, name=None, **opts)#

从NumPy数组或列表的列表中创建一个矩阵。

Parameters
valueslist or np.ndarray

值列表。

missing_valuescalar, optional

一个被视为“缺失”的标量值;此值的元素将被丢弃。 如果为 None,则生成的矩阵将是密集的。

dtypeDataType, optional

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

namestr, optional

为矩阵指定的名称。

Returns
Matrix
classmethod from_dicts(nested_dicts, dtype=None, *, order='rowwise', nrows=None, ncols=None, name=None)#

从字典的字典或字典的列表创建一个新的矩阵。

一个字典的字典形式为 {row: {col: val}} 如果顺序是“按行”并且 形式为 {col: {row: val}} 如果顺序是“按列”。

字典列表的形式为 [{col: val}, {col: val}, ...] 用于“按行”顺序 其中列表的第 i 个元素是第 i 行的列和值的字典。

Parameters
dMapping or Sequence

要转换的类似字典的对象。键将被转换为uint64作为索引。

dtype

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

order{“rowwise”, “columnwise”}, optional

“rowwise” 将输入的字典字典解释为 {row: {col: val}} 并将输入的字典列表解释为 [{col: val}, {col: val}]。 “columnwise” 将输入的字典字典解释为 {col: {row: val}} 并将输入的字典列表解释为 [{row: val}, {row: val}]。 默认值为“rowwise”。

nrowsint, optional

矩阵中的行数。如果未提供,nrows 将从字典中的最大行索引计算得出,对于字典列表,则作为列表的长度计算。

ncolsint, optional

矩阵中的列数。如果未提供,ncols 将从字典中找到的最大列索引计算得出。

namestr, optional

为矩阵指定的名称。

Returns
Matrix

另请参阅

to_dicts
classmethod from_edgelist(edgelist, values=None, dtype=None, *, nrows=None, ncols=None, dup_op=None, name=None)#

从(row, col)对或(row, col, value)三元组的边列表创建一个新的矩阵。

这将转换数据并调用Matrix.from_coo

Parameters
edgelistlist or np.ndarray or iterable

一系列(row, column)对或(row, column, value)三元组。 NumPy 边列表仅支持(row, column);值必须单独传递。

valueslist or np.ndarray or scalar, optional

值列表。如果提供了标量,所有值都将设置为这个单一值。 如果edgelist(row, column)对的序列,则默认值为1.0。

dtype

矩阵的数据类型。如果未提供,将检查值以选择适当的dtype。

nrowsint, optional

矩阵中的行数。如果未提供,nrows 将从边列表中找到的最大行索引计算得出。

ncolsint, optional

矩阵中的列数。如果未提供,ncols 将从边列表中找到的最大列索引计算得出。

dup_opBinaryOp, optional

用于在发现重复索引时合并值的函数。 如果发现重复项,保留 dup_op=None 将会引发错误。

namestr, optional

为矩阵指定的名称。

Returns
Matrix
classmethod from_scalar(value, nrows, ncols, dtype=None, *, name=None, **opts)#

创建一个完全密集的矩阵,并用标量值填充。

对于SuiteSparse:GraphBLAS后端,这将创建一个等值的完整矩阵,无论矩阵的形状如何,它都存储一个单一的值,因此通过Matrix.from_scalar创建的大型矩阵将使用非常低的内存。

如果你想创建一个与现有矩阵结构相同的新等值矩阵,你可以这样做:C = binary.second(A, value).new()

Parameters
valuescalar

用于填充矩阵的标量值。

nrowsint

行数。

ncolsint

列数。

dtypeDataType, optional

矩阵的数据类型。如果未提供,将检查标量值以选择适当的dtype。

namestr, optional

为矩阵指定的名称。

Returns
Matrix
get(row, col, default=None)#

获取位于 (row, col) 索引处的元素作为 Python 标量。

Parameters
rowint

行索引

colint

列索引

default

如果 (row, col) 处没有元素,则返回的值

Returns
Python scalar
isclose(other, *, rel_tol=1e-07, abs_tol=0.0, check_dtype=False, **opts)#

检查近似相等性(包括相同的大小和相同的结构)。

等价于:abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

Parameters
otherMatrix

要比较的矩阵。

rel_tolfloat

相对容差。

abs_tolfloat

绝对容差。

check_dtypebool

如果为True,还会检查数据类型是否匹配

Returns
bool

矩阵的所有值是否接近other中的值。

isequal(other, *, check_dtype=False, **opts)#

检查是否完全相等(相同大小,相同结构)。

Parameters
otherMatrix

要比较的矩阵

check_dtypesbool

如果为True,还会检查数据类型是否匹配

Returns
bool

另请参阅

isclose()

用于浮点数据类型的相等性检查

kronecker(other, op=binary.times)#

计算克罗内克积或和(取决于操作)。

请参阅用户指南中的Kronecker部分以获取更多详细信息。

Parameters
otherMatrix

计算中右侧的矩阵

opBinaryOp

用于元素组合的操作符

Returns
MatrixExpression

Examples

C << A.kronecker(B, op=binary.times)
mxm(other, op=semiring.plus_times)#

执行矩阵-矩阵乘法。

请参阅用户指南中的矩阵乘法部分以获取更多详细信息。

Parameters
otherMatrix

计算中右侧的矩阵

opSemiring

用于计算的半环

Returns
MatrixExpression

Examples

# Method syntax
C << A.mxm(B, op=semiring.min_plus)

# Functional syntax
C << semiring.min_plus(A @ B)
mxv(other, op=semiring.plus_times)#

执行矩阵-向量乘法。

请参阅用户指南中的矩阵乘法部分以获取更多详细信息。

Parameters
otherVector

向量,被视为一个(nx1)列矩阵

opSemiring

用于计算的半环

Returns
VectorExpression

Examples

# Method syntax
C << A.mxv(v, op=semiring.min_plus)

# Functional syntax
C << semiring.min_plus(A @ v)
power(n, op=semiring.plus_times)#

将一个方阵提升到(正整数)幂 n

矩阵幂是通过重复的矩阵平方和矩阵乘法来计算的。 对于作为邻接矩阵的图,使用默认的plus_times 半环的矩阵幂计算连接每对节点的路径数。 对于大矩阵和较大的n,结果可能会迅速增长。

Parameters
nint

指数必须是非负整数。如果n=0,结果将是一个对角矩阵,其值等于半环二元运算符的单位元。例如,plus_times将对角值设为1,这是times的单位元。当n=0时,二元运算符必须与一个幺半群相关联,以便可以确定单位元;否则,将引发ValueError。

opSemiring

用于计算的半环

Returns
MatrixExpression

Examples

C << A.power(4, op=semiring.plus_times)

# Is equivalent to:
tmp = (A @ A).new()
tmp << tmp @ tmp
C << tmp @ tmp

# And is more efficient than the naive implementation:
C = A.dup()
for i in range(1, 4):
    C << A @ C
reduce_columnwise(op=monoid.plus)#

通过使用op减少矩阵中每列的值来创建一个新的向量。

请参阅用户指南中的Reduce部分以获取更多详细信息。

Parameters
opMonoid

归约操作符

Returns
VectorExpression

Examples

w << A.reduce_columnwise(monoid.plus)
reduce_rowwise(op=monoid.plus)#

通过使用op减少矩阵中每行的值来创建一个新的向量。

请参阅用户指南中的Reduce部分以获取更多详细信息。

Parameters
opMonoid

归约操作符

Returns
VectorExpression

Examples

w << A.reduce_rowwise(monoid.plus)
reduce_scalar(op=monoid.plus, *, allow_empty=True)#

使用 op 将矩阵中的所有值减少为单个值。

请参阅用户指南中的Reduce部分以获取更多详细信息。

Parameters
opMonoid

归约操作符

allow_emptybool, default=True

如果为False且矩阵为空,标量结果将保持幺半群恒等元而不是缺失值

Returns
ScalarExpression

Examples

total << A.reduce_scalar(monoid.plus)
reposition(row_offset, column_offset, *, nrows=None, ncols=None)#

创建一个新矩阵,其值与原始矩阵相同,但通过向索引添加偏移量在(nrows x ncols)空间内重新定位。

正偏移将值向右(或向下)移动,负偏移将值向左(或向上)移动。 重新定位到新矩阵之外的值将被丢弃(即它们不会环绕)。

注意: 这不是一个标准的GraphBLAS方法。 它是使用extract和assign实现的。

Parameters
row_offsetint

行索引的偏移量。

column_offsetint

列索引的偏移量。

nrowsint, optional

如果指定,新矩阵将使用nrows进行大小调整。 默认情况下,新矩阵的行数与原始矩阵相同。

ncolsint, optional

如果指定,新矩阵将使用ncols进行大小调整。 默认情况下,新矩阵的列数与原始矩阵相同。

Returns
MatrixExpression

Examples

C = A.reposition(1, 2).new()
resize(nrows, ncols)#

原地操作,改变shape

Increasing nrows or ncols will expand with empty values.
Decreasing nrows or ncols will drop existing values above the new indices.
select(op, thunk=None)#

通过将op应用于矩阵的每个元素并保留那些op返回True的元素来创建一个新矩阵。

请参阅用户指南中的Select部分以获取更多详细信息。

Parameters
opSelectOp

要应用的运算符

thunk

传递给运算符的标量

Returns
MatrixExpression

Examples

# Method syntax
C << A.select(">=", 1)

# Functional syntax
C << select.value(A >= 1)
setdiag(values, k=0, *, mask=None, accum=None, **opts)#

使用标量、向量或数组设置第k条对角线。

这不是一个内置的GraphBLAS操作。它是作为一个配方实现的。

Parameters
valuesVector or list or np.ndarray or scalar

要分配给对角线的新值。向量和数组值的长度必须与要分配的对角线的大小匹配。

kint, default=0

设置哪条对角线或非对角线。例如,设置元素 A[i, i+k] = values[i]。默认值 k=0 是主对角线。

maskMask, optional

向量或矩阵掩码,用于控制要设置哪些对角线元素。 如果是矩阵掩码,则仅使用对角线作为掩码。

accumMonoid or BinaryOp, optional

用于组合现有对角线值和新值的运算符。

to_coo(dtype=None, *, rows=True, columns=True, values=True, sort=True)#

提取索引和值作为与矩阵的COO格式对应的numpy数组的3元组。

Parameters
dtype

请求的输出值数组的数据类型。

rowsbool, default=True

是否返回行;如果False,将返回None表示没有行

columns :bool, default=True

是否返回列;如果为False,则列将返回None

valuesbool, default=True

是否返回值;如果为False,则返回None作为值

sortbool, default=True

是否需要排序索引。 如果内部按行存储,排序将首先按行,然后按列。 如果内部按列存储,排序将首先按列,然后按行。

Returns
np.ndarray[dtype=uint64]Rows
np.ndarray[dtype=uint64]Columns
np.ndarrayValues
to_csc(dtype=None, *, sort=True)#

返回标准CSC表示的三个数组:indptr、row_indices、values。

在CSC中,列i的行索引存储在row_indices[indptr[i]:indptr[i+1]]中,值存储在values[indptr[i]:indptr[i+1]]中。

这会复制数据并保持矩阵不变。对于零拷贝移动语义,请参见 Matrix.ss.export。

Returns
np.ndarray[dtype=uint64]indptr
np.ndarray[dtype=uint64]row_indices
np.ndarrayvalues

另请参阅

to_coo
to_csr
to_dcsc
from_csc
Matrix.ss.export
io.to_scipy_sparse
to_csr(dtype=None, *, sort=True)#

返回标准CSR表示的三个数组:indptr、col_indices、values。

在CSR中,行i的列索引存储在col_indices[indptr[i]:indptr[i+1]]中,值存储在values[indptr[i]:indptr[i+1]]中。

这会复制数据并保持矩阵不变。对于零拷贝移动语义,请参见 Matrix.ss.export。

Returns
np.ndarray[dtype=uint64]indptr
np.ndarray[dtype=uint64]col_indices
np.ndarrayvalues

另请参阅

to_coo
to_csc
to_dcsr
from_csr
Matrix.ss.export
io.to_scipy_sparse
to_dcsc(dtype=None, *, sort=True)#

返回四个DCSC表示的数组:compressed_cols、indptr、row_indices、values。

在DCSC中,我们将每个非空列的索引存储在compressed_cols中。 列compressed_cols[i]的行索引存储在 col_indices[indptr[compressed_cols[i]]:indptr[compressed_cols[i]+1]]中,而值 存储在values[indptr[compressed_cols[i]]:indptr[compressed_cols[i]+1]]中。

这会复制数据并保持矩阵不变。对于零拷贝移动语义,请参见 Matrix.ss.export。

Returns
np.ndarray[dtype=uint64]compressed_cols
np.ndarray[dtype=uint64]indptr
np.ndarray[dtype=uint64]row_indices
np.ndarrayvalues

另请参阅

to_coo
to_csc
to_dcsr
from_dcsc
Matrix.ss.export
io.to_scipy_sparse
to_dcsr(dtype=None, *, sort=True)#

返回四个DCSR表示的数组:compressed_rows、indptr、col_indices、values。

在DCSR中,我们将每个非空行的索引存储在compressed_rows中。 行compressed_rows[i]的列索引存储在 col_indices[indptr[compressed_row[i]]:indptr[compressed_row[i]+1]]中,而值 存储在values[indptr[compressed_row[i]]:indptr[compressed_row[i]+1]]中。

这会复制数据并保持矩阵不变。对于零拷贝移动语义,请参见 Matrix.ss.export。

Returns
np.ndarray[dtype=uint64]compressed_rows
np.ndarray[dtype=uint64]indptr
np.ndarray[dtype=uint64]col_indices
np.ndarrayvalues

另请参阅

to_coo
to_csr
to_dcsc
from_dcsc
Matrix.ss.export
io.to_scipy_sparse
to_dense(fill_value=None, dtype=None, **opts)#

将矩阵转换为具有相同形状的NumPy数组,并用缺失值填充。

警告

这可能会创建非常大的数组,需要大量内存;请谨慎使用。

Parameters
fill_valuescalar, optional

用于填充缺失值的值。如果存在缺失值,则这是必需的。

dtypeDataType, optional

请求的输出值数组的数据类型。

Returns
np.ndarray
to_dicts(order='rowwise')#

将矩阵返回为字典的字典形式 {row: {col: val}}

Parameters
order{“rowwise”, “columnwise”}, optional

“rowwise” 返回字典的字典,格式为 {row: {col: val}}。 “columnwise” 返回字典的字典,格式为 {col: {row: val}}。 默认值为“rowwise”。

Returns
dict

另请参阅

from_dicts
to_edgelist(dtype=None, *, values=True, sort=True)#

提取索引和值作为numpy数组的2元组。

这调用了 to_coo 然后将数据转换为边列表。

Parameters
dtype

请求的输出值数组的数据类型。

valuesbool, default=True

是否返回值;如果为False,则返回None作为值

sortbool, default=True

是否需要排序索引。 如果内部按行存储,排序将首先按行,然后按列。 如果内部按列存储,排序将首先按列,然后按行。

Returns
np.ndarray[dtype=uint64]Edgelist
np.ndarrayValues
wait(how='materialize')#

等待计算完成或建立“发生之前”关系。

Parameters
how{“materialize”, “complete”}

“materialize” 完全计算一个对象。 “complete” 建立一个“发生在之前”的关系,对多线程有用。 有关更多详细信息,请参阅 GraphBLAS 文档。

In `non-blocking mode <../user_guide/init.html#graphblas-modes>`__,
the computations may be delayed and not yet safe to use by multiple threads.
Use wait to force completion of the Matrix.
Has no effect in `blocking mode <../user_guide/init.html#graphblas-modes>`__.
property S#

根据矩阵的结构创建一个掩码。

property T#

表示矩阵的转置。

可以在大多数操作的参数中使用。它也可以单独使用,作为转置操作

property V#

根据矩阵中的值创建一个掩码(将每个值视为真值)。

property ncols#

矩阵中的列数。

property nrows#

矩阵中的行数。

property nvals#

矩阵中非空值的数量。

property shape#

一个由(nrows, ncols)组成的元组。

向量#

class graphblas.Vector(dtype=FP64, size=0, *, name=None)#

创建一个新的GraphBLAS稀疏向量。

Parameters
dtype

向量中元素的数据类型。

sizeint

向量的大小。

namestr, optional

为向量指定的名称。这将在__repr__中显示。

__contains__(index)#

指示索引处是否存在值。

Examples

# Check if v[15] is non-empty
15 in v
__delitem__(keys, **opts)#

删除单个元素或子向量。

Examples

>>> del v[1:-1]
__getitem__(keys)#

提取单个元素或子向量。

请参阅用户指南中的提取部分以获取更多详细信息。

Examples

sub_v = v[[1, 3, 5]].new()
__iter__()#

遍历向量中存在的索引。

__setitem__(keys, expr, **opts)#

为单个元素或子向量赋值。

请参阅用户指南中的分配部分以获取更多详细信息。

Examples

# This makes a dense iso-value vector
v[:] = 1
apply(op, right=None, *, left=None)#

通过将 op 应用于向量的每个元素来创建一个新的向量。

请参阅用户指南中的Apply部分以获取更多详细信息。

常见用法是传递一个UnaryOp, 在这种情况下,rightleft 可能不会被定义。

也可以使用BinaryOp,在这种情况下,必须将标量作为leftright传递。

一个 IndexUnaryOp 也可以使用 将 thunk 作为 right 传入。

Parameters
opUnaryOp or BinaryOp or IndexUnaryOp

要应用的运算符

right

与BinaryOp或IndexUnaryOp一起使用的标量

left

与BinaryOp一起使用的标量

Returns
VectorExpression

Examples

# Method syntax
w << v.apply(op.abs)

# Functional syntax
w << op.abs(v)
build(indices, values, *, dup_op=None, clear=False, size=None)#

很少使用的方法,用于将值插入到现有的Vector中。典型的用例是创建一个新的Vector,并使用from_coo()同时插入值。

所有参数在from_coo()中的使用方式相同,除了clear,它表示在添加新值之前是否清除Vector。

clear()#

原地操作,清除向量中的所有值。

调用后,nvals 将返回 0。size 不会改变。

diag(k=0, *, name=None)#

返回一个矩阵,其对角线上的值由向量构建。

Parameters
kint

矩阵中的非对角线偏移。

dtype

新矩阵的数据类型。适用正常的类型转换规则。

namestr, optional

为新矩阵指定的名称。

Returns
Matrix
dup(dtype=None, *, clear=False, mask=None, name=None, **opts)#

创建向量的副本。

这是一个完整的副本,而不是对原件的视图。

Parameters
dtype

新向量的数据类型。适用正常的类型转换规则。

clearbool, default=False

如果为True,返回的Vector将为空。

maskMask, optional

控制原始元素中哪些元素包含在副本中的掩码。

namestr, optional

为向量指定的名称。

Returns
Vector
ewise_add(other, op=monoid.plus)#

对稀疏值的并集执行元素级计算,类似于人们期望稀疏向量的加法工作方式。

请参阅用户指南中的元素级联合部分以获取更多详细信息,特别是关于ewise_add和ewise_union()之间的区别。

Parameters
otherVector

计算中的另一个向量

opMonoid or BinaryOp

用于相交值的操作符

Returns
VectorExpression with a structure formed as the union of the input structures

Examples

# Method syntax
w << u.ewise_add(v, op=monoid.max)

# Functional syntax
w << monoid.max(u | v)
ewise_mult(other, op=binary.times)#

对稀疏值的交集执行元素级计算,类似于人们期望稀疏向量的乘法如何工作。

请参阅用户指南中的 Element-wise Intersection 部分以获取更多详细信息。

Parameters
otherVector

计算中的另一个向量

opMonoid or BinaryOp

用于相交值的操作符

Returns
VectorExpression with a structure formed as the intersection of the input structures

Examples

# Method syntax
w << u.ewise_mult(v, op=binary.gt)

# Functional syntax
w << binary.gt(u & v)
ewise_union(other, op, left_default, right_default)#

对稀疏值的并集执行逐元素计算,类似于人们期望稀疏向量的减法如何工作。

请参阅用户指南中的元素级联合部分以获取更多详细信息,特别是关于ewise_union和ewise_add()之间的区别。

Parameters
otherVector

计算中的另一个向量

opMonoid or BinaryOp

要使用的操作符

left_default

当左侧索引缺失时使用的标量值

right_default

当右侧索引缺失时使用的标量值

Returns
VectorExpression with a structure formed as the union of the input structures

Examples

# Method syntax
w << u.ewise_union(v, op=binary.div, left_default=1, right_default=1)

# Functional syntax
w << binary.div(u | v, left_default=1, right_default=1)
classmethod from_coo(indices, values=1.0, dtype=None, *, size=None, dup_op=None, name=None)#

从索引和值创建一个新的向量。

Parameters
indiceslist or np.ndarray

向量索引。

valueslist or np.ndarray or scalar, default 1.0

值列表。如果提供了标量,则所有值都将设置为该单一值。

dtype

向量的数据类型。如果未提供,将检查值以选择适当的dtype。

sizeint, optional

向量的大小。如果未提供,size 将从 indices 中找到的最大索引计算得出。

dup_opBinaryOp, optional

用于在发现重复索引时合并值的函数。 如果发现重复项,保留 dup_op=None 将会引发错误。

namestr, optional

为向量指定的名称。

Returns
Vector
classmethod from_dense(values, missing_value=None, *, dtype=None, name=None, **opts)#

从NumPy数组或列表创建一个向量。

Parameters
valueslist or np.ndarray

值列表。

missing_valuescalar, optional

一个被视为“缺失”的标量值;此值的元素将被丢弃。 如果为None,则生成的Vector将是密集的。

dtypeDataType, optional

向量的数据类型。如果未提供,将检查值以选择适当的dtype。

namestr, optional

为向量指定的名称。

Returns
Vector
classmethod from_dict(d, dtype=None, *, size=None, name=None)#

从字典创建一个新的向量,其中键作为索引,值作为值。

Parameters
dMapping

要转换的类似字典的对象。键将被转换为uint64作为索引。

dtype

向量的数据类型。如果未提供,将检查值以选择适当的dtype。

sizeint, optional

向量的大小。如果未提供,size 将从 indices 中找到的最大索引计算得出。

namestr, optional

为向量指定的名称。

Returns
Vector
classmethod from_pairs(pairs, dtype=None, *, size=None, dup_op=None, name=None)#

从索引和值创建一个新的向量。

这将转换数据并调用 Vector.from_coo

Parameters
pairslist or iterable

一系列(index, value)对。

dtype

向量的数据类型。如果未提供,将检查值以选择适当的dtype。

sizeint, optional

向量的大小。如果未提供,size 将从 pairs 中找到的最大索引计算得出。

dup_opBinaryOp, optional

用于在发现重复索引时合并值的函数。 如果发现重复项,保留 dup_op=None 将会引发错误。

namestr, optional

为向量指定的名称。

Returns
Vector
classmethod from_scalar(value, size, dtype=None, *, name=None, **opts)#

创建一个完全密集的向量,并用标量值填充。

对于SuiteSparse:GraphBLAS后端,这将创建一个等值的完整向量,无论向量的大小如何,它都存储一个单一的值,因此通过Vector.from_scalar创建的大向量将使用非常低的内存。

如果你想创建一个与现有Vector结构相同的新等值Vector,你可以这样做:w = binary.second(v, value).new()

Parameters
valuescalar

用于填充向量的标量值。

nrowsint

行数。

ncolsint

列数。

dtypeDataType, optional

向量的数据类型。如果未提供,将检查标量值以选择适当的dtype。

namestr, optional

为向量指定的名称。

Returns
Vector
get(index, default=None)#

获取位于 index 的元素作为 Python 标量。

Parameters
indexint

向量索引

default

如果索引处没有元素,则返回的值

Returns
Python scalar
inner(other, op=semiring.plus_times)#

执行向量-向量的内积(或点积)。

Parameters
otherVector

计算中右侧的向量

opSemiring

用于计算的半环

Returns
ScalarExpression

Examples

# Method syntax
s << v.inner(w, op=semiring.min_plus)

# Functional syntax
s << semiring.min_plus(v @ w)

注意: 这不是一个标准的GraphBLAS函数,但它与 矩阵乘法 系列中的其他函数相匹配。

isclose(other, *, rel_tol=1e-07, abs_tol=0.0, check_dtype=False, **opts)#

检查近似相等性(包括相同的大小和相同的结构)。

等价于:abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

Parameters
otherVector

要比较的向量

rel_tolfloat

相对容差

abs_tolfloat

绝对容差

check_dtypebool

如果为True,还会检查数据类型是否匹配

Returns
bool
isequal(other, *, check_dtype=False, **opts)#

检查是否完全相等(相同大小,相同结构)。

Parameters
otherVector

要比较的向量

check_dtypesbool, default=False

如果为True,还会检查数据类型是否匹配

Returns
bool

另请参阅

isclose()

用于浮点数据类型的相等性检查

outer(other, op=binary.times)#

执行向量-向量外积(或叉积)。

Parameters
otherVector

计算中右侧的向量

opBinaryOp

计算中使用的运算符

Returns
MatrixExpression

Examples

C << v.outer(w, op=binary.times)

注意: 这不是一个标准的 GraphBLAS 函数。

reduce(op=monoid.plus, *, allow_empty=True)#

使用 op 将向量中的所有值减少为单个值。

请参阅用户指南中的Reduce部分以获取更多详细信息。

Parameters
opMonoid

归约操作符

allow_emptybool, default=True

如果为False且Vector为空,Scalar结果将保持幺元恒等值而不是缺失值

Returns
ScalarExpression

Examples

total << v.reduce(monoid.plus)
reposition(offset, *, size=None)#

创建一个新的Vector,其值与原始Vector相同,但通过向索引添加offset在总大小内重新定位。

正偏移将值向右移动,负偏移向左移动。 重新定位到新向量之外的值将被丢弃(即它们不会环绕)。

注意: 这不是一个标准的GraphBLAS方法。 它是使用extract和assign实现的。

Parameters
offsetint

索引的偏移量。

sizeint, optional

如果指定,新的Vector将被调整大小。 默认大小与原始Vector相同。

Returns
VectorExpression

Examples

w = v.reposition(20).new()
resize(size)#

原地操作,改变size

Increasing size will expand with empty values.
Decreasing size will drop existing values above the new maximum index.
select(op, thunk=None)#

通过将op应用于Vector的每个元素并保留那些op返回True的元素来创建一个新的Vector。

请参阅用户指南中的Select部分以获取更多详细信息。

Parameters
opSelectOp

要应用的运算符

thunk

传递给运算符的标量

Returns
VectorExpression

Examples

# Method syntax
w << v.select(">=", 1)

# Functional syntax
w << select.value(v >= 1)
to_coo(dtype=None, *, indices=True, values=True, sort=True)#

提取索引和值作为numpy数组的2元组。

Parameters
dtype

请求的输出值数组的数据类型。

indices :bool, default=True

是否返回索引;如果为False,则返回None作为索引

valuesbool, default=True

是否返回值;如果为False,则返回None作为值

sortbool, default=True

是否需要排序的索引。

Returns
np.ndarray[dtype=uint64]Indices
np.ndarrayValues

另请参阅

to_dense
to_dict
from_coo
to_dense(fill_value=None, dtype=None, **opts)#

将向量转换为具有相同形状的NumPy数组,并用缺失值填充。

警告

这可能会创建非常大的数组,需要大量内存;请谨慎使用。

Parameters
fill_valuescalar, optional

用于填充缺失值的值。如果存在缺失值,则这是必需的。

dtypeDataType, optional

请求的输出值数组的数据类型。

Returns
np.ndarray

另请参阅

to_coo
to_dict
from_dense
to_dict()#

将Vector返回为字典形式 {index: val}

Returns
dict

另请参阅

to_coo
to_dense
from_dict
vxm(other, op=semiring.plus_times)#

执行向量-矩阵乘法,将向量视为计算左侧的(1xn)行向量。

请参阅用户指南中的 矩阵乘法 部分以获取更多详细信息。

Parameters
other: Matrix

计算中右侧的矩阵

opSemiring

用于计算的半环

Returns
VectorExpression

Examples

# Method syntax
C << v.vxm(A, op=semiring.min_plus)

# Functional syntax
C << semiring.min_plus(v @ A)
wait(how='materialize')#

等待计算完成或建立“发生之前”关系。

Parameters
how{“materialize”, “complete”}

“materialize” 完全计算一个对象。 “complete” 建立一个“发生在之前”的关系,对多线程有用。 有关更多详细信息,请参阅 GraphBLAS 文档。

In `non-blocking mode <../user_guide/init.html#graphblas-modes>`__,
the computations may be delayed and not yet safe to use by multiple threads.
Use wait to force completion of the Vector.
Has no effect in `blocking mode <../user_guide/init.html#graphblas-modes>`__.
property S#

根据向量的结构创建一个掩码。

property V#

根据向量中的值创建一个掩码(将每个值视为真值)。

property nvals#

向量中非空值的数量。

property shape#

一个(size,)的元组。

property size#

向量的大小。

标量#

class graphblas.Scalar(dtype=FP64, *, is_cscalar=False, name=None)#

创建一个新的GraphBLAS稀疏标量。

Parameters
dtype

标量的数据类型。

is_cscalarbool, default=False

如果为True,空状态将在Python端管理,而不是使用适当的GrB_Scalar对象。

namestr, optional

为标量指定的名称。这将在__repr__中显示。

__bool__()#

真实性检查。

如果标量非空且内部值为真,则被视为真值。

要仅检查是否存在某个值,请使用 is_empty

__eq__(other)#

检查相等性。

相等比较使用 isequal()。直接使用它以更精细地控制什么被认为是相等的。

apply(op, right=None, *, left=None)#

通过应用 op 创建一个新的标量。

请参阅用户指南中的Apply部分以获取更多详细信息。

常见用法是传递一个UnaryOp, 在这种情况下,rightleft 可能不会被定义。

也可以使用BinaryOp,在这种情况下,必须将标量作为leftright传递。

一个 IndexUnaryOp 也可以使用 将 thunk 作为 right 传入。

Parameters
opUnaryOp or BinaryOp or IndexUnaryOp

要应用的运算符

right

与BinaryOp或IndexUnaryOp一起使用的标量

left

与BinaryOp一起使用的标量

Returns
ScalarExpression

Examples

# Method syntax
b << a.apply(op.abs)

# Functional syntax
b << op.abs(a)
clear()#

原地操作,清除标量中的值。

调用后,nvals 将返回 0。

dup(dtype=None, *, clear=False, is_cscalar=None, name=None)#

创建 Scalar 的副本。

这是一个完整的副本,而不是对原件的视图。

Parameters
dtype

新标量的数据类型。适用常规的类型转换规则。

clearbool, default=False

如果为True,返回的Scalar将为空。

is_cscalarbool

如果为True,空状态将在Python端管理,而不是使用适当的GrB_Scalar对象。

namestr, optional

为标量指定的名称。

Returns
Scalar
ewise_add(other, op=monoid.plus)#

对稀疏值的并集执行元素级计算,类似于人们期望稀疏数据的加法工作方式。

请参阅用户指南中的元素级联合部分以获取更多详细信息,特别是关于ewise_add和ewise_union()之间的区别。

Parameters
otherScalar

计算中的另一个标量;也接受Python标量

opMonoid or BinaryOp

用于相交值的操作符

Returns
ScalarExpression that will be non-empty if any of the inputs is non-empty

Examples

# Method syntax
c << a.ewise_add(b, op=monoid.max)

# Functional syntax
c << monoid.max(a | b)
ewise_mult(other, op=binary.times)#

对稀疏值的交集执行逐元素计算,类似于人们期望稀疏数据的乘法如何工作。

请参阅用户指南中的 Element-wise Intersection 部分以获取更多详细信息。

Parameters
otherScalar

计算中的另一个标量;也接受Python标量

opMonoid or BinaryOp

用于相交值的操作符

Returns
ScalarExpression that will be empty if any of the inputs is empty

Examples

# Method syntax
c << a.ewise_mult(b, op=binary.gt)

# Functional syntax
c << binary.gt(a & b)
ewise_union(other, op, left_default, right_default)#

对稀疏值的并集执行逐元素计算,类似于人们期望稀疏数据的减法如何工作。

请参阅用户指南中的元素级联合部分以获取更多详细信息,特别是关于ewise_union和ewise_add()之间的区别。

Parameters
otherScalar

计算中的另一个标量;也接受Python标量

opMonoid or BinaryOp

要使用的操作符

left_default

当左侧索引缺失时使用的标量值

right_default

当右侧索引缺失时使用的标量值

Returns
ScalarExpression with a structure formed as the union of the input structures

Examples

# Method syntax
c << a.ewise_union(b, op=binary.div, left_default=1, right_default=1)

# Functional syntax
c << binary.div(a | b, left_default=1, right_default=1)
classmethod from_value(value, dtype=None, *, is_cscalar=False, name=None)#

从值创建一个新的标量。

Parameters
valuePython scalar

标量的内部值。

dtype

标量的数据类型。如果未提供,将检查值以选择适当的dtype。

is_cscalarbool, default=False

如果为True,空状态在Python端管理,而不是使用适当的GrB_Scalar对象。

namestr, optional

为标量指定的名称。

Returns
Scalar
get(default=None)#

获取标量的内部值作为Python标量。

Parameters
default

如果内部值缺失,则返回的值。

Returns
Python scalar
isclose(other, *, rel_tol=1e-07, abs_tol=0.0, check_dtype=False)#

检查近似相等性(包括值是否缺失)。

等价于:abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

Parameters
otherScalar

要比较的标量

rel_tolfloat

相对容差

abs_tolfloat

绝对容差

check_dtypebool

如果为True,还会检查数据类型是否匹配

Returns
bool
isequal(other, *, check_dtype=False)#

检查是否完全相等(包括值是否缺失)。

Parameters
otherScalar

要比较的标量

check_dtypesbool, default=False

如果为True,还会检查数据类型是否匹配

Returns
bool

另请参阅

isclose()

用于浮点数据类型的相等性检查

wait(how='materialize')#

等待计算完成或建立“发生之前”关系。

Parameters
how{“materialize”, “complete”}

“materialize” 完全计算一个对象。 “complete” 建立一个“发生在之前”的关系,对多线程有用。 有关更多详细信息,请参阅 GraphBLAS 文档。

In `non-blocking mode <../user_guide/init.html#graphblas-modes>`__,
the computations may be delayed and not yet safe to use by multiple threads.
Use wait to force completion of the Scalar.
Has no effect in `blocking mode <../user_guide/init.html#graphblas-modes>`__.
property is_cscalar#

如果空状态在Python端管理,则返回True。

property is_empty#

指示标量是否为空。

property is_grbscalar#

如果空状态由GraphBLAS后端管理,则返回True。

property nvals#

非空值的数量。

只能是0或1。

property value#

返回由 Scalar 持有的值作为 Python 对象,如果 Scalar 为空,则返回 None。

将值分配给 value 将会更新 Scalar。

示例用法:

>>> s.value
15
>>> s.value = 16
>>> s.value
16