/* LA: linear algebra C++ interface library Copyright (C) 2008 Jiri Pittner or This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // g++ -D _GLIBCPP_NO_TEMPLATE_EXPORT -g testblas.cc testblas2.cc nrutil_modif.cc -L/usr/local/lib/atlas -lstrassen -lf77blas -lcblas -latlas -ltraceback -lbfd -liberty #include #include "la.h" #include "vecmat3.h" #include "quaternion.h" #include "permutation.h" using namespace std; using namespace LA_Vecmat3; using namespace LA_Quaternion; using namespace LA; extern void test(const NRVec &x); double ad; void f1(const double *c) { ad=*c; } void f2(double *c) { *c=ad; } inline int randind(const int n) { return int(random()/(1.+RAND_MAX)*n); } complex mycident (const complex&x) {return x;} void printme(const NRPerm &p) { PERM_RANK_TYPE rank=p.rank(); int n=p.size(); cout< qq(n,rank); if(qq!=p) laerror("error in rank algorithm"); for(int i=0; i<4; ++i) { NRVec_from1 inv=p.inversions(i); NRPerm q(i,inv); if(q!=p) laerror("error in inversions algorithm"); } } static int unitary_n; static PERM_RANK_TYPE space_dim; void yyprintme(const NRPerm&p, const int parity, const PERM_RANK_TYPE nterms) { cout<< parity<<"/"<&y) { cout < &p) { CompressedPartition pc(p); cout<<'['< y(p); PERM_RANK_TYPE dim=y.generate_all_standard(yprintme); Partition q=p.adjoint(); PERM_RANK_TYPE snd=p.Sn_irrep_dim(); cout<<"IR dim "< u; u=x+y; cout <<"u= "< aa(0.,3,3); aa[0][0]=aa[1][1]=aa(2,2)=2.; NRMat bb(aa); double *p; aa.copyonwrite(); p= &aa[2][2]; *p=3.; bb.copyonwrite(); bb(0,2)=1.; cout << "aa= " < cc=aa & bb; cout << "aa o+ bb= " << cc <<"\n"; cout << cc.rsum() <<"\n"; cout << cc.csum() <<"\n"; NRVecw(3); w[0]=1; w[1]=2;w[2]=3; NRVec v(0.,3); v.gemv(0.,bb,'n',1.,w); cout << " v= " < bb(1.,n,n); for(int i=0;i amat,bmat,cmat; cin >>amat; cin >>bmat; cmat=amat*bmat; cout< amat(1.,2,2); NRMat bmat(amat); NRMat dmat(amat); //NRMat cmat; cmat=bmat*2.; NRMat cmat(bmat*2); //more efficient dmat.copyonwrite(); dmat[0][0]=0; cout< amat; NRVec avec; cin >>amat; cin >>avec; cout << amat*avec; cout << avec*amat; NRVec avec(0.,10); f1(avec); f2(avec); NRVec uu(3); uu[0]=1; uu[1]=2; uu[2]=3; cout << uu << (uu|uu) <<"\n"; NRSMat sa(0.,3); sa(0,0)=1; sa(0,2)=5; sa(2,2)=10;sa(1,0)=2;sa(1,1)=3; sa(2,1)=-1; NRSMat sb(0.,3); sb(0,0)=-2; sb(0,2)=1; sb(2,2)=2;sb(1,0)=-1;sb(1,1)=7; sb(2,1)=3; cout << "symetr\n" < m10(10.,3,3); cout << "10 + sa" << m10 + sa <<"\n"; */ /* const int dim=256; NRMat big1(dim,dim),big2(dim,dim),big3; for(int i=0;i atest, btest,ctest; { int cc,c1,c2,c3; cin >>cc>>c1>>c2>>c3; atest.s_cutoff(cc,c1,c2,c3); } cin>>atest; cin>>btest; NRMat dtest(atest.nrows(),btest.ncols()); dtest.gemm(0., atest, 't', btest, 'n', 1.); cout << dtest; NRMat etest(atest.nrows(),btest.ncols()); etest.strassen(0., atest, 't', btest, 'n', 1.); cout << etest; } if(0) { int dim; cin >>dim; NRMat big1(dim,dim),big2(dim,dim),big3,big4(dim,dim); for(int i=0;i a(3,3),b; NRVec v(3); for(int i=0;i<3;i++) for(int j=0;j<3;j++) { a[i][j]= i*i+j; v[i]=10-i;} b=a; b*= sin(1.)+1; cout << a < a(3,3),b; NRVec v(10); v[0]=2;v[1]=3;v[2]=1;v[3]=-3;v[4]=2;v[5]=-1;v[6]=3;v[7]=-2;v[8]=1;v[9]=1; for(int i=0;i<3;i++) for(int j=0;j<3;j++) { a[i][j]= (i+j)/10.; } cout < a(3,3); for(int i=0;i<3;i++) for(int j=0;j<3;j++) { a[i][j]= (i+j)/10.; } NRSMat b(a); NRMat c(b); cout < a(3,3); a[0][0]=1; a[0][1]=2;a[0][2]=3; a[1][0]=4; a[1][1]=-5;a[1][2]=7; a[2][0]=-3;a[2][1]=10;a[2][2]=2; NRMat b(2,3); b[0][0]=1;b[0][1]=2;b[0][2]=3; b[1][0]=2;b[1][1]=4;b[1][2]=6; cout < a(3,3); for(int i=0;i<3;i++) for(int j=0;j<3;j++) { a[i][j]= (i+j)/10.; } NRVec b(3); cout < a(3); NRMatv(3,3); for(int i=0;i<3;i++) for(int j=0;j<3;j++) { a(i,j)= (i+j)/10.; } NRVec b(3); cout <c=(NRMat)a; //nebo NRMatc(a); NRMatd=exp(c); diagonalize(a,b,&v); cout < a; cin >>a ; NRMat abak=a; NRMat u(a.nrows(),a.nrows()),v(a.ncols(),a.ncols()); NRVecs(a.ncols() sdiag(0., u.ncols(),v.nrows()); sdiag.diagonalset(s); cout < > a; cin >>a ; NRMat > abak=a; NRMat > u(a.nrows(),a.nrows()),v(a.ncols(),a.ncols()); NRVecs(a.ncols() > sdiag(0., u.ncols(),v.nrows()); NRVec > ss = s; sdiag.diagonalset(ss); cout < a(aa,3,3); NRMat a; cin >>a; cout < u(n,n),v(n,n); NRVecwr(n),wi(n); gdiagonalize(a,wr,wi,&u,&v,0); cout <z=diagofproduct(u,v,1); for(int i=0;i a; cin >>a; cout < > u(n,n),v(n,n); NRVec >w(n); gdiagonalize(a,w,&u,&v); cout < >z=diagofproduct(u,v,1,1); //NRMat > zz=u*v.transpose(1); cout < a; cin >>a; int n=a.nrows(); NRMat > u(n,n),v(n,n); NRVec >w(n); gdiagonalize(a,w,&u,&v,0,n,0,1); cout < >z=diagofproduct(u,v,1,1); cout < > a; cin >>a; int n=a.nrows(); NRMat > u(n,n),v(n,n); NRVec >w(n); gdiagonalize(a,w,&u,&v); //gdiagonalize(a,w,&u,&v,0,n,0,1); cout < >z=diagofproduct(u,v,1,1); cout < a(4,4); NRVec v(4); v[0]=1;v[1]=2;v[2]=3;v[3]=4; a=1.; a.copyonwrite(); a.add(3,0,.5); a.add(0,2,.2); a.add(2,1,.1); a.add(3,3,1.); a.add(1,1,-1.); SparseMat c(a); c*=10.; cout <b(c); cout < a(4,4),b(4,4); a=1.; a.copyonwrite(); a.add(3,0,.5); b.add(0,2,.2); b.add(2,1,.1); b.add(3,3,1.); b.add(1,1,-1.); SparseMatc=a+b; cout < a(4,4),b(4,4); a=0.; b=2; a.add(3,0,.5); a.add(0,2,.2); a.add(1,1,1); a.add(1,0,.2); b.add(2,1,.1); b.add(3,3,1.); b.add(1,1,-1.); NRMat aa(a),bb(b); SparseMatc; NRMatcc; //cout << NRMat(c); //cout <(c); cout <<"norms2 "< aa(n,n); cout << "\n\n\ntiming for size "< a(0.,n,n); for(int i=0; i b(exp(a)); //cout <(a); } else { for(int i=0; i bb(exp(aa)); //cout <>n; SparseMat aa(n,n); for(int i=0; i bb=exp(aa); NRVec v(n); for(int i=0; i res1=bb*v; NRVec res2=exptimes(aa,v); cout <<"difference = "<<(res1-res2).norm()<>n>>k>>m; { NRMat a(n,k),b(k,m),c(n,m),d(n,m); for(int i=0;i aa(a); d.gemm(0., aa, 'n', b, 'n', .6); cout< v(10.,10); v+= 5.; cout <>n; NRMat a(n,n); for(int i=0;i b; b|=a; NRVec er(n),ei(n); NRMat vr(n,n),vl(n,n); gdiagonalize(b,er,ei,&vl,&vr,1,0,1,1); cout < u=exp(a*.1); gdiagonalize(u,er,ei,&vl,&vr,1,0,1,1); cout <>k; int n=2*k; NRMat a(n,n); //matrix with known spectrum for(int i=0;i er(n),ei(n); NRMat vr(n,n),vl(n,n); cout <<"input matrix\n"<>n; NRMat a(n,n); for(int i=0;i b=exp0(a); cout <<"b=exp(a) matrix\n"< c=log(b); //matrixfunction(a,&mycident,1); cout <<"c=log(exp(a))\n"< e=exp(c); cout <<"e=exp(c)\n"< b=exp0(a); cout <<"exp0 took "< c=exp(a,true); cout <<" horner exp took "<1e-10); } if(0) { int n=3; NRMat a(n,n); a(0,0)=1.; a(0,1)=2.; a(1,0)=2.; a(1,1)=6.; a(2,2)=-4; a(0,2)=1; cout < c=inverse(a,&d); cout < a(3,3); NRMat b=a; for(int i=1; i<4;i++) b=b*b; } if(0) { NRMat aa,bb,cc; cin >>aa; cc=copytest(aa); cout < > a,b,c; a=complexify(aa); c=copytest(a); cout < > a,b,c; cin>>a; c=copytest(a); cout < a,b,c; cin >>a; c=copytest(a); cout < a; NRMat b=exp(a); NRMat c=log(b); cout < a; cin >>a; NRMat c=log(a); //matrixfunction(a,&mycident,1); cout < b=exp(c); cout <<"exp(log(x))\n"< a; cin >>a; NRMat aa(a); NRMat b=exp(aa); NRMat c=matrixfunction(a,&exp); cout < h; NRMat t; cin >>h; cin >>t; NRMat r1= exp(-t) * h * exp(t); NRMat r2=BCHexpansion(h,t,30); cout <>n; SparseMat a(n,n); int spars=n*n/3; for(int i=0; i aa(a); NRVec v(aa[0],n*n); cout < amat,bmat; cin >>amat; cin >>bmat; NRVec v(amat.nrows()); diagonalize(amat,v,1,1,0,&bmat,1); cout <>n >>m; NRSMat a(n,n); NRVec rr(n); for(int i=0;i aa; NRMat vv(n,n); aa=a; diagonalize(aa,rr,&vv); NRVec r(m); NRVec *eivecs = new NRVec[m]; davidson(a,r,eivecs,NULL,m,1,1e-6,1,200); cout <<"Davidson energies " <>n >>m; NRMat a(n,n); NRVec rr(n),ii(n); double tmp=0.; for(int i=0;i aa=a; NRMat vv=aa; gdiagonalize(aa, rr, ii, NULL, &vv, 1, 0, 2, 0, NULL,NULL); NRVec r(m); NRVec *eivecs = new NRVec[m]; davidson(a,r,eivecs,NULL,m,1,1e-6,1,200); cout <<"Davidson energies " <>n>>m; SparseMat aa(n,n); aa.setsymmetric(); for(int i=0; i r(m); davidson(aa,r,(NRVec *)NULL,"eivecs",m,1,1e-5,0,300,300); cout <>n>>m; SparseMat aa(n,n); aa.setsymmetric(); for(int i=0; i r(m); NRVec r2(m); davidson(aa,r,(NRVec *)NULL,"eivecs",m,1,1e-5,1,300,300); SparseMat bb(n,n); for(int i=0; i e1,e2,cc; e1=exp(bb); e2=exp(bb*-1.); aa.setunsymmetric(); cc=e1*aa*e2; davidson(cc,r2,(NRVec *)NULL,"eivecs2",m,1,1e-5,1,300,300); cout <<"original matrix" <>n>>m; SparseMat aa(n,n); for(int i=0; i r(m); davidson(aa,r,(NRVec *)NULL,"eivecs",m,1,1e-5,0,300,300); cout <>n >>m; NRMat a(n,m); NRVec b(n); NRVec x(m); for(int i=0;i bb=b; //cout <aa=a; linear_solve(aa,bb); //cout <>n >>m; SparseMat aa(n,m); NRVec b(n); NRVec x(m); //tridiagonal for(int i=0; i A(3,3); A=1.; double *p = (double *)A; *p=2.; cout < > diis(5); int dim; cin>>dim; NRVec solution(dim), deviation(dim); for(i=0; i1e-8 ; ++iter) { NRVec trial=solution; trial.copyonwrite(); for(i=0; i a,b; cin >>a; b=realsqrt(a); cout < a; NRMat b; cin >>a>>b; cout < a,b; cin >>a >>b; cout < a; cin >>a; if(a.nrows()!=a.ncols()) laerror("must be square matrix"); int n=a.nrows(); NRMatvl(n,n),vr(n,n); NRVec wr(n),wi(n); NRMat awork(a); gdiagonalize(awork,wr,wi,&vl,&vr,1,0,1,biorthonormalize); for(int i=0; i eival(0.,n,n); eival.diagonalset(wr); cout <<"test biorthonormality "<< (vl.transpose() * vr - 1.).norm()< hlp; hlp = a *vr - vr * eival; cout <<"test right eivectors "< ader(n,n); for(int i=0; i vlx(n,n),vrx(n,n); NRVec wrx(n),wix(n); gdiagonalize(awork,wrx,wix,&vlx,&vrx,1,0,1,biorthonormalize); for(int i=0; i vly(n,n),vry(n,n); NRVec wry(n),wiy(n); gdiagonalize(awork,wry,wiy,&vly,&vry,1,0,1,biorthonormalize); for(int i=0; i vld,vrd; NRVec wrd; vld = (vlx-vly) * (.5/h); vrd = (vrx-vry) * (.5/h); wrd = (wrx-wry) * (.5/h); NRMat vlg,vrg; NRVec wrg(n); //compute analytic derivative NRMat tmp(n,n); tmp.gemm(0.,vl,'t', ader * vr,'n',1.); hlp |= tmp; cout <<" C~+ VH C = \n"< Y = tmp; NRMat S = vr.transpose() * vr; cout <<"test S\n"< tmp2 = S * tmp; cout <<"test tmp2\n"< vri = inverse(vr); NRMat numX = vri * vrd; cout <<" numerical X matrix \n"<< numX; cout <<" numerical X matrix test = "<< (vr * numX - vrd).norm()< v(3); v[0]=1; v[1]=2; v[2]=3; NRVec > vv = v; NRVecu(v); vv += u; cout < scale; cin >> scale; NRMat > h; cin >>h; NRVec > x(h.nrows()); NRVec > y,z; x.randomize(1.); y=exptimes(h*scale,x,false,1.); z=exptimes(h,x,false,scale); cout <>nocc>>nvirt; int n=nocc+nvirt; NRMat t(nocc,nvirt); t.randomize(0.5); NRSMat A(n); A=1.; for(int i=0; i B(n,n); NRVec E(n); cout < Awork=A; diagonalize(Awork,E,&B); cout < U = A*B; for(int p=0; p > h(ham); h *= complex(0,1); h.simplify(); cout <<"norms of input (should be same) "< > hexp = exp(h); cout <<"sparse exp time "< >hamexp2(hexp); cout <<"norm of results "< >hamexp3 = complexmatrix(c,s); cout <<"sincos error = "<<(hamexp1-hamexp3).norm()< hreal(hamreal); SparseMat si,co; t=clock()/((double) (CLOCKS_PER_SEC)); sincos(si,co,hreal); cout <<"sparse sincos time "< >hamexp4 = complexmatrix(NRMat(co),NRMat(si)); cout <<"sincos error 2 = "<<(hamexp1-hamexp4).norm()< > rhs(ham.ncols()); rhs.randomize(1.); t=clock()/((double) (CLOCKS_PER_SEC)); NRVec > r1 = exptimes(ham*complex(0,1),rhs); cout <<"dense exptimes "< > r2 = exptimes(h,rhs); cout <<"sparse exptimes "< > r3 = cov + siv*complex(0,1); cout <<"sincostimes error = "<<(r1-r3).norm()< > siv2,cov2; t=clock()/((double) (CLOCKS_PER_SEC)); sincostimes(hreal,siv2,cov2,rhs); cout <<"sparse sincostimes "< > r4 = cov2 + siv2*complex(0,1); cout <<"sincostimes error 2 = "<<(r1-r4).norm()< > h; cin >> h; h *= complex(0,1); h.simplify(); cout <<"length of hamiltonian "< > hexp = exp(h); cout <<"sparse exp time "< > he(hexp); NRMat > hec(he); NRMat > het(he); hec.conjugateme(); het.transposeme(); //cout <>n; NRSMat hd(n); hd.randomize(1); SparseSMat h(hd); NRMat rd = hd*hd; SparseSMat r = h*h; NRSMatrx(r); NRMat r2(rx); cout <<"Error = "<<(r2-rd).norm()< h0; cin>>h0; cout <<"matrix read\n"; cout.flush(); SparseSMat h1 = h0; //.submatrix(0,2047,0,2047); SparseSMat > h = imagmatrix(h1); double t=clock()/((double) (CLOCKS_PER_SEC)); SparseSMat > r = h*h; cout <<"SparseSMat mult time "< > h3(h); NRMat > h2(h3); NRMat >r2 = exp(h2); cout <<"error = "<<(r2-NRMat >(NRSMat >(r))).norm()< > b(bb); //cholesky(b,0); //if(n<100)cout <<"Dense Cholesky result\n"< > c; c= cc.cholesky(); NRMat > cx(c); if(n<100)cout <<"Sparse pivoted Cholesky result \n"< bb(n,n); bb.gemm(0.,bh,'c',bh,'n',1.); if(n<1000) cout <<"Input matrix\n"< br(n,n); br.gemm(0.,b,'c',b,'n',1.); if(n<1000) cout <<"Result of reconstruction\n"<cgpu; a.moveto(gpu1); b.moveto(gpu1); t=clock()/((double) (CLOCKS_PER_SEC)); for(int i=0; i h1 = h0; CSRMat > h = imagmatrix(h1); double t=clock()/((double) (CLOCKS_PER_SEC)); CSRMat > r = h*h; cout <<"CSRMat mult time "< > h2(h); NRMat >r2 = exp(h2); cout <<"error = "<<(r2-NRMat >(r)).norm()< > m; ifstream f("libormat"); f >> m; f.close(); NRVec eivals(m.nrows()); NRMat > m_aux = m; NRMat > m_test = m; cout << "hermiticity error = " <<(m_aux.transpose(true) - m_aux).norm()< > eivalsc(eivals); NRMat > m4 = m.transpose(true); cout <<"unitarity error "<< (m4*m).norm(true)< > m5(m.nrows(),m.nrows()); for(int i=0; i(m(j,i).real(), - m(j,i).imag()); cout << "conjugatetest "<<(m4-m5).norm()< > m1= m_aux * m; NRMat > m1x = m; m1x.diagmultr(eivalsc); cout << "test m1 m1x "<<(m1-m1x).norm()< > m2= m.transpose(true) * m1; //NRMat > m2b= m * m_aux * m.transpose(true); cout <<"check "< > m3= vl.transpose(true)* m_aux *vr; cout <<"check2 "<tt = t.inverse(); cout < rotmat; quat2rotmat(r,rotmat); cout << rotmat[0][1]< rotmat2(3,3),rotmat3(3,3); Quaternion > rotmatder; rotmatder[0].resize(3,3); rotmatder[1].resize(3,3); rotmatder[2].resize(3,3); rotmatder[3].resize(3,3); normquat2rotmatder(r,rotmatder); cout << rotmatder; normquat2rotmat(r,rotmat2); normquat2rotmat(-r,rotmat3); cout << rotmat2< rr; rotmat2normquat(rotmat2,rr); cout <<"orig "< eul; r.normalize(NULL,true); r.normquat2eulerzyx(eul); cout<< "euler "< rback; rback.eulerzyx2normquat(eul); cout <<"q from euler back "< rm; euler2rotmat((double *)eul,rm,"xyz",false,false,false); cout < eulback; rotmat2euler((double *)eulback,rm,"xyz",false,false,false); cout <<"euler back from rm error "< axis={0,1/sqrt(2),1/sqrt(2)}; Quaternion rrr; rrr.axis2normquat(axis,0.5); NRVec axis2(3); double angle; rrr.normquat2axis(&axis2[0],angle); cout <<"back angle "< vec={1,2,3}; Quaternion qvec(vec); Quaternion rvec = qvec.rotateby(rrr); Quaternion rvec2 = qvec.rotateby(rrr.conjugate()); cout < rrvec = rvec.rotateby(rrr.conjugate()); cout < rotvec; rrr.rotate(rotvec,vec); Quaternion qq={1.5,2,-2,-1.123}; cout << " test "< qq2={-.5,1,2,3.23}; Mat3 m(1,2,3,4,5,6,7,8,-9); NRMat mm(m,3,3); Vec3 v(10,20,30); NRVec vv(v,3); cout << m*v< m2 = m.inverse(); cout< mm2(3,3); mm2=1.; multiply_by_inverse(mm2,mm); cout < xeul,yeul; qq.normquat2euler((double *)xeul,"zyx"); qq.normquat2eulerzyx((double *)yeul); cout < xqq; xqq.euler2normquat((double *)xeul,"zyx"); cout <<"normquat2euler test "< a,b,c; cin >>a>>b; c=a+b; cout< p; cin >>p; int n=p.size(); NRVec_from1 v(n); int i; for(i=1; i<=n; ++i) v[i]=10.*i; cout < c; cin>>c; cout< p(c); cout < cc(p); cout <>n; NRPerm p(n); p.randomize(); cout < cc(p); cout < pp(cc,n); cout < v(n); for(int i=0; i vv(v); v.permuteme(cc); cout < vvv= vv.permuted(pp); cout<>n; NRVec v(n); v.randomize(1.); NRVec vv(v); NRPerm p(n); vv.sort(0,p); NRVec vvv=v.permuted(p,true); NRVec v4=vv.permuted(p,false); cout<>n; NRPerm p(n); p.identity(); do{ cout <>n; NRPerm p(n); int tot=p.generate_all_lex(printme); cout <<"generated "<>n >>unitary_n; Partition p(n); space_dim=0; int tot=p.generate_all(pprintme,0); cout <<"partitions generated "<>n ; Sn_characters Sn(n); cout <>n >>m >>x; if(n p(n),q(m); p.randomize(1.); q.randomize(1.); NRVec qq(q); Polynomial qqq(qq); Polynomial pp(n); pp.randomize(1.); p*=10.; Polynomial a,b; p.polydiv(q,a,b); Polynomial r=p*q; Polynomial z=value(p,q); //p(q(x)) Polynomial y=value(q,p); cout < u(5,5); u.randomize(1.); cout << (value(p,u)*value(q,u) -value(r,u)).norm()<>n ; NRVec r(n); r.randomize(1.); double x0=r[0]*0.8+r[1]*0.2; r.sort(0); Polynomial p=polyfromroots(r); cout <>n ; NRVec x(n+1),y(n+1); x.randomize(1.); y.randomize(1.); Polynomial p=lagrange_interpolation(x,y); cout < yy=values(p,x); cout<<"interpolation error= "<<(y-yy).norm()<q=p.integral(2); Polynomialpp=q.derivative(2); cout<<"test deriv. "<<(pp-p).norm()<