Changeset 4a96b8b in sasview for sansutil/err1d.py
- Timestamp:
- Apr 27, 2012 9:37:14 AM (13 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 507c68f
- Parents:
- 7d6351e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansutil/err1d.py
r4025019 r4a96b8b 1 1 # This program is public domain 2 3 2 """ 4 3 Error propogation algorithms for simple arithmetic … … 8 7 integers, so be sure to create them with floating point inputs. 9 8 """ 10 11 12 9 from __future__ import division # Get true division 13 10 import numpy 14 11 15 def div(X,varX, Y,varY): 12 13 def div(X, varX, Y, varY): 16 14 """Division with error propagation""" 17 15 # Direct algorithm: … … 28 26 return Z, varZ 29 27 30 def mul(X,varX, Y,varY): 28 29 def mul(X, varX, Y, varY): 31 30 """Multiplication with error propagation""" 32 31 # Direct algorithm: … … 43 42 return Z, varZ 44 43 45 def sub(X,varX, Y, varY): 44 45 def sub(X, varX, Y, varY): 46 46 """Subtraction with error propagation""" 47 47 Z = X - Y … … 49 49 return Z, varZ 50 50 51 def add(X,varX, Y,varY): 51 52 def add(X, varX, Y, varY): 52 53 """Addition with error propagation""" 53 54 Z = X + Y … … 55 56 return Z, varZ 56 57 57 def exp(X,varX): 58 59 def exp(X, varX): 58 60 """Exponentiation with error propagation""" 59 61 Z = numpy.exp(X) 60 62 varZ = varX * Z**2 61 return Z, varZ63 return Z, varZ 62 64 63 def log(X,varX): 65 66 def log(X, varX): 64 67 """Logarithm with error propagation""" 65 68 Z = numpy.log(X) 66 69 varZ = varX / X**2 67 return Z, varZ70 return Z, varZ 68 71 69 72 # Confirm this formula before using it … … 74 77 # 75 78 76 def pow(X,varX,n): 79 80 def pow(X, varX, n): 77 81 """X**n with error propagation""" 78 82 # Direct algorithm … … 81 85 # Indirect algorithm to minimize intermediates 82 86 Z = X**n 83 varZ = varX /X87 varZ = varX / X 84 88 varZ /= X 85 89 varZ *= Z … … 88 92 return Z, varZ 89 93 90 def div_inplace(X,varX, Y,varY): 94 95 def div_inplace(X, varX, Y, varY): 91 96 """In-place division with error propagation""" 92 97 # Z = X/Y … … 98 103 del T # may want to use T[:] = Y for vectors 99 104 T = Y # reuse T for Y 100 T ** =2 # T now has Y**2105 T ** = 2 # T now has Y**2 101 106 varX /= T # varX now has varZ 102 return X, varX107 return X, varX 103 108 104 def mul_inplace(X,varX, Y,varY): 109 110 def mul_inplace(X, varX, Y, varY): 105 111 """In-place multiplication with error propagation""" 106 112 # Z = X * Y … … 114 120 varX += T # varX now has varZ 115 121 X *= Y # X now has Z 116 return X, varX122 return X, varX 117 123 118 def sub_inplace(X,varX, Y, varY): 124 125 def sub_inplace(X, varX, Y, varY): 119 126 """In-place subtraction with error propagation""" 120 127 # Z = X - Y … … 122 129 X -= Y 123 130 varX += varY 124 return X, varX131 return X, varX 125 132 126 def add_inplace(X,varX, Y,varY): 133 134 def add_inplace(X, varX, Y, varY): 127 135 """In-place addition with error propagation""" 128 136 # Z = X + Y … … 130 138 X += Y 131 139 varX += varY 132 return X, varX140 return X, varX 133 141 134 def pow_inplace(X,varX,n): 142 143 def pow_inplace(X, varX, n): 135 144 """In-place X**n with error propagation""" 136 145 # Direct algorithm … … 141 150 varX /= X # varX now has varX/X**2 142 151 X **= n # X now has Z = X**n 143 varX *= X 152 varX *= X 144 153 varX *= X # varX now has varX/X**2 * Z**2 145 154 varX *= n**2 # varX now has varZ 146 return X, varX155 return X, varX
Note: See TracChangeset
for help on using the changeset viewer.