[a9d5684] | 1 | import math |
---|
[3477478] | 2 | |
---|
| 3 | |
---|
[a9d5684] | 4 | def toX(x, y=None): |
---|
| 5 | """ |
---|
| 6 | This function is used to load value on Plottable.View |
---|
[3477478] | 7 | |
---|
[a9d5684] | 8 | :param x: Float value |
---|
[3477478] | 9 | |
---|
[a9d5684] | 10 | :return: x |
---|
[3477478] | 11 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 19 | |
---|
[a9d5684] | 20 | :param x: Float value |
---|
[3477478] | 21 | |
---|
[a9d5684] | 22 | :return: x |
---|
[3477478] | 23 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 34 | |
---|
[a9d5684] | 35 | Calculate x^(2) |
---|
[3477478] | 36 | |
---|
[a9d5684] | 37 | :param x: float value |
---|
[3477478] | 38 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 47 | |
---|
[a9d5684] | 48 | :param x: float value |
---|
[3477478] | 49 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 60 | |
---|
[a9d5684] | 61 | Calculate x^(4) |
---|
[3477478] | 62 | |
---|
[a9d5684] | 63 | :param x: float value |
---|
[3477478] | 64 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 73 | |
---|
[a9d5684] | 74 | :param x: float value |
---|
[3477478] | 75 | |
---|
[a9d5684] | 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)) |
---|
[3477478] | 81 | |
---|
[a9d5684] | 82 | |
---|
| 83 | def toLogX(x, y=None): |
---|
| 84 | """ |
---|
| 85 | This function is used to load value on Plottable.View |
---|
| 86 | calculate log x |
---|
[3477478] | 87 | |
---|
[a9d5684] | 88 | :param x: float value |
---|
[3477478] | 89 | |
---|
[a9d5684] | 90 | """ |
---|
| 91 | if not x > 0: |
---|
| 92 | raise ValueError, "Log(x)of a negative value " |
---|
| 93 | else: |
---|
| 94 | return math.log(x) |
---|
[3477478] | 95 | |
---|
[a9d5684] | 96 | def toOneOverX(x, y=None): |
---|
| 97 | """ |
---|
| 98 | """ |
---|
| 99 | if x != 0: |
---|
[3477478] | 100 | return 1 / x |
---|
[a9d5684] | 101 | else: |
---|
| 102 | raise ValueError, "cannot divide by zero" |
---|
[3477478] | 103 | |
---|
| 104 | |
---|
[a9d5684] | 105 | def toOneOverSqrtX(y, x=None): |
---|
| 106 | """ |
---|
| 107 | """ |
---|
| 108 | if y > 0: |
---|
[3477478] | 109 | return 1 / math.sqrt(y) |
---|
[a9d5684] | 110 | else: |
---|
| 111 | raise ValueError, "transform.toOneOverSqrtX: cannot be computed" |
---|
[3477478] | 112 | |
---|
| 113 | |
---|
[a9d5684] | 114 | def toLogYX2(y, x): |
---|
| 115 | """ |
---|
| 116 | """ |
---|
[3477478] | 117 | if (y * (x ** 2)) > 0: |
---|
| 118 | return math.log(y * (x ** 2)) |
---|
[a9d5684] | 119 | else: |
---|
| 120 | raise ValueError, "transform.toLogYX2: cannot be computed" |
---|
[3477478] | 121 | |
---|
| 122 | |
---|
[a9d5684] | 123 | def toLogYX4(y, x): |
---|
| 124 | """ |
---|
| 125 | """ |
---|
| 126 | if (math.pow(x, 4) * y) > 0: |
---|
[3477478] | 127 | return math.log(math.pow(x, 4) * y) |
---|
[a9d5684] | 128 | else: |
---|
[3477478] | 129 | raise ValueError, "transform.toLogYX4: input error" |
---|
| 130 | |
---|
| 131 | |
---|
[a9d5684] | 132 | def toYX4(y, x): |
---|
| 133 | """ |
---|
| 134 | """ |
---|
| 135 | return math.pow(x, 4) * y |
---|
| 136 | |
---|
[8817d07] | 137 | def toYX2(y, x): |
---|
| 138 | """ |
---|
| 139 | """ |
---|
| 140 | return math.pow(x, 2) * y |
---|
[a9d5684] | 141 | |
---|
| 142 | def toLogXY(y, x): |
---|
| 143 | """ |
---|
| 144 | This function is used to load value on Plottable.View |
---|
| 145 | calculate log x |
---|
[3477478] | 146 | |
---|
[a9d5684] | 147 | :param x: float value |
---|
[3477478] | 148 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 159 | |
---|
[a9d5684] | 160 | :param x: float value |
---|
| 161 | :param dx: float value |
---|
[3477478] | 162 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 172 | |
---|
[a9d5684] | 173 | :param x: float value |
---|
| 174 | :param dx: float value |
---|
[3477478] | 175 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 185 | |
---|
[a9d5684] | 186 | :param x: float value |
---|
| 187 | :param dx: float value |
---|
[3477478] | 188 | |
---|
[a9d5684] | 189 | """ |
---|
| 190 | if dx != None: |
---|
| 191 | err = 2 * x * dx |
---|
| 192 | return math.fabs(err) |
---|
| 193 | else: |
---|
| 194 | return 0.0 |
---|
[3477478] | 195 | |
---|
| 196 | |
---|
[a9d5684] | 197 | def errFromX2(x, y=None, dx=None, dy=None): |
---|
| 198 | """ |
---|
| 199 | calculate error of sqrt(x) |
---|
[3477478] | 200 | |
---|
[a9d5684] | 201 | :param x: float value |
---|
| 202 | :param dx: float value |
---|
[3477478] | 203 | |
---|
[a9d5684] | 204 | """ |
---|
[3477478] | 205 | if x > 0: |
---|
| 206 | if dx != None: |
---|
[a9d5684] | 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 |
---|
[3477478] | 219 | |
---|
[a9d5684] | 220 | :param x: float value |
---|
| 221 | :param dx: float value |
---|
[3477478] | 222 | |
---|
[a9d5684] | 223 | """ |
---|
[3477478] | 224 | if dx != None: |
---|
[a9d5684] | 225 | err = 4 * math.pow(x, 3) * dx |
---|
| 226 | return math.fabs(err) |
---|
| 227 | else: |
---|
| 228 | return 0.0 |
---|
[3477478] | 229 | |
---|
| 230 | |
---|
[a9d5684] | 231 | def errFromX4(x, y=None, dx=None, dy=None): |
---|
| 232 | """ |
---|
| 233 | calculate error of x^1/4 |
---|
[3477478] | 234 | |
---|
[a9d5684] | 235 | :param x: float value |
---|
| 236 | :param dx: float value |
---|
[3477478] | 237 | |
---|
[a9d5684] | 238 | """ |
---|
[3477478] | 239 | if x > 0: |
---|
| 240 | if dx != None: |
---|
| 241 | err = dx / (4 * math.pow(x, 3 / 4)) |
---|
[a9d5684] | 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 |
---|
[3477478] | 248 | |
---|
| 249 | |
---|
[a9d5684] | 250 | def errToLog10X(x, y=None, dx=None, dy=None): |
---|
| 251 | """ |
---|
| 252 | calculate error of Log(x) |
---|
[3477478] | 253 | |
---|
[a9d5684] | 254 | :param x: float value |
---|
| 255 | :param dx: float value |
---|
[3477478] | 256 | |
---|
[a9d5684] | 257 | """ |
---|
| 258 | if dx == None: |
---|
| 259 | dx = 0 |
---|
[3477478] | 260 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 272 | |
---|
| 273 | |
---|
[a9d5684] | 274 | def errToLogX(x, y=None, dx=None, dy=None): |
---|
| 275 | """ |
---|
| 276 | calculate error of Log(x) |
---|
[3477478] | 277 | |
---|
[a9d5684] | 278 | :param x: float value |
---|
| 279 | :param dx: float value |
---|
[3477478] | 280 | |
---|
[a9d5684] | 281 | """ |
---|
| 282 | if dx == None: |
---|
| 283 | dx = 0 |
---|
[3477478] | 284 | |
---|
[a9d5684] | 285 | # Check that the x point on the graph is zero |
---|
| 286 | if x != 0: |
---|
[3477478] | 287 | dx = dx / x |
---|
[a9d5684] | 288 | else: |
---|
| 289 | raise ValueError, "errToLogX: divide by zero" |
---|
| 290 | return dx |
---|
| 291 | |
---|
| 292 | |
---|
[821c330] | 293 | def errToYX2(y, x, dy=None, dx=None): |
---|
[a9d5684] | 294 | """ |
---|
| 295 | """ |
---|
| 296 | if dx == None: |
---|
| 297 | dx = 0 |
---|
| 298 | if dy == None: |
---|
| 299 | dy = 0 |
---|
[3477478] | 300 | err = math.sqrt((2 * x * y * dx) ** 2 + ((x ** 2) * dy) ** 2) |
---|
[a9d5684] | 301 | return err |
---|
[3477478] | 302 | |
---|
| 303 | |
---|
[a9d5684] | 304 | def errToLogXY(x, y, dx=None, dy=None): |
---|
| 305 | """ |
---|
| 306 | calculate error of Log(xy) |
---|
[3477478] | 307 | |
---|
[a9d5684] | 308 | """ |
---|
| 309 | # Check that the point on the graph is positive |
---|
| 310 | # within errors |
---|
[3477478] | 311 | if not (x - dx) > 0 or not (y - dy) > 0: |
---|
[a9d5684] | 312 | msg = "Transformation does not accept point " |
---|
| 313 | msg += " that are consistent with zero." |
---|
| 314 | raise ValueError, msg |
---|
[3477478] | 315 | if x != 0 and y != 0: |
---|
[a9d5684] | 316 | if dx == None: |
---|
| 317 | dx = 0 |
---|
| 318 | if dy == None: |
---|
| 319 | dy = 0 |
---|
[3477478] | 320 | err = (dx / x) ** 2 + (dy / y) ** 2 |
---|
[a9d5684] | 321 | else: |
---|
| 322 | raise ValueError, "cannot compute this error" |
---|
[3477478] | 323 | |
---|
[a9d5684] | 324 | return math.sqrt(math.fabs(err)) |
---|
[3477478] | 325 | |
---|
| 326 | |
---|
[821c330] | 327 | def errToLogYX2(y, x, dy=None, dx=None): |
---|
[a9d5684] | 328 | """ |
---|
| 329 | calculate error of Log(yx**2) |
---|
[3477478] | 330 | |
---|
[a9d5684] | 331 | """ |
---|
| 332 | # Check that the point on the graph is positive |
---|
| 333 | # within errors |
---|
[3477478] | 334 | if not (x - dx) > 0 or not (y - dy) > 0: |
---|
[a9d5684] | 335 | msg = "Transformation does not accept point" |
---|
| 336 | msg += " that are consistent with zero." |
---|
| 337 | raise ValueError, msg |
---|
[3477478] | 338 | if x > 0 and y > 0: |
---|
| 339 | if dx == None: |
---|
[a9d5684] | 340 | dx = 0 |
---|
[3477478] | 341 | if dy == None: |
---|
[a9d5684] | 342 | dy = 0 |
---|
[3477478] | 343 | err = (2.0 * dx / x) ** 2 + (dy / y) ** 2 |
---|
[a9d5684] | 344 | else: |
---|
| 345 | raise ValueError, "cannot compute this error" |
---|
| 346 | return math.sqrt(math.fabs(err)) |
---|
[3477478] | 347 | |
---|
| 348 | |
---|
[a9d5684] | 349 | def errOneOverX(x, y=None, dx=None, dy=None): |
---|
| 350 | """ |
---|
| 351 | calculate error on 1/x |
---|
[3477478] | 352 | |
---|
[a9d5684] | 353 | """ |
---|
[3477478] | 354 | if x != 0: |
---|
[a9d5684] | 355 | if dx == None: |
---|
| 356 | dx = 0 |
---|
[3477478] | 357 | err = dx / x ** 2 |
---|
[a9d5684] | 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) |
---|
[3477478] | 366 | |
---|
[a9d5684] | 367 | """ |
---|
[3477478] | 368 | if x > 0: |
---|
[a9d5684] | 369 | if dx == None: |
---|
| 370 | dx = 0 |
---|
[3477478] | 371 | err = -1 / 2 * math.pow(x, -3.0 / 2.0) * dx |
---|
[a9d5684] | 372 | else: |
---|
| 373 | raise ValueError, "Cannot compute this error" |
---|
| 374 | return math.fabs(err) |
---|
| 375 | |
---|
| 376 | |
---|
[821c330] | 377 | def errToLogYX4(y, x, dy=None, dx=None): |
---|
[a9d5684] | 378 | """ |
---|
| 379 | error for ln(y*x^(4)) |
---|
[3477478] | 380 | |
---|
[a9d5684] | 381 | :param x: float value |
---|
[3477478] | 382 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 394 | err = math.sqrt((4.0 * dx / x) ** 2 + (dy / y) ** 2) |
---|
[a9d5684] | 395 | return err |
---|
| 396 | |
---|
| 397 | |
---|
[821c330] | 398 | def errToYX4(y, x, dy=None, dx=None): |
---|
[a9d5684] | 399 | """ |
---|
| 400 | error for (y*x^(4)) |
---|
[3477478] | 401 | |
---|
[a9d5684] | 402 | :param x: float value |
---|
[3477478] | 403 | |
---|
[a9d5684] | 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 |
---|
[3477478] | 412 | err = math.sqrt((dy * pow(x, 4)) ** 2 + (4 * y * dx * math.pow(x, 3)) ** 2) |
---|
[a9d5684] | 413 | return err |
---|