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