capytaine 2.2__cp312-cp312-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. capytaine/.dylibs/libgcc_s.1.1.dylib +0 -0
  2. capytaine/.dylibs/libgfortran.5.dylib +0 -0
  3. capytaine/.dylibs/libquadmath.0.dylib +0 -0
  4. capytaine/__about__.py +16 -0
  5. capytaine/__init__.py +35 -0
  6. capytaine/bem/__init__.py +0 -0
  7. capytaine/bem/airy_waves.py +106 -0
  8. capytaine/bem/engines.py +441 -0
  9. capytaine/bem/problems_and_results.py +545 -0
  10. capytaine/bem/solver.py +497 -0
  11. capytaine/bodies/__init__.py +4 -0
  12. capytaine/bodies/bodies.py +1185 -0
  13. capytaine/bodies/dofs.py +19 -0
  14. capytaine/bodies/predefined/__init__.py +6 -0
  15. capytaine/bodies/predefined/cylinders.py +151 -0
  16. capytaine/bodies/predefined/rectangles.py +109 -0
  17. capytaine/bodies/predefined/spheres.py +70 -0
  18. capytaine/green_functions/__init__.py +2 -0
  19. capytaine/green_functions/abstract_green_function.py +12 -0
  20. capytaine/green_functions/delhommeau.py +432 -0
  21. capytaine/green_functions/libs/Delhommeau_float32.cpython-312-darwin.so +0 -0
  22. capytaine/green_functions/libs/Delhommeau_float64.cpython-312-darwin.so +0 -0
  23. capytaine/green_functions/libs/__init__.py +0 -0
  24. capytaine/io/__init__.py +0 -0
  25. capytaine/io/bemio.py +141 -0
  26. capytaine/io/legacy.py +328 -0
  27. capytaine/io/mesh_loaders.py +1085 -0
  28. capytaine/io/mesh_writers.py +692 -0
  29. capytaine/io/meshio.py +38 -0
  30. capytaine/io/xarray.py +516 -0
  31. capytaine/matrices/__init__.py +16 -0
  32. capytaine/matrices/block.py +590 -0
  33. capytaine/matrices/block_toeplitz.py +325 -0
  34. capytaine/matrices/builders.py +89 -0
  35. capytaine/matrices/linear_solvers.py +232 -0
  36. capytaine/matrices/low_rank.py +393 -0
  37. capytaine/meshes/__init__.py +6 -0
  38. capytaine/meshes/clipper.py +464 -0
  39. capytaine/meshes/collections.py +324 -0
  40. capytaine/meshes/geometry.py +409 -0
  41. capytaine/meshes/meshes.py +868 -0
  42. capytaine/meshes/predefined/__init__.py +6 -0
  43. capytaine/meshes/predefined/cylinders.py +314 -0
  44. capytaine/meshes/predefined/rectangles.py +261 -0
  45. capytaine/meshes/predefined/spheres.py +62 -0
  46. capytaine/meshes/properties.py +242 -0
  47. capytaine/meshes/quadratures.py +80 -0
  48. capytaine/meshes/quality.py +448 -0
  49. capytaine/meshes/surface_integrals.py +63 -0
  50. capytaine/meshes/symmetric.py +383 -0
  51. capytaine/post_pro/__init__.py +6 -0
  52. capytaine/post_pro/free_surfaces.py +88 -0
  53. capytaine/post_pro/impedance.py +92 -0
  54. capytaine/post_pro/kochin.py +54 -0
  55. capytaine/post_pro/rao.py +60 -0
  56. capytaine/tools/__init__.py +0 -0
  57. capytaine/tools/cache_on_disk.py +26 -0
  58. capytaine/tools/deprecation_handling.py +18 -0
  59. capytaine/tools/lists_of_points.py +52 -0
  60. capytaine/tools/lru_cache.py +49 -0
  61. capytaine/tools/optional_imports.py +27 -0
  62. capytaine/tools/prony_decomposition.py +94 -0
  63. capytaine/tools/symbolic_multiplication.py +107 -0
  64. capytaine/ui/__init__.py +0 -0
  65. capytaine/ui/cli.py +28 -0
  66. capytaine/ui/rich.py +5 -0
  67. capytaine/ui/vtk/__init__.py +3 -0
  68. capytaine/ui/vtk/animation.py +329 -0
  69. capytaine/ui/vtk/body_viewer.py +28 -0
  70. capytaine/ui/vtk/helpers.py +82 -0
  71. capytaine/ui/vtk/mesh_viewer.py +461 -0
  72. capytaine-2.2.dist-info/LICENSE +674 -0
  73. capytaine-2.2.dist-info/METADATA +751 -0
  74. capytaine-2.2.dist-info/RECORD +76 -0
  75. capytaine-2.2.dist-info/WHEEL +4 -0
  76. capytaine-2.2.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,590 @@
1
+ """This module implements block matrices to be used in Hierarchical Toeplitz matrices.
2
+
3
+ It takes inspiration from the following works:
4
+
5
+ * `openHmx module from Gypsilab by Matthieu Aussal (GPL licensed) <https://github.com/matthieuaussal/gypsilab>`_
6
+ * `HierarchicalMatrices by Markus Neumann (GPL licensed) <https://github.com/maekke97/HierarchicalMatrices>`_
7
+ """
8
+ # Copyright (C) 2017-2019 Matthieu Ancellin
9
+ # See LICENSE file at <https://github.com/mancellin/capytaine>
10
+
11
+ import logging
12
+
13
+ from numbers import Number
14
+ from typing import Tuple, List, Callable, Union, Iterable
15
+ from itertools import cycle, accumulate, chain, product
16
+ from collections.abc import Iterator
17
+
18
+ import numpy as np
19
+
20
+ from capytaine.matrices.low_rank import LowRankMatrix
21
+ from capytaine.tools.optional_imports import import_optional_dependency
22
+
23
+ LOG = logging.getLogger(__name__)
24
+
25
+
26
+ class BlockMatrix:
27
+ """A (2D) matrix, stored as a set of submatrices (or blocks).
28
+
29
+ Parameters
30
+ ----------
31
+ blocks: list of list of matrices
32
+ The blocks of the block matrix.
33
+ check: bool, optional
34
+ Should the code perform sanity checks on the inputs? (default: True)
35
+
36
+ Attributes
37
+ ----------
38
+ shape: pair of ints
39
+ shape of the full matrix
40
+ nb_blocks: pair of ints
41
+ number of blocks in each directions.
42
+ Example::
43
+
44
+ AAAABB
45
+ AAAABB -> nb_blocks = (1, 2)
46
+ AAAABB
47
+ """
48
+
49
+ ndim = 2 # Other dimensions have not been implemented.
50
+
51
+ def __init__(self, blocks, _stored_block_shapes=None, check=True):
52
+
53
+ self._stored_nb_blocks = (len(blocks), len(blocks[0]))
54
+
55
+ self._stored_blocks = np.empty(self._stored_nb_blocks, dtype=object)
56
+ for i in range(len(blocks)):
57
+ for j in range(len(blocks[i])):
58
+ self._stored_blocks[i, j] = blocks[i][j]
59
+
60
+ if _stored_block_shapes is None:
61
+ self._stored_block_shapes = ([block.shape[0] for block in self._stored_blocks[:, 0]],
62
+ [block.shape[1] for block in self._stored_blocks[0, :]])
63
+ else:
64
+ # To avoid going down the tree if it is already known.
65
+ self._stored_block_shapes = _stored_block_shapes
66
+
67
+ # Block shape of the full matrix
68
+ self.nb_blocks = self._compute_nb_blocks()
69
+
70
+ # Total shape of the full matrix
71
+ self.shape = self._compute_shape()
72
+
73
+ LOG.debug(f"New block matrix: %s", self)
74
+
75
+ if check:
76
+ assert self._check_dimensions_of_blocks()
77
+ assert self._check_dtype()
78
+
79
+ def _compute_shape(self):
80
+ # In a dedicated routine because it will be overloaded by subclasses.
81
+ return sum(self._stored_block_shapes[0]), sum(self._stored_block_shapes[1])
82
+
83
+ def _compute_nb_blocks(self):
84
+ return self._stored_nb_blocks
85
+
86
+ def _check_dimensions_of_blocks(self) -> bool:
87
+ """Check that the dimensions of the blocks are consistent."""
88
+ if not all(block.ndim == self.ndim for line in self._stored_blocks for block in line):
89
+ return False
90
+
91
+ for line in self.all_blocks:
92
+ block_height = line[0].shape[0]
93
+ for block in line[1:]:
94
+ if not block.shape[0] == block_height: # Same height on a given line
95
+ return False
96
+
97
+ for col in np.moveaxis(self.all_blocks, 1, 0):
98
+ block_width = col[0].shape[1]
99
+ for block in col[1:]:
100
+ if not block.shape[1] == block_width: # Same width on a given column
101
+ return False
102
+ return True
103
+
104
+ def _check_dtype(self) -> bool:
105
+ """Check that the type of the blocks are consistent."""
106
+ for line in self._stored_blocks:
107
+ for block in line:
108
+ if block.dtype != self.dtype:
109
+ return False
110
+ return True
111
+
112
+ # ACCESSING DATA
113
+
114
+ @property
115
+ def dtype(self):
116
+ """The type of data of all of the subblocks."""
117
+ try:
118
+ return self._stored_blocks[0][0].dtype
119
+ except AttributeError:
120
+ return None
121
+
122
+ @property
123
+ def all_blocks(self) -> np.ndarray:
124
+ """The matrix of matrices. For a full block matrix, all the blocks are stored in memory."""
125
+ return self._stored_blocks
126
+
127
+ @property
128
+ def block_shapes(self) -> Tuple[List[int], List[int]]:
129
+ """The shapes of the blocks composing the BlockMatrix.
130
+
131
+ Example::
132
+
133
+ AAAABB
134
+ AAAABB -> block_shapes = ([3], [4, 2])
135
+ AAAABB
136
+ """
137
+ return self._stored_block_shapes
138
+
139
+ def _stored_block_positions(self, global_frame=(0, 0)) -> Iterable[List[Tuple[int, int]]]:
140
+ """The position of each blocks in the matrix as a generator.
141
+ The list is used by subclasses where the same block may appear several times in different positions.
142
+
143
+ Parameters
144
+ ----------
145
+ global_frame: Tuple[int], optional
146
+ the coordinate of the top right corner. Default: 0, 0.
147
+
148
+ Example::
149
+
150
+ AAAABB
151
+ AAAABB -> list(matrix._stored_block_positions) = [[(0,0)], [(0, 4)], [(2, 0)], [(2, 4)]]
152
+ CCCCDD
153
+ """
154
+ x_acc = accumulate([0] + self.block_shapes[0][:-1])
155
+ y_acc = accumulate([0] + self.block_shapes[1][:-1])
156
+ return ([(global_frame[0] + x, global_frame[1] + y)] for x, y in product(x_acc, y_acc))
157
+
158
+ def _put_in_full_matrix(self, full_matrix: np.ndarray, where=(0, 0)) -> None:
159
+ """Copy the content of the block matrix in a matrix, which is modified in place."""
160
+ all_blocks_in_flat_iterator = (block for line in self._stored_blocks for block in line)
161
+ positions_of_all_blocks = self._stored_block_positions(global_frame=where)
162
+ for block, positions_of_the_block in zip(all_blocks_in_flat_iterator, positions_of_all_blocks):
163
+ if isinstance(block, BlockMatrix):
164
+ position_of_first_appearance = positions_of_the_block[0]
165
+ frame_of_first_appearance = (slice(position_of_first_appearance[0], position_of_first_appearance[0]+block.shape[0]),
166
+ slice(position_of_first_appearance[1], position_of_first_appearance[1]+block.shape[1]))
167
+ block._put_in_full_matrix(full_matrix, where=position_of_first_appearance)
168
+
169
+ for position in positions_of_the_block[1:]: # For the other appearances, only copy the first appearance
170
+ block_frame = (slice(position[0], position[0]+block.shape[0]),
171
+ slice(position[1], position[1]+block.shape[1]))
172
+ full_matrix[block_frame] = full_matrix[frame_of_first_appearance]
173
+
174
+ else:
175
+ full_block = block if isinstance(block, np.ndarray) else block.full_matrix()
176
+ for position in positions_of_the_block:
177
+ block_frame = (slice(position[0], position[0]+block.shape[0]),
178
+ slice(position[1], position[1]+block.shape[1]))
179
+ full_matrix[block_frame] = full_block
180
+
181
+ def full_matrix(self, dtype=None) -> np.ndarray:
182
+ """Flatten the block structure and return a full matrix."""
183
+ if dtype is None: dtype = self.dtype
184
+ full_matrix = np.empty(self.shape, dtype=dtype)
185
+ self._put_in_full_matrix(full_matrix)
186
+ return full_matrix
187
+
188
+ def __array__(self, dtype=None):
189
+ return self.full_matrix(dtype=dtype)
190
+
191
+ def no_toeplitz(self):
192
+ """Recursively replace the block toeplitz matrices by usual block matrices.
193
+ WARNING: the block matrices may still contain several references to the same block."""
194
+ blocks = [[block.no_toeplitz() if isinstance(block, BlockMatrix) else block for block in line] for line in self.all_blocks]
195
+ return BlockMatrix(blocks)
196
+
197
+ def __deepcopy__(self, memo):
198
+ from copy import deepcopy
199
+ blocks = [[deepcopy(block) for block in line] for line in self._stored_blocks]
200
+ # The call to deepcopy does not use the memo on purpose:
201
+ # the goal is to replace references to the same block by references to different copies of the block.
202
+ return self.__class__(blocks)
203
+
204
+ @property
205
+ def stored_data_size(self):
206
+ """Return the number of entries actually stored in memory."""
207
+ size = 0
208
+ for line in self._stored_blocks:
209
+ for block in line:
210
+ if isinstance(block, np.ndarray):
211
+ size += np.prod(block.shape)
212
+ else:
213
+ size += block.stored_data_size
214
+ return size
215
+
216
+ @property
217
+ def density(self):
218
+ return self.stored_data_size/np.prod(self.shape)
219
+
220
+ @property
221
+ def sparcity(self):
222
+ return 1 - self.density
223
+
224
+ def __hash__(self):
225
+ # Temporary
226
+ return id(self)
227
+
228
+ # TRANSFORMING DATA
229
+
230
+ def _apply_unary_op(self, op: Callable) -> 'BlockMatrix':
231
+ """Helper function applying a function recursively on all submatrices."""
232
+ LOG.debug(f"Apply op {op.__name__} to {self}")
233
+ result = [[op(block) for block in line] for line in self._stored_blocks]
234
+ return self.__class__(result, _stored_block_shapes=self._stored_block_shapes, check=False)
235
+
236
+ def _apply_binary_op(self, op: Callable, other: 'BlockMatrix') -> 'BlockMatrix':
237
+ """Helper function applying a binary operator recursively on all submatrices."""
238
+ if isinstance(other, self.__class__) and self.nb_blocks == other.nb_blocks:
239
+ LOG.debug(f"Apply op {op.__name__} to {self} and {other}")
240
+ result = [
241
+ [op(block, other_block) for block, other_block in zip(line, other_line)]
242
+ for line, other_line in zip(self._stored_blocks, other._stored_blocks)
243
+ ]
244
+ return self.__class__(result, _stored_block_shapes=self._stored_block_shapes, check=False)
245
+ else:
246
+ return NotImplemented
247
+
248
+ def __add__(self, other: 'BlockMatrix') -> 'BlockMatrix':
249
+ from operator import add
250
+ return self._apply_binary_op(add, other)
251
+
252
+ def __radd__(self, other: 'BlockMatrix') -> 'BlockMatrix':
253
+ return self + other
254
+
255
+ def __neg__(self) -> 'BlockMatrix':
256
+ from operator import neg
257
+ return self._apply_unary_op(neg)
258
+
259
+ def __sub__(self, other: 'BlockMatrix') -> 'BlockMatrix':
260
+ from operator import sub
261
+ return self._apply_binary_op(sub, other)
262
+
263
+ def __rsub__(self, other: 'BlockMatrix') -> 'BlockMatrix':
264
+ from operator import sub
265
+ return other._apply_binary_op(sub, self)
266
+
267
+ def __mul__(self, other: Union['BlockMatrix', Number]) -> 'BlockMatrix':
268
+ if isinstance(other, Number):
269
+ return self._apply_unary_op(lambda x: other*x)
270
+ else:
271
+ from operator import mul
272
+ return self._apply_binary_op(mul, other)
273
+
274
+ def __rmul__(self, other: Union['BlockMatrix', Number]) -> 'BlockMatrix':
275
+ return self * other
276
+
277
+ def __truediv__(self, other: Union['BlockMatrix', Number]) -> 'BlockMatrix':
278
+ from numbers import Number
279
+ if isinstance(other, Number):
280
+ return self._apply_unary_op(lambda x: x/other)
281
+ else:
282
+ from operator import truediv
283
+ return self._apply_binary_op(truediv, other)
284
+
285
+ def __rtruediv__(self, other: Union['BlockMatrix', Number]) -> 'BlockMatrix':
286
+ from numbers import Number
287
+ if isinstance(other, Number):
288
+ return self._apply_unary_op(lambda x: other/x)
289
+ else:
290
+ return self._apply_binary_op(lambda x, y: y/x, other)
291
+
292
+ def matvec(self, other):
293
+ """Matrix vector product.
294
+ Named as such to be used as scipy LinearOperator."""
295
+ LOG.debug(f"Multiplication of {self} with a full vector of size {other.shape}.")
296
+ result = np.zeros(self.shape[0], dtype=other.dtype)
297
+ line_heights = self.block_shapes[0]
298
+ line_positions = list(accumulate(chain([0], line_heights)))
299
+ col_widths = self.block_shapes[1]
300
+ col_positions = list(accumulate(chain([0], col_widths)))
301
+ for line, line_position, line_height in zip(self.all_blocks, line_positions, line_heights):
302
+ line_slice = slice(line_position, line_position+line_height)
303
+ for block, col_position, col_width in zip(line, col_positions, col_widths):
304
+ col_slice = slice(col_position, col_position+col_width)
305
+ result[line_slice] += block @ other[col_slice]
306
+ return result
307
+
308
+ def rmatvec(self, other):
309
+ """Vector matrix product.
310
+ Named as such to be used as scipy LinearOperator."""
311
+ LOG.debug(f"Multiplication of a full vector of size {other.shape} with {self}.")
312
+ result = np.zeros(self.shape[1], dtype=other.dtype)
313
+ line_heights = self.block_shapes[0]
314
+ line_positions = list(accumulate(chain([0], line_heights)))
315
+ col_widths = self.block_shapes[1]
316
+ col_positions = list(accumulate(chain([0], col_widths)))
317
+ for col, col_position, col_width in zip(self.all_blocks.T, col_positions, col_widths):
318
+ col_slice = slice(col_position, col_position+col_width)
319
+ for block, line_position, line_height in zip(col, line_positions, line_heights):
320
+ line_slice = slice(line_position, line_position+line_height)
321
+ if isinstance(block, BlockMatrix):
322
+ result[col_slice] += block.rmatvec(other[line_slice])
323
+ else:
324
+ result[col_slice] += other[line_slice] @ block
325
+ return result
326
+
327
+ def matmat(self, other):
328
+ """Matrix-matrix product."""
329
+ if isinstance(other, BlockMatrix) and self.block_shapes[1] == other.block_shapes[0]:
330
+ LOG.debug(f"Multiplication of %s with %s", self, other)
331
+ own_blocks = self.all_blocks
332
+ other_blocks = np.moveaxis(other.all_blocks, 1, 0)
333
+ new_matrix = []
334
+ for own_line in own_blocks:
335
+ new_line = []
336
+ for other_col in other_blocks:
337
+ new_line.append(sum(own_block @ other_block for own_block, other_block in zip(own_line, other_col)))
338
+ new_matrix.append(new_line)
339
+ return BlockMatrix(new_matrix, check=False)
340
+
341
+ elif isinstance(other, np.ndarray) and self.shape[1] == other.shape[0]:
342
+ LOG.debug(f"Multiplication of {self} with a full matrix of shape {other.shape}.")
343
+ # Cut the matrix and recursively call itself to use the code above.
344
+ from capytaine.matrices.builders import cut_matrix
345
+ cut_other = cut_matrix(other, self.block_shapes[1], [other.shape[1]], check=False)
346
+ return (self @ cut_other).full_matrix()
347
+
348
+ def __matmul__(self, other: Union['BlockMatrix', np.ndarray]) -> Union['BlockMatrix', np.ndarray]:
349
+ if not (isinstance(other, BlockMatrix) or isinstance(other, np.ndarray)):
350
+ return NotImplemented
351
+ elif other.ndim == 2: # Other is a matrix
352
+ if other.shape[1] == 1: # Actually a column vector
353
+ return self.matvec(other.flatten())
354
+ else:
355
+ return self.matmat(other)
356
+ elif other.ndim == 1: # Other is a vector
357
+ return self.matvec(other)
358
+ else:
359
+ return NotImplemented
360
+
361
+ def __rmatmul__(self, other: Union['BlockMatrix', np.ndarray]) -> Union['BlockMatrix', np.ndarray]:
362
+ if not (isinstance(other, BlockMatrix) or isinstance(other, np.ndarray)):
363
+ return NotImplemented
364
+ elif other.ndim == 2: # Other is a matrix
365
+ if other.shape[1] == 1: # Actually a column vector
366
+ return self.rmatvec(other.flatten())
367
+ else:
368
+ return NotImplemented
369
+ elif other.ndim == 1: # Other is a vector
370
+ return self.rmatvec(other)
371
+ else:
372
+ return NotImplemented
373
+
374
+ def astype(self, dtype: np.dtype) -> 'BlockMatrix':
375
+ return self._apply_unary_op(lambda x: x.astype(dtype))
376
+
377
+ def fft_of_list(*block_matrices, check=True):
378
+ """Compute the fft of a list of block matrices of the same type and shape.
379
+ The output is a list of block matrices of the same shape as the input ones.
380
+ The fft is computed element-wise, so the block structure does not cause any mathematical difficulty.
381
+ Returns an array of BlockMatrices.
382
+ """
383
+ class_of_matrices = type(block_matrices[0])
384
+ nb_blocks = block_matrices[0]._stored_nb_blocks
385
+
386
+ LOG.debug(f"FFT of {len(block_matrices)} {class_of_matrices.__name__} (stored blocks = {nb_blocks})")
387
+
388
+ if check:
389
+ # Check the validity of the shapes of the matrices given as input
390
+ shape = block_matrices[0].shape
391
+ assert [nb_blocks == matrix._stored_nb_blocks for matrix in block_matrices[1:]]
392
+ assert [shape == matrix.shape for matrix in block_matrices[1:]]
393
+ assert [class_of_matrices == type(matrix) for matrix in block_matrices[1:]]
394
+
395
+ # Initialize a vector of block matrices without values in the blocks.
396
+ result = np.empty(len(block_matrices), dtype=object)
397
+ for i in range(len(block_matrices)):
398
+ result[i] = class_of_matrices(np.empty(nb_blocks, dtype=object),
399
+ _stored_block_shapes=block_matrices[0]._stored_block_shapes,
400
+ check=False)
401
+
402
+ for i_block, j_block in product(range(nb_blocks[0]), range(nb_blocks[1])):
403
+ list_of_i_j_blocks = [block_matrices[i_matrix]._stored_blocks[i_block, j_block]
404
+ for i_matrix in range(len(block_matrices))]
405
+
406
+ if any(isinstance(block, np.ndarray) or isinstance(block, LowRankMatrix) for block in list_of_i_j_blocks):
407
+ list_of_i_j_blocks = [block if isinstance(block, np.ndarray) else block.full_matrix() for block in list_of_i_j_blocks]
408
+ fft_of_blocks = np.fft.fft(list_of_i_j_blocks, axis=0)
409
+ else:
410
+ fft_of_blocks = BlockMatrix.fft_of_list(*list_of_i_j_blocks, check=False)
411
+
412
+ for matrix, computed_block in zip(result, fft_of_blocks):
413
+ matrix._stored_blocks[i_block, j_block] = computed_block
414
+
415
+ return result
416
+
417
+ # COMPARISON AND REDUCTION
418
+
419
+ def __eq__(self, other: 'BlockMatrix') -> 'BlockMatrix[bool]':
420
+ from operator import eq
421
+ return self._apply_binary_op(eq, other)
422
+
423
+ def __invert__(self) -> 'BlockMatrix':
424
+ """Boolean not (~)"""
425
+ from operator import invert
426
+ return self._apply_unary_op(invert)
427
+
428
+ def __ne__(self, other: 'BlockMatrix') -> 'BlockMatrix[bool]':
429
+ return ~(self == other)
430
+
431
+ def all(self) -> bool:
432
+ for line in self._stored_blocks:
433
+ for block in line:
434
+ if not block.all():
435
+ return False
436
+ return True
437
+
438
+ def any(self) -> bool:
439
+ for line in self._stored_blocks:
440
+ for block in line:
441
+ if block.any():
442
+ return True
443
+ return False
444
+
445
+ def min(self) -> Number:
446
+ return min(block.min() for line in self._stored_blocks for block in line)
447
+
448
+ def max(self) -> Number:
449
+ return max(block.max() for line in self._stored_blocks for block in line)
450
+
451
+ # DISPLAYING DATA
452
+
453
+ @property
454
+ def str_shape(self):
455
+ blocks_str = []
456
+ for line in self.all_blocks:
457
+ for block in line:
458
+ if isinstance(block, BlockMatrix):
459
+ blocks_str.append(block.str_shape)
460
+ elif isinstance(block, np.ndarray) or isinstance(block, LowRankMatrix):
461
+ blocks_str.append("{}×{}".format(*block.shape))
462
+ else:
463
+ blocks_str.append("?×?")
464
+
465
+ if len(set(blocks_str)) == 1:
466
+ return "{}×{}×[".format(*self.nb_blocks) + blocks_str[0] + "]"
467
+ else:
468
+ blocks_str = np.array(blocks_str).reshape(self.nb_blocks).tolist()
469
+ return str(blocks_str).replace("'", "")
470
+
471
+ def __str__(self):
472
+ if not hasattr(self, '_str'):
473
+ args = [self.str_shape]
474
+ if self.dtype not in [np.float64, float]:
475
+ args.append(f"dtype={self.dtype}")
476
+ self._str = f"{self.__class__.__name__}(" + ", ".join(args) + ")"
477
+ return self._str
478
+
479
+ def _repr_pretty_(self, p, cycle):
480
+ p.text(self.__str__())
481
+
482
+ display_color = cycle([f'C{i}' for i in range(10)])
483
+
484
+ def _patches(self,
485
+ global_frame: Union[Tuple[int, int], np.ndarray]
486
+ ):
487
+ """Helper function for displaying the shape of the matrix.
488
+ Recursively returns a list of rectangles representing the sub-blocks of the matrix.
489
+
490
+ Uses BlockMatrix.display_color to assign color to the blocks.
491
+ By default, it cycles through matplotlib default colors.
492
+ But if display_color is redefined as a callable, it is called with the block as argument.
493
+
494
+ Parameters
495
+ ----------
496
+ global_frame: tuple of ints
497
+ coordinates of the origin in the top left corner.
498
+
499
+ Returns
500
+ -------
501
+ list of matplotlib.patches.Rectangle
502
+ """
503
+ matplotlib_patches = import_optional_dependency("matplotlib.patches", "matplotlib")
504
+ Rectangle = matplotlib_patches.Rectangle
505
+
506
+ all_blocks_in_flat_iterator = (block for line in self._stored_blocks for block in line)
507
+ positions_of_all_blocks = self._stored_block_positions(global_frame=global_frame)
508
+ patches = []
509
+ for block, positions_of_the_block in zip(all_blocks_in_flat_iterator, positions_of_all_blocks):
510
+ position_of_first_appearance = positions_of_the_block[0]
511
+ # Exchange coordinates: row index i -> y, column index j -> x
512
+ position_of_first_appearance = np.array((position_of_first_appearance[1], position_of_first_appearance[0]))
513
+
514
+ if isinstance(block, BlockMatrix):
515
+ patches_of_this_block = block._patches(np.array((position_of_first_appearance[1], position_of_first_appearance[0])))
516
+ elif isinstance(block, np.ndarray):
517
+
518
+ if isinstance(self.display_color, Iterator):
519
+ color = next(self.display_color)
520
+ elif callable(self.display_color):
521
+ color = self.display_color(block)
522
+ else:
523
+ color = np.random.rand(3)
524
+
525
+ patches_of_this_block = [Rectangle(position_of_first_appearance,
526
+ block.shape[1], block.shape[0],
527
+ edgecolor='k', facecolor=color)]
528
+ elif isinstance(block, LowRankMatrix):
529
+
530
+ if isinstance(self.display_color, Iterator):
531
+ color = next(self.display_color)
532
+ elif callable(self.display_color):
533
+ color = self.display_color(block)
534
+ else:
535
+ color = np.random.rand(3)
536
+
537
+ patches_of_this_block = [
538
+ # Left block
539
+ Rectangle(position_of_first_appearance,
540
+ block.left_matrix.shape[1], block.left_matrix.shape[0],
541
+ edgecolor='k', facecolor=color),
542
+ # Top block
543
+ Rectangle(position_of_first_appearance,
544
+ block.right_matrix.shape[1], block.right_matrix.shape[0],
545
+ edgecolor='k', facecolor=color),
546
+ # Rest of the matrix
547
+ Rectangle(position_of_first_appearance,
548
+ block.right_matrix.shape[1], block.left_matrix.shape[0],
549
+ facecolor=color, alpha=0.2),
550
+ ]
551
+ else:
552
+ raise NotImplementedError()
553
+
554
+ patches.extend(patches_of_this_block)
555
+
556
+ # For the other appearances, copy the patches of the first appearance
557
+ for block_position in positions_of_the_block[1:]:
558
+ block_position = np.array((block_position[1], block_position[0]))
559
+ for patch in patches_of_this_block: # A block can be made of several patches.
560
+ shift = block_position - position_of_first_appearance
561
+ patch_position = np.array(patch.get_xy()) + shift
562
+ patches.append(Rectangle(patch_position, patch.get_width(), patch.get_height(),
563
+ facecolor=patch.get_facecolor(), alpha=0.2))
564
+
565
+ return patches
566
+
567
+ def plot_shape(self):
568
+ """Plot the structure of the matrix using matplotlib."""
569
+ matplotlib = import_optional_dependency("matplotlib")
570
+ plt = matplotlib.pyplot
571
+
572
+ plt.figure()
573
+ for patch in self._patches((0, 0)):
574
+ plt.gca().add_patch(patch)
575
+ plt.axis('equal')
576
+ plt.xlim(0, self.shape[1])
577
+ plt.ylim(0, self.shape[0])
578
+ plt.gca().invert_yaxis()
579
+ # plt.show()
580
+
581
+
582
+ def access_block_by_path(self, path):
583
+ """
584
+ Access a diagonal block in a block matrix from the path of the
585
+ corresponding leaf
586
+ """
587
+ this_block = self
588
+ for index in path:
589
+ this_block = this_block.all_blocks[index, index]
590
+ return this_block