Guest User

Untitled

a guest
Mar 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. from math import sqrt
  2. from math import log10
  3. from math import fabs
  4. from math import floor
  5. from math import ceil
  6. from math import pi
  7.  
  8. class magnitude:
  9. def __init__(self, val, err):
  10. self.val = val
  11. self.err = err
  12.  
  13. def __str__(self):
  14. "Prints the magnitude in the format v.vvvv(v+-e) being 'v' the relevant digits of the magnitude value and '+-e' the most significant digit of the error."
  15. if self.err == 0 or self.val == 0:
  16. return str(self.val)
  17. if self.err >= fabs(self.val):
  18. return '{:.0e} +- {:.0e}'.format(self.val, self.err) + ' (Infinite uncertainty!)'
  19. else:
  20. temp = '{:.' + str(ceil(log10(fabs(self.val*(1+pi*1e-3))) - ceil(log10(self.err*(1+pi*1e-3))) )) + 'e}' # Calculates number of digits for the representative part. The adition of 'pi*1e-3' is to add some noise and avoid error values of '1', '10', '100', ..., because they are not well printed.
  21. temp = temp.format(self.val)
  22. temp = temp[:-4] # Remove last 4 chars.
  23. return temp + '(+-' + str(int(self.err*10**(ceil(-log10(self.err))))) + ')' + '{:.0e}'.format(fabs(self.val))[1:] + ' ({:d} ppm)'.format(int(self.err/fabs(self.val)*1e6))
  24. # --------------------
  25. def __add__(self, other):
  26. if type(other) == int or type(other) == float:
  27. other = magnitude(other, 0)
  28. return magnitude(self.val + other.val, sqrt(self.err**2 + other.err**2))
  29. def __radd__(self, other):
  30. if type(other) == int or type(other) == float:
  31. other = magnitude(other, 0)
  32. return magnitude(self.val + other.val, sqrt(self.err**2 + other.err**2))
  33. # --------------------
  34. def __sub__(self, other):
  35. if type(other) == int or type(other) == float:
  36. other = magnitude(other, 0)
  37. return magnitude(self.val - other.val, sqrt(self.err**2 + other.err**2))
  38. def __rsub__(self, other):
  39. if type(other) == int or type(other) == float:
  40. other = magnitude(other, 0)
  41. return magnitude(-self.val + other.val, sqrt(self.err**2 + other.err**2))
  42. # --------------------
  43. def __mul__(self, other):
  44. if type(other) == int or type(other) == float:
  45. other = magnitude(other, 0)
  46. return magnitude(self.val*other.val, sqrt(self.val**2*other.err**2 + self .err**2*other.val**2))
  47. def __rmul__(self, other):
  48. if type(other) == int or type(other) == float:
  49. other = magnitude(other, 0)
  50. return magnitude(self.val*other.val, sqrt(self.val**2*other.err**2 + self .err**2*other.val**2))
  51. # --------------------
  52. def __truediv__(self, other):
  53. if type(other) == int or type(other) == float:
  54. other = magnitude(other, 0)
  55. return magnitude(self.val/other.val, sqrt(self.err**2/other.val**2 + self.val**2/other.val**2*other.err**2))
  56. def __rtruediv__(self, other):
  57. if type(other) == int or type(other) == float:
  58. other = magnitude(other, 0)
  59. return magnitude(other.val/self.val, sqrt(other.err**2/self.val**2 + other.val**2/self.val**2*self.err**2))
Add Comment
Please, Sign In to add comment