wren-common 0.0.0.dev0__tar.gz

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.
@@ -0,0 +1,177 @@
1
+ # Sqlite database
2
+ *.db
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+ cover/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ .pybuilder/
79
+ target/
80
+
81
+ # Jupyter Notebook
82
+ .ipynb_checkpoints
83
+
84
+ # IPython
85
+ profile_default/
86
+ ipython_config.py
87
+
88
+ # pyenv
89
+ # For a library or package, you might want to ignore these files since the code is
90
+ # intended to run in multiple environments; otherwise, check them in:
91
+ # .python-version
92
+
93
+ # pipenv
94
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
96
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
97
+ # install all needed dependencies.
98
+ #Pipfile.lock
99
+
100
+ # UV
101
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
102
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
103
+ # commonly ignored for libraries.
104
+ #uv.lock
105
+
106
+ # poetry
107
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
108
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
109
+ # commonly ignored for libraries.
110
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
111
+ #poetry.lock
112
+
113
+ # pdm
114
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
115
+ #pdm.lock
116
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
117
+ # in version control.
118
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
119
+ .pdm.toml
120
+ .pdm-python
121
+ .pdm-build/
122
+
123
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
124
+ __pypackages__/
125
+
126
+ # Celery stuff
127
+ celerybeat-schedule
128
+ celerybeat.pid
129
+
130
+ # SageMath parsed files
131
+ *.sage.py
132
+
133
+ # Environments
134
+ .env
135
+ .venv
136
+ env/
137
+ venv/
138
+ ENV/
139
+ env.bak/
140
+ venv.bak/
141
+
142
+ # Spyder project settings
143
+ .spyderproject
144
+ .spyproject
145
+
146
+ # Rope project settings
147
+ .ropeproject
148
+
149
+ # mkdocs documentation
150
+ /site
151
+
152
+ # mypy
153
+ .mypy_cache/
154
+ .dmypy.json
155
+ dmypy.json
156
+
157
+ # Pyre type checker
158
+ .pyre/
159
+
160
+ # pytype static type analyzer
161
+ .pytype/
162
+
163
+ # Cython debug symbols
164
+ cython_debug/
165
+
166
+ # PyCharm
167
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
168
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
169
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
170
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
171
+ #.idea/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
175
+
176
+ *.zip
177
+ *.out
@@ -0,0 +1,5 @@
1
+ Metadata-Version: 2.4
2
+ Name: wren-common
3
+ Version: 0.0.0.dev0
4
+ Summary: Common code for the PDE library
5
+ Requires-Dist: numpy~=2.3
@@ -0,0 +1,16 @@
1
+ [project]
2
+ name = "wren-common"
3
+ description = "Common code for the PDE library"
4
+ dependencies = ["numpy~=2.3"]
5
+ dynamic = ["version"]
6
+
7
+ [build-system]
8
+ requires = ["hatchling", "hatch-vcs"]
9
+ build-backend = "hatchling.build"
10
+
11
+ [tool.hatch.version]
12
+ source = "vcs"
13
+ fallback-version = "0.0.0-dev"
14
+
15
+ [tool.pytest.ini_options]
16
+ pythonpath = ["src"]
File without changes
File without changes
@@ -0,0 +1,138 @@
1
+ import math
2
+
3
+ import numpy as np
4
+
5
+ from wren_common.types import NDArray
6
+
7
+ rng = np.random.default_rng(0)
8
+
9
+
10
+ type TestTensor = NDArray[np.float64]
11
+ type TestTensorPair = tuple[TestTensor, TestTensor]
12
+
13
+
14
+ def arange_tensor(*shape: int) -> TestTensor:
15
+ """Create a tensor of given shape using np.arange."""
16
+ return np.arange(1, math.prod(shape) + 1, dtype=np.float64).reshape(shape)
17
+
18
+
19
+ TEST_TENSORS: list[TestTensor] = [
20
+ tensor.astype(np.float64)
21
+ for tensor in [
22
+ # ---- 2D ----
23
+ arange_tensor(3, 3),
24
+ rng.random((5, 3)),
25
+ np.array([x % 2 == 0 for x in range(16)]).reshape(4, 4),
26
+ # ---- 3D ----
27
+ arange_tensor(4, 5, 5),
28
+ rng.normal(size=(3, 4, 6)),
29
+ # ---- 4D ----
30
+ arange_tensor(6, 5, 4, 3),
31
+ 10e50 * np.ones((3, 4, 3, 5)),
32
+ # ---- 5D ----
33
+ arange_tensor(4, 3, 4, 5, 3),
34
+ -10e-5 * arange_tensor(4, 3, 4, 5, 3),
35
+ # ---- 6D ----
36
+ arange_tensor(4, 3, 4, 3, 3, 6),
37
+ ]
38
+ ]
39
+
40
+
41
+ # NOTE: each pair of tensors must have the same shape
42
+ TEST_PAIR_TENSORS: list[TestTensorPair] = [
43
+ (a.astype(np.float64), b.astype(np.float64))
44
+ for a, b in [
45
+ # ---- 2D ----
46
+ (
47
+ arange_tensor(3, 4),
48
+ rng.integers(0, 10, size=(3, 4)),
49
+ ),
50
+ # ---- 3D ----
51
+ (arange_tensor(2, 3, 4), np.ones((2, 3, 4))),
52
+ # ---- 4D ----
53
+ (
54
+ arange_tensor(2, 3, 4, 5),
55
+ rng.normal(size=(2, 3, 4, 5)),
56
+ ),
57
+ # ---- 5D ----
58
+ (
59
+ np.zeros((2, 2, 3, 4, 3)),
60
+ arange_tensor(2, 2, 3, 4, 3),
61
+ ),
62
+ # ---- 6D ----
63
+ (
64
+ arange_tensor(2, 3, 2, 3, 2, 4),
65
+ rng.integers(0, 5, size=(2, 3, 2, 3, 2, 4)),
66
+ ),
67
+ # ---- 7D ----
68
+ (
69
+ arange_tensor(2, 2, 3, 2, 4, 3, 2),
70
+ np.ones((2, 2, 3, 2, 4, 3, 2)),
71
+ ),
72
+ # ---- 8D ----
73
+ (
74
+ arange_tensor(2, 3, 2, 4, 2, 3, 2, 3),
75
+ rng.random((2, 3, 2, 4, 2, 3, 2, 3)),
76
+ ),
77
+ ]
78
+ ]
79
+
80
+ for a, b in TEST_PAIR_TENSORS:
81
+ assert a.shape == b.shape
82
+ assert a.dtype == b.dtype
83
+
84
+
85
+ SMALL_TEST_SCALARS: tuple[float, ...] = (1, -1, math.pi)
86
+ TEST_SCALARS: tuple[float, ...] = (1, 2, 0.5, -1, 0, math.pi, -math.e, 1e30, -1e30)
87
+
88
+
89
+ def tensor_interior[DType: np.number](
90
+ tensor: NDArray[DType], order: int = 1
91
+ ) -> NDArray[DType]:
92
+ """
93
+ Return the interior of a given tensor as a 1D array.
94
+
95
+ Parameters
96
+ ----------
97
+ tensor : NDArray
98
+ The tensor of which the interior is to be taken.
99
+ order : int, optional
100
+ The number of elements to be removed from each direction in each dimension.
101
+
102
+ Returns
103
+ -------
104
+ NDArray
105
+ The one dimensional array containing all the elements of the interior in the
106
+ input tensor.
107
+
108
+ """
109
+ mask = np.zeros_like(tensor, dtype=bool)
110
+ interior_slices = tuple(slice(order, -order) for _ in range(tensor.ndim))
111
+ mask[interior_slices] = True
112
+ return tensor[mask]
113
+
114
+
115
+ def tensor_boundary[DType: np.number](
116
+ tensor: NDArray[DType], order: int = 1
117
+ ) -> NDArray[DType]:
118
+ """
119
+ Return the boundary of a given tensor as a 1D array.
120
+
121
+ Parameters
122
+ ----------
123
+ tensor : NDArray
124
+ The tensor of which the boundary is to be taken.
125
+ order : int, optional
126
+ The number of elements to be kept from each direction in each dimension.
127
+
128
+ Returns
129
+ -------
130
+ NDArray
131
+ The one dimensional array containing all the elements of boundary in the
132
+ input tensor.
133
+
134
+ """
135
+ mask = np.ones_like(tensor, dtype=bool)
136
+ interior_slices = tuple(slice(order, -order) for _ in range(tensor.ndim))
137
+ mask[interior_slices] = False
138
+ return tensor[mask]
@@ -0,0 +1,10 @@
1
+ from typing import SupportsIndex
2
+
3
+ import numpy as np
4
+
5
+ type Vector[T: np.floating] = np.ndarray[tuple[int], np.dtype[T]]
6
+ type Matrix[T: np.floating] = np.ndarray[tuple[int, int], np.dtype[T]]
7
+ type NDArray[T: np.number] = np.ndarray[tuple[int, ...], np.dtype[T]]
8
+
9
+
10
+ type Index1D = SupportsIndex | slice[SupportsIndex | None]