diff --git a/cytnx/UniTensor_conti.py b/cytnx/UniTensor_conti.py index f8d8bdff..83e1f3c0 100644 --- a/cytnx/UniTensor_conti.py +++ b/cytnx/UniTensor_conti.py @@ -242,6 +242,6 @@ def at(self, labels:List[str], locator:List[int]): @add_method(UniTensor) -def convert_from(self, Tin, force=False): - self.cfrom(Tin,force); +def convert_from(self, Tin, force=False, tol = 1e-14): + self.cfrom(Tin,force,tol); return self diff --git a/include/UniTensor.hpp b/include/UniTensor.hpp index a8475a0e..6520545b 100644 --- a/include/UniTensor.hpp +++ b/include/UniTensor.hpp @@ -405,6 +405,8 @@ namespace cytnx { virtual const cytnx_int16 &at_for_sparse(const std::vector &locator, const cytnx_int16 &aux) const; + virtual void from_(const boost::intrusive_ptr &rhs, const bool &force, + const cytnx_double &tol); virtual void from_(const boost::intrusive_ptr &rhs, const bool &force); virtual void group_basis_(); @@ -1700,7 +1702,8 @@ namespace cytnx { "This operation will destroy block structure. [Suggest] using get/set_block(s) to do " "operation on the block(s)."); } - void from_(const boost::intrusive_ptr &rhs, const bool &force); + void from_(const boost::intrusive_ptr &rhs, const bool &force, + const cytnx_double &tol); void group_basis_(); @@ -4078,8 +4081,9 @@ namespace cytnx { void _Save(std::fstream &f) const; /// @endcond - UniTensor &convert_from(const UniTensor &rhs, const bool &force = false) { - this->_impl->from_(rhs._impl, force); + UniTensor &convert_from(const UniTensor &rhs, const bool &force = false, + const cytnx_double &tol = 1e-14) { + this->_impl->from_(rhs._impl, force, tol); return *this; } diff --git a/pybind/unitensor_py.cpp b/pybind/unitensor_py.cpp index b77d385a..6a8839f1 100644 --- a/pybind/unitensor_py.cpp +++ b/pybind/unitensor_py.cpp @@ -1365,10 +1365,10 @@ void unitensor_binding(py::module &m) { }, py::arg("low"), py::arg("high"), py::arg("seed")= -1) - .def("cfrom", [](UniTensor &self, const UniTensor &in, const bool &force){ - self.convert_from(in,force); + .def("cfrom", [](UniTensor &self, const UniTensor &in, const bool &force, const cytnx_double &tol){ + self.convert_from(in,force,tol); }, - py::arg("Tin"), py::arg("force") = false) + py::arg("Tin"), py::arg("force") = false, py::arg("tol") = 1e-14) .def("get_qindices", [](UniTensor &self, const cytnx_uint64 &bidx){return self.get_qindices(bidx);}); ; // end of object line diff --git a/src/BlockUniTensor.cpp b/src/BlockUniTensor.cpp index a61fe5b6..df65be00 100644 --- a/src/BlockUniTensor.cpp +++ b/src/BlockUniTensor.cpp @@ -1992,7 +1992,8 @@ namespace cytnx { this->combineBonds(idx_mapper, force); } - void _BK_from_DN(BlockUniTensor *ths, DenseUniTensor *rhs, const bool &force) { + void _BK_from_DN(BlockUniTensor *ths, DenseUniTensor *rhs, const bool &force, + const cytnx_double &tol) { if (!force) { // more checking: if (int(rhs->bond_(0).type()) != bondType::BD_NONE) { @@ -2020,7 +2021,7 @@ namespace cytnx { elem = rhs->_block.at(cart); } else { if (!force) - if (abs(Scalar(rhs->_block.at(cart))) > 1e-14) { + if (abs(Scalar(rhs->_block.at(cart))) > tol) { cytnx_error_msg(true, "[ERROR] force = false, trying to convert DenseUT to BlockUT that " "violate the symmetry structure.%s", @@ -2034,12 +2035,13 @@ namespace cytnx { cytnx_error_msg(true, "[ERROR] BlockUT-> BlockUT not implemented.%s", "\n"); } - void BlockUniTensor::from_(const boost::intrusive_ptr &rhs, const bool &force) { + void BlockUniTensor::from_(const boost::intrusive_ptr &rhs, const bool &force, + const cytnx_double &tol) { // checking shape: cytnx_error_msg(this->shape() != rhs->shape(), "[ERROR][from_] shape does not match.%s", "\n"); if (rhs->uten_type() == UTenType.Dense) { - _BK_from_DN(this, (DenseUniTensor *)(rhs.get()), force); + _BK_from_DN(this, (DenseUniTensor *)(rhs.get()), force, tol); } else if (rhs->uten_type() == UTenType.Block) { _BK_from_BK(this, (BlockUniTensor *)(rhs.get()), force); } else { diff --git a/src/UniTensor_base.cpp b/src/UniTensor_base.cpp index 71f79457..6f30ab6a 100644 --- a/src/UniTensor_base.cpp +++ b/src/UniTensor_base.cpp @@ -699,7 +699,8 @@ namespace cytnx { "\n"); } - void UniTensor_base::from_(const boost::intrusive_ptr &rhs, const bool &force) { + void UniTensor_base::from_(const boost::intrusive_ptr &rhs, const bool &force, + const cytnx_double &tol) { cytnx_error_msg(true, "[ERROR] fatal internal, cannot call on a un-initialize UniTensor_base%s", "\n"); }