% hierarchical Tucker with 4 indices, full precision
for i1=0:1;for i2=0:1;for i3=0:1;for i4=0:1;
                A(i1+1,i2+1,i3+1,i4+1)=i1+2*i2+4*i3+8*i4;
            end;end;end;end
T=tensor(A);
a12=tenmat(T,[1,2]);
a34=tenmat(T,[3,4]);
a1=tenmat(T,[1]); a2=tenmat(T,[2]);
a3=tenmat(T,[3]); a4=tenmat(T,[4]);
a1234=tenmat(T,[ ]);  
[u1,s1,v1]=svd(double(a1));[u2,s2,v2]=svd(double(a2));
[u3,s3,v3]=svd(double(a3));[u4,s4,v4]=svd(double(a4));
[u12,s12,v12]=svd(double(a12));[u34,s34,v34]=svd(double(a34));
b12=kron(u2,u1)'*u12;
b34=kron(u4,u3)'*u34;
b1234=kron(u34,u12)'*a1234';
va=kron(kron(u4,u3),kron(u2,u1))*kron(b34,b12)*b1234;
norm(va-a1234')

