Skip to content

Commit 66b56aa

Browse files
committed
readme
1 parent fda1f5b commit 66b56aa

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ If you use this library in your publications please consider citing:
1919

2020
* U. Wolff, [Monte Carlo errors with less errors](https://inspirehep.net/literature/621085). *Comput.Phys.Commun.* 156 (2004) 143-153.
2121
* S. Schaefer, R. Sommer, F. Virotta, [Critical slowing down and error analysis in lattice QCD simulations](https://inspirehep.net/literature/871175). *Nucl.Phys.B* 845 (2011) 93-119.
22-
* M. Bruno, R. Sommer, In preparation.
22+
* M. Bruno, R. Sommer, [On fits to correlated and auto-correlated data](https://inspirehep.net/literature/2157883) *Comput.Phys.Commun.* 285 (2023) 108643.
2323

2424
### Authors
2525

26-
Copyright (C) 2020-2021, Mattia Bruno
26+
Copyright (C) 2020-2023, Mattia Bruno
2727

2828
## Installation
2929

@@ -45,8 +45,7 @@ help(pyobs.observable)
4545

4646
The library can also be installed from a local clone of
4747
the repository in *developer mode*, as described in the
48-
documentation. Recompilation of the C++ extensions might
49-
be necessary in this case.
48+
documentation.
5049

5150
## Example
5251

scripts/generate-unary.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
dest='pyobs/tensor/unary.py'
2+
3+
ops = {
4+
"log": ['Natural logarithm', 'logarithm', 'numpy.log', 'numpy.reciprocal'],
5+
"exp": ['exponential', 'exponential', 'numpy.exp', 'numpy.exp'],
6+
"sin": ['sine', 'sine', 'numpy.sin', 'numpy.cos'],
7+
"arcsin": ['Inverse sine', 'arcsine', 'numpy.arcsin', 'lambda x: (1-x*x)**(-0.5)'],
8+
"cos": ['cosine', 'cosine', 'numpy.cos', 'lambda x: -numpy.sin(x)'],
9+
"arccos": ['Inverse sine', 'arcsine', 'numpy.arcsin', 'lambda x: -(1-x*x)**(-0.5)'],
10+
"tan": ['tangent', 'tangent', 'numpy.tan', 'lambda x: 1 / numpy.cos(x) ** 2'],
11+
"arctan": ['inverser tangent', 'arctangent', 'numpy.arctan', 'lambda x: 1 / (1 + x*x)'],
12+
"cosh": ['Hyperbolic cosine', 'hyperbolic cosine', 'numpy.cosh', 'numpy.sinh'],
13+
"arccosh": ['inverse Hyperbolic cosine', 'inverse hyperbolic cosine', 'numpy.arccosh', 'lambda x: (x*x - 1)**(-0.5)'],
14+
"sinh": ['Hyperbolic sine', 'hyperbolic sine', 'numpy.sinh', 'numpy.cosh'],
15+
"arcsinh": ['inverse Hyperbolic sine', 'inverse hyperbolic sine', 'numpy.arcsinh', 'lambda x: (x*x + 1)**(-0.5)'],
16+
}
17+
18+
19+
def make_py(key, l):
20+
return f'''
21+
def {key}(x):
22+
"""
23+
Return the {l[0]} element-wise.
24+
25+
Parameters:
26+
x (obs): input observable
27+
28+
Returns:
29+
obs : the {l[1]} of the input observable, element-wise.
30+
31+
Examples:
32+
>>> y = pyobs.{key}(x)
33+
"""
34+
return __unary(x, {l[2]}, {l[3]})
35+
36+
'''
37+
38+
with open(dest,'w') as f:
39+
f.write("""#################################################################################
40+
#
41+
# unary.py: definitions of unary operations
42+
# Copyright (C) 2020-2023 Mattia Bruno
43+
#
44+
# This program is free software; you can redistribute it and/or
45+
# modify it under the terms of the GNU General Public License
46+
# as published by the Free Software Foundation; either version 2
47+
# of the License, or (at your option) any later version.
48+
#
49+
# This program is distributed in the hope that it will be useful,
50+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
51+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52+
# GNU General Public License for more details.
53+
#
54+
# You should have received a copy of the GNU General Public License
55+
# along with this program; if not, write to the Free Software
56+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
57+
#
58+
#################################################################################
59+
import pyobs
60+
import numpy
61+
62+
def __unary(x, f, df):
63+
new_mean = f(x.mean)
64+
aux = df(x.mean)
65+
g = pyobs.gradient(lambda xx: xx * aux, x.mean, gtype="diag")
66+
return pyobs.derobs([x], new_mean, [g])
67+
68+
""")
69+
70+
f.write('\n\n')
71+
72+
all = '__all__ = ['
73+
for key in ops:
74+
all += f'"{key}",'
75+
all += '\n]\n\n'
76+
f.write(all)
77+
78+
for key in ops:
79+
f.write(make_py(key, ops[key]))

tests/tensor/unary_special.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pyobs
2+
import numpy
3+
import scipy
4+
5+
T=32
6+
L=16
7+
mass=0.25
8+
p=0.0
9+
10+
xax=range(T//2)
11+
corr_ex = [pyobs.qft.free_scalar.Cphiphi(x,mass,p,T,L)+2.0 for x in xax]
12+
cov_ex = pyobs.qft.free_scalar.cov_Cphiphi(mass,p,T,L)[0:T//2,0:T//2]
13+
14+
N=1000
15+
tau=2.0
16+
rng = pyobs.random.generator('unary')
17+
data = rng.markov_chain(corr_ex,cov_ex/N,tau,N)
18+
19+
corr = pyobs.observable()
20+
corr.create(f'm{mass:g}-{L}x{T}', data.flatten(), shape=(len(xax),))
21+
print(corr)
22+
[c,dc] = corr.error()
23+
24+
mat = pyobs.reshape(corr/corr.mean[0]/1.01, (T//8,T//8))
25+
print(mat)
26+
flist = pyobs.tensor.unary.__all__
27+
flist.remove('arccosh')
28+
29+
for f in flist:
30+
[v0, e0] = pyobs.__dict__[f](mat).error()
31+
mean = numpy.__dict__[f](mat.mean)
32+
g = pyobs.num_grad(mat, numpy.__dict__[f])
33+
g0 = pyobs.gradient(g)
34+
[v1, e1] = pyobs.derobs([mat], mean, [g0]).error()
35+
assert numpy.all(numpy.fabs(e1-e0) < 2e-9)
36+
37+
mat = pyobs.reshape(corr, (T//8,T//8))
38+
flist = ['besselk']
39+
slist = ['kv']
40+
args = [1.0]
41+
42+
for i in range(len(flist)):
43+
[v0, e0] = pyobs.__dict__[flist[i]](args[i], mat).error()
44+
f = lambda x: scipy.special.__dict__[slist[i]](args[i], x)
45+
mean = f(mat.mean)
46+
g = pyobs.num_grad(mat, f)
47+
g0 = pyobs.gradient(g)
48+
[v1, e1] = pyobs.derobs([mat], mean, [g0]).error()
49+
assert numpy.all(numpy.fabs(e1-e0) < 1e-10)
50+

0 commit comments

Comments
 (0)