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.
@@ -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
@@ -22,19 +21,23 @@ config_dict = ConfigDict(
22
21
  arbitrary_types_allowed=True
23
22
  )
24
23
 
25
-
26
24
  @dataclass(config=config_dict)
27
- class Diagonal():
25
+ class Diagonal:
28
26
  """
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.
27
+ Represents the diagonal elements of the finite-difference method.
28
+
29
+ This class can be initialized with parameters such as its offset or boundary condition.
30
+
31
+ Parameters
32
+ ----------
33
+ mesh_info : MeshInfo
34
+ Instance describing the meta information on the mesh to be considered.
35
+ offset : int
36
+ Offset of the column index for the diagonal.
37
+ values : numpy.ndarray
38
+ Values associated with the diagonal.
39
+ boundary : Optional[PyFinitDiff.finite_difference_2D.boundaries.Boundary]
40
+ Instance of the boundary used for that diagonal.
38
41
  """
39
42
  mesh_info: MeshInfo
40
43
  offset: int
@@ -46,8 +49,10 @@ class Diagonal():
46
49
  """
47
50
  Returns the Triplet instance of the diagonal.
48
51
 
49
- Returns:
50
- Triplet: The Triplet instance containing the array and shape.
52
+ Returns
53
+ -------
54
+ Triplet
55
+ The Triplet instance containing the array and shape.
51
56
  """
52
57
  self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
53
58
 
@@ -60,22 +65,21 @@ class Diagonal():
60
65
 
61
66
  def compute_triplet(self) -> None:
62
67
  """
63
- Computes the diagonal index and generates a Triplet instance out of it.
68
+ Computes the diagonal indices and generates a Triplet instance out of it.
69
+
64
70
  The value of the third triplet column depends on the boundary condition.
65
71
  """
66
72
  self.rows: numpy.ndarray = numpy.arange(0, self.mesh_info.size)
67
73
  self.columns: numpy.ndarray = self.rows + self.offset
68
-
69
74
  self.apply_symmetry()
70
-
71
75
  self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
72
76
 
73
77
  def apply_symmetry(self) -> None:
74
78
  """
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.
79
+ Adjusts the values of the diagonal indices as defined by the boundary condition.
78
80
 
81
+ If the boundary is symmetric, the value stays the same. If anti-symmetric, a minus sign is added.
82
+ If zero, it returns zero.
79
83
  """
80
84
  shift_array = self.boundary.get_shift_vector(self.offset)
81
85
 
@@ -87,24 +91,25 @@ class Diagonal():
87
91
 
88
92
  def validate_index(self) -> None:
89
93
  """
90
- Removes all negative indices.
91
-
94
+ Removes all negative indices from the diagonal.
92
95
  """
93
96
  valid_index = self.columns >= 0
94
-
95
97
  self.columns = self.columns[valid_index]
96
98
  self.rows = self.rows[valid_index]
97
99
  self.values = self.values[valid_index]
98
100
 
99
101
  def plot(self) -> None:
100
102
  """
101
- Plots the Triplet instance.
102
-
103
+ Plots the Triplet instance representing the diagonal.
103
104
  """
104
105
  return self.triplet.plot()
105
106
 
106
-
107
107
  class ConstantDiagonal(Diagonal):
108
+ """
109
+ Represents a diagonal with constant values for the finite-difference method.
110
+
111
+ This subclass of Diagonal is initialized with constant values for the entire diagonal.
112
+ """
108
113
  def __init__(self, offset: int, value: float, mesh_info: list, boundary: Boundary):
109
114
  super().__init__(
110
115
  offset=offset,
@@ -113,16 +118,19 @@ class ConstantDiagonal(Diagonal):
113
118
  boundary=boundary,
114
119
  )
115
120
 
116
-
117
121
  @dataclass(config=config_dict)
118
- class DiagonalSet():
122
+ class DiagonalSet:
119
123
  """
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.
124
+ Represents a set of diagonals for the finite-difference method.
125
+
126
+ This class provides various utilities for manipulating and analyzing the set of diagonals.
122
127
 
123
- Attributes:
124
- mesh_info (object): Meta information about the mesh.
125
- diagonals (List[Diagonal]): List of Diagonal objects.
128
+ Parameters
129
+ ----------
130
+ mesh_info : MeshInfo
131
+ Meta information about the mesh.
132
+ diagonals : List[Diagonal]
133
+ List of Diagonal objects.
126
134
  """
127
135
  mesh_info: MeshInfo
128
136
  diagonals: List[Diagonal] = field(default_factory=list)
@@ -131,11 +139,15 @@ class DiagonalSet():
131
139
  """
132
140
  Appends a Diagonal to the set.
133
141
 
134
- Args:
135
- diagonal (Diagonal): The diagonal to append.
142
+ Parameters
143
+ ----------
144
+ diagonal : Diagonal
145
+ The diagonal to append.
136
146
 
137
- Returns:
138
- DiagonalSet: The updated DiagonalSet.
147
+ Returns
148
+ -------
149
+ DiagonalSet
150
+ The updated DiagonalSet.
139
151
  """
140
152
  self.diagonals.append(diagonal)
141
153
  return self
@@ -144,11 +156,15 @@ class DiagonalSet():
144
156
  """
145
157
  Concatenates another DiagonalSet to this one.
146
158
 
147
- Args:
148
- other_diagonal_set (DiagonalSet): The other DiagonalSet to concatenate.
159
+ Parameters
160
+ ----------
161
+ other_diagonal_set : DiagonalSet
162
+ The other DiagonalSet to concatenate.
149
163
 
150
- Returns:
151
- DiagonalSet: The updated DiagonalSet.
164
+ Returns
165
+ -------
166
+ DiagonalSet
167
+ The updated DiagonalSet.
152
168
  """
153
169
  self.diagonals += other_diagonal_set.diagonals
154
170
  return self
@@ -157,8 +173,10 @@ class DiagonalSet():
157
173
  """
158
174
  Returns a boolean array with True where the associated rows have a NaN value.
159
175
 
160
- Returns:
161
- numpy.ndarray: Boolean array indicating NaN rows.
176
+ Returns
177
+ -------
178
+ numpy.ndarray
179
+ Boolean array indicating NaN rows.
162
180
  """
163
181
  nan_index = self.get_nan_index()
164
182
  nan_rows = self.triplet.rows[nan_index]
@@ -169,8 +187,10 @@ class DiagonalSet():
169
187
  """
170
188
  Gets a list of rows containing NaN values.
171
189
 
172
- Returns:
173
- numpy.ndarray: Array of rows with NaN values.
190
+ Returns
191
+ -------
192
+ numpy.ndarray
193
+ Array of rows with NaN values.
174
194
  """
175
195
  nan_index = numpy.isnan(self.triplet.values)
176
196
  nan_rows = self.triplet.rows[nan_index]
@@ -180,8 +200,10 @@ class DiagonalSet():
180
200
  """
181
201
  Gets a list of rows without NaN values.
182
202
 
183
- Returns:
184
- numpy.ndarray: Array of rows without NaN values.
203
+ Returns
204
+ -------
205
+ numpy.ndarray
206
+ Array of rows without NaN values.
185
207
  """
186
208
  nan_rows = self.get_list_of_nan_rows()
187
209
  non_nan_index = ~numpy.isin(self.triplet.rows, nan_rows)
@@ -192,8 +214,10 @@ class DiagonalSet():
192
214
  """
193
215
  Gets a boolean array indicating the indices of NaN values in the triplet.
194
216
 
195
- Returns:
196
- numpy.ndarray: Boolean array indicating NaN indices.
217
+ Returns
218
+ -------
219
+ numpy.ndarray
220
+ Boolean array indicating NaN indices.
197
221
  """
198
222
  return numpy.isnan(self.triplet.values)
199
223
 
@@ -201,11 +225,15 @@ class DiagonalSet():
201
225
  """
202
226
  Returns the array elements for the specified rows.
203
227
 
204
- Args:
205
- rows (numpy.ndarray): The rows to search for.
228
+ Parameters
229
+ ----------
230
+ rows : numpy.ndarray
231
+ The rows to search for.
206
232
 
207
- Returns:
208
- numpy.ndarray: The array elements corresponding to the specified rows.
233
+ Returns
234
+ -------
235
+ numpy.ndarray
236
+ The array elements corresponding to the specified rows.
209
237
  """
210
238
  rows_index = numpy.isin(self.triplet.rows, rows)
211
239
  return self.triplet.array[rows_index]
@@ -214,8 +242,10 @@ class DiagonalSet():
214
242
  """
215
243
  Removes rows containing NaN values from the triplet.
216
244
 
217
- Returns:
218
- DiagonalSet: The updated DiagonalSet with NaN rows removed.
245
+ Returns
246
+ -------
247
+ DiagonalSet
248
+ The updated DiagonalSet with NaN rows removed.
219
249
  """
220
250
  nan_rows = self.get_row_nan_bool()
221
251
  return self.remove_rows(rows=nan_rows)
@@ -224,11 +254,15 @@ class DiagonalSet():
224
254
  """
225
255
  Removes the specified rows from the triplet.
226
256
 
227
- Args:
228
- rows (numpy.ndarray): The rows to remove.
257
+ Parameters
258
+ ----------
259
+ rows : numpy.ndarray
260
+ The rows to remove.
229
261
 
230
- Returns:
231
- DiagonalSet: The updated DiagonalSet with the specified rows removed.
262
+ Returns
263
+ -------
264
+ DiagonalSet
265
+ The updated DiagonalSet with the specified rows removed.
232
266
  """
233
267
  index_to_remove = numpy.isin(self.triplet.rows, rows)
234
268
  self.triplet.array = numpy.delete(self.triplet.array, index_to_remove, axis=0)
@@ -238,11 +272,15 @@ class DiagonalSet():
238
272
  """
239
273
  Replaces NaN rows in this DiagonalSet with the equivalent rows from another DiagonalSet.
240
274
 
241
- Args:
242
- other (DiagonalSet): The other DiagonalSet to source non-NaN rows from.
275
+ Parameters
276
+ ----------
277
+ other : DiagonalSet
278
+ The other DiagonalSet to source non-NaN rows from.
243
279
 
244
- Returns:
245
- DiagonalSet: The updated DiagonalSet with NaN rows replaced.
280
+ Returns
281
+ -------
282
+ DiagonalSet
283
+ The updated DiagonalSet with NaN rows replaced.
246
284
  """
247
285
  self_nan_rows = self.get_list_of_nan_rows()
248
286
  other_not_nan_rows = other.get_list_of_not_nan_rows()
@@ -256,8 +294,10 @@ class DiagonalSet():
256
294
  """
257
295
  Initializes the triplet by combining all diagonals in the set.
258
296
 
259
- Returns:
260
- DiagonalSet: The initialized DiagonalSet.
297
+ Returns
298
+ -------
299
+ DiagonalSet
300
+ The initialized DiagonalSet.
261
301
  """
262
302
  triplet = Triplet(
263
303
  array=numpy.array([0, 0, 0]),
@@ -273,8 +313,10 @@ class DiagonalSet():
273
313
  """
274
314
  Gets the lowest row index containing a NaN value.
275
315
 
276
- Returns:
277
- int: The lowest row index with a NaN value.
316
+ Returns
317
+ -------
318
+ int
319
+ The lowest row index with a NaN value.
278
320
  """
279
321
  nan_index = self.get_nan_index()
280
322
  rows = self.triplet.rows[nan_index]
@@ -284,8 +326,10 @@ class DiagonalSet():
284
326
  """
285
327
  Gets the highest row index containing a NaN value.
286
328
 
287
- Returns:
288
- int: The highest row index with a NaN value.
329
+ Returns
330
+ -------
331
+ int
332
+ The highest row index with a NaN value.
289
333
  """
290
334
  nan_index = self.get_nan_index()
291
335
  rows = self.triplet.rows[nan_index]
@@ -295,21 +339,21 @@ class DiagonalSet():
295
339
  """
296
340
  Adds another DiagonalSet to this one.
297
341
 
298
- Args:
299
- other (DiagonalSet): The other DiagonalSet to add.
342
+ Parameters
343
+ ----------
344
+ other : DiagonalSet
345
+ The other DiagonalSet to add.
300
346
 
301
- Returns:
302
- DiagonalSet: The updated DiagonalSet.
347
+ Returns
348
+ -------
349
+ DiagonalSet
350
+ The updated DiagonalSet.
303
351
  """
304
352
  self.diagonals += other.diagonals
305
353
  return self
306
354
 
307
355
  def plot(self) -> None:
308
356
  """
309
- Plots the Triplet instance.
310
-
311
- Returns:
312
- None
357
+ Plots the Triplet instance representing the entire DiagonalSet.
313
358
  """
314
359
  return self.triplet.plot()
315
- # -
@@ -11,7 +11,6 @@ from PyFinitDiff.finite_difference_2D.boundaries import Boundaries
11
11
  from PyFinitDiff.finite_difference_2D.utils import MeshInfo
12
12
  from PyFinitDiff.finite_difference_2D.diagonals import DiagonalSet, ConstantDiagonal
13
13
 
14
-
15
14
  config_dict = ConfigDict(
16
15
  extra='forbid',
17
16
  strict=True,
@@ -20,25 +19,43 @@ config_dict = ConfigDict(
20
19
  frozen=False
21
20
  )
22
21
 
23
-
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) -> tuple:
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:
@@ -202,5 +254,3 @@ class FiniteDifference():
202
254
  )
203
255
  self._triplet += y_diagonals.triplet
204
256
 
205
-
206
- # -