Modifying the value of a float-like object
smichr
smichr at gmail.com
Fri May 1 08:13:54 CEST 2009
On Apr 18, 4:21 pm, Eric.Le.Bi... at spectro.jussieu.fr wrote:
> On Apr 15, 5:33 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
>
> I adjusted your code in a few ways, and put the result athttp://code.activestate.com/recipes/576721/(with due credit):
>
> 1) There was a strange behavior, which is fixed (by performing only
> dynamic calculations of UExpr, with an optimization through a new
> "constant" class):
>
Although normal arithmetic operations are correlated, the correlation
is lost (AFAICT) as soon as you use a function:
>>> a=Number_with_uncert(3,1);y=a+cos(a);y.error
1.0099083406911706
>>> #check it manually
>>> def f(a):
... return a+cos(a)
...
>>> h=1e-5;print (f(3+h)-f(3))/h
0.858884941879
>>> #check it analytically
>>> 1-sin(3)
0.85887999194013276
It's effectively creating a new variable, tmp=cos(a) so y looks like a
+tmp and the uncertainty in this is (a.error**2 + tmp.error**2)**.5:
>>> a
<class '__main__.Semi_formal_var'> object with result 3.0 +- 1
>>> cos(a)
<class '__main__.Semi_formal_var'> object with result -0.9899924966 +-
0.14112000806
>>> (1+0.14112000806**2)**.5
1.009908340729422
That result we got when requesting the error in y
We can also see that the a inside the cos() is invisible by taking the
derivative wrt a
>>> y.derivative_value(a)
0.99999999996214217
Also, this approach is limited in that only variables can be arguments
to functions, not node expressions. If x and d are variables, x/d
becomes an expression and you cannot compute sin(x/d). Or am I missing
something?
/c
More information about the Python-list
mailing list