1 | import math |
---|
2 | |
---|
3 | def toX(x, y=None): |
---|
4 | """ |
---|
5 | This function is used to load value on Plottable.View |
---|
6 | |
---|
7 | :param x: Float value |
---|
8 | |
---|
9 | :return: x |
---|
10 | |
---|
11 | """ |
---|
12 | return x |
---|
13 | |
---|
14 | def toX_pos(x, y=None): |
---|
15 | """ |
---|
16 | This function is used to load value on Plottable.View |
---|
17 | |
---|
18 | :param x: Float value |
---|
19 | |
---|
20 | :return: x |
---|
21 | |
---|
22 | """ |
---|
23 | if not x > 0: |
---|
24 | raise ValueError, "Transformation only accepts positive values." |
---|
25 | else: |
---|
26 | return x |
---|
27 | |
---|
28 | def toX2(x, y=None): |
---|
29 | """ |
---|
30 | This function is used to load value on Plottable.View |
---|
31 | |
---|
32 | Calculate x^(2) |
---|
33 | |
---|
34 | :param x: float value |
---|
35 | |
---|
36 | """ |
---|
37 | return x * x |
---|
38 | |
---|
39 | def fromX2(x, y=None): |
---|
40 | """ |
---|
41 | This function is used to load value on Plottable.View |
---|
42 | Calculate square root of x |
---|
43 | |
---|
44 | :param x: float value |
---|
45 | |
---|
46 | """ |
---|
47 | if not x >= 0 : |
---|
48 | raise ValueError, "square root of a negative value " |
---|
49 | else: |
---|
50 | return math.sqrt(x) |
---|
51 | |
---|
52 | def toLogX(x, y=None): |
---|
53 | """ |
---|
54 | This function is used to load value on Plottable.View |
---|
55 | calculate log x |
---|
56 | |
---|
57 | :param x: float value |
---|
58 | |
---|
59 | """ |
---|
60 | if not x > 0: |
---|
61 | raise ValueError, "Log(x)of a negative value " |
---|
62 | else: |
---|
63 | return math.log(x) |
---|
64 | |
---|
65 | def toOneOverX(x, y=None): |
---|
66 | """ |
---|
67 | """ |
---|
68 | if x != 0: |
---|
69 | return 1/x |
---|
70 | else: |
---|
71 | raise ValueError,"cannot divide by zero" |
---|
72 | |
---|
73 | def toOneOverSqrtX(y, x=None): |
---|
74 | """ |
---|
75 | """ |
---|
76 | if y > 0: |
---|
77 | return 1/math.sqrt(y) |
---|
78 | else: |
---|
79 | raise ValueError,"transform.toOneOverSqrtX: cannot be computed" |
---|
80 | |
---|
81 | def toLogYX2(y, x): |
---|
82 | """ |
---|
83 | """ |
---|
84 | if (y *(x**2)) > 0: |
---|
85 | return math.log(y * (x**2)) |
---|
86 | else: |
---|
87 | raise ValueError,"transform.toLogYX2: cannot be computed" |
---|
88 | |
---|
89 | def toLogYX4(y, x): |
---|
90 | """ |
---|
91 | """ |
---|
92 | if (math.pow(x, 4) * y) > 0: |
---|
93 | return math.log(math.pow(x,4) * y) |
---|
94 | else: |
---|
95 | raise ValueError,"transform.toLogYX4: input error" |
---|
96 | |
---|
97 | def toLogXY(y, x): |
---|
98 | """ |
---|
99 | This function is used to load value on Plottable.View |
---|
100 | calculate log x |
---|
101 | |
---|
102 | :param x: float value |
---|
103 | |
---|
104 | """ |
---|
105 | if not (x * y) > 0: |
---|
106 | raise ValueError, "Log(X*Y)of a negative value " |
---|
107 | else: |
---|
108 | return math.log(x * y) |
---|
109 | |
---|
110 | def errToX(x, y=None, dx=None, dy=None): |
---|
111 | """ |
---|
112 | calculate error of x**2 |
---|
113 | |
---|
114 | :param x: float value |
---|
115 | :param dx: float value |
---|
116 | |
---|
117 | """ |
---|
118 | if dx == None: |
---|
119 | dx = 0 |
---|
120 | return dx |
---|
121 | |
---|
122 | def errToX_pos(x, y=None, dx=None, dy=None): |
---|
123 | """ |
---|
124 | calculate error of x**2 |
---|
125 | |
---|
126 | :param x: float value |
---|
127 | :param dx: float value |
---|
128 | |
---|
129 | """ |
---|
130 | if dx == None: |
---|
131 | dx = 0 |
---|
132 | return dx |
---|
133 | |
---|
134 | def errToX2(x, y=None, dx=None, dy=None): |
---|
135 | """ |
---|
136 | calculate error of x**2 |
---|
137 | |
---|
138 | :param x: float value |
---|
139 | :param dx: float value |
---|
140 | |
---|
141 | """ |
---|
142 | if dx != None: |
---|
143 | err = 2 * x * dx |
---|
144 | return math.fabs(err) |
---|
145 | else: |
---|
146 | return 0.0 |
---|
147 | |
---|
148 | def errFromX2(x, y=None, dx=None, dy=None): |
---|
149 | """ |
---|
150 | calculate error of sqrt(x) |
---|
151 | |
---|
152 | :param x: float value |
---|
153 | :param dx: float value |
---|
154 | |
---|
155 | """ |
---|
156 | if (x > 0): |
---|
157 | if(dx != None): |
---|
158 | err = dx/(2 * math.sqrt(x)) |
---|
159 | else: |
---|
160 | err = 0 |
---|
161 | return math.fabs(err) |
---|
162 | else: |
---|
163 | msg = "transform.errFromX2: can't compute error of negative x" |
---|
164 | raise ValueError, msg |
---|
165 | |
---|
166 | def errToLog10X(x, y=None, dx=None, dy=None): |
---|
167 | """ |
---|
168 | calculate error of Log(x) |
---|
169 | |
---|
170 | :param x: float value |
---|
171 | :param dx: float value |
---|
172 | |
---|
173 | """ |
---|
174 | if dx == None: |
---|
175 | dx = 0 |
---|
176 | |
---|
177 | # Check that the point on the graph is positive |
---|
178 | # within errors |
---|
179 | if not (x - dx) > 0: |
---|
180 | msg = "Transformation does not accept" |
---|
181 | msg += " point that are consistent with zero." |
---|
182 | raise ValueError, msg |
---|
183 | if x != 0: |
---|
184 | dx = dx /(x * math.log(10)) |
---|
185 | else: |
---|
186 | raise ValueError, "errToLogX: divide by zero" |
---|
187 | return dx |
---|
188 | |
---|
189 | def errToLogX(x, y=None, dx=None, dy=None): |
---|
190 | """ |
---|
191 | calculate error of Log(x) |
---|
192 | |
---|
193 | :param x: float value |
---|
194 | :param dx: float value |
---|
195 | |
---|
196 | """ |
---|
197 | if dx == None: |
---|
198 | dx = 0 |
---|
199 | |
---|
200 | # Check that the x point on the graph is zero |
---|
201 | if x != 0: |
---|
202 | dx = dx/x |
---|
203 | else: |
---|
204 | raise ValueError, "errToLogX: divide by zero" |
---|
205 | return dx |
---|
206 | |
---|
207 | def errToYX2(x, y, dx=None, dy=None): |
---|
208 | """ |
---|
209 | """ |
---|
210 | if dx == None: |
---|
211 | dx = 0 |
---|
212 | if dy == None: |
---|
213 | dy = 0 |
---|
214 | err = math.sqrt((2 * x * y * dx)**2 + ((x**2) * dy)**2) |
---|
215 | return err |
---|
216 | |
---|
217 | def errToLogXY(x, y, dx=None, dy=None): |
---|
218 | """ |
---|
219 | calculate error of Log(xy) |
---|
220 | |
---|
221 | """ |
---|
222 | # Check that the point on the graph is positive |
---|
223 | # within errors |
---|
224 | if (not (x - dx) > 0) or (not (y - dy) > 0): |
---|
225 | msg = "Transformation does not accept point " |
---|
226 | msg += " that are consistent with zero." |
---|
227 | raise ValueError, msg |
---|
228 | if (x != 0) and (y != 0): |
---|
229 | if dx == None: |
---|
230 | dx = 0 |
---|
231 | if dy == None: |
---|
232 | dy = 0 |
---|
233 | err = (dx/x)**2 + (dy/y)**2 |
---|
234 | else: |
---|
235 | raise ValueError, "cannot compute this error" |
---|
236 | |
---|
237 | return math.sqrt(math.fabs(err)) |
---|
238 | |
---|
239 | def errToLogYX2(x, y, dx=None, dy=None): |
---|
240 | """ |
---|
241 | calculate error of Log(yx**2) |
---|
242 | |
---|
243 | """ |
---|
244 | # Check that the point on the graph is positive |
---|
245 | # within errors |
---|
246 | if (not (x - dx) > 0) or (not (y - dy) > 0): |
---|
247 | msg = "Transformation does not accept point" |
---|
248 | msg += " that are consistent with zero." |
---|
249 | raise ValueError, msg |
---|
250 | if (x > 0) and (y > 0): |
---|
251 | if (dx == None): |
---|
252 | dx = 0 |
---|
253 | if (dy == None): |
---|
254 | dy = 0 |
---|
255 | err = (2.0*dx/x)**2 + (dy/y)**2 |
---|
256 | else: |
---|
257 | raise ValueError, "cannot compute this error" |
---|
258 | return math.sqrt(math.fabs(err)) |
---|
259 | |
---|
260 | def errOneOverX(x, y=None, dx=None, dy=None): |
---|
261 | """ |
---|
262 | calculate error on 1/x |
---|
263 | |
---|
264 | """ |
---|
265 | if (x != 0): |
---|
266 | if dx == None: |
---|
267 | dx = 0 |
---|
268 | err = dx/x**2 |
---|
269 | else: |
---|
270 | raise ValueError,"Cannot compute this error" |
---|
271 | return math.fabs(err) |
---|
272 | |
---|
273 | def errOneOverSqrtX(x, y=None, dx=None, dy=None): |
---|
274 | """ |
---|
275 | Calculate error on 1/sqrt(x) |
---|
276 | |
---|
277 | """ |
---|
278 | if (x > 0): |
---|
279 | if dx == None: |
---|
280 | dx = 0 |
---|
281 | err = -1/2*math.pow(x, -3.0/2.0) * dx |
---|
282 | else: |
---|
283 | raise ValueError, "Cannot compute this error" |
---|
284 | return math.fabs(err) |
---|
285 | |
---|
286 | def errToLogYX4(x, y=None, dx=None, dy=None): |
---|
287 | """ |
---|
288 | error for ln(y*x^(4)) |
---|
289 | |
---|
290 | :param x: float value |
---|
291 | |
---|
292 | """ |
---|
293 | # Check that the point on the graph is positive |
---|
294 | # within errors |
---|
295 | if (not (x - dx) > 0) or (not (y - dy) > 0): |
---|
296 | msg = "Transformation does not accept point " |
---|
297 | msg += " that are consistent with zero." |
---|
298 | raise ValueError, msg |
---|
299 | if dx == None: |
---|
300 | dx = 0 |
---|
301 | if dy == None: |
---|
302 | dy = 0 |
---|
303 | err = math.sqrt((4.0 * dx/x)**2 + (dy/y)**2) |
---|
304 | return err |
---|
305 | |
---|
306 | |
---|