maroba / findiff

Python package for numerical derivatives and partial differential equations in any number of dimensions.
MIT License
420 stars 60 forks source link

Cannot get matrix representation of irregular spaced operator #21

Closed juliusbierk closed 4 years ago

juliusbierk commented 4 years ago

First of all, thanks for a great library!

An example of the problem I'm having:

from findiff import FinDiff
import numpy as np

x = np.logspace(0, 2, 100)
operator = FinDiff(0, x)
f_test = x**2

assert np.max(np.abs((operator(f_test) - 2*x))) < 1e-12  # Works fine

m_op = operator.matrix(x.shape)  # fails

Traceback:

Traceback (most recent call last):
  File "/home/julius/.PyCharm2019.3/config/scratches/SH.py", line 10, in <module>
    m_op = operator.matrix(x.shape)
  File "/usr/local/lib/python3.6/dist-packages/findiff/operators.py", line 106, in matrix
    return self.pds.matrix(shape, h, acc)
  File "/usr/local/lib/python3.6/dist-packages/findiff/diff.py", line 415, in matrix
    v = c / h**order
TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'a

In principle there should be no reason to give the shape of x, since this is internally known, however, calling operator.matrix() throws missing argument exception.

maroba commented 4 years ago

Currently the matrix representation is only supported for uniform grids. The exception is thrown because for nonuniform grids h is None. I will fix that to make sure that this gives a reasonable error message and for the next release I'll try to work on the matrix representation for non-uniform grids. Thanks for reporting this error!