mirror of
https://github.com/bvanroll/college-python-image.git
synced 2025-09-01 05:22:50 +00:00
first commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
A sub-package for efficiently dealing with polynomials.
|
||||
|
||||
Within the documentation for this sub-package, a "finite power series,"
|
||||
i.e., a polynomial (also referred to simply as a "series") is represented
|
||||
by a 1-D numpy array of the polynomial's coefficients, ordered from lowest
|
||||
order term to highest. For example, array([1,2,3]) represents
|
||||
``P_0 + 2*P_1 + 3*P_2``, where P_n is the n-th order basis polynomial
|
||||
applicable to the specific module in question, e.g., `polynomial` (which
|
||||
"wraps" the "standard" basis) or `chebyshev`. For optimal performance,
|
||||
all operations on polynomials, including evaluation at an argument, are
|
||||
implemented as operations on the coefficients. Additional (module-specific)
|
||||
information can be found in the docstring for the module of interest.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
from .polynomial import Polynomial
|
||||
from .chebyshev import Chebyshev
|
||||
from .legendre import Legendre
|
||||
from .hermite import Hermite
|
||||
from .hermite_e import HermiteE
|
||||
from .laguerre import Laguerre
|
||||
|
||||
from numpy.testing import _numpy_tester
|
||||
test = _numpy_tester().test
|
||||
bench = _numpy_tester().bench
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,959 @@
|
||||
"""
|
||||
Abstract base class for the various polynomial Classes.
|
||||
|
||||
The ABCPolyBase class provides the methods needed to implement the common API
|
||||
for the various polynomial classes. It operates as a mixin, but uses the
|
||||
abc module from the stdlib, hence it is only available for Python >= 2.6.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
from abc import ABCMeta, abstractmethod, abstractproperty
|
||||
from numbers import Number
|
||||
|
||||
import numpy as np
|
||||
from . import polyutils as pu
|
||||
|
||||
__all__ = ['ABCPolyBase']
|
||||
|
||||
class ABCPolyBase(object):
|
||||
"""An abstract base class for series classes.
|
||||
|
||||
ABCPolyBase provides the standard Python numerical methods
|
||||
'+', '-', '*', '//', '%', 'divmod', '**', and '()' along with the
|
||||
methods listed below.
|
||||
|
||||
.. versionadded:: 1.9.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coef : array_like
|
||||
Series coefficients in order of increasing degree, i.e.,
|
||||
``(1, 2, 3)`` gives ``1*P_0(x) + 2*P_1(x) + 3*P_2(x)``, where
|
||||
``P_i`` is the basis polynomials of degree ``i``.
|
||||
domain : (2,) array_like, optional
|
||||
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
|
||||
to the interval ``[window[0], window[1]]`` by shifting and scaling.
|
||||
The default value is the derived class domain.
|
||||
window : (2,) array_like, optional
|
||||
Window, see domain for its use. The default value is the
|
||||
derived class window.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
coef : (N,) ndarray
|
||||
Series coefficients in order of increasing degree.
|
||||
domain : (2,) ndarray
|
||||
Domain that is mapped to window.
|
||||
window : (2,) ndarray
|
||||
Window that domain is mapped to.
|
||||
|
||||
Class Attributes
|
||||
----------------
|
||||
maxpower : int
|
||||
Maximum power allowed, i.e., the largest number ``n`` such that
|
||||
``p(x)**n`` is allowed. This is to limit runaway polynomial size.
|
||||
domain : (2,) ndarray
|
||||
Default domain of the class.
|
||||
window : (2,) ndarray
|
||||
Default window of the class.
|
||||
|
||||
"""
|
||||
__metaclass__ = ABCMeta
|
||||
|
||||
# Not hashable
|
||||
__hash__ = None
|
||||
|
||||
# Opt out of numpy ufuncs and Python ops with ndarray subclasses.
|
||||
__array_ufunc__ = None
|
||||
|
||||
# Limit runaway size. T_n^m has degree n*m
|
||||
maxpower = 100
|
||||
|
||||
@abstractproperty
|
||||
def domain(self):
|
||||
pass
|
||||
|
||||
@abstractproperty
|
||||
def window(self):
|
||||
pass
|
||||
|
||||
@abstractproperty
|
||||
def nickname(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _add(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _sub(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _mul(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _div(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _pow(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _val(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _int(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _der(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _fit(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _line(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _roots(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _fromroots(self):
|
||||
pass
|
||||
|
||||
def has_samecoef(self, other):
|
||||
"""Check if coefficients match.
|
||||
|
||||
.. versionadded:: 1.6.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
other : class instance
|
||||
The other class must have the ``coef`` attribute.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool : boolean
|
||||
True if the coefficients are the same, False otherwise.
|
||||
|
||||
"""
|
||||
if len(self.coef) != len(other.coef):
|
||||
return False
|
||||
elif not np.all(self.coef == other.coef):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def has_samedomain(self, other):
|
||||
"""Check if domains match.
|
||||
|
||||
.. versionadded:: 1.6.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
other : class instance
|
||||
The other class must have the ``domain`` attribute.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool : boolean
|
||||
True if the domains are the same, False otherwise.
|
||||
|
||||
"""
|
||||
return np.all(self.domain == other.domain)
|
||||
|
||||
def has_samewindow(self, other):
|
||||
"""Check if windows match.
|
||||
|
||||
.. versionadded:: 1.6.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
other : class instance
|
||||
The other class must have the ``window`` attribute.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool : boolean
|
||||
True if the windows are the same, False otherwise.
|
||||
|
||||
"""
|
||||
return np.all(self.window == other.window)
|
||||
|
||||
def has_sametype(self, other):
|
||||
"""Check if types match.
|
||||
|
||||
.. versionadded:: 1.7.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
other : object
|
||||
Class instance.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool : boolean
|
||||
True if other is same class as self
|
||||
|
||||
"""
|
||||
return isinstance(other, self.__class__)
|
||||
|
||||
def _get_coefficients(self, other):
|
||||
"""Interpret other as polynomial coefficients.
|
||||
|
||||
The `other` argument is checked to see if it is of the same
|
||||
class as self with identical domain and window. If so,
|
||||
return its coefficients, otherwise return `other`.
|
||||
|
||||
.. versionadded:: 1.9.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
other : anything
|
||||
Object to be checked.
|
||||
|
||||
Returns
|
||||
-------
|
||||
coef
|
||||
The coefficients of`other` if it is a compatible instance,
|
||||
of ABCPolyBase, otherwise `other`.
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
When `other` is an incompatible instance of ABCPolyBase.
|
||||
|
||||
"""
|
||||
if isinstance(other, ABCPolyBase):
|
||||
if not isinstance(other, self.__class__):
|
||||
raise TypeError("Polynomial types differ")
|
||||
elif not np.all(self.domain == other.domain):
|
||||
raise TypeError("Domains differ")
|
||||
elif not np.all(self.window == other.window):
|
||||
raise TypeError("Windows differ")
|
||||
return other.coef
|
||||
return other
|
||||
|
||||
def __init__(self, coef, domain=None, window=None):
|
||||
[coef] = pu.as_series([coef], trim=False)
|
||||
self.coef = coef
|
||||
|
||||
if domain is not None:
|
||||
[domain] = pu.as_series([domain], trim=False)
|
||||
if len(domain) != 2:
|
||||
raise ValueError("Domain has wrong number of elements.")
|
||||
self.domain = domain
|
||||
|
||||
if window is not None:
|
||||
[window] = pu.as_series([window], trim=False)
|
||||
if len(window) != 2:
|
||||
raise ValueError("Window has wrong number of elements.")
|
||||
self.window = window
|
||||
|
||||
def __repr__(self):
|
||||
format = "%s(%s, domain=%s, window=%s)"
|
||||
coef = repr(self.coef)[6:-1]
|
||||
domain = repr(self.domain)[6:-1]
|
||||
window = repr(self.window)[6:-1]
|
||||
name = self.__class__.__name__
|
||||
return format % (name, coef, domain, window)
|
||||
|
||||
def __str__(self):
|
||||
format = "%s(%s)"
|
||||
coef = str(self.coef)
|
||||
name = self.nickname
|
||||
return format % (name, coef)
|
||||
|
||||
# Pickle and copy
|
||||
|
||||
def __getstate__(self):
|
||||
ret = self.__dict__.copy()
|
||||
ret['coef'] = self.coef.copy()
|
||||
ret['domain'] = self.domain.copy()
|
||||
ret['window'] = self.window.copy()
|
||||
return ret
|
||||
|
||||
def __setstate__(self, dict):
|
||||
self.__dict__ = dict
|
||||
|
||||
# Call
|
||||
|
||||
def __call__(self, arg):
|
||||
off, scl = pu.mapparms(self.domain, self.window)
|
||||
arg = off + scl*arg
|
||||
return self._val(arg, self.coef)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.coef)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.coef)
|
||||
|
||||
# Numeric properties.
|
||||
|
||||
def __neg__(self):
|
||||
return self.__class__(-self.coef, self.domain, self.window)
|
||||
|
||||
def __pos__(self):
|
||||
return self
|
||||
|
||||
def __add__(self, other):
|
||||
othercoef = self._get_coefficients(other)
|
||||
try:
|
||||
coef = self._add(self.coef, othercoef)
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def __sub__(self, other):
|
||||
othercoef = self._get_coefficients(other)
|
||||
try:
|
||||
coef = self._sub(self.coef, othercoef)
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def __mul__(self, other):
|
||||
othercoef = self._get_coefficients(other)
|
||||
try:
|
||||
coef = self._mul(self.coef, othercoef)
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def __div__(self, other):
|
||||
# set to __floordiv__, /, for now.
|
||||
return self.__floordiv__(other)
|
||||
|
||||
def __truediv__(self, other):
|
||||
# there is no true divide if the rhs is not a Number, although it
|
||||
# could return the first n elements of an infinite series.
|
||||
# It is hard to see where n would come from, though.
|
||||
if not isinstance(other, Number) or isinstance(other, bool):
|
||||
form = "unsupported types for true division: '%s', '%s'"
|
||||
raise TypeError(form % (type(self), type(other)))
|
||||
return self.__floordiv__(other)
|
||||
|
||||
def __floordiv__(self, other):
|
||||
res = self.__divmod__(other)
|
||||
if res is NotImplemented:
|
||||
return res
|
||||
return res[0]
|
||||
|
||||
def __mod__(self, other):
|
||||
res = self.__divmod__(other)
|
||||
if res is NotImplemented:
|
||||
return res
|
||||
return res[1]
|
||||
|
||||
def __divmod__(self, other):
|
||||
othercoef = self._get_coefficients(other)
|
||||
try:
|
||||
quo, rem = self._div(self.coef, othercoef)
|
||||
except ZeroDivisionError as e:
|
||||
raise e
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
quo = self.__class__(quo, self.domain, self.window)
|
||||
rem = self.__class__(rem, self.domain, self.window)
|
||||
return quo, rem
|
||||
|
||||
def __pow__(self, other):
|
||||
coef = self._pow(self.coef, other, maxpower=self.maxpower)
|
||||
res = self.__class__(coef, self.domain, self.window)
|
||||
return res
|
||||
|
||||
def __radd__(self, other):
|
||||
try:
|
||||
coef = self._add(other, self.coef)
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def __rsub__(self, other):
|
||||
try:
|
||||
coef = self._sub(other, self.coef)
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def __rmul__(self, other):
|
||||
try:
|
||||
coef = self._mul(other, self.coef)
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def __rdiv__(self, other):
|
||||
# set to __floordiv__ /.
|
||||
return self.__rfloordiv__(other)
|
||||
|
||||
def __rtruediv__(self, other):
|
||||
# An instance of ABCPolyBase is not considered a
|
||||
# Number.
|
||||
return NotImplemented
|
||||
|
||||
def __rfloordiv__(self, other):
|
||||
res = self.__rdivmod__(other)
|
||||
if res is NotImplemented:
|
||||
return res
|
||||
return res[0]
|
||||
|
||||
def __rmod__(self, other):
|
||||
res = self.__rdivmod__(other)
|
||||
if res is NotImplemented:
|
||||
return res
|
||||
return res[1]
|
||||
|
||||
def __rdivmod__(self, other):
|
||||
try:
|
||||
quo, rem = self._div(other, self.coef)
|
||||
except ZeroDivisionError as e:
|
||||
raise e
|
||||
except Exception:
|
||||
return NotImplemented
|
||||
quo = self.__class__(quo, self.domain, self.window)
|
||||
rem = self.__class__(rem, self.domain, self.window)
|
||||
return quo, rem
|
||||
|
||||
# Enhance me
|
||||
# some augmented arithmetic operations could be added here
|
||||
|
||||
def __eq__(self, other):
|
||||
res = (isinstance(other, self.__class__) and
|
||||
np.all(self.domain == other.domain) and
|
||||
np.all(self.window == other.window) and
|
||||
(self.coef.shape == other.coef.shape) and
|
||||
np.all(self.coef == other.coef))
|
||||
return res
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
#
|
||||
# Extra methods.
|
||||
#
|
||||
|
||||
def copy(self):
|
||||
"""Return a copy.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
Copy of self.
|
||||
|
||||
"""
|
||||
return self.__class__(self.coef, self.domain, self.window)
|
||||
|
||||
def degree(self):
|
||||
"""The degree of the series.
|
||||
|
||||
.. versionadded:: 1.5.0
|
||||
|
||||
Returns
|
||||
-------
|
||||
degree : int
|
||||
Degree of the series, one less than the number of coefficients.
|
||||
|
||||
"""
|
||||
return len(self) - 1
|
||||
|
||||
def cutdeg(self, deg):
|
||||
"""Truncate series to the given degree.
|
||||
|
||||
Reduce the degree of the series to `deg` by discarding the
|
||||
high order terms. If `deg` is greater than the current degree a
|
||||
copy of the current series is returned. This can be useful in least
|
||||
squares where the coefficients of the high degree terms may be very
|
||||
small.
|
||||
|
||||
.. versionadded:: 1.5.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
deg : non-negative int
|
||||
The series is reduced to degree `deg` by discarding the high
|
||||
order terms. The value of `deg` must be a non-negative integer.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
New instance of series with reduced degree.
|
||||
|
||||
"""
|
||||
return self.truncate(deg + 1)
|
||||
|
||||
def trim(self, tol=0):
|
||||
"""Remove trailing coefficients
|
||||
|
||||
Remove trailing coefficients until a coefficient is reached whose
|
||||
absolute value greater than `tol` or the beginning of the series is
|
||||
reached. If all the coefficients would be removed the series is set
|
||||
to ``[0]``. A new series instance is returned with the new
|
||||
coefficients. The current instance remains unchanged.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
tol : non-negative number.
|
||||
All trailing coefficients less than `tol` will be removed.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
Contains the new set of coefficients.
|
||||
|
||||
"""
|
||||
coef = pu.trimcoef(self.coef, tol)
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def truncate(self, size):
|
||||
"""Truncate series to length `size`.
|
||||
|
||||
Reduce the series to length `size` by discarding the high
|
||||
degree terms. The value of `size` must be a positive integer. This
|
||||
can be useful in least squares where the coefficients of the
|
||||
high degree terms may be very small.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
size : positive int
|
||||
The series is reduced to length `size` by discarding the high
|
||||
degree terms. The value of `size` must be a positive integer.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
New instance of series with truncated coefficients.
|
||||
|
||||
"""
|
||||
isize = int(size)
|
||||
if isize != size or isize < 1:
|
||||
raise ValueError("size must be a positive integer")
|
||||
if isize >= len(self.coef):
|
||||
coef = self.coef
|
||||
else:
|
||||
coef = self.coef[:isize]
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def convert(self, domain=None, kind=None, window=None):
|
||||
"""Convert series to a different kind and/or domain and/or window.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
domain : array_like, optional
|
||||
The domain of the converted series. If the value is None,
|
||||
the default domain of `kind` is used.
|
||||
kind : class, optional
|
||||
The polynomial series type class to which the current instance
|
||||
should be converted. If kind is None, then the class of the
|
||||
current instance is used.
|
||||
window : array_like, optional
|
||||
The window of the converted series. If the value is None,
|
||||
the default window of `kind` is used.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
The returned class can be of different type than the current
|
||||
instance and/or have a different domain and/or different
|
||||
window.
|
||||
|
||||
Notes
|
||||
-----
|
||||
Conversion between domains and class types can result in
|
||||
numerically ill defined series.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
"""
|
||||
if kind is None:
|
||||
kind = self.__class__
|
||||
if domain is None:
|
||||
domain = kind.domain
|
||||
if window is None:
|
||||
window = kind.window
|
||||
return self(kind.identity(domain, window=window))
|
||||
|
||||
def mapparms(self):
|
||||
"""Return the mapping parameters.
|
||||
|
||||
The returned values define a linear map ``off + scl*x`` that is
|
||||
applied to the input arguments before the series is evaluated. The
|
||||
map depends on the ``domain`` and ``window``; if the current
|
||||
``domain`` is equal to the ``window`` the resulting map is the
|
||||
identity. If the coefficients of the series instance are to be
|
||||
used by themselves outside this class, then the linear function
|
||||
must be substituted for the ``x`` in the standard representation of
|
||||
the base polynomials.
|
||||
|
||||
Returns
|
||||
-------
|
||||
off, scl : float or complex
|
||||
The mapping function is defined by ``off + scl*x``.
|
||||
|
||||
Notes
|
||||
-----
|
||||
If the current domain is the interval ``[l1, r1]`` and the window
|
||||
is ``[l2, r2]``, then the linear mapping function ``L`` is
|
||||
defined by the equations::
|
||||
|
||||
L(l1) = l2
|
||||
L(r1) = r2
|
||||
|
||||
"""
|
||||
return pu.mapparms(self.domain, self.window)
|
||||
|
||||
def integ(self, m=1, k=[], lbnd=None):
|
||||
"""Integrate.
|
||||
|
||||
Return a series instance that is the definite integral of the
|
||||
current series.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
m : non-negative int
|
||||
The number of integrations to perform.
|
||||
k : array_like
|
||||
Integration constants. The first constant is applied to the
|
||||
first integration, the second to the second, and so on. The
|
||||
list of values must less than or equal to `m` in length and any
|
||||
missing values are set to zero.
|
||||
lbnd : Scalar
|
||||
The lower bound of the definite integral.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
A new series representing the integral. The domain is the same
|
||||
as the domain of the integrated series.
|
||||
|
||||
"""
|
||||
off, scl = self.mapparms()
|
||||
if lbnd is None:
|
||||
lbnd = 0
|
||||
else:
|
||||
lbnd = off + scl*lbnd
|
||||
coef = self._int(self.coef, m, k, lbnd, 1./scl)
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def deriv(self, m=1):
|
||||
"""Differentiate.
|
||||
|
||||
Return a series instance of that is the derivative of the current
|
||||
series.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
m : non-negative int
|
||||
Find the derivative of order `m`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
A new series representing the derivative. The domain is the same
|
||||
as the domain of the differentiated series.
|
||||
|
||||
"""
|
||||
off, scl = self.mapparms()
|
||||
coef = self._der(self.coef, m, scl)
|
||||
return self.__class__(coef, self.domain, self.window)
|
||||
|
||||
def roots(self):
|
||||
"""Return the roots of the series polynomial.
|
||||
|
||||
Compute the roots for the series. Note that the accuracy of the
|
||||
roots decrease the further outside the domain they lie.
|
||||
|
||||
Returns
|
||||
-------
|
||||
roots : ndarray
|
||||
Array containing the roots of the series.
|
||||
|
||||
"""
|
||||
roots = self._roots(self.coef)
|
||||
return pu.mapdomain(roots, self.window, self.domain)
|
||||
|
||||
def linspace(self, n=100, domain=None):
|
||||
"""Return x, y values at equally spaced points in domain.
|
||||
|
||||
Returns the x, y values at `n` linearly spaced points across the
|
||||
domain. Here y is the value of the polynomial at the points x. By
|
||||
default the domain is the same as that of the series instance.
|
||||
This method is intended mostly as a plotting aid.
|
||||
|
||||
.. versionadded:: 1.5.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
n : int, optional
|
||||
Number of point pairs to return. The default value is 100.
|
||||
domain : {None, array_like}, optional
|
||||
If not None, the specified domain is used instead of that of
|
||||
the calling instance. It should be of the form ``[beg,end]``.
|
||||
The default is None which case the class domain is used.
|
||||
|
||||
Returns
|
||||
-------
|
||||
x, y : ndarray
|
||||
x is equal to linspace(self.domain[0], self.domain[1], n) and
|
||||
y is the series evaluated at element of x.
|
||||
|
||||
"""
|
||||
if domain is None:
|
||||
domain = self.domain
|
||||
x = np.linspace(domain[0], domain[1], n)
|
||||
y = self(x)
|
||||
return x, y
|
||||
|
||||
@classmethod
|
||||
def fit(cls, x, y, deg, domain=None, rcond=None, full=False, w=None,
|
||||
window=None):
|
||||
"""Least squares fit to data.
|
||||
|
||||
Return a series instance that is the least squares fit to the data
|
||||
`y` sampled at `x`. The domain of the returned instance can be
|
||||
specified and this will often result in a superior fit with less
|
||||
chance of ill conditioning.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : array_like, shape (M,)
|
||||
x-coordinates of the M sample points ``(x[i], y[i])``.
|
||||
y : array_like, shape (M,) or (M, K)
|
||||
y-coordinates of the sample points. Several data sets of sample
|
||||
points sharing the same x-coordinates can be fitted at once by
|
||||
passing in a 2D-array that contains one dataset per column.
|
||||
deg : int or 1-D array_like
|
||||
Degree(s) of the fitting polynomials. If `deg` is a single integer
|
||||
all terms up to and including the `deg`'th term are included in the
|
||||
fit. For NumPy versions >= 1.11.0 a list of integers specifying the
|
||||
degrees of the terms to include may be used instead.
|
||||
domain : {None, [beg, end], []}, optional
|
||||
Domain to use for the returned series. If ``None``,
|
||||
then a minimal domain that covers the points `x` is chosen. If
|
||||
``[]`` the class domain is used. The default value was the
|
||||
class domain in NumPy 1.4 and ``None`` in later versions.
|
||||
The ``[]`` option was added in numpy 1.5.0.
|
||||
rcond : float, optional
|
||||
Relative condition number of the fit. Singular values smaller
|
||||
than this relative to the largest singular value will be
|
||||
ignored. The default value is len(x)*eps, where eps is the
|
||||
relative precision of the float type, about 2e-16 in most
|
||||
cases.
|
||||
full : bool, optional
|
||||
Switch determining nature of return value. When it is False
|
||||
(the default) just the coefficients are returned, when True
|
||||
diagnostic information from the singular value decomposition is
|
||||
also returned.
|
||||
w : array_like, shape (M,), optional
|
||||
Weights. If not None the contribution of each point
|
||||
``(x[i],y[i])`` to the fit is weighted by `w[i]`. Ideally the
|
||||
weights are chosen so that the errors of the products
|
||||
``w[i]*y[i]`` all have the same variance. The default value is
|
||||
None.
|
||||
|
||||
.. versionadded:: 1.5.0
|
||||
window : {[beg, end]}, optional
|
||||
Window to use for the returned series. The default
|
||||
value is the default class domain
|
||||
|
||||
.. versionadded:: 1.6.0
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
A series that represents the least squares fit to the data and
|
||||
has the domain specified in the call.
|
||||
|
||||
[resid, rank, sv, rcond] : list
|
||||
These values are only returned if `full` = True
|
||||
|
||||
resid -- sum of squared residuals of the least squares fit
|
||||
rank -- the numerical rank of the scaled Vandermonde matrix
|
||||
sv -- singular values of the scaled Vandermonde matrix
|
||||
rcond -- value of `rcond`.
|
||||
|
||||
For more details, see `linalg.lstsq`.
|
||||
|
||||
"""
|
||||
if domain is None:
|
||||
domain = pu.getdomain(x)
|
||||
elif type(domain) is list and len(domain) == 0:
|
||||
domain = cls.domain
|
||||
|
||||
if window is None:
|
||||
window = cls.window
|
||||
|
||||
xnew = pu.mapdomain(x, domain, window)
|
||||
res = cls._fit(xnew, y, deg, w=w, rcond=rcond, full=full)
|
||||
if full:
|
||||
[coef, status] = res
|
||||
return cls(coef, domain=domain, window=window), status
|
||||
else:
|
||||
coef = res
|
||||
return cls(coef, domain=domain, window=window)
|
||||
|
||||
@classmethod
|
||||
def fromroots(cls, roots, domain=[], window=None):
|
||||
"""Return series instance that has the specified roots.
|
||||
|
||||
Returns a series representing the product
|
||||
``(x - r[0])*(x - r[1])*...*(x - r[n-1])``, where ``r`` is a
|
||||
list of roots.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
roots : array_like
|
||||
List of roots.
|
||||
domain : {[], None, array_like}, optional
|
||||
Domain for the resulting series. If None the domain is the
|
||||
interval from the smallest root to the largest. If [] the
|
||||
domain is the class domain. The default is [].
|
||||
window : {None, array_like}, optional
|
||||
Window for the returned series. If None the class window is
|
||||
used. The default is None.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
Series with the specified roots.
|
||||
|
||||
"""
|
||||
[roots] = pu.as_series([roots], trim=False)
|
||||
if domain is None:
|
||||
domain = pu.getdomain(roots)
|
||||
elif type(domain) is list and len(domain) == 0:
|
||||
domain = cls.domain
|
||||
|
||||
if window is None:
|
||||
window = cls.window
|
||||
|
||||
deg = len(roots)
|
||||
off, scl = pu.mapparms(domain, window)
|
||||
rnew = off + scl*roots
|
||||
coef = cls._fromroots(rnew) / scl**deg
|
||||
return cls(coef, domain=domain, window=window)
|
||||
|
||||
@classmethod
|
||||
def identity(cls, domain=None, window=None):
|
||||
"""Identity function.
|
||||
|
||||
If ``p`` is the returned series, then ``p(x) == x`` for all
|
||||
values of x.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
domain : {None, array_like}, optional
|
||||
If given, the array must be of the form ``[beg, end]``, where
|
||||
``beg`` and ``end`` are the endpoints of the domain. If None is
|
||||
given then the class domain is used. The default is None.
|
||||
window : {None, array_like}, optional
|
||||
If given, the resulting array must be if the form
|
||||
``[beg, end]``, where ``beg`` and ``end`` are the endpoints of
|
||||
the window. If None is given then the class window is used. The
|
||||
default is None.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
Series of representing the identity.
|
||||
|
||||
"""
|
||||
if domain is None:
|
||||
domain = cls.domain
|
||||
if window is None:
|
||||
window = cls.window
|
||||
off, scl = pu.mapparms(window, domain)
|
||||
coef = cls._line(off, scl)
|
||||
return cls(coef, domain, window)
|
||||
|
||||
@classmethod
|
||||
def basis(cls, deg, domain=None, window=None):
|
||||
"""Series basis polynomial of degree `deg`.
|
||||
|
||||
Returns the series representing the basis polynomial of degree `deg`.
|
||||
|
||||
.. versionadded:: 1.7.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
deg : int
|
||||
Degree of the basis polynomial for the series. Must be >= 0.
|
||||
domain : {None, array_like}, optional
|
||||
If given, the array must be of the form ``[beg, end]``, where
|
||||
``beg`` and ``end`` are the endpoints of the domain. If None is
|
||||
given then the class domain is used. The default is None.
|
||||
window : {None, array_like}, optional
|
||||
If given, the resulting array must be if the form
|
||||
``[beg, end]``, where ``beg`` and ``end`` are the endpoints of
|
||||
the window. If None is given then the class window is used. The
|
||||
default is None.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
A series with the coefficient of the `deg` term set to one and
|
||||
all others zero.
|
||||
|
||||
"""
|
||||
if domain is None:
|
||||
domain = cls.domain
|
||||
if window is None:
|
||||
window = cls.window
|
||||
ideg = int(deg)
|
||||
|
||||
if ideg != deg or ideg < 0:
|
||||
raise ValueError("deg must be non-negative integer")
|
||||
return cls([0]*ideg + [1], domain, window)
|
||||
|
||||
@classmethod
|
||||
def cast(cls, series, domain=None, window=None):
|
||||
"""Convert series to series of this class.
|
||||
|
||||
The `series` is expected to be an instance of some polynomial
|
||||
series of one of the types supported by by the numpy.polynomial
|
||||
module, but could be some other class that supports the convert
|
||||
method.
|
||||
|
||||
.. versionadded:: 1.7.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
series : series
|
||||
The series instance to be converted.
|
||||
domain : {None, array_like}, optional
|
||||
If given, the array must be of the form ``[beg, end]``, where
|
||||
``beg`` and ``end`` are the endpoints of the domain. If None is
|
||||
given then the class domain is used. The default is None.
|
||||
window : {None, array_like}, optional
|
||||
If given, the resulting array must be if the form
|
||||
``[beg, end]``, where ``beg`` and ``end`` are the endpoints of
|
||||
the window. If None is given then the class window is used. The
|
||||
default is None.
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_series : series
|
||||
A series of the same kind as the calling class and equal to
|
||||
`series` when evaluated.
|
||||
|
||||
See Also
|
||||
--------
|
||||
convert : similar instance method
|
||||
|
||||
"""
|
||||
if domain is None:
|
||||
domain = cls.domain
|
||||
if window is None:
|
||||
window = cls.window
|
||||
return series.convert(domain, cls, window)
|
2190
projecten1/lib/python3.6/site-packages/numpy/polynomial/chebyshev.py
Normal file
2190
projecten1/lib/python3.6/site-packages/numpy/polynomial/chebyshev.py
Normal file
File diff suppressed because it is too large
Load Diff
1853
projecten1/lib/python3.6/site-packages/numpy/polynomial/hermite.py
Normal file
1853
projecten1/lib/python3.6/site-packages/numpy/polynomial/hermite.py
Normal file
File diff suppressed because it is too large
Load Diff
1850
projecten1/lib/python3.6/site-packages/numpy/polynomial/hermite_e.py
Normal file
1850
projecten1/lib/python3.6/site-packages/numpy/polynomial/hermite_e.py
Normal file
File diff suppressed because it is too large
Load Diff
1803
projecten1/lib/python3.6/site-packages/numpy/polynomial/laguerre.py
Normal file
1803
projecten1/lib/python3.6/site-packages/numpy/polynomial/laguerre.py
Normal file
File diff suppressed because it is too large
Load Diff
1833
projecten1/lib/python3.6/site-packages/numpy/polynomial/legendre.py
Normal file
1833
projecten1/lib/python3.6/site-packages/numpy/polynomial/legendre.py
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,412 @@
|
||||
"""
|
||||
Utility classes and functions for the polynomial modules.
|
||||
|
||||
This module provides: error and warning objects; a polynomial base class;
|
||||
and some routines used in both the `polynomial` and `chebyshev` modules.
|
||||
|
||||
Error objects
|
||||
-------------
|
||||
|
||||
.. autosummary::
|
||||
:toctree: generated/
|
||||
|
||||
PolyError base class for this sub-package's errors.
|
||||
PolyDomainError raised when domains are mismatched.
|
||||
|
||||
Warning objects
|
||||
---------------
|
||||
|
||||
.. autosummary::
|
||||
:toctree: generated/
|
||||
|
||||
RankWarning raised in least-squares fit for rank-deficient matrix.
|
||||
|
||||
Base class
|
||||
----------
|
||||
|
||||
.. autosummary::
|
||||
:toctree: generated/
|
||||
|
||||
PolyBase Obsolete base class for the polynomial classes. Do not use.
|
||||
|
||||
Functions
|
||||
---------
|
||||
|
||||
.. autosummary::
|
||||
:toctree: generated/
|
||||
|
||||
as_series convert list of array_likes into 1-D arrays of common type.
|
||||
trimseq remove trailing zeros.
|
||||
trimcoef remove small trailing coefficients.
|
||||
getdomain return the domain appropriate for a given set of abscissae.
|
||||
mapdomain maps points between domains.
|
||||
mapparms parameters of the linear map between domains.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
|
||||
__all__ = [
|
||||
'RankWarning', 'PolyError', 'PolyDomainError', 'as_series', 'trimseq',
|
||||
'trimcoef', 'getdomain', 'mapdomain', 'mapparms', 'PolyBase']
|
||||
|
||||
#
|
||||
# Warnings and Exceptions
|
||||
#
|
||||
|
||||
class RankWarning(UserWarning):
|
||||
"""Issued by chebfit when the design matrix is rank deficient."""
|
||||
pass
|
||||
|
||||
class PolyError(Exception):
|
||||
"""Base class for errors in this module."""
|
||||
pass
|
||||
|
||||
class PolyDomainError(PolyError):
|
||||
"""Issued by the generic Poly class when two domains don't match.
|
||||
|
||||
This is raised when an binary operation is passed Poly objects with
|
||||
different domains.
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
#
|
||||
# Base class for all polynomial types
|
||||
#
|
||||
|
||||
class PolyBase(object):
|
||||
"""
|
||||
Base class for all polynomial types.
|
||||
|
||||
Deprecated in numpy 1.9.0, use the abstract
|
||||
ABCPolyBase class instead. Note that the latter
|
||||
requires a number of virtual functions to be
|
||||
implemented.
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
#
|
||||
# Helper functions to convert inputs to 1-D arrays
|
||||
#
|
||||
def trimseq(seq):
|
||||
"""Remove small Poly series coefficients.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
seq : sequence
|
||||
Sequence of Poly series coefficients. This routine fails for
|
||||
empty sequences.
|
||||
|
||||
Returns
|
||||
-------
|
||||
series : sequence
|
||||
Subsequence with trailing zeros removed. If the resulting sequence
|
||||
would be empty, return the first element. The returned sequence may
|
||||
or may not be a view.
|
||||
|
||||
Notes
|
||||
-----
|
||||
Do not lose the type info if the sequence contains unknown objects.
|
||||
|
||||
"""
|
||||
if len(seq) == 0:
|
||||
return seq
|
||||
else:
|
||||
for i in range(len(seq) - 1, -1, -1):
|
||||
if seq[i] != 0:
|
||||
break
|
||||
return seq[:i+1]
|
||||
|
||||
|
||||
def as_series(alist, trim=True):
|
||||
"""
|
||||
Return argument as a list of 1-d arrays.
|
||||
|
||||
The returned list contains array(s) of dtype double, complex double, or
|
||||
object. A 1-d argument of shape ``(N,)`` is parsed into ``N`` arrays of
|
||||
size one; a 2-d argument of shape ``(M,N)`` is parsed into ``M`` arrays
|
||||
of size ``N`` (i.e., is "parsed by row"); and a higher dimensional array
|
||||
raises a Value Error if it is not first reshaped into either a 1-d or 2-d
|
||||
array.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
alist : array_like
|
||||
A 1- or 2-d array_like
|
||||
trim : boolean, optional
|
||||
When True, trailing zeros are removed from the inputs.
|
||||
When False, the inputs are passed through intact.
|
||||
|
||||
Returns
|
||||
-------
|
||||
[a1, a2,...] : list of 1-D arrays
|
||||
A copy of the input data as a list of 1-d arrays.
|
||||
|
||||
Raises
|
||||
------
|
||||
ValueError
|
||||
Raised when `as_series` cannot convert its input to 1-d arrays, or at
|
||||
least one of the resulting arrays is empty.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy.polynomial import polyutils as pu
|
||||
>>> a = np.arange(4)
|
||||
>>> pu.as_series(a)
|
||||
[array([ 0.]), array([ 1.]), array([ 2.]), array([ 3.])]
|
||||
>>> b = np.arange(6).reshape((2,3))
|
||||
>>> pu.as_series(b)
|
||||
[array([ 0., 1., 2.]), array([ 3., 4., 5.])]
|
||||
|
||||
>>> pu.as_series((1, np.arange(3), np.arange(2, dtype=np.float16)))
|
||||
[array([ 1.]), array([ 0., 1., 2.]), array([ 0., 1.])]
|
||||
|
||||
>>> pu.as_series([2, [1.1, 0.]])
|
||||
[array([ 2.]), array([ 1.1])]
|
||||
|
||||
>>> pu.as_series([2, [1.1, 0.]], trim=False)
|
||||
[array([ 2.]), array([ 1.1, 0. ])]
|
||||
|
||||
"""
|
||||
arrays = [np.array(a, ndmin=1, copy=0) for a in alist]
|
||||
if min([a.size for a in arrays]) == 0:
|
||||
raise ValueError("Coefficient array is empty")
|
||||
if any([a.ndim != 1 for a in arrays]):
|
||||
raise ValueError("Coefficient array is not 1-d")
|
||||
if trim:
|
||||
arrays = [trimseq(a) for a in arrays]
|
||||
|
||||
if any([a.dtype == np.dtype(object) for a in arrays]):
|
||||
ret = []
|
||||
for a in arrays:
|
||||
if a.dtype != np.dtype(object):
|
||||
tmp = np.empty(len(a), dtype=np.dtype(object))
|
||||
tmp[:] = a[:]
|
||||
ret.append(tmp)
|
||||
else:
|
||||
ret.append(a.copy())
|
||||
else:
|
||||
try:
|
||||
dtype = np.common_type(*arrays)
|
||||
except Exception:
|
||||
raise ValueError("Coefficient arrays have no common type")
|
||||
ret = [np.array(a, copy=1, dtype=dtype) for a in arrays]
|
||||
return ret
|
||||
|
||||
|
||||
def trimcoef(c, tol=0):
|
||||
"""
|
||||
Remove "small" "trailing" coefficients from a polynomial.
|
||||
|
||||
"Small" means "small in absolute value" and is controlled by the
|
||||
parameter `tol`; "trailing" means highest order coefficient(s), e.g., in
|
||||
``[0, 1, 1, 0, 0]`` (which represents ``0 + x + x**2 + 0*x**3 + 0*x**4``)
|
||||
both the 3-rd and 4-th order coefficients would be "trimmed."
|
||||
|
||||
Parameters
|
||||
----------
|
||||
c : array_like
|
||||
1-d array of coefficients, ordered from lowest order to highest.
|
||||
tol : number, optional
|
||||
Trailing (i.e., highest order) elements with absolute value less
|
||||
than or equal to `tol` (default value is zero) are removed.
|
||||
|
||||
Returns
|
||||
-------
|
||||
trimmed : ndarray
|
||||
1-d array with trailing zeros removed. If the resulting series
|
||||
would be empty, a series containing a single zero is returned.
|
||||
|
||||
Raises
|
||||
------
|
||||
ValueError
|
||||
If `tol` < 0
|
||||
|
||||
See Also
|
||||
--------
|
||||
trimseq
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy.polynomial import polyutils as pu
|
||||
>>> pu.trimcoef((0,0,3,0,5,0,0))
|
||||
array([ 0., 0., 3., 0., 5.])
|
||||
>>> pu.trimcoef((0,0,1e-3,0,1e-5,0,0),1e-3) # item == tol is trimmed
|
||||
array([ 0.])
|
||||
>>> i = complex(0,1) # works for complex
|
||||
>>> pu.trimcoef((3e-4,1e-3*(1-i),5e-4,2e-5*(1+i)), 1e-3)
|
||||
array([ 0.0003+0.j , 0.0010-0.001j])
|
||||
|
||||
"""
|
||||
if tol < 0:
|
||||
raise ValueError("tol must be non-negative")
|
||||
|
||||
[c] = as_series([c])
|
||||
[ind] = np.nonzero(np.abs(c) > tol)
|
||||
if len(ind) == 0:
|
||||
return c[:1]*0
|
||||
else:
|
||||
return c[:ind[-1] + 1].copy()
|
||||
|
||||
def getdomain(x):
|
||||
"""
|
||||
Return a domain suitable for given abscissae.
|
||||
|
||||
Find a domain suitable for a polynomial or Chebyshev series
|
||||
defined at the values supplied.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : array_like
|
||||
1-d array of abscissae whose domain will be determined.
|
||||
|
||||
Returns
|
||||
-------
|
||||
domain : ndarray
|
||||
1-d array containing two values. If the inputs are complex, then
|
||||
the two returned points are the lower left and upper right corners
|
||||
of the smallest rectangle (aligned with the axes) in the complex
|
||||
plane containing the points `x`. If the inputs are real, then the
|
||||
two points are the ends of the smallest interval containing the
|
||||
points `x`.
|
||||
|
||||
See Also
|
||||
--------
|
||||
mapparms, mapdomain
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy.polynomial import polyutils as pu
|
||||
>>> points = np.arange(4)**2 - 5; points
|
||||
array([-5, -4, -1, 4])
|
||||
>>> pu.getdomain(points)
|
||||
array([-5., 4.])
|
||||
>>> c = np.exp(complex(0,1)*np.pi*np.arange(12)/6) # unit circle
|
||||
>>> pu.getdomain(c)
|
||||
array([-1.-1.j, 1.+1.j])
|
||||
|
||||
"""
|
||||
[x] = as_series([x], trim=False)
|
||||
if x.dtype.char in np.typecodes['Complex']:
|
||||
rmin, rmax = x.real.min(), x.real.max()
|
||||
imin, imax = x.imag.min(), x.imag.max()
|
||||
return np.array((complex(rmin, imin), complex(rmax, imax)))
|
||||
else:
|
||||
return np.array((x.min(), x.max()))
|
||||
|
||||
def mapparms(old, new):
|
||||
"""
|
||||
Linear map parameters between domains.
|
||||
|
||||
Return the parameters of the linear map ``offset + scale*x`` that maps
|
||||
`old` to `new` such that ``old[i] -> new[i]``, ``i = 0, 1``.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
old, new : array_like
|
||||
Domains. Each domain must (successfully) convert to a 1-d array
|
||||
containing precisely two values.
|
||||
|
||||
Returns
|
||||
-------
|
||||
offset, scale : scalars
|
||||
The map ``L(x) = offset + scale*x`` maps the first domain to the
|
||||
second.
|
||||
|
||||
See Also
|
||||
--------
|
||||
getdomain, mapdomain
|
||||
|
||||
Notes
|
||||
-----
|
||||
Also works for complex numbers, and thus can be used to calculate the
|
||||
parameters required to map any line in the complex plane to any other
|
||||
line therein.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy.polynomial import polyutils as pu
|
||||
>>> pu.mapparms((-1,1),(-1,1))
|
||||
(0.0, 1.0)
|
||||
>>> pu.mapparms((1,-1),(-1,1))
|
||||
(0.0, -1.0)
|
||||
>>> i = complex(0,1)
|
||||
>>> pu.mapparms((-i,-1),(1,i))
|
||||
((1+1j), (1+0j))
|
||||
|
||||
"""
|
||||
oldlen = old[1] - old[0]
|
||||
newlen = new[1] - new[0]
|
||||
off = (old[1]*new[0] - old[0]*new[1])/oldlen
|
||||
scl = newlen/oldlen
|
||||
return off, scl
|
||||
|
||||
def mapdomain(x, old, new):
|
||||
"""
|
||||
Apply linear map to input points.
|
||||
|
||||
The linear map ``offset + scale*x`` that maps the domain `old` to
|
||||
the domain `new` is applied to the points `x`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : array_like
|
||||
Points to be mapped. If `x` is a subtype of ndarray the subtype
|
||||
will be preserved.
|
||||
old, new : array_like
|
||||
The two domains that determine the map. Each must (successfully)
|
||||
convert to 1-d arrays containing precisely two values.
|
||||
|
||||
Returns
|
||||
-------
|
||||
x_out : ndarray
|
||||
Array of points of the same shape as `x`, after application of the
|
||||
linear map between the two domains.
|
||||
|
||||
See Also
|
||||
--------
|
||||
getdomain, mapparms
|
||||
|
||||
Notes
|
||||
-----
|
||||
Effectively, this implements:
|
||||
|
||||
.. math ::
|
||||
x\\_out = new[0] + m(x - old[0])
|
||||
|
||||
where
|
||||
|
||||
.. math ::
|
||||
m = \\frac{new[1]-new[0]}{old[1]-old[0]}
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy.polynomial import polyutils as pu
|
||||
>>> old_domain = (-1,1)
|
||||
>>> new_domain = (0,2*np.pi)
|
||||
>>> x = np.linspace(-1,1,6); x
|
||||
array([-1. , -0.6, -0.2, 0.2, 0.6, 1. ])
|
||||
>>> x_out = pu.mapdomain(x, old_domain, new_domain); x_out
|
||||
array([ 0. , 1.25663706, 2.51327412, 3.76991118, 5.02654825,
|
||||
6.28318531])
|
||||
>>> x - pu.mapdomain(x_out, new_domain, old_domain)
|
||||
array([ 0., 0., 0., 0., 0., 0.])
|
||||
|
||||
Also works for complex numbers (and thus can be used to map any line in
|
||||
the complex plane to any other line therein).
|
||||
|
||||
>>> i = complex(0,1)
|
||||
>>> old = (-1 - i, 1 + i)
|
||||
>>> new = (-1 + i, 1 - i)
|
||||
>>> z = np.linspace(old[0], old[1], 6); z
|
||||
array([-1.0-1.j , -0.6-0.6j, -0.2-0.2j, 0.2+0.2j, 0.6+0.6j, 1.0+1.j ])
|
||||
>>> new_z = P.mapdomain(z, old, new); new_z
|
||||
array([-1.0+1.j , -0.6+0.6j, -0.2+0.2j, 0.2-0.2j, 0.6-0.6j, 1.0-1.j ])
|
||||
|
||||
"""
|
||||
x = np.asanyarray(x)
|
||||
off, scl = mapparms(old, new)
|
||||
return off + scl*x
|
@@ -0,0 +1,11 @@
|
||||
from __future__ import division, print_function
|
||||
|
||||
def configuration(parent_package='',top_path=None):
|
||||
from numpy.distutils.misc_util import Configuration
|
||||
config = Configuration('polynomial', parent_package, top_path)
|
||||
config.add_data_dir('tests')
|
||||
return config
|
||||
|
||||
if __name__ == '__main__':
|
||||
from numpy.distutils.core import setup
|
||||
setup(configuration=configuration)
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,614 @@
|
||||
"""Tests for chebyshev module.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
import numpy.polynomial.chebyshev as cheb
|
||||
from numpy.polynomial.polynomial import polyval
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite
|
||||
)
|
||||
|
||||
|
||||
def trim(x):
|
||||
return cheb.chebtrim(x, tol=1e-6)
|
||||
|
||||
T0 = [1]
|
||||
T1 = [0, 1]
|
||||
T2 = [-1, 0, 2]
|
||||
T3 = [0, -3, 0, 4]
|
||||
T4 = [1, 0, -8, 0, 8]
|
||||
T5 = [0, 5, 0, -20, 0, 16]
|
||||
T6 = [-1, 0, 18, 0, -48, 0, 32]
|
||||
T7 = [0, -7, 0, 56, 0, -112, 0, 64]
|
||||
T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128]
|
||||
T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
|
||||
|
||||
Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
|
||||
|
||||
|
||||
class TestPrivate(object):
|
||||
|
||||
def test__cseries_to_zseries(self):
|
||||
for i in range(5):
|
||||
inp = np.array([2] + [1]*i, np.double)
|
||||
tgt = np.array([.5]*i + [2] + [.5]*i, np.double)
|
||||
res = cheb._cseries_to_zseries(inp)
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test__zseries_to_cseries(self):
|
||||
for i in range(5):
|
||||
inp = np.array([.5]*i + [2] + [.5]*i, np.double)
|
||||
tgt = np.array([2] + [1]*i, np.double)
|
||||
res = cheb._zseries_to_cseries(inp)
|
||||
assert_equal(res, tgt)
|
||||
|
||||
|
||||
class TestConstants(object):
|
||||
|
||||
def test_chebdomain(self):
|
||||
assert_equal(cheb.chebdomain, [-1, 1])
|
||||
|
||||
def test_chebzero(self):
|
||||
assert_equal(cheb.chebzero, [0])
|
||||
|
||||
def test_chebone(self):
|
||||
assert_equal(cheb.chebone, [1])
|
||||
|
||||
def test_chebx(self):
|
||||
assert_equal(cheb.chebx, [0, 1])
|
||||
|
||||
|
||||
class TestArithmetic(object):
|
||||
|
||||
def test_chebadd(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] += 1
|
||||
res = cheb.chebadd([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_chebsub(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] -= 1
|
||||
res = cheb.chebsub([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_chebmulx(self):
|
||||
assert_equal(cheb.chebmulx([0]), [0])
|
||||
assert_equal(cheb.chebmulx([1]), [0, 1])
|
||||
for i in range(1, 5):
|
||||
ser = [0]*i + [1]
|
||||
tgt = [0]*(i - 1) + [.5, 0, .5]
|
||||
assert_equal(cheb.chebmulx(ser), tgt)
|
||||
|
||||
def test_chebmul(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(i + j + 1)
|
||||
tgt[i + j] += .5
|
||||
tgt[abs(i - j)] += .5
|
||||
res = cheb.chebmul([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_chebdiv(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
ci = [0]*i + [1]
|
||||
cj = [0]*j + [1]
|
||||
tgt = cheb.chebadd(ci, cj)
|
||||
quo, rem = cheb.chebdiv(tgt, ci)
|
||||
res = cheb.chebadd(cheb.chebmul(quo, ci), rem)
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
|
||||
class TestEvaluation(object):
|
||||
# coefficients of 1 + 2*x + 3*x**2
|
||||
c1d = np.array([2.5, 2., 1.5])
|
||||
c2d = np.einsum('i,j->ij', c1d, c1d)
|
||||
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
||||
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
y = polyval(x, [1., 2., 3.])
|
||||
|
||||
def test_chebval(self):
|
||||
#check empty input
|
||||
assert_equal(cheb.chebval([], [1]).size, 0)
|
||||
|
||||
#check normal input)
|
||||
x = np.linspace(-1, 1)
|
||||
y = [polyval(x, c) for c in Tlist]
|
||||
for i in range(10):
|
||||
msg = "At i=%d" % i
|
||||
tgt = y[i]
|
||||
res = cheb.chebval(x, [0]*i + [1])
|
||||
assert_almost_equal(res, tgt, err_msg=msg)
|
||||
|
||||
#check that shape is preserved
|
||||
for i in range(3):
|
||||
dims = [2]*i
|
||||
x = np.zeros(dims)
|
||||
assert_equal(cheb.chebval(x, [1]).shape, dims)
|
||||
assert_equal(cheb.chebval(x, [1, 0]).shape, dims)
|
||||
assert_equal(cheb.chebval(x, [1, 0, 0]).shape, dims)
|
||||
|
||||
def test_chebval2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, cheb.chebval2d, x1, x2[:2], self.c2d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2
|
||||
res = cheb.chebval2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = cheb.chebval2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_chebval3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, cheb.chebval3d, x1, x2, x3[:2], self.c3d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2*y3
|
||||
res = cheb.chebval3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = cheb.chebval3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_chebgrid2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j->ij', y1, y2)
|
||||
res = cheb.chebgrid2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = cheb.chebgrid2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3)*2)
|
||||
|
||||
def test_chebgrid3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
||||
res = cheb.chebgrid3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = cheb.chebgrid3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3)*3)
|
||||
|
||||
|
||||
class TestIntegral(object):
|
||||
|
||||
def test_chebint(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, cheb.chebint, [0], .5)
|
||||
assert_raises(ValueError, cheb.chebint, [0], -1)
|
||||
assert_raises(ValueError, cheb.chebint, [0], 1, [0, 0])
|
||||
assert_raises(ValueError, cheb.chebint, [0], lbnd=[0])
|
||||
assert_raises(ValueError, cheb.chebint, [0], scl=[0])
|
||||
assert_raises(ValueError, cheb.chebint, [0], axis=.5)
|
||||
|
||||
# test integration of zero polynomial
|
||||
for i in range(2, 5):
|
||||
k = [0]*(i - 2) + [1]
|
||||
res = cheb.chebint([0], m=i, k=k)
|
||||
assert_almost_equal(res, [0, 1])
|
||||
|
||||
# check single integration with integration constant
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [1/scl]
|
||||
chebpol = cheb.poly2cheb(pol)
|
||||
chebint = cheb.chebint(chebpol, m=1, k=[i])
|
||||
res = cheb.cheb2poly(chebint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check single integration with integration constant and lbnd
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
chebpol = cheb.poly2cheb(pol)
|
||||
chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1)
|
||||
assert_almost_equal(cheb.chebval(-1, chebint), i)
|
||||
|
||||
# check single integration with integration constant and scaling
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [2/scl]
|
||||
chebpol = cheb.poly2cheb(pol)
|
||||
chebint = cheb.chebint(chebpol, m=1, k=[i], scl=2)
|
||||
res = cheb.cheb2poly(chebint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with default k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = cheb.chebint(tgt, m=1)
|
||||
res = cheb.chebint(pol, m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with defined k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = cheb.chebint(tgt, m=1, k=[k])
|
||||
res = cheb.chebint(pol, m=j, k=list(range(j)))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with lbnd
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1)
|
||||
res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = cheb.chebint(tgt, m=1, k=[k], scl=2)
|
||||
res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_chebint_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([cheb.chebint(c) for c in c2d.T]).T
|
||||
res = cheb.chebint(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([cheb.chebint(c) for c in c2d])
|
||||
res = cheb.chebint(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([cheb.chebint(c, k=3) for c in c2d])
|
||||
res = cheb.chebint(c2d, k=3, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestDerivative(object):
|
||||
|
||||
def test_chebder(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, cheb.chebder, [0], .5)
|
||||
assert_raises(ValueError, cheb.chebder, [0], -1)
|
||||
|
||||
# check that zeroth derivative does nothing
|
||||
for i in range(5):
|
||||
tgt = [0]*i + [1]
|
||||
res = cheb.chebder(tgt, m=0)
|
||||
assert_equal(trim(res), trim(tgt))
|
||||
|
||||
# check that derivation is the inverse of integration
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = cheb.chebder(cheb.chebint(tgt, m=j), m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check derivation with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = cheb.chebder(cheb.chebint(tgt, m=j, scl=2), m=j, scl=.5)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_chebder_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([cheb.chebder(c) for c in c2d.T]).T
|
||||
res = cheb.chebder(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([cheb.chebder(c) for c in c2d])
|
||||
res = cheb.chebder(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestVander(object):
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
|
||||
def test_chebvander(self):
|
||||
# check for 1d x
|
||||
x = np.arange(3)
|
||||
v = cheb.chebvander(x, 3)
|
||||
assert_(v.shape == (3, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], cheb.chebval(x, coef))
|
||||
|
||||
# check for 2d x
|
||||
x = np.array([[1, 2], [3, 4], [5, 6]])
|
||||
v = cheb.chebvander(x, 3)
|
||||
assert_(v.shape == (3, 2, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], cheb.chebval(x, coef))
|
||||
|
||||
def test_chebvander2d(self):
|
||||
# also tests chebval2d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3))
|
||||
van = cheb.chebvander2d(x1, x2, [1, 2])
|
||||
tgt = cheb.chebval2d(x1, x2, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = cheb.chebvander2d([x1], [x2], [1, 2])
|
||||
assert_(van.shape == (1, 5, 6))
|
||||
|
||||
def test_chebvander3d(self):
|
||||
# also tests chebval3d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3, 4))
|
||||
van = cheb.chebvander3d(x1, x2, x3, [1, 2, 3])
|
||||
tgt = cheb.chebval3d(x1, x2, x3, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = cheb.chebvander3d([x1], [x2], [x3], [1, 2, 3])
|
||||
assert_(van.shape == (1, 5, 24))
|
||||
|
||||
|
||||
class TestFitting(object):
|
||||
|
||||
def test_chebfit(self):
|
||||
def f(x):
|
||||
return x*(x - 1)*(x - 2)
|
||||
|
||||
def f2(x):
|
||||
return x**4 + x**2 + 1
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, cheb.chebfit, [1], [1], -1)
|
||||
assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0)
|
||||
assert_raises(TypeError, cheb.chebfit, [], [1], 0)
|
||||
assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0)
|
||||
assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0)
|
||||
assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0)
|
||||
assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]])
|
||||
assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1, 1])
|
||||
assert_raises(ValueError, cheb.chebfit, [1], [1], [-1,])
|
||||
assert_raises(ValueError, cheb.chebfit, [1], [1], [2, -1, 6])
|
||||
assert_raises(TypeError, cheb.chebfit, [1], [1], [])
|
||||
|
||||
# Test fit
|
||||
x = np.linspace(0, 2)
|
||||
y = f(x)
|
||||
#
|
||||
coef3 = cheb.chebfit(x, y, 3)
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(cheb.chebval(x, coef3), y)
|
||||
coef3 = cheb.chebfit(x, y, [0, 1, 2, 3])
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(cheb.chebval(x, coef3), y)
|
||||
#
|
||||
coef4 = cheb.chebfit(x, y, 4)
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(cheb.chebval(x, coef4), y)
|
||||
coef4 = cheb.chebfit(x, y, [0, 1, 2, 3, 4])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(cheb.chebval(x, coef4), y)
|
||||
# check things still work if deg is not in strict increasing
|
||||
coef4 = cheb.chebfit(x, y, [2, 3, 4, 1, 0])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(cheb.chebval(x, coef4), y)
|
||||
#
|
||||
coef2d = cheb.chebfit(x, np.array([y, y]).T, 3)
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
coef2d = cheb.chebfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
# test weighting
|
||||
w = np.zeros_like(x)
|
||||
yw = y.copy()
|
||||
w[1::2] = 1
|
||||
y[0::2] = 0
|
||||
wcoef3 = cheb.chebfit(x, yw, 3, w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
wcoef3 = cheb.chebfit(x, yw, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
#
|
||||
wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, 3, w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
# test scaling with complex values x points whose square
|
||||
# is zero when summed.
|
||||
x = [1, 1j, -1, -1j]
|
||||
assert_almost_equal(cheb.chebfit(x, x, 1), [0, 1])
|
||||
assert_almost_equal(cheb.chebfit(x, x, [0, 1]), [0, 1])
|
||||
# test fitting only even polynomials
|
||||
x = np.linspace(-1, 1)
|
||||
y = f2(x)
|
||||
coef1 = cheb.chebfit(x, y, 4)
|
||||
assert_almost_equal(cheb.chebval(x, coef1), y)
|
||||
coef2 = cheb.chebfit(x, y, [0, 2, 4])
|
||||
assert_almost_equal(cheb.chebval(x, coef2), y)
|
||||
assert_almost_equal(coef1, coef2)
|
||||
|
||||
|
||||
class TestInterpolate(object):
|
||||
|
||||
def f(self, x):
|
||||
return x * (x - 1) * (x - 2)
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, cheb.chebinterpolate, self.f, -1)
|
||||
assert_raises(TypeError, cheb.chebinterpolate, self.f, 10.)
|
||||
|
||||
def test_dimensions(self):
|
||||
for deg in range(1, 5):
|
||||
assert_(cheb.chebinterpolate(self.f, deg).shape == (deg + 1,))
|
||||
|
||||
def test_approximation(self):
|
||||
|
||||
def powx(x, p):
|
||||
return x**p
|
||||
|
||||
x = np.linspace(-1, 1, 10)
|
||||
for deg in range(0, 10):
|
||||
for p in range(0, deg + 1):
|
||||
c = cheb.chebinterpolate(powx, deg, (p,))
|
||||
assert_almost_equal(cheb.chebval(x, c), powx(x, p), decimal=12)
|
||||
|
||||
|
||||
class TestCompanion(object):
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, cheb.chebcompanion, [])
|
||||
assert_raises(ValueError, cheb.chebcompanion, [1])
|
||||
|
||||
def test_dimensions(self):
|
||||
for i in range(1, 5):
|
||||
coef = [0]*i + [1]
|
||||
assert_(cheb.chebcompanion(coef).shape == (i, i))
|
||||
|
||||
def test_linear_root(self):
|
||||
assert_(cheb.chebcompanion([1, 2])[0, 0] == -.5)
|
||||
|
||||
|
||||
class TestGauss(object):
|
||||
|
||||
def test_100(self):
|
||||
x, w = cheb.chebgauss(100)
|
||||
|
||||
# test orthogonality. Note that the results need to be normalized,
|
||||
# otherwise the huge values that can arise from fast growing
|
||||
# functions like Laguerre can be very confusing.
|
||||
v = cheb.chebvander(x, 99)
|
||||
vv = np.dot(v.T * w, v)
|
||||
vd = 1/np.sqrt(vv.diagonal())
|
||||
vv = vd[:, None] * vv * vd
|
||||
assert_almost_equal(vv, np.eye(100))
|
||||
|
||||
# check that the integral of 1 is correct
|
||||
tgt = np.pi
|
||||
assert_almost_equal(w.sum(), tgt)
|
||||
|
||||
|
||||
class TestMisc(object):
|
||||
|
||||
def test_chebfromroots(self):
|
||||
res = cheb.chebfromroots([])
|
||||
assert_almost_equal(trim(res), [1])
|
||||
for i in range(1, 5):
|
||||
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
|
||||
tgt = [0]*i + [1]
|
||||
res = cheb.chebfromroots(roots)*2**(i-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_chebroots(self):
|
||||
assert_almost_equal(cheb.chebroots([1]), [])
|
||||
assert_almost_equal(cheb.chebroots([1, 2]), [-.5])
|
||||
for i in range(2, 5):
|
||||
tgt = np.linspace(-1, 1, i)
|
||||
res = cheb.chebroots(cheb.chebfromroots(tgt))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_chebtrim(self):
|
||||
coef = [2, -1, 1, 0]
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, cheb.chebtrim, coef, -1)
|
||||
|
||||
# Test results
|
||||
assert_equal(cheb.chebtrim(coef), coef[:-1])
|
||||
assert_equal(cheb.chebtrim(coef, 1), coef[:-3])
|
||||
assert_equal(cheb.chebtrim(coef, 2), [0])
|
||||
|
||||
def test_chebline(self):
|
||||
assert_equal(cheb.chebline(3, 4), [3, 4])
|
||||
|
||||
def test_cheb2poly(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(cheb.cheb2poly([0]*i + [1]), Tlist[i])
|
||||
|
||||
def test_poly2cheb(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1])
|
||||
|
||||
def test_weight(self):
|
||||
x = np.linspace(-1, 1, 11)[1:-1]
|
||||
tgt = 1./(np.sqrt(1 + x) * np.sqrt(1 - x))
|
||||
res = cheb.chebweight(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
def test_chebpts1(self):
|
||||
#test exceptions
|
||||
assert_raises(ValueError, cheb.chebpts1, 1.5)
|
||||
assert_raises(ValueError, cheb.chebpts1, 0)
|
||||
|
||||
#test points
|
||||
tgt = [0]
|
||||
assert_almost_equal(cheb.chebpts1(1), tgt)
|
||||
tgt = [-0.70710678118654746, 0.70710678118654746]
|
||||
assert_almost_equal(cheb.chebpts1(2), tgt)
|
||||
tgt = [-0.86602540378443871, 0, 0.86602540378443871]
|
||||
assert_almost_equal(cheb.chebpts1(3), tgt)
|
||||
tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325]
|
||||
assert_almost_equal(cheb.chebpts1(4), tgt)
|
||||
|
||||
def test_chebpts2(self):
|
||||
#test exceptions
|
||||
assert_raises(ValueError, cheb.chebpts2, 1.5)
|
||||
assert_raises(ValueError, cheb.chebpts2, 1)
|
||||
|
||||
#test points
|
||||
tgt = [-1, 1]
|
||||
assert_almost_equal(cheb.chebpts2(2), tgt)
|
||||
tgt = [-1, 0, 1]
|
||||
assert_almost_equal(cheb.chebpts2(3), tgt)
|
||||
tgt = [-1, -0.5, .5, 1]
|
||||
assert_almost_equal(cheb.chebpts2(4), tgt)
|
||||
tgt = [-1.0, -0.707106781187, 0, 0.707106781187, 1.0]
|
||||
assert_almost_equal(cheb.chebpts2(5), tgt)
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,612 @@
|
||||
"""Test inter-conversion of different polynomial classes.
|
||||
|
||||
This tests the convert and cast methods of all the polynomial classes.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import operator as op
|
||||
from numbers import Number
|
||||
|
||||
import numpy as np
|
||||
from numpy.polynomial import (
|
||||
Polynomial, Legendre, Chebyshev, Laguerre, Hermite, HermiteE)
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite)
|
||||
from numpy.compat import long
|
||||
|
||||
|
||||
classes = (
|
||||
Polynomial, Legendre, Chebyshev, Laguerre,
|
||||
Hermite, HermiteE)
|
||||
|
||||
|
||||
def test_class_methods():
|
||||
for Poly1 in classes:
|
||||
for Poly2 in classes:
|
||||
yield check_conversion, Poly1, Poly2
|
||||
yield check_cast, Poly1, Poly2
|
||||
for Poly in classes:
|
||||
yield check_call, Poly
|
||||
yield check_identity, Poly
|
||||
yield check_basis, Poly
|
||||
yield check_fromroots, Poly
|
||||
yield check_fit, Poly
|
||||
yield check_equal, Poly
|
||||
yield check_not_equal, Poly
|
||||
yield check_add, Poly
|
||||
yield check_sub, Poly
|
||||
yield check_mul, Poly
|
||||
yield check_floordiv, Poly
|
||||
yield check_truediv, Poly
|
||||
yield check_mod, Poly
|
||||
yield check_divmod, Poly
|
||||
yield check_pow, Poly
|
||||
yield check_integ, Poly
|
||||
yield check_deriv, Poly
|
||||
yield check_roots, Poly
|
||||
yield check_linspace, Poly
|
||||
yield check_mapparms, Poly
|
||||
yield check_degree, Poly
|
||||
yield check_copy, Poly
|
||||
yield check_cutdeg, Poly
|
||||
yield check_truncate, Poly
|
||||
yield check_trim, Poly
|
||||
yield check_ufunc_override, Poly
|
||||
|
||||
|
||||
#
|
||||
# helper functions
|
||||
#
|
||||
random = np.random.random
|
||||
|
||||
|
||||
def assert_poly_almost_equal(p1, p2, msg=""):
|
||||
try:
|
||||
assert_(np.all(p1.domain == p2.domain))
|
||||
assert_(np.all(p1.window == p2.window))
|
||||
assert_almost_equal(p1.coef, p2.coef)
|
||||
except AssertionError:
|
||||
msg = "Result: %s\nTarget: %s", (p1, p2)
|
||||
raise AssertionError(msg)
|
||||
|
||||
|
||||
#
|
||||
# conversion methods that depend on two classes
|
||||
#
|
||||
|
||||
|
||||
def check_conversion(Poly1, Poly2):
|
||||
x = np.linspace(0, 1, 10)
|
||||
coef = random((3,))
|
||||
|
||||
d1 = Poly1.domain + random((2,))*.25
|
||||
w1 = Poly1.window + random((2,))*.25
|
||||
p1 = Poly1(coef, domain=d1, window=w1)
|
||||
|
||||
d2 = Poly2.domain + random((2,))*.25
|
||||
w2 = Poly2.window + random((2,))*.25
|
||||
p2 = p1.convert(kind=Poly2, domain=d2, window=w2)
|
||||
|
||||
assert_almost_equal(p2.domain, d2)
|
||||
assert_almost_equal(p2.window, w2)
|
||||
assert_almost_equal(p2(x), p1(x))
|
||||
|
||||
|
||||
def check_cast(Poly1, Poly2):
|
||||
x = np.linspace(0, 1, 10)
|
||||
coef = random((3,))
|
||||
|
||||
d1 = Poly1.domain + random((2,))*.25
|
||||
w1 = Poly1.window + random((2,))*.25
|
||||
p1 = Poly1(coef, domain=d1, window=w1)
|
||||
|
||||
d2 = Poly2.domain + random((2,))*.25
|
||||
w2 = Poly2.window + random((2,))*.25
|
||||
p2 = Poly2.cast(p1, domain=d2, window=w2)
|
||||
|
||||
assert_almost_equal(p2.domain, d2)
|
||||
assert_almost_equal(p2.window, w2)
|
||||
assert_almost_equal(p2(x), p1(x))
|
||||
|
||||
|
||||
#
|
||||
# methods that depend on one class
|
||||
#
|
||||
|
||||
|
||||
def check_identity(Poly):
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
x = np.linspace(d[0], d[1], 11)
|
||||
p = Poly.identity(domain=d, window=w)
|
||||
assert_equal(p.domain, d)
|
||||
assert_equal(p.window, w)
|
||||
assert_almost_equal(p(x), x)
|
||||
|
||||
|
||||
def check_basis(Poly):
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
p = Poly.basis(5, domain=d, window=w)
|
||||
assert_equal(p.domain, d)
|
||||
assert_equal(p.window, w)
|
||||
assert_equal(p.coef, [0]*5 + [1])
|
||||
|
||||
|
||||
def check_fromroots(Poly):
|
||||
# check that requested roots are zeros of a polynomial
|
||||
# of correct degree, domain, and window.
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
r = random((5,))
|
||||
p1 = Poly.fromroots(r, domain=d, window=w)
|
||||
assert_equal(p1.degree(), len(r))
|
||||
assert_equal(p1.domain, d)
|
||||
assert_equal(p1.window, w)
|
||||
assert_almost_equal(p1(r), 0)
|
||||
|
||||
# check that polynomial is monic
|
||||
pdom = Polynomial.domain
|
||||
pwin = Polynomial.window
|
||||
p2 = Polynomial.cast(p1, domain=pdom, window=pwin)
|
||||
assert_almost_equal(p2.coef[-1], 1)
|
||||
|
||||
|
||||
def check_fit(Poly):
|
||||
|
||||
def f(x):
|
||||
return x*(x - 1)*(x - 2)
|
||||
x = np.linspace(0, 3)
|
||||
y = f(x)
|
||||
|
||||
# check default value of domain and window
|
||||
p = Poly.fit(x, y, 3)
|
||||
assert_almost_equal(p.domain, [0, 3])
|
||||
assert_almost_equal(p(x), y)
|
||||
assert_equal(p.degree(), 3)
|
||||
|
||||
# check with given domains and window
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
p = Poly.fit(x, y, 3, domain=d, window=w)
|
||||
assert_almost_equal(p(x), y)
|
||||
assert_almost_equal(p.domain, d)
|
||||
assert_almost_equal(p.window, w)
|
||||
p = Poly.fit(x, y, [0, 1, 2, 3], domain=d, window=w)
|
||||
assert_almost_equal(p(x), y)
|
||||
assert_almost_equal(p.domain, d)
|
||||
assert_almost_equal(p.window, w)
|
||||
|
||||
# check with class domain default
|
||||
p = Poly.fit(x, y, 3, [])
|
||||
assert_equal(p.domain, Poly.domain)
|
||||
assert_equal(p.window, Poly.window)
|
||||
p = Poly.fit(x, y, [0, 1, 2, 3], [])
|
||||
assert_equal(p.domain, Poly.domain)
|
||||
assert_equal(p.window, Poly.window)
|
||||
|
||||
# check that fit accepts weights.
|
||||
w = np.zeros_like(x)
|
||||
z = y + random(y.shape)*.25
|
||||
w[::2] = 1
|
||||
p1 = Poly.fit(x[::2], z[::2], 3)
|
||||
p2 = Poly.fit(x, z, 3, w=w)
|
||||
p3 = Poly.fit(x, z, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(p1(x), p2(x))
|
||||
assert_almost_equal(p2(x), p3(x))
|
||||
|
||||
|
||||
def check_equal(Poly):
|
||||
p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3])
|
||||
p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3])
|
||||
p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3])
|
||||
p4 = Poly([1, 2, 3], domain=[0, 1], window=[1, 2])
|
||||
assert_(p1 == p1)
|
||||
assert_(not p1 == p2)
|
||||
assert_(not p1 == p3)
|
||||
assert_(not p1 == p4)
|
||||
|
||||
|
||||
def check_not_equal(Poly):
|
||||
p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3])
|
||||
p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3])
|
||||
p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3])
|
||||
p4 = Poly([1, 2, 3], domain=[0, 1], window=[1, 2])
|
||||
assert_(not p1 != p1)
|
||||
assert_(p1 != p2)
|
||||
assert_(p1 != p3)
|
||||
assert_(p1 != p4)
|
||||
|
||||
|
||||
def check_add(Poly):
|
||||
# This checks commutation, not numerical correctness
|
||||
c1 = list(random((4,)) + .5)
|
||||
c2 = list(random((3,)) + .5)
|
||||
p1 = Poly(c1)
|
||||
p2 = Poly(c2)
|
||||
p3 = p1 + p2
|
||||
assert_poly_almost_equal(p2 + p1, p3)
|
||||
assert_poly_almost_equal(p1 + c2, p3)
|
||||
assert_poly_almost_equal(c2 + p1, p3)
|
||||
assert_poly_almost_equal(p1 + tuple(c2), p3)
|
||||
assert_poly_almost_equal(tuple(c2) + p1, p3)
|
||||
assert_poly_almost_equal(p1 + np.array(c2), p3)
|
||||
assert_poly_almost_equal(np.array(c2) + p1, p3)
|
||||
assert_raises(TypeError, op.add, p1, Poly([0], domain=Poly.domain + 1))
|
||||
assert_raises(TypeError, op.add, p1, Poly([0], window=Poly.window + 1))
|
||||
if Poly is Polynomial:
|
||||
assert_raises(TypeError, op.add, p1, Chebyshev([0]))
|
||||
else:
|
||||
assert_raises(TypeError, op.add, p1, Polynomial([0]))
|
||||
|
||||
|
||||
def check_sub(Poly):
|
||||
# This checks commutation, not numerical correctness
|
||||
c1 = list(random((4,)) + .5)
|
||||
c2 = list(random((3,)) + .5)
|
||||
p1 = Poly(c1)
|
||||
p2 = Poly(c2)
|
||||
p3 = p1 - p2
|
||||
assert_poly_almost_equal(p2 - p1, -p3)
|
||||
assert_poly_almost_equal(p1 - c2, p3)
|
||||
assert_poly_almost_equal(c2 - p1, -p3)
|
||||
assert_poly_almost_equal(p1 - tuple(c2), p3)
|
||||
assert_poly_almost_equal(tuple(c2) - p1, -p3)
|
||||
assert_poly_almost_equal(p1 - np.array(c2), p3)
|
||||
assert_poly_almost_equal(np.array(c2) - p1, -p3)
|
||||
assert_raises(TypeError, op.sub, p1, Poly([0], domain=Poly.domain + 1))
|
||||
assert_raises(TypeError, op.sub, p1, Poly([0], window=Poly.window + 1))
|
||||
if Poly is Polynomial:
|
||||
assert_raises(TypeError, op.sub, p1, Chebyshev([0]))
|
||||
else:
|
||||
assert_raises(TypeError, op.sub, p1, Polynomial([0]))
|
||||
|
||||
|
||||
def check_mul(Poly):
|
||||
c1 = list(random((4,)) + .5)
|
||||
c2 = list(random((3,)) + .5)
|
||||
p1 = Poly(c1)
|
||||
p2 = Poly(c2)
|
||||
p3 = p1 * p2
|
||||
assert_poly_almost_equal(p2 * p1, p3)
|
||||
assert_poly_almost_equal(p1 * c2, p3)
|
||||
assert_poly_almost_equal(c2 * p1, p3)
|
||||
assert_poly_almost_equal(p1 * tuple(c2), p3)
|
||||
assert_poly_almost_equal(tuple(c2) * p1, p3)
|
||||
assert_poly_almost_equal(p1 * np.array(c2), p3)
|
||||
assert_poly_almost_equal(np.array(c2) * p1, p3)
|
||||
assert_poly_almost_equal(p1 * 2, p1 * Poly([2]))
|
||||
assert_poly_almost_equal(2 * p1, p1 * Poly([2]))
|
||||
assert_raises(TypeError, op.mul, p1, Poly([0], domain=Poly.domain + 1))
|
||||
assert_raises(TypeError, op.mul, p1, Poly([0], window=Poly.window + 1))
|
||||
if Poly is Polynomial:
|
||||
assert_raises(TypeError, op.mul, p1, Chebyshev([0]))
|
||||
else:
|
||||
assert_raises(TypeError, op.mul, p1, Polynomial([0]))
|
||||
|
||||
|
||||
def check_floordiv(Poly):
|
||||
c1 = list(random((4,)) + .5)
|
||||
c2 = list(random((3,)) + .5)
|
||||
c3 = list(random((2,)) + .5)
|
||||
p1 = Poly(c1)
|
||||
p2 = Poly(c2)
|
||||
p3 = Poly(c3)
|
||||
p4 = p1 * p2 + p3
|
||||
c4 = list(p4.coef)
|
||||
assert_poly_almost_equal(p4 // p2, p1)
|
||||
assert_poly_almost_equal(p4 // c2, p1)
|
||||
assert_poly_almost_equal(c4 // p2, p1)
|
||||
assert_poly_almost_equal(p4 // tuple(c2), p1)
|
||||
assert_poly_almost_equal(tuple(c4) // p2, p1)
|
||||
assert_poly_almost_equal(p4 // np.array(c2), p1)
|
||||
assert_poly_almost_equal(np.array(c4) // p2, p1)
|
||||
assert_poly_almost_equal(2 // p2, Poly([0]))
|
||||
assert_poly_almost_equal(p2 // 2, 0.5*p2)
|
||||
assert_raises(
|
||||
TypeError, op.floordiv, p1, Poly([0], domain=Poly.domain + 1))
|
||||
assert_raises(
|
||||
TypeError, op.floordiv, p1, Poly([0], window=Poly.window + 1))
|
||||
if Poly is Polynomial:
|
||||
assert_raises(TypeError, op.floordiv, p1, Chebyshev([0]))
|
||||
else:
|
||||
assert_raises(TypeError, op.floordiv, p1, Polynomial([0]))
|
||||
|
||||
|
||||
def check_truediv(Poly):
|
||||
# true division is valid only if the denominator is a Number and
|
||||
# not a python bool.
|
||||
p1 = Poly([1,2,3])
|
||||
p2 = p1 * 5
|
||||
|
||||
for stype in np.ScalarType:
|
||||
if not issubclass(stype, Number) or issubclass(stype, bool):
|
||||
continue
|
||||
s = stype(5)
|
||||
assert_poly_almost_equal(op.truediv(p2, s), p1)
|
||||
assert_raises(TypeError, op.truediv, s, p2)
|
||||
for stype in (int, long, float):
|
||||
s = stype(5)
|
||||
assert_poly_almost_equal(op.truediv(p2, s), p1)
|
||||
assert_raises(TypeError, op.truediv, s, p2)
|
||||
for stype in [complex]:
|
||||
s = stype(5, 0)
|
||||
assert_poly_almost_equal(op.truediv(p2, s), p1)
|
||||
assert_raises(TypeError, op.truediv, s, p2)
|
||||
for s in [tuple(), list(), dict(), bool(), np.array([1])]:
|
||||
assert_raises(TypeError, op.truediv, p2, s)
|
||||
assert_raises(TypeError, op.truediv, s, p2)
|
||||
for ptype in classes:
|
||||
assert_raises(TypeError, op.truediv, p2, ptype(1))
|
||||
|
||||
|
||||
def check_mod(Poly):
|
||||
# This checks commutation, not numerical correctness
|
||||
c1 = list(random((4,)) + .5)
|
||||
c2 = list(random((3,)) + .5)
|
||||
c3 = list(random((2,)) + .5)
|
||||
p1 = Poly(c1)
|
||||
p2 = Poly(c2)
|
||||
p3 = Poly(c3)
|
||||
p4 = p1 * p2 + p3
|
||||
c4 = list(p4.coef)
|
||||
assert_poly_almost_equal(p4 % p2, p3)
|
||||
assert_poly_almost_equal(p4 % c2, p3)
|
||||
assert_poly_almost_equal(c4 % p2, p3)
|
||||
assert_poly_almost_equal(p4 % tuple(c2), p3)
|
||||
assert_poly_almost_equal(tuple(c4) % p2, p3)
|
||||
assert_poly_almost_equal(p4 % np.array(c2), p3)
|
||||
assert_poly_almost_equal(np.array(c4) % p2, p3)
|
||||
assert_poly_almost_equal(2 % p2, Poly([2]))
|
||||
assert_poly_almost_equal(p2 % 2, Poly([0]))
|
||||
assert_raises(TypeError, op.mod, p1, Poly([0], domain=Poly.domain + 1))
|
||||
assert_raises(TypeError, op.mod, p1, Poly([0], window=Poly.window + 1))
|
||||
if Poly is Polynomial:
|
||||
assert_raises(TypeError, op.mod, p1, Chebyshev([0]))
|
||||
else:
|
||||
assert_raises(TypeError, op.mod, p1, Polynomial([0]))
|
||||
|
||||
|
||||
def check_divmod(Poly):
|
||||
# This checks commutation, not numerical correctness
|
||||
c1 = list(random((4,)) + .5)
|
||||
c2 = list(random((3,)) + .5)
|
||||
c3 = list(random((2,)) + .5)
|
||||
p1 = Poly(c1)
|
||||
p2 = Poly(c2)
|
||||
p3 = Poly(c3)
|
||||
p4 = p1 * p2 + p3
|
||||
c4 = list(p4.coef)
|
||||
quo, rem = divmod(p4, p2)
|
||||
assert_poly_almost_equal(quo, p1)
|
||||
assert_poly_almost_equal(rem, p3)
|
||||
quo, rem = divmod(p4, c2)
|
||||
assert_poly_almost_equal(quo, p1)
|
||||
assert_poly_almost_equal(rem, p3)
|
||||
quo, rem = divmod(c4, p2)
|
||||
assert_poly_almost_equal(quo, p1)
|
||||
assert_poly_almost_equal(rem, p3)
|
||||
quo, rem = divmod(p4, tuple(c2))
|
||||
assert_poly_almost_equal(quo, p1)
|
||||
assert_poly_almost_equal(rem, p3)
|
||||
quo, rem = divmod(tuple(c4), p2)
|
||||
assert_poly_almost_equal(quo, p1)
|
||||
assert_poly_almost_equal(rem, p3)
|
||||
quo, rem = divmod(p4, np.array(c2))
|
||||
assert_poly_almost_equal(quo, p1)
|
||||
assert_poly_almost_equal(rem, p3)
|
||||
quo, rem = divmod(np.array(c4), p2)
|
||||
assert_poly_almost_equal(quo, p1)
|
||||
assert_poly_almost_equal(rem, p3)
|
||||
quo, rem = divmod(p2, 2)
|
||||
assert_poly_almost_equal(quo, 0.5*p2)
|
||||
assert_poly_almost_equal(rem, Poly([0]))
|
||||
quo, rem = divmod(2, p2)
|
||||
assert_poly_almost_equal(quo, Poly([0]))
|
||||
assert_poly_almost_equal(rem, Poly([2]))
|
||||
assert_raises(TypeError, divmod, p1, Poly([0], domain=Poly.domain + 1))
|
||||
assert_raises(TypeError, divmod, p1, Poly([0], window=Poly.window + 1))
|
||||
if Poly is Polynomial:
|
||||
assert_raises(TypeError, divmod, p1, Chebyshev([0]))
|
||||
else:
|
||||
assert_raises(TypeError, divmod, p1, Polynomial([0]))
|
||||
|
||||
|
||||
def check_roots(Poly):
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
tgt = np.sort(random((5,)))
|
||||
res = np.sort(Poly.fromroots(tgt, domain=d, window=w).roots())
|
||||
assert_almost_equal(res, tgt)
|
||||
# default domain and window
|
||||
res = np.sort(Poly.fromroots(tgt).roots())
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
def check_degree(Poly):
|
||||
p = Poly.basis(5)
|
||||
assert_equal(p.degree(), 5)
|
||||
|
||||
|
||||
def check_copy(Poly):
|
||||
p1 = Poly.basis(5)
|
||||
p2 = p1.copy()
|
||||
assert_(p1 == p2)
|
||||
assert_(p1 is not p2)
|
||||
assert_(p1.coef is not p2.coef)
|
||||
assert_(p1.domain is not p2.domain)
|
||||
assert_(p1.window is not p2.window)
|
||||
|
||||
|
||||
def check_integ(Poly):
|
||||
P = Polynomial
|
||||
# Check defaults
|
||||
p0 = Poly.cast(P([1*2, 2*3, 3*4]))
|
||||
p1 = P.cast(p0.integ())
|
||||
p2 = P.cast(p0.integ(2))
|
||||
assert_poly_almost_equal(p1, P([0, 2, 3, 4]))
|
||||
assert_poly_almost_equal(p2, P([0, 0, 1, 1, 1]))
|
||||
# Check with k
|
||||
p0 = Poly.cast(P([1*2, 2*3, 3*4]))
|
||||
p1 = P.cast(p0.integ(k=1))
|
||||
p2 = P.cast(p0.integ(2, k=[1, 1]))
|
||||
assert_poly_almost_equal(p1, P([1, 2, 3, 4]))
|
||||
assert_poly_almost_equal(p2, P([1, 1, 1, 1, 1]))
|
||||
# Check with lbnd
|
||||
p0 = Poly.cast(P([1*2, 2*3, 3*4]))
|
||||
p1 = P.cast(p0.integ(lbnd=1))
|
||||
p2 = P.cast(p0.integ(2, lbnd=1))
|
||||
assert_poly_almost_equal(p1, P([-9, 2, 3, 4]))
|
||||
assert_poly_almost_equal(p2, P([6, -9, 1, 1, 1]))
|
||||
# Check scaling
|
||||
d = 2*Poly.domain
|
||||
p0 = Poly.cast(P([1*2, 2*3, 3*4]), domain=d)
|
||||
p1 = P.cast(p0.integ())
|
||||
p2 = P.cast(p0.integ(2))
|
||||
assert_poly_almost_equal(p1, P([0, 2, 3, 4]))
|
||||
assert_poly_almost_equal(p2, P([0, 0, 1, 1, 1]))
|
||||
|
||||
|
||||
def check_deriv(Poly):
|
||||
# Check that the derivative is the inverse of integration. It is
|
||||
# assumes that the integration has been checked elsewhere.
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
p1 = Poly([1, 2, 3], domain=d, window=w)
|
||||
p2 = p1.integ(2, k=[1, 2])
|
||||
p3 = p1.integ(1, k=[1])
|
||||
assert_almost_equal(p2.deriv(1).coef, p3.coef)
|
||||
assert_almost_equal(p2.deriv(2).coef, p1.coef)
|
||||
# default domain and window
|
||||
p1 = Poly([1, 2, 3])
|
||||
p2 = p1.integ(2, k=[1, 2])
|
||||
p3 = p1.integ(1, k=[1])
|
||||
assert_almost_equal(p2.deriv(1).coef, p3.coef)
|
||||
assert_almost_equal(p2.deriv(2).coef, p1.coef)
|
||||
|
||||
|
||||
def check_linspace(Poly):
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
p = Poly([1, 2, 3], domain=d, window=w)
|
||||
# check default domain
|
||||
xtgt = np.linspace(d[0], d[1], 20)
|
||||
ytgt = p(xtgt)
|
||||
xres, yres = p.linspace(20)
|
||||
assert_almost_equal(xres, xtgt)
|
||||
assert_almost_equal(yres, ytgt)
|
||||
# check specified domain
|
||||
xtgt = np.linspace(0, 2, 20)
|
||||
ytgt = p(xtgt)
|
||||
xres, yres = p.linspace(20, domain=[0, 2])
|
||||
assert_almost_equal(xres, xtgt)
|
||||
assert_almost_equal(yres, ytgt)
|
||||
|
||||
|
||||
def check_pow(Poly):
|
||||
d = Poly.domain + random((2,))*.25
|
||||
w = Poly.window + random((2,))*.25
|
||||
tgt = Poly([1], domain=d, window=w)
|
||||
tst = Poly([1, 2, 3], domain=d, window=w)
|
||||
for i in range(5):
|
||||
assert_poly_almost_equal(tst**i, tgt)
|
||||
tgt = tgt * tst
|
||||
# default domain and window
|
||||
tgt = Poly([1])
|
||||
tst = Poly([1, 2, 3])
|
||||
for i in range(5):
|
||||
assert_poly_almost_equal(tst**i, tgt)
|
||||
tgt = tgt * tst
|
||||
# check error for invalid powers
|
||||
assert_raises(ValueError, op.pow, tgt, 1.5)
|
||||
assert_raises(ValueError, op.pow, tgt, -1)
|
||||
|
||||
|
||||
def check_call(Poly):
|
||||
P = Polynomial
|
||||
d = Poly.domain
|
||||
x = np.linspace(d[0], d[1], 11)
|
||||
|
||||
# Check defaults
|
||||
p = Poly.cast(P([1, 2, 3]))
|
||||
tgt = 1 + x*(2 + 3*x)
|
||||
res = p(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
def check_cutdeg(Poly):
|
||||
p = Poly([1, 2, 3])
|
||||
assert_raises(ValueError, p.cutdeg, .5)
|
||||
assert_raises(ValueError, p.cutdeg, -1)
|
||||
assert_equal(len(p.cutdeg(3)), 3)
|
||||
assert_equal(len(p.cutdeg(2)), 3)
|
||||
assert_equal(len(p.cutdeg(1)), 2)
|
||||
assert_equal(len(p.cutdeg(0)), 1)
|
||||
|
||||
|
||||
def check_truncate(Poly):
|
||||
p = Poly([1, 2, 3])
|
||||
assert_raises(ValueError, p.truncate, .5)
|
||||
assert_raises(ValueError, p.truncate, 0)
|
||||
assert_equal(len(p.truncate(4)), 3)
|
||||
assert_equal(len(p.truncate(3)), 3)
|
||||
assert_equal(len(p.truncate(2)), 2)
|
||||
assert_equal(len(p.truncate(1)), 1)
|
||||
|
||||
|
||||
def check_trim(Poly):
|
||||
c = [1, 1e-6, 1e-12, 0]
|
||||
p = Poly(c)
|
||||
assert_equal(p.trim().coef, c[:3])
|
||||
assert_equal(p.trim(1e-10).coef, c[:2])
|
||||
assert_equal(p.trim(1e-5).coef, c[:1])
|
||||
|
||||
|
||||
def check_mapparms(Poly):
|
||||
# check with defaults. Should be identity.
|
||||
d = Poly.domain
|
||||
w = Poly.window
|
||||
p = Poly([1], domain=d, window=w)
|
||||
assert_almost_equal([0, 1], p.mapparms())
|
||||
#
|
||||
w = 2*d + 1
|
||||
p = Poly([1], domain=d, window=w)
|
||||
assert_almost_equal([1, 2], p.mapparms())
|
||||
|
||||
|
||||
def check_ufunc_override(Poly):
|
||||
p = Poly([1, 2, 3])
|
||||
x = np.ones(3)
|
||||
assert_raises(TypeError, np.add, p, x)
|
||||
assert_raises(TypeError, np.add, x, p)
|
||||
|
||||
|
||||
class TestInterpolate(object):
|
||||
|
||||
def f(self, x):
|
||||
return x * (x - 1) * (x - 2)
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, Chebyshev.interpolate, self.f, -1)
|
||||
assert_raises(TypeError, Chebyshev.interpolate, self.f, 10.)
|
||||
|
||||
def test_dimensions(self):
|
||||
for deg in range(1, 5):
|
||||
assert_(Chebyshev.interpolate(self.f, deg).degree() == deg)
|
||||
|
||||
def test_approximation(self):
|
||||
|
||||
def powx(x, p):
|
||||
return x**p
|
||||
|
||||
x = np.linspace(0, 2, 10)
|
||||
for deg in range(0, 10):
|
||||
for t in range(0, deg + 1):
|
||||
p = Chebyshev.interpolate(powx, deg, domain=[0, 2], args=(t,))
|
||||
assert_almost_equal(p(x), powx(x, t), decimal=12)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,551 @@
|
||||
"""Tests for hermite module.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
import numpy.polynomial.hermite as herm
|
||||
from numpy.polynomial.polynomial import polyval
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite
|
||||
)
|
||||
|
||||
H0 = np.array([1])
|
||||
H1 = np.array([0, 2])
|
||||
H2 = np.array([-2, 0, 4])
|
||||
H3 = np.array([0, -12, 0, 8])
|
||||
H4 = np.array([12, 0, -48, 0, 16])
|
||||
H5 = np.array([0, 120, 0, -160, 0, 32])
|
||||
H6 = np.array([-120, 0, 720, 0, -480, 0, 64])
|
||||
H7 = np.array([0, -1680, 0, 3360, 0, -1344, 0, 128])
|
||||
H8 = np.array([1680, 0, -13440, 0, 13440, 0, -3584, 0, 256])
|
||||
H9 = np.array([0, 30240, 0, -80640, 0, 48384, 0, -9216, 0, 512])
|
||||
|
||||
Hlist = [H0, H1, H2, H3, H4, H5, H6, H7, H8, H9]
|
||||
|
||||
|
||||
def trim(x):
|
||||
return herm.hermtrim(x, tol=1e-6)
|
||||
|
||||
|
||||
class TestConstants(object):
|
||||
|
||||
def test_hermdomain(self):
|
||||
assert_equal(herm.hermdomain, [-1, 1])
|
||||
|
||||
def test_hermzero(self):
|
||||
assert_equal(herm.hermzero, [0])
|
||||
|
||||
def test_hermone(self):
|
||||
assert_equal(herm.hermone, [1])
|
||||
|
||||
def test_hermx(self):
|
||||
assert_equal(herm.hermx, [0, .5])
|
||||
|
||||
|
||||
class TestArithmetic(object):
|
||||
x = np.linspace(-3, 3, 100)
|
||||
|
||||
def test_hermadd(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] += 1
|
||||
res = herm.hermadd([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_hermsub(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] -= 1
|
||||
res = herm.hermsub([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_hermmulx(self):
|
||||
assert_equal(herm.hermmulx([0]), [0])
|
||||
assert_equal(herm.hermmulx([1]), [0, .5])
|
||||
for i in range(1, 5):
|
||||
ser = [0]*i + [1]
|
||||
tgt = [0]*(i - 1) + [i, 0, .5]
|
||||
assert_equal(herm.hermmulx(ser), tgt)
|
||||
|
||||
def test_hermmul(self):
|
||||
# check values of result
|
||||
for i in range(5):
|
||||
pol1 = [0]*i + [1]
|
||||
val1 = herm.hermval(self.x, pol1)
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
pol2 = [0]*j + [1]
|
||||
val2 = herm.hermval(self.x, pol2)
|
||||
pol3 = herm.hermmul(pol1, pol2)
|
||||
val3 = herm.hermval(self.x, pol3)
|
||||
assert_(len(pol3) == i + j + 1, msg)
|
||||
assert_almost_equal(val3, val1*val2, err_msg=msg)
|
||||
|
||||
def test_hermdiv(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
ci = [0]*i + [1]
|
||||
cj = [0]*j + [1]
|
||||
tgt = herm.hermadd(ci, cj)
|
||||
quo, rem = herm.hermdiv(tgt, ci)
|
||||
res = herm.hermadd(herm.hermmul(quo, ci), rem)
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
|
||||
class TestEvaluation(object):
|
||||
# coefficients of 1 + 2*x + 3*x**2
|
||||
c1d = np.array([2.5, 1., .75])
|
||||
c2d = np.einsum('i,j->ij', c1d, c1d)
|
||||
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
||||
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
y = polyval(x, [1., 2., 3.])
|
||||
|
||||
def test_hermval(self):
|
||||
#check empty input
|
||||
assert_equal(herm.hermval([], [1]).size, 0)
|
||||
|
||||
#check normal input)
|
||||
x = np.linspace(-1, 1)
|
||||
y = [polyval(x, c) for c in Hlist]
|
||||
for i in range(10):
|
||||
msg = "At i=%d" % i
|
||||
tgt = y[i]
|
||||
res = herm.hermval(x, [0]*i + [1])
|
||||
assert_almost_equal(res, tgt, err_msg=msg)
|
||||
|
||||
#check that shape is preserved
|
||||
for i in range(3):
|
||||
dims = [2]*i
|
||||
x = np.zeros(dims)
|
||||
assert_equal(herm.hermval(x, [1]).shape, dims)
|
||||
assert_equal(herm.hermval(x, [1, 0]).shape, dims)
|
||||
assert_equal(herm.hermval(x, [1, 0, 0]).shape, dims)
|
||||
|
||||
def test_hermval2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, herm.hermval2d, x1, x2[:2], self.c2d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2
|
||||
res = herm.hermval2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herm.hermval2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_hermval3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, herm.hermval3d, x1, x2, x3[:2], self.c3d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2*y3
|
||||
res = herm.hermval3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herm.hermval3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_hermgrid2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j->ij', y1, y2)
|
||||
res = herm.hermgrid2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herm.hermgrid2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3)*2)
|
||||
|
||||
def test_hermgrid3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
||||
res = herm.hermgrid3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herm.hermgrid3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3)*3)
|
||||
|
||||
|
||||
class TestIntegral(object):
|
||||
|
||||
def test_hermint(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, herm.hermint, [0], .5)
|
||||
assert_raises(ValueError, herm.hermint, [0], -1)
|
||||
assert_raises(ValueError, herm.hermint, [0], 1, [0, 0])
|
||||
assert_raises(ValueError, herm.hermint, [0], lbnd=[0])
|
||||
assert_raises(ValueError, herm.hermint, [0], scl=[0])
|
||||
assert_raises(ValueError, herm.hermint, [0], axis=.5)
|
||||
|
||||
# test integration of zero polynomial
|
||||
for i in range(2, 5):
|
||||
k = [0]*(i - 2) + [1]
|
||||
res = herm.hermint([0], m=i, k=k)
|
||||
assert_almost_equal(res, [0, .5])
|
||||
|
||||
# check single integration with integration constant
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [1/scl]
|
||||
hermpol = herm.poly2herm(pol)
|
||||
hermint = herm.hermint(hermpol, m=1, k=[i])
|
||||
res = herm.herm2poly(hermint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check single integration with integration constant and lbnd
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
hermpol = herm.poly2herm(pol)
|
||||
hermint = herm.hermint(hermpol, m=1, k=[i], lbnd=-1)
|
||||
assert_almost_equal(herm.hermval(-1, hermint), i)
|
||||
|
||||
# check single integration with integration constant and scaling
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [2/scl]
|
||||
hermpol = herm.poly2herm(pol)
|
||||
hermint = herm.hermint(hermpol, m=1, k=[i], scl=2)
|
||||
res = herm.herm2poly(hermint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with default k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herm.hermint(tgt, m=1)
|
||||
res = herm.hermint(pol, m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with defined k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herm.hermint(tgt, m=1, k=[k])
|
||||
res = herm.hermint(pol, m=j, k=list(range(j)))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with lbnd
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herm.hermint(tgt, m=1, k=[k], lbnd=-1)
|
||||
res = herm.hermint(pol, m=j, k=list(range(j)), lbnd=-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herm.hermint(tgt, m=1, k=[k], scl=2)
|
||||
res = herm.hermint(pol, m=j, k=list(range(j)), scl=2)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_hermint_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([herm.hermint(c) for c in c2d.T]).T
|
||||
res = herm.hermint(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([herm.hermint(c) for c in c2d])
|
||||
res = herm.hermint(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([herm.hermint(c, k=3) for c in c2d])
|
||||
res = herm.hermint(c2d, k=3, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestDerivative(object):
|
||||
|
||||
def test_hermder(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, herm.hermder, [0], .5)
|
||||
assert_raises(ValueError, herm.hermder, [0], -1)
|
||||
|
||||
# check that zeroth derivative does nothing
|
||||
for i in range(5):
|
||||
tgt = [0]*i + [1]
|
||||
res = herm.hermder(tgt, m=0)
|
||||
assert_equal(trim(res), trim(tgt))
|
||||
|
||||
# check that derivation is the inverse of integration
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = herm.hermder(herm.hermint(tgt, m=j), m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check derivation with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_hermder_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([herm.hermder(c) for c in c2d.T]).T
|
||||
res = herm.hermder(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([herm.hermder(c) for c in c2d])
|
||||
res = herm.hermder(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestVander(object):
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
|
||||
def test_hermvander(self):
|
||||
# check for 1d x
|
||||
x = np.arange(3)
|
||||
v = herm.hermvander(x, 3)
|
||||
assert_(v.shape == (3, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], herm.hermval(x, coef))
|
||||
|
||||
# check for 2d x
|
||||
x = np.array([[1, 2], [3, 4], [5, 6]])
|
||||
v = herm.hermvander(x, 3)
|
||||
assert_(v.shape == (3, 2, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], herm.hermval(x, coef))
|
||||
|
||||
def test_hermvander2d(self):
|
||||
# also tests hermval2d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3))
|
||||
van = herm.hermvander2d(x1, x2, [1, 2])
|
||||
tgt = herm.hermval2d(x1, x2, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = herm.hermvander2d([x1], [x2], [1, 2])
|
||||
assert_(van.shape == (1, 5, 6))
|
||||
|
||||
def test_hermvander3d(self):
|
||||
# also tests hermval3d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3, 4))
|
||||
van = herm.hermvander3d(x1, x2, x3, [1, 2, 3])
|
||||
tgt = herm.hermval3d(x1, x2, x3, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = herm.hermvander3d([x1], [x2], [x3], [1, 2, 3])
|
||||
assert_(van.shape == (1, 5, 24))
|
||||
|
||||
|
||||
class TestFitting(object):
|
||||
|
||||
def test_hermfit(self):
|
||||
def f(x):
|
||||
return x*(x - 1)*(x - 2)
|
||||
|
||||
def f2(x):
|
||||
return x**4 + x**2 + 1
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, herm.hermfit, [1], [1], -1)
|
||||
assert_raises(TypeError, herm.hermfit, [[1]], [1], 0)
|
||||
assert_raises(TypeError, herm.hermfit, [], [1], 0)
|
||||
assert_raises(TypeError, herm.hermfit, [1], [[[1]]], 0)
|
||||
assert_raises(TypeError, herm.hermfit, [1, 2], [1], 0)
|
||||
assert_raises(TypeError, herm.hermfit, [1], [1, 2], 0)
|
||||
assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[[1]])
|
||||
assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[1, 1])
|
||||
assert_raises(ValueError, herm.hermfit, [1], [1], [-1,])
|
||||
assert_raises(ValueError, herm.hermfit, [1], [1], [2, -1, 6])
|
||||
assert_raises(TypeError, herm.hermfit, [1], [1], [])
|
||||
|
||||
# Test fit
|
||||
x = np.linspace(0, 2)
|
||||
y = f(x)
|
||||
#
|
||||
coef3 = herm.hermfit(x, y, 3)
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(herm.hermval(x, coef3), y)
|
||||
coef3 = herm.hermfit(x, y, [0, 1, 2, 3])
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(herm.hermval(x, coef3), y)
|
||||
#
|
||||
coef4 = herm.hermfit(x, y, 4)
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(herm.hermval(x, coef4), y)
|
||||
coef4 = herm.hermfit(x, y, [0, 1, 2, 3, 4])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(herm.hermval(x, coef4), y)
|
||||
# check things still work if deg is not in strict increasing
|
||||
coef4 = herm.hermfit(x, y, [2, 3, 4, 1, 0])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(herm.hermval(x, coef4), y)
|
||||
#
|
||||
coef2d = herm.hermfit(x, np.array([y, y]).T, 3)
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
coef2d = herm.hermfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
# test weighting
|
||||
w = np.zeros_like(x)
|
||||
yw = y.copy()
|
||||
w[1::2] = 1
|
||||
y[0::2] = 0
|
||||
wcoef3 = herm.hermfit(x, yw, 3, w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
wcoef3 = herm.hermfit(x, yw, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
#
|
||||
wcoef2d = herm.hermfit(x, np.array([yw, yw]).T, 3, w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
wcoef2d = herm.hermfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
# test scaling with complex values x points whose square
|
||||
# is zero when summed.
|
||||
x = [1, 1j, -1, -1j]
|
||||
assert_almost_equal(herm.hermfit(x, x, 1), [0, .5])
|
||||
assert_almost_equal(herm.hermfit(x, x, [0, 1]), [0, .5])
|
||||
# test fitting only even Legendre polynomials
|
||||
x = np.linspace(-1, 1)
|
||||
y = f2(x)
|
||||
coef1 = herm.hermfit(x, y, 4)
|
||||
assert_almost_equal(herm.hermval(x, coef1), y)
|
||||
coef2 = herm.hermfit(x, y, [0, 2, 4])
|
||||
assert_almost_equal(herm.hermval(x, coef2), y)
|
||||
assert_almost_equal(coef1, coef2)
|
||||
|
||||
|
||||
class TestCompanion(object):
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, herm.hermcompanion, [])
|
||||
assert_raises(ValueError, herm.hermcompanion, [1])
|
||||
|
||||
def test_dimensions(self):
|
||||
for i in range(1, 5):
|
||||
coef = [0]*i + [1]
|
||||
assert_(herm.hermcompanion(coef).shape == (i, i))
|
||||
|
||||
def test_linear_root(self):
|
||||
assert_(herm.hermcompanion([1, 2])[0, 0] == -.25)
|
||||
|
||||
|
||||
class TestGauss(object):
|
||||
|
||||
def test_100(self):
|
||||
x, w = herm.hermgauss(100)
|
||||
|
||||
# test orthogonality. Note that the results need to be normalized,
|
||||
# otherwise the huge values that can arise from fast growing
|
||||
# functions like Laguerre can be very confusing.
|
||||
v = herm.hermvander(x, 99)
|
||||
vv = np.dot(v.T * w, v)
|
||||
vd = 1/np.sqrt(vv.diagonal())
|
||||
vv = vd[:, None] * vv * vd
|
||||
assert_almost_equal(vv, np.eye(100))
|
||||
|
||||
# check that the integral of 1 is correct
|
||||
tgt = np.sqrt(np.pi)
|
||||
assert_almost_equal(w.sum(), tgt)
|
||||
|
||||
|
||||
class TestMisc(object):
|
||||
|
||||
def test_hermfromroots(self):
|
||||
res = herm.hermfromroots([])
|
||||
assert_almost_equal(trim(res), [1])
|
||||
for i in range(1, 5):
|
||||
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
|
||||
pol = herm.hermfromroots(roots)
|
||||
res = herm.hermval(roots, pol)
|
||||
tgt = 0
|
||||
assert_(len(pol) == i + 1)
|
||||
assert_almost_equal(herm.herm2poly(pol)[-1], 1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
def test_hermroots(self):
|
||||
assert_almost_equal(herm.hermroots([1]), [])
|
||||
assert_almost_equal(herm.hermroots([1, 1]), [-.5])
|
||||
for i in range(2, 5):
|
||||
tgt = np.linspace(-1, 1, i)
|
||||
res = herm.hermroots(herm.hermfromroots(tgt))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_hermtrim(self):
|
||||
coef = [2, -1, 1, 0]
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, herm.hermtrim, coef, -1)
|
||||
|
||||
# Test results
|
||||
assert_equal(herm.hermtrim(coef), coef[:-1])
|
||||
assert_equal(herm.hermtrim(coef, 1), coef[:-3])
|
||||
assert_equal(herm.hermtrim(coef, 2), [0])
|
||||
|
||||
def test_hermline(self):
|
||||
assert_equal(herm.hermline(3, 4), [3, 2])
|
||||
|
||||
def test_herm2poly(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(herm.herm2poly([0]*i + [1]), Hlist[i])
|
||||
|
||||
def test_poly2herm(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(herm.poly2herm(Hlist[i]), [0]*i + [1])
|
||||
|
||||
def test_weight(self):
|
||||
x = np.linspace(-5, 5, 11)
|
||||
tgt = np.exp(-x**2)
|
||||
res = herm.hermweight(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,552 @@
|
||||
"""Tests for hermite_e module.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
import numpy.polynomial.hermite_e as herme
|
||||
from numpy.polynomial.polynomial import polyval
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite
|
||||
)
|
||||
|
||||
He0 = np.array([1])
|
||||
He1 = np.array([0, 1])
|
||||
He2 = np.array([-1, 0, 1])
|
||||
He3 = np.array([0, -3, 0, 1])
|
||||
He4 = np.array([3, 0, -6, 0, 1])
|
||||
He5 = np.array([0, 15, 0, -10, 0, 1])
|
||||
He6 = np.array([-15, 0, 45, 0, -15, 0, 1])
|
||||
He7 = np.array([0, -105, 0, 105, 0, -21, 0, 1])
|
||||
He8 = np.array([105, 0, -420, 0, 210, 0, -28, 0, 1])
|
||||
He9 = np.array([0, 945, 0, -1260, 0, 378, 0, -36, 0, 1])
|
||||
|
||||
Helist = [He0, He1, He2, He3, He4, He5, He6, He7, He8, He9]
|
||||
|
||||
|
||||
def trim(x):
|
||||
return herme.hermetrim(x, tol=1e-6)
|
||||
|
||||
|
||||
class TestConstants(object):
|
||||
|
||||
def test_hermedomain(self):
|
||||
assert_equal(herme.hermedomain, [-1, 1])
|
||||
|
||||
def test_hermezero(self):
|
||||
assert_equal(herme.hermezero, [0])
|
||||
|
||||
def test_hermeone(self):
|
||||
assert_equal(herme.hermeone, [1])
|
||||
|
||||
def test_hermex(self):
|
||||
assert_equal(herme.hermex, [0, 1])
|
||||
|
||||
|
||||
class TestArithmetic(object):
|
||||
x = np.linspace(-3, 3, 100)
|
||||
|
||||
def test_hermeadd(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] += 1
|
||||
res = herme.hermeadd([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_hermesub(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] -= 1
|
||||
res = herme.hermesub([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_hermemulx(self):
|
||||
assert_equal(herme.hermemulx([0]), [0])
|
||||
assert_equal(herme.hermemulx([1]), [0, 1])
|
||||
for i in range(1, 5):
|
||||
ser = [0]*i + [1]
|
||||
tgt = [0]*(i - 1) + [i, 0, 1]
|
||||
assert_equal(herme.hermemulx(ser), tgt)
|
||||
|
||||
def test_hermemul(self):
|
||||
# check values of result
|
||||
for i in range(5):
|
||||
pol1 = [0]*i + [1]
|
||||
val1 = herme.hermeval(self.x, pol1)
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
pol2 = [0]*j + [1]
|
||||
val2 = herme.hermeval(self.x, pol2)
|
||||
pol3 = herme.hermemul(pol1, pol2)
|
||||
val3 = herme.hermeval(self.x, pol3)
|
||||
assert_(len(pol3) == i + j + 1, msg)
|
||||
assert_almost_equal(val3, val1*val2, err_msg=msg)
|
||||
|
||||
def test_hermediv(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
ci = [0]*i + [1]
|
||||
cj = [0]*j + [1]
|
||||
tgt = herme.hermeadd(ci, cj)
|
||||
quo, rem = herme.hermediv(tgt, ci)
|
||||
res = herme.hermeadd(herme.hermemul(quo, ci), rem)
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
|
||||
class TestEvaluation(object):
|
||||
# coefficients of 1 + 2*x + 3*x**2
|
||||
c1d = np.array([4., 2., 3.])
|
||||
c2d = np.einsum('i,j->ij', c1d, c1d)
|
||||
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
||||
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
y = polyval(x, [1., 2., 3.])
|
||||
|
||||
def test_hermeval(self):
|
||||
#check empty input
|
||||
assert_equal(herme.hermeval([], [1]).size, 0)
|
||||
|
||||
#check normal input)
|
||||
x = np.linspace(-1, 1)
|
||||
y = [polyval(x, c) for c in Helist]
|
||||
for i in range(10):
|
||||
msg = "At i=%d" % i
|
||||
tgt = y[i]
|
||||
res = herme.hermeval(x, [0]*i + [1])
|
||||
assert_almost_equal(res, tgt, err_msg=msg)
|
||||
|
||||
#check that shape is preserved
|
||||
for i in range(3):
|
||||
dims = [2]*i
|
||||
x = np.zeros(dims)
|
||||
assert_equal(herme.hermeval(x, [1]).shape, dims)
|
||||
assert_equal(herme.hermeval(x, [1, 0]).shape, dims)
|
||||
assert_equal(herme.hermeval(x, [1, 0, 0]).shape, dims)
|
||||
|
||||
def test_hermeval2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, herme.hermeval2d, x1, x2[:2], self.c2d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2
|
||||
res = herme.hermeval2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herme.hermeval2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_hermeval3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, herme.hermeval3d, x1, x2, x3[:2], self.c3d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2*y3
|
||||
res = herme.hermeval3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herme.hermeval3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_hermegrid2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j->ij', y1, y2)
|
||||
res = herme.hermegrid2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herme.hermegrid2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3)*2)
|
||||
|
||||
def test_hermegrid3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
||||
res = herme.hermegrid3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = herme.hermegrid3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3)*3)
|
||||
|
||||
|
||||
class TestIntegral(object):
|
||||
|
||||
def test_hermeint(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, herme.hermeint, [0], .5)
|
||||
assert_raises(ValueError, herme.hermeint, [0], -1)
|
||||
assert_raises(ValueError, herme.hermeint, [0], 1, [0, 0])
|
||||
assert_raises(ValueError, herme.hermeint, [0], lbnd=[0])
|
||||
assert_raises(ValueError, herme.hermeint, [0], scl=[0])
|
||||
assert_raises(ValueError, herme.hermeint, [0], axis=.5)
|
||||
|
||||
# test integration of zero polynomial
|
||||
for i in range(2, 5):
|
||||
k = [0]*(i - 2) + [1]
|
||||
res = herme.hermeint([0], m=i, k=k)
|
||||
assert_almost_equal(res, [0, 1])
|
||||
|
||||
# check single integration with integration constant
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [1/scl]
|
||||
hermepol = herme.poly2herme(pol)
|
||||
hermeint = herme.hermeint(hermepol, m=1, k=[i])
|
||||
res = herme.herme2poly(hermeint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check single integration with integration constant and lbnd
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
hermepol = herme.poly2herme(pol)
|
||||
hermeint = herme.hermeint(hermepol, m=1, k=[i], lbnd=-1)
|
||||
assert_almost_equal(herme.hermeval(-1, hermeint), i)
|
||||
|
||||
# check single integration with integration constant and scaling
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [2/scl]
|
||||
hermepol = herme.poly2herme(pol)
|
||||
hermeint = herme.hermeint(hermepol, m=1, k=[i], scl=2)
|
||||
res = herme.herme2poly(hermeint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with default k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herme.hermeint(tgt, m=1)
|
||||
res = herme.hermeint(pol, m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with defined k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herme.hermeint(tgt, m=1, k=[k])
|
||||
res = herme.hermeint(pol, m=j, k=list(range(j)))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with lbnd
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herme.hermeint(tgt, m=1, k=[k], lbnd=-1)
|
||||
res = herme.hermeint(pol, m=j, k=list(range(j)), lbnd=-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = herme.hermeint(tgt, m=1, k=[k], scl=2)
|
||||
res = herme.hermeint(pol, m=j, k=list(range(j)), scl=2)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_hermeint_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([herme.hermeint(c) for c in c2d.T]).T
|
||||
res = herme.hermeint(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([herme.hermeint(c) for c in c2d])
|
||||
res = herme.hermeint(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([herme.hermeint(c, k=3) for c in c2d])
|
||||
res = herme.hermeint(c2d, k=3, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestDerivative(object):
|
||||
|
||||
def test_hermeder(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, herme.hermeder, [0], .5)
|
||||
assert_raises(ValueError, herme.hermeder, [0], -1)
|
||||
|
||||
# check that zeroth derivative does nothing
|
||||
for i in range(5):
|
||||
tgt = [0]*i + [1]
|
||||
res = herme.hermeder(tgt, m=0)
|
||||
assert_equal(trim(res), trim(tgt))
|
||||
|
||||
# check that derivation is the inverse of integration
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = herme.hermeder(herme.hermeint(tgt, m=j), m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check derivation with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = herme.hermeder(
|
||||
herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_hermeder_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([herme.hermeder(c) for c in c2d.T]).T
|
||||
res = herme.hermeder(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([herme.hermeder(c) for c in c2d])
|
||||
res = herme.hermeder(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestVander(object):
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
|
||||
def test_hermevander(self):
|
||||
# check for 1d x
|
||||
x = np.arange(3)
|
||||
v = herme.hermevander(x, 3)
|
||||
assert_(v.shape == (3, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], herme.hermeval(x, coef))
|
||||
|
||||
# check for 2d x
|
||||
x = np.array([[1, 2], [3, 4], [5, 6]])
|
||||
v = herme.hermevander(x, 3)
|
||||
assert_(v.shape == (3, 2, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], herme.hermeval(x, coef))
|
||||
|
||||
def test_hermevander2d(self):
|
||||
# also tests hermeval2d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3))
|
||||
van = herme.hermevander2d(x1, x2, [1, 2])
|
||||
tgt = herme.hermeval2d(x1, x2, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = herme.hermevander2d([x1], [x2], [1, 2])
|
||||
assert_(van.shape == (1, 5, 6))
|
||||
|
||||
def test_hermevander3d(self):
|
||||
# also tests hermeval3d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3, 4))
|
||||
van = herme.hermevander3d(x1, x2, x3, [1, 2, 3])
|
||||
tgt = herme.hermeval3d(x1, x2, x3, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = herme.hermevander3d([x1], [x2], [x3], [1, 2, 3])
|
||||
assert_(van.shape == (1, 5, 24))
|
||||
|
||||
|
||||
class TestFitting(object):
|
||||
|
||||
def test_hermefit(self):
|
||||
def f(x):
|
||||
return x*(x - 1)*(x - 2)
|
||||
|
||||
def f2(x):
|
||||
return x**4 + x**2 + 1
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, herme.hermefit, [1], [1], -1)
|
||||
assert_raises(TypeError, herme.hermefit, [[1]], [1], 0)
|
||||
assert_raises(TypeError, herme.hermefit, [], [1], 0)
|
||||
assert_raises(TypeError, herme.hermefit, [1], [[[1]]], 0)
|
||||
assert_raises(TypeError, herme.hermefit, [1, 2], [1], 0)
|
||||
assert_raises(TypeError, herme.hermefit, [1], [1, 2], 0)
|
||||
assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[[1]])
|
||||
assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[1, 1])
|
||||
assert_raises(ValueError, herme.hermefit, [1], [1], [-1,])
|
||||
assert_raises(ValueError, herme.hermefit, [1], [1], [2, -1, 6])
|
||||
assert_raises(TypeError, herme.hermefit, [1], [1], [])
|
||||
|
||||
# Test fit
|
||||
x = np.linspace(0, 2)
|
||||
y = f(x)
|
||||
#
|
||||
coef3 = herme.hermefit(x, y, 3)
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(herme.hermeval(x, coef3), y)
|
||||
coef3 = herme.hermefit(x, y, [0, 1, 2, 3])
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(herme.hermeval(x, coef3), y)
|
||||
#
|
||||
coef4 = herme.hermefit(x, y, 4)
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(herme.hermeval(x, coef4), y)
|
||||
coef4 = herme.hermefit(x, y, [0, 1, 2, 3, 4])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(herme.hermeval(x, coef4), y)
|
||||
# check things still work if deg is not in strict increasing
|
||||
coef4 = herme.hermefit(x, y, [2, 3, 4, 1, 0])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(herme.hermeval(x, coef4), y)
|
||||
#
|
||||
coef2d = herme.hermefit(x, np.array([y, y]).T, 3)
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
coef2d = herme.hermefit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
# test weighting
|
||||
w = np.zeros_like(x)
|
||||
yw = y.copy()
|
||||
w[1::2] = 1
|
||||
y[0::2] = 0
|
||||
wcoef3 = herme.hermefit(x, yw, 3, w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
wcoef3 = herme.hermefit(x, yw, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
#
|
||||
wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, 3, w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
# test scaling with complex values x points whose square
|
||||
# is zero when summed.
|
||||
x = [1, 1j, -1, -1j]
|
||||
assert_almost_equal(herme.hermefit(x, x, 1), [0, 1])
|
||||
assert_almost_equal(herme.hermefit(x, x, [0, 1]), [0, 1])
|
||||
# test fitting only even Legendre polynomials
|
||||
x = np.linspace(-1, 1)
|
||||
y = f2(x)
|
||||
coef1 = herme.hermefit(x, y, 4)
|
||||
assert_almost_equal(herme.hermeval(x, coef1), y)
|
||||
coef2 = herme.hermefit(x, y, [0, 2, 4])
|
||||
assert_almost_equal(herme.hermeval(x, coef2), y)
|
||||
assert_almost_equal(coef1, coef2)
|
||||
|
||||
|
||||
class TestCompanion(object):
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, herme.hermecompanion, [])
|
||||
assert_raises(ValueError, herme.hermecompanion, [1])
|
||||
|
||||
def test_dimensions(self):
|
||||
for i in range(1, 5):
|
||||
coef = [0]*i + [1]
|
||||
assert_(herme.hermecompanion(coef).shape == (i, i))
|
||||
|
||||
def test_linear_root(self):
|
||||
assert_(herme.hermecompanion([1, 2])[0, 0] == -.5)
|
||||
|
||||
|
||||
class TestGauss(object):
|
||||
|
||||
def test_100(self):
|
||||
x, w = herme.hermegauss(100)
|
||||
|
||||
# test orthogonality. Note that the results need to be normalized,
|
||||
# otherwise the huge values that can arise from fast growing
|
||||
# functions like Laguerre can be very confusing.
|
||||
v = herme.hermevander(x, 99)
|
||||
vv = np.dot(v.T * w, v)
|
||||
vd = 1/np.sqrt(vv.diagonal())
|
||||
vv = vd[:, None] * vv * vd
|
||||
assert_almost_equal(vv, np.eye(100))
|
||||
|
||||
# check that the integral of 1 is correct
|
||||
tgt = np.sqrt(2*np.pi)
|
||||
assert_almost_equal(w.sum(), tgt)
|
||||
|
||||
|
||||
class TestMisc(object):
|
||||
|
||||
def test_hermefromroots(self):
|
||||
res = herme.hermefromroots([])
|
||||
assert_almost_equal(trim(res), [1])
|
||||
for i in range(1, 5):
|
||||
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
|
||||
pol = herme.hermefromroots(roots)
|
||||
res = herme.hermeval(roots, pol)
|
||||
tgt = 0
|
||||
assert_(len(pol) == i + 1)
|
||||
assert_almost_equal(herme.herme2poly(pol)[-1], 1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
def test_hermeroots(self):
|
||||
assert_almost_equal(herme.hermeroots([1]), [])
|
||||
assert_almost_equal(herme.hermeroots([1, 1]), [-1])
|
||||
for i in range(2, 5):
|
||||
tgt = np.linspace(-1, 1, i)
|
||||
res = herme.hermeroots(herme.hermefromroots(tgt))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_hermetrim(self):
|
||||
coef = [2, -1, 1, 0]
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, herme.hermetrim, coef, -1)
|
||||
|
||||
# Test results
|
||||
assert_equal(herme.hermetrim(coef), coef[:-1])
|
||||
assert_equal(herme.hermetrim(coef, 1), coef[:-3])
|
||||
assert_equal(herme.hermetrim(coef, 2), [0])
|
||||
|
||||
def test_hermeline(self):
|
||||
assert_equal(herme.hermeline(3, 4), [3, 4])
|
||||
|
||||
def test_herme2poly(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(herme.herme2poly([0]*i + [1]), Helist[i])
|
||||
|
||||
def test_poly2herme(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(herme.poly2herme(Helist[i]), [0]*i + [1])
|
||||
|
||||
def test_weight(self):
|
||||
x = np.linspace(-5, 5, 11)
|
||||
tgt = np.exp(-.5*x**2)
|
||||
res = herme.hermeweight(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,533 @@
|
||||
"""Tests for laguerre module.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
import numpy.polynomial.laguerre as lag
|
||||
from numpy.polynomial.polynomial import polyval
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite
|
||||
)
|
||||
|
||||
L0 = np.array([1])/1
|
||||
L1 = np.array([1, -1])/1
|
||||
L2 = np.array([2, -4, 1])/2
|
||||
L3 = np.array([6, -18, 9, -1])/6
|
||||
L4 = np.array([24, -96, 72, -16, 1])/24
|
||||
L5 = np.array([120, -600, 600, -200, 25, -1])/120
|
||||
L6 = np.array([720, -4320, 5400, -2400, 450, -36, 1])/720
|
||||
|
||||
Llist = [L0, L1, L2, L3, L4, L5, L6]
|
||||
|
||||
|
||||
def trim(x):
|
||||
return lag.lagtrim(x, tol=1e-6)
|
||||
|
||||
|
||||
class TestConstants(object):
|
||||
|
||||
def test_lagdomain(self):
|
||||
assert_equal(lag.lagdomain, [0, 1])
|
||||
|
||||
def test_lagzero(self):
|
||||
assert_equal(lag.lagzero, [0])
|
||||
|
||||
def test_lagone(self):
|
||||
assert_equal(lag.lagone, [1])
|
||||
|
||||
def test_lagx(self):
|
||||
assert_equal(lag.lagx, [1, -1])
|
||||
|
||||
|
||||
class TestArithmetic(object):
|
||||
x = np.linspace(-3, 3, 100)
|
||||
|
||||
def test_lagadd(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] += 1
|
||||
res = lag.lagadd([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_lagsub(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] -= 1
|
||||
res = lag.lagsub([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_lagmulx(self):
|
||||
assert_equal(lag.lagmulx([0]), [0])
|
||||
assert_equal(lag.lagmulx([1]), [1, -1])
|
||||
for i in range(1, 5):
|
||||
ser = [0]*i + [1]
|
||||
tgt = [0]*(i - 1) + [-i, 2*i + 1, -(i + 1)]
|
||||
assert_almost_equal(lag.lagmulx(ser), tgt)
|
||||
|
||||
def test_lagmul(self):
|
||||
# check values of result
|
||||
for i in range(5):
|
||||
pol1 = [0]*i + [1]
|
||||
val1 = lag.lagval(self.x, pol1)
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
pol2 = [0]*j + [1]
|
||||
val2 = lag.lagval(self.x, pol2)
|
||||
pol3 = lag.lagmul(pol1, pol2)
|
||||
val3 = lag.lagval(self.x, pol3)
|
||||
assert_(len(pol3) == i + j + 1, msg)
|
||||
assert_almost_equal(val3, val1*val2, err_msg=msg)
|
||||
|
||||
def test_lagdiv(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
ci = [0]*i + [1]
|
||||
cj = [0]*j + [1]
|
||||
tgt = lag.lagadd(ci, cj)
|
||||
quo, rem = lag.lagdiv(tgt, ci)
|
||||
res = lag.lagadd(lag.lagmul(quo, ci), rem)
|
||||
assert_almost_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
|
||||
class TestEvaluation(object):
|
||||
# coefficients of 1 + 2*x + 3*x**2
|
||||
c1d = np.array([9., -14., 6.])
|
||||
c2d = np.einsum('i,j->ij', c1d, c1d)
|
||||
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
||||
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
y = polyval(x, [1., 2., 3.])
|
||||
|
||||
def test_lagval(self):
|
||||
#check empty input
|
||||
assert_equal(lag.lagval([], [1]).size, 0)
|
||||
|
||||
#check normal input)
|
||||
x = np.linspace(-1, 1)
|
||||
y = [polyval(x, c) for c in Llist]
|
||||
for i in range(7):
|
||||
msg = "At i=%d" % i
|
||||
tgt = y[i]
|
||||
res = lag.lagval(x, [0]*i + [1])
|
||||
assert_almost_equal(res, tgt, err_msg=msg)
|
||||
|
||||
#check that shape is preserved
|
||||
for i in range(3):
|
||||
dims = [2]*i
|
||||
x = np.zeros(dims)
|
||||
assert_equal(lag.lagval(x, [1]).shape, dims)
|
||||
assert_equal(lag.lagval(x, [1, 0]).shape, dims)
|
||||
assert_equal(lag.lagval(x, [1, 0, 0]).shape, dims)
|
||||
|
||||
def test_lagval2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, lag.lagval2d, x1, x2[:2], self.c2d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2
|
||||
res = lag.lagval2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = lag.lagval2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_lagval3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, lag.lagval3d, x1, x2, x3[:2], self.c3d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2*y3
|
||||
res = lag.lagval3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = lag.lagval3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_laggrid2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j->ij', y1, y2)
|
||||
res = lag.laggrid2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = lag.laggrid2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3)*2)
|
||||
|
||||
def test_laggrid3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
||||
res = lag.laggrid3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = lag.laggrid3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3)*3)
|
||||
|
||||
|
||||
class TestIntegral(object):
|
||||
|
||||
def test_lagint(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, lag.lagint, [0], .5)
|
||||
assert_raises(ValueError, lag.lagint, [0], -1)
|
||||
assert_raises(ValueError, lag.lagint, [0], 1, [0, 0])
|
||||
assert_raises(ValueError, lag.lagint, [0], lbnd=[0])
|
||||
assert_raises(ValueError, lag.lagint, [0], scl=[0])
|
||||
assert_raises(ValueError, lag.lagint, [0], axis=.5)
|
||||
|
||||
# test integration of zero polynomial
|
||||
for i in range(2, 5):
|
||||
k = [0]*(i - 2) + [1]
|
||||
res = lag.lagint([0], m=i, k=k)
|
||||
assert_almost_equal(res, [1, -1])
|
||||
|
||||
# check single integration with integration constant
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [1/scl]
|
||||
lagpol = lag.poly2lag(pol)
|
||||
lagint = lag.lagint(lagpol, m=1, k=[i])
|
||||
res = lag.lag2poly(lagint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check single integration with integration constant and lbnd
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
lagpol = lag.poly2lag(pol)
|
||||
lagint = lag.lagint(lagpol, m=1, k=[i], lbnd=-1)
|
||||
assert_almost_equal(lag.lagval(-1, lagint), i)
|
||||
|
||||
# check single integration with integration constant and scaling
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [2/scl]
|
||||
lagpol = lag.poly2lag(pol)
|
||||
lagint = lag.lagint(lagpol, m=1, k=[i], scl=2)
|
||||
res = lag.lag2poly(lagint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with default k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = lag.lagint(tgt, m=1)
|
||||
res = lag.lagint(pol, m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with defined k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = lag.lagint(tgt, m=1, k=[k])
|
||||
res = lag.lagint(pol, m=j, k=list(range(j)))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with lbnd
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = lag.lagint(tgt, m=1, k=[k], lbnd=-1)
|
||||
res = lag.lagint(pol, m=j, k=list(range(j)), lbnd=-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = lag.lagint(tgt, m=1, k=[k], scl=2)
|
||||
res = lag.lagint(pol, m=j, k=list(range(j)), scl=2)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_lagint_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([lag.lagint(c) for c in c2d.T]).T
|
||||
res = lag.lagint(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([lag.lagint(c) for c in c2d])
|
||||
res = lag.lagint(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([lag.lagint(c, k=3) for c in c2d])
|
||||
res = lag.lagint(c2d, k=3, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestDerivative(object):
|
||||
|
||||
def test_lagder(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, lag.lagder, [0], .5)
|
||||
assert_raises(ValueError, lag.lagder, [0], -1)
|
||||
|
||||
# check that zeroth derivative does nothing
|
||||
for i in range(5):
|
||||
tgt = [0]*i + [1]
|
||||
res = lag.lagder(tgt, m=0)
|
||||
assert_equal(trim(res), trim(tgt))
|
||||
|
||||
# check that derivation is the inverse of integration
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = lag.lagder(lag.lagint(tgt, m=j), m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check derivation with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = lag.lagder(lag.lagint(tgt, m=j, scl=2), m=j, scl=.5)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_lagder_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([lag.lagder(c) for c in c2d.T]).T
|
||||
res = lag.lagder(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([lag.lagder(c) for c in c2d])
|
||||
res = lag.lagder(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestVander(object):
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
|
||||
def test_lagvander(self):
|
||||
# check for 1d x
|
||||
x = np.arange(3)
|
||||
v = lag.lagvander(x, 3)
|
||||
assert_(v.shape == (3, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], lag.lagval(x, coef))
|
||||
|
||||
# check for 2d x
|
||||
x = np.array([[1, 2], [3, 4], [5, 6]])
|
||||
v = lag.lagvander(x, 3)
|
||||
assert_(v.shape == (3, 2, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], lag.lagval(x, coef))
|
||||
|
||||
def test_lagvander2d(self):
|
||||
# also tests lagval2d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3))
|
||||
van = lag.lagvander2d(x1, x2, [1, 2])
|
||||
tgt = lag.lagval2d(x1, x2, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = lag.lagvander2d([x1], [x2], [1, 2])
|
||||
assert_(van.shape == (1, 5, 6))
|
||||
|
||||
def test_lagvander3d(self):
|
||||
# also tests lagval3d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3, 4))
|
||||
van = lag.lagvander3d(x1, x2, x3, [1, 2, 3])
|
||||
tgt = lag.lagval3d(x1, x2, x3, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = lag.lagvander3d([x1], [x2], [x3], [1, 2, 3])
|
||||
assert_(van.shape == (1, 5, 24))
|
||||
|
||||
|
||||
class TestFitting(object):
|
||||
|
||||
def test_lagfit(self):
|
||||
def f(x):
|
||||
return x*(x - 1)*(x - 2)
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, lag.lagfit, [1], [1], -1)
|
||||
assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
|
||||
assert_raises(TypeError, lag.lagfit, [], [1], 0)
|
||||
assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
|
||||
assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
|
||||
assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
|
||||
assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
|
||||
assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
|
||||
assert_raises(ValueError, lag.lagfit, [1], [1], [-1,])
|
||||
assert_raises(ValueError, lag.lagfit, [1], [1], [2, -1, 6])
|
||||
assert_raises(TypeError, lag.lagfit, [1], [1], [])
|
||||
|
||||
# Test fit
|
||||
x = np.linspace(0, 2)
|
||||
y = f(x)
|
||||
#
|
||||
coef3 = lag.lagfit(x, y, 3)
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(lag.lagval(x, coef3), y)
|
||||
coef3 = lag.lagfit(x, y, [0, 1, 2, 3])
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(lag.lagval(x, coef3), y)
|
||||
#
|
||||
coef4 = lag.lagfit(x, y, 4)
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(lag.lagval(x, coef4), y)
|
||||
coef4 = lag.lagfit(x, y, [0, 1, 2, 3, 4])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(lag.lagval(x, coef4), y)
|
||||
#
|
||||
coef2d = lag.lagfit(x, np.array([y, y]).T, 3)
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
coef2d = lag.lagfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
# test weighting
|
||||
w = np.zeros_like(x)
|
||||
yw = y.copy()
|
||||
w[1::2] = 1
|
||||
y[0::2] = 0
|
||||
wcoef3 = lag.lagfit(x, yw, 3, w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
wcoef3 = lag.lagfit(x, yw, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
#
|
||||
wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, 3, w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
# test scaling with complex values x points whose square
|
||||
# is zero when summed.
|
||||
x = [1, 1j, -1, -1j]
|
||||
assert_almost_equal(lag.lagfit(x, x, 1), [1, -1])
|
||||
assert_almost_equal(lag.lagfit(x, x, [0, 1]), [1, -1])
|
||||
|
||||
|
||||
class TestCompanion(object):
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, lag.lagcompanion, [])
|
||||
assert_raises(ValueError, lag.lagcompanion, [1])
|
||||
|
||||
def test_dimensions(self):
|
||||
for i in range(1, 5):
|
||||
coef = [0]*i + [1]
|
||||
assert_(lag.lagcompanion(coef).shape == (i, i))
|
||||
|
||||
def test_linear_root(self):
|
||||
assert_(lag.lagcompanion([1, 2])[0, 0] == 1.5)
|
||||
|
||||
|
||||
class TestGauss(object):
|
||||
|
||||
def test_100(self):
|
||||
x, w = lag.laggauss(100)
|
||||
|
||||
# test orthogonality. Note that the results need to be normalized,
|
||||
# otherwise the huge values that can arise from fast growing
|
||||
# functions like Laguerre can be very confusing.
|
||||
v = lag.lagvander(x, 99)
|
||||
vv = np.dot(v.T * w, v)
|
||||
vd = 1/np.sqrt(vv.diagonal())
|
||||
vv = vd[:, None] * vv * vd
|
||||
assert_almost_equal(vv, np.eye(100))
|
||||
|
||||
# check that the integral of 1 is correct
|
||||
tgt = 1.0
|
||||
assert_almost_equal(w.sum(), tgt)
|
||||
|
||||
|
||||
class TestMisc(object):
|
||||
|
||||
def test_lagfromroots(self):
|
||||
res = lag.lagfromroots([])
|
||||
assert_almost_equal(trim(res), [1])
|
||||
for i in range(1, 5):
|
||||
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
|
||||
pol = lag.lagfromroots(roots)
|
||||
res = lag.lagval(roots, pol)
|
||||
tgt = 0
|
||||
assert_(len(pol) == i + 1)
|
||||
assert_almost_equal(lag.lag2poly(pol)[-1], 1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
def test_lagroots(self):
|
||||
assert_almost_equal(lag.lagroots([1]), [])
|
||||
assert_almost_equal(lag.lagroots([0, 1]), [1])
|
||||
for i in range(2, 5):
|
||||
tgt = np.linspace(0, 3, i)
|
||||
res = lag.lagroots(lag.lagfromroots(tgt))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_lagtrim(self):
|
||||
coef = [2, -1, 1, 0]
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, lag.lagtrim, coef, -1)
|
||||
|
||||
# Test results
|
||||
assert_equal(lag.lagtrim(coef), coef[:-1])
|
||||
assert_equal(lag.lagtrim(coef, 1), coef[:-3])
|
||||
assert_equal(lag.lagtrim(coef, 2), [0])
|
||||
|
||||
def test_lagline(self):
|
||||
assert_equal(lag.lagline(3, 4), [7, -4])
|
||||
|
||||
def test_lag2poly(self):
|
||||
for i in range(7):
|
||||
assert_almost_equal(lag.lag2poly([0]*i + [1]), Llist[i])
|
||||
|
||||
def test_poly2lag(self):
|
||||
for i in range(7):
|
||||
assert_almost_equal(lag.poly2lag(Llist[i]), [0]*i + [1])
|
||||
|
||||
def test_weight(self):
|
||||
x = np.linspace(0, 10, 11)
|
||||
tgt = np.exp(-x)
|
||||
res = lag.lagweight(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,552 @@
|
||||
"""Tests for legendre module.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
import numpy.polynomial.legendre as leg
|
||||
from numpy.polynomial.polynomial import polyval
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite
|
||||
)
|
||||
|
||||
L0 = np.array([1])
|
||||
L1 = np.array([0, 1])
|
||||
L2 = np.array([-1, 0, 3])/2
|
||||
L3 = np.array([0, -3, 0, 5])/2
|
||||
L4 = np.array([3, 0, -30, 0, 35])/8
|
||||
L5 = np.array([0, 15, 0, -70, 0, 63])/8
|
||||
L6 = np.array([-5, 0, 105, 0, -315, 0, 231])/16
|
||||
L7 = np.array([0, -35, 0, 315, 0, -693, 0, 429])/16
|
||||
L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435])/128
|
||||
L9 = np.array([0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155])/128
|
||||
|
||||
Llist = [L0, L1, L2, L3, L4, L5, L6, L7, L8, L9]
|
||||
|
||||
|
||||
def trim(x):
|
||||
return leg.legtrim(x, tol=1e-6)
|
||||
|
||||
|
||||
class TestConstants(object):
|
||||
|
||||
def test_legdomain(self):
|
||||
assert_equal(leg.legdomain, [-1, 1])
|
||||
|
||||
def test_legzero(self):
|
||||
assert_equal(leg.legzero, [0])
|
||||
|
||||
def test_legone(self):
|
||||
assert_equal(leg.legone, [1])
|
||||
|
||||
def test_legx(self):
|
||||
assert_equal(leg.legx, [0, 1])
|
||||
|
||||
|
||||
class TestArithmetic(object):
|
||||
x = np.linspace(-1, 1, 100)
|
||||
|
||||
def test_legadd(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] += 1
|
||||
res = leg.legadd([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_legsub(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] -= 1
|
||||
res = leg.legsub([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_legmulx(self):
|
||||
assert_equal(leg.legmulx([0]), [0])
|
||||
assert_equal(leg.legmulx([1]), [0, 1])
|
||||
for i in range(1, 5):
|
||||
tmp = 2*i + 1
|
||||
ser = [0]*i + [1]
|
||||
tgt = [0]*(i - 1) + [i/tmp, 0, (i + 1)/tmp]
|
||||
assert_equal(leg.legmulx(ser), tgt)
|
||||
|
||||
def test_legmul(self):
|
||||
# check values of result
|
||||
for i in range(5):
|
||||
pol1 = [0]*i + [1]
|
||||
val1 = leg.legval(self.x, pol1)
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
pol2 = [0]*j + [1]
|
||||
val2 = leg.legval(self.x, pol2)
|
||||
pol3 = leg.legmul(pol1, pol2)
|
||||
val3 = leg.legval(self.x, pol3)
|
||||
assert_(len(pol3) == i + j + 1, msg)
|
||||
assert_almost_equal(val3, val1*val2, err_msg=msg)
|
||||
|
||||
def test_legdiv(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
ci = [0]*i + [1]
|
||||
cj = [0]*j + [1]
|
||||
tgt = leg.legadd(ci, cj)
|
||||
quo, rem = leg.legdiv(tgt, ci)
|
||||
res = leg.legadd(leg.legmul(quo, ci), rem)
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
|
||||
class TestEvaluation(object):
|
||||
# coefficients of 1 + 2*x + 3*x**2
|
||||
c1d = np.array([2., 2., 2.])
|
||||
c2d = np.einsum('i,j->ij', c1d, c1d)
|
||||
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
||||
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
y = polyval(x, [1., 2., 3.])
|
||||
|
||||
def test_legval(self):
|
||||
#check empty input
|
||||
assert_equal(leg.legval([], [1]).size, 0)
|
||||
|
||||
#check normal input)
|
||||
x = np.linspace(-1, 1)
|
||||
y = [polyval(x, c) for c in Llist]
|
||||
for i in range(10):
|
||||
msg = "At i=%d" % i
|
||||
tgt = y[i]
|
||||
res = leg.legval(x, [0]*i + [1])
|
||||
assert_almost_equal(res, tgt, err_msg=msg)
|
||||
|
||||
#check that shape is preserved
|
||||
for i in range(3):
|
||||
dims = [2]*i
|
||||
x = np.zeros(dims)
|
||||
assert_equal(leg.legval(x, [1]).shape, dims)
|
||||
assert_equal(leg.legval(x, [1, 0]).shape, dims)
|
||||
assert_equal(leg.legval(x, [1, 0, 0]).shape, dims)
|
||||
|
||||
def test_legval2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, leg.legval2d, x1, x2[:2], self.c2d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2
|
||||
res = leg.legval2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = leg.legval2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_legval3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, leg.legval3d, x1, x2, x3[:2], self.c3d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2*y3
|
||||
res = leg.legval3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = leg.legval3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_leggrid2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j->ij', y1, y2)
|
||||
res = leg.leggrid2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = leg.leggrid2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3)*2)
|
||||
|
||||
def test_leggrid3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
||||
res = leg.leggrid3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = leg.leggrid3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3)*3)
|
||||
|
||||
|
||||
class TestIntegral(object):
|
||||
|
||||
def test_legint(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, leg.legint, [0], .5)
|
||||
assert_raises(ValueError, leg.legint, [0], -1)
|
||||
assert_raises(ValueError, leg.legint, [0], 1, [0, 0])
|
||||
assert_raises(ValueError, leg.legint, [0], lbnd=[0])
|
||||
assert_raises(ValueError, leg.legint, [0], scl=[0])
|
||||
assert_raises(ValueError, leg.legint, [0], axis=.5)
|
||||
|
||||
# test integration of zero polynomial
|
||||
for i in range(2, 5):
|
||||
k = [0]*(i - 2) + [1]
|
||||
res = leg.legint([0], m=i, k=k)
|
||||
assert_almost_equal(res, [0, 1])
|
||||
|
||||
# check single integration with integration constant
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [1/scl]
|
||||
legpol = leg.poly2leg(pol)
|
||||
legint = leg.legint(legpol, m=1, k=[i])
|
||||
res = leg.leg2poly(legint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check single integration with integration constant and lbnd
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
legpol = leg.poly2leg(pol)
|
||||
legint = leg.legint(legpol, m=1, k=[i], lbnd=-1)
|
||||
assert_almost_equal(leg.legval(-1, legint), i)
|
||||
|
||||
# check single integration with integration constant and scaling
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [2/scl]
|
||||
legpol = leg.poly2leg(pol)
|
||||
legint = leg.legint(legpol, m=1, k=[i], scl=2)
|
||||
res = leg.leg2poly(legint)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with default k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = leg.legint(tgt, m=1)
|
||||
res = leg.legint(pol, m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with defined k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = leg.legint(tgt, m=1, k=[k])
|
||||
res = leg.legint(pol, m=j, k=list(range(j)))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with lbnd
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1)
|
||||
res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = leg.legint(tgt, m=1, k=[k], scl=2)
|
||||
res = leg.legint(pol, m=j, k=list(range(j)), scl=2)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_legint_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([leg.legint(c) for c in c2d.T]).T
|
||||
res = leg.legint(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([leg.legint(c) for c in c2d])
|
||||
res = leg.legint(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([leg.legint(c, k=3) for c in c2d])
|
||||
res = leg.legint(c2d, k=3, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestDerivative(object):
|
||||
|
||||
def test_legder(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, leg.legder, [0], .5)
|
||||
assert_raises(ValueError, leg.legder, [0], -1)
|
||||
|
||||
# check that zeroth derivative does nothing
|
||||
for i in range(5):
|
||||
tgt = [0]*i + [1]
|
||||
res = leg.legder(tgt, m=0)
|
||||
assert_equal(trim(res), trim(tgt))
|
||||
|
||||
# check that derivation is the inverse of integration
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = leg.legder(leg.legint(tgt, m=j), m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check derivation with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_legder_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([leg.legder(c) for c in c2d.T]).T
|
||||
res = leg.legder(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([leg.legder(c) for c in c2d])
|
||||
res = leg.legder(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestVander(object):
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
|
||||
def test_legvander(self):
|
||||
# check for 1d x
|
||||
x = np.arange(3)
|
||||
v = leg.legvander(x, 3)
|
||||
assert_(v.shape == (3, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], leg.legval(x, coef))
|
||||
|
||||
# check for 2d x
|
||||
x = np.array([[1, 2], [3, 4], [5, 6]])
|
||||
v = leg.legvander(x, 3)
|
||||
assert_(v.shape == (3, 2, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], leg.legval(x, coef))
|
||||
|
||||
def test_legvander2d(self):
|
||||
# also tests polyval2d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3))
|
||||
van = leg.legvander2d(x1, x2, [1, 2])
|
||||
tgt = leg.legval2d(x1, x2, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = leg.legvander2d([x1], [x2], [1, 2])
|
||||
assert_(van.shape == (1, 5, 6))
|
||||
|
||||
def test_legvander3d(self):
|
||||
# also tests polyval3d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3, 4))
|
||||
van = leg.legvander3d(x1, x2, x3, [1, 2, 3])
|
||||
tgt = leg.legval3d(x1, x2, x3, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = leg.legvander3d([x1], [x2], [x3], [1, 2, 3])
|
||||
assert_(van.shape == (1, 5, 24))
|
||||
|
||||
|
||||
class TestFitting(object):
|
||||
|
||||
def test_legfit(self):
|
||||
def f(x):
|
||||
return x*(x - 1)*(x - 2)
|
||||
|
||||
def f2(x):
|
||||
return x**4 + x**2 + 1
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, leg.legfit, [1], [1], -1)
|
||||
assert_raises(TypeError, leg.legfit, [[1]], [1], 0)
|
||||
assert_raises(TypeError, leg.legfit, [], [1], 0)
|
||||
assert_raises(TypeError, leg.legfit, [1], [[[1]]], 0)
|
||||
assert_raises(TypeError, leg.legfit, [1, 2], [1], 0)
|
||||
assert_raises(TypeError, leg.legfit, [1], [1, 2], 0)
|
||||
assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[[1]])
|
||||
assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[1, 1])
|
||||
assert_raises(ValueError, leg.legfit, [1], [1], [-1,])
|
||||
assert_raises(ValueError, leg.legfit, [1], [1], [2, -1, 6])
|
||||
assert_raises(TypeError, leg.legfit, [1], [1], [])
|
||||
|
||||
# Test fit
|
||||
x = np.linspace(0, 2)
|
||||
y = f(x)
|
||||
#
|
||||
coef3 = leg.legfit(x, y, 3)
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(leg.legval(x, coef3), y)
|
||||
coef3 = leg.legfit(x, y, [0, 1, 2, 3])
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(leg.legval(x, coef3), y)
|
||||
#
|
||||
coef4 = leg.legfit(x, y, 4)
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(leg.legval(x, coef4), y)
|
||||
coef4 = leg.legfit(x, y, [0, 1, 2, 3, 4])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(leg.legval(x, coef4), y)
|
||||
# check things still work if deg is not in strict increasing
|
||||
coef4 = leg.legfit(x, y, [2, 3, 4, 1, 0])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(leg.legval(x, coef4), y)
|
||||
#
|
||||
coef2d = leg.legfit(x, np.array([y, y]).T, 3)
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
coef2d = leg.legfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
# test weighting
|
||||
w = np.zeros_like(x)
|
||||
yw = y.copy()
|
||||
w[1::2] = 1
|
||||
y[0::2] = 0
|
||||
wcoef3 = leg.legfit(x, yw, 3, w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
wcoef3 = leg.legfit(x, yw, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
#
|
||||
wcoef2d = leg.legfit(x, np.array([yw, yw]).T, 3, w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
wcoef2d = leg.legfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
# test scaling with complex values x points whose square
|
||||
# is zero when summed.
|
||||
x = [1, 1j, -1, -1j]
|
||||
assert_almost_equal(leg.legfit(x, x, 1), [0, 1])
|
||||
assert_almost_equal(leg.legfit(x, x, [0, 1]), [0, 1])
|
||||
# test fitting only even Legendre polynomials
|
||||
x = np.linspace(-1, 1)
|
||||
y = f2(x)
|
||||
coef1 = leg.legfit(x, y, 4)
|
||||
assert_almost_equal(leg.legval(x, coef1), y)
|
||||
coef2 = leg.legfit(x, y, [0, 2, 4])
|
||||
assert_almost_equal(leg.legval(x, coef2), y)
|
||||
assert_almost_equal(coef1, coef2)
|
||||
|
||||
|
||||
class TestCompanion(object):
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, leg.legcompanion, [])
|
||||
assert_raises(ValueError, leg.legcompanion, [1])
|
||||
|
||||
def test_dimensions(self):
|
||||
for i in range(1, 5):
|
||||
coef = [0]*i + [1]
|
||||
assert_(leg.legcompanion(coef).shape == (i, i))
|
||||
|
||||
def test_linear_root(self):
|
||||
assert_(leg.legcompanion([1, 2])[0, 0] == -.5)
|
||||
|
||||
|
||||
class TestGauss(object):
|
||||
|
||||
def test_100(self):
|
||||
x, w = leg.leggauss(100)
|
||||
|
||||
# test orthogonality. Note that the results need to be normalized,
|
||||
# otherwise the huge values that can arise from fast growing
|
||||
# functions like Laguerre can be very confusing.
|
||||
v = leg.legvander(x, 99)
|
||||
vv = np.dot(v.T * w, v)
|
||||
vd = 1/np.sqrt(vv.diagonal())
|
||||
vv = vd[:, None] * vv * vd
|
||||
assert_almost_equal(vv, np.eye(100))
|
||||
|
||||
# check that the integral of 1 is correct
|
||||
tgt = 2.0
|
||||
assert_almost_equal(w.sum(), tgt)
|
||||
|
||||
|
||||
class TestMisc(object):
|
||||
|
||||
def test_legfromroots(self):
|
||||
res = leg.legfromroots([])
|
||||
assert_almost_equal(trim(res), [1])
|
||||
for i in range(1, 5):
|
||||
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
|
||||
pol = leg.legfromroots(roots)
|
||||
res = leg.legval(roots, pol)
|
||||
tgt = 0
|
||||
assert_(len(pol) == i + 1)
|
||||
assert_almost_equal(leg.leg2poly(pol)[-1], 1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
def test_legroots(self):
|
||||
assert_almost_equal(leg.legroots([1]), [])
|
||||
assert_almost_equal(leg.legroots([1, 2]), [-.5])
|
||||
for i in range(2, 5):
|
||||
tgt = np.linspace(-1, 1, i)
|
||||
res = leg.legroots(leg.legfromroots(tgt))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_legtrim(self):
|
||||
coef = [2, -1, 1, 0]
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, leg.legtrim, coef, -1)
|
||||
|
||||
# Test results
|
||||
assert_equal(leg.legtrim(coef), coef[:-1])
|
||||
assert_equal(leg.legtrim(coef, 1), coef[:-3])
|
||||
assert_equal(leg.legtrim(coef, 2), [0])
|
||||
|
||||
def test_legline(self):
|
||||
assert_equal(leg.legline(3, 4), [3, 4])
|
||||
|
||||
def test_leg2poly(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i])
|
||||
|
||||
def test_poly2leg(self):
|
||||
for i in range(10):
|
||||
assert_almost_equal(leg.poly2leg(Llist[i]), [0]*i + [1])
|
||||
|
||||
def test_weight(self):
|
||||
x = np.linspace(-1, 1, 11)
|
||||
tgt = 1.
|
||||
res = leg.legweight(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,572 @@
|
||||
"""Tests for polynomial module.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
import numpy.polynomial.polynomial as poly
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite
|
||||
)
|
||||
|
||||
|
||||
def trim(x):
|
||||
return poly.polytrim(x, tol=1e-6)
|
||||
|
||||
T0 = [1]
|
||||
T1 = [0, 1]
|
||||
T2 = [-1, 0, 2]
|
||||
T3 = [0, -3, 0, 4]
|
||||
T4 = [1, 0, -8, 0, 8]
|
||||
T5 = [0, 5, 0, -20, 0, 16]
|
||||
T6 = [-1, 0, 18, 0, -48, 0, 32]
|
||||
T7 = [0, -7, 0, 56, 0, -112, 0, 64]
|
||||
T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128]
|
||||
T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
|
||||
|
||||
Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
|
||||
|
||||
|
||||
class TestConstants(object):
|
||||
|
||||
def test_polydomain(self):
|
||||
assert_equal(poly.polydomain, [-1, 1])
|
||||
|
||||
def test_polyzero(self):
|
||||
assert_equal(poly.polyzero, [0])
|
||||
|
||||
def test_polyone(self):
|
||||
assert_equal(poly.polyone, [1])
|
||||
|
||||
def test_polyx(self):
|
||||
assert_equal(poly.polyx, [0, 1])
|
||||
|
||||
|
||||
class TestArithmetic(object):
|
||||
|
||||
def test_polyadd(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] += 1
|
||||
res = poly.polyadd([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_polysub(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(max(i, j) + 1)
|
||||
tgt[i] += 1
|
||||
tgt[j] -= 1
|
||||
res = poly.polysub([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_polymulx(self):
|
||||
assert_equal(poly.polymulx([0]), [0])
|
||||
assert_equal(poly.polymulx([1]), [0, 1])
|
||||
for i in range(1, 5):
|
||||
ser = [0]*i + [1]
|
||||
tgt = [0]*(i + 1) + [1]
|
||||
assert_equal(poly.polymulx(ser), tgt)
|
||||
|
||||
def test_polymul(self):
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
tgt = np.zeros(i + j + 1)
|
||||
tgt[i + j] += 1
|
||||
res = poly.polymul([0]*i + [1], [0]*j + [1])
|
||||
assert_equal(trim(res), trim(tgt), err_msg=msg)
|
||||
|
||||
def test_polydiv(self):
|
||||
# check zero division
|
||||
assert_raises(ZeroDivisionError, poly.polydiv, [1], [0])
|
||||
|
||||
# check scalar division
|
||||
quo, rem = poly.polydiv([2], [2])
|
||||
assert_equal((quo, rem), (1, 0))
|
||||
quo, rem = poly.polydiv([2, 2], [2])
|
||||
assert_equal((quo, rem), ((1, 1), 0))
|
||||
|
||||
# check rest.
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
msg = "At i=%d, j=%d" % (i, j)
|
||||
ci = [0]*i + [1, 2]
|
||||
cj = [0]*j + [1, 2]
|
||||
tgt = poly.polyadd(ci, cj)
|
||||
quo, rem = poly.polydiv(tgt, ci)
|
||||
res = poly.polyadd(poly.polymul(quo, ci), rem)
|
||||
assert_equal(res, tgt, err_msg=msg)
|
||||
|
||||
|
||||
class TestEvaluation(object):
|
||||
# coefficients of 1 + 2*x + 3*x**2
|
||||
c1d = np.array([1., 2., 3.])
|
||||
c2d = np.einsum('i,j->ij', c1d, c1d)
|
||||
c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
|
||||
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
y = poly.polyval(x, [1., 2., 3.])
|
||||
|
||||
def test_polyval(self):
|
||||
#check empty input
|
||||
assert_equal(poly.polyval([], [1]).size, 0)
|
||||
|
||||
#check normal input)
|
||||
x = np.linspace(-1, 1)
|
||||
y = [x**i for i in range(5)]
|
||||
for i in range(5):
|
||||
tgt = y[i]
|
||||
res = poly.polyval(x, [0]*i + [1])
|
||||
assert_almost_equal(res, tgt)
|
||||
tgt = x*(x**2 - 1)
|
||||
res = poly.polyval(x, [0, -1, 0, 1])
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#check that shape is preserved
|
||||
for i in range(3):
|
||||
dims = [2]*i
|
||||
x = np.zeros(dims)
|
||||
assert_equal(poly.polyval(x, [1]).shape, dims)
|
||||
assert_equal(poly.polyval(x, [1, 0]).shape, dims)
|
||||
assert_equal(poly.polyval(x, [1, 0, 0]).shape, dims)
|
||||
|
||||
def test_polyvalfromroots(self):
|
||||
# check exception for broadcasting x values over root array with
|
||||
# too few dimensions
|
||||
assert_raises(ValueError, poly.polyvalfromroots,
|
||||
[1], [1], tensor=False)
|
||||
|
||||
# check empty input
|
||||
assert_equal(poly.polyvalfromroots([], [1]).size, 0)
|
||||
assert_(poly.polyvalfromroots([], [1]).shape == (0,))
|
||||
|
||||
# check empty input + multidimensional roots
|
||||
assert_equal(poly.polyvalfromroots([], [[1] * 5]).size, 0)
|
||||
assert_(poly.polyvalfromroots([], [[1] * 5]).shape == (5, 0))
|
||||
|
||||
# check scalar input
|
||||
assert_equal(poly.polyvalfromroots(1, 1), 0)
|
||||
assert_(poly.polyvalfromroots(1, np.ones((3, 3))).shape == (3,))
|
||||
|
||||
# check normal input)
|
||||
x = np.linspace(-1, 1)
|
||||
y = [x**i for i in range(5)]
|
||||
for i in range(1, 5):
|
||||
tgt = y[i]
|
||||
res = poly.polyvalfromroots(x, [0]*i)
|
||||
assert_almost_equal(res, tgt)
|
||||
tgt = x*(x - 1)*(x + 1)
|
||||
res = poly.polyvalfromroots(x, [-1, 0, 1])
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check that shape is preserved
|
||||
for i in range(3):
|
||||
dims = [2]*i
|
||||
x = np.zeros(dims)
|
||||
assert_equal(poly.polyvalfromroots(x, [1]).shape, dims)
|
||||
assert_equal(poly.polyvalfromroots(x, [1, 0]).shape, dims)
|
||||
assert_equal(poly.polyvalfromroots(x, [1, 0, 0]).shape, dims)
|
||||
|
||||
# check compatibility with factorization
|
||||
ptest = [15, 2, -16, -2, 1]
|
||||
r = poly.polyroots(ptest)
|
||||
x = np.linspace(-1, 1)
|
||||
assert_almost_equal(poly.polyval(x, ptest),
|
||||
poly.polyvalfromroots(x, r))
|
||||
|
||||
# check multidimensional arrays of roots and values
|
||||
# check tensor=False
|
||||
rshape = (3, 5)
|
||||
x = np.arange(-3, 2)
|
||||
r = np.random.randint(-5, 5, size=rshape)
|
||||
res = poly.polyvalfromroots(x, r, tensor=False)
|
||||
tgt = np.empty(r.shape[1:])
|
||||
for ii in range(tgt.size):
|
||||
tgt[ii] = poly.polyvalfromroots(x[ii], r[:, ii])
|
||||
assert_equal(res, tgt)
|
||||
|
||||
# check tensor=True
|
||||
x = np.vstack([x, 2*x])
|
||||
res = poly.polyvalfromroots(x, r, tensor=True)
|
||||
tgt = np.empty(r.shape[1:] + x.shape)
|
||||
for ii in range(r.shape[1]):
|
||||
for jj in range(x.shape[0]):
|
||||
tgt[ii, jj, :] = poly.polyvalfromroots(x[jj], r[:, ii])
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_polyval2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, poly.polyval2d, x1, x2[:2], self.c2d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2
|
||||
res = poly.polyval2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = poly.polyval2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_polyval3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test exceptions
|
||||
assert_raises(ValueError, poly.polyval3d, x1, x2, x3[:2], self.c3d)
|
||||
|
||||
#test values
|
||||
tgt = y1*y2*y3
|
||||
res = poly.polyval3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = poly.polyval3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3))
|
||||
|
||||
def test_polygrid2d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j->ij', y1, y2)
|
||||
res = poly.polygrid2d(x1, x2, self.c2d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = poly.polygrid2d(z, z, self.c2d)
|
||||
assert_(res.shape == (2, 3)*2)
|
||||
|
||||
def test_polygrid3d(self):
|
||||
x1, x2, x3 = self.x
|
||||
y1, y2, y3 = self.y
|
||||
|
||||
#test values
|
||||
tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
|
||||
res = poly.polygrid3d(x1, x2, x3, self.c3d)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
#test shape
|
||||
z = np.ones((2, 3))
|
||||
res = poly.polygrid3d(z, z, z, self.c3d)
|
||||
assert_(res.shape == (2, 3)*3)
|
||||
|
||||
|
||||
class TestIntegral(object):
|
||||
|
||||
def test_polyint(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, poly.polyint, [0], .5)
|
||||
assert_raises(ValueError, poly.polyint, [0], -1)
|
||||
assert_raises(ValueError, poly.polyint, [0], 1, [0, 0])
|
||||
assert_raises(ValueError, poly.polyint, [0], lbnd=[0])
|
||||
assert_raises(ValueError, poly.polyint, [0], scl=[0])
|
||||
assert_raises(ValueError, poly.polyint, [0], axis=.5)
|
||||
|
||||
# test integration of zero polynomial
|
||||
for i in range(2, 5):
|
||||
k = [0]*(i - 2) + [1]
|
||||
res = poly.polyint([0], m=i, k=k)
|
||||
assert_almost_equal(res, [0, 1])
|
||||
|
||||
# check single integration with integration constant
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [1/scl]
|
||||
res = poly.polyint(pol, m=1, k=[i])
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check single integration with integration constant and lbnd
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
res = poly.polyint(pol, m=1, k=[i], lbnd=-1)
|
||||
assert_almost_equal(poly.polyval(-1, res), i)
|
||||
|
||||
# check single integration with integration constant and scaling
|
||||
for i in range(5):
|
||||
scl = i + 1
|
||||
pol = [0]*i + [1]
|
||||
tgt = [i] + [0]*i + [2/scl]
|
||||
res = poly.polyint(pol, m=1, k=[i], scl=2)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with default k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = poly.polyint(tgt, m=1)
|
||||
res = poly.polyint(pol, m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with defined k
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = poly.polyint(tgt, m=1, k=[k])
|
||||
res = poly.polyint(pol, m=j, k=list(range(j)))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with lbnd
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1)
|
||||
res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check multiple integrations with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
pol = [0]*i + [1]
|
||||
tgt = pol[:]
|
||||
for k in range(j):
|
||||
tgt = poly.polyint(tgt, m=1, k=[k], scl=2)
|
||||
res = poly.polyint(pol, m=j, k=list(range(j)), scl=2)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_polyint_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([poly.polyint(c) for c in c2d.T]).T
|
||||
res = poly.polyint(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([poly.polyint(c) for c in c2d])
|
||||
res = poly.polyint(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([poly.polyint(c, k=3) for c in c2d])
|
||||
res = poly.polyint(c2d, k=3, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestDerivative(object):
|
||||
|
||||
def test_polyder(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, poly.polyder, [0], .5)
|
||||
assert_raises(ValueError, poly.polyder, [0], -1)
|
||||
|
||||
# check that zeroth derivative does nothing
|
||||
for i in range(5):
|
||||
tgt = [0]*i + [1]
|
||||
res = poly.polyder(tgt, m=0)
|
||||
assert_equal(trim(res), trim(tgt))
|
||||
|
||||
# check that derivation is the inverse of integration
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = poly.polyder(poly.polyint(tgt, m=j), m=j)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
# check derivation with scaling
|
||||
for i in range(5):
|
||||
for j in range(2, 5):
|
||||
tgt = [0]*i + [1]
|
||||
res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_polyder_axis(self):
|
||||
# check that axis keyword works
|
||||
c2d = np.random.random((3, 4))
|
||||
|
||||
tgt = np.vstack([poly.polyder(c) for c in c2d.T]).T
|
||||
res = poly.polyder(c2d, axis=0)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
tgt = np.vstack([poly.polyder(c) for c in c2d])
|
||||
res = poly.polyder(c2d, axis=1)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
class TestVander(object):
|
||||
# some random values in [-1, 1)
|
||||
x = np.random.random((3, 5))*2 - 1
|
||||
|
||||
def test_polyvander(self):
|
||||
# check for 1d x
|
||||
x = np.arange(3)
|
||||
v = poly.polyvander(x, 3)
|
||||
assert_(v.shape == (3, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], poly.polyval(x, coef))
|
||||
|
||||
# check for 2d x
|
||||
x = np.array([[1, 2], [3, 4], [5, 6]])
|
||||
v = poly.polyvander(x, 3)
|
||||
assert_(v.shape == (3, 2, 4))
|
||||
for i in range(4):
|
||||
coef = [0]*i + [1]
|
||||
assert_almost_equal(v[..., i], poly.polyval(x, coef))
|
||||
|
||||
def test_polyvander2d(self):
|
||||
# also tests polyval2d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3))
|
||||
van = poly.polyvander2d(x1, x2, [1, 2])
|
||||
tgt = poly.polyval2d(x1, x2, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = poly.polyvander2d([x1], [x2], [1, 2])
|
||||
assert_(van.shape == (1, 5, 6))
|
||||
|
||||
def test_polyvander3d(self):
|
||||
# also tests polyval3d for non-square coefficient array
|
||||
x1, x2, x3 = self.x
|
||||
c = np.random.random((2, 3, 4))
|
||||
van = poly.polyvander3d(x1, x2, x3, [1, 2, 3])
|
||||
tgt = poly.polyval3d(x1, x2, x3, c)
|
||||
res = np.dot(van, c.flat)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# check shape
|
||||
van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3])
|
||||
assert_(van.shape == (1, 5, 24))
|
||||
|
||||
|
||||
class TestCompanion(object):
|
||||
|
||||
def test_raises(self):
|
||||
assert_raises(ValueError, poly.polycompanion, [])
|
||||
assert_raises(ValueError, poly.polycompanion, [1])
|
||||
|
||||
def test_dimensions(self):
|
||||
for i in range(1, 5):
|
||||
coef = [0]*i + [1]
|
||||
assert_(poly.polycompanion(coef).shape == (i, i))
|
||||
|
||||
def test_linear_root(self):
|
||||
assert_(poly.polycompanion([1, 2])[0, 0] == -.5)
|
||||
|
||||
|
||||
class TestMisc(object):
|
||||
|
||||
def test_polyfromroots(self):
|
||||
res = poly.polyfromroots([])
|
||||
assert_almost_equal(trim(res), [1])
|
||||
for i in range(1, 5):
|
||||
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
|
||||
tgt = Tlist[i]
|
||||
res = poly.polyfromroots(roots)*2**(i-1)
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_polyroots(self):
|
||||
assert_almost_equal(poly.polyroots([1]), [])
|
||||
assert_almost_equal(poly.polyroots([1, 2]), [-.5])
|
||||
for i in range(2, 5):
|
||||
tgt = np.linspace(-1, 1, i)
|
||||
res = poly.polyroots(poly.polyfromroots(tgt))
|
||||
assert_almost_equal(trim(res), trim(tgt))
|
||||
|
||||
def test_polyfit(self):
|
||||
def f(x):
|
||||
return x*(x - 1)*(x - 2)
|
||||
|
||||
def f2(x):
|
||||
return x**4 + x**2 + 1
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, poly.polyfit, [1], [1], -1)
|
||||
assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
|
||||
assert_raises(TypeError, poly.polyfit, [], [1], 0)
|
||||
assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
|
||||
assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
|
||||
assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
|
||||
assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
|
||||
assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
|
||||
assert_raises(ValueError, poly.polyfit, [1], [1], [-1,])
|
||||
assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6])
|
||||
assert_raises(TypeError, poly.polyfit, [1], [1], [])
|
||||
|
||||
# Test fit
|
||||
x = np.linspace(0, 2)
|
||||
y = f(x)
|
||||
#
|
||||
coef3 = poly.polyfit(x, y, 3)
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(poly.polyval(x, coef3), y)
|
||||
coef3 = poly.polyfit(x, y, [0, 1, 2, 3])
|
||||
assert_equal(len(coef3), 4)
|
||||
assert_almost_equal(poly.polyval(x, coef3), y)
|
||||
#
|
||||
coef4 = poly.polyfit(x, y, 4)
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(poly.polyval(x, coef4), y)
|
||||
coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4])
|
||||
assert_equal(len(coef4), 5)
|
||||
assert_almost_equal(poly.polyval(x, coef4), y)
|
||||
#
|
||||
coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3])
|
||||
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
|
||||
# test weighting
|
||||
w = np.zeros_like(x)
|
||||
yw = y.copy()
|
||||
w[1::2] = 1
|
||||
yw[0::2] = 0
|
||||
wcoef3 = poly.polyfit(x, yw, 3, w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef3, coef3)
|
||||
#
|
||||
wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
|
||||
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
|
||||
# test scaling with complex values x points whose square
|
||||
# is zero when summed.
|
||||
x = [1, 1j, -1, -1j]
|
||||
assert_almost_equal(poly.polyfit(x, x, 1), [0, 1])
|
||||
assert_almost_equal(poly.polyfit(x, x, [0, 1]), [0, 1])
|
||||
# test fitting only even Polyendre polynomials
|
||||
x = np.linspace(-1, 1)
|
||||
y = f2(x)
|
||||
coef1 = poly.polyfit(x, y, 4)
|
||||
assert_almost_equal(poly.polyval(x, coef1), y)
|
||||
coef2 = poly.polyfit(x, y, [0, 2, 4])
|
||||
assert_almost_equal(poly.polyval(x, coef2), y)
|
||||
assert_almost_equal(coef1, coef2)
|
||||
|
||||
def test_polytrim(self):
|
||||
coef = [2, -1, 1, 0]
|
||||
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, poly.polytrim, coef, -1)
|
||||
|
||||
# Test results
|
||||
assert_equal(poly.polytrim(coef), coef[:-1])
|
||||
assert_equal(poly.polytrim(coef, 1), coef[:-3])
|
||||
assert_equal(poly.polytrim(coef, 2), [0])
|
||||
|
||||
def test_polyline(self):
|
||||
assert_equal(poly.polyline(3, 4), [3, 4])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,110 @@
|
||||
"""Tests for polyutils module.
|
||||
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy as np
|
||||
import numpy.polynomial.polyutils as pu
|
||||
from numpy.testing import (
|
||||
assert_almost_equal, assert_raises, assert_equal, assert_,
|
||||
run_module_suite
|
||||
)
|
||||
|
||||
|
||||
class TestMisc(object):
|
||||
|
||||
def test_trimseq(self):
|
||||
for i in range(5):
|
||||
tgt = [1]
|
||||
res = pu.trimseq([1] + [0]*5)
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_as_series(self):
|
||||
# check exceptions
|
||||
assert_raises(ValueError, pu.as_series, [[]])
|
||||
assert_raises(ValueError, pu.as_series, [[[1, 2]]])
|
||||
assert_raises(ValueError, pu.as_series, [[1], ['a']])
|
||||
# check common types
|
||||
types = ['i', 'd', 'O']
|
||||
for i in range(len(types)):
|
||||
for j in range(i):
|
||||
ci = np.ones(1, types[i])
|
||||
cj = np.ones(1, types[j])
|
||||
[resi, resj] = pu.as_series([ci, cj])
|
||||
assert_(resi.dtype.char == resj.dtype.char)
|
||||
assert_(resj.dtype.char == types[i])
|
||||
|
||||
def test_trimcoef(self):
|
||||
coef = [2, -1, 1, 0]
|
||||
# Test exceptions
|
||||
assert_raises(ValueError, pu.trimcoef, coef, -1)
|
||||
# Test results
|
||||
assert_equal(pu.trimcoef(coef), coef[:-1])
|
||||
assert_equal(pu.trimcoef(coef, 1), coef[:-3])
|
||||
assert_equal(pu.trimcoef(coef, 2), [0])
|
||||
|
||||
|
||||
class TestDomain(object):
|
||||
|
||||
def test_getdomain(self):
|
||||
# test for real values
|
||||
x = [1, 10, 3, -1]
|
||||
tgt = [-1, 10]
|
||||
res = pu.getdomain(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# test for complex values
|
||||
x = [1 + 1j, 1 - 1j, 0, 2]
|
||||
tgt = [-1j, 2 + 1j]
|
||||
res = pu.getdomain(x)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
def test_mapdomain(self):
|
||||
# test for real values
|
||||
dom1 = [0, 4]
|
||||
dom2 = [1, 3]
|
||||
tgt = dom2
|
||||
res = pu. mapdomain(dom1, dom1, dom2)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# test for complex values
|
||||
dom1 = [0 - 1j, 2 + 1j]
|
||||
dom2 = [-2, 2]
|
||||
tgt = dom2
|
||||
x = dom1
|
||||
res = pu.mapdomain(x, dom1, dom2)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# test for multidimensional arrays
|
||||
dom1 = [0, 4]
|
||||
dom2 = [1, 3]
|
||||
tgt = np.array([dom2, dom2])
|
||||
x = np.array([dom1, dom1])
|
||||
res = pu.mapdomain(x, dom1, dom2)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# test that subtypes are preserved.
|
||||
dom1 = [0, 4]
|
||||
dom2 = [1, 3]
|
||||
x = np.matrix([dom1, dom1])
|
||||
res = pu.mapdomain(x, dom1, dom2)
|
||||
assert_(isinstance(res, np.matrix))
|
||||
|
||||
def test_mapparms(self):
|
||||
# test for real values
|
||||
dom1 = [0, 4]
|
||||
dom2 = [1, 3]
|
||||
tgt = [1, .5]
|
||||
res = pu. mapparms(dom1, dom2)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
# test for complex values
|
||||
dom1 = [0 - 1j, 2 + 1j]
|
||||
dom2 = [-2, 2]
|
||||
tgt = [-1 + 1j, 1 - 1j]
|
||||
res = pu.mapparms(dom1, dom2)
|
||||
assert_almost_equal(res, tgt)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
@@ -0,0 +1,74 @@
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import numpy.polynomial as poly
|
||||
from numpy.testing import run_module_suite, assert_equal
|
||||
|
||||
|
||||
class TestStr(object):
|
||||
def test_polynomial_str(self):
|
||||
res = str(poly.Polynomial([0, 1]))
|
||||
tgt = 'poly([0. 1.])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_chebyshev_str(self):
|
||||
res = str(poly.Chebyshev([0, 1]))
|
||||
tgt = 'cheb([0. 1.])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_legendre_str(self):
|
||||
res = str(poly.Legendre([0, 1]))
|
||||
tgt = 'leg([0. 1.])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_hermite_str(self):
|
||||
res = str(poly.Hermite([0, 1]))
|
||||
tgt = 'herm([0. 1.])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_hermiteE_str(self):
|
||||
res = str(poly.HermiteE([0, 1]))
|
||||
tgt = 'herme([0. 1.])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_laguerre_str(self):
|
||||
res = str(poly.Laguerre([0, 1]))
|
||||
tgt = 'lag([0. 1.])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
|
||||
class TestRepr(object):
|
||||
def test_polynomial_str(self):
|
||||
res = repr(poly.Polynomial([0, 1]))
|
||||
tgt = 'Polynomial([0., 1.], domain=[-1, 1], window=[-1, 1])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_chebyshev_str(self):
|
||||
res = repr(poly.Chebyshev([0, 1]))
|
||||
tgt = 'Chebyshev([0., 1.], domain=[-1, 1], window=[-1, 1])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_legendre_repr(self):
|
||||
res = repr(poly.Legendre([0, 1]))
|
||||
tgt = 'Legendre([0., 1.], domain=[-1, 1], window=[-1, 1])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_hermite_repr(self):
|
||||
res = repr(poly.Hermite([0, 1]))
|
||||
tgt = 'Hermite([0., 1.], domain=[-1, 1], window=[-1, 1])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_hermiteE_repr(self):
|
||||
res = repr(poly.HermiteE([0, 1]))
|
||||
tgt = 'HermiteE([0., 1.], domain=[-1, 1], window=[-1, 1])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
def test_laguerre_repr(self):
|
||||
res = repr(poly.Laguerre([0, 1]))
|
||||
tgt = 'Laguerre([0., 1.], domain=[0, 1], window=[0, 1])'
|
||||
assert_equal(res, tgt)
|
||||
|
||||
|
||||
#
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
Reference in New Issue
Block a user