Page MenuHomec4science

mesure.py
No OneTemporary

File Metadata

Created
Fri, Jan 3, 04:35

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 = math.fabs(self.v * m.e) + math.fabs(self.e * m.v)
self.v *= m.v
else:
self.v *= m
self.e *= math.fabs(m)
return self
def __itruediv__(self, m):
if isinstance(m, Mesure):
self.e = math.fabs(self.e / m.v) + math.fabs(self.v * m.e / m.v**2)
self.v /= m.v
else:
self.v /= m
self.e /= math.fabs(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, math.fabs(self.e * k))
def __rtruediv__(self, k):
return Mesure(self.v / k, math.fabs(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, math.fabs(self.v * m.e) + math.fabs(self.e * m.v))
def __truediv__(self, m):
if not isinstance(m, Mesure):
return self.__rtruediv__(m)
return Mesure(self.v / m.v, math.fabs(self.e / m.v) + math.fabs(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 * math.fabs(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, math.fabs(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.fabs(math.cos(self.v)) * self.e)
def cos(self):
return Mesure(math.cos(self.v), math.fabs(math.sin(self.v)) * self.e)
def __float__(self):
return self.v
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 linearRegression(X, Y):
N = len(Y)
ssx = variance(X)
ssxy = covariance(X, Y)
m = ssxy / ssx
x = middle(X)
y = middle(Y)
b = y - m * x
# m error computation
sse = 0
for i in range(N):
sse += (Y[i] - (m * X[i] + b))**2
sse /= (N - 1)
dm = math.sqrt(sse / (ssx * N))
# b error computation
sqx = 0
for i in range(N):
sqx += X[i]**2
db = math.sqrt(sse * (1.0 + sqx / ssx) / N)
return (Mesure(m, dm), Mesure(b, db))
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, gain = 1.0, verbose = False):
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 = []
if verbose:
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]) / gain)
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