Source code#
Triplet#
- class Triplet(shape: Tuple[int, int], array: ndarray, add_extra_column: bool = False)[source]#
Bases:
object
A class representing a sparse matrix in triplet (COO) format, useful for finite-difference operations.
- Parameters:
shape (tuple of int) – Shape of the matrix (rows, columns).
array (numpy.ndarray) – A 2D array with three columns representing row indices, column indices, and values.
add_extra_column (bool, optional) – If True, adds an extra column of ones to the array (default is False).
- shape: Tuple[int, int]#
- array: ndarray#
- add_extra_column: bool = False#
- property index: ndarray#
Return the first two columns of the array, representing the indices.
- Returns:
Array of row and column indices.
- Return type:
numpy.ndarray
- property rows: ndarray#
Return the row indices (first column).
- Returns:
Array of row indices.
- Return type:
numpy.ndarray
- property columns: ndarray#
Return the column indices (second column).
- Returns:
Array of column indices.
- Return type:
numpy.ndarray
- property values: ndarray#
Return the values (third column).
- Returns:
Array of values.
- Return type:
numpy.ndarray
- property size: int#
Return the number of entries in the triplet.
- Returns:
Number of entries.
- Return type:
int
- remove_below_i(i_value: int) Triplet [source]#
Remove all entries with row indices below a specified value.
- Parameters:
i_value (int) – Threshold for row indices. Entries with row indices below this value are removed.
- Returns:
The modified Triplet instance.
- Return type:
- remove_above_i(i_value: int) Triplet [source]#
Remove all entries with row indices above a specified value.
- Parameters:
i_value (int) – Threshold for row indices. Entries with row indices above this value are removed.
- Returns:
The modified Triplet instance.
- Return type:
- remove_below_j(j_value: int) Triplet [source]#
Remove all entries with column indices below a specified value.
- Parameters:
j_value (int) – Threshold for column indices. Entries with column indices below this value are removed.
- Returns:
The modified Triplet instance.
- Return type:
- remove_above_j(j_value: int) Triplet [source]#
Remove all entries with column indices above a specified value.
- Parameters:
j_value (int) – Threshold for column indices. Entries with column indices above this value are removed.
- Returns:
The modified Triplet instance.
- Return type:
- delete(index: ndarray) None [source]#
Delete entries at the given indices.
- Parameters:
index (numpy.ndarray) – Indices of entries to delete.
- append(other: Triplet) None [source]#
Append another triplet to this one.
- Parameters:
other (Triplet) – Another Triplet instance to append.
- append_array(array: ndarray) None [source]#
Append a NumPy array to this triplet.
- Parameters:
array (numpy.ndarray) – The array to append to the triplet.
- remove_duplicate() Triplet [source]#
Remove duplicate entries by summing their values.
- Returns:
A new Triplet instance with duplicate entries removed.
- Return type:
- coincide_i(mask: Triplet) None [source]#
Remove entries whose row indices do not coincide with those in the mask.
- Parameters:
mask (Triplet) – The mask triplet used to filter rows.
- enumerate(start: int = 0, stop: int = None)[source]#
Enumerate over the entries, yielding an index and the entry values.
- Parameters:
start (int, optional) – Starting index for enumeration (default is 0).
stop (int, optional) – Stopping index for enumeration (default is None).
- Yields:
tuple – Index and a tuple of row index, column index, and value.
- get_duplicate_index() ndarray [source]#
Get indices of duplicate entries.
- Returns:
Array of duplicate indices.
- Return type:
numpy.ndarray
- property max_i: int#
Return the maximum row index.
- Returns:
Maximum row index.
- Return type:
int
- property max_j: int#
Return the maximum column index.
- Returns:
Maximum column index.
- Return type:
int
- property min_i: int#
Return the minimum row index.
- Returns:
Minimum row index.
- Return type:
int
- property min_j: int#
Return the minimum column index.
- Returns:
Minimum column index.
- Return type:
int
- property diagonal: ndarray#
Return the diagonal elements of the triplet.
- Returns:
Diagonal elements.
- Return type:
numpy.ndarray
- shift_diagonal(value: float) Triplet [source]#
Shift the diagonal elements by a specified value.
- Parameters:
value (float) – Value to add to the diagonal elements.
- Returns:
A new Triplet instance with the shifted diagonal.
- Return type:
- update_elements(other_triplet: Triplet, i_range: slice) Triplet [source]#
Update elements within a specified range.
Diagonal#
- class Diagonal(mesh_info: object, offset: int, values: float, boundary: object)[source]#
Bases:
object
Represents a diagonal element of the finite-difference method.
This class constructs a diagonal with parameters such as its offset and boundary condition.
- Parameters:
mesh_info (object) – Instance describing the meta information on the mesh to be considered.
offset (int) – Offset of the column index for the diagonal.
values (float) – Value associated with the diagonal.
boundary (object) – Instance of the boundary used for the diagonal.
- mesh_info: object#
- offset: int#
- values: float#
- boundary: object#
- property triplet: Triplet#
Return the Triplet instance of the diagonal.
- Returns:
A Triplet instance representing the non-zero values in the diagonal.
- Return type:
- compute_triplet() None [source]#
Compute the diagonal indices and generate a Triplet instance out of it.
The value of the third column in the triplet depends on the boundary condition.
- class ConstantDiagonal(offset: int, value: float, mesh_info: list, boundary: object)[source]#
Bases:
Diagonal
Represents a diagonal with constant values.
This subclass of Diagonal is initialized with constant values for the entire diagonal.
- apply_symmetry() None #
Apply boundary symmetry conditions to the diagonal.
If the boundary is symmetric, the value stays the same. If it is anti-symmetric, a negative sign is added. If it is zero, the value becomes zero.
- compute_triplet() None #
Compute the diagonal indices and generate a Triplet instance out of it.
The value of the third column in the triplet depends on the boundary condition.
- plot() None #
Plot the Triplet instance representing the diagonal.
- property triplet: Triplet#
Return the Triplet instance of the diagonal.
- Returns:
A Triplet instance representing the non-zero values in the diagonal.
- Return type:
- validate_index() None #
Remove all negative indices from the diagonal.
- mesh_info: object#
- offset: int#
- values: float#
- boundary: object#
FiniteDifference1D#
- class Boundary(name: str, value: BoundaryValue | None, mesh_info: object)[source]#
Bases:
object
Class representing a boundary with a specific name, value, and mesh information.
- Parameters:
name (str) – The name of the boundary.
value (Optional[BoundaryValue]) – The value associated with the boundary, such as BoundaryValue.SYMMETRIC, BoundaryValue.ANTI_SYMMETRIC, BoundaryValue.ZERO, or BoundaryValue.NONE.
mesh_info (object) – The mesh information object containing information about the mesh size and structure.
- get_factor() float [source]#
Get the factor associated with the boundary value.
- Returns:
The factor corresponding to the boundary value.
- Return type:
float
- Raises:
ValueError – If the boundary value is unexpected.
- get_shift_vector(offset: int) ndarray | None [source]#
Calculate the shift vector based on the boundary name and offset.
- Parameters:
offset (int) – The offset value to be used in the shift vector calculation.
- Returns:
The shift vector as a numpy array, or None if the boundary name is ‘center’.
- Return type:
Optional[numpy.ndarray]
- Raises:
ValueError – If the boundary name is unexpected.
- class FiniteDifference(n_x: int, dx: float = 1, derivative: int = 1, accuracy: int = 2, boundaries: ~PyFinitDiff.finite_difference_1D.boundaries.Boundaries = <factory>, x_derivative: bool = True)[source]#
Bases:
object
Represents a specific finite difference configuration, including the discretization of the mesh, derivative order, accuracy, and boundary conditions.
- Parameters:
n_x (int) – Number of points in the x direction.
dx (float, optional) – Infinitesimal displacement in the x direction (default is 1).
derivative (int, optional) – Order of the derivative to convert into a finite-difference matrix (default is 1).
accuracy (int, optional) – Accuracy of the derivative approximation (error is inversely proportional to the power of this value, default is 2).
boundaries (PyFinitDiff.finite_difference_1D.boundaries.Boundary, optional) – Values of the four possible boundaries of the system (default is an empty Boundaries object).
x_derivative (bool, optional) – Whether to add the x derivative (default is True).
- mesh_info#
Contains information about the mesh, such as the number of points and spacing.
- Type:
MeshInfo
- boundaries#
Boundary conditions applied to the system.
- finit_coefficient#
Coefficients used for finite difference calculation.
- Type:
FiniteCoefficients
- _triplet#
The triplet representation of the finite-difference matrix, initialized to None.
- Type:
tuple or None
- n_x: int#
- dx: float = 1#
- derivative: int = 1#
- accuracy: int = 2#
- boundaries: Boundaries#
- x_derivative: bool = True#
- property shape#
Tuple representing the shape of the finite-difference system.
- Returns:
The shape of the system, represented by the number of points in the x direction.
- Return type:
tuple
- property triplet#
Returns the triplet representation of the non-zero values of the finite-difference configuration. Constructs the triplet if it has not been initialized.
- Returns:
The triplet representation of the finite-difference matrix.
- Return type:
tuple
- iterate_central_coefficient(coefficients: str, offset_multiplier: int)[source]#
Iterates through the given type of coefficients to provide the offset, value, and boundary type.
- Parameters:
coefficients (str) – The type of coefficients to iterate over.
offset_multiplier (int) – Multiplier applied to the coefficient offset.
- Yields:
tuple – A tuple containing the offset, value, and corresponding boundary type.
- get_diagonal_set_full(offset_multiplier: int, delta: float) DiagonalSet [source]#
Constructs and returns a complete set of diagonals, including central, forward, and backward coefficients.
- Parameters:
offset_multiplier (int) – Multiplier applied to the coefficient offset.
delta (float) – Scaling factor for the coefficient values.
- Returns:
A set of diagonals representing the finite-difference configuration, including adjustments for boundaries.
- Return type:
DiagonalSet
FiniteDifference2D#
- class Boundary(name: str, value: BoundaryValue | None, mesh_info: object)[source]#
Bases:
object
Represents a boundary with a specific name, value, and mesh information.
- Parameters:
name (str) – The name of the boundary.
value (Optional[BoundaryValue]) – The value associated with the boundary (e.g., BoundaryValue.SYMMETRIC, BoundaryValue.ANTI_SYMMETRIC).
mesh_info (object) – Mesh information object, used to determine mesh-related properties.
- get_factor() float [source]#
Gets the factor associated with the boundary value.
- Returns:
The factor corresponding to the boundary value. Possible values are: - 1.0 for BoundaryValue.SYMMETRIC - -1.0 for BoundaryValue.ANTI_SYMMETRIC - 0.0 for BoundaryValue.ZERO - numpy.nan for BoundaryValue.NONE
- Return type:
float
- get_shift_vector(offset: int) ndarray | None [source]#
Calculates the shift vector based on the boundary name and offset.
- Parameters:
offset (int) – The offset value to be used in the shift vector calculation.
- Returns:
The shift vector as a numpy array, or None if the boundary is ‘center’.
- Return type:
Optional[numpy.ndarray]
- class FiniteDifference(n_x: int, n_y: int, dx: float = 1, dy: float = 1, derivative: int = 1, accuracy: int = 2, boundaries: ~PyFinitDiff.finite_difference_2D.boundaries.Boundaries = <factory>, x_derivative: bool = True, y_derivative: bool = True)[source]#
Bases:
object
Represents a specific finite difference configuration, defined by the discretization of the mesh, derivative order, accuracy, and boundary conditions.
- Parameters:
n_x (int) – Number of points in the x direction.
n_y (int) – Number of points in the y direction.
dx (float, optional) – Infinitesimal displacement in the x direction (default is 1).
dy (float, optional) – Infinitesimal displacement in the y direction (default is 1).
derivative (int, optional) – Order of the derivative to convert into a finite-difference matrix (default is 1).
accuracy (int, optional) – Accuracy of the derivative approximation (error is inversely proportional to the power of this value, default is 2).
boundaries (PyFinitDiff.finite_difference_2D.boundaries.Boundary, optional) – Values of the four possible boundaries of the system (default is an empty Boundaries object).
x_derivative (bool, optional) – Whether to add the x derivative (default is True).
y_derivative (bool, optional) – Whether to add the y derivative (default is True).
- mesh_info#
Contains information about the mesh, such as the number of points and spacing.
- Type:
MeshInfo
- boundaries#
Boundary conditions applied to the system.
- finite_coefficient#
Coefficients used for finite difference calculations.
- Type:
FiniteCoefficients
- _triplet#
The triplet representation of the finite-difference matrix, initialized to None.
- Type:
Triplet or None
- n_x: int#
- n_y: int#
- dx: float = 1#
- dy: float = 1#
- derivative: int = 1#
- accuracy: int = 2#
- boundaries: Boundaries#
- x_derivative: bool = True#
- y_derivative: bool = True#
- property shape#
Returns the shape of the mesh as a tuple.
- Returns:
The shape of the system, represented by the number of points in the x and y directions.
- Return type:
tuple
- property triplet#
Returns the triplet representation of the non-null values of the finite-difference configuration. Constructs the triplet if it has not been initialized.
- Returns:
The Triplet instance containing the array and shape.
- Return type:
- iterate_central_coefficient(coefficients: str, offset_multiplier: int)[source]#
Iterates through the given type of coefficients to provide the offset, value, and boundary type.
- Parameters:
coefficients (str) – The type of coefficients to iterate over.
offset_multiplier (int) – Multiplier applied to the coefficient offset.
- Yields:
tuple – A tuple containing the offset, value, and corresponding boundary type.
- get_diagonal_set_full(offset_multiplier: int, delta: float) DiagonalSet [source]#
Constructs and returns a complete set of diagonals, including central, forward, and backward coefficients.
- Parameters:
offset_multiplier (int) – Multiplier applied to the coefficient offset.
delta (float) – Scaling factor for the coefficient values.
- Returns:
A set of diagonals representing the finite-difference configuration, including adjustments for boundaries.
- Return type:
DiagonalSet