MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_both_ref_mutators

Function test_both_ref_mutators

tests/test_eigen_matrix.py:570–611  ·  view source on GitHub ↗

Tests a complex chain of nested eigen/numpy references

()

Source from the content-addressed store, hash-verified

568
569
570def test_both_ref_mutators():
571 """Tests a complex chain of nested eigen/numpy references"""
572
573 m.reset_refs() # In case another test already changed it
574
575 z = m.get_cm_ref() # numpy -> eigen
576 z[0, 2] -= 3
577 z2 = m.incr_matrix(z, 1) # numpy -> eigen -> numpy -> eigen
578 z2[1, 1] += 6
579 z3 = m.incr_matrix(z, 2) # (numpy -> eigen)^3
580 z3[2, 2] += -5
581 z4 = m.incr_matrix(z, 3) # (numpy -> eigen)^4
582 z4[1, 1] -= 1
583 z5 = m.incr_matrix(z, 4) # (numpy -> eigen)^5
584 z5[0, 0] = 0
585 assert np.all(z == z2)
586 assert np.all(z == z3)
587 assert np.all(z == z4)
588 assert np.all(z == z5)
589 expect = np.array([[0.0, 22, 20], [31, 37, 33], [41, 42, 38]])
590 assert np.all(z == expect)
591
592 y = np.array(range(100), dtype="float64").reshape(10, 10)
593 y2 = m.incr_matrix_any(y, 10) # np -> eigen -> np
594 y3 = m.incr_matrix_any(
595 y2[0::2, 0::2], -33
596 ) # np -> eigen -> np slice -> np -> eigen -> np
597 y4 = m.even_rows(y3) # numpy -> eigen slice -> (... y3)
598 y5 = m.even_cols(y4) # numpy -> eigen slice -> (... y4)
599 y6 = m.incr_matrix_any(y5, 1000) # numpy -> eigen -> (... y5)
600
601 # Apply same mutations using just numpy:
602 yexpect = np.array(range(100), dtype="float64").reshape(10, 10)
603 yexpect += 10
604 yexpect[0::2, 0::2] -= 33
605 yexpect[0::4, 0::4] += 1000
606 assert np.all(y6 == yexpect[0::4, 0::4])
607 assert np.all(y5 == yexpect[0::4, 0::4])
608 assert np.all(y4 == yexpect[0::4, 0::2])
609 assert np.all(y3 == yexpect[0::2, 0::2])
610 assert np.all(y2 == yexpect)
611 assert np.all(y == yexpect)
612
613
614def test_nocopy_wrapper():

Callers

nothing calls this directly

Calls 2

arrayMethod · 0.80
reshapeMethod · 0.80

Tested by

no test coverage detected