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.
@@ -8,7 +8,6 @@ from typing import Optional, List
8
8
  from pydantic.dataclasses import dataclass
9
9
  from pydantic import ConfigDict
10
10
 
11
-
12
11
  import numpy
13
12
  from PyFinitDiff.triplet import Triplet
14
13
  from PyFinitDiff.finite_difference_2D.utils import MeshInfo
@@ -24,17 +23,22 @@ config_dict = ConfigDict(
24
23
 
25
24
 
26
25
  @dataclass(config=config_dict)
27
- class Diagonal():
26
+ class Diagonal:
28
27
  """
29
- This class represents the diagonal elements of the finite-difference method.
30
- The class can be initialized with different parameters such as its offset or
31
- boundary condition.
32
-
33
- Attributes:
34
- mesh_info (MeshInfo): Instance describing the meta info on the mesh to be considered.
35
- offset (int): Offset of the column index for the diagonal.
36
- values (numpy.ndarray): Values associated with the diagonal.
37
- boundary (Optional[Boundary]): Instance of the boundary used for that diagonal.
28
+ Represents the diagonal elements of the finite-difference method.
29
+
30
+ This class can be initialized with parameters such as its offset or boundary condition.
31
+
32
+ Parameters
33
+ ----------
34
+ mesh_info : MeshInfo
35
+ Instance describing the meta information on the mesh to be considered.
36
+ offset : int
37
+ Offset of the column index for the diagonal.
38
+ values : numpy.ndarray
39
+ Values associated with the diagonal.
40
+ boundary : Optional[PyFinitDiff.finite_difference_2D.boundaries.Boundary]
41
+ Instance of the boundary used for that diagonal.
38
42
  """
39
43
  mesh_info: MeshInfo
40
44
  offset: int
@@ -46,8 +50,10 @@ class Diagonal():
46
50
  """
47
51
  Returns the Triplet instance of the diagonal.
48
52
 
49
- Returns:
50
- Triplet: The Triplet instance containing the array and shape.
53
+ Returns
54
+ -------
55
+ Triplet
56
+ The Triplet instance containing the array and shape.
51
57
  """
52
58
  self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
53
59
 
@@ -60,22 +66,21 @@ class Diagonal():
60
66
 
61
67
  def compute_triplet(self) -> None:
62
68
  """
63
- Computes the diagonal index and generates a Triplet instance out of it.
69
+ Computes the diagonal indices and generates a Triplet instance out of it.
70
+
64
71
  The value of the third triplet column depends on the boundary condition.
65
72
  """
66
73
  self.rows: numpy.ndarray = numpy.arange(0, self.mesh_info.size)
67
74
  self.columns: numpy.ndarray = self.rows + self.offset
68
-
69
75
  self.apply_symmetry()
70
-
71
76
  self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
72
77
 
73
78
  def apply_symmetry(self) -> None:
74
79
  """
75
- Adjusts the values of the diagonal index as defined by the boundary condition.
76
- If boundary is symmetric the value stays the same, if anti-symmetric a minus sign
77
- is added, if zero it returns zero.
80
+ Adjusts the values of the diagonal indices as defined by the boundary condition.
78
81
 
82
+ If the boundary is symmetric, the value stays the same. If anti-symmetric, a minus sign is added.
83
+ If zero, it returns zero.
79
84
  """
80
85
  shift_array = self.boundary.get_shift_vector(self.offset)
81
86
 
@@ -87,24 +92,26 @@ class Diagonal():
87
92
 
88
93
  def validate_index(self) -> None:
89
94
  """
90
- Removes all negative indices.
91
-
95
+ Removes all negative indices from the diagonal.
92
96
  """
93
97
  valid_index = self.columns >= 0
94
-
95
98
  self.columns = self.columns[valid_index]
96
99
  self.rows = self.rows[valid_index]
97
100
  self.values = self.values[valid_index]
98
101
 
99
102
  def plot(self) -> None:
100
103
  """
101
- Plots the Triplet instance.
102
-
104
+ Plots the Triplet instance representing the diagonal.
103
105
  """
104
106
  return self.triplet.plot()
105
107
 
106
108
 
107
109
  class ConstantDiagonal(Diagonal):
110
+ """
111
+ Represents a diagonal with constant values for the finite-difference method.
112
+
113
+ This subclass of Diagonal is initialized with constant values for the entire diagonal.
114
+ """
108
115
  def __init__(self, offset: int, value: float, mesh_info: list, boundary: Boundary):
109
116
  super().__init__(
110
117
  offset=offset,
@@ -115,14 +122,18 @@ class ConstantDiagonal(Diagonal):
115
122
 
116
123
 
117
124
  @dataclass(config=config_dict)
118
- class DiagonalSet():
125
+ class DiagonalSet:
119
126
  """
120
- This class represents a set of diagonals for the finite-difference method. It provides
121
- various utilities for manipulating and analyzing the set of diagonals.
127
+ Represents a set of diagonals for the finite-difference method.
128
+
129
+ This class provides various utilities for manipulating and analyzing the set of diagonals.
122
130
 
123
- Attributes:
124
- mesh_info (object): Meta information about the mesh.
125
- diagonals (List[Diagonal]): List of Diagonal objects.
131
+ Parameters
132
+ ----------
133
+ mesh_info : MeshInfo
134
+ Meta information about the mesh.
135
+ diagonals : List[Diagonal]
136
+ List of Diagonal objects.
126
137
  """
127
138
  mesh_info: MeshInfo
128
139
  diagonals: List[Diagonal] = field(default_factory=list)
@@ -131,11 +142,15 @@ class DiagonalSet():
131
142
  """
132
143
  Appends a Diagonal to the set.
133
144
 
134
- Args:
135
- diagonal (Diagonal): The diagonal to append.
145
+ Parameters
146
+ ----------
147
+ diagonal : Diagonal
148
+ The diagonal to append.
136
149
 
137
- Returns:
138
- DiagonalSet: The updated DiagonalSet.
150
+ Returns
151
+ -------
152
+ DiagonalSet
153
+ The updated DiagonalSet.
139
154
  """
140
155
  self.diagonals.append(diagonal)
141
156
  return self
@@ -144,11 +159,15 @@ class DiagonalSet():
144
159
  """
145
160
  Concatenates another DiagonalSet to this one.
146
161
 
147
- Args:
148
- other_diagonal_set (DiagonalSet): The other DiagonalSet to concatenate.
162
+ Parameters
163
+ ----------
164
+ other_diagonal_set : DiagonalSet
165
+ The other DiagonalSet to concatenate.
149
166
 
150
- Returns:
151
- DiagonalSet: The updated DiagonalSet.
167
+ Returns
168
+ -------
169
+ DiagonalSet
170
+ The updated DiagonalSet.
152
171
  """
153
172
  self.diagonals += other_diagonal_set.diagonals
154
173
  return self
@@ -157,8 +176,10 @@ class DiagonalSet():
157
176
  """
158
177
  Returns a boolean array with True where the associated rows have a NaN value.
159
178
 
160
- Returns:
161
- numpy.ndarray: Boolean array indicating NaN rows.
179
+ Returns
180
+ -------
181
+ numpy.ndarray
182
+ Boolean array indicating NaN rows.
162
183
  """
163
184
  nan_index = self.get_nan_index()
164
185
  nan_rows = self.triplet.rows[nan_index]
@@ -169,8 +190,10 @@ class DiagonalSet():
169
190
  """
170
191
  Gets a list of rows containing NaN values.
171
192
 
172
- Returns:
173
- numpy.ndarray: Array of rows with NaN values.
193
+ Returns
194
+ -------
195
+ numpy.ndarray
196
+ Array of rows with NaN values.
174
197
  """
175
198
  nan_index = numpy.isnan(self.triplet.values)
176
199
  nan_rows = self.triplet.rows[nan_index]
@@ -180,8 +203,10 @@ class DiagonalSet():
180
203
  """
181
204
  Gets a list of rows without NaN values.
182
205
 
183
- Returns:
184
- numpy.ndarray: Array of rows without NaN values.
206
+ Returns
207
+ -------
208
+ numpy.ndarray
209
+ Array of rows without NaN values.
185
210
  """
186
211
  nan_rows = self.get_list_of_nan_rows()
187
212
  non_nan_index = ~numpy.isin(self.triplet.rows, nan_rows)
@@ -192,8 +217,10 @@ class DiagonalSet():
192
217
  """
193
218
  Gets a boolean array indicating the indices of NaN values in the triplet.
194
219
 
195
- Returns:
196
- numpy.ndarray: Boolean array indicating NaN indices.
220
+ Returns
221
+ -------
222
+ numpy.ndarray
223
+ Boolean array indicating NaN indices.
197
224
  """
198
225
  return numpy.isnan(self.triplet.values)
199
226
 
@@ -201,11 +228,15 @@ class DiagonalSet():
201
228
  """
202
229
  Returns the array elements for the specified rows.
203
230
 
204
- Args:
205
- rows (numpy.ndarray): The rows to search for.
231
+ Parameters
232
+ ----------
233
+ rows : numpy.ndarray
234
+ The rows to search for.
206
235
 
207
- Returns:
208
- numpy.ndarray: The array elements corresponding to the specified rows.
236
+ Returns
237
+ -------
238
+ numpy.ndarray
239
+ The array elements corresponding to the specified rows.
209
240
  """
210
241
  rows_index = numpy.isin(self.triplet.rows, rows)
211
242
  return self.triplet.array[rows_index]
@@ -214,8 +245,10 @@ class DiagonalSet():
214
245
  """
215
246
  Removes rows containing NaN values from the triplet.
216
247
 
217
- Returns:
218
- DiagonalSet: The updated DiagonalSet with NaN rows removed.
248
+ Returns
249
+ -------
250
+ DiagonalSet
251
+ The updated DiagonalSet with NaN rows removed.
219
252
  """
220
253
  nan_rows = self.get_row_nan_bool()
221
254
  return self.remove_rows(rows=nan_rows)
@@ -224,11 +257,15 @@ class DiagonalSet():
224
257
  """
225
258
  Removes the specified rows from the triplet.
226
259
 
227
- Args:
228
- rows (numpy.ndarray): The rows to remove.
260
+ Parameters
261
+ ----------
262
+ rows : numpy.ndarray
263
+ The rows to remove.
229
264
 
230
- Returns:
231
- DiagonalSet: The updated DiagonalSet with the specified rows removed.
265
+ Returns
266
+ -------
267
+ DiagonalSet
268
+ The updated DiagonalSet with the specified rows removed.
232
269
  """
233
270
  index_to_remove = numpy.isin(self.triplet.rows, rows)
234
271
  self.triplet.array = numpy.delete(self.triplet.array, index_to_remove, axis=0)
@@ -238,11 +275,15 @@ class DiagonalSet():
238
275
  """
239
276
  Replaces NaN rows in this DiagonalSet with the equivalent rows from another DiagonalSet.
240
277
 
241
- Args:
242
- other (DiagonalSet): The other DiagonalSet to source non-NaN rows from.
278
+ Parameters
279
+ ----------
280
+ other : DiagonalSet
281
+ The other DiagonalSet to source non-NaN rows from.
243
282
 
244
- Returns:
245
- DiagonalSet: The updated DiagonalSet with NaN rows replaced.
283
+ Returns
284
+ -------
285
+ DiagonalSet
286
+ The updated DiagonalSet with NaN rows replaced.
246
287
  """
247
288
  self_nan_rows = self.get_list_of_nan_rows()
248
289
  other_not_nan_rows = other.get_list_of_not_nan_rows()
@@ -256,8 +297,10 @@ class DiagonalSet():
256
297
  """
257
298
  Initializes the triplet by combining all diagonals in the set.
258
299
 
259
- Returns:
260
- DiagonalSet: The initialized DiagonalSet.
300
+ Returns
301
+ -------
302
+ DiagonalSet
303
+ The initialized DiagonalSet.
261
304
  """
262
305
  triplet = Triplet(
263
306
  array=numpy.array([0, 0, 0]),
@@ -273,8 +316,10 @@ class DiagonalSet():
273
316
  """
274
317
  Gets the lowest row index containing a NaN value.
275
318
 
276
- Returns:
277
- int: The lowest row index with a NaN value.
319
+ Returns
320
+ -------
321
+ int
322
+ The lowest row index with a NaN value.
278
323
  """
279
324
  nan_index = self.get_nan_index()
280
325
  rows = self.triplet.rows[nan_index]
@@ -284,8 +329,10 @@ class DiagonalSet():
284
329
  """
285
330
  Gets the highest row index containing a NaN value.
286
331
 
287
- Returns:
288
- int: The highest row index with a NaN value.
332
+ Returns
333
+ -------
334
+ int
335
+ The highest row index with a NaN value.
289
336
  """
290
337
  nan_index = self.get_nan_index()
291
338
  rows = self.triplet.rows[nan_index]
@@ -295,21 +342,21 @@ class DiagonalSet():
295
342
  """
296
343
  Adds another DiagonalSet to this one.
297
344
 
298
- Args:
299
- other (DiagonalSet): The other DiagonalSet to add.
345
+ Parameters
346
+ ----------
347
+ other : DiagonalSet
348
+ The other DiagonalSet to add.
300
349
 
301
- Returns:
302
- DiagonalSet: The updated DiagonalSet.
350
+ Returns
351
+ -------
352
+ DiagonalSet
353
+ The updated DiagonalSet.
303
354
  """
304
355
  self.diagonals += other.diagonals
305
356
  return self
306
357
 
307
358
  def plot(self) -> None:
308
359
  """
309
- Plots the Triplet instance.
310
-
311
- Returns:
312
- None
360
+ Plots the Triplet instance representing the entire DiagonalSet.
313
361
  """
314
362
  return self.triplet.plot()
315
- # -
@@ -4,14 +4,12 @@
4
4
  from pydantic.dataclasses import dataclass
5
5
  from pydantic import ConfigDict
6
6
  from dataclasses import field
7
-
8
7
  from PyFinitDiff.coefficients import FiniteCoefficients
9
8
  from PyFinitDiff.triplet import Triplet
10
9
  from PyFinitDiff.finite_difference_2D.boundaries import Boundaries
11
10
  from PyFinitDiff.finite_difference_2D.utils import MeshInfo
12
11
  from PyFinitDiff.finite_difference_2D.diagonals import DiagonalSet, ConstantDiagonal
13
12
 
14
-
15
13
  config_dict = ConfigDict(
16
14
  extra='forbid',
17
15
  strict=True,
@@ -22,23 +20,42 @@ config_dict = ConfigDict(
22
20
 
23
21
 
24
22
  @dataclass(config=config_dict)
25
- class FiniteDifference():
23
+ class FiniteDifference:
26
24
  """
27
- This class represents a specific finite difference configuration, which is defined
28
- with the discretization of the mesh, the derivative order, accuracy, and the boundary
29
- conditions that are defined. More information is provided at the following link:
30
- 'math.toronto.edu/mpugh/Teaching/Mat1062/notes2.pdf'
31
-
32
- Attributes:
33
- n_x (int): Number of points in the x direction.
34
- n_y (int): Number of points in the y direction.
35
- dx (float): Infinitesimal displacement in the x direction. Defaults to 1.
36
- dy (float): Infinitesimal displacement in the y direction. Defaults to 1.
37
- derivative (int): Derivative order to convert into finite-difference matrix. Defaults to 1.
38
- accuracy (int): Accuracy of the derivative approximation (error is inversely proportional to the power of this value). Defaults to 2.
39
- boundaries (Boundaries): Values of the four possible boundaries of the system.
40
- x_derivative (bool): Add the x derivative. Defaults to True.
41
- y_derivative (bool): Add the y derivative. Defaults to True.
25
+ Represents a specific finite difference configuration, defined by the discretization
26
+ of the mesh, derivative order, accuracy, and boundary conditions.
27
+
28
+ Parameters
29
+ ----------
30
+ n_x : int
31
+ Number of points in the x direction.
32
+ n_y : int
33
+ Number of points in the y direction.
34
+ dx : float, optional
35
+ Infinitesimal displacement in the x direction (default is 1).
36
+ dy : float, optional
37
+ Infinitesimal displacement in the y direction (default is 1).
38
+ derivative : int, optional
39
+ Order of the derivative to convert into a finite-difference matrix (default is 1).
40
+ accuracy : int, optional
41
+ Accuracy of the derivative approximation (error is inversely proportional to the power of this value, default is 2).
42
+ boundaries : PyFinitDiff.finite_difference_2D.boundaries.Boundary, optional
43
+ Values of the four possible boundaries of the system (default is an empty Boundaries object).
44
+ x_derivative : bool, optional
45
+ Whether to add the x derivative (default is True).
46
+ y_derivative : bool, optional
47
+ Whether to add the y derivative (default is True).
48
+
49
+ Attributes
50
+ ----------
51
+ mesh_info : MeshInfo
52
+ Contains information about the mesh, such as the number of points and spacing.
53
+ boundaries : PyFinitDiff.finite_difference_2D.boundaries.Boundary
54
+ Boundary conditions applied to the system.
55
+ finite_coefficient : FiniteCoefficients
56
+ Coefficients used for finite difference calculations.
57
+ _triplet : Triplet or None
58
+ The triplet representation of the finite-difference matrix, initialized to None.
42
59
  """
43
60
  n_x: int
44
61
  n_y: int
@@ -51,7 +68,9 @@ class FiniteDifference():
51
68
  y_derivative: bool = True
52
69
 
53
70
  def __post_init__(self):
54
- """Post-initialization to set up mesh info and finite coefficients."""
71
+ """
72
+ Post-initialization to set up mesh info and finite coefficients.
73
+ """
55
74
  self.mesh_info = MeshInfo(
56
75
  n_x=self.n_x,
57
76
  n_y=self.n_y,
@@ -68,17 +87,26 @@ class FiniteDifference():
68
87
 
69
88
  @property
70
89
  def shape(self):
71
- """Returns the shape of the mesh as a tuple."""
90
+ """
91
+ Returns the shape of the mesh as a tuple.
92
+
93
+ Returns
94
+ -------
95
+ tuple
96
+ The shape of the system, represented by the number of points in the x and y directions.
97
+ """
72
98
  return (self.n_x, self.n_y)
73
99
 
74
100
  @property
75
101
  def triplet(self):
76
102
  """
77
- Triplet representing the non-null values of the specific
78
- finite-difference configuration.
103
+ Returns the triplet representation of the non-null values of the finite-difference configuration.
104
+ Constructs the triplet if it has not been initialized.
79
105
 
80
- Returns:
81
- Triplet: The Triplet instance containing the array and shape.
106
+ Returns
107
+ -------
108
+ Triplet
109
+ The Triplet instance containing the array and shape.
82
110
  """
83
111
  if not self._triplet:
84
112
  self.construct_triplet()
@@ -86,48 +114,66 @@ class FiniteDifference():
86
114
 
87
115
  @property
88
116
  def _dx(self) -> float:
89
- """Returns the dx value raised to the power of the derivative order."""
117
+ """
118
+ Returns the scaled infinitesimal displacement for the x direction.
119
+
120
+ Returns
121
+ -------
122
+ float
123
+ The displacement scaled by the derivative order.
124
+ """
90
125
  return self.dx ** self.derivative
91
126
 
92
127
  @property
93
128
  def _dy(self) -> float:
94
- """Returns the dy value raised to the power of the derivative order."""
95
- return self.dy ** self.derivative
96
-
97
- def iterate_central_coefficient(
98
- self,
99
- coefficients: str,
100
- offset_multiplier: int) -> tuple:
101
129
  """
102
- Iterate through the given type coefficients.
130
+ Returns the scaled infinitesimal displacement for the y direction.
103
131
 
104
- Args:
105
- coefficients (str): The coefficient type.
106
- offset_multiplier (int): The offset multiplier.
132
+ Returns
133
+ -------
134
+ float
135
+ The displacement scaled by the derivative order.
136
+ """
137
+ return self.dy ** self.derivative
107
138
 
108
- Yields:
109
- tuple: The offset, value, and boundary.
139
+ def iterate_central_coefficient(self, coefficients: str, offset_multiplier: int):
140
+ """
141
+ Iterates through the given type of coefficients to provide the offset, value, and boundary type.
142
+
143
+ Parameters
144
+ ----------
145
+ coefficients : str
146
+ The type of coefficients to iterate over.
147
+ offset_multiplier : int
148
+ Multiplier applied to the coefficient offset.
149
+
150
+ Yields
151
+ ------
152
+ tuple
153
+ A tuple containing the offset, value, and corresponding boundary type.
110
154
  """
111
155
  for offset, value in coefficients:
112
156
  offset *= offset_multiplier
113
157
  boundary = self.boundaries.offset_to_boundary(offset=offset)
114
158
  yield offset, value, boundary
115
159
 
116
- def _add_diagonal_coefficient(
117
- self,
118
- coefficient_type: str,
119
- offset_multiplier: int,
120
- delta: float) -> 'DiagonalSet':
160
+ def _add_diagonal_coefficient(self, coefficient_type: str, offset_multiplier: int, delta: float) -> 'DiagonalSet':
121
161
  """
122
162
  Adds a diagonal coefficient to the list of diagonals.
123
163
 
124
- Args:
125
- coefficient_type (str): The coefficient type.
126
- offset_multiplier (int): The offset multiplier.
127
- delta (float): The delta value.
128
-
129
- Returns:
130
- DiagonalSet: The diagonal set with added coefficients.
164
+ Parameters
165
+ ----------
166
+ coefficient_type : str
167
+ The type of coefficients to add (e.g., 'central', 'forward', 'backward').
168
+ offset_multiplier : int
169
+ Multiplier applied to the offset for each coefficient.
170
+ delta : float
171
+ Scaling factor for the coefficient values.
172
+
173
+ Returns
174
+ -------
175
+ DiagonalSet
176
+ A set of diagonals representing the finite-difference configuration.
131
177
  """
132
178
  diagonal_set = DiagonalSet(mesh_info=self.mesh_info)
133
179
  coefficients = getattr(self.finite_coefficient, coefficient_type)
@@ -151,15 +197,19 @@ class FiniteDifference():
151
197
 
152
198
  def get_diagonal_set_full(self, offset_multiplier: int, delta: float) -> 'DiagonalSet':
153
199
  """
154
- Constructs and returns the central coefficients diagonals which is completed with
155
- forward and backward coefficients if some 'nan' values are left.
156
-
157
- Args:
158
- offset_multiplier (int): The offset multiplier.
159
- delta (float): The delta value.
160
-
161
- Returns:
162
- DiagonalSet: The full set of diagonal coefficients.
200
+ Constructs and returns a complete set of diagonals, including central, forward, and backward coefficients.
201
+
202
+ Parameters
203
+ ----------
204
+ offset_multiplier : int
205
+ Multiplier applied to the coefficient offset.
206
+ delta : float
207
+ Scaling factor for the coefficient values.
208
+
209
+ Returns
210
+ -------
211
+ DiagonalSet
212
+ A set of diagonals representing the finite-difference configuration, including adjustments for boundaries.
163
213
  """
164
214
  central_diagonal = self._add_diagonal_coefficient(
165
215
  coefficient_type='central',
@@ -185,7 +235,9 @@ class FiniteDifference():
185
235
  return central_diagonal
186
236
 
187
237
  def construct_triplet(self) -> None:
188
- """Constructs the Triplet instance for the finite-difference configuration."""
238
+ """
239
+ Constructs the Triplet instance for the finite-difference configuration.
240
+ """
189
241
  self._triplet = Triplet(array=[[0, 0, 0]], shape=self.mesh_info.shape)
190
242
 
191
243
  if self.x_derivative:
@@ -201,6 +253,3 @@ class FiniteDifference():
201
253
  delta=self._dy
202
254
  )
203
255
  self._triplet += y_diagonals.triplet
204
-
205
-
206
- # -