From 18c659780eeb6cf04430cdf9899197f4851d1bf6 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 1 Jun 2020 08:56:15 -0500 Subject: [PATCH] ulab: update .. add new modules and functions to our shared-bindings stubs --- extmod/ulab | 2 +- shared-bindings/ulab/approx/__init__.pyi | 52 ++++++++++ shared-bindings/ulab/compare/__init__.pyi | 8 ++ shared-bindings/ulab/vector/__init__.pyi | 116 ++++++++++++---------- 4 files changed, 124 insertions(+), 54 deletions(-) create mode 100644 shared-bindings/ulab/approx/__init__.pyi diff --git a/extmod/ulab b/extmod/ulab index cf61d728e7..cbdd1295c1 160000 --- a/extmod/ulab +++ b/extmod/ulab @@ -1 +1 @@ -Subproject commit cf61d728e70b9ec57e5711b40540793a89296f5d +Subproject commit cbdd1295c11e9b810a2712ac5bdfd51381967bf0 diff --git a/shared-bindings/ulab/approx/__init__.pyi b/shared-bindings/ulab/approx/__init__.pyi new file mode 100644 index 0000000000..89f136f690 --- /dev/null +++ b/shared-bindings/ulab/approx/__init__.pyi @@ -0,0 +1,52 @@ +"""Numerical approximation methods""" + +def bisect(fun, a, b, *, xtol=2.4e-7, maxiter=100) -> float: + """ + :param callable f: The function to bisect + :param float a: The left side of the interval + :param float b: The right side of the interval + :param float xtol: The tolerance value + :param float maxiter: The maximum number of iterations to perform + + Find a solution (zero) of the function ``f(x)`` on the interval + (``a``..``b``) using the bisection method. The result is accurate to within + ``xtol`` unless more than ``maxiter`` steps are required.""" + ... + +def newton(fun, x0, *, xtol=2.4e-7, rtol=0.0, maxiter=50) -> float: + """ + :param callable f: The function to bisect + :param float x0: The initial x value + :param float xtol: The absolute tolerance value + :param float rtol: The relative tolerance value + :param float maxiter: The maximum number of iterations to perform + + Find a solution (zero) of the function ``f(x)`` using Newton's Method. + The result is accurate to within ``xtol * rtol * |f(x)|`` unless more than + ``maxiter`` steps are requried.""" + ... + +def fmin(fun, x0, *, xatol=2.4e-7, fatol=2.4e-7, maxiter=200) -> float: + """ + :param callable f: The function to bisect + :param float x0: The initial x value + :param float xatol: The absolute tolerance value + :param float fatol: The relative tolerance value + + Find a minimum of the function ``f(x)`` using the downhill simplex method. + The located ``x`` is within ``fxtol`` of the actual minimum, and ``f(x)`` + is within ``fatol`` of the actual minimum unless more than ``maxiter`` + steps are requried.""" + ... + +def interp(x: ulab.array, xp:ulab.array, fp:ulab.array, *, left=None, right=None) -> ulab.array: + """ + :param ulab.array x: The x-coordinates at which to evaluate the interpolated values. + :param ulab.array xp: The x-coordinates of the data points, must be increasing + :param ulab.array fp: The y-coordinates of the data points, same length as xp + :param left: Value to return for ``x < xp[0]``, default is ``fp[0]``. + :param right: Value to return for ``x > xp[-1]``, default is ``fp[-1]``. + + Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.""" + ... + diff --git a/shared-bindings/ulab/compare/__init__.pyi b/shared-bindings/ulab/compare/__init__.pyi index 00a9eae1e6..1606e43c20 100644 --- a/shared-bindings/ulab/compare/__init__.pyi +++ b/shared-bindings/ulab/compare/__init__.pyi @@ -28,3 +28,11 @@ def minimum(x1, x2): must be the same size. If the inputs are both scalars, a number is returned""" ... + +def equal(x1, x2): + """Return an array of bool which is true where x1[i] == x2[i] and false elsewhere""" + ... + +def not_equal(x1, x2): + """Return an array of bool which is false where x1[i] == x2[i] and true elsewhere""" + ... diff --git a/shared-bindings/ulab/vector/__init__.pyi b/shared-bindings/ulab/vector/__init__.pyi index 2c7a804463..0e590f3c83 100644 --- a/shared-bindings/ulab/vector/__init__.pyi +++ b/shared-bindings/ulab/vector/__init__.pyi @@ -5,104 +5,114 @@ applying the function to every element in the array. This is typically much more efficient than expressing the same operation as a Python loop.""" def acos(): - """Computes the inverse cosine function""" - ... + """Computes the inverse cosine function""" + ... def acosh(): - """Computes the inverse hyperbolic cosine function""" - ... + """Computes the inverse hyperbolic cosine function""" + ... def asin(): - """Computes the inverse sine function""" - ... + """Computes the inverse sine function""" + ... def asinh(): - """Computes the inverse hyperbolic sine function""" - ... + """Computes the inverse hyperbolic sine function""" + ... def around(a, *, decimals): - """Returns a new float array in which each element is rounded to - ``decimals`` places.""" - ... + """Returns a new float array in which each element is rounded to + ``decimals`` places.""" + ... def atan(): - """Computes the inverse tangent function; the return values are in the - range [-pi/2,pi/2].""" - ... + """Computes the inverse tangent function; the return values are in the + range [-pi/2,pi/2].""" + ... def atan2(y,x): - """Computes the inverse tangent function of y/x; the return values are in - the range [-pi, pi].""" - ... + """Computes the inverse tangent function of y/x; the return values are in + the range [-pi, pi].""" + ... def atanh(): - """Computes the inverse hyperbolic tangent function""" - ... + """Computes the inverse hyperbolic tangent function""" + ... def ceil(): - """Rounds numbers up to the next whole number""" - ... + """Rounds numbers up to the next whole number""" + ... def cos(): - """Computes the cosine function""" - ... + """Computes the cosine function""" + ... def erf(): - """Computes the error function, which has applications in statistics""" - ... + """Computes the error function, which has applications in statistics""" + ... def erfc(): - """Computes the complementary error function, which has applications in statistics""" - ... + """Computes the complementary error function, which has applications in statistics""" + ... def exp(): - """Computes the exponent function.""" - ... + """Computes the exponent function.""" + ... def expm1(): - """Computes $e^x-1$. In certain applications, using this function preserves numeric accuracy better than the `exp` function.""" - ... + """Computes $e^x-1$. In certain applications, using this function preserves numeric accuracy better than the `exp` function.""" + ... def floor(): - """Rounds numbers up to the next whole number""" - ... + """Rounds numbers up to the next whole number""" + ... def gamma(): - """Computes the gamma function""" - ... + """Computes the gamma function""" + ... def lgamma(): - """Computes the natural log of the gamma function""" - ... + """Computes the natural log of the gamma function""" + ... def log(): - """Computes the natural log""" - ... + """Computes the natural log""" + ... def log10(): - """Computes the log base 10""" - ... + """Computes the log base 10""" + ... def log2(): - """Computes the log base 2""" - ... + """Computes the log base 2""" + ... def sin(): - """Computes the sine""" - ... + """Computes the sine""" + ... def sinh(): - """Computes the hyperbolic sine""" - ... + """Computes the hyperbolic sine""" + ... def sqrt(): - """Computes the square root""" - ... + """Computes the square root""" + ... def tan(): - """Computes the tangent""" - ... + """Computes the tangent""" + ... def tanh(): - """Computes the hyperbolic tangent""" - ... + """Computes the hyperbolic tangent""" + ... + +def vectorise(f, *, otypes=None): + """ + :param callable f: The function to wrap + :param otypes: List of array types that may be returned by the function. None is intepreted to mean the return value is float. + + Wrap a Python function `f` so that it can be applied to arrays. + + The callable must return only values of the types specified by otypes, or the result is undefined.""" + ...