diff --git a/Makefile.am b/Makefile.am index fc683cd..c836bb8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ lib_LTLIBRARIES = libla.la -include_HEADERS = version.h simple.h vecmat3.h quaternion.h fortran.h cuda_la.h auxstorage.h davidson.h laerror.h mat.h qsort.h vec.h bisection.h diis.h la.h noncblas.h smat.h bitvector.h fourindex.h la_traits.h nonclass.h sparsemat.h sparsesmat.h csrmat.h conjgrad.h gmres.h matexp.h permutation.h polynomial.h contfrac.h graph.h -libla_la_SOURCES = simple.cc quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc permutation.cc polynomial.cc contfrac.cc graph.cc +include_HEADERS = version.h simple.h vecmat3.h quaternion.h fortran.h cuda_la.h auxstorage.h davidson.h laerror.h mat.h qsort.h vec.h bisection.h diis.h la.h noncblas.h smat.h bitvector.h fourindex.h la_traits.h random.h nonclass.h sparsemat.h sparsesmat.h csrmat.h conjgrad.h gmres.h matexp.h permutation.h polynomial.h contfrac.h graph.h +libla_la_SOURCES = simple.cc quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc permutation.cc polynomial.cc contfrac.cc graph.cc random.cc nodist_libla_la_SOURCES = version.cc check_PROGRAMS = t test tX t_SOURCES = t.cc t2.cc diff --git a/la_traits.h b/la_traits.h index 81e4b9c..e32cfee 100644 --- a/la_traits.h +++ b/la_traits.h @@ -44,6 +44,7 @@ //using namespace std; #include "laerror.h" +#include "random.h" #include "cuda_la.h" @@ -70,6 +71,7 @@ extern "C" { namespace LA { + //forward declarations template class NRVec; template class NRVec_from1; diff --git a/mat.cc b/mat.cc index e8f4383..798f903 100644 --- a/mat.cc +++ b/mat.cc @@ -1314,7 +1314,7 @@ void NRMat::randomize(const double &x) { #endif for(register int i=0; i::randomize(const double &x) { NRMat tmp(nn, mm, cpu); double *tmp_data = tmp; for(register size_t i=0; i<(size_t)nn*mm; ++i){ - tmp_data[i] = x*(2.*random()/(1. + RAND_MAX) - 1.); + tmp_data[i] = x*RANDDOUBLESIGNED(); } tmp.moveto(this->location); *this |= tmp; @@ -1343,8 +1343,8 @@ void NRMat >::randomize(const double &x) { #endif for(register int i=0; i(re, im); } } @@ -1353,8 +1353,8 @@ void NRMat >::randomize(const double &x) { NRMat > tmp(nn, mm, cpu); std::complex *tmp_data = tmp; for(register size_t i=0; i<(size_t)nn*mm; ++i){ - const double re = x*(2.*random()/(1. + RAND_MAX) - 1.); - const double im = x*(2.*random()/(1. + RAND_MAX) - 1.); + const double re = x*RANDDOUBLESIGNED(); + const double im = x*RANDDOUBLESIGNED(); tmp_data[i] = std::complex(re, im); } tmp.moveto(this->location); diff --git a/permutation.cc b/permutation.cc index 11e7229..03600cb 100644 --- a/permutation.cc +++ b/permutation.cc @@ -167,7 +167,7 @@ this->copyonwrite(); this->identity(); for(int i=n-1; i>=1; --i) { - int j= random()%(i+1); + int j= RANDINT32()%(i+1); T tmp = (*this)[i+1]; (*this)[i+1]=(*this)[j+1]; (*this)[j+1]=tmp; diff --git a/quaternion.cc b/quaternion.cc index 26fee64..f8a4914 100644 --- a/quaternion.cc +++ b/quaternion.cc @@ -417,15 +417,15 @@ void Quaternion::random_rotation() T s1,s2,s; do { - q[0]=2.*random()/(1. + RAND_MAX) - 1.; - q[1]=2.*random()/(1. + RAND_MAX) - 1.; + q[0]= RANDDOUBLESIGNED(); + q[1]= RANDDOUBLESIGNED(); s1 = q[0]*q[0] + q[1]*q[1]; } while(s1>1); do { - q[2]=2.*random()/(1. + RAND_MAX) - 1.; - q[3]=2.*random()/(1. + RAND_MAX) - 1.; + q[2]= RANDDOUBLESIGNED(); + q[3]= RANDDOUBLESIGNED(); s2 = q[2]*q[2] + q[3]*q[3]; } while(s2>1); diff --git a/random.cc b/random.cc new file mode 100644 index 0000000..3a68c27 --- /dev/null +++ b/random.cc @@ -0,0 +1,12 @@ +#include +#include "random.h" + +namespace LA { + +extern "C" { +WEAK_SYMBOL double randdouble() {return random()/(1.+RAND_MAX);} +WEAK_SYMBOL double randdoublesigned() {return 2.*random()/(1.+RAND_MAX)-1.;} +WEAK_SYMBOL int randint32() {return random();} +} +}//namespace + diff --git a/random.h b/random.h new file mode 100644 index 0000000..b36d8ef --- /dev/null +++ b/random.h @@ -0,0 +1,33 @@ +#ifndef _RANDOM_H +#define _RANDOM_H + +namespace LA { + +//RANDOM numbers defaulting to standard library but switchable to user's functions +#ifndef RANDDOUBLE +#define RANDDOUBLE randdouble +#endif + +#ifndef RANDDOUBLESIGNED +#define RANDDOUBLESIGNED randdoublesigned +#endif + +#ifndef RANDINT32 +#define RANDINT32 randint32 +#endif + +extern "C" { +extern double randdouble(); +extern double randdoublesigned(); +extern int randint32(); +} + +#ifdef __GNUC__ +#define WEAK_SYMBOL __attribute__((weak)) +#else +#define WEAK_SYMBOL +#endif + +}//namespace + +#endif diff --git a/smat.cc b/smat.cc index 12feeae..dff16ac 100644 --- a/smat.cc +++ b/smat.cc @@ -249,7 +249,7 @@ void NRSMat::randomize(const double &x) { NOT_GPU(*this); for(size_t i=0; i::randomize(const double &x) { ******************************************************************************/ template<> void NRSMat >::randomize(const double &x) { - for(register size_t i=0; i mycident (const complex&x) {return x;} @@ -640,7 +640,7 @@ for(int n=8; n<=1024*1024;n+=n) cout << "\n\n\ntiming for size "< a(0.,n,n); - for(int i=0; i b(exp(a)); @@ -653,7 +653,7 @@ for(int n=8; n<=1024*1024;n+=n) } else { - for(int i=0; i>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); @@ -697,8 +697,8 @@ cout <<"test error = "<<(c-d).norm()< a(k,n),b(k,m),c(n,m),d(n,m); -for(int i=0;i aa(a); d.gemm(0., aa, 't', b, 'n', .7); @@ -707,8 +707,8 @@ cout <<"test error = "<<(c-d).norm()< a(n,k),b(m,k),c(n,m),d(n,m); -for(int i=0;i aa(a); d.gemm(0., aa, 'n', b, 't', .8); @@ -717,8 +717,8 @@ cout <<"test error = "<<(c-d).norm()< a(k,n),b(m,k),c(n,m),d(n,m); -for(int i=0;i aa(a); d.gemm(0., aa, 't', b, 't', .9); @@ -764,8 +764,8 @@ cin >>n; NRMat a(n,n); for(int i=0;i b; b|=a; NRVec er(n),ei(n); @@ -810,7 +810,7 @@ cin>>n; NRMat a(n,n); for(int i=0;i a(n,n); NRVecu(n),v,w; for(int i=0;i>n; NRMat a(n,n); for(int i=0;i b=exp(a); @@ -1029,7 +1029,7 @@ cin >>n; SparseMat a(n,n); for(int i=0;i aa(a); @@ -1061,11 +1061,11 @@ cin >>n; NRSMat a(n); for(int i=0;i y(1,n); -for(int i=0;i>n; SparseMat a(n,n); int spars=n*n/3; - for(int i=0; i aa(a); NRVec v(aa[0],n*n); @@ -1115,7 +1115,7 @@ NRVec rr(n); for(int i=0;i rr(n),ii(n); double tmp=0.; for(int i=0;i>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.); @@ -1236,12 +1236,12 @@ cin >>n>>m; { int k= randind(n); int l= randind(n); - double a=random()/(1.+RAND_MAX); - double b=random()/(1.+RAND_MAX)-.5; + double a=RANDDOUBLE(); + double b=RANDDOUBLE()-.5; aa.add(k,l,a); aa.add(l,k,a+b/20); } - for(int i=0; i r(m); davidson(aa,r,(NRVec *)NULL,"eivecs",m,1,1e-5,0,300,300); cout < x(m); for(int i=0;i b(n); NRVec x(m); //tridiagonal - for(int i=0; i>dim; NRVec solution(dim), deviation(dim); for(i=0; i1e-8 ; ++iter) { @@ -1400,7 +1400,7 @@ cout <<"test eigenvalues "< ader(n,n); for(int i=0; i>n; NRMat bb(0.,n,n); int nn=0; -for(int i=0; i0.995 || i==j) {++nn; bb(i,j)=bb(j,i)=(double)random()/RAND_MAX*(i==j?10.:.5/(i+j)*(random()>RAND_MAX/2?1:-1));}} +for(int i=0; i0.995 || i==j) {++nn; bb(i,j)=bb(j,i)=RANDDOUBLE()*(i==j?10.:.5/(i+j)*(RANDDOUBLE()>.5?1:-1));}} bb=bb*bb.transpose(); //cout <>n; SparseSMat bh(n,n); -for(int i=0; i<=n/400; ++i) for(int j=i; j0.995 || i==j) - {bh.add(i,j,(double)random()/RAND_MAX*(i==j?10.:(random()>RAND_MAX/2?1:-1)),false);}} +for(int i=0; i<=n/400; ++i) for(int j=i; j0.995 || i==j) + {bh.add(i,j,RANDDOUBLE()*(i==j?10.:(RANDDOUBLE()>.5?1:-1)),false);}} if(n<1000) cout <<"Random matrix\n"< bb(n,n); bb.gemm(0.,bh,'c',bh,'n',1.); @@ -2440,7 +2440,7 @@ simple_linfit fit; double funcs[3]; for(int i=0; i<100; ++i) { - double x = 10*(2.*random()/(1. + RAND_MAX) - 1.); + double x = 10*RANDDOUBLESIGNED(); funcs[0]=1; funcs[1]=x; funcs[2]=x*x; @@ -2585,7 +2585,7 @@ if(sizeof(int)!=read(f,&seed,sizeof(int))) laerror("cannot read /dev/random"); close(f); srand(seed); -double t=random(); +double t=RANDINT32(); t/=(1L<<30); t-=1; cout <<"theta = "<::randomize(const double &x){ NOT_GPU(*this); for(register int i=0; i >::randomize(const double &x) { NOT_GPU(*this); for(register int i=0; i(x*(2.*random()/(1. + RAND_MAX) - 1.), x*(2.*random()/(1. + RAND_MAX) - 1.)); + v[i] = std::complex(x*RANDDOUBLESIGNED(), x*RANDDOUBLESIGNED()); } }