From 56fa511fbd7ff3ec8b6458a72e8f75ce26a4ba76 Mon Sep 17 00:00:00 2001 From: kaihsin Date: Tue, 25 Jul 2023 21:57:15 -0400 Subject: [PATCH 1/2] add linalg.Conj(_) for UniTensor --- include/UniTensor.hpp | 2 +- include/linalg.hpp | 16 +++++++++++++++- pybind/linalg_py.cpp | 15 +++++++++++++-- src/linalg/Conj.cpp | 4 ++++ src/linalg/Conj_.cpp | 2 ++ test.cc | 9 ++++++++- 6 files changed, 43 insertions(+), 5 deletions(-) diff --git a/include/UniTensor.hpp b/include/UniTensor.hpp index 5b342744a..7bd7419b3 100644 --- a/include/UniTensor.hpp +++ b/include/UniTensor.hpp @@ -3305,7 +3305,7 @@ namespace cytnx { @note Compare to Conj_(), this fucntion will create a new object UniTensor. @see Conj_() */ - UniTensor Conj() { + UniTensor Conj() const { UniTensor out; out._impl = this->_impl->Conj(); return out; diff --git a/include/linalg.hpp b/include/linalg.hpp index bd27f24e7..f88712eec 100644 --- a/include/linalg.hpp +++ b/include/linalg.hpp @@ -198,7 +198,6 @@ namespace cytnx { 2. If the object is UniTensor, then the result will depend on the UniTensor's rowrank. */ - namespace linalg { // Add: @@ -839,6 +838,21 @@ namespace cytnx { */ void Pow_(UniTensor &Tin, const double &p); + /** + * @brief Elementwise conjugate of the UniTensor + * @param[in] UT The input UniTensor. + * @return [UniTensor] The UniTensor with all element being conjugated + * @see See UniTensor.Conj() for further details + */ + cytnx::UniTensor Conj(const cytnx::UniTensor &UT); + + /** + * @brief Inplace elementwise conjugate of the UniTensor + * @param[in] UT The input UniTensor. + * @see See UniTensor.Conj_() for further details + */ + void Conj_(cytnx::UniTensor &UT); + //==================================================================================== // [Tensor] // ==================================================================================== diff --git a/pybind/linalg_py.cpp b/pybind/linalg_py.cpp index ccf81234e..35840a179 100644 --- a/pybind/linalg_py.cpp +++ b/pybind/linalg_py.cpp @@ -357,8 +357,19 @@ void linalg_binding(py::module &m) { m_linalg.def("InvM_", &cytnx::linalg::InvM_, py::arg("Tio")); m_linalg.def("Inv_", &cytnx::linalg::Inv_, py::arg("Tio"), py::arg("clip")); m_linalg.def("Inv", &cytnx::linalg::Inv, py::arg("Tio"), py::arg("clip")); - m_linalg.def("Conj", &cytnx::linalg::Conj, py::arg("Tin")); - m_linalg.def("Conj_", &cytnx::linalg::Conj_, py::arg("Tio")); + + m_linalg.def( + "Conj", [](const cytnx::Tensor &Tin) { return cytnx::linalg::Conj(Tin); }, py::arg("Tin")); + + m_linalg.def( + "Conj_", [](cytnx::Tensor &Tin) { cytnx::linalg::Conj_(Tin); }, py::arg("Tin")); + + m_linalg.def( + "Conj", [](const cytnx::UniTensor &Tin) { return cytnx::linalg::Conj(Tin); }, py::arg("Tin")); + + m_linalg.def( + "Conj_", [](cytnx::UniTensor &Tin) { cytnx::linalg::Conj_(Tin); }, py::arg("Tin")); + m_linalg.def("Matmul", &cytnx::linalg::Matmul, py::arg("T1"), py::arg("T2")); m_linalg.def("Matmul_dg", &cytnx::linalg::Matmul_dg, py::arg("T1"), py::arg("T2")); m_linalg.def("Diag", &cytnx::linalg::Diag, py::arg("Tin")); diff --git a/src/linalg/Conj.cpp b/src/linalg/Conj.cpp index 040ea5cd2..04ccc745f 100644 --- a/src/linalg/Conj.cpp +++ b/src/linalg/Conj.cpp @@ -2,8 +2,10 @@ #include "linalg_internal_interface.hpp" #include #include "Tensor.hpp" +#include "UniTensor.hpp" namespace cytnx { + namespace linalg { Tensor Conj(const Tensor &Tin) { // cytnx_error_msg(Tin.shape().size() != 2,"[Inv] error, Inv can only operate on rank-2 @@ -38,5 +40,7 @@ namespace cytnx { } } + UniTensor Conj(const UniTensor &Tin) { return Tin.Conj(); } + } // namespace linalg } // namespace cytnx diff --git a/src/linalg/Conj_.cpp b/src/linalg/Conj_.cpp index dd6273954..a86c947d9 100644 --- a/src/linalg/Conj_.cpp +++ b/src/linalg/Conj_.cpp @@ -33,5 +33,7 @@ namespace cytnx { } } + void Conj(UniTensor &UT) { UT.Conj_(); } + } // namespace linalg } // namespace cytnx diff --git a/test.cc b/test.cc index 36b9ad5f3..b5e69bc85 100644 --- a/test.cc +++ b/test.cc @@ -27,8 +27,15 @@ pair operator>>(const std::string &a, const cytnx_int6 return make_pair(a, qnidx); } +// pair operator>>(const char* a[], const cytnx_int64 &qnidx) { +// return make_pair(std::string(a), qnidx); +// } + int main(int argc, char *argv[]) { - auto ttss = lbl("a") >> 4; + auto ttss = "a"s >> 4; + // auto ttss2 = "a" >> 4 + + // auto ttss = lbl("a") >> 4; return 0; int size = 5; From 321ea6aff7e80e9528ebddf1a9cfe50cc43902fa Mon Sep 17 00:00:00 2001 From: kaihsin Date: Tue, 25 Jul 2023 22:02:19 -0400 Subject: [PATCH 2/2] fix missing _ --- src/linalg/Conj_.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linalg/Conj_.cpp b/src/linalg/Conj_.cpp index a86c947d9..30ea4bd87 100644 --- a/src/linalg/Conj_.cpp +++ b/src/linalg/Conj_.cpp @@ -33,7 +33,7 @@ namespace cytnx { } } - void Conj(UniTensor &UT) { UT.Conj_(); } + void Conj_(UniTensor &UT) { UT.Conj_(); } } // namespace linalg } // namespace cytnx