PyFinitDiff 2.0.4.post0__py3-none-any.whl → 2.1.3.post0__py3-none-any.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.
@@ -9,23 +9,32 @@ from dataclasses import dataclass
9
9
  @dataclass
10
10
  class MeshInfo:
11
11
  """
12
- This class represents the mesh information for a finite-difference grid.
12
+ Represents the mesh information for a finite-difference grid.
13
13
 
14
- Attributes:
15
- n_x (int): Number of points in the x direction.
16
- n_y (int): Number of points in the y direction.
17
- dx (float): Infinitesimal displacement in the x direction. Defaults to 1.
18
- dy (float): Infinitesimal displacement in the y direction. Defaults to 1.
19
- size (int): Total number of points in the mesh.
20
- shape (Tuple[int, int]): Shape of the mesh as a tuple.
14
+ Attributes
15
+ ----------
16
+ n_x : int
17
+ Number of points in the x direction.
18
+ n_y : int
19
+ Number of points in the y direction.
20
+ dx : float, optional
21
+ Infinitesimal displacement in the x direction. Defaults to 1.
22
+ dy : float, optional
23
+ Infinitesimal displacement in the y direction. Defaults to 1.
24
+ size : int
25
+ Total number of points in the mesh.
26
+ shape : Tuple[int, int]
27
+ Shape of the mesh as a tuple.
21
28
  """
22
29
  n_x: int
23
30
  n_y: int
24
31
  dx: float = 1
25
32
  dy: float = 1
26
33
 
27
- def __post_init__(self):
28
- """Post-initialization to calculate mesh size and shape."""
34
+ def __post_init__(self) -> None:
35
+ """
36
+ Post-initialization to calculate mesh size and shape.
37
+ """
29
38
  self.size = self.n_x * self.n_y
30
39
  self.shape = (self.n_x, self.n_y)
31
40
 
@@ -37,21 +46,35 @@ def get_circular_mesh_triplet(
37
46
  x_offset: float = 0,
38
47
  y_offset: float = 0,
39
48
  value_out: float = 0,
40
- value_in: float = 1) -> 'DiagonalTriplet':
49
+ value_in: float = 1) -> DiagonalTriplet:
41
50
  """
42
- Gets a Triplet corresponding to a 2D mesh with a circular structure of value_in inside and value_out outside.
51
+ Generates a DiagonalTriplet representing a 2D mesh with a circular structure.
43
52
 
44
- Args:
45
- n_x (int): The number of points in the x-axis.
46
- n_y (int): The number of points in the y-axis.
47
- radius (float): The radius of the circular structure.
48
- x_offset (float, optional): The x offset of the circular structure. Defaults to 0.
49
- y_offset (float, optional): The y offset of the circular structure. Defaults to 0.
50
- value_in (float, optional): The value inside the circular structure. Defaults to 1.
51
- value_out (float, optional): The value outside the circular structure. Defaults to 0.
53
+ The mesh is represented with `value_in` inside a circle of a given `radius`,
54
+ and `value_out` outside the circle. The circular structure is centered at
55
+ a specific (x, y) offset.
52
56
 
53
- Returns:
54
- DiagonalTriplet: The 2D circular mesh triplet.
57
+ Parameters
58
+ ----------
59
+ n_x : int
60
+ The number of points in the x-axis.
61
+ n_y : int
62
+ The number of points in the y-axis.
63
+ radius : float
64
+ The radius of the circular structure.
65
+ x_offset : float, optional
66
+ The x offset of the circular structure. Defaults to 0.
67
+ y_offset : float, optional
68
+ The y offset of the circular structure. Defaults to 0.
69
+ value_out : float, optional
70
+ The value outside the circular structure. Defaults to 0.
71
+ value_in : float, optional
72
+ The value inside the circular structure. Defaults to 1.
73
+
74
+ Returns
75
+ -------
76
+ DiagonalTriplet
77
+ The 2D circular mesh triplet.
55
78
  """
56
79
  y, x = numpy.mgrid[
57
80
  -100:100:complex(n_y),
@@ -62,7 +85,4 @@ def get_circular_mesh_triplet(
62
85
  mesh = numpy.ones(x.shape) * value_out
63
86
  mesh[r < radius] = value_in
64
87
 
65
- return DiagonalTriplet(mesh, shape=[n_x, n_y])
66
-
67
-
68
- # -
88
+ return DiagonalTriplet(mesh, shape=(n_x, n_y))
PyFinitDiff/triplet.py CHANGED
@@ -10,6 +10,19 @@ import matplotlib.pyplot as plt
10
10
 
11
11
  @dataclass
12
12
  class Triplet:
13
+ """
14
+ A class representing a sparse matrix in triplet (COO) format, useful for finite-difference operations.
15
+
16
+ Parameters
17
+ ----------
18
+ shape : tuple of int
19
+ Shape of the matrix (rows, columns).
20
+ array : numpy.ndarray
21
+ A 2D array with three columns representing row indices, column indices, and values.
22
+ add_extra_column : bool, optional
23
+ If True, adds an extra column of ones to the array (default is False).
24
+
25
+ """
13
26
  shape: Tuple[int, int]
14
27
  array: numpy.ndarray
15
28
  add_extra_column: bool = False
@@ -30,79 +43,221 @@ class Triplet:
30
43
 
31
44
  @property
32
45
  def index(self) -> numpy.ndarray:
33
- """Return the first two columns of the array, representing the indices."""
46
+ """
47
+ Return the first two columns of the array, representing the indices.
48
+
49
+ Returns
50
+ -------
51
+ numpy.ndarray
52
+ Array of row and column indices.
53
+ """
34
54
  return self.array[:, :2].astype(int)
35
55
 
36
56
  @property
37
57
  def rows(self) -> numpy.ndarray:
38
- """Return the row indices (first column)."""
58
+ """
59
+ Return the row indices (first column).
60
+
61
+ Returns
62
+ -------
63
+ numpy.ndarray
64
+ Array of row indices.
65
+ """
39
66
  return self.array[:, 0].astype(int)
40
67
 
41
68
  @property
42
69
  def columns(self) -> numpy.ndarray:
43
- """Return the column indices (second column)."""
70
+ """
71
+ Return the column indices (second column).
72
+
73
+ Returns
74
+ -------
75
+ numpy.ndarray
76
+ Array of column indices.
77
+ """
44
78
  return self.array[:, 1].astype(int)
45
79
 
46
80
  @property
47
81
  def values(self) -> numpy.ndarray:
48
- """Return the values (third column)."""
82
+ """
83
+ Return the values (third column).
84
+
85
+ Returns
86
+ -------
87
+ numpy.ndarray
88
+ Array of values.
89
+ """
49
90
  return self.array[:, 2]
50
91
 
51
92
  @values.setter
52
93
  def values(self, value: Union[float, numpy.ndarray]) -> None:
53
- """Set the values in the array."""
94
+ """
95
+ Set the values in the array.
96
+
97
+ Parameters
98
+ ----------
99
+ value : float or numpy.ndarray
100
+ The new value(s) to set in the third column of the array.
101
+ """
54
102
  self.array[:, 2] = value
55
103
 
56
104
  @property
57
105
  def size(self) -> int:
58
- """Return the number of entries in the triplet."""
106
+ """
107
+ Return the number of entries in the triplet.
108
+
109
+ Returns
110
+ -------
111
+ int
112
+ Number of entries.
113
+ """
59
114
  return self.array.shape[0]
60
115
 
61
116
  def remove_below_i(self, i_value: int) -> 'Triplet':
62
- """Remove all entries with row indices below i_value."""
117
+ """
118
+ Remove all entries with row indices below a specified value.
119
+
120
+ Parameters
121
+ ----------
122
+ i_value : int
123
+ Threshold for row indices. Entries with row indices below this value are removed.
124
+
125
+ Returns
126
+ -------
127
+ Triplet
128
+ The modified Triplet instance.
129
+ """
63
130
  self.array = self.array[self.rows > i_value]
64
131
  return self
65
132
 
66
133
  def remove_above_i(self, i_value: int) -> 'Triplet':
67
- """Remove all entries with row indices above i_value."""
134
+ """
135
+ Remove all entries with row indices above a specified value.
136
+
137
+ Parameters
138
+ ----------
139
+ i_value : int
140
+ Threshold for row indices. Entries with row indices above this value are removed.
141
+
142
+ Returns
143
+ -------
144
+ Triplet
145
+ The modified Triplet instance.
146
+ """
68
147
  self.array = self.array[self.rows < i_value]
69
148
  return self
70
149
 
71
150
  def remove_below_j(self, j_value: int) -> 'Triplet':
72
- """Remove all entries with column indices below j_value."""
151
+ """
152
+ Remove all entries with column indices below a specified value.
153
+
154
+ Parameters
155
+ ----------
156
+ j_value : int
157
+ Threshold for column indices. Entries with column indices below this value are removed.
158
+
159
+ Returns
160
+ -------
161
+ Triplet
162
+ The modified Triplet instance.
163
+ """
73
164
  self.array = self.array[self.columns > j_value]
74
165
  return self
75
166
 
76
167
  def remove_above_j(self, j_value: int) -> 'Triplet':
77
- """Remove all entries with column indices above j_value."""
168
+ """
169
+ Remove all entries with column indices above a specified value.
170
+
171
+ Parameters
172
+ ----------
173
+ j_value : int
174
+ Threshold for column indices. Entries with column indices above this value are removed.
175
+
176
+ Returns
177
+ -------
178
+ Triplet
179
+ The modified Triplet instance.
180
+ """
78
181
  self.array = self.array[self.columns < j_value]
79
182
  return self
80
183
 
81
184
  def delete(self, index: numpy.ndarray) -> None:
82
- """Delete entries at the given indices."""
185
+ """
186
+ Delete entries at the given indices.
187
+
188
+ Parameters
189
+ ----------
190
+ index : numpy.ndarray
191
+ Indices of entries to delete.
192
+ """
83
193
  self.array = numpy.delete(self.array, index.astype(int), axis=0)
84
194
 
85
195
  def append(self, other: 'Triplet') -> None:
86
- """Append another triplet to this one."""
196
+ """
197
+ Append another triplet to this one.
198
+
199
+ Parameters
200
+ ----------
201
+ other : Triplet
202
+ Another Triplet instance to append.
203
+ """
87
204
  self.array = numpy.r_[self.array, other.array]
88
205
 
89
206
  def append_array(self, array: numpy.ndarray) -> None:
90
- """Append a NumPy array to this triplet."""
207
+ """
208
+ Append a NumPy array to this triplet.
209
+
210
+ Parameters
211
+ ----------
212
+ array : numpy.ndarray
213
+ The array to append to the triplet.
214
+ """
91
215
  self.array = numpy.r_[self.array, array]
92
216
 
93
217
  def __add__(self, other: 'Triplet') -> 'Triplet':
94
- """Concatenate two triplets and merge any duplicate indices."""
218
+ """
219
+ Concatenate two triplets and merge any duplicate indices.
220
+
221
+ Parameters
222
+ ----------
223
+ other : Triplet
224
+ Another Triplet instance to concatenate with this one.
225
+
226
+ Returns
227
+ -------
228
+ Triplet
229
+ A new Triplet instance with merged values for duplicate indices.
230
+ """
95
231
  combined_array = numpy.r_[self.array, other.array]
96
232
  return Triplet(array=combined_array, shape=self.shape).remove_duplicate()
97
233
 
98
234
  def __mul__(self, factor: float) -> 'Triplet':
99
- """Multiply all values in the triplet by a factor."""
235
+ """
236
+ Multiply all values in the triplet by a factor.
237
+
238
+ Parameters
239
+ ----------
240
+ factor : float
241
+ The factor to multiply all values by.
242
+
243
+ Returns
244
+ -------
245
+ Triplet
246
+ A new Triplet instance with values scaled by the factor.
247
+ """
100
248
  return Triplet(array=self.array * [1, 1, factor], shape=self.shape)
101
249
 
102
250
  __rmul__ = __mul__
103
251
 
104
252
  def remove_duplicate(self) -> 'Triplet':
105
- """Remove duplicate entries by summing their values."""
253
+ """
254
+ Remove duplicate entries by summing their values.
255
+
256
+ Returns
257
+ -------
258
+ Triplet
259
+ A new Triplet instance with duplicate entries removed.
260
+ """
106
261
  new_array = self.array
107
262
  index_to_delete = []
108
263
  duplicate = self.get_duplicate_index()
@@ -121,26 +276,68 @@ class Triplet:
121
276
  return Triplet(array=triplet_array, shape=self.shape)
122
277
 
123
278
  def coincide_i(self, mask: 'Triplet') -> None:
124
- """Remove entries whose row indices do not coincide with those in the mask."""
279
+ """
280
+ Remove entries whose row indices do not coincide with those in the mask.
281
+
282
+ Parameters
283
+ ----------
284
+ mask : Triplet
285
+ The mask triplet used to filter rows.
286
+ """
125
287
  mask_i_unique = numpy.unique(mask.rows[mask.values != 0])
126
288
  self.array = numpy.vstack([self.array[self.rows == i] for i in mask_i_unique])
127
289
 
128
290
  def __sub__(self, other: 'Triplet') -> 'Triplet':
129
- """Subtract another triplet by removing matching row indices."""
291
+ """
292
+ Subtract another triplet by removing matching row indices.
293
+
294
+ Parameters
295
+ ----------
296
+ other : Triplet
297
+ Another Triplet instance to subtract.
298
+
299
+ Returns
300
+ -------
301
+ Triplet
302
+ A new Triplet instance with non-matching rows.
303
+ """
130
304
  non_matching_indices = ~numpy.isin(self.rows, other.rows)
131
305
  return Triplet(array=self.array[non_matching_indices], shape=self.shape)
132
306
 
133
307
  def __iter__(self):
134
- """Iterator to yield the indices and values as tuples."""
308
+ """
309
+ Iterator to yield the indices and values as tuples.
310
+
311
+ Yields
312
+ ------
313
+ tuple
314
+ A tuple containing row index, column index, and value.
315
+ """
135
316
  return iter((int(i), int(j), value) for i, j, value in self.array)
136
317
 
137
318
  def enumerate(self, start: int = 0, stop: int = None):
138
- """Enumerate over the entries, yielding an index and the entry values."""
319
+ """
320
+ Enumerate over the entries, yielding an index and the entry values.
321
+
322
+ Parameters
323
+ ----------
324
+ start : int, optional
325
+ Starting index for enumeration (default is 0).
326
+ stop : int, optional
327
+ Stopping index for enumeration (default is None).
328
+
329
+ Yields
330
+ ------
331
+ tuple
332
+ Index and a tuple of row index, column index, and value.
333
+ """
139
334
  for idx, (i, j, value) in enumerate(self.array[start:stop], start=start):
140
335
  yield idx, (int(i), int(j), value)
141
336
 
142
337
  def merge_duplicate(self) -> None:
143
- """Merge duplicate entries by summing their values."""
338
+ """
339
+ Merge duplicate entries by summing their values.
340
+ """
144
341
  duplicates = self.get_duplicate_index()
145
342
  if duplicates.size == 0:
146
343
  return
@@ -150,52 +347,129 @@ class Triplet:
150
347
  self.delete(duplicate[1:])
151
348
 
152
349
  def get_duplicate_index(self) -> numpy.ndarray:
153
- """Get indices of duplicate entries."""
350
+ """
351
+ Get indices of duplicate entries.
352
+
353
+ Returns
354
+ -------
355
+ numpy.ndarray
356
+ Array of duplicate indices.
357
+ """
154
358
  unique_indices, inverse_indices, counts = numpy.unique(self.index, axis=0, return_inverse=True, return_counts=True)
155
359
  duplicate_indices = numpy.where(counts > 1)[0]
156
360
  return numpy.array([numpy.where(inverse_indices == idx)[0] for idx in duplicate_indices], dtype=object)
157
361
 
158
362
  @property
159
363
  def max_i(self) -> int:
160
- """Return the maximum row index."""
364
+ """
365
+ Return the maximum row index.
366
+
367
+ Returns
368
+ -------
369
+ int
370
+ Maximum row index.
371
+ """
161
372
  return self.rows.max()
162
373
 
163
374
  @property
164
375
  def max_j(self) -> int:
165
- """Return the maximum column index."""
376
+ """
377
+ Return the maximum column index.
378
+
379
+ Returns
380
+ -------
381
+ int
382
+ Maximum column index.
383
+ """
166
384
  return self.columns.max()
167
385
 
168
386
  @property
169
387
  def min_i(self) -> int:
170
- """Return the minimum row index."""
388
+ """
389
+ Return the minimum row index.
390
+
391
+ Returns
392
+ -------
393
+ int
394
+ Minimum row index.
395
+ """
171
396
  return self.rows.min()
172
397
 
173
398
  @property
174
399
  def min_j(self) -> int:
175
- """Return the minimum column index."""
400
+ """
401
+ Return the minimum column index.
402
+
403
+ Returns
404
+ -------
405
+ int
406
+ Minimum column index.
407
+ """
176
408
  return self.columns.min()
177
409
 
178
410
  @property
179
411
  def diagonal(self) -> numpy.ndarray:
180
- """Return the diagonal elements of the triplet."""
412
+ """
413
+ Return the diagonal elements of the triplet.
414
+
415
+ Returns
416
+ -------
417
+ numpy.ndarray
418
+ Diagonal elements.
419
+ """
181
420
  return self.array[self.rows == self.columns]
182
421
 
183
422
  def shift_diagonal(self, value: float) -> 'Triplet':
184
- """Shift the diagonal elements by a specified value."""
423
+ """
424
+ Shift the diagonal elements by a specified value.
425
+
426
+ Parameters
427
+ ----------
428
+ value : float
429
+ Value to add to the diagonal elements.
430
+
431
+ Returns
432
+ -------
433
+ Triplet
434
+ A new Triplet instance with the shifted diagonal.
435
+ """
185
436
  diagonal_triplet = DiagonalTriplet(numpy.ones(min(self.max_i, self.max_j)) * value, shape=self.shape)
186
437
  return self + diagonal_triplet
187
438
 
188
439
  def update_elements(self, other_triplet: 'Triplet', i_range: slice) -> 'Triplet':
189
- """Update elements within a specified range."""
440
+ """
441
+ Update elements within a specified range.
442
+
443
+ Parameters
444
+ ----------
445
+ other_triplet : Triplet
446
+ The Triplet instance whose elements will be used for updating.
447
+ i_range : slice
448
+ Range of rows to update.
449
+
450
+ Returns
451
+ -------
452
+ Triplet
453
+ The updated Triplet instance.
454
+ """
190
455
  self.array[i_range, :] = other_triplet.array[i_range, :]
191
456
  return self
192
457
 
193
458
  def to_dense(self) -> numpy.ndarray:
194
- """Convert the triplet to a dense matrix representation."""
459
+ """
460
+ Convert the triplet to a dense matrix representation.
461
+
462
+ Returns
463
+ -------
464
+ numpy.ndarray
465
+ Dense matrix representation of the triplet.
466
+ """
195
467
  return self.to_scipy_sparse().toarray()
196
468
 
197
469
  def plot(self) -> NoReturn:
198
- """Plot the dense matrix representation of the triplet."""
470
+ """
471
+ Plot the dense matrix representation of the triplet.
472
+ """
199
473
  plt.figure()
200
474
  plt.title('Finite-difference coefficients structure')
201
475
  plt.xlabel('Columns')
@@ -209,7 +483,13 @@ class Triplet:
209
483
  plt.show()
210
484
 
211
485
  def to_scipy_sparse(self) -> coo_matrix:
212
- """Convert the triplet to a SciPy sparse matrix."""
486
+ """
487
+ Convert the triplet to a SciPy sparse matrix.
488
+
489
+ Returns
490
+ -------
491
+ coo_matrix: The scipy sparse matrix representation.
492
+ """
213
493
  return coo_matrix((self.values, (self.rows, self.columns)), shape=(numpy.prod(self.shape),) * 2)
214
494
 
215
495
 
@@ -218,4 +498,3 @@ class DiagonalTriplet(Triplet):
218
498
  size = mesh.size
219
499
  triplet_array = numpy.column_stack((numpy.arange(size), numpy.arange(size), mesh.ravel()))
220
500
  super().__init__(array=triplet_array, shape=shape)
221
-