Tests Eigen's ability to mutate numpy values
()
| 447 | |
| 448 | |
| 449 | def test_eigen_ref_mutators(): |
| 450 | """Tests Eigen's ability to mutate numpy values""" |
| 451 | |
| 452 | orig = np.array([[1.0, 2, 3], [4, 5, 6], [7, 8, 9]]) |
| 453 | zr = np.array(orig) |
| 454 | zc = np.array(orig, order="F") |
| 455 | m.add_rm(zr, 1, 0, 100) |
| 456 | assert np.all(zr == np.array([[1.0, 2, 3], [104, 5, 6], [7, 8, 9]])) |
| 457 | m.add_cm(zc, 1, 0, 200) |
| 458 | assert np.all(zc == np.array([[1.0, 2, 3], [204, 5, 6], [7, 8, 9]])) |
| 459 | |
| 460 | m.add_any(zr, 1, 0, 20) |
| 461 | assert np.all(zr == np.array([[1.0, 2, 3], [124, 5, 6], [7, 8, 9]])) |
| 462 | m.add_any(zc, 1, 0, 10) |
| 463 | assert np.all(zc == np.array([[1.0, 2, 3], [214, 5, 6], [7, 8, 9]])) |
| 464 | |
| 465 | # Can't reference a col-major array with a row-major Ref, and vice versa: |
| 466 | with pytest.raises(TypeError): |
| 467 | m.add_rm(zc, 1, 0, 1) |
| 468 | with pytest.raises(TypeError): |
| 469 | m.add_cm(zr, 1, 0, 1) |
| 470 | |
| 471 | # Overloads: |
| 472 | m.add1(zr, 1, 0, -100) |
| 473 | m.add2(zr, 1, 0, -20) |
| 474 | assert np.all(zr == orig) |
| 475 | m.add1(zc, 1, 0, -200) |
| 476 | m.add2(zc, 1, 0, -10) |
| 477 | assert np.all(zc == orig) |
| 478 | |
| 479 | # a non-contiguous slice (this won't work on either the row- or |
| 480 | # column-contiguous refs, but should work for the any) |
| 481 | cornersr = zr[0::2, 0::2] |
| 482 | cornersc = zc[0::2, 0::2] |
| 483 | |
| 484 | assert np.all(cornersr == np.array([[1.0, 3], [7, 9]])) |
| 485 | assert np.all(cornersc == np.array([[1.0, 3], [7, 9]])) |
| 486 | |
| 487 | with pytest.raises(TypeError): |
| 488 | m.add_rm(cornersr, 0, 1, 25) |
| 489 | with pytest.raises(TypeError): |
| 490 | m.add_cm(cornersr, 0, 1, 25) |
| 491 | with pytest.raises(TypeError): |
| 492 | m.add_rm(cornersc, 0, 1, 25) |
| 493 | with pytest.raises(TypeError): |
| 494 | m.add_cm(cornersc, 0, 1, 25) |
| 495 | m.add_any(cornersr, 0, 1, 25) |
| 496 | m.add_any(cornersc, 0, 1, 44) |
| 497 | assert np.all(zr == np.array([[1.0, 2, 28], [4, 5, 6], [7, 8, 9]])) |
| 498 | assert np.all(zc == np.array([[1.0, 2, 47], [4, 5, 6], [7, 8, 9]])) |
| 499 | |
| 500 | # You shouldn't be allowed to pass a non-writeable array to a mutating Eigen method: |
| 501 | zro = zr[0:4, 0:4] |
| 502 | zro.flags.writeable = False |
| 503 | with pytest.raises(TypeError): |
| 504 | m.add_rm(zro, 0, 0, 0) |
| 505 | with pytest.raises(TypeError): |
| 506 | m.add_any(zro, 0, 0, 0) |