Skip to content

Commit 966b0d2

Browse files
committed
Avoid another Eigen->NumPy conversion
1 parent f7e0015 commit 966b0d2

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/matplotlib/tests/test_transforms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,8 @@ def test_line_extent_compound_coords2(self):
778778
trans = mtransforms.blended_transform_factory(
779779
ax.transAxes, mtransforms.Affine2D().scale(10) + ax.transData)
780780
ax.plot([0.1, 1.2, 0.8], [35, -5, 18], transform=trans)
781-
assert_array_equal(ax.dataLim.get_points(),
782-
np.array([[np.inf, -50.], [-np.inf, 350.]]))
781+
assert_array_almost_equal(ax.dataLim.get_points(),
782+
np.array([[np.inf, -50.], [-np.inf, 350.]]))
783783

784784
def test_line_extents_affine(self):
785785
ax = plt.axes()

lib/matplotlib/transforms.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import math
4343

4444
import numpy as np
45-
from numpy.linalg import inv
4645

4746
from matplotlib import _api, _eigen
4847
from matplotlib._path import count_bboxes_overlapping_bbox
@@ -1902,11 +1901,11 @@ def transform_affine(self, values):
19021901
def inverted(self):
19031902
# docstring inherited
19041903
if self._inverted is None or self._invalid:
1905-
mtx = self.get_matrix()
1904+
mtx = self._get_eigen_matrix()
19061905
shorthand_name = None
19071906
if self._shorthand_name:
1908-
shorthand_name = '(%s)-1' % self._shorthand_name
1909-
self._inverted = Affine2D(inv(mtx), shorthand_name=shorthand_name)
1907+
shorthand_name = '({self._shorthand_name})-1'
1908+
self._inverted = Affine2D(mtx.inverse(), shorthand_name=shorthand_name)
19101909
self._invalid = 0
19111910
return self._inverted
19121911

src/_eigen.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ PYBIND11_MODULE(_eigen, m) {
1212
.def(py::init([]() {
1313
return Eigen::Affine2d::Identity();
1414
}))
15+
.def(py::init([](const Eigen::Affine2d& matrix) {
16+
return Eigen::Affine2d(matrix);
17+
}),
18+
"matrix"_a.none(false))
1519
.def(py::init([](const Eigen::Matrix3d& matrix) {
1620
return Eigen::Affine2d(matrix);
1721
}),
@@ -76,6 +80,11 @@ PYBIND11_MODULE(_eigen, m) {
7680
}, "other"_a,
7781
"Combines two transforms.")
7882

83+
.def("inverse", [](const Eigen::Affine2d& self) {
84+
return self.inverse();
85+
},
86+
"Calculate the inverse of this transformation.")
87+
7988
.def("remove_translate", [](Eigen::Affine2d& self) {
8089
self(0, 2) = 0.0;
8190
self(1, 2) = 0.0;

0 commit comments

Comments
 (0)