bs-python-utils 0.0.1__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.
@@ -0,0 +1,46 @@
1
+ """
2
+ sets up sparse integration over a Gaussian
3
+ """
4
+ from pathlib import Path
5
+
6
+ import numpy as np
7
+
8
+ from bs_python_utils.bsnputils import TwoArrays
9
+ from bs_python_utils.bsutils import bs_error_abort
10
+
11
+
12
+ def setup_sparse_gaussian(
13
+ ndims: int, iprec: int, GHsparsedir: str | None = None
14
+ ) -> TwoArrays:
15
+ """
16
+ get nodes and weights for sparse integration Ef(X) with X = N(0,1) in `ndims` dimensions
17
+
18
+ usage: nodes, weights = setup_sparse_gaussian(mdims, iprec); intf = f(nodes) @ weights
19
+
20
+ Args:
21
+ ndims: number of dimensions (1 to 5)
22
+ iprec: precision (must be 9, 13, or 17)
23
+
24
+ Returns:
25
+ a pair of arrays `nodes` and `weights`;
26
+ `nodes` has `ndims`-1 columns and weights is a vector
27
+ """
28
+ GHdir = (
29
+ Path.home() / "Dropbox" / "GHsparseGrids"
30
+ if GHsparsedir is None
31
+ else Path(GHsparsedir)
32
+ )
33
+ if iprec not in [9, 13, 17]:
34
+ bs_error_abort(
35
+ f"We only do sparse integration with precision 9, 13, or 17, not {iprec}"
36
+ )
37
+
38
+ if ndims in [1, 2, 3, 4, 5]:
39
+ grid = np.loadtxt(GHdir / f"GHsparseGrid{ndims}prec{iprec}.txt")
40
+ weights = grid[:, 0]
41
+ nodes = grid[:, 1:]
42
+ return nodes, weights
43
+ else:
44
+ bs_error_abort(
45
+ f"We only do sparse integration in one to five dimensions, not {ndims}"
46
+ )
@@ -0,0 +1,34 @@
1
+ """
2
+ personal library of Matplotlib utility programs.
3
+ """
4
+ import matplotlib.axes as axes
5
+
6
+ from bs_python_utils.bsutils import bs_error_abort
7
+
8
+
9
+ def ax_text(ax: axes.Axes, str_txt: str, x: float, y: float) -> axes.Axes:
10
+ """
11
+ annotate an ax with text in Matplotlib
12
+
13
+ Args:
14
+ ax: axis we want to annotate
15
+ str_txt: string of text
16
+ x: position in fraction of horizontal axis
17
+ y: position in fraction of vertical axis
18
+
19
+ Returns:
20
+ annotated ax
21
+ """
22
+ if not (isinstance(x, float) and 0 <= x <= 1):
23
+ bs_error_abort("x should be a number between 0.0 and 1.0")
24
+ if not (isinstance(y, float) and 0 <= y <= 1):
25
+ bs_error_abort("y should be a number between 0.0 and 1.0")
26
+ ax.text(
27
+ x,
28
+ y,
29
+ str_txt,
30
+ horizontalalignment="center",
31
+ verticalalignment="center",
32
+ transform=ax.transAxes,
33
+ )
34
+ return ax