From 663acb73df9f8eab28bf4486ce690806df6bf16c Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Fri, 3 Apr 2020 16:42:58 -0500 Subject: [PATCH] [numerics] Improve documentation for TabulatedFunction --- interfaces/cython/cantera/func1.pyx | 27 ++++++++++++-------- interfaces/cython/cantera/test/test_func1.py | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/interfaces/cython/cantera/func1.pyx b/interfaces/cython/cantera/func1.pyx index c0eeaf34454..3a8ad13267a 100644 --- a/interfaces/cython/cantera/func1.pyx +++ b/interfaces/cython/cantera/func1.pyx @@ -110,25 +110,31 @@ cdef class TabulatedFunction(Func1): by two iterable objects containing sample point location and function values, or a single array that concatenates those inputs in two rows or columns. Between sample points, values are evaluated based on the optional - keyword argument 'method'; options are 'linear' (linear interpolation, - default) or 'previous' (nearest previous value). Outside the sample + keyword argument ``method``; options are ``'linear'`` (linear interpolation, + default) or ``'previous'`` (nearest previous value). Outside the sample interval, the value at the closest end point is returned. - Examples for `TabulatedFunction` objects are:: + Examples for `TabulatedFunction` objects using a single (two-dimensional) + array as input are:: >>> t1 = TabulatedFunction([[0, 2], [1, 1], [2, 0]]) >>> [t1(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]] [2.0, 2.0, 1.5, 0.5, 0.0, 0.0] - >>> t2 = TabulatedFunction([[0, 2], [1, 1], [2, 0]], method='previous') + >>> t2 = TabulatedFunction(np.array([0, 1, 2]), (2, 1, 0)) >>> [t2(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]] - [2.0, 2.0, 2.0, 1.0, 0.0, 0.0] + [2.0, 2.0, 1.5, 0.5, 0.0, 0.0] - >>> t3 = TabulatedFunction([0, 1, 2], [2, 1, 0]) + where the optional ``method`` keyword argument changes the type of + interpolation from the ``'linear'`` default to ``'previous'``:: + + >>> t3 = TabulatedFunction([[0, 2], [1, 1], [2, 0]], method='previous') >>> [t3(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]] - [2.0, 2.0, 1.5, 0.5, 0.0, 0.0] + [2.0, 2.0, 2.0, 1.0, 0.0, 0.0] + + Alternatively, a `TabulatedFunction` can be defined using two input arrays:: - >>> t4 = TabulatedFunction(np.array([0, 1, 2]), (2, 1, 0)) + >>> t4 = TabulatedFunction([0, 1, 2], [2, 1, 0]) >>> [t4(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]] [2.0, 2.0, 1.5, 0.5, 0.0, 0.0] """ @@ -150,9 +156,8 @@ cdef class TabulatedFunction(Func1): "requires two rows or columns") self._set_tables(time, fval, stringify(method)) else: - raise TypeError( - "'TabulatedFunction' must be constructed from " - "a numeric array with two columns") + raise TypeError("'TabulatedFunction' must be constructed from " + "a numeric array with two dimensions") elif len(args) == 2: # tabulated function (two arrays mimic C++ interface) diff --git a/interfaces/cython/cantera/test/test_func1.py b/interfaces/cython/cantera/test/test_func1.py index 3c918453d88..a7d1b11fe96 100644 --- a/interfaces/cython/cantera/test/test_func1.py +++ b/interfaces/cython/cantera/test/test_func1.py @@ -119,4 +119,4 @@ def test_tabulated_failures(self): with self.assertRaisesRegex(ct.CanteraError, 'monotonically'): ct.TabulatedFunction((0, 1, 0.5, 2), (2, 1, 1, 0)) with self.assertRaisesRegex(ct.CanteraError, 'not implemented'): - ct.TabulatedFunction((0, 1, 1, 2), (2, 1, 1, 0), method='quadratic') + ct.TabulatedFunction((0, 1, 1, 2), (2, 1, 1, 0), method='not implemented')