Python可以帮忙:)

# 这让我想起了用JavaScript做欧拉公式和梯形公式计算的事情

# 功能实用,性能嘛,再说吧,典型的行为学派观点^^

# 线性插值(并做乘积)

import decimal

def _process(bf, cf):
    '''
    Process the data and return its result
    '''
    lis = []
    i = 0
    j = 0

    decimal.getcontext().prec = 11
    _b = [(decimal.Decimal(line[9:25]), decimal.Decimal(line[34:50])) for line in open(bf) if len(line) == 65]
    _c = [(decimal.Decimal(line[9:25]), decimal.Decimal(line[34:50])) for line in open(cf) if len(line) == 65]

    while 1:
        try:
            if _c[j][0] < _b[i][0]:
                j += 1
                if _c[j][0] >= _b[i][0]:
                    _v = _b[i][1]*(_c[j-1][1]+(_b[i][0]-_c[j-1][0])*(_c[j][1]-_c[j-1][1])/(_c[j][0]-_c[j-1][0]))
                    lis.append((_b[i][0], _v))
                    i += 1
                    j -= 1
            else:
                _v = _b[i][1]*(_c[j][1]+(_b[i][0]-_c[j][0])*(_c[j+1][1]-_c[j][1])/(_c[j+1][0]-_c[j][0]))
                lis.append((_b[i][0], _v))
                i += 1
        except IndexError:
            # print 'Done!'
            break

    return lis

base data file: 244(slice)
     plot points:

             x                        y                   e

         5.0002500125E-04         4.8430921737E-01      0.54177
         1.5000750038E-03         3.0355905909E+00      0.18986
         2.5001250063E-03         3.7486307653E+00      0.16132
         3.5001750088E-03         6.5356686466E+00      0.12887
         4.5002250113E-03         7.9923502044E+00      0.09901

compared data file: m2(slice)
     plot points:

             x                        y                   e

         1.0000000000E-11         2.4814747679E+02      0.00000
         1.1543570000E-11         2.3116622757E+02      0.00000
         1.1660930000E-11         2.3003818964E+02      0.00000
         1.3087150000E-11         2.1727277313E+02      0.00000
         1.3321870000E-11         2.1543349880E+02      0.00000

test:
>>> from process import _process as p
>>> res = p('244', 'm2')
>>> len(res)
19999
>>> res[0]
(Decimal('0.00050002500125'), Decimal('4.9097028328'))
>>> sum = 0
>>> for r in res:
...     sum += r[1]
...
>>> print sum
200601.01471


你可能感兴趣的:(python,DECIMAL,线性插值)