Code Search for Developers
 
 
  

trapezoid_skel.py from matplotlib at Krugle


Show trapezoid_skel.py syntax highlighted

#!/usr/bin/env python
"""Simple trapezoid-rule integrator."""

import numpy as N

def trapz(x, y):
    """Simple trapezoid integrator for sequence-based innput.

    Inputs:
      - x,y: arrays of the same length.

    Output:
      - The result of applying the trapezoid rule to the input, assuming that
      y[i] = f(x[i]) for some function f to be integrated.

    Minimally modified from matplotlib.mlab."""
    raise NotImplementedError


def trapzf(f,a,b,npts=100):
    """Simple trapezoid-based integrator.

    Inputs:
      - f: function to be integrated.

      - a,b: limits of integration.

    Optional inputs:
      - npts(100): the number of equally spaced points to sample f at, between
      a and b.

    Output:
      - The value of the trapezoid-rule approximation to the integral."""
    raise NotImplementedError

if __name__ == '__main__':
    # Simple tests for trapezoid integrator, when this module is called as a
    # script from the command line.  From ipython, run it via:
    #
    # run -e trapezoid
    #
    # so that ipython ignores the SystemExit exception automatically raised by
    # the unittest module at the end.

    import unittest
    import numpy.testing as ntest

    def square(x): return x**2

    class trapzTestCase(unittest.TestCase):
        def test_err(self):
            self.assertRaises(ValueError,trapz,range(2),range(3))

        def test_call(self):
            x = N.linspace(0,1,100)
            y = N.array(map(square,x))
            ntest.assert_almost_equal(trapz(x,y),1./3,4)

    class trapzfTestCase(unittest.TestCase):
        def test_square(self):
            ntest.assert_almost_equal(trapzf(square,0,1),1./3,4)

        def test_square2(self):
            ntest.assert_almost_equal(trapzf(square,0,3,350),9.0,4)

    unittest.main()




See more files for this project here

matplotlib

Matplotlib is a pure python plotting library with the goal of making\r\npublication quality plots using a syntax familiar to matlab users. \r\nThe library uses Numeric for handling large\r\ndata sets and supports a variety of output backends

Project homepage: http://sourceforge.net/projects/matplotlib
Programming language(s): C,C++,Python
License: other

  faces/
    faces_skel0.tgz
    fmatch_skel1.py
  fortran_wrap/
    Makefile
    fib3.f
    test.py
  distributions_skel.py
  erathostenes_skel.py
  fft_imdenoise_skel.py
  fit_synapse_skel.py
  fitting_skel.py
  montecarlo_pi_skel.py
  polyroots1d_skel.py
  qsort_skel.py
  quad_newton_skel.py
  recarray_demo_skel.py
  regress_demo_skel.py
  scrape_key_stats_skel.py
  shoot_skel.py
  spline_demo_skel.py
  stats_descriptives_skel.py
  stats_distributions_skel.py
  trapezoid_skel.py
  wallis_pi_skel.py
  wordfreqs_skel.py