@@ -3871,11 +3871,11 @@ def matmul(x1: NDArray, x2: NDArray, **kwargs: Any) -> NDArray:
3871
3871
r = x2 .chunks [- 1 ]
3872
3872
3873
3873
for row in range (0 , n , p ):
3874
- row_end = (row + p ) if ( row + p ) < n else n
3874
+ row_end = builtins . min (row + p , n )
3875
3875
for col in range (0 , m , q ):
3876
- col_end = (col + q ) if ( col + q ) < m else m
3876
+ col_end = builtins . min (col + q , m )
3877
3877
for aux in range (0 , k , r ):
3878
- aux_end = (aux + r ) if ( aux + r ) < k else k
3878
+ aux_end = builtins . min (aux + r , k )
3879
3879
bx1 = x1 [row :row_end , aux :aux_end ]
3880
3880
bx2 = x2 [aux :aux_end , col :col_end ]
3881
3881
result [row :row_end , col :col_end ] += np .matmul (bx1 , bx2 )
@@ -3905,8 +3905,8 @@ def permute_dims(arr: NDArray, axes: tuple[int] | list[int] | None = None, **kwa
3905
3905
3906
3906
Returns
3907
3907
-------
3908
- out: : ref:`NDArray`
3909
- A Blosc2 : ref:`NDArray` with axes transposed.
3908
+ out:: ref:`NDArray`
3909
+ A Blosc2: ref:`NDArray` with axes transposed.
3910
3910
3911
3911
Raises
3912
3912
------
@@ -3947,8 +3947,6 @@ def permute_dims(arr: NDArray, axes: tuple[int] | list[int] | None = None, **kwa
3947
3947
[17, 18, 19, 20],
3948
3948
[21, 22, 23, 24]]])
3949
3949
3950
-
3951
-
3952
3950
>>> at = blosc2.permute_dims(a, axes=(1, 0, 2))
3953
3951
>>> at[:]
3954
3952
array([[[ 1, 2, 3, 4],
@@ -3959,37 +3957,39 @@ def permute_dims(arr: NDArray, axes: tuple[int] | list[int] | None = None, **kwa
3959
3957
[21, 22, 23, 24]]])
3960
3958
3961
3959
"""
3962
-
3963
3960
if np .isscalar (arr ) or arr .ndim < 2 :
3964
3961
return arr
3965
3962
3963
+ ndim = arr .ndim
3964
+
3966
3965
if axes is None :
3967
- axes = range (arr . ndim )[::- 1 ]
3966
+ axes = tuple ( range (ndim ) )[::- 1 ]
3968
3967
else :
3969
- axes_transformed = tuple (axis if axis >= 0 else arr .ndim + axis for axis in axes )
3970
- if sorted (axes_transformed ) != list (range (arr .ndim )):
3971
- raise ValueError (f"axes { axes } is not a valid permutation of { arr .ndim } dimensions" )
3972
- axes = axes_transformed
3968
+ axes = tuple (axis if axis >= 0 else ndim + axis for axis in axes )
3969
+ if sorted (axes ) != list (range (ndim )):
3970
+ raise ValueError (f"axes { axes } is not a valid permutation of { ndim } dimensions" )
3973
3971
3974
3972
new_shape = tuple (arr .shape [axis ] for axis in axes )
3975
- if "chunks" not in kwargs :
3973
+ if "chunks" not in kwargs or kwargs [ "chunks" ] is None :
3976
3974
kwargs ["chunks" ] = tuple (arr .chunks [axis ] for axis in axes )
3977
3975
3978
3976
result = blosc2 .empty (shape = new_shape , dtype = arr .dtype , ** kwargs )
3979
3977
3980
- chunk_slices = [
3981
- [slice (start , builtins .min (dim , start + chunk )) for start in range (0 , dim , chunk )]
3982
- for dim , chunk in zip (arr .shape , arr .chunks , strict = False )
3983
- ]
3978
+ chunks = arr .chunks
3979
+ shape = arr .shape
3980
+
3981
+ for info in arr .iterchunks_info ():
3982
+ coords = info .coords
3983
+ start_stop = [
3984
+ (coord * chunk , builtins .min (chunk * (coord + 1 ), dim ))
3985
+ for coord , chunk , dim in zip (coords , chunks , shape , strict = False )
3986
+ ]
3984
3987
3985
- block_counts = [ len ( s ) for s in chunk_slices ]
3986
- grid = np . indices ( block_counts ). reshape ( len ( block_counts ), - 1 ). T
3988
+ src_slice = tuple ( slice ( start , stop ) for start , stop in start_stop )
3989
+ dst_slice = tuple ( slice ( start_stop [ ax ][ 0 ], start_stop [ ax ][ 1 ]) for ax in axes )
3987
3990
3988
- for idx in grid :
3989
- block_slices = tuple (chunk_slices [axis ][i ] for axis , i in enumerate (idx ))
3990
- block = arr [block_slices ]
3991
- target_slices = tuple (block_slices [axis ] for axis in axes )
3992
- result [target_slices ] = np .transpose (block , axes = axes ).copy ()
3991
+ transposed = np .transpose (arr [src_slice ], axes = axes )
3992
+ result [dst_slice ] = np .ascontiguousarray (transposed )
3993
3993
3994
3994
return result
3995
3995
@@ -4002,14 +4002,14 @@ def transpose(x, **kwargs: Any) -> NDArray:
4002
4002
4003
4003
Parameters
4004
4004
----------
4005
- x: : ref:`NDArray`
4005
+ x:: ref:`NDArray`
4006
4006
The input array.
4007
4007
kwargs: Any, optional
4008
4008
Keyword arguments that are supported by the :func:`empty` constructor.
4009
4009
4010
4010
Returns
4011
4011
-------
4012
- out: : ref:`NDArray`
4012
+ out:: ref:`NDArray`
4013
4013
The Blosc2 NDArray with axes transposed.
4014
4014
4015
4015
References
@@ -4023,7 +4023,7 @@ def transpose(x, **kwargs: Any) -> NDArray:
4023
4023
stacklevel = 2 ,
4024
4024
)
4025
4025
4026
- # If arguments are dimension < 2 they are returned
4026
+ # If arguments are dimension < 2, they are returned
4027
4027
if np .isscalar (x ) or x .ndim < 2 :
4028
4028
return x
4029
4029
@@ -4039,14 +4039,14 @@ def matrix_transpose(arr: NDArray, **kwargs: Any) -> NDArray:
4039
4039
4040
4040
Parameters
4041
4041
----------
4042
- arr: : ref:`NDArray`
4042
+ arr:: ref:`NDArray`
4043
4043
The input NDArray having shape ``(..., M, N)`` and whose innermost two dimensions form
4044
4044
``MxN`` matrices.
4045
4045
4046
4046
Returns
4047
4047
-------
4048
- out: : ref:`NDArray`
4049
- A new : ref:`NDArray` containing the transpose for each matrix and having shape
4048
+ out:: ref:`NDArray`
4049
+ A new: ref:`NDArray` containing the transpose for each matrix and having shape
4050
4050
``(..., N, M)``.
4051
4051
"""
4052
4052
axes = None
0 commit comments