Skip to content

Commit 1990e69

Browse files
committed
demo.cpp: added CP example
1 parent b227917 commit 1990e69

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

examples/demo/demo.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <random>
2525

2626
#include <TiledArray/expressions/einsum.h>
27+
#include <TiledArray/math/solvers/cp.h>
2728

2829
auto make_tile(const TA::Range &range) {
2930
// Construct a tile
@@ -152,7 +153,23 @@ int main(int argc, char *argv[]) {
152153
}
153154

154155
world.gop.fence();
155-
std::cout << array << std::endl;
156+
}
157+
158+
// CP decomposition
159+
{
160+
TSpArrayD a65;
161+
a65 = einsum(a6("i,r"), a5("j,r"), "i,j,r");
162+
TSpArrayD a765;
163+
a765 = einsum(a7("i,r"), a65("j,k,r"), "i,j,k");
164+
165+
using cp_solver_t = CP_ALS<TA::TensorD, TA::SparsePolicy>;
166+
cp_solver_t cpd(a765);
167+
cpd.compute_rank(70, 10, /* build_rank */ false, 1e-3);
168+
auto a765_cp = cpd.reconstruct();
169+
TA::axpy(a765, -1, a765_cp); // now a765 contains a765-a765_cp
170+
// std::cout << "a765 - a765_CP:" << a765 << std::endl;
171+
std::cout << "CP error = " << TA::norm2(a765) << std::endl;
172+
world.gop.fence();
156173
}
157174

158175
return 0;

0 commit comments

Comments
 (0)