Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ENH, WIP: Add 2-D fitting function for polynomials #14151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bugfixes, so that test work
  • Loading branch information
AWehrhahn committed May 13, 2020
commit fcb83cea953a138df5c6f2d29a4149f297211d5c
18 changes: 9 additions & 9 deletions 18 numpy/polynomial/polyutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,23 +725,23 @@ def _fit(vander_f, x, y, deg, rcond=None, full=False, w=None):
else:
return c

def _get_coeff_idx(coeff):
def _get_coeff_idx(shape):
"""
Get the powers of x and y corresponding to a 2d coefficient matrix
in the same order as the vandermode matrix,
i.e. [[0, 0], [1, 0], ...]

Parameters
----------
coeff : array of shape (n, m)
coefficient matrix
shape : 2-tuple
shape of the coefficient matrix, i.e. degree + 1

Returns
-------
idx : array of shape (n * m, 2)
pairs of indices to the coefficient matrix
"""
idx = np.indices(coeff.shape)
idx = np.indices(shape)
idx = idx.T.swapaxes(0, 1).reshape((-1, 2))
return idx

Expand Down Expand Up @@ -835,7 +835,7 @@ def polyscale2d(coeff, scale_x, scale_y, copy=True):
"""
if copy:
coeff = np.copy(coeff)
idx = _get_coeff_idx(coeff)
idx = _get_coeff_idx(coeff.shape)
for k, (i, j) in enumerate(idx):
coeff[i, j] /= scale_x ** i * scale_y ** j
return coeff
Expand Down Expand Up @@ -870,7 +870,7 @@ def polyshift2d(coeff, offset_x, offset_y, copy=True):
"""
if copy:
coeff = np.copy(coeff)
idx = _get_coeff_idx(coeff)
idx = _get_coeff_idx(coeff.shape)
# Copy coeff because it changes during the loop
coeff2 = np.copy(coeff)
for k, m in idx:
Expand Down Expand Up @@ -945,8 +945,7 @@ def _fit2d(vander2d_f, x, y, z, deg=1, max_degree=None, w=None, scale=True, rcon
if deg.size == 1:
deg = np.array([deg, deg])
deg = deg[:2]
coeff = np.zeros((deg[0] + 1, deg[1] + 1))
idx = _get_coeff_idx(coeff)
idx = _get_coeff_idx(deg+1)

# Scale coordinates to smaller values to avoid numerical problems at larger degrees
if scale:
Expand All @@ -970,7 +969,7 @@ def _fit2d(vander2d_f, x, y, z, deg=1, max_degree=None, w=None, scale=True, rcon
raise TypeError("expected 1D vector for w")
if len(x) != len(w):
raise TypeError("expected x and w to have same length")
A = A * w
A = A * w[:, None]
z = z * w

if rcond is None:
Expand All @@ -980,6 +979,7 @@ def _fit2d(vander2d_f, x, y, z, deg=1, max_degree=None, w=None, scale=True, rcon
C, resids, rank, s = np.linalg.lstsq(A, z, rcond)

# Reorder coefficients into numpy compatible 2d array
coeff = np.zeros(deg + 1, dtype=C.dtype)
for k, (i, j) in enumerate(idx):
coeff[i, j] = C[k]

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.