PyFinitDiff 2.0.4.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.
@@ -3,35 +3,43 @@
3
3
 
4
4
  from dataclasses import dataclass, field
5
5
 
6
-
7
6
  # Local imports
8
7
  import numpy
9
8
  from PyFinitDiff.triplet import Triplet
10
9
  from PyFinitDiff.finite_difference_1D.boundaries import Boundary
11
10
 
12
-
13
11
  @dataclass
14
- class Diagonal():
12
+ class Diagonal:
15
13
  """
16
- This class is a construction of diagonals element of the finit-difference method.
17
- The class can be initialized with different parameters suchs as it's offset or
18
- boundary condition.
19
-
14
+ Represents a diagonal element of the finite-difference method.
15
+
16
+ This class constructs a diagonal with parameters such as its offset and boundary condition.
17
+
18
+ Parameters
19
+ ----------
20
+ mesh_info : object
21
+ Instance describing the meta information on the mesh to be considered.
22
+ offset : int
23
+ Offset of the column index for the diagonal.
24
+ values : float
25
+ Value associated with the diagonal.
26
+ boundary : PyFinitDiff.finite_difference_1D.boundaries.Boundary
27
+ Instance of the boundary used for the diagonal.
20
28
  """
21
29
  mesh_info: object
22
- """ Instance describing the meta info on the mesh to be considered. """
23
30
  offset: int
24
- """ Offset of the column index for the diagonal. """
25
31
  values: float
26
- """ Value associated with the diagonal. """
27
32
  boundary: Boundary
28
- """ Instance of the boundary used for that diagonal. """
29
33
 
30
34
  @property
31
35
  def triplet(self) -> Triplet:
32
36
  """
33
37
  Return the Triplet instance of the diagonal.
34
38
 
39
+ Returns
40
+ -------
41
+ Triplet
42
+ A Triplet instance representing the non-zero values in the diagonal.
35
43
  """
36
44
  self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
37
45
 
@@ -44,33 +52,21 @@ class Diagonal():
44
52
 
45
53
  def compute_triplet(self) -> None:
46
54
  """
47
- Compute the diagonal index and generate a Triplet instance out of it.
48
- The value of the third triplet column depends on the boundary condition.
55
+ Compute the diagonal indices and generate a Triplet instance out of it.
49
56
 
50
- :returns: No return
51
- :rtype: None
57
+ The value of the third column in the triplet depends on the boundary condition.
52
58
  """
53
59
  self.rows: numpy.ndarray = numpy.arange(0, self.mesh_info.size)
54
-
55
60
  self.columns: numpy.ndarray = self.rows + self.offset
56
-
57
61
  self.apply_symmetry()
58
-
59
62
  self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
60
63
 
61
64
  def apply_symmetry(self) -> None:
62
65
  """
63
- Return the value of the diabonal index as defined by the boundary condition.
64
- If boundary is symmetric the value stays the same, if anti-symmetric a minus sign
65
- is added, if zero it returns zero.
66
-
67
- :param values: The values
68
- :type values: The initial value
69
- :param shift_array: The shift array
70
- :type shift_array: numpy.ndarray
66
+ Apply boundary symmetry conditions to the diagonal.
71
67
 
72
- :returns: The symmetrized value
73
- :rtype: float
68
+ If the boundary is symmetric, the value stays the same. If it is anti-symmetric,
69
+ a negative sign is added. If it is zero, the value becomes zero.
74
70
  """
75
71
  shift_array = self.boundary.get_shift_vector(self.offset)
76
72
 
@@ -78,33 +74,29 @@ class Diagonal():
78
74
  return
79
75
 
80
76
  self.columns = self.columns + 2 * shift_array
81
-
82
77
  self.values[shift_array != 0] *= self.boundary.get_factor()
83
78
 
84
79
  def validate_index(self) -> None:
85
80
  """
86
- Removes all negative index
87
-
88
- :returns: No return
89
- :rtype: None
81
+ Remove all negative indices from the diagonal.
90
82
  """
91
83
  valid_index = self.columns >= 0
92
-
93
84
  self.columns = self.columns[valid_index]
94
-
95
85
  self.rows = self.rows[valid_index]
96
-
97
86
  self.values = self.values[valid_index]
98
87
 
99
88
  def plot(self) -> None:
100
89
  """
101
- Plots the Triplet instance.
102
-
90
+ Plot the Triplet instance representing the diagonal.
103
91
  """
104
92
  return self.triplet.plot()
105
93
 
106
-
107
94
  class ConstantDiagonal(Diagonal):
95
+ """
96
+ Represents a diagonal with constant values.
97
+
98
+ This subclass of Diagonal is initialized with constant values for the entire diagonal.
99
+ """
108
100
  def __init__(self, offset: int, value: float, mesh_info: list, boundary: Boundary):
109
101
  super().__init__(
110
102
  offset=offset,
@@ -113,137 +105,218 @@ class ConstantDiagonal(Diagonal):
113
105
  boundary=boundary,
114
106
  )
115
107
 
116
-
117
108
  @dataclass
118
- class DiagonalSet():
109
+ class DiagonalSet:
110
+ """
111
+ Represents a set of diagonals for the finite-difference matrix.
112
+
113
+ Parameters
114
+ ----------
115
+ mesh_info : object
116
+ Instance describing the meta information on the mesh.
117
+ diagonals : list of Diagonal
118
+ List containing diagonal elements.
119
+ """
119
120
  mesh_info: object
120
121
  diagonals: list = field(default_factory=list)
121
122
 
122
123
  def append(self, diagonal: Diagonal) -> 'DiagonalSet':
124
+ """
125
+ Append a diagonal to the set.
126
+
127
+ Parameters
128
+ ----------
129
+ diagonal : Diagonal
130
+ The diagonal to be added to the set.
131
+
132
+ Returns
133
+ -------
134
+ DiagonalSet
135
+ The modified DiagonalSet instance.
136
+ """
123
137
  self.diagonals.append(diagonal)
124
138
  return self
125
139
 
126
140
  def concatenate(self, other_diagonal_set: 'DiagonalSet') -> 'DiagonalSet':
127
- self.diagonals += other_diagonal_set.diagonals
141
+ """
142
+ Concatenate another DiagonalSet to this one.
143
+
144
+ Parameters
145
+ ----------
146
+ other_diagonal_set : DiagonalSet
147
+ The other DiagonalSet to concatenate.
128
148
 
149
+ Returns
150
+ -------
151
+ DiagonalSet
152
+ The modified DiagonalSet instance.
153
+ """
154
+ self.diagonals += other_diagonal_set.diagonals
129
155
  return self
130
156
 
131
157
  def get_row_nan_bool(self) -> numpy.ndarray:
132
158
  """
133
- Returns a bool array with True where the associated rows has a numpy.nan value.
159
+ Get a boolean array indicating which rows have NaN values.
134
160
 
135
- :returns: The nan index.
136
- :rtype: numpy.ndarray
161
+ Returns
162
+ -------
163
+ numpy.ndarray
164
+ Boolean array with True where rows have NaN values.
137
165
  """
138
166
  nan_index = self.get_nan_index()
139
-
140
167
  nan_rows = self.triplet.rows[nan_index]
141
168
  nan_index = numpy.isin(self.triplet.rows, nan_rows)
142
-
143
169
  return nan_index
144
170
 
145
171
  def get_list_of_nan_rows(self) -> numpy.ndarray:
146
- nan_index = numpy.isnan(self.triplet.values)
172
+ """
173
+ Get a list of rows containing NaN values.
147
174
 
175
+ Returns
176
+ -------
177
+ numpy.ndarray
178
+ Unique list of rows with NaN values.
179
+ """
180
+ nan_index = numpy.isnan(self.triplet.values)
148
181
  nan_rows = self.triplet.rows[nan_index]
149
-
150
182
  return numpy.unique(nan_rows)
151
183
 
152
184
  def get_list_of_not_nan_rows(self) -> numpy.ndarray:
153
185
  """
154
- Gets the row list where no nan value is associated.
186
+ Get a list of rows without NaN values.
155
187
 
156
- :returns: The list of not nan rows.
157
- :rtype: numpy.ndarray
188
+ Returns
189
+ -------
190
+ numpy.ndarray
191
+ List of rows that do not have NaN values.
158
192
  """
159
193
  nan_rows = self.get_list_of_nan_rows()
160
-
161
194
  non_nan_index = ~numpy.isin(self.triplet.rows, nan_rows)
162
-
163
195
  non_nan_rows = self.triplet.rows[non_nan_index]
164
-
165
196
  return non_nan_rows
166
197
 
167
198
  def get_nan_index(self) -> numpy.ndarray:
168
199
  """
169
- Return a boolean list of True/False depending of the values of the triplet
200
+ Get a boolean array indicating which entries have NaN values.
170
201
 
171
- :returns: The nan index.
172
- :rtype: numpy.ndarray
202
+ Returns
203
+ -------
204
+ numpy.ndarray
205
+ Boolean array indicating which entries in the triplet have NaN values.
173
206
  """
174
207
  return numpy.isnan(self.triplet.values)
175
208
 
176
209
  def get_array_from_rows(self, rows: numpy.ndarray) -> numpy.ndarray:
177
210
  """
178
- Returns the array element which for which the rows is in the given rows input
211
+ Get the array elements for the specified rows.
179
212
 
180
- :param rows: The rows to search for
181
- :type rows: numpy.ndarray
213
+ Parameters
214
+ ----------
215
+ rows : numpy.ndarray
216
+ Rows to search for in the triplet.
182
217
 
183
- :returns: The array from rows.
184
- :rtype: numpy.ndarray
218
+ Returns
219
+ -------
220
+ numpy.ndarray
221
+ Array elements corresponding to the specified rows.
185
222
  """
186
223
  rows_index = numpy.isin(self.triplet.rows, rows)
187
-
188
224
  return self.triplet.array[rows_index]
189
225
 
190
226
  def remove_nan_rows(self) -> 'DiagonalSet':
191
- nan_rows = self.get_row_nan_bool()
227
+ """
228
+ Remove rows with NaN values from the DiagonalSet.
192
229
 
230
+ Returns
231
+ -------
232
+ DiagonalSet
233
+ The modified DiagonalSet instance with NaN rows removed.
234
+ """
235
+ nan_rows = self.get_row_nan_bool()
193
236
  return self.remove_rows(rows=nan_rows)
194
237
 
195
238
  def remove_rows(self, rows: numpy.ndarray) -> 'DiagonalSet':
196
- index_to_remove = numpy.isin(self.triplet.rows, rows)
239
+ """
240
+ Remove specified rows from the DiagonalSet.
197
241
 
242
+ Parameters
243
+ ----------
244
+ rows : numpy.ndarray
245
+ Rows to be removed.
246
+
247
+ Returns
248
+ -------
249
+ DiagonalSet
250
+ The modified DiagonalSet instance with specified rows removed.
251
+ """
252
+ index_to_remove = numpy.isin(self.triplet.rows, rows)
198
253
  self.triplet.array = numpy.delete(
199
254
  arr=self.triplet.array,
200
255
  obj=index_to_remove,
201
256
  axis=0
202
257
  )
203
-
204
258
  return self
205
259
 
206
260
  def replace_nan_rows_with(self, other: 'DiagonalSet') -> 'DiagonalSet':
207
261
  """
208
- Replace the nan rows in self for the equivalent rows in the other_diagonal_set if any.
262
+ Replace NaN rows in this DiagonalSet with corresponding rows from another DiagonalSet.
209
263
 
210
- :param other_diagonal_set: The other diagonal set
211
- :type other_diagonal_set: 'DiagonalSet'
264
+ Parameters
265
+ ----------
266
+ other : DiagonalSet
267
+ The other DiagonalSet whose non-NaN rows will replace NaN rows in this set.
212
268
 
213
- :returns: The self instance
214
- :rtype: 'DiagonalSet'
269
+ Returns
270
+ -------
271
+ DiagonalSet
272
+ The modified DiagonalSet instance with replaced rows.
215
273
  """
216
274
  self_nan_rows = self.get_list_of_nan_rows()
217
-
218
275
  other_not_nan_rows = other.get_list_of_not_nan_rows()
219
-
220
276
  replace_rows = numpy.intersect1d(self_nan_rows, other_not_nan_rows)
221
-
222
277
  self.remove_rows(replace_rows)
223
-
224
278
  add_array = other.get_array_from_rows(self_nan_rows)
225
-
226
279
  self.triplet.append_array(add_array)
227
280
 
228
281
  def initialize_triplet(self) -> 'DiagonalSet':
282
+ """
283
+ Initialize the Triplet instance by concatenating all diagonal triplets.
284
+
285
+ Returns
286
+ -------
287
+ DiagonalSet
288
+ The modified DiagonalSet instance with initialized Triplet.
289
+ """
229
290
  triplet = Triplet(
230
291
  array=[0, 0, 0],
231
292
  shape=self.mesh_info.shape
232
293
  )
233
-
234
294
  for diagonal in self.diagonals:
235
295
  diagonal.compute_triplet()
236
296
  triplet += diagonal.triplet
237
-
238
297
  self.triplet = triplet
239
-
240
298
  return self
241
299
 
242
300
  def __add__(self, other: 'DiagonalSet') -> 'DiagonalSet':
243
- self.diagonals += other.diagonals
301
+ """
302
+ Add another DiagonalSet to this one.
244
303
 
304
+ Parameters
305
+ ----------
306
+ other : DiagonalSet
307
+ The other DiagonalSet to add.
308
+
309
+ Returns
310
+ -------
311
+ DiagonalSet
312
+ The combined DiagonalSet instance.
313
+ """
314
+ self.diagonals += other.diagonals
245
315
  return self
246
316
 
247
317
  def plot(self):
318
+ """
319
+ Plot the Triplet instance representing the entire DiagonalSet.
320
+ """
248
321
  return self.triplet.plot()
249
- # -
322
+
@@ -10,7 +10,6 @@ from PyFinitDiff.finite_difference_1D.boundaries import Boundaries
10
10
  from PyFinitDiff.finite_difference_1D.utils import MeshInfo
11
11
  from PyFinitDiff.finite_difference_1D.diagonals import DiagonalSet, ConstantDiagonal
12
12
 
13
-
14
13
  config_dict = ConfigDict(
15
14
  extra='forbid',
16
15
  strict=True,
@@ -19,25 +18,44 @@ config_dict = ConfigDict(
19
18
  frozen=False
20
19
  )
21
20
 
22
-
23
21
  @dataclass(config=config_dict)
24
- class FiniteDifference():
22
+ class FiniteDifference:
25
23
  """
26
- This class represent a specific finit difference configuration, which is defined with the descretization of the mesh, the derivative order,
27
- accuracy and the boundary condition that are defined. More information is providided at the following link: 'math.toronto.edu/mpugh/Teaching/Mat1062/notes2.pdf'
24
+ Represents a specific finite difference configuration, including the discretization
25
+ of the mesh, derivative order, accuracy, and boundary conditions.
26
+
27
+ Parameters
28
+ ----------
29
+ n_x : int
30
+ Number of points in the x direction.
31
+ dx : float, optional
32
+ Infinitesimal displacement in the x direction (default is 1).
33
+ derivative : int, optional
34
+ Order of the derivative to convert into a finite-difference matrix (default is 1).
35
+ accuracy : int, optional
36
+ Accuracy of the derivative approximation (error is inversely proportional to the power of this value, default is 2).
37
+ boundaries : PyFinitDiff.finite_difference_1D.boundaries.Boundary, optional
38
+ Values of the four possible boundaries of the system (default is an empty Boundaries object).
39
+ x_derivative : bool, optional
40
+ Whether to add the x derivative (default is True).
41
+
42
+ Attributes
43
+ ----------
44
+ mesh_info : MeshInfo
45
+ Contains information about the mesh, such as the number of points and spacing.
46
+ boundaries : PyFinitDiff.finite_difference_1D.boundaries.Boundary
47
+ Boundary conditions applied to the system.
48
+ finit_coefficient : FiniteCoefficients
49
+ Coefficients used for finite difference calculation.
50
+ _triplet : tuple or None
51
+ The triplet representation of the finite-difference matrix, initialized to None.
28
52
  """
29
53
  n_x: int
30
- """ Number of point in the x direction """
31
54
  dx: float = 1
32
- """ Infinetisemal displacement in x direction """
33
55
  derivative: int = 1
34
- """ Derivative order to convert into finit-difference matrix. """
35
56
  accuracy: int = 2
36
- """ Accuracy of the derivative approximation [error is inversly proportional to the power of that value]. """
37
57
  boundaries: Boundaries = field(default_factory=Boundaries)
38
- """ Values of the four possible boundaries of the system. """
39
58
  x_derivative: bool = True
40
- """ Add the x derivative """
41
59
 
42
60
  def __post_init__(self):
43
61
  self.mesh_info = MeshInfo(
@@ -54,14 +72,26 @@ class FiniteDifference():
54
72
 
55
73
  @property
56
74
  def shape(self):
75
+ """
76
+ Tuple representing the shape of the finite-difference system.
77
+
78
+ Returns
79
+ -------
80
+ tuple
81
+ The shape of the system, represented by the number of points in the x direction.
82
+ """
57
83
  return (self.n_x,)
58
84
 
59
85
  @property
60
86
  def triplet(self):
61
87
  """
62
- Triplet representing the non-nul values of the specific
63
- finite-difference configuration.
88
+ Returns the triplet representation of the non-zero values of the finite-difference configuration.
89
+ Constructs the triplet if it has not been initialized.
64
90
 
91
+ Returns
92
+ -------
93
+ tuple
94
+ The triplet representation of the finite-difference matrix.
65
95
  """
66
96
  if not self._triplet:
67
97
  self.construct_triplet()
@@ -69,26 +99,43 @@ class FiniteDifference():
69
99
 
70
100
  @property
71
101
  def _dx(self) -> float:
102
+ """
103
+ Returns the scaled infinitesimal displacement for the x direction.
104
+
105
+ Returns
106
+ -------
107
+ float
108
+ The displacement scaled by the derivative order.
109
+ """
72
110
  return self.dx ** self.derivative
73
111
 
74
112
  @property
75
113
  def _dy(self) -> float:
76
- return self.dy ** self.derivative
77
-
78
- def iterate_central_coefficient(
79
- self,
80
- coefficients: str,
81
- offset_multiplier: int) -> tuple:
82
114
  """
83
- Iterate throught the given type coefficients
115
+ Returns the scaled infinitesimal displacement for the y direction.
84
116
 
85
- :param coefficient_type: The coefficient type
86
- :type coefficient_type: str
87
- :param offset_multiplier: The offset multiplier
88
- :type offset_multiplier: int
117
+ Returns
118
+ -------
119
+ float
120
+ The displacement scaled by the derivative order.
121
+ """
122
+ return self.dy ** self.derivative
89
123
 
90
- :returns: The offset, value, coefficient and boundary type
91
- :rtype: tuple
124
+ def iterate_central_coefficient(self, coefficients: str, offset_multiplier: int) -> tuple:
125
+ """
126
+ Iterates through the given type of coefficients to provide the offset, value, and boundary type.
127
+
128
+ Parameters
129
+ ----------
130
+ coefficients : str
131
+ The type of coefficients to iterate over.
132
+ offset_multiplier : int
133
+ Multiplier applied to the coefficient offset.
134
+
135
+ Yields
136
+ ------
137
+ tuple
138
+ A tuple containing the offset, value, and corresponding boundary type.
92
139
  """
93
140
  for offset, value in coefficients:
94
141
  offset *= offset_multiplier
@@ -97,25 +144,23 @@ class FiniteDifference():
97
144
 
98
145
  yield offset, value, boundary
99
146
 
100
- def _add_diagonal_coefficient(
101
- self,
102
- coefficient_type: str,
103
- offset_multiplier: int,
104
- delta: float) -> None:
147
+ def _add_diagonal_coefficient(self, coefficient_type: str, offset_multiplier: int, delta: float) -> DiagonalSet:
105
148
  """
106
149
  Adds a diagonal coefficient to the list of diagonals.
107
150
 
108
- :param coefficient_type: The coefficient type
109
- :type coefficient_type: str
110
- :param diagonals: The diagonals
111
- :type diagonals: list
112
- :param offset_multiplier: The offset multiplier
113
- :type offset_multiplier: int
114
- :param delta: The delta
115
- :type delta: float
116
-
117
- :returns: No return
118
- :rtype: None
151
+ Parameters
152
+ ----------
153
+ coefficient_type : str
154
+ The type of coefficients to add (e.g., 'central', 'forward', 'backward').
155
+ offset_multiplier : int
156
+ Multiplier applied to the offset for each coefficient.
157
+ delta : float
158
+ Scaling factor for the coefficient values.
159
+
160
+ Returns
161
+ -------
162
+ DiagonalSet
163
+ A set of diagonals representing the finite-difference configuration.
119
164
  """
120
165
  diagonal_set = DiagonalSet(mesh_info=self.mesh_info)
121
166
 
@@ -140,18 +185,21 @@ class FiniteDifference():
140
185
 
141
186
  return diagonal_set
142
187
 
143
- def get_diagonal_set_full(self, offset_multiplier: int, delta: float) -> None:
188
+ def get_diagonal_set_full(self, offset_multiplier: int, delta: float) -> DiagonalSet:
144
189
  """
145
- Constructs and returns the central coefficents diagonals which is completed with
146
- forward and backward coefficients if some 'nan' values are left.
147
-
148
- :param offset_multiplier: The offset multiplier
149
- :type offset_multiplier: int
150
- :param delta: The delta
151
- :type delta: float
152
-
153
- :returns: No return
154
- :rtype: None
190
+ Constructs and returns a complete set of diagonals, including central, forward, and backward coefficients.
191
+
192
+ Parameters
193
+ ----------
194
+ offset_multiplier : int
195
+ Multiplier applied to the coefficient offset.
196
+ delta : float
197
+ Scaling factor for the coefficient values.
198
+
199
+ Returns
200
+ -------
201
+ DiagonalSet
202
+ A set of diagonals representing the finite-difference configuration, including adjustments for boundaries.
155
203
  """
156
204
  central_diagonal = self._add_diagonal_coefficient(
157
205
  coefficient_type='central',
@@ -172,12 +220,14 @@ class FiniteDifference():
172
220
  )
173
221
 
174
222
  central_diagonal.replace_nan_rows_with(forward_diagonal)
175
-
176
223
  central_diagonal.replace_nan_rows_with(backward_diagonal)
177
224
 
178
225
  return central_diagonal
179
226
 
180
227
  def construct_triplet(self) -> None:
228
+ """
229
+ Constructs the triplet representation of the finite-difference matrix for the x direction.
230
+ """
181
231
  x_diagonals = self.get_diagonal_set_full(
182
232
  offset_multiplier=1,
183
233
  delta=self._dx
@@ -185,5 +235,4 @@ class FiniteDifference():
185
235
 
186
236
  self._triplet = x_diagonals.triplet
187
237
 
188
-
189
238
  # -