- adding assignment operator to PLUFact,

- refactoring class constructors from kalman loop and
- tidying up multInvRight in GeneralMatrix


git-svn-id: https://www.dynare.org/svn/dynare/trunk@2831 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
george 2009-07-10 11:38:57 +00:00
parent 631c4f8794
commit d448eb4e71
4 changed files with 156 additions and 127 deletions

View File

@ -683,12 +683,12 @@ BasicKalmanTask::filterNonDiffuse(const Vector&a,const GeneralMatrix&P,
int inc =1;
const int rcols= Rt.numCols();
GeneralMatrix Ft (Ht.numRows(), Ht.numCols() );
PLUFact Ftinv(Ht.numRows(), Ht.numCols());
GeneralMatrix Lt(Tt);
GeneralMatrix PtLttrans(m,m);
GeneralMatrix Mt(m,n);
GeneralMatrix Kt(m,n);
GeneralMatrix QtRttrans(rcols,Rt.numRows());
// PLUFact Ftinv(Ft);
bool isTunit=0;// Tt->isUnit();
bool isQzero= Qt.isZero();
@ -702,13 +702,14 @@ BasicKalmanTask::filterNonDiffuse(const Vector&a,const GeneralMatrix&P,
int nonsteady=1;
for(;t<=data.numCols()&&nonsteady;++t)
{
ConstVector yt(data,t-1);
// ConstVector yt(data,t-1);
/*****************
This calculates $$v_t = y_t - Z_t*a_t.$$
*****************/
// Vector vt(yt);
vt=yt;
// vt=yt;
memcpy(vt.base(), &(data.get(0,t-1)), n*sizeof(double));
// Zt.multsVec(vt,at);
BLAS_dgemv("N", &n, &m, &neg_alpha, Zt.base(), &n, at.base(),
&inc, &alpha, vt.base(), &inc);
@ -727,10 +728,10 @@ BasicKalmanTask::filterNonDiffuse(const Vector&a,const GeneralMatrix&P,
BLAS_dgemm("N", "N", &n, &n, &m, &alpha, Zt.base(), &n,
Mt.base(), &m, &alpha, Ft.base(), &n);
PLUFact Ftinv(Ft); // Ftinv=Ft;
if(!Ftinv.isRegular())
return 0.0;
// PLUFact Ftinv(Ft);
// if(!Ftinv.isRegular())
// return 0.0;
Ftinv=Ft;
/*****************
This calculates $$K_t = T_tP_tZ_t^TF_t^{-1}.$$
*****************/
@ -738,6 +739,7 @@ BasicKalmanTask::filterNonDiffuse(const Vector&a,const GeneralMatrix&P,
BLAS_dgemm("N", "N", &m, &n, &m, &alpha, Tt.base(), &m,
Mt.base(), &m, &omega, Kt.base(), &m);
Ftinv.multInvRight(Kt);
// Kt.multInvRight(Ft);
/*****************
This calculates $$L_t = T_t-K_tZ_t.$$

View File

@ -83,14 +83,10 @@ NormCholesky::NormCholesky(const GeneralMatrix&a)
TS_RAISE_IF(info<0,
"Internal error in NormCholesky constructor");
;
for(int i= 0;i<L.numRows();i++)
for(int j= i+1;j<L.numCols();j++)
L.get(i,j)= 0.0;
;
for(int j= 0;j<L.numCols();j++){
double d= L.get(j,j);
Vector Lj(L,j);
@ -98,8 +94,6 @@ NormCholesky::NormCholesky(const GeneralMatrix&a)
D[j]= d*d;
}
;
}
;
@ -132,7 +126,6 @@ rows(m.numRows())
calcDetSign();
}
;
PLUFact::PLUFact(const PLUFact&fact)
:inv(fact.inv),ipiv(new int[fact.rows]),
@ -141,6 +134,41 @@ rows(fact.rows),rcond(fact.rcond),detsign(fact.detsign),info(fact.info)
memcpy(ipiv,fact.ipiv,rows*sizeof(int));
}
PLUFact::PLUFact(const int nc,const int nr )
:inv(nr*nc),ipiv(new int[nr]),rows(nr)
{
TS_RAISE_IF(nr!=nc,
"Matrix not square in PLUFact constructor");
}
const PLUFact&
PLUFact::operator = (const GeneralMatrix&m)
{
TS_RAISE_IF(!m.isFinite(),
"Matrix is not finite in PLUFact assignement");
TS_RAISE_IF(m.numRows()!=m.numCols(),
"Matrix not square in PLUFact assignement");
TS_RAISE_IF(m.numRows()!=rows,
"Matrix not matching PLUFact size for assignement");
inv= m.getData();
LAPACK_dgetrf(&rows,&rows,inv.base(),&rows,ipiv,&info);
TS_RAISE_IF(info<0,
"Internal error in PLUFact assignement");
double mnorm= m.getNormInf();
double*work= new double[4*rows];
int*iwork= new int[rows];
int infotmp;
LAPACK_dgecon("I",&rows,inv.base(),&rows,&mnorm,&rcond,work,
iwork,&infotmp);
delete[]iwork;
delete[]work;
TS_RAISE_IF(infotmp<0,
"Internal error in PLUFact assignement");
calcDetSign();
return *this;
}
;
void PLUFact::PL_dgetrs(const char*trans,double*b,int ldb,int bcols)const
@ -230,13 +258,10 @@ void PLUFact::calcDetSign()
if(ipiv[i]!=i+1)
detsign*= -1;
;
for(int i= 0;i<rows;i++)
if(inv[i*(rows+1)]<0)
detsign*= -1;
;
}
;

View File

@ -61,8 +61,10 @@ class PLUFact{
public:
PLUFact(const GeneralMatrix&m);
PLUFact(const PLUFact&plu);
PLUFact(const int nc,const int nr );
virtual~PLUFact()
{delete[]ipiv;}
const PLUFact& operator = (const GeneralMatrix&m);
void multInvLeft(GeneralMatrix&a)const;
void multInvRight(GeneralMatrix&a)const;
void multInvLeft(Vector&a)const;

View File

@ -85,8 +85,6 @@ GeneralMatrix::GeneralMatrix(const GeneralMatrix& a, const char* dum1,
GeneralMatrix::~GeneralMatrix()
{
if(tmpGMp)
delete tmpGMp;
}
@ -180,12 +178,14 @@ void
GeneralMatrix::multInvRight( GeneralMatrix&A)
{
// check or allocate tmp space for Transpose *this
/**
if (tmpGMp)
{
if (tmpGMp->numCols()!=rows || tmpGMp->numRows()!=cols)
delete (tmpGMp);
}
if (!tmpGMp)
********/
tmpGMp= new GeneralMatrix(cols,rows); // allocate space only once if and when needed!
// tmpGMp=(*this)' i.e. Transpose (*this)
@ -216,7 +216,7 @@ GeneralMatrix::multInvRight( GeneralMatrix&A)
for (int j = 0; j < cols; j++)
get(i,j) = tmpGMp->get(j,i);
}
delete tmpGMp;
}