Page MenuHomec4science

mesure.py
No OneTemporary

File Metadata

Created
Sat, Jan 18, 09:19

mesure.py

import math
class Mesure:
def __init__(self, v, e):
self.v = float(v)
self.e = math.fabs(float(e))
def __str__(self):
return str(self.v) + " +- " + str(self.e)
def __iadd__(self, m):
if isinstance(m, Mesure):
self.v += m.v
self.e += m.e
else:
self.v += m
return self
def __isub__(self, m):
if isinstance(m, Mesure):
self.v -= m.v
self.e += m.e
else:
self.v -= m
return self
def __imul__(self, m):
if isinstance(m, Mesure):
self.e = self.v * m.e + self.e * m.v
self.v *= m.v
else:
self.v *= m
self.e *= m
return self
def __itruediv__(self, m):
if isinstance(m, Mesure):
self.e = self.e / m.v + self.v * m.e / m.v**2
self.v /= m.v
else:
self.v /= m
self.e /= m
return self
def __radd__(self, k):
return Mesure(self.v + k, self.e)
def __rsub__(self, k):
return Mesure(self.v - k, self.e)
def __rmul__(self, k):
return Mesure(self.v * k, self.e * k)
def __rtruediv__(self, k):
return Mesure(self.v / k, self.e / k)
def __add__(self, m):
if not isinstance(m, Mesure):
return self.__radd__(m)
return Mesure(self.v + m.v, self.e + m.e)
def __sub__(self, m):
if not isinstance(m, Mesure):
return self.__rsub__(m)
return Mesure(self.v - m.v, self.e + m.e)
def __mul__(self, m):
if not isinstance(m, Mesure):
return self.__rmul__(m)
return Mesure(self.v * m.v, self.v * m.e + self.e * m.v)
def __truediv__(self, m):
if not isinstance(m, Mesure):
return self.__rtruediv__(m)
return Mesure(self.v / m.v, self.e / m.v + self.v * m.e / m.v**2)
def __pow__(self, r):
if not isinstance(r, int) and not isinstance(r, float):
raise TypeError("Mesure: pow is only for int or float args")
err = r * self.v**(r-1) * self.e
return Mesure(self.v**r, math.fabs(err))
def __eq__(self, m):
if isinstance(m, Mesure):
return m.v == self.v and m.e == self.e
return self.v == m
def __ne__(self, m):
if isinstance(m, Mesure):
return m.v != self.v or m.e != self.e
return self.v != m
def __lt__(self, m):
if isinstance(m, Mesure):
return self.v < m.v
return self.v < m
def __le__(self, m):
if isinstance(m, Mesure):
return self.v <= m.v
return self.v <= m
def __gt__(self, m):
if isinstance(m, Mesure):
return self.v > m.v
return self.v > m
def __ge__(self, m):
if isinstance(m, Mesure):
return self.v >= m.v
return self.v >= m
def square(self):
return Mesure(self.v**2, 2 * self.v * self.e)
def sqrt(self):
return Mesure(math.sqrt(self.v), self.e / (2 * math.sqrt(self.v)))
def exp(self):
e = math.exp(self.v)
return Mesure(e, e * self.e)
def log(self):
return Mesure(math.log(self.v), self.e / self.v)
def sin(self):
return Mesure(math.sin(self.v), math.cos(self.v) * self.e)
def cos(self):
return Mesure(math.cos(self.v), math.sin(self.v) * self.e)
def genMesures(values, error):
out = []
for value in values:
out.append(Mesure(value, error))
return out
def minMaxMesure(m, M):
return Mesure((float(M) + float(m))/2, (float(M) - float(m))/2)
def middle(X):
x = 0
for i in X:
x += i
return x / len(X)
def covariance(X, Y):
N = len(X)
x = middle(Y)
y = middle(Y)
c = 0
for i in range(N):
c += (X[i] - x)*(Y[i] - y)
return c / N
def variance(X):
return covariance(X, X)
def leastSquareMethod(X, Y):
m = covariance(X, Y) / variance(X)
x = middle(X)
y = middle(Y)
b = y - m * x
if isinstance(m, Mesure):
m = m.v
if isinstance(b, Mesure):
b = b.v
return (m, b)
def splitline(line):
out = (line[:-1]).split(" ")
flag = False
while(not flag):
i = 0
flag = False
while(i < len(out)):
if (out[i] == ''):
del out[i]
flag = True
else:
i += 1
return out
def loadMesures(filename, values, errors):
f = open(filename, 'r')
firstline = None
if isinstance(values, str):
firstline = splitline(f.readline())
values = firstline.index(values)
if isinstance(errors, str):
if firstline is None:
firstline = splitline(f.readline())
errors = firstline.index(errors)
out = []
print("File %s: loading columns (%d, %d)" % (filename, values, errors))
for line in f:
l = splitline(line)
try:
out.append(Mesure(l[values], l[errors]))
except ValueError:
print("Could not parse line", line, "Ignoring it")
f.close()
return out
def computeRange(y, Y, x, X):
dy = Y - y
dx = (X - x).v
rm = (dy.v-dy.e)/dx
rp = (dy.v+dy.e)/dx
return ( (rm, Y.v - Y.e - rm * x.v), (rp, Y.v + Y.e - rp * x.v) )

Event Timeline