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 | Source |
---|
29 | ------ |
---|
30 | |
---|
31 | `power_law.py <https://github.com/SasView/sasmodels/blob/master/sasmodels/models/power_law.py>`_ |
---|
32 | |
---|
33 | Authorship and Verification |
---|
34 | ---------------------------- |
---|
35 | |
---|
36 | * **Author:** |
---|
37 | * **Last Modified by:** |
---|
38 | * **Last Reviewed by:** |
---|
39 | * **Source added by :** Steve King **Date:** March 25, 2019 |
---|
40 | """ |
---|
41 | |
---|
42 | import numpy as np |
---|
43 | from numpy import inf, errstate |
---|
44 | |
---|
45 | name = "power_law" |
---|
46 | title = "Simple power law with a flat background" |
---|
47 | |
---|
48 | description = """ |
---|
49 | Evaluates the function |
---|
50 | I(q) = scale * q^(-power) + background |
---|
51 | NB: enter power as a positive number! |
---|
52 | """ |
---|
53 | category = "shape-independent" |
---|
54 | |
---|
55 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
56 | parameters = [["power", "", 4.0, [-inf, inf], "", "Power law exponent"]] |
---|
57 | |
---|
58 | # NB: Scale and Background are implicit parameters on every model |
---|
59 | def Iq(q, power): |
---|
60 | # pylint: disable=missing-docstring |
---|
61 | with errstate(divide='ignore'): |
---|
62 | result = q**-power |
---|
63 | return result |
---|
64 | Iq.vectorized = True # Iq accepts an array of q values |
---|
65 | |
---|
66 | def random(): |
---|
67 | """Return a random parameter set for the model.""" |
---|
68 | power = np.random.uniform(1, 6) |
---|
69 | pars = dict( |
---|
70 | scale=0.1**power*10**np.random.uniform(-4, 2), |
---|
71 | power=power, |
---|
72 | ) |
---|
73 | return pars |
---|
74 | |
---|
75 | demo = dict(scale=1.0, power=4.0, background=0.0) |
---|
76 | |
---|
77 | tests = [ |
---|
78 | [{'scale': 1.0, 'power': 4.0, 'background' : 0.0}, |
---|
79 | [0.0106939, 0.469418], [7.64644e+07, 20.5949]], |
---|
80 | ] |
---|