🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@anntzer
Copy link
Contributor

@anntzer anntzer commented Jan 5, 2022

The following test script shows a ~3x speedup.

import math, numpy as np

mtx = np.array([[.1, .2, .3], [.4, .5, .6], [0, 0, 1]])
theta = np.pi / 4

def rotate(mtx, theta):
    a = math.cos(theta)
    b = math.sin(theta)
    rotate_mtx = np.array([[a, -b, 0.0], [b, a, 0.0], [0.0, 0.0, 1.0]],
                          float)
    return np.dot(rotate_mtx, mtx)

def rfast(mtx, theta):
    a = math.cos(theta)
    b = math.sin(theta)
    (xx, xy, x0), (yx, yy, y0), _ = mtx.tolist()
    # mtx = [[a -b 0], [b a 0], [0 0 1]] * mtx
    mtx[0, 0] = a * xx - b * yx
    mtx[0, 1] = a * xy - b * yy
    mtx[0, 2] = a * x0 - b * y0
    mtx[1, 0] = b * xx + a * yx
    mtx[1, 1] = b * xy + a * yy
    mtx[1, 2] = b * x0 + a * y0
    return mtx

%timeit rotate(mtx, theta)
%timeit rfast(mtx, theta)

PR Summary

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

The following test script shows a ~3x speedup.

```python
import math, numpy as np

mtx = np.array([[.1, .2, .3], [.4, .5, .6], [0, 0, 1]])
theta = np.pi / 4

def rotate(mtx, theta):
    a = math.cos(theta)
    b = math.sin(theta)
    rotate_mtx = np.array([[a, -b, 0.0], [b, a, 0.0], [0.0, 0.0, 1.0]],
                          float)
    return np.dot(rotate_mtx, mtx)

def rfast(mtx, theta):
    a = math.cos(theta)
    b = math.sin(theta)
    (xx, xy, x0), (yx, yy, y0), _ = mtx.tolist()
    # mtx = [[a -b 0], [b a 0], [0 0 1]] * mtx
    mtx[0, 0] = a * xx - b * yx
    mtx[0, 1] = a * xy - b * yy
    mtx[0, 2] = a * x0 - b * y0
    mtx[1, 0] = b * xx + a * yx
    mtx[1, 1] = b * xy + a * yy
    mtx[1, 2] = b * x0 + a * y0
    return mtx

%timeit rotate(mtx, theta)
%timeit rfast(mtx, theta)
```
Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mtx isn't a pointer, is it?

@anntzer
Copy link
Contributor Author

anntzer commented Jan 5, 2022

It is. (Or rather, modifications are done in place.)

@tacaswell tacaswell added this to the v3.6.0 milestone Jan 5, 2022
Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anntzer

Some notes, possibly for follow-ups:

  • skew() could also be rewritten to be element-wise
  • Does it still make sense to keep Affine2D._mtx as a numpy array if we only do element-wise operations?

@timhoffm timhoffm merged commit 45de25e into matplotlib:main Jan 5, 2022
@anntzer anntzer deleted the fr branch January 5, 2022 22:44
@anntzer anntzer mentioned this pull request Jan 5, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants