SVD for Mat3

This commit is contained in:
2023-11-18 18:48:20 +01:00
parent a4c422f32a
commit 731b2a128d
3 changed files with 181 additions and 9 deletions

27
t.cc
View File

@@ -2786,6 +2786,12 @@ cout <<"resorted graph = "<<adjperm<<endl;
if(1)
{
int seed;
int f=open("/dev/random",O_RDONLY);
if(sizeof(int)!=read(f,&seed,sizeof(int))) laerror("cannot read /dev/random");
close(f);
srand(seed);
Mat3<double> a;
a.randomize(10.);
cout<<a<<endl;
@@ -2794,22 +2800,37 @@ Mat3<double> work(ata);
Vec3<double> w;
Mat3<double> v;
work.eivec_sym(w,v,true);
cout <<w<<endl<<sqrt(w[0])<<" "<<sqrt(w[1])<<" "<<sqrt(w[2])<<" "<<endl<<endl<<v<<endl;
//cout <<w<<endl<<sqrt(w[0])<<" "<<sqrt(w[1])<<" "<<sqrt(w[2])<<" "<<endl<<endl<<v<<endl;
Mat3<double> aat = a*a.transpose();
Mat3<double> work2(aat);
Vec3<double> w2;
Mat3<double> v2;
work2.eivec_sym(w2,v2,true);
cout <<w2<<endl<<sqrt(w2[0])<<" "<<sqrt(w2[1])<<" "<<sqrt(w2[2])<<" "<<endl<<endl<<v2<<endl;
//cout <<w2<<endl<<sqrt(w2[0])<<" "<<sqrt(w2[1])<<" "<<sqrt(w2[2])<<" "<<endl<<endl<<v2<<endl;
//cout <<"dets "<<v2.determinant()<<" "<<v.determinant()<<endl;
Mat3<double> u3,v3;
Vec3<double> w3;
a.svd(u3,w3,v3);
cout <<"Mat3 SVD\n";
cout <<u3<<endl<<w3<<endl<<endl<<v3.transpose()<<endl<<endl;
cout <<"Mat3 SVD dets "<<u3.determinant()<<" "<<v3.determinant()<<endl;
NRMat<double> aa(a);
cout <<aa.nrows()<<" "<<aa.ncols()<<endl;
NRMat<double> uu(3,3),vv(3,3);
NRVec<double> ss(3);
singular_decomposition(aa,&uu,ss,&vv,0);
cout <<"REGULAR SVD\n";
cout <<uu<<ss<<vv<<endl;
cout <<"svd dets "<<determinant(uu)<<" "<<determinant(vv)<<endl;
Mat3<double> ainv= a.inverse();
Mat3<double> ainv2= a.svdinverse();
cout <<endl<<"Inverse via svd\n"<<ainv2<<endl;
cout <<"Difference of inverses = "<<(ainv-ainv2).norm()<<endl;
}
}