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

Function test_numpy_ref_mutators

tests/test_eigen_matrix.py:518–567  ·  view source on GitHub ↗

Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)

()

Source from the content-addressed store, hash-verified

516
517
518def test_numpy_ref_mutators():
519 """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)"""
520
521 m.reset_refs() # In case another test already changed it
522
523 zc = m.get_cm_ref()
524 zcro = m.get_cm_const_ref()
525 zr = m.get_rm_ref()
526 zrro = m.get_rm_const_ref()
527
528 assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4
529
530 assert not zc.flags.owndata
531 assert zc.flags.writeable
532 assert not zr.flags.owndata
533 assert zr.flags.writeable
534 assert not zcro.flags.owndata
535 assert not zcro.flags.writeable
536 assert not zrro.flags.owndata
537 assert not zrro.flags.writeable
538
539 zc[1, 2] = 99
540 expect = np.array([[11.0, 12, 13], [21, 22, 99], [31, 32, 33]])
541 # We should have just changed zc, of course, but also zcro and the original eigen matrix
542 assert np.all(zc == expect)
543 assert np.all(zcro == expect)
544 assert np.all(m.get_cm_ref() == expect)
545
546 zr[1, 2] = 99
547 assert np.all(zr == expect)
548 assert np.all(zrro == expect)
549 assert np.all(m.get_rm_ref() == expect)
550
551 # Make sure the readonly ones are numpy-readonly:
552 with pytest.raises(ValueError):
553 zcro[1, 2] = 6
554 with pytest.raises(ValueError):
555 zrro[1, 2] = 6
556
557 # We should be able to explicitly copy like this (and since we're copying,
558 # the const should drop away)
559 y1 = np.array(m.get_cm_const_ref())
560
561 assert y1.flags.owndata
562 assert y1.flags.writeable
563 # We should get copies of the eigen data, which was modified above:
564 assert y1[1, 2] == 99
565 y1[1, 2] += 12
566 assert y1[1, 2] == 111
567 assert zc[1, 2] == 99 # Make sure we aren't referencing the original
568
569
570def test_both_ref_mutators():

Callers

nothing calls this directly

Calls 1

arrayMethod · 0.80

Tested by

no test coverage detected