*** empty log message ***
This commit is contained in:
parent
50dcdd2c17
commit
74ac2998b0
4
mat.cc
4
mat.cc
@ -19,6 +19,7 @@ template class NRMat<int>;
|
|||||||
template class NRMat<short>;
|
template class NRMat<short>;
|
||||||
template class NRMat<char>;
|
template class NRMat<char>;
|
||||||
template class NRMat<unsigned char>;
|
template class NRMat<unsigned char>;
|
||||||
|
template class NRMat<unsigned int>;
|
||||||
template class NRMat<unsigned long>;
|
template class NRMat<unsigned long>;
|
||||||
|
|
||||||
|
|
||||||
@ -123,8 +124,10 @@ NRMat<T> & NRMat<T>::operator=(const T &a)
|
|||||||
if (nn != mm) laerror("RMat.operator=scalar on non-square matrix");
|
if (nn != mm) laerror("RMat.operator=scalar on non-square matrix");
|
||||||
#endif
|
#endif
|
||||||
#ifdef MATPTR
|
#ifdef MATPTR
|
||||||
|
memset(v[0],0,nn*nn*sizeof(T));
|
||||||
for (int i=0; i< nn; i++) v[i][i] = a;
|
for (int i=0; i< nn; i++) v[i][i] = a;
|
||||||
#else
|
#else
|
||||||
|
memset(v,0,nn*nn*sizeof(T));
|
||||||
for (int i=0; i< nn*nn; i+=nn+1) v[i] = a;
|
for (int i=0; i< nn*nn; i+=nn+1) v[i] = a;
|
||||||
#endif
|
#endif
|
||||||
return *this;
|
return *this;
|
||||||
@ -1067,6 +1070,7 @@ INSTANTIZE(short)
|
|||||||
INSTANTIZE(char)
|
INSTANTIZE(char)
|
||||||
INSTANTIZE(unsigned char)
|
INSTANTIZE(unsigned char)
|
||||||
INSTANTIZE(unsigned long)
|
INSTANTIZE(unsigned long)
|
||||||
|
INSTANTIZE(unsigned int)
|
||||||
|
|
||||||
|
|
||||||
export template <typename T>
|
export template <typename T>
|
||||||
|
18
matexp.h
18
matexp.h
@ -8,7 +8,7 @@
|
|||||||
#include "la_traits.h"
|
#include "la_traits.h"
|
||||||
|
|
||||||
template<class T,class R>
|
template<class T,class R>
|
||||||
const T polynom2(const T &x, const NRVec<R> &c)
|
const T polynom0(const T &x, const NRVec<R> &c)
|
||||||
{
|
{
|
||||||
int order=c.size()-1;
|
int order=c.size()-1;
|
||||||
T z,y;
|
T z,y;
|
||||||
@ -30,13 +30,14 @@ return y;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//algorithm which minimazes number of multiplications, at the cost of storage
|
||||||
template<class T,class R>
|
template<class T,class R>
|
||||||
const T polynom(const T &x, const NRVec<R> &c)
|
const T polynom(const T &x, const NRVec<R> &c)
|
||||||
{
|
{
|
||||||
int n=c.size()-1;
|
int n=c.size()-1;
|
||||||
int i,j,k,m=0,t;
|
int i,j,k,m=0,t;
|
||||||
|
|
||||||
if(n<=4) return polynom2(x,c); //here the horner scheme is optimal
|
if(n<=4) return polynom0(x,c); //here the horner scheme is optimal
|
||||||
|
|
||||||
//first find m which minimizes the number of multiplications
|
//first find m which minimizes the number of multiplications
|
||||||
j=10*n;
|
j=10*n;
|
||||||
@ -50,6 +51,7 @@ for(i=2;i<=n+1;i++)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//allocate array for powers up to m
|
//allocate array for powers up to m
|
||||||
T *xpows = new T[m];
|
T *xpows = new T[m];
|
||||||
xpows[0]=x;
|
xpows[0]=x;
|
||||||
@ -65,7 +67,10 @@ for(i=0; i<=n/m;i++)
|
|||||||
{
|
{
|
||||||
k++;
|
k++;
|
||||||
if(k>n) break;
|
if(k>n) break;
|
||||||
if(j==0) {if(i==0) s=x; /*just to get the dimensions of the matrix*/ s=c[k]; /*create diagonal matrix*/}
|
if(j==0) {
|
||||||
|
if(i==0) s=x; /*just to get the dimensions of the matrix*/
|
||||||
|
s=c[k]; /*create diagonal matrix*/
|
||||||
|
}
|
||||||
else
|
else
|
||||||
LA_traits<T>::axpy(s,xpows[j-1],c[k]); //general s+=xpows[j-1]*c[k]; but more efficient for matrices
|
LA_traits<T>::axpy(s,xpows[j-1],c[k]); //general s+=xpows[j-1]*c[k]; but more efficient for matrices
|
||||||
}
|
}
|
||||||
@ -196,6 +201,7 @@ do {
|
|||||||
while(t*exptaylor[n]>precision);//taylor 0 will terminate in any case
|
while(t*exptaylor[n]>precision);//taylor 0 will terminate in any case
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int i; //adjust the coefficients in order to avoid scaling the argument
|
int i; //adjust the coefficients in order to avoid scaling the argument
|
||||||
NRVec<typename LA_traits<T>::elementtype> taylor2(n+1);
|
NRVec<typename LA_traits<T>::elementtype> taylor2(n+1);
|
||||||
for(i=0,t=1.;i<=n;i++)
|
for(i=0,t=1.;i<=n;i++)
|
||||||
@ -209,14 +215,16 @@ return taylor2;
|
|||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
const T exp(const T &x)
|
const T exp(const T &x, const bool simple=false)
|
||||||
{
|
{
|
||||||
int power;
|
int power;
|
||||||
|
|
||||||
//prepare the polynom of and effectively scale T
|
//prepare the polynom of and effectively scale T
|
||||||
NRVec<typename LA_traits<T>::elementtype> taylor2=exp_aux(x,power);
|
NRVec<typename LA_traits<T>::elementtype> taylor2=exp_aux(x,power);
|
||||||
|
|
||||||
T r=polynom(x,taylor2); //for accuracy summing from the smallest terms up would be better, but this is more efficient for matrices
|
|
||||||
|
T r= simple?polynom0(x,taylor2):polynom(x,taylor2);
|
||||||
|
//for accuracy summing from the smallest terms up would be better, but this is more efficient for matrices
|
||||||
|
|
||||||
//power the result back
|
//power the result back
|
||||||
for(int i=0; i<power; i++) r=r*r;
|
for(int i=0; i<power; i++) r=r*r;
|
||||||
|
3
smat.cc
3
smat.cc
@ -19,6 +19,7 @@ template class NRSMat< complex<double> >;
|
|||||||
template class NRSMat<int>;
|
template class NRSMat<int>;
|
||||||
template class NRSMat<short>;
|
template class NRSMat<short>;
|
||||||
template class NRSMat<char>;
|
template class NRSMat<char>;
|
||||||
|
template class NRSMat<unsigned int>;
|
||||||
template class NRSMat<unsigned long>;
|
template class NRSMat<unsigned long>;
|
||||||
|
|
||||||
|
|
||||||
@ -80,6 +81,7 @@ template <typename T>
|
|||||||
NRSMat<T> & NRSMat<T>::operator=(const T &a)
|
NRSMat<T> & NRSMat<T>::operator=(const T &a)
|
||||||
{
|
{
|
||||||
copyonwrite();
|
copyonwrite();
|
||||||
|
memset(v,0,NN2*sizeof(T));
|
||||||
for (int i=0; i<nn; i++) v[i*(i+1)/2+i] = a;
|
for (int i=0; i<nn; i++) v[i*(i+1)/2+i] = a;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -406,5 +408,6 @@ INSTANTIZE(complex<double>)
|
|||||||
INSTANTIZE(int)
|
INSTANTIZE(int)
|
||||||
INSTANTIZE(short)
|
INSTANTIZE(short)
|
||||||
INSTANTIZE(char)
|
INSTANTIZE(char)
|
||||||
|
INSTANTIZE(unsigned int)
|
||||||
INSTANTIZE(unsigned long)
|
INSTANTIZE(unsigned long)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user