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