Exemplo n.º 1
0
        names = self.function.names()
        if len(names) > 1:
            raise Exception('Too many variables.')

        self.var = names[0] if len(names) > 0 else 'x'

    def __call__(self, x):
        return self.function({self.var: x})


if __name__ == '__main__':
    argparser = ArgumentParser(description=
            'Estimates the definite integral of a single-variable function.')
    argparser.add_argument('-a', '--lower', default=0.0, type=float,
            help='Lower integration bound (default: 0.0).')
    argparser.add_argument('-b', '--upper', default=1.0, type=float,
            help='Upper integration bound (defualt: 1.0).')
    argparser.add_argument('-p', '--precision', default=1e-4, type=float,
            help='Estimation precision (default: 1e-4).')
    argparser.add_argument('-f', '--fork', default=1, type=int, 
            help='Number of worker processes.')
    argparser.add_argument('expression', help='Single-variable expression.')
    args = argparser.parse_args()

    try:
        print mp_defined_integral(SingleVariableFunction(args.expression), 
                args.lower, args.upper, args.precision, args.fork)
    except Exception, e:
        print 'ERROR: ' + str(e)

 def test_mp(self):
     self.assertAlmostEqual(0.5, mp_defined_integral(ident, 0, 1, 0.001, 1), delta=0.001)
     self.assertAlmostEqual(0.5, mp_defined_integral(ident, 0, 1, 0.000001, 4), delta=0.000001)
     self.assertAlmostEqual(0.5, mp_defined_integral(ident, 0, 1, 0.000001, 16), delta=0.000001)