1 | #power_law model |
---|
2 | #conversion of PowerLawAbsModel.py |
---|
3 | #converted by Steve King, Dec 2015 |
---|
4 | |
---|
5 | r""" |
---|
6 | This model calculates a simple power law with a flat background. |
---|
7 | |
---|
8 | Definition |
---|
9 | ---------- |
---|
10 | |
---|
11 | .. math:: |
---|
12 | |
---|
13 | I(q) = \text{scale} \cdot q^{-\text{power}} + \text{background} |
---|
14 | |
---|
15 | Note the minus sign in front of the exponent. The exponent *power* |
---|
16 | should therefore be entered as a **positive** number for fitting. |
---|
17 | |
---|
18 | Also note that unlike many other models, *scale* in this model |
---|
19 | is NOT explicitly related to a volume fraction. Be careful if |
---|
20 | combining this model with other models. |
---|
21 | |
---|
22 | |
---|
23 | References |
---|
24 | ---------- |
---|
25 | |
---|
26 | None. |
---|
27 | """ |
---|
28 | |
---|
29 | import numpy as np |
---|
30 | from numpy import inf, errstate |
---|
31 | |
---|
32 | name = "power_law" |
---|
33 | title = "Simple power law with a flat background" |
---|
34 | |
---|
35 | description = """ |
---|
36 | Evaluates the function |
---|
37 | I(q) = scale * q^(-power) + background |
---|
38 | NB: enter power as a positive number! |
---|
39 | """ |
---|
40 | category = "shape-independent" |
---|
41 | |
---|
42 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
43 | parameters = [["power", "", 4.0, [-inf, inf], "", "Power law exponent"]] |
---|
44 | |
---|
45 | # NB: Scale and Background are implicit parameters on every model |
---|
46 | def Iq(q, power): |
---|
47 | # pylint: disable=missing-docstring |
---|
48 | with errstate(divide='ignore'): |
---|
49 | result = q**-power |
---|
50 | return result |
---|
51 | Iq.vectorized = True # Iq accepts an array of q values |
---|
52 | |
---|
53 | def random(): |
---|
54 | power = np.random.uniform(1, 6) |
---|
55 | pars = dict( |
---|
56 | scale=0.1**power*10**np.random.uniform(-4, 2), |
---|
57 | power=power, |
---|
58 | ) |
---|
59 | return pars |
---|
60 | |
---|
61 | demo = dict(scale=1.0, power=4.0, background=0.0) |
---|
62 | |
---|
63 | tests = [ |
---|
64 | [{'scale': 1.0, 'power': 4.0, 'background' : 0.0}, |
---|
65 | [0.0106939, 0.469418], [7.64644e+07, 20.5949]], |
---|
66 | ] |
---|