Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions docs/notebooks/21_lagrange.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion qmat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

**Utility modules** ⚙️

- :class:`lagrange` : Barycentric polynomial approximations (integral, interpolation, derivation)
- :class:`lagrange` : Barycentric polynomial approximations (integral, interpolation, derivative)
- :class:`nodes` : generation of multiple types of quadrature nodes
- :class:`sdc` : utility function to run SDC on simple problems
- :class:`mathutils` : utility functions for math operations
Expand Down
25 changes: 15 additions & 10 deletions qmat/lagrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
"""
Base module for Barycentric Lagrange Approximation, based on `[Berrut & Trefethen, 2004] <https://doi.org/10.1137/S0036144502417715>`_.
Allows to easily build integration / interpolation / derivation matrices, from any list of node points.
Allows to easily build integration / interpolation / derivative matrices, from any list of node points.

Examples
--------
Expand All @@ -22,8 +22,8 @@
>>> # Alternative interpolation using the object as a function
>>> uFine = approx(fGrid)
>>>
>>> # Derivation
>>> D = approx.getDerivationMatrix()
>>> # Derivative
>>> D = approx.getDerivativeMatrix()
>>> du = D @ u
"""
import numpy as np
Expand Down Expand Up @@ -437,9 +437,9 @@ def getIntegrationMatrix(self, intervals, numQuad='FEJER', duplicates=True):

return Q

def getDerivationMatrix(self, order=1, duplicates=True):
def getDerivativeMatrix(self, order=1, duplicates=True):
r"""
Generate derivation matrix of first or second order (or both) based on
Generate derivative matrix of first or second order (or both) based on
the Lagrange interpolant.
The first order differentiation matrix :math:`D^{(1)}` approximates

Expand Down Expand Up @@ -473,11 +473,11 @@ def getDerivationMatrix(self, order=1, duplicates=True):
.. math::
D^{(2)}_{jj} = -\sum_{i \neq j} D^{(2)}_{ij}

⚠️ If you want a derivation matrix with many points (~1000 or more),
⚠️ If you want a derivative matrix with many points (~1000 or more),
favor the use of `weightComputation="STABLE"` when initializing
the `LagrangeApproximation` object. If not, some (very small) weights
could be approximated by zeros, which would make the computation
of the derivation matrices fail ...
of the derivative matrices fail ...

Note
----
Expand All @@ -487,7 +487,7 @@ def getDerivationMatrix(self, order=1, duplicates=True):
Parameters
----------
order : int or str, optional
The order of the derivation matrix, use "ALL" to retrieve both.
The order of the derivative matrix, use "ALL" to retrieve both.
The default is 1.
duplicates : bool
Wether or not take into account duplicates in the points.
Expand All @@ -497,8 +497,8 @@ def getDerivationMatrix(self, order=1, duplicates=True):
Returns
-------
D : np.2darray or tuple of np.2darray
Derivation matrix. If order="ALL", return a tuple containing all
derivations matrix in increasing derivation order.
Derivative matrix. If order="ALL", return a tuple containing all
derivative matrices in increasing derivative order.
"""
if order not in [1, 2, "ALL"]:
raise NotImplementedError(f"order={order}")
Expand Down Expand Up @@ -531,6 +531,11 @@ def getDerivationMatrix(self, order=1, duplicates=True):
else:
return D1, D2

def getDerivationMatrix(self, *args, **kwargs):
import warnings
warnings.warn("Function `getDerivationMatrix` is deprecated. Use `getDerivativeMatrix` instead!", DeprecationWarning)
return self.getDerivativeMatrix(*args, **kwargs)


def getSparseInterpolationMatrix(inPoints, outPoints, order):
"""
Expand Down
12 changes: 6 additions & 6 deletions tests/test_2_lagrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def testIntegration(nNodes, weightComputation, numQuad):

@pytest.mark.parametrize("weightComputation", ["AUTO", "FAST", "STABLE", "CHEBFUN"])
@pytest.mark.parametrize("nNodes", nNodeTests)
def testDerivation(nNodes, weightComputation):
def testDerivative(nNodes, weightComputation):
nodes = np.sort(np.random.rand(nNodes))
approx = LagrangeApproximation(nodes, weightComputation=weightComputation)

D1, D2 = approx.getDerivationMatrix(order="ALL")
D1, D2 = approx.getDerivativeMatrix(order="ALL")

assert np.allclose(D1, approx.getDerivationMatrix())
assert np.allclose(D1, approx.getDerivativeMatrix())
assert np.allclose(D2, approx.getDerivationMatrix(order=2))

polyCoeffs = np.random.rand(nNodes)
Expand Down Expand Up @@ -156,9 +156,9 @@ def testDuplicates(nPoints, nCopy, duplicates):
assert np.allclose(Q[:, approx._nnzIdx], Q_ref), "[Q] nonzero values different from reference"
assert np.allclose(Q_noDuplicates, Q_ref), "[Q] no duplicates values different from reference"

D1, D2 = approx.getDerivationMatrix(order="ALL")
D1_noDuplicates, D2_noDuplicates = approx.getDerivationMatrix(order="ALL", duplicates=False)
D1_ref, D2_ref = approxUnique.getDerivationMatrix(order="ALL")
D1, D2 = approx.getDerivativeMatrix(order="ALL")
D1_noDuplicates, D2_noDuplicates = approx.getDerivativeMatrix(order="ALL", duplicates=False)
D1_ref, D2_ref = approxUnique.getDerivativeMatrix(order="ALL")

assert np.allclose(D1[:, approx._zerIdx], 0), "[D1] zero indices have non-zero values"
assert np.allclose(D1[:, approx._nnzIdx], D1_ref), "[D1] nonzero values different from reference"
Expand Down