Dan, wow! This looks extremely promising!
I initially tried to create a Float_ref class that inherits from numpy.array, so that objects of the new class behave like numpy.array in calculations, but also contain an "uncertainty" atribute, but this is apparently not allowed ("cannot create 'builtin_function_or_method' instances"). So I thought I'd directly add an attribute to a numpy.array: "x = numpy.array(3.14); x.uncertainty = 0.01", but this is not allowed either. Thus, it is not obvious for me to extend the 1x1 numpy.array object so that it also contains an "uncertainty" attribute. However, I think your suggestion can largely solve the original problem, with a small price to pay: numbers with uncertainties can be defined in a module that keeps declared values in a list of numpy.arrays, and the associated uncertainties in another list: after "registering" many "mutable float" objects, the modules would contain the following lists: >>> mutable_floats = [ numpy.array(3.14), numpy.array(1.41),...] >>> uncertainties = [ 0.01, 0.01 ,...] The floatref class could instead be a simple function that returns the last numpy.array created, after adding an element to both the mutable_float and uncertainties lists. What we lose here is that it's hard to get the uncertainty associated to a given variable (this is obviously possible if no other variable has the same value, though). Dan, you gave me hope that precise error propagation calculations can be done by repeated calls to unmodified calculation code designed for floats! Thanks! On Apr 15, 10:44 am, Dan Goodman <dg.gm...@thesamovar.net> wrote: > eric.le.bi...@spectro.jussieu.fr wrote: > > Hello, > > > Is there a way to easily build an object that behaves exactly like a > > float, but whose value can be changed? The goal is to maintain a list > > [x, y,…] of these float-like objects, and to modify their value on the > > fly (with something like x.value = 3.14) so that any expression like "x > > +y" uses the new value. > > Hi Eric, > > Numpy's array object can do something like what you want: > > In [27]: x=array(0.0) > > In [28]: print x, sin(x) > 0.0 0.0 > > In [29]: x.itemset(pi/2) > > In [30]: print x, sin(x) > 1.57079632679 1.0 > > Not sure if this a recommended way of using array or not, but it seems > to work. The problem is that any calculation you do with such an object > will result in a float, and not another numpy array (although inplace > operations work): > > In [40]: print (x*2).__class__ > <type 'numpy.float64'> > > In [41]: x *= 2 > > In [42]: print x.__class__ > <type 'numpy.ndarray'> > > So it's not a perfect solution, but it might be OK for what you need. > > Dan -- http://mail.python.org/mailman/listinfo/python-list