Skip to content

Commit

Permalink
add back buUt norm
Browse files Browse the repository at this point in the history
  • Loading branch information
j9263178 committed Oct 21, 2023
1 parent 20ab1e0 commit 6cf1579
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
27 changes: 13 additions & 14 deletions src/linalg/Norm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,19 @@ namespace cytnx {
}

Tensor Norm(const UniTensor& uTl) {
cytnx_error_msg(uTl.uten_type() != UTenType.Dense,
"[Error][Norm] Can only use Norm on DenseUniTensor or Tensor%s", "\n");
return Norm(uTl.get_block_());
// if(uTl.uten_type() == UTenType.Dense){
// // return Norm(uTl.get_block_());
// }else{

// std::vector<Tensor> bks = uTl.get_blocks_();
// Tensor res = Norm(bks[0]);
// for(int i = 1; i < bks.size(); i++){
// res+=Norm(bks[i]);
// }
// return res;
// }
// cytnx_error_msg(uTl.uten_type() != UTenType.Dense,
// "[Error][Norm] Can only use Norm on DenseUniTensor or Tensor%s", "\n");
// return Norm(uTl.get_block_());
if (uTl.uten_type() == UTenType.Dense) {
return Norm(uTl.get_block_());
} else {
std::vector<Tensor> bks = uTl.get_blocks_();
Tensor res = Norm(bks[0]).Pow(2);
for (int i = 1; i < bks.size(); i++) {
res += Norm(bks[i]).Pow(2);
}
return res.Pow(0.5);
}
}

} // namespace linalg
Expand Down
28 changes: 14 additions & 14 deletions tests/linalg_test/linalg_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,20 @@ TEST_F(linalg_Test, DenseUt_Norm) {
EXPECT_EQ(linalg::Norm(arange3x3cd_ut).item(), ans);
}

// TEST_F(linalg_Test, BkUt_Norm) {
// cytnx_double ans=0;
// for(cytnx_uint64 i = 0; i<9;i++){
// ans+=i*i*2;
// }
// ans+=9;
// ans = sqrt(ans);
// Bond I = Bond(BD_IN, {Qs(-1), Qs(1)}, {3, 3});
// Bond J = Bond(BD_OUT, {Qs(-1), Qs(1)}, {3, 3});
// UniTensor in = UniTensor({I, J});
// in.put_block_(arange3x3cd, 0);
// in.put_block_(ones3x3cd, 1);
// EXPECT_EQ(linalg::Norm(in).item(), ans);
// }
TEST_F(linalg_Test, BkUt_Norm) {
cytnx_double ans = 0;
for (cytnx_uint64 i = 0; i < 9; i++) {
ans += i * i * 2;
}
ans += 9;
ans = sqrt(ans);
Bond I = Bond(BD_IN, {Qs(-1), Qs(1)}, {3, 3});
Bond J = Bond(BD_OUT, {Qs(-1), Qs(1)}, {3, 3});
UniTensor in = UniTensor({I, J});
in.put_block_(arange3x3cd, 0);
in.put_block_(ones3x3cd, 1);
EXPECT_EQ(linalg::Norm(in).item(), ans);
}

TEST_F(linalg_Test, Tensor_Eig) {
auto res = linalg::Eig(arange3x3cd);
Expand Down

0 comments on commit 6cf1579

Please sign in to comment.