diff --git a/include/UniTensor.hpp b/include/UniTensor.hpp index 0ece48653..fcee9f48f 100644 --- a/include/UniTensor.hpp +++ b/include/UniTensor.hpp @@ -606,7 +606,14 @@ namespace cytnx { void print_diagram(const bool &bond_info = false) const; void print_blocks(const bool &full_info = true) const; void print_block(const cytnx_int64 &idx, const bool &full_info = true) const; - Tensor get_block(const cytnx_uint64 &idx = 0) const { return this->_block.clone(); } + Tensor get_block() const { return this->_block.clone(); } + Tensor get_block(const cytnx_uint64 &idx) const { + cytnx_error_msg(idx != 0, + "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu " + "invalid. Use get_block(0).\n", + (unsigned long long)idx); + return this->_block.clone(); + } Tensor get_block(const std::vector &qnum, const bool &force) const { cytnx_error_msg( @@ -631,9 +638,23 @@ namespace cytnx { } // return a share view of block, this only work for non-symm tensor. - Tensor &get_block_(const cytnx_uint64 &idx = 0) { return this->_block; } + Tensor &get_block_() { return this->_block; } + Tensor &get_block_(const cytnx_uint64 &idx) { + cytnx_error_msg(idx != 0, + "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu " + "invalid. Use get_block_(0).\n", + (unsigned long long)idx); + return this->_block; + } // return a share view of block, this only work for non-symm tensor. - const Tensor &get_block_(const cytnx_uint64 &idx = 0) const { return this->_block; } + const Tensor &get_block_() const { return this->_block; } + const Tensor &get_block_(const cytnx_uint64 &idx) const { + cytnx_error_msg(idx != 0, + "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu " + "invalid. Use get_block_(0).\n", + (unsigned long long)idx); + return this->_block; + } cytnx_uint64 Nblocks() const { return 1; }; std::vector get_blocks() const { @@ -655,8 +676,8 @@ namespace cytnx { return this->_interface_block; // this will not share memory!! } - void put_block(const Tensor &in, const cytnx_uint64 &idx = 0) { - // We don't check the dtype for DenseUniTensor, since it'll be more convinent to change + void put_block(const Tensor &in) { + // We don't check the dtype for DenseUniTensor, since it'll be more convenient to change // DenseUniTensor's dtype // cytnx_error_msg(in.dtype() != this->dtype(), @@ -681,9 +702,16 @@ namespace cytnx { this->_block = in.clone(); } } + void put_block(const Tensor &in, const cytnx_uint64 &idx) { + cytnx_error_msg(idx != 0, + "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu " + "invalid. Use put_block(0).\n", + (unsigned long long)idx); + put_block(in); + } // share view of the block - void put_block_(Tensor &in, const cytnx_uint64 &idx = 0) { - // We don't check the dtype for DenseUniTensor, since it'll be more convinent to change + void put_block_(Tensor &in) { + // We don't check the dtype for DenseUniTensor, since it'll be more convenient to change // DenseUniTensor's dtype // cytnx_error_msg(in.dtype() != this->dtype(), @@ -708,6 +736,13 @@ namespace cytnx { this->_block = in; } } + void put_block_(Tensor &in, const cytnx_uint64 &idx) { + cytnx_error_msg(idx != 0, + "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu " + "invalid. Use put_block_(0).\n", + (unsigned long long)idx); + put_block_(in); + } void put_block(const Tensor &in, const std::vector &qnum, const bool &force) { cytnx_error_msg( diff --git a/tests/DenseUniTensor_test.cpp b/tests/DenseUniTensor_test.cpp index f9d7d9774..18b5bfe32 100644 --- a/tests/DenseUniTensor_test.cpp +++ b/tests/DenseUniTensor_test.cpp @@ -1266,11 +1266,9 @@ TEST_F(DenseUniTensorTest, get_block) { /*=====test info===== describe:test get_block out of range ====================*/ -#if FAIL_CASE_OPEN TEST_F(DenseUniTensorTest, get_block_out_of_range) { EXPECT_THROW(utzero345.get_block(3), std::logic_error); } -#endif /*=====test info===== describe:test get_block, diagonal @@ -1386,11 +1384,9 @@ TEST_F(DenseUniTensorTest, get_block__uninit) { /*=====test info===== describe:test get_block out of range ====================*/ -#if FAIL_CASE_OPEN TEST_F(DenseUniTensorTest, get_block__out_of_range) { EXPECT_THROW(utzero345.get_block_(3), std::logic_error); } -#endif TEST_F(DenseUniTensorTest, get_blocks) { EXPECT_THROW(utzero345.get_blocks(), std::logic_error); } @@ -1496,7 +1492,6 @@ TEST_F(DenseUniTensorTest, put_block_rank_mismatch) { /*=====test info===== describe:test put_block_, out of index ====================*/ -#if FAIL_CASE_OPEN TEST_F(DenseUniTensorTest, put_block_out_of_idx) { constexpr cytnx_uint64 dim1 = 2, dim2 = 3; auto tens = zeros({dim1, dim2}); @@ -1507,7 +1502,6 @@ TEST_F(DenseUniTensorTest, put_block_out_of_idx) { auto ut = UniTensor({Bond(dim1), Bond(dim2)}); EXPECT_THROW(ut.put_block(tens, 1), std::logic_error); } -#endif /*=====test info===== describe:test put_block_ @@ -1592,7 +1586,6 @@ TEST_F(DenseUniTensorTest, put_block__rank_mismatch) { /*=====test info===== describe:test put_block_, out of index ====================*/ -#if FAIL_CASE_OPEN TEST_F(DenseUniTensorTest, put_block__out_of_idx) { constexpr cytnx_uint64 dim1 = 2, dim2 = 3; auto tens = zeros({dim1, dim2}); @@ -1603,7 +1596,6 @@ TEST_F(DenseUniTensorTest, put_block__out_of_idx) { auto ut = UniTensor({Bond(dim1), Bond(dim2)}); EXPECT_THROW(ut.put_block_(tens, 1), std::logic_error); } -#endif /*=====test info===== describe:test put_blocks, input uninitialized UniTensor