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 | def toYX2(y, x): |
---|
138 | """ |
---|
139 | """ |
---|
140 | return math.pow(x, 2) * y |
---|
141 | |
---|
142 | def toLogXY(y, x): |
---|
143 | """ |
---|
144 | This function is used to load value on Plottable.View |
---|
145 | calculate log x |
---|
146 | |
---|
147 | :param x: float value |
---|
148 | |
---|
149 | """ |
---|
150 | if not (x * y) > 0: |
---|
151 | raise ValueError, "Log(X*Y)of a negative value " |
---|
152 | else: |
---|
153 | return math.log(x * y) |
---|
154 | |
---|
155 | |
---|
156 | def errToX(x, y=None, dx=None, dy=None): |
---|
157 | """ |
---|
158 | calculate error of x**2 |
---|
159 | |
---|
160 | :param x: float value |
---|
161 | :param dx: float value |
---|
162 | |
---|
163 | """ |
---|
164 | if dx == None: |
---|
165 | dx = 0 |
---|
166 | return dx |
---|
167 | |
---|
168 | |
---|
169 | def errToX_pos(x, y=None, dx=None, dy=None): |
---|
170 | """ |
---|
171 | calculate error of x**2 |
---|
172 | |
---|
173 | :param x: float value |
---|
174 | :param dx: float value |
---|
175 | |
---|
176 | """ |
---|
177 | if dx == None: |
---|
178 | dx = 0 |
---|
179 | return dx |
---|
180 | |
---|
181 | |
---|
182 | def errToX2(x, y=None, dx=None, dy=None): |
---|
183 | """ |
---|
184 | calculate error of x**2 |
---|
185 | |
---|
186 | :param x: float value |
---|
187 | :param dx: float value |
---|
188 | |
---|
189 | """ |
---|
190 | if dx != None: |
---|
191 | err = 2 * x * dx |
---|
192 | return math.fabs(err) |
---|
193 | else: |
---|
194 | return 0.0 |
---|
195 | |
---|
196 | |
---|
197 | def errFromX2(x, y=None, dx=None, dy=None): |
---|
198 | """ |
---|
199 | calculate error of sqrt(x) |
---|
200 | |
---|
201 | :param x: float value |
---|
202 | :param dx: float value |
---|
203 | |
---|
204 | """ |
---|
205 | if x > 0: |
---|
206 | if dx != None: |
---|
207 | err = dx / (2 * math.sqrt(x)) |
---|
208 | else: |
---|
209 | err = 0 |
---|
210 | return math.fabs(err) |
---|
211 | else: |
---|
212 | msg = "transform.errFromX2: can't compute error of negative x" |
---|
213 | raise ValueError, msg |
---|
214 | |
---|
215 | |
---|
216 | def errToX4(x, y=None, dx=None, dy=None): |
---|
217 | """ |
---|
218 | calculate error of x**4 |
---|
219 | |
---|
220 | :param x: float value |
---|
221 | :param dx: float value |
---|
222 | |
---|
223 | """ |
---|
224 | if dx != None: |
---|
225 | err = 4 * math.pow(x, 3) * dx |
---|
226 | return math.fabs(err) |
---|
227 | else: |
---|
228 | return 0.0 |
---|
229 | |
---|
230 | |
---|
231 | def errFromX4(x, y=None, dx=None, dy=None): |
---|
232 | """ |
---|
233 | calculate error of x^1/4 |
---|
234 | |
---|
235 | :param x: float value |
---|
236 | :param dx: float value |
---|
237 | |
---|
238 | """ |
---|
239 | if x > 0: |
---|
240 | if dx != None: |
---|
241 | err = dx / (4 * math.pow(x, 3 / 4)) |
---|
242 | else: |
---|
243 | err = 0 |
---|
244 | return math.fabs(err) |
---|
245 | else: |
---|
246 | msg = "transform.errFromX4: can't compute error of negative x" |
---|
247 | raise ValueError, msg |
---|
248 | |
---|
249 | |
---|
250 | def errToLog10X(x, y=None, dx=None, dy=None): |
---|
251 | """ |
---|
252 | calculate error of Log(x) |
---|
253 | |
---|
254 | :param x: float value |
---|
255 | :param dx: float value |
---|
256 | |
---|
257 | """ |
---|
258 | if dx == None: |
---|
259 | dx = 0 |
---|
260 | |
---|
261 | # Check that the point on the graph is positive |
---|
262 | # within errors |
---|
263 | if not (x - dx) > 0: |
---|
264 | msg = "Transformation does not accept" |
---|
265 | msg += " point that are consistent with zero." |
---|
266 | raise ValueError, msg |
---|
267 | if x != 0: |
---|
268 | dx = dx / (x * math.log(10)) |
---|
269 | else: |
---|
270 | raise ValueError, "errToLogX: divide by zero" |
---|
271 | return dx |
---|
272 | |
---|
273 | |
---|
274 | def errToLogX(x, y=None, dx=None, dy=None): |
---|
275 | """ |
---|
276 | calculate error of Log(x) |
---|
277 | |
---|
278 | :param x: float value |
---|
279 | :param dx: float value |
---|
280 | |
---|
281 | """ |
---|
282 | if dx == None: |
---|
283 | dx = 0 |
---|
284 | |
---|
285 | # Check that the x point on the graph is zero |
---|
286 | if x != 0: |
---|
287 | dx = dx / x |
---|
288 | else: |
---|
289 | raise ValueError, "errToLogX: divide by zero" |
---|
290 | return dx |
---|
291 | |
---|
292 | |
---|
293 | def errToYX2(y, x, dy=None, dx=None): |
---|
294 | """ |
---|
295 | """ |
---|
296 | if dx == None: |
---|
297 | dx = 0 |
---|
298 | if dy == None: |
---|
299 | dy = 0 |
---|
300 | err = math.sqrt((2 * x * y * dx) ** 2 + ((x ** 2) * dy) ** 2) |
---|
301 | return err |
---|
302 | |
---|
303 | |
---|
304 | def errToLogXY(x, y, dx=None, dy=None): |
---|
305 | """ |
---|
306 | calculate error of Log(xy) |
---|
307 | |
---|
308 | """ |
---|
309 | # Check that the point on the graph is positive |
---|
310 | # within errors |
---|
311 | if not (x - dx) > 0 or not (y - dy) > 0: |
---|
312 | msg = "Transformation does not accept point " |
---|
313 | msg += " that are consistent with zero." |
---|
314 | raise ValueError, msg |
---|
315 | if x != 0 and y != 0: |
---|
316 | if dx == None: |
---|
317 | dx = 0 |
---|
318 | if dy == None: |
---|
319 | dy = 0 |
---|
320 | err = (dx / x) ** 2 + (dy / y) ** 2 |
---|
321 | else: |
---|
322 | raise ValueError, "cannot compute this error" |
---|
323 | |
---|
324 | return math.sqrt(math.fabs(err)) |
---|
325 | |
---|
326 | |
---|
327 | def errToLogYX2(y, x, dy=None, dx=None): |
---|
328 | """ |
---|
329 | calculate error of Log(yx**2) |
---|
330 | |
---|
331 | """ |
---|
332 | # Check that the point on the graph is positive |
---|
333 | # within errors |
---|
334 | if not (x - dx) > 0 or not (y - dy) > 0: |
---|
335 | msg = "Transformation does not accept point" |
---|
336 | msg += " that are consistent with zero." |
---|
337 | raise ValueError, msg |
---|
338 | if x > 0 and y > 0: |
---|
339 | if dx == None: |
---|
340 | dx = 0 |
---|
341 | if dy == None: |
---|
342 | dy = 0 |
---|
343 | err = (2.0 * dx / x) ** 2 + (dy / y) ** 2 |
---|
344 | else: |
---|
345 | raise ValueError, "cannot compute this error" |
---|
346 | return math.sqrt(math.fabs(err)) |
---|
347 | |
---|
348 | |
---|
349 | def errOneOverX(x, y=None, dx=None, dy=None): |
---|
350 | """ |
---|
351 | calculate error on 1/x |
---|
352 | |
---|
353 | """ |
---|
354 | if x != 0: |
---|
355 | if dx == None: |
---|
356 | dx = 0 |
---|
357 | err = dx / x ** 2 |
---|
358 | else: |
---|
359 | raise ValueError, "Cannot compute this error" |
---|
360 | return math.fabs(err) |
---|
361 | |
---|
362 | |
---|
363 | def errOneOverSqrtX(x, y=None, dx=None, dy=None): |
---|
364 | """ |
---|
365 | Calculate error on 1/sqrt(x) |
---|
366 | |
---|
367 | """ |
---|
368 | if x > 0: |
---|
369 | if dx == None: |
---|
370 | dx = 0 |
---|
371 | err = -1 / 2 * math.pow(x, -3.0 / 2.0) * dx |
---|
372 | else: |
---|
373 | raise ValueError, "Cannot compute this error" |
---|
374 | return math.fabs(err) |
---|
375 | |
---|
376 | |
---|
377 | def errToLogYX4(y, x, dy=None, dx=None): |
---|
378 | """ |
---|
379 | error for ln(y*x^(4)) |
---|
380 | |
---|
381 | :param x: float value |
---|
382 | |
---|
383 | """ |
---|
384 | # Check that the point on the graph is positive |
---|
385 | # within errors |
---|
386 | if (not (x - dx) > 0) or (not (y - dy) > 0): |
---|
387 | msg = "Transformation does not accept point " |
---|
388 | msg += " that are consistent with zero." |
---|
389 | raise ValueError, msg |
---|
390 | if dx == None: |
---|
391 | dx = 0 |
---|
392 | if dy == None: |
---|
393 | dy = 0 |
---|
394 | err = math.sqrt((4.0 * dx / x) ** 2 + (dy / y) ** 2) |
---|
395 | return err |
---|
396 | |
---|
397 | |
---|
398 | def errToYX4(y, x, dy=None, dx=None): |
---|
399 | """ |
---|
400 | error for (y*x^(4)) |
---|
401 | |
---|
402 | :param x: float value |
---|
403 | |
---|
404 | """ |
---|
405 | # Check that the point on the graph is positive |
---|
406 | # within errors |
---|
407 | |
---|
408 | if dx == None: |
---|
409 | dx = 0 |
---|
410 | if dy == None: |
---|
411 | dy = 0 |
---|
412 | err = math.sqrt((dy * pow(x, 4)) ** 2 + (4 * y * dx * math.pow(x, 3)) ** 2) |
---|
413 | return err |
---|
414 | |
---|