Skip to content

Commit 4ef3a88

Browse files
committed
Rename tensor to blockarray and BlockTensor to BlockArray
Also tried to update documentation to no longer refer to BlockTensor
1 parent 0e7be7c commit 4ef3a88

File tree

10 files changed

+52
-53
lines changed

10 files changed

+52
-53
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# BlockTensor
1+
# BlockArray
22

3-
BlockTensor is a package for working with tensors logically partitioned into blocks (or subtensors) in a nested format. For example, block matrices and block vectors can be created as:
3+
BlockArray is a package for working with tensors logically partitioned into blocks (or subtensors) in a nested format. For example, block matrices and block vectors can be created as:
44
```python
5-
from blocktensor.tensor import BlockTensor
6-
from blocktensor.linalg import mult_mat_vec
5+
from BlockArray.tensor import BlockArray
6+
from BlockArray.linalg import mult_mat_vec
77

88
# model the block vector
99
# [x0, x1]
1010
# where x0 = np.array([1, 2, 3])
1111
# x1 = np.array([4, 5])
12-
x = BlockTensor([np.array([1, 2, 3]), np.array([4, 5])])
12+
x = BlockArray([np.array([1, 2, 3]), np.array([4, 5])])
1313

1414
# model the block matrix
1515
# [[A00, A01],
@@ -29,7 +29,7 @@ A10 = np.array(
2929
A11 = np.array(
3030
[[1, 2],
3131
[3, 4]])
32-
A = BlockTensor([[A00, A01], [A10, A11]])
32+
A = BlockArray([[A00, A01], [A10, A11]])
3333

3434
# Basic math operations
3535
y = mult_mat_vec(2*A, x)
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def _block_shape(array: larr.LabelledArray):
3939

4040
def validate_subtensor_shapes(array: larr.LabelledArray, bshape):
4141
"""
42-
Validate subtensors in a BlockTensor have a valid shape
42+
Validate subtensors in a BlockArray have a valid shape
4343
4444
array :
4545
The block array containing the subtensors
4646
shape :
47-
The target block shape of the BlockTensor
47+
The target block shape of the BlockArray
4848
"""
4949
# Subtensors are valid along an axis at a certain block if:
5050
# all subtensors in the remaining dimensions have the same shape along that
@@ -69,13 +69,13 @@ def validate_subtensor_shapes(array: larr.LabelledArray, bshape):
6969
valid_bsizes = [bsize == _bsize for _bsize in ascblock_sizes]
7070
assert all(valid_bsizes)
7171

72-
class BlockTensor:
72+
class BlockArray:
7373
"""
7474
An n-dimensional block tensor object
7575
76-
`BlockTensor` has two main attributes: an underlying `LabelledArray` that stores the subtensors in an n-d layout and a `bshape` attributes that stores the shape of the blocks along each axis.
76+
`BlockArray` has two main attributes: an underlying `LabelledArray` that stores the subtensors in an n-d layout and a `bshape` attributes that stores the shape of the blocks along each axis.
7777
78-
For example, consider a `BlockTensor` `A` with the block shape `((10, 5), (2, 4))`.
78+
For example, consider a `BlockArray` `A` with the block shape `((10, 5), (2, 4))`.
7979
This represents a matrix with blocks of size 10 and 5 along the rows, and blocks of size 2 and 4 along the columns. Subtensors of `A` would then have shapes:
8080
`A[0, 0].shape == (10, 2)`
8181
`A[0, 1].shape == (10, 4)`
@@ -316,7 +316,7 @@ def __rtruediv__(self, other):
316316
## Numpy ufunc interface
317317
def __array_ufunc__(ufunc, method, *inputs, **kwargs):
318318
for btensor in inputs:
319-
if not isinstance(btensor, BlockTensor):
319+
if not isinstance(btensor, BlockArray):
320320
raise TypeError(
321321
f"ufunc {ufunc} cannot be called with inputs" +
322322
", ".join([f"{type(input)}" for input in inputs])
@@ -329,24 +329,24 @@ def __array_ufunc__(ufunc, method, *inputs, **kwargs):
329329
ret_shape = inputs[0].shape
330330
ret_labels = inputs[0].labels
331331

332-
return BlockTensor(subtensors_out, ret_shape, ret_labels)
332+
return BlockArray(subtensors_out, ret_shape, ret_labels)
333333

334334
def validate_elementwise_binary_op(a, b):
335335
"""
336-
Validates if BlockTensor inputs are applicable
336+
Validates if BlockArray inputs are applicable
337337
"""
338338
assert a.bshape == b.bshape
339339

340-
def _elementwise_binary_op(op: Callable[[T, T], T], a: BlockTensor, b: BlockTensor):
340+
def _elementwise_binary_op(op: Callable[[T, T], T], a: BlockArray, b: BlockArray):
341341
"""
342-
Compute elementwise binary operation on BlockTensors
342+
Compute elementwise binary operation on BlockArrays
343343
344344
Parameters
345345
----------
346346
op: function
347347
A function with signature func(a, b) -> c, where a, b, c are vector of
348348
the same shape
349-
a, b: BlockTensor
349+
a, b: BlockArray
350350
"""
351351
validate_elementwise_binary_op(a, b)
352352
array = tuple([op(ai, bi) for ai, bi in zip(a.subtensors_flat, b.subtensors_flat)])
@@ -364,13 +364,13 @@ def _elementwise_binary_op(op: Callable[[T, T], T], a: BlockTensor, b: BlockTens
364364
power = functools.partial(_elementwise_binary_op, operator.pow)
365365

366366

367-
def _elementwise_unary_op(op: Callable[[T], T], a: BlockTensor):
367+
def _elementwise_unary_op(op: Callable[[T], T], a: BlockArray):
368368
"""
369-
Compute elementwise unary operation on a BlockTensor
369+
Compute elementwise unary operation on a BlockArray
370370
371371
Parameters
372372
----------
373-
a: BlockTensor
373+
a: BlockArray
374374
"""
375375
array = larr.LabelledArray([op(ai) for ai in a.subtensors_flat], a.shape, a.labels)
376376
return type(a)(array)
@@ -385,9 +385,9 @@ def scalar_mul(alpha, a):
385385
def scalar_div(alpha, a):
386386
return _elementwise_unary_op(lambda subvec: subvec/alpha, a)
387387

388-
def to_ndarray(block_tensor: BlockTensor):
388+
def to_ndarray(block_tensor: BlockArray):
389389
"""
390-
Convert a BlockTensor object to a ndarray object
390+
Convert a BlockArray object to a ndarray object
391391
"""
392392
# .bsize (block size) is the resulting shape of the monolithic array
393393
ret_array = np.zeros(block_tensor.mshape)

blocktensor/h5utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Utilities for reading/writing BlockTensor objects to hdf5
2+
Utilities for reading/writing BlockArray objects to hdf5
33
"""
44

55
import h5py

blocktensor/mat.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
import itertools
77
import numpy as np
8-
from blocktensor.tensor import BlockTensor
8+
from blocktensor.blockarray import BlockArray
99
from petsc4py import PETSc
1010

1111
from . import subops as gops
1212
from .labelledarray import LabelledArray, flatten_array
13-
from .tensor import BlockTensor
1413

1514
# pylint: disable=no-member
1615

@@ -366,7 +365,7 @@ def convert_bmat_to_petsc(bmat):
366365
barray = LabelledArray(mats, bmat.shape, bmat.labels)
367366
return BlockMatrix(barray)
368367

369-
class BlockMatrix(BlockTensor):
368+
class BlockMatrix(BlockArray):
370369
"""
371370
Represents a block matrix with blocks indexed by keys
372371
"""

blocktensor/ufunc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from . import types
10-
from .tensor import BlockTensor
10+
from .blockarray import BlockArray
1111

1212
Signature = Tuple[str, ...]
1313
Signatures = List[Signature]
@@ -204,7 +204,7 @@ def recursive_concatenate(arrays: types.FlatArray, shape: types.Shape, axes: typ
204204

205205
def apply_ufunc(ufunc: np.ufunc, method: str, *inputs, **kwargs):
206206
"""
207-
Apply a ufunc on sequence of BlockTensor inputs
207+
Apply a ufunc on sequence of BlockArray inputs
208208
"""
209209
if method != '__call__':
210210
raise ValueError(f"ufunc method {method} is not supported")
@@ -258,7 +258,7 @@ def apply_ufunc(ufunc: np.ufunc, method: str, *inputs, **kwargs):
258258

259259
subtensors_out.append(ufunc(*subtensor_ins, **kwargs))
260260

261-
outputs.append(BlockTensor(subtensors_out, shape_out, labels_out))
261+
outputs.append(BlockArray(subtensors_out, shape_out, labels_out))
262262

263263
if len(outputs) == 1:
264264
return outputs[0]

blocktensor/vec.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from petsc4py import PETSc
1111

1212
from . import subops as gops
13-
from .tensor import BlockTensor
13+
from .blockarray import BlockArray
1414
from .mat import BlockMatrix
1515

1616
## pylint: disable=no-member
@@ -94,7 +94,7 @@ def norm(a):
9494
"""Return the 2-norm of a vector"""
9595
return dot(a, a)**0.5
9696

97-
class BlockVector(BlockTensor):
97+
class BlockVector(BlockArray):
9898
"""
9999
Represents a block vector with blocks indexed by labels
100100
"""
@@ -138,8 +138,8 @@ def __setitem__(self, key, value):
138138
value : array_like or BlockVector
139139
"""
140140
_array = self[key]
141-
if isinstance(_array, BlockTensor):
142-
if isinstance(value, BlockTensor):
141+
if isinstance(_array, BlockArray):
142+
if isinstance(value, BlockArray):
143143
for subvec, subvec_value in zip(_array, value):
144144
gops.set_vec(subvec, subvec_value)
145145
else:

docs/source/intro.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
Introduction
33
************
44

5-
This package provides a ``BlockTensor`` object that makes it easier to work with tensors that are logically divided into multiple blocks or subtensors.
6-
The basic usage of the ``BlockTensor`` is illustrated below for the case of a block matrix and block vector::
5+
This package provides a ``BlockArray`` object that makes it easier to work with tensors that are logically divided into multiple blocks or subtensors.
6+
The basic usage of the ``BlockArray`` is illustrated below for the case of a block matrix and block vector::
77

88
import numpy as np
9-
from blocktensor.tensor import BlockTensor
9+
from blocktensor.tensor import BlockArray
1010

1111
# `A` is a block representation of the matrix
1212
# [[1, 2, 3],
@@ -24,15 +24,15 @@ The basic usage of the ``BlockTensor`` is illustrated below for the case of a bl
2424
[[7, 8]])
2525
A11 = np.array(
2626
[[9]])
27-
A = BlockTensor([[A00, A01], [A10, A11]], labels=(('a', 'b'), ('a', 'b')))
27+
A = BlockArray([[A00, A01], [A10, A11]], labels=(('a', 'b'), ('a', 'b')))
2828

2929

3030
# `X` is a block representation of the vector
3131
# [1, 2, 3]
3232
# the first block is labelled 'a' and the second 'b'
3333
X0 = np.array([1, 2])
3434
X1 = np.array([3])
35-
X = BlockTensor([X0, X1], labels=(('a', 'b'),))
35+
X = BlockArray([X0, X1], labels=(('a', 'b'),))
3636

3737
In the above example, the matrix ``A`` is represented by 2 row blocks (with corresponding submatrix row sizes 2 and 1) and 2 column blocks (with corresponding submatrix column sizes 2 and 1) while the vector ``X`` is represented with two row blocks (with corresponding subvector sizes 2 and 1).
3838
Blocks of ``A`` and ``X`` are also labelled; labels are useful to organize blocks since blocks often represent different different systems.
@@ -47,9 +47,9 @@ Indexing block tensors works similarly to indexing in ``numpy`` with some additi
4747
* ``tensor[0]`` returns subtensor ``a``
4848
* ``tensor[-1]`` returns subtensor ``c``
4949
* range of indices by slice
50-
* ``tensor[0:2]`` returns a ``BlockTensor`` with subtensors ``(a, b)``
51-
* ``tensor[:]`` returns the same ``BlockTensor`` as ``tensor``
50+
* ``tensor[0:2]`` returns a ``BlockArray`` with subtensors ``(a, b)``
51+
* ``tensor[:]`` returns the same ``BlockArray`` as ``tensor``
5252
* range of indices by list of single indices
53-
* ``tensor[['a', 'b']]`` returns a ``BlockTensor`` with subtensors ``(a, b)``
54-
* ``tensor[[0, 1]]`` returns a ``BlockTensor`` with subtensors ``(a, b)``
55-
* ``tensor[[0, -1]]`` returns a ``BlockTensor`` with subtensors ``(a, c)``
53+
* ``tensor[['a', 'b']]`` returns a ``BlockArray`` with subtensors ``(a, b)``
54+
* ``tensor[[0, 1]]`` returns a ``BlockArray`` with subtensors ``(a, b)``
55+
* ``tensor[[0, -1]]`` returns a ``BlockArray`` with subtensors ``(a, c)``
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44

55
import numpy as np
66

7-
import blocktensor.tensor as btensor
7+
import blocktensor.blockarray as ba
88

99

1010
a = np.ones((4, 4))
1111
b = np.ones((4, 2))
1212
c = np.ones((2, 4))
1313
d = np.ones((2, 2))
14-
A = btensor.BlockTensor([[a, b], [c, d]])
14+
A = ba.BlockArray([[a, b], [c, d]])
1515

1616
a = 2 * np.ones((4, 4))
1717
b = 2 * np.ones((4, 2))
1818
c = 2 * np.ones((2, 4))
1919
d = 2 * np.ones((2, 2))
20-
B = btensor.BlockTensor([[a, b], [c, d]])
20+
B = ba.BlockArray([[a, b], [c, d]])
2121

2222

2323
def _test_elementwise_binary_op(sub_op, a, b, block_op=None):
2424
"""
2525
Test a generic element-wise binary operation
2626
2727
This should test whether a operation applied across each block gives the
28-
same result as an equivalent operation on the BlockTensors
28+
same result as an equivalent operation on the BlockArrays
2929
"""
3030
if block_op is None:
3131
block_op = sub_op
@@ -51,7 +51,7 @@ def test_div():
5151
_test_elementwise_binary_op(lambda x, y: x/y, A, B)
5252

5353
def test_power():
54-
_test_elementwise_binary_op(lambda x, y: x**y, A, B, btensor.power)
54+
_test_elementwise_binary_op(lambda x, y: x**y, A, B, ba.power)
5555

5656
def test_bshape():
5757
print(f"A.bshape = {A.bshape}")

tests/test_ufuncutils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import numpy as np
3-
from blocktensor import ufunc, tensor as btensor
3+
from blocktensor import blockarray as btensor, ufunc
44

55
SIGNATURE = '(i,j),(j,k)->(i, k)'
66

@@ -57,7 +57,7 @@ def test_recursive_concatenate():
5757
b = np.ones((4, 2))
5858
c = np.ones((2, 4))
5959
d = np.ones((2, 2))
60-
A = btensor.BlockTensor([[a, b], [c, d]])
60+
A = btensor.BlockArray([[a, b], [c, d]])
6161

6262
ufunc.recursive_concatenate(A.subtensors_flat, A.shape, A.dims)
6363

@@ -66,13 +66,13 @@ def test_apply_ufunc():
6666
b = np.ones((4, 2))
6767
c = np.ones((2, 4))
6868
d = np.ones((2, 2))
69-
A = btensor.BlockTensor([[a, b], [c, d]])
69+
A = btensor.BlockArray([[a, b], [c, d]])
7070

7171
a = np.ones((4, 4))
7272
b = np.ones((4, 2))
7373
c = np.ones((2, 4))
7474
d = np.ones((2, 2))
75-
B = btensor.BlockTensor([[a, b], [c, d]])
75+
B = btensor.BlockArray([[a, b], [c, d]])
7676

7777
# C = ufuncutils.apply_ufunc(np.add, '__call__', *[A, B])
7878
# print(C.shape)

tests/test_vec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from blocktensor import mat as bmat
55
from blocktensor import linalg as bla
66
from blocktensor import vec as bvec
7-
from blocktensor import tensor as btensor
7+
from blocktensor import blockarray as btensor
88

99
# pylint: disable=unused-import
1010
# pylint: disable=missing-function-docstring

0 commit comments

Comments
 (0)