1 | import math |
---|
2 | |
---|
3 | |
---|
4 | def toX(x, y=None): |
---|
5 | """ |
---|
6 | This function is used to load value on Plottable.View |
---|
7 | |
---|
8 | :param x: Float value |
---|
9 | |
---|
10 | :return: x |
---|
11 | |
---|
12 | """ |
---|
13 | return x |
---|
14 | |
---|
15 | |
---|
16 | def toX_pos(x, y=None): |
---|
17 | """ |
---|
18 | This function is used to load value on Plottable.View |
---|
19 | |
---|
20 | :param x: Float value |
---|
21 | |
---|
22 | :return: x |
---|
23 | |
---|
24 | """ |
---|
25 | if not x > 0: |
---|
26 | raise ValueError, "Transformation only accepts positive values." |
---|
27 | else: |
---|
28 | return x |
---|
29 | |
---|
30 | |
---|
31 | def toX2(x, y=None): |
---|
32 | """ |
---|
33 | This function is used to load value on Plottable.View |
---|
34 | |
---|
35 | Calculate x^(2) |
---|
36 | |
---|
37 | :param x: float value |
---|
38 | |
---|
39 | """ |
---|
40 | return x * x |
---|
41 | |
---|
42 | |
---|
43 | def fromX2(x, y=None): |
---|
44 | """ |
---|
45 | This function is used to load value on Plottable.View |
---|
46 | Calculate square root of x |
---|
47 | |
---|
48 | :param x: float value |
---|
49 | |
---|
50 | """ |
---|
51 | if not x >= 0: |
---|
52 | raise ValueError, "square root of a negative value " |
---|
53 | else: |
---|
54 | return math.sqrt(x) |
---|
55 | |
---|
56 | |
---|
57 | def toX4(x, y=None): |
---|
58 | """ |
---|
59 | This function is used to load value on Plottable.View |
---|
60 | |
---|
61 | Calculate x^(4) |
---|
62 | |
---|
63 | :param x: float value |
---|
64 | |
---|
65 | """ |
---|
66 | return x * x * x * x |
---|
67 | |
---|
68 | |
---|
69 | def fromX4(x, y=None): |
---|
70 | """ |
---|
71 | This function is used to load value on Plottable.View |
---|
72 | Calculate square root of x |
---|
73 | |
---|
74 | :param x: float value |
---|
75 | |
---|
76 | """ |
---|
77 | if not x >= 0: |
---|
78 | raise ValueError, "double square root of a negative value " |
---|
79 | else: |
---|
80 | return math.sqrt(math.sqrt(x)) |
---|
81 | |
---|
82 | |
---|
83 | def toLogX(x, y=None): |
---|
84 | """ |
---|
85 | This function is used to load value on Plottable.View |
---|
86 | calculate log x |
---|
87 | |
---|
88 | :param x: float value |
---|
89 | |
---|
90 | """ |
---|
91 | if not x > 0: |
---|
92 | raise ValueError, "Log(x)of a negative value " |
---|
93 | else: |
---|
94 | return math.log(x) |
---|
95 | |
---|
96 | def toOneOverX(x, y=None): |
---|
97 | """ |
---|
98 | """ |
---|
99 | if x != 0: |
---|
100 | return 1/x |
---|
101 | else: |
---|
102 | raise ValueError, "cannot divide by zero" |
---|
103 | |
---|
104 | |
---|
105 | def toOneOverSqrtX(y, x=None): |
---|
106 | """ |
---|
107 | """ |
---|
108 | if y > 0: |
---|
109 | return 1/math.sqrt(y) |
---|
110 | else: |
---|
111 | raise ValueError, "transform.toOneOverSqrtX: cannot be computed" |
---|
112 | |
---|
113 | |
---|
114 | def toLogYX2(y, x): |
---|
115 | """ |
---|
116 | """ |
---|
117 | if (y * (x**2)) > 0: |
---|
118 | return math.log(y * (x**2)) |
---|
119 | else: |
---|
120 | raise ValueError, "transform.toLogYX2: cannot be computed" |
---|
121 | |
---|
122 | |
---|
123 | def toLogYX4(y, x): |
---|
124 | """ |
---|
125 | """ |
---|
126 | if (math.pow(x, 4) * y) > 0: |
---|
127 | return math.log(math.pow(x,4) * y) |
---|
128 | else: |
---|
129 | raise ValueError,"transform.toLogYX4: input error" |
---|
130 | |
---|
131 | |
---|
132 | def toYX4(y, x): |
---|
133 | """ |
---|
134 | """ |
---|
135 | return math.pow(x, 4) * y |
---|
136 | |
---|
137 | |
---|
138 | def toLogXY(y, x): |
---|
139 | """ |
---|
140 | This function is used to load value on Plottable.View |
---|
141 | calculate log x |
---|
142 | |
---|
143 | :param x: float value |
---|
144 | |
---|
145 | """ |
---|
146 | if not (x * y) > 0: |
---|
147 | raise ValueError, "Log(X*Y)of a negative value " |
---|
148 | else: |
---|
149 | return math.log(x * y) |
---|
150 | |
---|
151 | |
---|
152 | def errToX(x, y=None, dx=None, dy=None): |
---|
153 | """ |
---|
154 | calculate error of x**2 |
---|
155 | |
---|
156 | :param x: float value |
---|
157 | :param dx: float value |
---|
158 | |
---|
159 | """ |
---|
160 | if dx == None: |
---|
161 | dx = 0 |
---|
162 | return dx |
---|
163 | |
---|
164 | |
---|
165 | def errToX_pos(x, y=None, dx=None, dy=None): |
---|
166 | """ |
---|
167 | calculate error of x**2 |
---|
168 | |
---|
169 | :param x: float value |
---|
170 | :param dx: float value |
---|
171 | |
---|
172 | """ |
---|
173 | if dx == None: |
---|
174 | dx = 0 |
---|
175 | return dx |
---|
176 | |
---|
177 | |
---|
178 | def errToX2(x, y=None, dx=None, dy=None): |
---|
179 | """ |
---|
180 | calculate error of x**2 |
---|
181 | |
---|
182 | :param x: float value |
---|
183 | :param dx: float value |
---|
184 | |
---|
185 | """ |
---|
186 | if dx != None: |
---|
187 | err = 2 * x * dx |
---|
188 | return math.fabs(err) |
---|
189 | else: |
---|
190 | return 0.0 |
---|
191 | |
---|
192 | |
---|
193 | def errFromX2(x, y=None, dx=None, dy=None): |
---|
194 | """ |
---|
195 | calculate error of sqrt(x) |
---|
196 | |
---|
197 | :param x: float value |
---|
198 | :param dx: float value |
---|
199 | |
---|
200 | """ |
---|
201 | if (x > 0): |
---|
202 | if(dx != None): |
---|
203 | err = dx / (2 * math.sqrt(x)) |
---|
204 | else: |
---|
205 | err = 0 |
---|
206 | return math.fabs(err) |
---|
207 | else: |
---|
208 | msg = "transform.errFromX2: can't compute error of negative x" |
---|
209 | raise ValueError, msg |
---|
210 | |
---|
211 | |
---|
212 | def errToX4(x, y=None, dx=None, dy=None): |
---|
213 | """ |
---|
214 | calculate error of x**4 |
---|
215 | |
---|
216 | :param x: float value |
---|
217 | :param dx: float value |
---|
218 | |
---|
219 | """ |
---|
220 | if dx != None: |
---|
221 | err = 4 * math.pow(x, 3) * dx |
---|
222 | return math.fabs(err) |
---|
223 | else: |
---|
224 | return 0.0 |
---|
225 | |
---|
226 | |
---|
227 | def errFromX4(x, y=None, dx=None, dy=None): |
---|
228 | """ |
---|
229 | calculate error of x^1/4 |
---|
230 | |
---|
231 | :param x: float value |
---|
232 | :param dx: float value |
---|
233 | |
---|
234 | """ |
---|
235 | if (x > 0): |
---|
236 | if(dx != None): |
---|
237 | err = dx / (4 * math.pow(x, 3/4)) |
---|
238 | else: |
---|
239 | err = 0 |
---|
240 | return math.fabs(err) |
---|
241 | else: |
---|
242 | msg = "transform.errFromX4: can't compute error of negative x" |
---|
243 | raise ValueError, msg |
---|
244 | |
---|
245 | |
---|
246 | def errToLog10X(x, y=None, dx=None, dy=None): |
---|
247 | """ |
---|
248 | calculate error of Log(x) |
---|
249 | |
---|
250 | :param x: float value |
---|
251 | :param dx: float value |
---|
252 | |
---|
253 | """ |
---|
254 | if dx == None: |
---|
255 | dx = 0 |
---|
256 | |
---|
257 | # Check that the point on the graph is positive |
---|
258 | # within errors |
---|
259 | if not (x - dx) > 0: |
---|
260 | msg = "Transformation does not accept" |
---|
261 | msg += " point that are consistent with zero." |
---|
262 | raise ValueError, msg |
---|
263 | if x != 0: |
---|
264 | dx = dx / (x * math.log(10)) |
---|
265 | else: |
---|
266 | raise ValueError, "errToLogX: divide by zero" |
---|
267 | return dx |
---|
268 | |
---|
269 | |
---|
270 | def errToLogX(x, y=None, dx=None, dy=None): |
---|
271 | """ |
---|
272 | calculate error of Log(x) |
---|
273 | |
---|
274 | :param x: float value |
---|
275 | :param dx: float value |
---|
276 | |
---|
277 | """ |
---|
278 | if dx == None: |
---|
279 | dx = 0 |
---|
280 | |
---|
281 | # Check that the x point on the graph is zero |
---|
282 | if x != 0: |
---|
283 | dx = dx/x |
---|
284 | else: |
---|
285 | raise ValueError, "errToLogX: divide by zero" |
---|
286 | return dx |
---|
287 | |
---|
288 | |
---|
289 | def errToYX2(x, y, dx=None, dy=None): |
---|
290 | """ |
---|
291 | """ |
---|
292 | if dx == None: |
---|
293 | dx = 0 |
---|
294 | if dy == None: |
---|
295 | dy = 0 |
---|
296 | err = math.sqrt((2 * x * y * dx)**2 + ((x**2) * dy)**2) |
---|
297 | return err |
---|
298 | |
---|
299 | |
---|
300 | def errToLogXY(x, y, dx=None, dy=None): |
---|
301 | """ |
---|
302 | calculate error of Log(xy) |
---|
303 | |
---|
304 | """ |
---|
305 | # Check that the point on the graph is positive |
---|
306 | # within errors |
---|
307 | if (not (x - dx) > 0) or (not (y - dy) > 0): |
---|
308 | msg = "Transformation does not accept point " |
---|
309 | msg += " that are consistent with zero." |
---|
310 | raise ValueError, msg |
---|
311 | if (x != 0) and (y != 0): |
---|
312 | if dx == None: |
---|
313 | dx = 0 |
---|
314 | if dy == None: |
---|
315 | dy = 0 |
---|
316 | err = (dx/x)**2 + (dy/y)**2 |
---|
317 | else: |
---|
318 | raise ValueError, "cannot compute this error" |
---|
319 | |
---|
320 | return math.sqrt(math.fabs(err)) |
---|
321 | |
---|
322 | |
---|
323 | def errToLogYX2(x, y, dx=None, dy=None): |
---|
324 | """ |
---|
325 | calculate error of Log(yx**2) |
---|
326 | |
---|
327 | """ |
---|
328 | # Check that the point on the graph is positive |
---|
329 | # within errors |
---|
330 | if (not (x - dx) > 0) or (not (y - dy) > 0): |
---|
331 | msg = "Transformation does not accept point" |
---|
332 | msg += " that are consistent with zero." |
---|
333 | raise ValueError, msg |
---|
334 | if (x > 0) and (y > 0): |
---|
335 | if (dx == None): |
---|
336 | dx = 0 |
---|
337 | if (dy == None): |
---|
338 | dy = 0 |
---|
339 | err = (2.0*dx/x)**2 + (dy/y)**2 |
---|
340 | else: |
---|
341 | raise ValueError, "cannot compute this error" |
---|
342 | return math.sqrt(math.fabs(err)) |
---|
343 | |
---|
344 | |
---|
345 | def errOneOverX(x, y=None, dx=None, dy=None): |
---|
346 | """ |
---|
347 | calculate error on 1/x |
---|
348 | |
---|
349 | """ |
---|
350 | if (x != 0): |
---|
351 | if dx == None: |
---|
352 | dx = 0 |
---|
353 | err = dx/x**2 |
---|
354 | else: |
---|
355 | raise ValueError, "Cannot compute this error" |
---|
356 | return math.fabs(err) |
---|
357 | |
---|
358 | |
---|
359 | def errOneOverSqrtX(x, y=None, dx=None, dy=None): |
---|
360 | """ |
---|
361 | Calculate error on 1/sqrt(x) |
---|
362 | |
---|
363 | """ |
---|
364 | if (x > 0): |
---|
365 | if dx == None: |
---|
366 | dx = 0 |
---|
367 | err = -1/2*math.pow(x, -3.0/2.0) * dx |
---|
368 | else: |
---|
369 | raise ValueError, "Cannot compute this error" |
---|
370 | return math.fabs(err) |
---|
371 | |
---|
372 | |
---|
373 | def errToLogYX4(x, y=None, dx=None, dy=None): |
---|
374 | """ |
---|
375 | error for ln(y*x^(4)) |
---|
376 | |
---|
377 | :param x: float value |
---|
378 | |
---|
379 | """ |
---|
380 | # Check that the point on the graph is positive |
---|
381 | # within errors |
---|
382 | if (not (x - dx) > 0) or (not (y - dy) > 0): |
---|
383 | msg = "Transformation does not accept point " |
---|
384 | msg += " that are consistent with zero." |
---|
385 | raise ValueError, msg |
---|
386 | if dx == None: |
---|
387 | dx = 0 |
---|
388 | if dy == None: |
---|
389 | dy = 0 |
---|
390 | err = math.sqrt((4.0 * dx/x)**2 + (dy/y)**2) |
---|
391 | return err |
---|
392 | |
---|
393 | |
---|
394 | def errToYX4(x, y=None, dx=None, dy=None): |
---|
395 | """ |
---|
396 | error for (y*x^(4)) |
---|
397 | |
---|
398 | :param x: float value |
---|
399 | |
---|
400 | """ |
---|
401 | # Check that the point on the graph is positive |
---|
402 | # within errors |
---|
403 | |
---|
404 | if dx == None: |
---|
405 | dx = 0 |
---|
406 | if dy == None: |
---|
407 | dy = 0 |
---|
408 | err = math.sqrt((dy * pow(x, 4))**2 + (4 * y * dx * math.pow(x, 3))**2) |
---|
409 | return err |
---|