PyFinitDiff 1.2.2__py3-none-any.whl → 1.3.0__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.
Files changed (57) hide show
  1. PyFinitDiff/VERSION +1 -1
  2. PyFinitDiff/__pycache__/__init__.cpython-311.pyc +0 -0
  3. PyFinitDiff/__pycache__/triplet.cpython-311.pyc +0 -0
  4. PyFinitDiff/__pycache__/utils.cpython-311.pyc +0 -0
  5. PyFinitDiff/coefficients/__pycache__/__init__.cpython-311.pyc +0 -0
  6. PyFinitDiff/coefficients/__pycache__/backward.cpython-311.pyc +0 -0
  7. PyFinitDiff/coefficients/__pycache__/central.cpython-311.pyc +0 -0
  8. PyFinitDiff/coefficients/__pycache__/forward.cpython-311.pyc +0 -0
  9. PyFinitDiff/finite_difference_1D/__init__.py +6 -6
  10. PyFinitDiff/finite_difference_1D/__pycache__/__init__.cpython-311.pyc +0 -0
  11. PyFinitDiff/finite_difference_1D/__pycache__/boundaries.cpython-311.pyc +0 -0
  12. PyFinitDiff/finite_difference_1D/__pycache__/derivative.cpython-311.pyc +0 -0
  13. PyFinitDiff/finite_difference_1D/__pycache__/diagonals.cpython-311.pyc +0 -0
  14. PyFinitDiff/finite_difference_1D/__pycache__/finite_difference.cpython-311.pyc +0 -0
  15. PyFinitDiff/finite_difference_1D/__pycache__/utils.cpython-311.pyc +0 -0
  16. PyFinitDiff/finite_difference_1D/boundaries.py +115 -28
  17. PyFinitDiff/finite_difference_1D/derivative.py +44 -0
  18. PyFinitDiff/finite_difference_1D/diagonals.py +4 -3
  19. PyFinitDiff/finite_difference_1D/finite_difference.py +22 -8
  20. PyFinitDiff/finite_difference_1D/utils.py +40 -4
  21. PyFinitDiff/finite_difference_2D/__init__.py +5 -7
  22. PyFinitDiff/finite_difference_2D/__pycache__/__init__.cpython-311.pyc +0 -0
  23. PyFinitDiff/finite_difference_2D/__pycache__/boundaries.cpython-311.pyc +0 -0
  24. PyFinitDiff/finite_difference_2D/__pycache__/derivative.cpython-311.pyc +0 -0
  25. PyFinitDiff/finite_difference_2D/__pycache__/diagonals.cpython-311.pyc +0 -0
  26. PyFinitDiff/finite_difference_2D/__pycache__/finite_difference.cpython-311.pyc +0 -0
  27. PyFinitDiff/finite_difference_2D/__pycache__/utils.cpython-311.pyc +0 -0
  28. PyFinitDiff/finite_difference_2D/boundaries.py +103 -23
  29. PyFinitDiff/finite_difference_2D/dense.py +15 -9
  30. PyFinitDiff/finite_difference_2D/derivative.py +18 -29
  31. PyFinitDiff/finite_difference_2D/diagonals.py +147 -91
  32. PyFinitDiff/finite_difference_2D/finite_difference.py +67 -56
  33. PyFinitDiff/finite_difference_2D/utils.py +51 -5
  34. {PyFinitDiff-1.2.2.dist-info → PyFinitDiff-1.3.0.dist-info}/METADATA +1 -1
  35. PyFinitDiff-1.3.0.dist-info/RECORD +61 -0
  36. tests/1D/__pycache__/__init__.cpython-311.pyc +0 -0
  37. tests/1D/__pycache__/test_api.cpython-311-pytest-8.2.2.pyc +0 -0
  38. tests/1D/__pycache__/test_derivative.cpython-311-pytest-8.2.2.pyc +0 -0
  39. tests/1D/test_api.py +19 -12
  40. tests/1D/test_derivative.py +30 -9
  41. tests/2D/__pycache__/__init__.cpython-311.pyc +0 -0
  42. tests/2D/__pycache__/test_api.cpython-311-pytest-8.2.2.pyc +0 -0
  43. tests/2D/__pycache__/test_derivative.cpython-311-pytest-8.2.2.pyc +0 -0
  44. tests/2D/__pycache__/test_eigen.cpython-311-pytest-8.2.2.pyc +0 -0
  45. tests/2D/test_api.py +19 -13
  46. tests/2D/test_derivative.py +32 -25
  47. tests/2D/test_eigen.py +21 -6
  48. PyFinitDiff/finite_difference_1D/__pycache__/derivatives.cpython-311.pyc +0 -0
  49. PyFinitDiff/finite_difference_1D/__pycache__/meshes.cpython-311.pyc +0 -0
  50. PyFinitDiff/finite_difference_1D/derivatives.py +0 -53
  51. PyFinitDiff/finite_difference_1D/meshes.py +0 -41
  52. PyFinitDiff/finite_difference_2D/__pycache__/meshes.cpython-311.pyc +0 -0
  53. PyFinitDiff/finite_difference_2D/meshes.py +0 -50
  54. PyFinitDiff-1.2.2.dist-info/RECORD +0 -65
  55. {PyFinitDiff-1.2.2.dist-info → PyFinitDiff-1.3.0.dist-info}/LICENSE +0 -0
  56. {PyFinitDiff-1.2.2.dist-info → PyFinitDiff-1.3.0.dist-info}/WHEEL +0 -0
  57. {PyFinitDiff-1.2.2.dist-info → PyFinitDiff-1.3.0.dist-info}/top_level.txt +0 -0
PyFinitDiff/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.2
1
+ 1.3.0
@@ -1,6 +1,6 @@
1
- from .boundaries import *
2
- from .diagonals import *
3
- from .meshes import *
4
- from .derivatives import *
5
- from .finite_difference import *
6
- from .utils import *
1
+ from .finite_difference import FiniteDifference # noqa: F401
2
+ from .boundaries import Boundaries # noqa: F401
3
+ from .utils import get_circular_mesh_triplet # noqa: F401
4
+ from .derivative import get_function_derivative # noqa: F401
5
+
6
+ # -
@@ -2,77 +2,152 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  import numpy
5
- from dataclasses import dataclass
6
5
 
6
+ from typing import Optional, List, Tuple
7
+ from pydantic.dataclasses import dataclass
8
+ from pydantic import ConfigDict
9
+
10
+ config_dict = ConfigDict(
11
+ extra='forbid',
12
+ strict=True,
13
+ arbitrary_types_allowed=True,
14
+ kw_only=True,
15
+ frozen=True
16
+ )
17
+
18
+
19
+ @dataclass(config=config_dict)
20
+ class Boundary:
21
+ """Class representing a boundary with a specific name, value, and mesh information.
22
+
23
+ Attributes:
24
+ name (str): The name of the boundary.
25
+ value (Optional[str]): The value associated with the boundary.
26
+ mesh_info (object): The mesh information object.
27
+ """
7
28
 
8
- @dataclass(frozen=True)
9
- class Boundary():
10
29
  name: str
11
- value: str
30
+ value: Optional[str]
12
31
  mesh_info: object
13
32
 
14
33
  def get_factor(self) -> float:
34
+ """Gets the factor associated with the boundary value.
35
+
36
+ Returns:
37
+ float: The factor corresponding to the boundary value.
38
+
39
+ Raises:
40
+ ValueError: If the boundary value is unexpected.
41
+ """
15
42
  match self.value:
16
43
  case 'symmetric':
17
- return +1
44
+ return 1.0
18
45
  case 'anti-symmetric':
19
- return -1
46
+ return -1.0
20
47
  case 'zero':
21
- return 0
48
+ return 0.0
22
49
  case 'none':
23
50
  return numpy.nan
51
+ case _:
52
+ raise ValueError(f"Unexpected boundary value: {self.value}")
24
53
 
25
- def get_shift_vector(self, offset: int) -> numpy.ndarray:
54
+ def get_shift_vector(self, offset: int) -> Optional[numpy.ndarray]:
55
+ """Calculates the shift vector based on the boundary name and offset.
56
+
57
+ Args:
58
+ offset (int): The offset value to be used in the shift vector calculation.
59
+
60
+ Returns:
61
+ Optional[numpy.ndarray]: The shift vector as a numpy array, or None if the boundary name is 'center'.
62
+
63
+ Raises:
64
+ ValueError: If the boundary name is unexpected.
65
+ """
26
66
  offset = abs(offset)
27
67
 
28
68
  match self.name.lower():
29
69
  case 'center':
30
- shift_vector = None
70
+ return None
31
71
  case 'left':
32
72
  shift_vector = numpy.zeros(self.mesh_info.size)
33
73
  shift_vector[:offset] = numpy.arange(offset)[::-1] + 1
74
+ return shift_vector
34
75
  case 'right':
35
76
  shift_vector = numpy.zeros(self.mesh_info.size)
36
- shift_vector[-offset - 1:] = - numpy.arange(offset + 1)
77
+ shift_vector[-offset - 1:] = -numpy.arange(offset + 1)
78
+ return shift_vector
79
+ case _:
80
+ raise ValueError(f"Unexpected boundary name: {self.name}")
37
81
 
38
- return shift_vector
39
82
 
83
+ @dataclass(config=config_dict)
84
+ class Boundaries:
85
+ """Class representing the boundaries with left and right values.
40
86
 
41
- @dataclass()
42
- class Boundaries():
43
- left: str = 'zero'
44
- """ Value of the left boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
45
- right: str = 'zero'
46
- """ Value of the right boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
87
+ Attributes:
88
+ left (Optional[str]): Value of the left boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
89
+ right (Optional[str]): Value of the right boundary. Defaults to 'zero' either ['zero', 'symmetric', 'anti-symmetric'].
90
+ acceptable_boundary (List[str]): List of acceptable boundary values.
91
+ all_boundaries (List[str]): List of all boundary names.
92
+ """
93
+ left: Optional[str] = 'zero'
94
+ right: Optional[str] = 'zero'
47
95
 
48
96
  acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
49
-
50
97
  all_boundaries = ['left', 'right']
51
98
 
52
99
  def __post_init__(self) -> None:
100
+ """Post-initialization method to assert acceptable boundary values."""
53
101
  for boundary in self.all_boundaries:
54
102
  self.assert_boundary_acceptable(boundary_string=boundary)
55
103
 
56
104
  def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
105
+ """Asserts that both boundaries are not the same axis symmetries if they are not 'zero'.
106
+
107
+ Args:
108
+ boundary_0 (str): The first boundary value.
109
+ boundary_1 (str): The second boundary value.
110
+
111
+ Raises:
112
+ ValueError: If both boundaries are set to the same axis symmetries.
113
+ """
57
114
  if boundary_0 != 'zero' and boundary_1 != 'zero':
58
- raise ValueError("Same-axis symmetries shouldn't be set on both end")
115
+ raise ValueError("Same-axis symmetries shouldn't be set on both ends")
59
116
 
60
117
  def assert_boundary_acceptable(self, boundary_string: str) -> None:
118
+ """Asserts that a given boundary value is acceptable.
119
+
120
+ Args:
121
+ boundary_string (str): The name of the boundary to check.
122
+
123
+ Raises:
124
+ AssertionError: If the boundary value is not acceptable.
125
+ """
61
126
  boundary_value = getattr(self, boundary_string)
62
- assert boundary_value in self.acceptable_boundary, f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. Input must be in: {self.acceptable_boundary}"
127
+ assert boundary_value in self.acceptable_boundary, (
128
+ f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. "
129
+ f"Input must be in: {self.acceptable_boundary}"
130
+ )
63
131
 
64
- def get_boundary_pairs(self) -> list:
65
- return [(self.left, self.right), (self.top, self.bottom)]
132
+ def get_boundary_pairs(self) -> List[Tuple[str, str]]:
133
+ """Gets the pairs of boundaries.
66
134
 
67
- def get_boundary(self, name: str) -> Boundary:
135
+ Returns:
136
+ List[Tuple[str, str]]: A list of tuples containing boundary pairs.
68
137
  """
69
- Return a specific instance of the boundary
138
+ return [(self.left, self.right)]
70
139
 
71
- :param name: The name
72
- :type name: str
140
+ def get_boundary(self, name: str) -> Boundary:
141
+ """Returns a specific instance of the boundary.
73
142
 
74
- :returns: The boundary.
75
- :rtype: Boundary
143
+ Args:
144
+ name (str): The name of the boundary.
145
+
146
+ Returns:
147
+ Boundary: The boundary instance.
148
+
149
+ Raises:
150
+ AttributeError: If the boundary name is not an attribute of the instance.
76
151
  """
77
152
  if not hasattr(self, name):
78
153
  value = None
@@ -88,6 +163,17 @@ class Boundaries():
88
163
  return boundary
89
164
 
90
165
  def offset_to_boundary(self, offset: int) -> str:
166
+ """Determines the boundary based on the offset.
167
+
168
+ Args:
169
+ offset (int): The offset value.
170
+
171
+ Returns:
172
+ str: The name of the boundary corresponding to the offset.
173
+
174
+ Raises:
175
+ ValueError: If the offset does not correspond to a valid boundary.
176
+ """
91
177
  if offset == 0:
92
178
  return self.get_boundary('center')
93
179
 
@@ -99,5 +185,6 @@ class Boundaries():
99
185
  if offset > -self.mesh_info.n_x:
100
186
  return self.get_boundary('left')
101
187
 
188
+ raise ValueError("Offset does not correspond to a valid boundary.")
102
189
 
103
190
  # -
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ from PyFinitDiff.coefficients import FiniteCoefficients
5
+
6
+
7
+ def get_function_derivative(
8
+ function: callable,
9
+ x_eval: float,
10
+ derivative: int,
11
+ delta: float,
12
+ function_kwargs: dict,
13
+ accuracy: int = 4,
14
+ coefficient_type: str = 'central') -> float:
15
+ """
16
+ Computes the derivative of a given function at a specified point using finite differences.
17
+
18
+ Args:
19
+ function (callable): The function to differentiate.
20
+ x_eval (float): The point at which to evaluate the derivative.
21
+ derivative (int): The order of the derivative.
22
+ delta (float): The distance between points.
23
+ function_kwargs (dict): Additional keyword arguments to pass to the function.
24
+ accuracy (int, optional): The accuracy of the finite difference approximation. Defaults to 4.
25
+ coefficient_type (str, optional): The type of finite difference coefficients ('central', 'forward', 'backward'). Defaults to 'central'.
26
+
27
+ Returns:
28
+ float: The value of the derivative at the specified point.
29
+ """
30
+ coefficients = FiniteCoefficients(
31
+ derivative=derivative,
32
+ accuracy=accuracy,
33
+ coefficient_type=coefficient_type
34
+ )
35
+
36
+ summation = 0
37
+ for index, value in coefficients:
38
+ x = x_eval + index * delta
39
+ y = function(x, **function_kwargs)
40
+ summation += value * y
41
+
42
+ return summation / delta ** derivative
43
+
44
+ # -
@@ -7,7 +7,8 @@ from dataclasses import dataclass, field
7
7
  # Local imports
8
8
  import numpy
9
9
  from PyFinitDiff.triplet import Triplet
10
- import PyFinitDiff.finite_difference_1D as module
10
+ from PyFinitDiff.finite_difference_1D.utils import MeshInfo
11
+ from PyFinitDiff.finite_difference_1D.boundaries import Boundary
11
12
 
12
13
 
13
14
  @dataclass
@@ -24,7 +25,7 @@ class Diagonal():
24
25
  """ Offset of the column index for the diagonal. """
25
26
  values: float
26
27
  """ Value associated with the diagonal. """
27
- boundary: module.Boundary
28
+ boundary: Boundary
28
29
  """ Instance of the boundary used for that diagonal. """
29
30
 
30
31
  @property
@@ -105,7 +106,7 @@ class Diagonal():
105
106
 
106
107
 
107
108
  class ConstantDiagonal(Diagonal):
108
- def __init__(self, offset: int, value: float, mesh_info: list, boundary: module.Boundary):
109
+ def __init__(self, offset: int, value: float, mesh_info: list, boundary: Boundary):
109
110
  super().__init__(
110
111
  offset=offset,
111
112
  mesh_info=mesh_info,
@@ -1,12 +1,26 @@
1
1
  #!/usr/bin/env python
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
- from dataclasses import dataclass, field
5
- import PyFinitDiff.finite_difference_1D as module
4
+ from pydantic.dataclasses import dataclass
5
+ from pydantic import ConfigDict
6
+ from dataclasses import field
7
+
6
8
  from PyFinitDiff.coefficients import FiniteCoefficients
9
+ from PyFinitDiff.finite_difference_1D.boundaries import Boundaries
10
+ from PyFinitDiff.finite_difference_1D.utils import MeshInfo
11
+ from PyFinitDiff.finite_difference_1D.diagonals import DiagonalSet, ConstantDiagonal
12
+
13
+
14
+ config_dict = ConfigDict(
15
+ extra='forbid',
16
+ strict=True,
17
+ arbitrary_types_allowed=True,
18
+ kw_only=True,
19
+ frozen=True
20
+ )
21
+
7
22
 
8
-
9
- @dataclass
23
+ @dataclass(config=config_dict)
10
24
  class FiniteDifference():
11
25
  """
12
26
  This class represent a specific finit difference configuration, which is defined with the descretization of the mesh, the derivative order,
@@ -20,13 +34,13 @@ class FiniteDifference():
20
34
  """ Derivative order to convert into finit-difference matrix. """
21
35
  accuracy: int = 2
22
36
  """ Accuracy of the derivative approximation [error is inversly proportional to the power of that value]. """
23
- boundaries: module.Boundaries = field(default_factory=module.Boundaries())
37
+ boundaries: Boundaries = field(default_factory=Boundaries)
24
38
  """ Values of the four possible boundaries of the system. """
25
39
  x_derivative: bool = True
26
40
  """ Add the x derivative """
27
41
 
28
42
  def __post_init__(self):
29
- self.mesh_info = module.MeshInfo(
43
+ self.mesh_info = MeshInfo(
30
44
  n_x=self.n_x,
31
45
  dx=self.dx,
32
46
  )
@@ -103,7 +117,7 @@ class FiniteDifference():
103
117
  :returns: No return
104
118
  :rtype: None
105
119
  """
106
- diagonal_set = module.DiagonalSet(mesh_info=self.mesh_info)
120
+ diagonal_set = DiagonalSet(mesh_info=self.mesh_info)
107
121
 
108
122
  coefficients = getattr(self.finit_coefficient, coefficient_type)
109
123
 
@@ -113,7 +127,7 @@ class FiniteDifference():
113
127
  )
114
128
 
115
129
  for offset, value, boundary in iterator:
116
- diagonal = module.ConstantDiagonal(
130
+ diagonal = ConstantDiagonal(
117
131
  mesh_info=self.mesh_info,
118
132
  offset=offset,
119
133
  boundary=boundary,
@@ -2,17 +2,53 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  from dataclasses import dataclass
5
+ import numpy
6
+ from PyFinitDiff.triplet import DiagonalTriplet
5
7
 
6
8
 
7
9
  @dataclass
8
- class MeshInfo():
10
+ class MeshInfo:
11
+ """
12
+ This class represents the mesh information for a 1D finite-difference grid.
13
+
14
+ Attributes:
15
+ n_x (int): Number of points in the x direction.
16
+ dx (float): Infinitesimal displacement in the x direction. Defaults to 1.
17
+ size (int): Total number of points in the mesh.
18
+ shape (Tuple[int]): Shape of the mesh as a tuple.
19
+ """
9
20
  n_x: int
10
- """ Number of point in the x direction """
11
21
  dx: float = 1
12
- """ Infinetisemal displacement in x direction """
13
22
 
14
23
  def __post_init__(self):
24
+ """Post-initialization to calculate mesh size and shape."""
15
25
  self.size = self.n_x
16
26
  self.shape = (self.n_x,)
17
27
 
18
- # -
28
+
29
+ def get_circular_mesh_triplet(
30
+ n_x: int,
31
+ radius: float,
32
+ x_offset: float = 0,
33
+ value_out: float = 0,
34
+ value_in: float = 1) -> DiagonalTriplet:
35
+ """
36
+ Gets a Triplet corresponding to a 1D mesh with a circular structure of value_in inside and value_out outside.
37
+
38
+ Args:
39
+ n_x (int): The number of points in the x-axis.
40
+ radius (float): The radius of the circular structure.
41
+ x_offset (float, optional): The x offset of the circular structure. Defaults to 0.
42
+ value_out (float, optional): The value outside the circular structure. Defaults to 0.
43
+ value_in (float, optional): The value inside the circular structure. Defaults to 1.
44
+
45
+ Returns:
46
+ DiagonalTriplet: The 1D circular mesh triplet.
47
+ """
48
+ x = numpy.linspace(-100, 100, n_x)
49
+
50
+ r = numpy.abs(x - x_offset)
51
+ mesh = numpy.ones(x.shape) * value_out
52
+ mesh[r < radius] = value_in
53
+
54
+ return DiagonalTriplet(mesh, shape=(n_x,))
@@ -1,8 +1,6 @@
1
- from .boundaries import *
2
- from .finite_difference import *
3
- from .diagonals import *
4
- from .utils import *
5
- from .derivative import *
6
- from .meshes import *
1
+ from .finite_difference import FiniteDifference # noqa: F401
2
+ from .boundaries import Boundaries # noqa: F401
3
+ from .utils import get_circular_mesh_triplet # noqa: F401
4
+ from .derivative import get_array_derivative # noqa: F401
7
5
 
8
- # from .dense import *
6
+ # -