PyFinitDiff 1.2.3__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 (52) 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 +113 -30
  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 +21 -7
  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.3.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/2D/__pycache__/__init__.cpython-311.pyc +0 -0
  40. tests/2D/__pycache__/test_api.cpython-311-pytest-8.2.2.pyc +0 -0
  41. tests/2D/__pycache__/test_derivative.cpython-311-pytest-8.2.2.pyc +0 -0
  42. tests/2D/__pycache__/test_eigen.cpython-311-pytest-8.2.2.pyc +0 -0
  43. PyFinitDiff/finite_difference_1D/__pycache__/derivatives.cpython-311.pyc +0 -0
  44. PyFinitDiff/finite_difference_1D/__pycache__/meshes.cpython-311.pyc +0 -0
  45. PyFinitDiff/finite_difference_1D/derivatives.py +0 -53
  46. PyFinitDiff/finite_difference_1D/meshes.py +0 -41
  47. PyFinitDiff/finite_difference_2D/__pycache__/meshes.cpython-311.pyc +0 -0
  48. PyFinitDiff/finite_difference_2D/meshes.py +0 -50
  49. PyFinitDiff-1.2.3.dist-info/RECORD +0 -65
  50. {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.0.dist-info}/LICENSE +0 -0
  51. {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.0.dist-info}/WHEEL +0 -0
  52. {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.0.dist-info}/top_level.txt +0 -0
PyFinitDiff/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.3
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
+ # -
@@ -3,81 +3,151 @@
3
3
 
4
4
  import numpy
5
5
 
6
- from typing import Optional, Union
6
+ from typing import Optional, List, Tuple
7
7
  from pydantic.dataclasses import dataclass
8
8
  from pydantic import ConfigDict
9
9
 
10
- config_dict = ConfigDict(extra='forbid', strict=True)
10
+ config_dict = ConfigDict(
11
+ extra='forbid',
12
+ strict=True,
13
+ arbitrary_types_allowed=True,
14
+ kw_only=True,
15
+ frozen=True
16
+ )
11
17
 
12
18
 
13
- @dataclass(config=config_dict, kw_only=True, frozen=True)
14
- class Boundary():
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
+ """
28
+
15
29
  name: str
16
- value: Union[str, None]
30
+ value: Optional[str]
17
31
  mesh_info: object
18
32
 
19
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
+ """
20
42
  match self.value:
21
43
  case 'symmetric':
22
- return +1
44
+ return 1.0
23
45
  case 'anti-symmetric':
24
- return -1
46
+ return -1.0
25
47
  case 'zero':
26
- return 0
48
+ return 0.0
27
49
  case 'none':
28
50
  return numpy.nan
51
+ case _:
52
+ raise ValueError(f"Unexpected boundary value: {self.value}")
53
+
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'.
29
62
 
30
- def get_shift_vector(self, offset: int) -> numpy.ndarray:
63
+ Raises:
64
+ ValueError: If the boundary name is unexpected.
65
+ """
31
66
  offset = abs(offset)
32
67
 
33
68
  match self.name.lower():
34
69
  case 'center':
35
- shift_vector = None
70
+ return None
36
71
  case 'left':
37
72
  shift_vector = numpy.zeros(self.mesh_info.size)
38
73
  shift_vector[:offset] = numpy.arange(offset)[::-1] + 1
74
+ return shift_vector
39
75
  case 'right':
40
76
  shift_vector = numpy.zeros(self.mesh_info.size)
41
- shift_vector[-offset - 1:] = - numpy.arange(offset + 1)
42
-
43
- return shift_vector
44
-
45
-
46
- @dataclass()
47
- class Boundaries():
77
+ shift_vector[-offset - 1:] = -numpy.arange(offset + 1)
78
+ return shift_vector
79
+ case _:
80
+ raise ValueError(f"Unexpected boundary name: {self.name}")
81
+
82
+
83
+ @dataclass(config=config_dict)
84
+ class Boundaries:
85
+ """Class representing the boundaries with left and right values.
86
+
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
+ """
48
93
  left: Optional[str] = 'zero'
49
- """ Value of the left boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
50
94
  right: Optional[str] = 'zero'
51
- """ Value of the right boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
52
95
 
53
96
  acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
54
-
55
97
  all_boundaries = ['left', 'right']
56
98
 
57
99
  def __post_init__(self) -> None:
100
+ """Post-initialization method to assert acceptable boundary values."""
58
101
  for boundary in self.all_boundaries:
59
102
  self.assert_boundary_acceptable(boundary_string=boundary)
60
103
 
61
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
+ """
62
114
  if boundary_0 != 'zero' and boundary_1 != 'zero':
63
- 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")
64
116
 
65
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
+ """
66
126
  boundary_value = getattr(self, boundary_string)
67
- 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
+ )
68
131
 
69
- def get_boundary_pairs(self) -> list:
70
- 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.
71
134
 
72
- def get_boundary(self, name: str) -> Boundary:
135
+ Returns:
136
+ List[Tuple[str, str]]: A list of tuples containing boundary pairs.
73
137
  """
74
- Return a specific instance of the boundary
138
+ return [(self.left, self.right)]
139
+
140
+ def get_boundary(self, name: str) -> Boundary:
141
+ """Returns a specific instance of the boundary.
142
+
143
+ Args:
144
+ name (str): The name of the boundary.
75
145
 
76
- :param name: The name
77
- :type name: str
146
+ Returns:
147
+ Boundary: The boundary instance.
78
148
 
79
- :returns: The boundary.
80
- :rtype: Boundary
149
+ Raises:
150
+ AttributeError: If the boundary name is not an attribute of the instance.
81
151
  """
82
152
  if not hasattr(self, name):
83
153
  value = None
@@ -93,6 +163,17 @@ class Boundaries():
93
163
  return boundary
94
164
 
95
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
+ """
96
177
  if offset == 0:
97
178
  return self.get_boundary('center')
98
179
 
@@ -104,4 +185,6 @@ class Boundaries():
104
185
  if offset > -self.mesh_info.n_x:
105
186
  return self.get_boundary('left')
106
187
 
188
+ raise ValueError("Offset does not correspond to a valid boundary.")
189
+
107
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
+ )
7
21
 
8
22
 
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
+ # -