File: Uncertainities.py

package info (click to toggle)
python-uncertainities 0.001-3.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 32 kB
  • ctags: 28
  • sloc: python: 82; makefile: 37; sh: 25
file content (147 lines) | stat: -rw-r--r-- 3,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python
'''Ratanie neistot
'''

# author: Tomas Stanys <stanys@fmph.uniba.sk>
# Contributed by Radovan Garabik he..he..
# version 0.001
from types import *
import math

def exp(x): return math.e**x

def ToUncertain(x):
    if isinstance(x, Uncertain):
            return x
    else:
        return Uncertain(x)

class Uncertain:
    '''This class implements numbers with uncertainity.'''

    def __init__(self, value, error=0.):
            self.value, self.error = value, error

    def __repr__(self):
        return 'Uncertain(%s,%s)' % (self.value, self.error)

    def __str__(self):
        return str(self.value)+" +- "+str(self.error)

    def tomas(self):
        return "%.2f +- %.2f" % (self.value, self.error)        

    # a + b
    def __add__(self, other):
        other = ToUncertain(other)
        nvalue = self.value+other.value
        nerr = math.sqrt(self.error*self.error + other.error*other.error)
        return Uncertain(nvalue, nerr)

    def __radd__(self, other):
        return Uncertain(other) + self

    # a - b
    def __sub__(self, other):
        other = ToUncertain(other)
        nvalue = self.value-other.value
        nerr = math.sqrt(self.error*self.error + other.error*other.error)
        return Uncertain(nvalue, nerr)

    def __rsub__(self, other):
        return Uncertain(other) - self

    # a * b
    def __mul__(a, b):
        b = ToUncertain(b)
        nvalue = a.value*b.value
        nerr = math.sqrt(a.value*a.value*b.error*b.error + 
                b.value*b.value*a.error*a.error)
        return Uncertain(nvalue, nerr)

    def __rmul__(b, a):
        return Uncertain(a) * b

    # a / b
    def __div__(a, b):
        b = ToUncertain(b)
        nvalue = a.value/b.value
        nerr = math.sqrt(a.error*a.error/(b.value*b.value) + 
                a.value*a.value*b.error*b.error/(b.value*b.value*b.value*b.value))
        return Uncertain(nvalue, nerr)
        
    def __rdiv__(b, a):
        return Uncertain(a) / b

    # a % b
    #def __mod__(a, b):
    #    div = a / b
    #    try:
    #        div = int(div)
    #    except OverflowError:
    #        div = long(div)
    #    return a - b * div

    def __rmod__(b, a):
        return Uncertain(a) % b

    # a ** b
    def __pow__(a, b):
	b = ToUncertain(b)
        nvalue = a.value**b.value
        nerr = math.sqrt((b.value*a.value**(b.value-1))**2*a.error**2 + 
                (math.log(a.value)*a.value**b.value)**2*b.error**2)
        return Uncertain(nvalue, nerr)

    def __rpow__(b, a):
        return Uncertain(a) ** b

    # -a
    def __neg__(a):
        return Uncertain(-a.value, a.error)

    # abs(a)
    def __abs__(a):
        return Uncertain(abs(a.value), a.error)

    # int(a)
    def __int__(a):
        return int(a.value)

    # long(a)
    #def __long__(a):
    #    return long(a.value)

    # float(a)
    #def __float__(a):
    #    return float(a.value)

    def __eq__(self, other):
        other=ToUncertain(other)
    	return (abs(self.value-other.value)<min(self.error,other.error))
    

    # cmp(a,b)
    def __cmp__(a, b):
        return cmp(a.value,b.value)

    def __rcmp__(b, a):
           return cmp(Uncertain(a), b)

    # a != 0
    def __nonzero__(a):
        return not(a.value==0 and a.error==0)

    # coercion
    #def __coerce__(a, b):
    #    return a, Uncertain(b.value)

def test():
     a = Uncertain(5., 1.)
     b = Uncertain(2., 0.5)
     c = 20
 #   c = Uncertain(10., 5.)
     print c*a/b, int(a/b)

if __name__ == '__main__':
    test()