Dynare_pp files Cleared from references to CWEB and reformatted

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@2182 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
george 2008-10-20 16:11:59 +00:00
parent 9da509936c
commit 5f5705de80
69 changed files with 6576 additions and 8445 deletions

View File

@ -1,360 +0,0 @@
#define _W32_FT_OFFSET (116444736000000000LL)
/*1:*/
#line 6 "./journal.cweb"
#include "journal.h"
#include "kord_exception.h"
#ifndef __MINGW32__
# include <sys/resource.h>
# include <sys/utsname.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
SystemResources _sysres;
#ifdef __MINGW32__
/*16:*/
#line 249 "./journal.cweb"
typedef struct _filetime{
unsigned long dwLowDateTime;
unsigned long dwHighDateTime;
}filetime;
extern"C"{
void __stdcall GetSystemTimeAsFileTime(filetime*);
};
typedef union{
long long ns100;
filetime ft;
}w32_ftv;
void gettimeofday(struct timeval*p,struct timezone*tz)
{
w32_ftv _now;
GetSystemTimeAsFileTime(&(_now.ft));
p->tv_usec= (long)((_now.ns100/10LL)%1000000LL);
p->tv_sec= (long)((_now.ns100-_W32_FT_OFFSET)/10000000LL);
return;
}
/*:16*/
#line 20 "./journal.cweb"
;
/*17:*/
#line 282 "./journal.cweb"
#define _SC_PAGESIZE 1
#define _SC_PHYS_PAGES 2
#define _SC_AVPHYS_PAGES 3
#define _SC_NPROCESSORS_ONLN 4
struct Win32MemoryStatus{
unsigned long dwLength;
unsigned long dwMemoryLoad;
unsigned int dwTotalPhys;
unsigned int dwAvailPhys;
unsigned int dwTotalPageFile;
unsigned int dwAvailPageFile;
unsigned int dwTotalVirtual;
unsigned int dwAvailVirtual;
Win32MemoryStatus();
};
extern"C"{
void __stdcall GlobalMemoryStatus(Win32MemoryStatus*);
};
Win32MemoryStatus::Win32MemoryStatus()
{
dwLength= sizeof(Win32MemoryStatus);
GlobalMemoryStatus(this);
}
long sysconf(int name)
{
switch(name){
case _SC_PAGESIZE:
return 1024;
case _SC_PHYS_PAGES:
{
Win32MemoryStatus memstat;
return memstat.dwTotalPhys/1024;
}
case _SC_AVPHYS_PAGES:
{
Win32MemoryStatus memstat;
return memstat.dwAvailPhys/1024;
}
case _SC_NPROCESSORS_ONLN:
return-1;
default:
KORD_RAISE("Not implemented in Win32 sysconf.");
return-1;
}
}
/*:17*/
#line 21 "./journal.cweb"
;
#endif
/*2:*/
#line 40 "./journal.cweb"
SystemResources::SystemResources()
{
gettimeofday(&start,NULL);
}
/*:2*/
#line 24 "./journal.cweb"
;
/*3:*/
#line 48 "./journal.cweb"
long int SystemResources::pageSize()
{
return sysconf(_SC_PAGESIZE);
}
/*:3*/
#line 25 "./journal.cweb"
;
/*4:*/
#line 55 "./journal.cweb"
long int SystemResources::physicalPages()
{
return sysconf(_SC_PHYS_PAGES);
}
/*:4*/
#line 26 "./journal.cweb"
;
/*5:*/
#line 62 "./journal.cweb"
long int SystemResources::onlineProcessors()
{
return sysconf(_SC_NPROCESSORS_ONLN);
}
/*:5*/
#line 27 "./journal.cweb"
;
/*6:*/
#line 69 "./journal.cweb"
long int SystemResources::availableMemory()
{
return pageSize()*sysconf(_SC_AVPHYS_PAGES);
}
/*:6*/
#line 28 "./journal.cweb"
;
/*7:*/
#line 78 "./journal.cweb"
void SystemResources::getRUS(double&load_avg,long int&pg_avail,
double&utime,double&stime,double&elapsed,
long int&idrss,long int&majflt)
{
struct timeval now;
gettimeofday(&now,NULL);
elapsed= now.tv_sec-start.tv_sec+(now.tv_usec-start.tv_usec)*1.0e-6;
#ifndef __MINGW32__
struct rusage rus;
getrusage(RUSAGE_SELF,&rus);
utime= rus.ru_utime.tv_sec+rus.ru_utime.tv_usec*1.0e-6;
stime= rus.ru_stime.tv_sec+rus.ru_stime.tv_usec*1.0e-6;
idrss= rus.ru_idrss;
majflt= rus.ru_majflt;
getloadavg(&load_avg,1);
#else
utime= -1.0;
stime= -1.0;
idrss= -1;
majflt= -1;
load_avg= -1.0;
#endif
pg_avail= sysconf(_SC_AVPHYS_PAGES);
}
/*:7*/
#line 29 "./journal.cweb"
;
/*8:*/
#line 107 "./journal.cweb"
SystemResourcesFlash::SystemResourcesFlash()
{
_sysres.getRUS(load_avg,pg_avail,utime,stime,
elapsed,idrss,majflt);
}
/*:8*/
#line 30 "./journal.cweb"
;
/*9:*/
#line 115 "./journal.cweb"
void SystemResourcesFlash::diff(const SystemResourcesFlash&pre)
{
utime-= pre.utime;
stime-= pre.stime;
elapsed-= pre.elapsed;
idrss-= pre.idrss;
majflt-= pre.majflt;
}
/*:9*/
#line 31 "./journal.cweb"
;
/*10:*/
#line 126 "./journal.cweb"
JournalRecord&JournalRecord::operator<<(const IntSequence&s)
{
operator<<("[");
for(int i= 0;i<s.size();i++){
operator<<(s[i]);
if(i<s.size()-1)
operator<<(",");
}
operator<<("]");
return*this;
}
/*:10*/
#line 32 "./journal.cweb"
;
/*11:*/
#line 140 "./journal.cweb"
void JournalRecord::writePrefix(const SystemResourcesFlash&f)
{
for(int i= 0;i<MAXLEN;i++)
prefix[i]= ' ';
double mb= 1024*1024;
sprintf(prefix,"%07.6g",f.elapsed);
sprintf(prefix+7,":%c%05d",recChar,ord);
sprintf(prefix+14,":%1.1f",f.load_avg);
sprintf(prefix+18,":%05.4g",f.pg_avail*_sysres.pageSize()/mb);
sprintf(prefix+24,"%s",": : ");
for(int i= 0;i<2*journal.getDepth();i++)
prefix[i+33]= ' ';
prefix[2*journal.getDepth()+33]= '\0';
}
/*:11*/
#line 33 "./journal.cweb"
;
/*12:*/
#line 157 "./journal.cweb"
void JournalRecordPair::writePrefixForEnd(const SystemResourcesFlash&f)
{
for(int i= 0;i<MAXLEN;i++)
prefix_end[i]= ' ';
double mb= 1024*1024;
SystemResourcesFlash difnow;
difnow.diff(f);
sprintf(prefix_end,"%07.6g",f.elapsed+difnow.elapsed);
sprintf(prefix_end+7,":E%05d",ord);
sprintf(prefix_end+14,":%1.1f",difnow.load_avg);
sprintf(prefix_end+18,":%05.4g",difnow.pg_avail*_sysres.pageSize()/mb);
sprintf(prefix_end+24,":%06.5g",difnow.majflt*_sysres.pageSize()/mb);
sprintf(prefix_end+31,"%s",": ");
for(int i= 0;i<2*journal.getDepth();i++)
prefix_end[i+33]= ' ';
prefix_end[2*journal.getDepth()+33]= '\0';
}
/*:12*/
#line 34 "./journal.cweb"
;
/*13:*/
#line 177 "./journal.cweb"
JournalRecordPair::~JournalRecordPair()
{
journal.decrementDepth();
writePrefixForEnd(flash);
journal<<prefix_end;
journal<<mes;
journal<<endl;
journal.flush();
}
/*:13*/
#line 35 "./journal.cweb"
;
/*14:*/
#line 189 "./journal.cweb"
JournalRecord&endrec(JournalRecord&rec)
{
rec.journal<<rec.prefix;
rec.journal<<rec.mes;
rec.journal<<endl;
rec.journal.flush();
rec.journal.incrementOrd();
return rec;
}
/*:14*/
#line 36 "./journal.cweb"
;
/*15:*/
#line 201 "./journal.cweb"
void Journal::printHeader()
{
(*this)<<"This is Dynare++, Copyright (C) 2004,2005 Michel Juillard, Ondra Kamenik\n";
(*this)<<"Dynare++ comes with ABSOLUTELY NO WARRANTY and is distributed under\n";
(*this)<<"General Public License, see http://www.gnu.org/license/gpl.html\n";
(*this)<<"\n\n";
#ifndef __MINGW32__
utsname info;
uname(&info);
(*this)<<"System info: ";
(*this)<<info.sysname<<" "<<info.release<<" "<<info.version<<" ";
(*this)<<info.machine<<", processors online: "<<_sysres.onlineProcessors();
(*this)<<"\n\nStart time: ";
char ts[100];
time_t curtime= time(NULL);
tm loctime;
localtime_r(&curtime,&loctime);
asctime_r(&loctime,ts);
(*this)<<ts<<"\n";
#else
(*this)<<"System info: (not implemented for MINGW)\n";
(*this)<<"Start time: (not implemented for MINGW)\n\n";
#endif
(*this)<<" ------ elapsed time (seconds) \n";
(*this)<<" | ------ record unique identifier \n";
(*this)<<" | | ------ load average \n";
(*this)<<" | | | ------ available memory (MB) \n";
(*this)<<" | | | | ------ major faults (MB)\n";
(*this)<<" | | | | | \n";
(*this)<<" V V V V V \n";
(*this)<<"\n";
}
/*:15*/
#line 37 "./journal.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./approximation.cweb"
#include "kord_exception.h"
#include "approximation.h"
@ -7,40 +6,35 @@
#include "korder_stoch.h"
/*2:*/
#line 26 "./approximation.cweb"
ZAuxContainer::ZAuxContainer(const _Ctype*gss,int ngss,int ng,int ny,int nu)
:StackContainer<FGSTensor> (4,1)
{
stack_sizes[0]= ngss;stack_sizes[1]= ng;
stack_sizes[2]= ny;stack_sizes[3]= nu;
conts[0]= gss;
calculateOffsets();
stack_sizes[0]= ngss;stack_sizes[1]= ng;
stack_sizes[2]= ny;stack_sizes[3]= nu;
conts[0]= gss;
calculateOffsets();
}
/*:2*/
#line 12 "./approximation.cweb"
;
/*3:*/
#line 41 "./approximation.cweb"
ZAuxContainer::itype ZAuxContainer::getType(int i,const Symmetry&s)const
{
if(i==0)
if(s[2]> 0)
return zero;
else
return matrix;
return zero;
if(i==0)
if(s[2]> 0)
return zero;
else
return matrix;
return zero;
}
/*:3*/
#line 13 "./approximation.cweb"
;
/*4:*/
#line 54 "./approximation.cweb"
Approximation::Approximation(DynamicModel&m,Journal&j,int ns)
:model(m),journal(j),rule_ders(NULL),rule_ders_ss(NULL),fdr(NULL),udr(NULL),
@ -48,347 +42,312 @@ ypart(model.nstat(),model.npred(),model.nboth(),model.nforw()),
mom(UNormalMoments(model.order(),model.getVcov())),nvs(4),steps(ns),
ss(ypart.ny(),steps+1)
{
nvs[0]= ypart.nys();nvs[1]= model.nexog();
nvs[2]= model.nexog();nvs[3]= 1;
ss.nans();
nvs[0]= ypart.nys();nvs[1]= model.nexog();
nvs[2]= model.nexog();nvs[3]= 1;
ss.nans();
}
/*:4*/
#line 14 "./approximation.cweb"
;
/*5:*/
#line 68 "./approximation.cweb"
Approximation::~Approximation()
{
if(rule_ders_ss)delete rule_ders_ss;
if(rule_ders)delete rule_ders;
if(fdr)delete fdr;
if(udr)delete udr;
if(rule_ders_ss)delete rule_ders_ss;
if(rule_ders)delete rule_ders;
if(fdr)delete fdr;
if(udr)delete udr;
}
/*:5*/
#line 15 "./approximation.cweb"
;
/*6:*/
#line 78 "./approximation.cweb"
const FoldDecisionRule&Approximation::getFoldDecisionRule()const
{
KORD_RAISE_IF(fdr==NULL,
"Folded decision rule has not been created in Approximation::getFoldDecisionRule");
return*fdr;
KORD_RAISE_IF(fdr==NULL,
"Folded decision rule has not been created in Approximation::getFoldDecisionRule");
return*fdr;
}
/*:6*/
#line 16 "./approximation.cweb"
;
/*7:*/
#line 88 "./approximation.cweb"
const UnfoldDecisionRule&Approximation::getUnfoldDecisionRule()const
{
KORD_RAISE_IF(udr==NULL,
"Unfolded decision rule has not been created in Approximation::getUnfoldDecisionRule");
return*udr;
KORD_RAISE_IF(udr==NULL,
"Unfolded decision rule has not been created in Approximation::getUnfoldDecisionRule");
return*udr;
}
/*:7*/
#line 17 "./approximation.cweb"
;
/*8:*/
#line 102 "./approximation.cweb"
void Approximation::approxAtSteady()
{
model.calcDerivativesAtSteady();
FirstOrder fo(model.nstat(),model.npred(),model.nboth(),model.nforw(),
model.nexog(),*(model.getModelDerivatives().get(Symmetry(1))),
journal);
KORD_RAISE_IF_X(!fo.isStable(),
"The model is not Blanchard-Kahn stable",
KORD_MD_NOT_STABLE);
if(model.order()>=2){
KOrder korder(model.nstat(),model.npred(),model.nboth(),model.nforw(),
model.getModelDerivatives(),fo.getGy(),fo.getGu(),
model.getVcov(),journal);
korder.switchToFolded();
for(int k= 2;k<=model.order();k++)
korder.performStep<KOrder::fold> (k);
saveRuleDerivs(korder.getFoldDers());
}else{
FirstOrderDerivs<KOrder::fold> fo_ders(fo);
saveRuleDerivs(fo_ders);
}
check(0.0);
model.calcDerivativesAtSteady();
FirstOrder fo(model.nstat(),model.npred(),model.nboth(),model.nforw(),
model.nexog(),*(model.getModelDerivatives().get(Symmetry(1))),
journal);
KORD_RAISE_IF_X(!fo.isStable(),
"The model is not Blanchard-Kahn stable",
KORD_MD_NOT_STABLE);
if(model.order()>=2){
KOrder korder(model.nstat(),model.npred(),model.nboth(),model.nforw(),
model.getModelDerivatives(),fo.getGy(),fo.getGu(),
model.getVcov(),journal);
korder.switchToFolded();
for(int k= 2;k<=model.order();k++)
korder.performStep<KOrder::fold> (k);
saveRuleDerivs(korder.getFoldDers());
}else{
FirstOrderDerivs<KOrder::fold> fo_ders(fo);
saveRuleDerivs(fo_ders);
}
check(0.0);
}
/*:8*/
#line 18 "./approximation.cweb"
;
/*9:*/
#line 159 "./approximation.cweb"
void Approximation::walkStochSteady()
{
/*10:*/
#line 186 "./approximation.cweb"
model.solveDeterministicSteady();
approxAtSteady();
Vector steady0(ss,0);
steady0= (const Vector&)model.getSteady();
/*:10*/
#line 162 "./approximation.cweb"
;
double sigma_so_far= 0.0;
double dsigma= (steps==0)?0.0:1.0/steps;
for(int i= 1;i<=steps;i++){
JournalRecordPair pa(journal);
pa<<"Approximation about stochastic steady for sigma="<<sigma_so_far+dsigma<<endrec;
Vector last_steady((const Vector&)model.getSteady());
/*11:*/
#line 196 "./approximation.cweb"
DRFixPoint<KOrder::fold> fp(*rule_ders,ypart,model.getSteady(),dsigma);
bool converged= fp.calcFixPoint(DecisionRule::horner,model.getSteady());
JournalRecord rec(journal);
rec<<"Fix point calcs: iter="<<fp.getNumIter()<<", newton_iter="
<<fp.getNewtonTotalIter()<<", last_newton_iter="<<fp.getNewtonLastIter()<<".";
if(converged)
rec<<" Converged."<<endrec;
else{
rec<<" Not converged!!"<<endrec;
KORD_RAISE_X("Fix point calculation not converged",KORD_FP_NOT_CONV);
}
Vector steadyi(ss,i);
steadyi= (const Vector&)model.getSteady();
/*:11*/
#line 171 "./approximation.cweb"
;
/*12:*/
#line 216 "./approximation.cweb"
Vector dy((const Vector&)model.getSteady());
dy.add(-1.0,last_steady);
StochForwardDerivs<KOrder::fold> hh(ypart,model.nexog(),*rule_ders_ss,mom,dy,
dsigma,sigma_so_far);
JournalRecord rec1(journal);
rec1<<"Calculation of g** expectations done"<<endrec;
/*:12*/
#line 172 "./approximation.cweb"
;
/*13:*/
#line 229 "./approximation.cweb"
model.calcDerivativesAtSteady();
KOrderStoch korder_stoch(ypart,model.nexog(),model.getModelDerivatives(),
hh,journal);
for(int d= 1;d<=model.order();d++){
korder_stoch.performStep<KOrder::fold> (d);
}
saveRuleDerivs(korder_stoch.getFoldDers());
/*:13*/
#line 173 "./approximation.cweb"
;
check(sigma_so_far+dsigma);
sigma_so_far+= dsigma;
}
/*14:*/
#line 240 "./approximation.cweb"
if(fdr){
delete fdr;
fdr= NULL;
}
if(udr){
delete udr;
udr= NULL;
}
fdr= new FoldDecisionRule(*rule_ders,ypart,model.nexog(),
model.getSteady(),1.0-sigma_so_far);
if(steps==0){
/*15:*/
#line 258 "./approximation.cweb"
DRFixPoint<KOrder::fold> fp(*rule_ders,ypart,model.getSteady(),1.0);
bool converged= fp.calcFixPoint(DecisionRule::horner,model.getSteady());
JournalRecord rec(journal);
rec<<"Fix point calcs: iter="<<fp.getNumIter()<<", newton_iter="
<<fp.getNewtonTotalIter()<<", last_newton_iter="<<fp.getNewtonLastIter()<<".";
if(converged)
rec<<" Converged."<<endrec;
else{
rec<<" Not converged!!"<<endrec;
KORD_RAISE_X("Fix point calculation not converged",KORD_FP_NOT_CONV);
}
{
JournalRecordPair recp(journal);
recp<<"Centralizing about fix-point."<<endrec;
FoldDecisionRule*dr_backup= fdr;
fdr= new FoldDecisionRule(*dr_backup,model.getSteady());
delete dr_backup;
}
/*:15*/
#line 253 "./approximation.cweb"
;
}
/*:14*/
#line 179 "./approximation.cweb"
;
/*10:*/
model.solveDeterministicSteady();
approxAtSteady();
Vector steady0(ss,0);
steady0= (const Vector&)model.getSteady();
/*:10*/
;
double sigma_so_far= 0.0;
double dsigma= (steps==0)?0.0:1.0/steps;
for(int i= 1;i<=steps;i++){
JournalRecordPair pa(journal);
pa<<"Approximation about stochastic steady for sigma="<<sigma_so_far+dsigma<<endrec;
Vector last_steady((const Vector&)model.getSteady());
/*11:*/
DRFixPoint<KOrder::fold> fp(*rule_ders,ypart,model.getSteady(),dsigma);
bool converged= fp.calcFixPoint(DecisionRule::horner,model.getSteady());
JournalRecord rec(journal);
rec<<"Fix point calcs: iter="<<fp.getNumIter()<<", newton_iter="
<<fp.getNewtonTotalIter()<<", last_newton_iter="<<fp.getNewtonLastIter()<<".";
if(converged)
rec<<" Converged."<<endrec;
else{
rec<<" Not converged!!"<<endrec;
KORD_RAISE_X("Fix point calculation not converged",KORD_FP_NOT_CONV);
}
Vector steadyi(ss,i);
steadyi= (const Vector&)model.getSteady();
/*:11*/
;
/*12:*/
Vector dy((const Vector&)model.getSteady());
dy.add(-1.0,last_steady);
StochForwardDerivs<KOrder::fold> hh(ypart,model.nexog(),*rule_ders_ss,mom,dy,
dsigma,sigma_so_far);
JournalRecord rec1(journal);
rec1<<"Calculation of g** expectations done"<<endrec;
/*:12*/
;
/*13:*/
model.calcDerivativesAtSteady();
KOrderStoch korder_stoch(ypart,model.nexog(),model.getModelDerivatives(),
hh,journal);
for(int d= 1;d<=model.order();d++){
korder_stoch.performStep<KOrder::fold> (d);
}
saveRuleDerivs(korder_stoch.getFoldDers());
/*:13*/
;
check(sigma_so_far+dsigma);
sigma_so_far+= dsigma;
}
/*14:*/
if(fdr){
delete fdr;
fdr= NULL;
}
if(udr){
delete udr;
udr= NULL;
}
fdr= new FoldDecisionRule(*rule_ders,ypart,model.nexog(),
model.getSteady(),1.0-sigma_so_far);
if(steps==0){
/*15:*/
DRFixPoint<KOrder::fold> fp(*rule_ders,ypart,model.getSteady(),1.0);
bool converged= fp.calcFixPoint(DecisionRule::horner,model.getSteady());
JournalRecord rec(journal);
rec<<"Fix point calcs: iter="<<fp.getNumIter()<<", newton_iter="
<<fp.getNewtonTotalIter()<<", last_newton_iter="<<fp.getNewtonLastIter()<<".";
if(converged)
rec<<" Converged."<<endrec;
else{
rec<<" Not converged!!"<<endrec;
KORD_RAISE_X("Fix point calculation not converged",KORD_FP_NOT_CONV);
}
{
JournalRecordPair recp(journal);
recp<<"Centralizing about fix-point."<<endrec;
FoldDecisionRule*dr_backup= fdr;
fdr= new FoldDecisionRule(*dr_backup,model.getSteady());
delete dr_backup;
}
/*:15*/
;
}
/*:14*/
;
}
/*:9*/
#line 19 "./approximation.cweb"
;
/*16:*/
#line 285 "./approximation.cweb"
void Approximation::saveRuleDerivs(const FGSContainer&g)
{
if(rule_ders){
delete rule_ders;
delete rule_ders_ss;
}
rule_ders= new FGSContainer(g);
rule_ders_ss= new FGSContainer(4);
for(FGSContainer::iterator run= (*rule_ders).begin();run!=(*rule_ders).end();++run){
FGSTensor*ten= new FGSTensor(ypart.nstat+ypart.npred,ypart.nyss(),*((*run).second));
rule_ders_ss->insert(ten);
}
if(rule_ders){
delete rule_ders;
delete rule_ders_ss;
}
rule_ders= new FGSContainer(g);
rule_ders_ss= new FGSContainer(4);
for(FGSContainer::iterator run= (*rule_ders).begin();run!=(*rule_ders).end();++run){
FGSTensor*ten= new FGSTensor(ypart.nstat+ypart.npred,ypart.nyss(),*((*run).second));
rule_ders_ss->insert(ten);
}
}
/*:16*/
#line 20 "./approximation.cweb"
;
/*17:*/
#line 312 "./approximation.cweb"
void Approximation::calcStochShift(Vector&out,double at_sigma)const
{
KORD_RAISE_IF(out.length()!=ypart.ny(),
"Wrong length of output vector for Approximation::calcStochShift");
out.zeros();
ZAuxContainer zaux(rule_ders_ss,ypart.nyss(),ypart.ny(),
ypart.nys(),model.nexog());
int dfac= 1;
for(int d= 1;d<=rule_ders->getMaxDim();d++,dfac*= d){
if(KOrder::is_even(d)){
Symmetry sym(0,d,0,0);
/*18:*/
#line 333 "./approximation.cweb"
FGSTensor*ten= new FGSTensor(ypart.ny(),TensorDimens(sym,nvs));
ten->zeros();
for(int l= 1;l<=d;l++){
const FSSparseTensor*f= model.getModelDerivatives().get(Symmetry(l));
zaux.multAndAdd(*f,*ten);
}
/*:18*/
#line 326 "./approximation.cweb"
;
/*19:*/
#line 342 "./approximation.cweb"
FGSTensor*tmp= new FGSTensor(ypart.ny(),TensorDimens(Symmetry(0,0,0,0),nvs));
tmp->zeros();
ten->contractAndAdd(1,*tmp,*(mom.get(Symmetry(d))));
out.add(pow(at_sigma,d)/dfac,tmp->getData());
delete ten;
delete tmp;
/*:19*/
#line 327 "./approximation.cweb"
;
}
}
KORD_RAISE_IF(out.length()!=ypart.ny(),
"Wrong length of output vector for Approximation::calcStochShift");
out.zeros();
ZAuxContainer zaux(rule_ders_ss,ypart.nyss(),ypart.ny(),
ypart.nys(),model.nexog());
int dfac= 1;
for(int d= 1;d<=rule_ders->getMaxDim();d++,dfac*= d){
if(KOrder::is_even(d)){
Symmetry sym(0,d,0,0);
/*18:*/
FGSTensor*ten= new FGSTensor(ypart.ny(),TensorDimens(sym,nvs));
ten->zeros();
for(int l= 1;l<=d;l++){
const FSSparseTensor*f= model.getModelDerivatives().get(Symmetry(l));
zaux.multAndAdd(*f,*ten);
}
/*:18*/
;
/*19:*/
FGSTensor*tmp= new FGSTensor(ypart.ny(),TensorDimens(Symmetry(0,0,0,0),nvs));
tmp->zeros();
ten->contractAndAdd(1,*tmp,*(mom.get(Symmetry(d))));
out.add(pow(at_sigma,d)/dfac,tmp->getData());
delete ten;
delete tmp;
/*:19*/
;
}
}
}
/*:17*/
#line 21 "./approximation.cweb"
;
/*20:*/
#line 359 "./approximation.cweb"
void Approximation::check(double at_sigma)const
{
Vector stoch_shift(ypart.ny());
Vector system_resid(ypart.ny());
Vector xx(model.nexog());
xx.zeros();
model.evaluateSystem(system_resid,model.getSteady(),xx);
calcStochShift(stoch_shift,at_sigma);
stoch_shift.add(1.0,system_resid);
JournalRecord rec1(journal);
rec1<<"Error of current approximation for shocks at sigma "<<at_sigma
<<" is "<<stoch_shift.getMax()<<endrec;
calcStochShift(stoch_shift,1.0);
stoch_shift.add(1.0,system_resid);
JournalRecord rec2(journal);
rec2<<"Error of current approximation for full shocks is "<<stoch_shift.getMax()<<endrec;
Vector stoch_shift(ypart.ny());
Vector system_resid(ypart.ny());
Vector xx(model.nexog());
xx.zeros();
model.evaluateSystem(system_resid,model.getSteady(),xx);
calcStochShift(stoch_shift,at_sigma);
stoch_shift.add(1.0,system_resid);
JournalRecord rec1(journal);
rec1<<"Error of current approximation for shocks at sigma "<<at_sigma
<<" is "<<stoch_shift.getMax()<<endrec;
calcStochShift(stoch_shift,1.0);
stoch_shift.add(1.0,system_resid);
JournalRecord rec2(journal);
rec2<<"Error of current approximation for full shocks is "<<stoch_shift.getMax()<<endrec;
}
/*:20*/
#line 22 "./approximation.cweb"
;
/*21:*/
#line 394 "./approximation.cweb"
TwoDMatrix*Approximation::calcYCov()const
{
const TwoDMatrix&gy= *(rule_ders->get(Symmetry(1,0,0,0)));
const TwoDMatrix&gu= *(rule_ders->get(Symmetry(0,1,0,0)));
TwoDMatrix G(model.numeq(),model.numeq());
G.zeros();
G.place(gy,0,model.nstat());
TwoDMatrix B((const TwoDMatrix&)G);
B.mult(-1.0);
TwoDMatrix C(G,"transpose");
TwoDMatrix A(model.numeq(),model.numeq());
A.zeros();
for(int i= 0;i<model.numeq();i++)
A.get(i,i)= 1.0;
TwoDMatrix guSigma(gu,model.getVcov());
TwoDMatrix guTrans(gu,"transpose");
TwoDMatrix*X= new TwoDMatrix(guSigma,guTrans);
GeneralSylvester gs(1,model.numeq(),model.numeq(),0,
A.base(),B.base(),C.base(),X->base());
gs.solve();
return X;
const TwoDMatrix&gy= *(rule_ders->get(Symmetry(1,0,0,0)));
const TwoDMatrix&gu= *(rule_ders->get(Symmetry(0,1,0,0)));
TwoDMatrix G(model.numeq(),model.numeq());
G.zeros();
G.place(gy,0,model.nstat());
TwoDMatrix B((const TwoDMatrix&)G);
B.mult(-1.0);
TwoDMatrix C(G,"transpose");
TwoDMatrix A(model.numeq(),model.numeq());
A.zeros();
for(int i= 0;i<model.numeq();i++)
A.get(i,i)= 1.0;
TwoDMatrix guSigma(gu,model.getVcov());
TwoDMatrix guTrans(gu,"transpose");
TwoDMatrix*X= new TwoDMatrix(guSigma,guTrans);
GeneralSylvester gs(1,model.numeq(),model.numeq(),0,
A.base(),B.base(),C.base(),X->base());
gs.solve();
return X;
}
/*:21*/
#line 23 "./approximation.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 53 "./approximation.hweb"
#ifndef APPROXIMATION_H
#define APPROXIMATION_H
@ -10,59 +9,55 @@
#include "journal.h"
/*2:*/
#line 80 "./approximation.hweb"
class ZAuxContainer:public StackContainer<FGSTensor> ,public FoldedStackContainer{
public:
typedef StackContainer<FGSTensor> ::_Ctype _Ctype;
typedef StackContainer<FGSTensor> ::itype itype;
ZAuxContainer(const _Ctype*gss,int ngss,int ng,int ny,int nu);
itype getType(int i,const Symmetry&s)const;
typedef StackContainer<FGSTensor> ::_Ctype _Ctype;
typedef StackContainer<FGSTensor> ::itype itype;
ZAuxContainer(const _Ctype*gss,int ngss,int ng,int ny,int nu);
itype getType(int i,const Symmetry&s)const;
};
/*:2*/
#line 62 "./approximation.hweb"
;
/*3:*/
#line 115 "./approximation.hweb"
class Approximation{
DynamicModel&model;
Journal&journal;
FGSContainer*rule_ders;
FGSContainer*rule_ders_ss;
FoldDecisionRule*fdr;
UnfoldDecisionRule*udr;
const PartitionY ypart;
const FNormalMoments mom;
IntSequence nvs;
int steps;
TwoDMatrix ss;
DynamicModel&model;
Journal&journal;
FGSContainer*rule_ders;
FGSContainer*rule_ders_ss;
FoldDecisionRule*fdr;
UnfoldDecisionRule*udr;
const PartitionY ypart;
const FNormalMoments mom;
IntSequence nvs;
int steps;
TwoDMatrix ss;
public:
Approximation(DynamicModel&m,Journal&j,int ns);
virtual~Approximation();
const FoldDecisionRule&getFoldDecisionRule()const;
const UnfoldDecisionRule&getUnfoldDecisionRule()const;
const TwoDMatrix&getSS()const
{return ss;}
const DynamicModel&getModel()const
{return model;}
void walkStochSteady();
TwoDMatrix*calcYCov()const;
Approximation(DynamicModel&m,Journal&j,int ns);
virtual~Approximation();
const FoldDecisionRule&getFoldDecisionRule()const;
const UnfoldDecisionRule&getUnfoldDecisionRule()const;
const TwoDMatrix&getSS()const
{return ss;}
const DynamicModel&getModel()const
{return model;}
void walkStochSteady();
TwoDMatrix*calcYCov()const;
protected:
void approxAtSteady();
void calcStochShift(Vector&out,double at_sigma)const;
void saveRuleDerivs(const FGSContainer&g);
void check(double at_sigma)const;
void approxAtSteady();
void calcStochShift(Vector&out,double at_sigma)const;
void saveRuleDerivs(const FGSContainer&g);
void check(double at_sigma)const;
};
/*:3*/
#line 63 "./approximation.hweb"
;
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,65 +1,58 @@
/*1:*/
#line 6 "./dynamic_model.cweb"
#include "dynamic_model.h"
/*2:*/
#line 14 "./dynamic_model.cweb"
void NameList::print()const
{
for(int i= 0;i<getNum();i++)
printf("%s\n",getName(i));
for(int i= 0;i<getNum();i++)
printf("%s\n",getName(i));
}
/*:2*/
#line 9 "./dynamic_model.cweb"
;
/*3:*/
#line 22 "./dynamic_model.cweb"
void NameList::writeMat4(FILE*fd,const char*vname)const
{
int maxlen= 0;
for(int i= 0;i<getNum();i++)
if(maxlen<(int)strlen(getName(i)))
maxlen= (int)strlen(getName(i));
if(maxlen==0)
return;
TwoDMatrix m(getNum(),maxlen);
for(int i= 0;i<getNum();i++)
for(int j= 0;j<maxlen;j++)
if(j<(int)strlen(getName(i)))
m.get(i,j)= (double)(getName(i)[j]);
else
m.get(i,j)= (double)(' ');
Mat4Header header(m,vname,"text matrix");
header.write(fd);
fwrite(m.getData().base(),sizeof(double),m.nrows()*m.ncols(),fd);
int maxlen= 0;
for(int i= 0;i<getNum();i++)
if(maxlen<(int)strlen(getName(i)))
maxlen= (int)strlen(getName(i));
if(maxlen==0)
return;
TwoDMatrix m(getNum(),maxlen);
for(int i= 0;i<getNum();i++)
for(int j= 0;j<maxlen;j++)
if(j<(int)strlen(getName(i)))
m.get(i,j)= (double)(getName(i)[j]);
else
m.get(i,j)= (double)(' ');
Mat4Header header(m,vname,"text matrix");
header.write(fd);
fwrite(m.getData().base(),sizeof(double),m.nrows()*m.ncols(),fd);
}
/*:3*/
#line 10 "./dynamic_model.cweb"
;
/*4:*/
#line 47 "./dynamic_model.cweb"
void NameList::writeMat4Indices(FILE*fd,const char*prefix)const
{
char tmp[100];
TwoDMatrix aux(1,1);
for(int i= 0;i<getNum();i++){
sprintf(tmp,"%s_i_%s",prefix,getName(i));
aux.get(0,0)= i+1;
aux.writeMat4(fd,tmp);
}
char tmp[100];
TwoDMatrix aux(1,1);
for(int i= 0;i<getNum();i++){
sprintf(tmp,"%s_i_%s",prefix,getName(i));
aux.get(0,0)= i+1;
aux.writeMat4(fd,tmp);
}
}
/*:4*/
#line 11 "./dynamic_model.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 13 "./dynamic_model.hweb"
#ifndef DYNAMIC_MODEL_H
#define DYNAMIC_MODEL_H
@ -10,56 +9,52 @@
#include "Vector.h"
/*2:*/
#line 29 "./dynamic_model.hweb"
class NameList{
public:
virtual~NameList(){}
virtual int getNum()const= 0;
virtual const char*getName(int i)const= 0;
void print()const;
void writeMat4(FILE*fd,const char*vname)const;
void writeMat4Indices(FILE*fd,const char*prefix)const;
virtual~NameList(){}
virtual int getNum()const= 0;
virtual const char*getName(int i)const= 0;
void print()const;
void writeMat4(FILE*fd,const char*vname)const;
void writeMat4Indices(FILE*fd,const char*prefix)const;
};
/*:2*/
#line 22 "./dynamic_model.hweb"
;
/*3:*/
#line 89 "./dynamic_model.hweb"
class DynamicModel{
public:
virtual DynamicModel*clone()const= 0;
virtual~DynamicModel(){}
virtual int nstat()const= 0;
virtual int nboth()const= 0;
virtual int npred()const= 0;
virtual int nforw()const= 0;
virtual int nexog()const= 0;
virtual int order()const= 0;
int numeq()const
{return nstat()+nboth()+npred()+nforw();}
virtual const NameList&getAllEndoNames()const= 0;
virtual const NameList&getStateNames()const= 0;
virtual const NameList&getExogNames()const= 0;
virtual const TwoDMatrix&getVcov()const= 0;
virtual const TensorContainer<FSSparseTensor> &getModelDerivatives()const= 0;
virtual const Vector&getSteady()const= 0;
virtual Vector&getSteady()= 0;
virtual void solveDeterministicSteady()= 0;
virtual void evaluateSystem(Vector&out,const Vector&yy,const Vector&xx)= 0;
virtual void evaluateSystem(Vector&out,const Vector&yym,const Vector&yy,
const Vector&yyp,const Vector&xx)= 0;
virtual void calcDerivativesAtSteady()= 0;
virtual DynamicModel*clone()const= 0;
virtual~DynamicModel(){}
virtual int nstat()const= 0;
virtual int nboth()const= 0;
virtual int npred()const= 0;
virtual int nforw()const= 0;
virtual int nexog()const= 0;
virtual int order()const= 0;
int numeq()const
{return nstat()+nboth()+npred()+nforw();}
virtual const NameList&getAllEndoNames()const= 0;
virtual const NameList&getStateNames()const= 0;
virtual const NameList&getExogNames()const= 0;
virtual const TwoDMatrix&getVcov()const= 0;
virtual const TensorContainer<FSSparseTensor> &getModelDerivatives()const= 0;
virtual const Vector&getSteady()const= 0;
virtual Vector&getSteady()= 0;
virtual void solveDeterministicSteady()= 0;
virtual void evaluateSystem(Vector&out,const Vector&yy,const Vector&xx)= 0;
virtual void evaluateSystem(Vector&out,const Vector&yym,const Vector&yy,
const Vector&yyp,const Vector&xx)= 0;
virtual void calcDerivativesAtSteady()= 0;
};
/*:3*/
#line 23 "./dynamic_model.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 9 "./faa_di_bruno.cweb"
#include "faa_di_bruno.h"
#include "fine_container.h"
@ -8,123 +7,113 @@
double FaaDiBruno::magic_mult= 1.5;
/*2:*/
#line 25 "./faa_di_bruno.cweb"
void FaaDiBruno::calculate(const StackContainer<FGSTensor> &cont,
const TensorContainer<FSSparseTensor> &f,
FGSTensor&out)
const TensorContainer<FSSparseTensor> &f,
FGSTensor&out)
{
out.zeros();
for(int l= 1;l<=out.dimen();l++){
int mem_mb,p_size_mb;
int max= estimRefinment(out.getDims(),out.nrows(),l,mem_mb,p_size_mb);
FoldedFineContainer fine_cont(cont,max);
fine_cont.multAndAdd(l,f,out);
JournalRecord recc(journal);
recc<<"dim="<<l<<" avmem="<<mem_mb<<" tmpmem="<<p_size_mb<<" max="<<max
<<" stacks="<<cont.numStacks()<<"->"<<fine_cont.numStacks()<<endrec;
}
out.zeros();
for(int l= 1;l<=out.dimen();l++){
int mem_mb,p_size_mb;
int max= estimRefinment(out.getDims(),out.nrows(),l,mem_mb,p_size_mb);
FoldedFineContainer fine_cont(cont,max);
fine_cont.multAndAdd(l,f,out);
JournalRecord recc(journal);
recc<<"dim="<<l<<" avmem="<<mem_mb<<" tmpmem="<<p_size_mb<<" max="<<max
<<" stacks="<<cont.numStacks()<<"->"<<fine_cont.numStacks()<<endrec;
}
}
/*:2*/
#line 16 "./faa_di_bruno.cweb"
;
/*3:*/
#line 45 "./faa_di_bruno.cweb"
void FaaDiBruno::calculate(const FoldedStackContainer&cont,const FGSContainer&g,
FGSTensor&out)
FGSTensor&out)
{
out.zeros();
for(int l= 1;l<=out.dimen();l++){
long int mem= SystemResources::availableMemory();
cont.multAndAdd(l,g,out);
JournalRecord rec(journal);
int mem_mb= mem/1024/1024;
rec<<"dim="<<l<<" avmem="<<mem_mb<<endrec;
}
out.zeros();
for(int l= 1;l<=out.dimen();l++){
long int mem= SystemResources::availableMemory();
cont.multAndAdd(l,g,out);
JournalRecord rec(journal);
int mem_mb= mem/1024/1024;
rec<<"dim="<<l<<" avmem="<<mem_mb<<endrec;
}
}
/*:3*/
#line 17 "./faa_di_bruno.cweb"
;
/*4:*/
#line 63 "./faa_di_bruno.cweb"
void FaaDiBruno::calculate(const StackContainer<UGSTensor> &cont,
const TensorContainer<FSSparseTensor> &f,
UGSTensor&out)
const TensorContainer<FSSparseTensor> &f,
UGSTensor&out)
{
out.zeros();
for(int l= 1;l<=out.dimen();l++){
int mem_mb,p_size_mb;
int max= estimRefinment(out.getDims(),out.nrows(),l,mem_mb,p_size_mb);
UnfoldedFineContainer fine_cont(cont,max);
fine_cont.multAndAdd(l,f,out);
JournalRecord recc(journal);
recc<<"dim="<<l<<" avmem="<<mem_mb<<" tmpmem="<<p_size_mb<<" max="<<max
<<" stacks="<<cont.numStacks()<<"->"<<fine_cont.numStacks()<<endrec;
}
out.zeros();
for(int l= 1;l<=out.dimen();l++){
int mem_mb,p_size_mb;
int max= estimRefinment(out.getDims(),out.nrows(),l,mem_mb,p_size_mb);
UnfoldedFineContainer fine_cont(cont,max);
fine_cont.multAndAdd(l,f,out);
JournalRecord recc(journal);
recc<<"dim="<<l<<" avmem="<<mem_mb<<" tmpmem="<<p_size_mb<<" max="<<max
<<" stacks="<<cont.numStacks()<<"->"<<fine_cont.numStacks()<<endrec;
}
}
/*:4*/
#line 18 "./faa_di_bruno.cweb"
;
/*5:*/
#line 81 "./faa_di_bruno.cweb"
void FaaDiBruno::calculate(const UnfoldedStackContainer&cont,const UGSContainer&g,
UGSTensor&out)
UGSTensor&out)
{
out.zeros();
for(int l= 1;l<=out.dimen();l++){
long int mem= SystemResources::availableMemory();
cont.multAndAdd(l,g,out);
JournalRecord rec(journal);
int mem_mb= mem/1024/1024;
rec<<"dim="<<l<<" avmem="<<mem_mb<<endrec;
}
out.zeros();
for(int l= 1;l<=out.dimen();l++){
long int mem= SystemResources::availableMemory();
cont.multAndAdd(l,g,out);
JournalRecord rec(journal);
int mem_mb= mem/1024/1024;
rec<<"dim="<<l<<" avmem="<<mem_mb<<endrec;
}
}
/*:5*/
#line 19 "./faa_di_bruno.cweb"
;
/*6:*/
#line 126 "./faa_di_bruno.cweb"
int FaaDiBruno::estimRefinment(const TensorDimens&tdims,int nr,int l,
int&avmem_mb,int&tmpmem_mb)
int&avmem_mb,int&tmpmem_mb)
{
int nthreads= THREAD_GROUP::max_parallel_threads;
long int per_size1= tdims.calcUnfoldMaxOffset();
long int per_size2= (long int)pow((double)tdims.getNVS().getMax(),l);
double lambda= 0.0;
long int per_size= sizeof(double)*nr
*(long int)(lambda*per_size1+(1-lambda)*per_size2);
long int mem= SystemResources::availableMemory();
int max= 0;
double num_cols= ((double)(mem-magic_mult*nthreads*per_size))
/nthreads/sizeof(double)/nr;
if(num_cols> 0){
double maxd= pow(num_cols,((double)1)/l);
max= (int)floor(maxd);
}
if(max==0){
max= 10;
JournalRecord rec(journal);
rec<<"dim="<<l<<" run out of memory, imposing max="<<max;
if(nthreads> 1)
rec<<" (decrease number of threads)";
rec<<endrec;
}
avmem_mb= mem/1024/1024;
tmpmem_mb= (nthreads*per_size)/1024/1024;
return max;
int nthreads= THREAD_GROUP::max_parallel_threads;
long int per_size1= tdims.calcUnfoldMaxOffset();
long int per_size2= (long int)pow((double)tdims.getNVS().getMax(),l);
double lambda= 0.0;
long int per_size= sizeof(double)*nr
*(long int)(lambda*per_size1+(1-lambda)*per_size2);
long int mem= SystemResources::availableMemory();
int max= 0;
double num_cols= ((double)(mem-magic_mult*nthreads*per_size))
/nthreads/sizeof(double)/nr;
if(num_cols> 0){
double maxd= pow(num_cols,((double)1)/l);
max= (int)floor(maxd);
}
if(max==0){
max= 10;
JournalRecord rec(journal);
rec<<"dim="<<l<<" run out of memory, imposing max="<<max;
if(nthreads> 1)
rec<<" (decrease number of threads)";
rec<<endrec;
}
avmem_mb= mem/1024/1024;
tmpmem_mb= (nthreads*per_size)/1024/1024;
return max;
}
/*:6*/
#line 20 "./faa_di_bruno.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 13 "./faa_di_bruno.hweb"
#ifndef FAA_DI_BRUNO_H
#define FAA_DI_BRUNO_H
@ -11,28 +10,26 @@
#include "gs_tensor.h"
/*2:*/
#line 30 "./faa_di_bruno.hweb"
class FaaDiBruno{
Journal&journal;
Journal&journal;
public:
FaaDiBruno(Journal&jr)
:journal(jr){}
void calculate(const StackContainer<FGSTensor> &cont,const TensorContainer<FSSparseTensor> &f,
FGSTensor&out);
void calculate(const FoldedStackContainer&cont,const FGSContainer&g,
FGSTensor&out);
void calculate(const StackContainer<UGSTensor> &cont,const TensorContainer<FSSparseTensor> &f,
UGSTensor&out);
void calculate(const UnfoldedStackContainer&cont,const UGSContainer&g,
UGSTensor&out);
FaaDiBruno(Journal&jr)
:journal(jr){}
void calculate(const StackContainer<FGSTensor> &cont,const TensorContainer<FSSparseTensor> &f,
FGSTensor&out);
void calculate(const FoldedStackContainer&cont,const FGSContainer&g,
FGSTensor&out);
void calculate(const StackContainer<UGSTensor> &cont,const TensorContainer<FSSparseTensor> &f,
UGSTensor&out);
void calculate(const UnfoldedStackContainer&cont,const UGSContainer&g,
UGSTensor&out);
protected:
int estimRefinment(const TensorDimens&tdims,int nr,int l,int&avmem_mb,int&tmpmem_mb);
static double magic_mult;
int estimRefinment(const TensorDimens&tdims,int nr,int l,int&avmem_mb,int&tmpmem_mb);
static double magic_mult;
};
/*:2*/
#line 23 "./faa_di_bruno.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./first_order.cweb"
#include "kord_exception.h"
@ -7,227 +6,199 @@
#include "cpplapack.h"
/*2:*/
#line 20 "./first_order.cweb"
int order_eigs(const double*alphar,const double*alphai,const double*beta)
{
return(*alphar**alphar+*alphai**alphai<*beta**beta);
return(*alphar**alphar+*alphai**alphai<*beta**beta);
}
/*:2*/
#line 12 "./first_order.cweb"
;
/*3:*/
#line 40 "./first_order.cweb"
void FirstOrder::solve(const TwoDMatrix&fd)
{
JournalRecordPair pa(journal);
pa<<"Recovering first order derivatives "<<endrec;
/*4:*/
#line 132 "./first_order.cweb"
/*5:*/
#line 145 "./first_order.cweb"
int off= 0;
ConstTwoDMatrix fyplus(fd,off,ypart.nyss());
off+= ypart.nyss();
ConstTwoDMatrix fyszero(fd,off,ypart.nstat);
off+= ypart.nstat;
ConstTwoDMatrix fypzero(fd,off,ypart.npred);
off+= ypart.npred;
ConstTwoDMatrix fybzero(fd,off,ypart.nboth);
off+= ypart.nboth;
ConstTwoDMatrix fyfzero(fd,off,ypart.nforw);
off+= ypart.nforw;
ConstTwoDMatrix fymins(fd,off,ypart.nys());
off+= ypart.nys();
ConstTwoDMatrix fuzero(fd,off,nu);
off+= nu;
/*:5*/
#line 133 "./first_order.cweb"
;
/*6:*/
#line 163 "./first_order.cweb"
int n= ypart.ny()+ypart.nboth;
TwoDMatrix matD(n,n);
matD.zeros();
matD.place(fypzero,0,0);
matD.place(fybzero,0,ypart.npred);
matD.place(fyplus,0,ypart.nys()+ypart.nstat);
for(int i= 0;i<ypart.nboth;i++)
matD.get(ypart.ny()+i,ypart.npred+i)= 1.0;
/*:6*/
#line 134 "./first_order.cweb"
;
/*7:*/
#line 175 "./first_order.cweb"
TwoDMatrix matE(n,n);
matE.zeros();
matE.place(fymins,0,0);
matE.place(fyszero,0,ypart.nys());
matE.place(fyfzero,0,ypart.nys()+ypart.nstat+ypart.nboth);
for(int i= 0;i<ypart.nboth;i++)
matE.get(ypart.ny()+i,ypart.nys()+ypart.nstat+i)= -1.0;
matE.mult(-1.0);
/*:7*/
#line 135 "./first_order.cweb"
;
/*8:*/
#line 186 "./first_order.cweb"
TwoDMatrix vsl(n,n);
TwoDMatrix vsr(n,n);
int lwork= 100*n+16;
Vector work(lwork);
IntSequence bwork(n);
int info;
LAPACK_dgges("N","V","S",order_eigs,&n,matE.getData().base(),&n,
matD.getData().base(),&n,&sdim,alphar.base(),alphai.base(),
beta.base(),vsl.getData().base(),&n,vsr.getData().base(),&n,
work.base(),&lwork,&(bwork[0]),&info);
bk_cond= (sdim==ypart.nys());
/*:8*/
#line 136 "./first_order.cweb"
;
/*9:*/
#line 201 "./first_order.cweb"
ConstGeneralMatrix z11(vsr,0,0,ypart.nys(),ypart.nys());
ConstGeneralMatrix z12(vsr,0,ypart.nys(),ypart.nys(),n-ypart.nys());
ConstGeneralMatrix z21(vsr,ypart.nys(),0,n-ypart.nys(),ypart.nys());
ConstGeneralMatrix z22(vsr,ypart.nys(),ypart.nys(),n-ypart.nys(),n-ypart.nys());
/*:9*/
#line 137 "./first_order.cweb"
;
/*10:*/
#line 208 "./first_order.cweb"
GeneralMatrix sfder(z12,"transpose");
z22.multInvLeftTrans(sfder);
sfder.mult(-1);
/*:10*/
#line 138 "./first_order.cweb"
;
/*11:*/
#line 217 "./first_order.cweb"
ConstGeneralMatrix s11(matE,0,0,ypart.nys(),ypart.nys());
ConstGeneralMatrix t11(matD,0,0,ypart.nys(),ypart.nys());
GeneralMatrix dumm(s11,"transpose");
z11.multInvLeftTrans(dumm);
GeneralMatrix preder(dumm,"transpose");
t11.multInvLeft(preder);
preder.multLeft(z11);
/*:11*/
#line 139 "./first_order.cweb"
;
/*12:*/
#line 227 "./first_order.cweb"
gy.place(preder,ypart.nstat,0);
GeneralMatrix sder(sfder,0,0,ypart.nstat,ypart.nys());
gy.place(sder,0,0);
GeneralMatrix fder(sfder,ypart.nstat+ypart.nboth,0,ypart.nforw,ypart.nys());
gy.place(fder,ypart.nstat+ypart.nys(),0);
/*:12*/
#line 140 "./first_order.cweb"
;
/*13:*/
#line 235 "./first_order.cweb"
GeneralMatrix bder((const GeneralMatrix&)sfder,ypart.nstat,0,ypart.nboth,ypart.nys());
GeneralMatrix bder2(preder,ypart.npred,0,ypart.nboth,ypart.nys());
bder.add(-1,bder2);
b_error= bder.getData().getMax();
/*:13*/
#line 141 "./first_order.cweb"
;
/*:4*/
#line 46 "./first_order.cweb"
;
/*14:*/
#line 255 "./first_order.cweb"
GeneralMatrix matA(ypart.ny(),ypart.ny());
matA.zeros();
ConstGeneralMatrix gss(gy,ypart.nstat+ypart.npred,0,ypart.nyss(),ypart.nys());
GeneralMatrix aux(fyplus,gss);
matA.place(aux,0,ypart.nstat);
ConstGeneralMatrix fyzero(fd,0,ypart.nyss(),ypart.ny(),ypart.ny());
matA.add(1.0,fyzero);
gu.zeros();
gu.add(-1.0,fuzero);
ConstGeneralMatrix(matA).multInvLeft(gu);
/*:14*/
#line 47 "./first_order.cweb"
;
journalEigs();
if(!gy.isFinite()||!gu.isFinite()){
throw KordException(__FILE__,__LINE__,
"NaN or Inf asserted in first order derivatives in FirstOrder::solve");
}
JournalRecordPair pa(journal);
pa<<"Recovering first order derivatives "<<endrec;
/*4:*/
/*5:*/
int off= 0;
ConstTwoDMatrix fyplus(fd,off,ypart.nyss());
off+= ypart.nyss();
ConstTwoDMatrix fyszero(fd,off,ypart.nstat);
off+= ypart.nstat;
ConstTwoDMatrix fypzero(fd,off,ypart.npred);
off+= ypart.npred;
ConstTwoDMatrix fybzero(fd,off,ypart.nboth);
off+= ypart.nboth;
ConstTwoDMatrix fyfzero(fd,off,ypart.nforw);
off+= ypart.nforw;
ConstTwoDMatrix fymins(fd,off,ypart.nys());
off+= ypart.nys();
ConstTwoDMatrix fuzero(fd,off,nu);
off+= nu;
/*:5*/
;
/*6:*/
int n= ypart.ny()+ypart.nboth;
TwoDMatrix matD(n,n);
matD.zeros();
matD.place(fypzero,0,0);
matD.place(fybzero,0,ypart.npred);
matD.place(fyplus,0,ypart.nys()+ypart.nstat);
for(int i= 0;i<ypart.nboth;i++)
matD.get(ypart.ny()+i,ypart.npred+i)= 1.0;
/*:6*/
;
/*7:*/
TwoDMatrix matE(n,n);
matE.zeros();
matE.place(fymins,0,0);
matE.place(fyszero,0,ypart.nys());
matE.place(fyfzero,0,ypart.nys()+ypart.nstat+ypart.nboth);
for(int i= 0;i<ypart.nboth;i++)
matE.get(ypart.ny()+i,ypart.nys()+ypart.nstat+i)= -1.0;
matE.mult(-1.0);
/*:7*/
;
/*8:*/
TwoDMatrix vsl(n,n);
TwoDMatrix vsr(n,n);
int lwork= 100*n+16;
Vector work(lwork);
IntSequence bwork(n);
int info;
LAPACK_dgges("N","V","S",order_eigs,&n,matE.getData().base(),&n,
matD.getData().base(),&n,&sdim,alphar.base(),alphai.base(),
beta.base(),vsl.getData().base(),&n,vsr.getData().base(),&n,
work.base(),&lwork,&(bwork[0]),&info);
bk_cond= (sdim==ypart.nys());
/*:8*/
;
/*9:*/
ConstGeneralMatrix z11(vsr,0,0,ypart.nys(),ypart.nys());
ConstGeneralMatrix z12(vsr,0,ypart.nys(),ypart.nys(),n-ypart.nys());
ConstGeneralMatrix z21(vsr,ypart.nys(),0,n-ypart.nys(),ypart.nys());
ConstGeneralMatrix z22(vsr,ypart.nys(),ypart.nys(),n-ypart.nys(),n-ypart.nys());
/*:9*/
;
/*10:*/
GeneralMatrix sfder(z12,"transpose");
z22.multInvLeftTrans(sfder);
sfder.mult(-1);
/*:10*/
;
/*11:*/
ConstGeneralMatrix s11(matE,0,0,ypart.nys(),ypart.nys());
ConstGeneralMatrix t11(matD,0,0,ypart.nys(),ypart.nys());
GeneralMatrix dumm(s11,"transpose");
z11.multInvLeftTrans(dumm);
GeneralMatrix preder(dumm,"transpose");
t11.multInvLeft(preder);
preder.multLeft(z11);
/*:11*/
;
/*12:*/
gy.place(preder,ypart.nstat,0);
GeneralMatrix sder(sfder,0,0,ypart.nstat,ypart.nys());
gy.place(sder,0,0);
GeneralMatrix fder(sfder,ypart.nstat+ypart.nboth,0,ypart.nforw,ypart.nys());
gy.place(fder,ypart.nstat+ypart.nys(),0);
/*:12*/
;
/*13:*/
GeneralMatrix bder((const GeneralMatrix&)sfder,ypart.nstat,0,ypart.nboth,ypart.nys());
GeneralMatrix bder2(preder,ypart.npred,0,ypart.nboth,ypart.nys());
bder.add(-1,bder2);
b_error= bder.getData().getMax();
/*:13*/
;
/*:4*/
;
/*14:*/
GeneralMatrix matA(ypart.ny(),ypart.ny());
matA.zeros();
ConstGeneralMatrix gss(gy,ypart.nstat+ypart.npred,0,ypart.nyss(),ypart.nys());
GeneralMatrix aux(fyplus,gss);
matA.place(aux,0,ypart.nstat);
ConstGeneralMatrix fyzero(fd,0,ypart.nyss(),ypart.ny(),ypart.ny());
matA.add(1.0,fyzero);
gu.zeros();
gu.add(-1.0,fuzero);
ConstGeneralMatrix(matA).multInvLeft(gu);
/*:14*/
;
journalEigs();
if(!gy.isFinite()||!gu.isFinite()){
throw KordException(__FILE__,__LINE__,
"NaN or Inf asserted in first order derivatives in FirstOrder::solve");
}
}
/*:3*/
#line 13 "./first_order.cweb"
;
/*15:*/
#line 268 "./first_order.cweb"
void FirstOrder::journalEigs()
{
if(bk_cond){
JournalRecord jr(journal);
jr<<"Blanchard-Kahn conditition satisfied, model stable"<<endrec;
}else{
JournalRecord jr(journal);
jr<<"Blanchard-Kahn condition not satisfied, model not stable: sdim="<<sdim
<<" "<<"npred="<<ypart.nys()<<endrec;
}
if(!bk_cond){
for(int i= 0;i<alphar.length();i++){
if(i==sdim||i==ypart.nys()){
JournalRecord jr(journal);
jr<<"---------------------------------------------------- ";
if(i==sdim)
jr<<"sdim";
else
jr<<"npred";
jr<<endrec;
}
JournalRecord jr(journal);
double mod= sqrt(alphar[i]*alphar[i]+alphai[i]*alphai[i]);
mod= mod/round(100000*std::abs(beta[i]))*100000;
jr<<i<<"\t("<<alphar[i]<<","<<alphai[i]<<") / "<<beta[i]
<<" \t"<<mod<<endrec;
}
}
if(bk_cond){
JournalRecord jr(journal);
jr<<"Blanchard-Kahn conditition satisfied, model stable"<<endrec;
}else{
JournalRecord jr(journal);
jr<<"Blanchard-Kahn condition not satisfied, model not stable: sdim="<<sdim
<<" "<<"npred="<<ypart.nys()<<endrec;
}
if(!bk_cond){
for(int i= 0;i<alphar.length();i++){
if(i==sdim||i==ypart.nys()){
JournalRecord jr(journal);
jr<<"---------------------------------------------------- ";
if(i==sdim)
jr<<"sdim";
else
jr<<"npred";
jr<<endrec;
}
JournalRecord jr(journal);
double mod= sqrt(alphar[i]*alphar[i]+alphai[i]*alphai[i]);
mod= mod/round(100000*std::abs(beta[i]))*100000;
jr<<i<<"\t("<<alphar[i]<<","<<alphai[i]<<") / "<<beta[i]
<<" \t"<<mod<<endrec;
}
}
}
/*:15*/
#line 14 "./first_order.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 10 "./first_order.hweb"
#ifndef FIRST_ORDER_H
@ -8,71 +7,67 @@
#include "korder.h"
/*2:*/
#line 23 "./first_order.hweb"
template<int> class FirstOrderDerivs;
class FirstOrder{
template<int> friend class FirstOrderDerivs;
PartitionY ypart;
int nu;
TwoDMatrix gy;
TwoDMatrix gu;
bool bk_cond;
double b_error;
int sdim;
Vector alphar;
Vector alphai;
Vector beta;
Journal&journal;
template<int> friend class FirstOrderDerivs;
PartitionY ypart;
int nu;
TwoDMatrix gy;
TwoDMatrix gu;
bool bk_cond;
double b_error;
int sdim;
Vector alphar;
Vector alphai;
Vector beta;
Journal&journal;
public:
FirstOrder(int num_stat,int num_pred,int num_both,int num_forw,
int num_u,const FSSparseTensor&f,Journal&jr)
:ypart(num_stat,num_pred,num_both,num_forw),
nu(num_u),
gy(ypart.ny(),ypart.nys()),
gu(ypart.ny(),nu),
alphar(ypart.ny()+ypart.nboth),
alphai(ypart.ny()+ypart.nboth),
beta(ypart.ny()+ypart.nboth),
journal(jr)
{solve(FFSTensor(f));}
bool isStable()const
{return bk_cond;}
const TwoDMatrix&getGy()const
{return gy;}
const TwoDMatrix&getGu()const
{return gu;}
FirstOrder(int num_stat,int num_pred,int num_both,int num_forw,
int num_u,const FSSparseTensor&f,Journal&jr)
:ypart(num_stat,num_pred,num_both,num_forw),
nu(num_u),
gy(ypart.ny(),ypart.nys()),
gu(ypart.ny(),nu),
alphar(ypart.ny()+ypart.nboth),
alphai(ypart.ny()+ypart.nboth),
beta(ypart.ny()+ypart.nboth),
journal(jr)
{solve(FFSTensor(f));}
bool isStable()const
{return bk_cond;}
const TwoDMatrix&getGy()const
{return gy;}
const TwoDMatrix&getGu()const
{return gu;}
protected:
void solve(const TwoDMatrix&f);
void journalEigs();
void solve(const TwoDMatrix&f);
void journalEigs();
};
/*:2*/
#line 17 "./first_order.hweb"
;
/*3:*/
#line 64 "./first_order.hweb"
template<int t>
class FirstOrderDerivs:public ctraits<t> ::Tg{
public:
FirstOrderDerivs(const FirstOrder&fo)
:ctraits<t> ::Tg(4)
{
IntSequence nvs(4);
nvs[0]= fo.ypart.nys();nvs[1]= fo.nu;nvs[2]= fo.nu;nvs[3]= 1;
_Ttensor*ten= new _Ttensor(fo.ypart.ny(),TensorDimens(Symmetry(1,0,0,0),nvs));
ten->zeros();ten->add(1.0,fo.gy);
insert(ten);
ten= new _Ttensor(fo.ypart.ny(),TensorDimens(Symmetry(0,1,0,0),nvs));
ten->zeros();ten->add(1.0,fo.gu);
insert(ten);
}
FirstOrderDerivs(const FirstOrder&fo)
:ctraits<t> ::Tg(4)
{
IntSequence nvs(4);
nvs[0]= fo.ypart.nys();nvs[1]= fo.nu;nvs[2]= fo.nu;nvs[3]= 1;
_Ttensor*ten= new _Ttensor(fo.ypart.ny(),TensorDimens(Symmetry(1,0,0,0),nvs));
ten->zeros();ten->add(1.0,fo.gy);
insert(ten);
ten= new _Ttensor(fo.ypart.ny(),TensorDimens(Symmetry(0,1,0,0),nvs));
ten->zeros();ten->add(1.0,fo.gu);
insert(ten);
}
};
/*:3*/
#line 18 "./first_order.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./global_check.cweb"
#include "SymSchurDecomp.h"
@ -12,7 +11,6 @@
#include "cpplapack.h"
/*2:*/
#line 31 "./global_check.cweb"
ResidFunction::ResidFunction(const Approximation&app)
:VectorFunction(app.getModel().nexog(),app.getModel().numeq()),approx(app),
@ -22,448 +20,399 @@ yplus(NULL),ystar(NULL),u(NULL),hss(NULL)
}
/*:2*/
#line 17 "./global_check.cweb"
;
/*3:*/
#line 40 "./global_check.cweb"
ResidFunction::ResidFunction(const ResidFunction&rf)
:VectorFunction(rf),approx(rf.approx),model(rf.model->clone()),
yplus(NULL),ystar(NULL),u(NULL),hss(NULL)
{
if(rf.yplus)
yplus= new Vector(*(rf.yplus));
if(rf.ystar)
ystar= new Vector(*(rf.ystar));
if(rf.u)
u= new Vector(*(rf.u));
if(rf.hss)
hss= new FTensorPolynomial(*(rf.hss));
if(rf.yplus)
yplus= new Vector(*(rf.yplus));
if(rf.ystar)
ystar= new Vector(*(rf.ystar));
if(rf.u)
u= new Vector(*(rf.u));
if(rf.hss)
hss= new FTensorPolynomial(*(rf.hss));
}
/*:3*/
#line 18 "./global_check.cweb"
;
/*4:*/
#line 57 "./global_check.cweb"
ResidFunction::~ResidFunction()
{
delete model;
/*5:*/
#line 65 "./global_check.cweb"
if(yplus)
delete yplus;
if(ystar)
delete ystar;
if(u)
delete u;
if(hss)
delete hss;
/*:5*/
#line 61 "./global_check.cweb"
;
delete model;
/*5:*/
if(yplus)
delete yplus;
if(ystar)
delete ystar;
if(u)
delete u;
if(hss)
delete hss;
/*:5*/
;
}
/*:4*/
#line 19 "./global_check.cweb"
;
/*6:*/
#line 79 "./global_check.cweb"
void ResidFunction::setYU(const Vector&ys,const Vector&xx)
{
/*5:*/
#line 65 "./global_check.cweb"
if(yplus)
delete yplus;
if(ystar)
delete ystar;
if(u)
delete u;
if(hss)
delete hss;
/*:5*/
#line 82 "./global_check.cweb"
;
ystar= new Vector(ys);
u= new Vector(xx);
yplus= new Vector(model->numeq());
approx.getFoldDecisionRule().evaluate(DecisionRule::horner,
*yplus,*ystar,*u);
/*7:*/
#line 103 "./global_check.cweb"
union{const FoldDecisionRule*c;FoldDecisionRule*n;}dr;
dr.c= &(approx.getFoldDecisionRule());
FTensorPolynomial dr_ss(model->nstat()+model->npred(),model->nboth()+model->nforw(),
*(dr.n));
/*:7*/
#line 90 "./global_check.cweb"
;
/*8:*/
#line 110 "./global_check.cweb"
Vector ytmp_star(ConstVector(*yplus,model->nstat(),model->npred()+model->nboth()));
ConstVector ysteady_star(dr.c->getSteady(),model->nstat(),
model->npred()+model->nboth());
ytmp_star.add(-1.0,ysteady_star);
/*:8*/
#line 91 "./global_check.cweb"
;
/*9:*/
#line 117 "./global_check.cweb"
hss= new FTensorPolynomial(dr_ss,ytmp_star);
ConstVector ysteady_ss(dr.c->getSteady(),model->nstat()+model->npred(),
model->nboth()+model->nforw());
if(hss->check(Symmetry(0))){
hss->get(Symmetry(0))->getData().add(1.0,ysteady_ss);
}else{
FFSTensor*ten= new FFSTensor(hss->nrows(),hss->nvars(),0);
ten->getData()= ysteady_ss;
hss->insert(ten);
}
/*:9*/
#line 92 "./global_check.cweb"
;
/*5:*/
if(yplus)
delete yplus;
if(ystar)
delete ystar;
if(u)
delete u;
if(hss)
delete hss;
/*:5*/
;
ystar= new Vector(ys);
u= new Vector(xx);
yplus= new Vector(model->numeq());
approx.getFoldDecisionRule().evaluate(DecisionRule::horner,
*yplus,*ystar,*u);
/*7:*/
union{const FoldDecisionRule*c;FoldDecisionRule*n;}dr;
dr.c= &(approx.getFoldDecisionRule());
FTensorPolynomial dr_ss(model->nstat()+model->npred(),model->nboth()+model->nforw(),
*(dr.n));
/*:7*/
;
/*8:*/
Vector ytmp_star(ConstVector(*yplus,model->nstat(),model->npred()+model->nboth()));
ConstVector ysteady_star(dr.c->getSteady(),model->nstat(),
model->npred()+model->nboth());
ytmp_star.add(-1.0,ysteady_star);
/*:8*/
;
/*9:*/
hss= new FTensorPolynomial(dr_ss,ytmp_star);
ConstVector ysteady_ss(dr.c->getSteady(),model->nstat()+model->npred(),
model->nboth()+model->nforw());
if(hss->check(Symmetry(0))){
hss->get(Symmetry(0))->getData().add(1.0,ysteady_ss);
}else{
FFSTensor*ten= new FFSTensor(hss->nrows(),hss->nvars(),0);
ten->getData()= ysteady_ss;
hss->insert(ten);
}
/*:9*/
;
}
/*:6*/
#line 20 "./global_check.cweb"
;
/*10:*/
#line 132 "./global_check.cweb"
void ResidFunction::eval(const Vector&point,const ParameterSignal&sig,Vector&out)
{
KORD_RAISE_IF(point.length()!=hss->nvars(),
"Wrong dimension of input vector in ResidFunction::eval");
KORD_RAISE_IF(out.length()!=model->numeq(),
"Wrong dimension of output vector in ResidFunction::eval");
Vector yss(hss->nrows());
hss->evalHorner(yss,point);
model->evaluateSystem(out,*ystar,*yplus,yss,*u);
KORD_RAISE_IF(point.length()!=hss->nvars(),
"Wrong dimension of input vector in ResidFunction::eval");
KORD_RAISE_IF(out.length()!=model->numeq(),
"Wrong dimension of output vector in ResidFunction::eval");
Vector yss(hss->nrows());
hss->evalHorner(yss,point);
model->evaluateSystem(out,*ystar,*yplus,yss,*u);
}
/*:10*/
#line 21 "./global_check.cweb"
;
/*11:*/
#line 148 "./global_check.cweb"
void GlobalChecker::check(const Quadrature&quad,int level,
const ConstVector&ys,const ConstVector&x,Vector&out)
const ConstVector&ys,const ConstVector&x,Vector&out)
{
for(int ifunc= 0;ifunc<vfs.getNum();ifunc++)
((GResidFunction&)(vfs.getFunc(ifunc))).setYU(ys,x);
quad.integrate(vfs,level,out);
for(int ifunc= 0;ifunc<vfs.getNum();ifunc++)
((GResidFunction&)(vfs.getFunc(ifunc))).setYU(ys,x);
quad.integrate(vfs,level,out);
}
/*:11*/
#line 22 "./global_check.cweb"
;
/*12:*/
#line 165 "./global_check.cweb"
void GlobalChecker::check(int max_evals,const ConstTwoDMatrix&y,
const ConstTwoDMatrix&x,TwoDMatrix&out)
const ConstTwoDMatrix&x,TwoDMatrix&out)
{
JournalRecordPair pa(journal);
pa<<"Checking approximation error for "<<y.ncols()
<<" states with at most "<<max_evals<<" evaluations"<<endrec;
/*13:*/
#line 182 "./global_check.cweb"
GaussHermite gh;
SmolyakQuadrature dummy_sq(model.nexog(),1,gh);
int smol_evals;
int smol_level;
dummy_sq.designLevelForEvals(max_evals,smol_level,smol_evals);
ProductQuadrature dummy_pq(model.nexog(),gh);
int prod_evals;
int prod_level;
dummy_pq.designLevelForEvals(max_evals,prod_level,prod_evals);
bool take_smolyak= (smol_evals<prod_evals)&&(smol_level>=prod_level-1);
/*:13*/
#line 173 "./global_check.cweb"
;
Quadrature*quad;
int lev;
/*14:*/
#line 198 "./global_check.cweb"
if(take_smolyak){
quad= new SmolyakQuadrature(model.nexog(),smol_level,gh);
lev= smol_level;
JournalRecord rec(journal);
rec<<"Selected Smolyak (level,evals)=("<<smol_level<<","
<<smol_evals<<") over product ("<<prod_level<<","
<<prod_evals<<")"<<endrec;
}else{
quad= new ProductQuadrature(model.nexog(),gh);
lev= prod_level;
JournalRecord rec(journal);
rec<<"Selected product (level,evals)=("<<prod_level<<","
<<prod_evals<<") over Smolyak ("<<smol_level<<","
<<smol_evals<<")"<<endrec;
}
/*:14*/
#line 176 "./global_check.cweb"
;
/*15:*/
#line 216 "./global_check.cweb"
int first_row= (y.nrows()==model.numeq())?model.nstat():0;
ConstTwoDMatrix ysmat(y,first_row,0,model.npred()+model.nboth(),y.ncols());
for(int j= 0;j<y.ncols();j++){
ConstVector yj(ysmat,j);
ConstVector xj(x,j);
Vector outj(out,j);
check(*quad,lev,yj,xj,outj);
}
/*:15*/
#line 177 "./global_check.cweb"
;
delete quad;
JournalRecordPair pa(journal);
pa<<"Checking approximation error for "<<y.ncols()
<<" states with at most "<<max_evals<<" evaluations"<<endrec;
/*13:*/
GaussHermite gh;
SmolyakQuadrature dummy_sq(model.nexog(),1,gh);
int smol_evals;
int smol_level;
dummy_sq.designLevelForEvals(max_evals,smol_level,smol_evals);
ProductQuadrature dummy_pq(model.nexog(),gh);
int prod_evals;
int prod_level;
dummy_pq.designLevelForEvals(max_evals,prod_level,prod_evals);
bool take_smolyak= (smol_evals<prod_evals)&&(smol_level>=prod_level-1);
/*:13*/
;
Quadrature*quad;
int lev;
/*14:*/
if(take_smolyak){
quad= new SmolyakQuadrature(model.nexog(),smol_level,gh);
lev= smol_level;
JournalRecord rec(journal);
rec<<"Selected Smolyak (level,evals)=("<<smol_level<<","
<<smol_evals<<") over product ("<<prod_level<<","
<<prod_evals<<")"<<endrec;
}else{
quad= new ProductQuadrature(model.nexog(),gh);
lev= prod_level;
JournalRecord rec(journal);
rec<<"Selected product (level,evals)=("<<prod_level<<","
<<prod_evals<<") over Smolyak ("<<smol_level<<","
<<smol_evals<<")"<<endrec;
}
/*:14*/
;
/*15:*/
int first_row= (y.nrows()==model.numeq())?model.nstat():0;
ConstTwoDMatrix ysmat(y,first_row,0,model.npred()+model.nboth(),y.ncols());
for(int j= 0;j<y.ncols();j++){
ConstVector yj(ysmat,j);
ConstVector xj(x,j);
Vector outj(out,j);
check(*quad,lev,yj,xj,outj);
}
/*:15*/
;
delete quad;
}
/*:12*/
#line 23 "./global_check.cweb"
;
/*16:*/
#line 233 "./global_check.cweb"
void GlobalChecker::checkAlongShocksAndSave(FILE*fd,const char*prefix,
int m,double mult,int max_evals)
int m,double mult,int max_evals)
{
JournalRecordPair pa(journal);
pa<<"Calculating errors along shocks +/- "
<<mult<<" std errors, granularity "<<m<<endrec;
/*17:*/
#line 250 "./global_check.cweb"
TwoDMatrix y_mat(model.numeq(),2*m*model.nexog()+1);
for(int j= 0;j<2*m*model.nexog()+1;j++){
Vector yj(y_mat,j);
yj= (const Vector&)model.getSteady();
}
/*:17*/
#line 240 "./global_check.cweb"
;
/*18:*/
#line 258 "./global_check.cweb"
TwoDMatrix exo_mat(model.nexog(),2*m*model.nexog()+1);
exo_mat.zeros();
for(int ishock= 0;ishock<model.nexog();ishock++){
double max_sigma= sqrt(model.getVcov().get(ishock,ishock));
for(int j= 0;j<2*m;j++){
int jmult= (j<m)?j-m:j-m+1;
exo_mat.get(ishock,1+2*m*ishock+j)=
mult*jmult*max_sigma/m;
}
}
/*:18*/
#line 241 "./global_check.cweb"
;
TwoDMatrix errors(model.numeq(),2*m*model.nexog()+1);
check(max_evals,y_mat,exo_mat,errors);
/*19:*/
#line 271 "./global_check.cweb"
TwoDMatrix res(model.nexog(),2*m+1);
JournalRecord rec(journal);
rec<<"Shock value error"<<endrec;
ConstVector err0(errors,0);
char shock[9];
char erbuf[17];
for(int ishock= 0;ishock<model.nexog();ishock++){
TwoDMatrix err_out(model.numeq(),2*m+1);
sprintf(shock,"%-8s",model.getExogNames().getName(ishock));
for(int j= 0;j<2*m+1;j++){
int jj;
Vector error(err_out,j);
if(j!=m){
if(j<m)
jj= 1+2*m*ishock+j;
else
jj= 1+2*m*ishock+j-1;
ConstVector coljj(errors,jj);
error= coljj;
}else{
jj= 0;
error= err0;
}
JournalRecord rec1(journal);
sprintf(erbuf,"%12.7g ",error.getMax());
rec1<<shock<<" "<<exo_mat.get(ishock,jj)
<<"\t"<<erbuf<<endrec;
}
char tmp[100];
sprintf(tmp,"%s_shock_%s_errors",prefix,model.getExogNames().getName(ishock));
err_out.writeMat4(fd,tmp);
}
/*:19*/
#line 246 "./global_check.cweb"
;
JournalRecordPair pa(journal);
pa<<"Calculating errors along shocks +/- "
<<mult<<" std errors, granularity "<<m<<endrec;
/*17:*/
TwoDMatrix y_mat(model.numeq(),2*m*model.nexog()+1);
for(int j= 0;j<2*m*model.nexog()+1;j++){
Vector yj(y_mat,j);
yj= (const Vector&)model.getSteady();
}
/*:17*/
;
/*18:*/
TwoDMatrix exo_mat(model.nexog(),2*m*model.nexog()+1);
exo_mat.zeros();
for(int ishock= 0;ishock<model.nexog();ishock++){
double max_sigma= sqrt(model.getVcov().get(ishock,ishock));
for(int j= 0;j<2*m;j++){
int jmult= (j<m)?j-m:j-m+1;
exo_mat.get(ishock,1+2*m*ishock+j)=
mult*jmult*max_sigma/m;
}
}
/*:18*/
;
TwoDMatrix errors(model.numeq(),2*m*model.nexog()+1);
check(max_evals,y_mat,exo_mat,errors);
/*19:*/
TwoDMatrix res(model.nexog(),2*m+1);
JournalRecord rec(journal);
rec<<"Shock value error"<<endrec;
ConstVector err0(errors,0);
char shock[9];
char erbuf[17];
for(int ishock= 0;ishock<model.nexog();ishock++){
TwoDMatrix err_out(model.numeq(),2*m+1);
sprintf(shock,"%-8s",model.getExogNames().getName(ishock));
for(int j= 0;j<2*m+1;j++){
int jj;
Vector error(err_out,j);
if(j!=m){
if(j<m)
jj= 1+2*m*ishock+j;
else
jj= 1+2*m*ishock+j-1;
ConstVector coljj(errors,jj);
error= coljj;
}else{
jj= 0;
error= err0;
}
JournalRecord rec1(journal);
sprintf(erbuf,"%12.7g ",error.getMax());
rec1<<shock<<" "<<exo_mat.get(ishock,jj)
<<"\t"<<erbuf<<endrec;
}
char tmp[100];
sprintf(tmp,"%s_shock_%s_errors",prefix,model.getExogNames().getName(ishock));
err_out.writeMat4(fd,tmp);
}
/*:19*/
;
}
/*:16*/
#line 24 "./global_check.cweb"
;
/*20:*/
#line 320 "./global_check.cweb"
void GlobalChecker::checkOnEllipseAndSave(FILE*fd,const char*prefix,
int m,double mult,int max_evals)
int m,double mult,int max_evals)
{
JournalRecordPair pa(journal);
pa<<"Calculating errors at "<<m
<<" ellipse points scaled by "<<mult<<endrec;
/*21:*/
#line 338 "./global_check.cweb"
TwoDMatrix*ycov= approx.calcYCov();
TwoDMatrix ycovpred((const TwoDMatrix&)*ycov,model.nstat(),model.nstat(),
model.npred()+model.nboth(),model.npred()+model.nboth());
delete ycov;
SymSchurDecomp ssd(ycovpred);
ssd.correctDefinitness(1.e-05);
TwoDMatrix ycovfac(ycovpred.nrows(),ycovpred.ncols());
KORD_RAISE_IF(!ssd.isPositiveSemidefinite(),
"Covariance matrix of the states not positive \
semidefinite in GlobalChecker::checkOnEllipseAndSave");
ssd.getFactor(ycovfac);
/*:21*/
#line 327 "./global_check.cweb"
;
/*22:*/
#line 363 "./global_check.cweb"
int d= model.npred()+model.nboth()-1;
TwoDMatrix ymat(model.npred()+model.nboth(),(d==0)?2:m);
if(d==0){
ymat.get(0,0)= 1;
ymat.get(0,1)= -1;
}else{
int icol= 0;
ReversePerScheme ps;
QMCarloCubeQuadrature qmc(d,m,ps);
qmcpit beg= qmc.start(m);
qmcpit end= qmc.end(m);
for(qmcpit run= beg;run!=end;++run,icol++){
Vector ycol(ymat,icol);
Vector x(run.point());
x.mult(2*M_PI);
ycol[0]= 1;
for(int i= 0;i<d;i++){
Vector subsphere(ycol,0,i+1);
subsphere.mult(cos(x[i]));
ycol[i+1]= sin(x[i]);
}
}
}
/*:22*/
#line 328 "./global_check.cweb"
;
/*23:*/
#line 392 "./global_check.cweb"
TwoDMatrix umat(model.nexog(),ymat.ncols());
umat.zeros();
ymat.mult(mult);
ymat.multLeft(ycovfac);
ConstVector ys(model.getSteady(),model.nstat(),
model.npred()+model.nboth());
for(int icol= 0;icol<ymat.ncols();icol++){
Vector ycol(ymat,icol);
ycol.add(1.0,ys);
}
/*:23*/
#line 329 "./global_check.cweb"
;
/*24:*/
#line 405 "./global_check.cweb"
TwoDMatrix out(model.numeq(),ymat.ncols());
check(max_evals,ymat,umat,out);
char tmp[100];
sprintf(tmp,"%s_ellipse_points",prefix);
ymat.writeMat4(fd,tmp);
sprintf(tmp,"%s_ellipse_errors",prefix);
out.writeMat4(fd,tmp);
/*:24*/
#line 330 "./global_check.cweb"
;
JournalRecordPair pa(journal);
pa<<"Calculating errors at "<<m
<<" ellipse points scaled by "<<mult<<endrec;
/*21:*/
TwoDMatrix*ycov= approx.calcYCov();
TwoDMatrix ycovpred((const TwoDMatrix&)*ycov,model.nstat(),model.nstat(),
model.npred()+model.nboth(),model.npred()+model.nboth());
delete ycov;
SymSchurDecomp ssd(ycovpred);
ssd.correctDefinitness(1.e-05);
TwoDMatrix ycovfac(ycovpred.nrows(),ycovpred.ncols());
KORD_RAISE_IF(!ssd.isPositiveSemidefinite(),
"Covariance matrix of the states not positive \
semidefinite in GlobalChecker::checkOnEllipseAndSave");
ssd.getFactor(ycovfac);
/*:21*/
;
/*22:*/
int d= model.npred()+model.nboth()-1;
TwoDMatrix ymat(model.npred()+model.nboth(),(d==0)?2:m);
if(d==0){
ymat.get(0,0)= 1;
ymat.get(0,1)= -1;
}else{
int icol= 0;
ReversePerScheme ps;
QMCarloCubeQuadrature qmc(d,m,ps);
qmcpit beg= qmc.start(m);
qmcpit end= qmc.end(m);
for(qmcpit run= beg;run!=end;++run,icol++){
Vector ycol(ymat,icol);
Vector x(run.point());
x.mult(2*M_PI);
ycol[0]= 1;
for(int i= 0;i<d;i++){
Vector subsphere(ycol,0,i+1);
subsphere.mult(cos(x[i]));
ycol[i+1]= sin(x[i]);
}
}
}
/*:22*/
;
/*23:*/
TwoDMatrix umat(model.nexog(),ymat.ncols());
umat.zeros();
ymat.mult(mult);
ymat.multLeft(ycovfac);
ConstVector ys(model.getSteady(),model.nstat(),
model.npred()+model.nboth());
for(int icol= 0;icol<ymat.ncols();icol++){
Vector ycol(ymat,icol);
ycol.add(1.0,ys);
}
/*:23*/
;
/*24:*/
TwoDMatrix out(model.numeq(),ymat.ncols());
check(max_evals,ymat,umat,out);
char tmp[100];
sprintf(tmp,"%s_ellipse_points",prefix);
ymat.writeMat4(fd,tmp);
sprintf(tmp,"%s_ellipse_errors",prefix);
out.writeMat4(fd,tmp);
/*:24*/
;
}
/*:20*/
#line 25 "./global_check.cweb"
;
/*25:*/
#line 418 "./global_check.cweb"
void GlobalChecker::checkAlongSimulationAndSave(FILE*fd,const char*prefix,
int m,int max_evals)
int m,int max_evals)
{
JournalRecordPair pa(journal);
pa<<"Calculating errors at "<<m
<<" simulated points"<<endrec;
RandomShockRealization sr(model.getVcov(),system_random_generator.int_uniform());
TwoDMatrix*y= approx.getFoldDecisionRule().simulate(DecisionRule::horner,
m,model.getSteady(),sr);
TwoDMatrix x(model.nexog(),m);
x.zeros();
TwoDMatrix out(model.numeq(),m);
check(max_evals,*y,x,out);
char tmp[100];
sprintf(tmp,"%s_simul_points",prefix);
y->writeMat4(fd,tmp);
sprintf(tmp,"%s_simul_errors",prefix);
out.writeMat4(fd,tmp);
delete y;
JournalRecordPair pa(journal);
pa<<"Calculating errors at "<<m
<<" simulated points"<<endrec;
RandomShockRealization sr(model.getVcov(),system_random_generator.int_uniform());
TwoDMatrix*y= approx.getFoldDecisionRule().simulate(DecisionRule::horner,
m,model.getSteady(),sr);
TwoDMatrix x(model.nexog(),m);
x.zeros();
TwoDMatrix out(model.numeq(),m);
check(max_evals,*y,x,out);
char tmp[100];
sprintf(tmp,"%s_simul_points",prefix);
y->writeMat4(fd,tmp);
sprintf(tmp,"%s_simul_errors",prefix);
out.writeMat4(fd,tmp);
delete y;
}
/*:25*/
#line 26 "./global_check.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 49 "./global_check.hweb"
#ifndef GLOBAL_CHECK_H
#define GLOBAL_CHECK_H
@ -12,91 +11,83 @@
#include "approximation.h"
/*2:*/
#line 85 "./global_check.hweb"
class ResidFunction:public VectorFunction{
protected:
const Approximation&approx;
DynamicModel*model;
Vector*yplus;
Vector*ystar;
Vector*u;
FTensorPolynomial*hss;
const Approximation&approx;
DynamicModel*model;
Vector*yplus;
Vector*ystar;
Vector*u;
FTensorPolynomial*hss;
public:
ResidFunction(const Approximation&app);
ResidFunction(const ResidFunction&rf);
virtual~ResidFunction();
virtual VectorFunction*clone()const
{return new ResidFunction(*this);}
virtual void eval(const Vector&point,const ParameterSignal&sig,Vector&out);
void setYU(const Vector&ys,const Vector&xx);
ResidFunction(const Approximation&app);
ResidFunction(const ResidFunction&rf);
virtual~ResidFunction();
virtual VectorFunction*clone()const
{return new ResidFunction(*this);}
virtual void eval(const Vector&point,const ParameterSignal&sig,Vector&out);
void setYU(const Vector&ys,const Vector&xx);
};
/*:2*/
#line 60 "./global_check.hweb"
;
/*3:*/
#line 106 "./global_check.hweb"
class GResidFunction:public GaussConverterFunction{
public:
GResidFunction(const Approximation&app)
:GaussConverterFunction(new ResidFunction(app),app.getModel().getVcov()){}
GResidFunction(const GResidFunction&rf)
:GaussConverterFunction(rf){}
virtual~GResidFunction(){}
virtual VectorFunction*clone()const
{return new GResidFunction(*this);}
void setYU(const Vector&ys,const Vector&xx)
{((ResidFunction*)func)->setYU(ys,xx);}
GResidFunction(const Approximation&app)
:GaussConverterFunction(new ResidFunction(app),app.getModel().getVcov()){}
GResidFunction(const GResidFunction&rf)
:GaussConverterFunction(rf){}
virtual~GResidFunction(){}
virtual VectorFunction*clone()const
{return new GResidFunction(*this);}
void setYU(const Vector&ys,const Vector&xx)
{((ResidFunction*)func)->setYU(ys,xx);}
};
/*:3*/
#line 61 "./global_check.hweb"
;
/*4:*/
#line 133 "./global_check.hweb"
class GlobalChecker{
const Approximation&approx;
const DynamicModel&model;
Journal&journal;
GResidFunction rf;
VectorFunctionSet vfs;
const Approximation&approx;
const DynamicModel&model;
Journal&journal;
GResidFunction rf;
VectorFunctionSet vfs;
public:
GlobalChecker(const Approximation&app,int n,Journal&jr)
:approx(app),model(approx.getModel()),journal(jr),
rf(approx),vfs(rf,n){}
void check(int max_evals,const ConstTwoDMatrix&y,
const ConstTwoDMatrix&x,TwoDMatrix&out);
void checkAlongShocksAndSave(FILE*fd,const char*prefix,
int m,double mult,int max_evals);
void checkOnEllipseAndSave(FILE*fd,const char*prefix,
int m,double mult,int max_evals);
void checkAlongSimulationAndSave(FILE*fd,const char*prefix,
int m,int max_evals);
void checkUnconditionalAndSave(FILE*fd,const char*prefix,
int m,int max_evals);
GlobalChecker(const Approximation&app,int n,Journal&jr)
:approx(app),model(approx.getModel()),journal(jr),
rf(approx),vfs(rf,n){}
void check(int max_evals,const ConstTwoDMatrix&y,
const ConstTwoDMatrix&x,TwoDMatrix&out);
void checkAlongShocksAndSave(FILE*fd,const char*prefix,
int m,double mult,int max_evals);
void checkOnEllipseAndSave(FILE*fd,const char*prefix,
int m,double mult,int max_evals);
void checkAlongSimulationAndSave(FILE*fd,const char*prefix,
int m,int max_evals);
void checkUnconditionalAndSave(FILE*fd,const char*prefix,
int m,int max_evals);
protected:
void check(const Quadrature&quad,int level,
const ConstVector&y,const ConstVector&x,Vector&out);
void check(const Quadrature&quad,int level,
const ConstVector&y,const ConstVector&x,Vector&out);
};
/*:4*/
#line 62 "./global_check.hweb"
;
/*5:*/
#line 161 "./global_check.hweb"
class ResidFunctionSig:public ResidFunction{
public:
ResidFunctionSig(const Approximation&app,const Vector&ys,const Vector&xx);
ResidFunctionSig(const Approximation&app,const Vector&ys,const Vector&xx);
};
/*:5*/
#line 63 "./global_check.hweb"
;
#endif

View File

@ -1,6 +1,5 @@
#define _W32_FT_OFFSET (116444736000000000LL)
/*1:*/
#line 6 "./journal.cweb"
#include "journal.h"
#include "kord_exception.h"
@ -16,36 +15,33 @@
SystemResources _sysres;
#ifdef __MINGW32__
/*16:*/
#line 249 "./journal.cweb"
typedef struct _filetime{
unsigned long dwLowDateTime;
unsigned long dwHighDateTime;
unsigned long dwLowDateTime;
unsigned long dwHighDateTime;
}filetime;
extern"C"{
void __stdcall GetSystemTimeAsFileTime(filetime*);
void __stdcall GetSystemTimeAsFileTime(filetime*);
};
typedef union{
long long ns100;
filetime ft;
long long ns100;
filetime ft;
}w32_ftv;
void D_gettimeofday(struct timeval*p,struct timezone*tz)
{
w32_ftv _now;
GetSystemTimeAsFileTime(&(_now.ft));
p->tv_usec= (long)((_now.ns100/10LL)%1000000LL);
p->tv_sec= (long)((_now.ns100-_W32_FT_OFFSET)/10000000LL);
return;
w32_ftv _now;
GetSystemTimeAsFileTime(&(_now.ft));
p->tv_usec= (long)((_now.ns100/10LL)%1000000LL);
p->tv_sec= (long)((_now.ns100-_W32_FT_OFFSET)/10000000LL);
return;
}
/*:16*/
#line 20 "./journal.cweb"
;
/*17:*/
#line 282 "./journal.cweb"
#define _SC_PAGESIZE 1
#define _SC_PHYS_PAGES 2
@ -53,308 +49,279 @@ return;
#define _SC_NPROCESSORS_ONLN 4
struct Win32MemoryStatus{
unsigned long dwLength;
unsigned long dwMemoryLoad;
unsigned int dwTotalPhys;
unsigned int dwAvailPhys;
unsigned int dwTotalPageFile;
unsigned int dwAvailPageFile;
unsigned int dwTotalVirtual;
unsigned int dwAvailVirtual;
Win32MemoryStatus();
unsigned long dwLength;
unsigned long dwMemoryLoad;
unsigned int dwTotalPhys;
unsigned int dwAvailPhys;
unsigned int dwTotalPageFile;
unsigned int dwAvailPageFile;
unsigned int dwTotalVirtual;
unsigned int dwAvailVirtual;
Win32MemoryStatus();
};
extern"C"{
void __stdcall GlobalMemoryStatus(Win32MemoryStatus*);
void __stdcall GlobalMemoryStatus(Win32MemoryStatus*);
};
Win32MemoryStatus::Win32MemoryStatus()
{
dwLength= sizeof(Win32MemoryStatus);
GlobalMemoryStatus(this);
dwLength= sizeof(Win32MemoryStatus);
GlobalMemoryStatus(this);
}
long sysconf(int name)
{
switch(name){
case _SC_PAGESIZE:
return 1024;
case _SC_PHYS_PAGES:
{
Win32MemoryStatus memstat;
return memstat.dwTotalPhys/1024;
}
case _SC_AVPHYS_PAGES:
{
Win32MemoryStatus memstat;
return memstat.dwAvailPhys/1024;
}
case _SC_NPROCESSORS_ONLN:
return-1;
default:
KORD_RAISE("Not implemented in Win32 sysconf.");
return-1;
}
switch(name){
case _SC_PAGESIZE:
return 1024;
case _SC_PHYS_PAGES:
{
Win32MemoryStatus memstat;
return memstat.dwTotalPhys/1024;
}
case _SC_AVPHYS_PAGES:
{
Win32MemoryStatus memstat;
return memstat.dwAvailPhys/1024;
}
case _SC_NPROCESSORS_ONLN:
return-1;
default:
KORD_RAISE("Not implemented in Win32 sysconf.");
return-1;
}
}
/*:17*/
#line 21 "./journal.cweb"
;
#endif
/*2:*/
#line 40 "./journal.cweb"
SystemResources::SystemResources()
{
D_gettimeofday(&start,NULL);
D_gettimeofday(&start,NULL);
}
/*:2*/
#line 24 "./journal.cweb"
;
/*3:*/
#line 48 "./journal.cweb"
long int SystemResources::pageSize()
{
return sysconf(_SC_PAGESIZE);
return sysconf(_SC_PAGESIZE);
}
/*:3*/
#line 25 "./journal.cweb"
;
/*4:*/
#line 55 "./journal.cweb"
long int SystemResources::physicalPages()
{
return sysconf(_SC_PHYS_PAGES);
return sysconf(_SC_PHYS_PAGES);
}
/*:4*/
#line 26 "./journal.cweb"
;
/*5:*/
#line 62 "./journal.cweb"
long int SystemResources::onlineProcessors()
{
return sysconf(_SC_NPROCESSORS_ONLN);
return sysconf(_SC_NPROCESSORS_ONLN);
}
/*:5*/
#line 27 "./journal.cweb"
;
/*6:*/
#line 69 "./journal.cweb"
long int SystemResources::availableMemory()
{
return pageSize()*sysconf(_SC_AVPHYS_PAGES);
return pageSize()*sysconf(_SC_AVPHYS_PAGES);
}
/*:6*/
#line 28 "./journal.cweb"
;
/*7:*/
#line 78 "./journal.cweb"
void SystemResources::getRUS(double&load_avg,long int&pg_avail,
double&utime,double&stime,double&elapsed,
long int&idrss,long int&majflt)
double&utime,double&stime,double&elapsed,
long int&idrss,long int&majflt)
{
struct timeval now;
D_gettimeofday(&now,NULL);
elapsed= now.tv_sec-start.tv_sec+(now.tv_usec-start.tv_usec)*1.0e-6;
struct timeval now;
D_gettimeofday(&now,NULL);
elapsed= now.tv_sec-start.tv_sec+(now.tv_usec-start.tv_usec)*1.0e-6;
#ifndef __MINGW32__
struct rusage rus;
getrusage(RUSAGE_SELF,&rus);
utime= rus.ru_utime.tv_sec+rus.ru_utime.tv_usec*1.0e-6;
stime= rus.ru_stime.tv_sec+rus.ru_stime.tv_usec*1.0e-6;
idrss= rus.ru_idrss;
majflt= rus.ru_majflt;
getloadavg(&load_avg,1);
struct rusage rus;
getrusage(RUSAGE_SELF,&rus);
utime= rus.ru_utime.tv_sec+rus.ru_utime.tv_usec*1.0e-6;
stime= rus.ru_stime.tv_sec+rus.ru_stime.tv_usec*1.0e-6;
idrss= rus.ru_idrss;
majflt= rus.ru_majflt;
getloadavg(&load_avg,1);
#else
utime= -1.0;
stime= -1.0;
idrss= -1;
majflt= -1;
load_avg= -1.0;
utime= -1.0;
stime= -1.0;
idrss= -1;
majflt= -1;
load_avg= -1.0;
#endif
pg_avail= sysconf(_SC_AVPHYS_PAGES);
pg_avail= sysconf(_SC_AVPHYS_PAGES);
}
/*:7*/
#line 29 "./journal.cweb"
;
/*8:*/
#line 107 "./journal.cweb"
SystemResourcesFlash::SystemResourcesFlash()
{
_sysres.getRUS(load_avg,pg_avail,utime,stime,
elapsed,idrss,majflt);
_sysres.getRUS(load_avg,pg_avail,utime,stime,
elapsed,idrss,majflt);
}
/*:8*/
#line 30 "./journal.cweb"
;
/*9:*/
#line 115 "./journal.cweb"
void SystemResourcesFlash::diff(const SystemResourcesFlash&pre)
{
utime-= pre.utime;
stime-= pre.stime;
elapsed-= pre.elapsed;
idrss-= pre.idrss;
majflt-= pre.majflt;
utime-= pre.utime;
stime-= pre.stime;
elapsed-= pre.elapsed;
idrss-= pre.idrss;
majflt-= pre.majflt;
}
/*:9*/
#line 31 "./journal.cweb"
;
/*10:*/
#line 126 "./journal.cweb"
JournalRecord&JournalRecord::operator<<(const IntSequence&s)
{
operator<<("[");
for(int i= 0;i<s.size();i++){
operator<<(s[i]);
if(i<s.size()-1)
operator<<(",");
}
operator<<("]");
return*this;
operator<<("[");
for(int i= 0;i<s.size();i++){
operator<<(s[i]);
if(i<s.size()-1)
operator<<(",");
}
operator<<("]");
return*this;
}
/*:10*/
#line 32 "./journal.cweb"
;
/*11:*/
#line 140 "./journal.cweb"
void JournalRecord::writePrefix(const SystemResourcesFlash&f)
{
for(int i= 0;i<MAXLEN;i++)
prefix[i]= ' ';
double mb= 1024*1024;
sprintf(prefix,"%07.6g",f.elapsed);
sprintf(prefix+7,":%c%05d",recChar,ord);
sprintf(prefix+14,":%1.1f",f.load_avg);
sprintf(prefix+18,":%05.4g",f.pg_avail*_sysres.pageSize()/mb);
sprintf(prefix+24,"%s",": : ");
for(int i= 0;i<2*journal.getDepth();i++)
prefix[i+33]= ' ';
prefix[2*journal.getDepth()+33]= '\0';
for(int i= 0;i<MAXLEN;i++)
prefix[i]= ' ';
double mb= 1024*1024;
sprintf(prefix,"%07.6g",f.elapsed);
sprintf(prefix+7,":%c%05d",recChar,ord);
sprintf(prefix+14,":%1.1f",f.load_avg);
sprintf(prefix+18,":%05.4g",f.pg_avail*_sysres.pageSize()/mb);
sprintf(prefix+24,"%s",": : ");
for(int i= 0;i<2*journal.getDepth();i++)
prefix[i+33]= ' ';
prefix[2*journal.getDepth()+33]= '\0';
}
/*:11*/
#line 33 "./journal.cweb"
;
/*12:*/
#line 157 "./journal.cweb"
void JournalRecordPair::writePrefixForEnd(const SystemResourcesFlash&f)
{
for(int i= 0;i<MAXLEN;i++)
prefix_end[i]= ' ';
double mb= 1024*1024;
SystemResourcesFlash difnow;
difnow.diff(f);
sprintf(prefix_end,"%07.6g",f.elapsed+difnow.elapsed);
sprintf(prefix_end+7,":E%05d",ord);
sprintf(prefix_end+14,":%1.1f",difnow.load_avg);
sprintf(prefix_end+18,":%05.4g",difnow.pg_avail*_sysres.pageSize()/mb);
sprintf(prefix_end+24,":%06.5g",difnow.majflt*_sysres.pageSize()/mb);
sprintf(prefix_end+31,"%s",": ");
for(int i= 0;i<2*journal.getDepth();i++)
prefix_end[i+33]= ' ';
prefix_end[2*journal.getDepth()+33]= '\0';
for(int i= 0;i<MAXLEN;i++)
prefix_end[i]= ' ';
double mb= 1024*1024;
SystemResourcesFlash difnow;
difnow.diff(f);
sprintf(prefix_end,"%07.6g",f.elapsed+difnow.elapsed);
sprintf(prefix_end+7,":E%05d",ord);
sprintf(prefix_end+14,":%1.1f",difnow.load_avg);
sprintf(prefix_end+18,":%05.4g",difnow.pg_avail*_sysres.pageSize()/mb);
sprintf(prefix_end+24,":%06.5g",difnow.majflt*_sysres.pageSize()/mb);
sprintf(prefix_end+31,"%s",": ");
for(int i= 0;i<2*journal.getDepth();i++)
prefix_end[i+33]= ' ';
prefix_end[2*journal.getDepth()+33]= '\0';
}
/*:12*/
#line 34 "./journal.cweb"
;
/*13:*/
#line 177 "./journal.cweb"
JournalRecordPair::~JournalRecordPair()
{
journal.decrementDepth();
writePrefixForEnd(flash);
journal<<prefix_end;
journal<<mes;
journal<<endl;
journal.flush();
journal.decrementDepth();
writePrefixForEnd(flash);
journal<<prefix_end;
journal<<mes;
journal<<endl;
journal.flush();
}
/*:13*/
#line 35 "./journal.cweb"
;
/*14:*/
#line 189 "./journal.cweb"
JournalRecord&endrec(JournalRecord&rec)
{
rec.journal<<rec.prefix;
rec.journal<<rec.mes;
rec.journal<<endl;
rec.journal.flush();
rec.journal.incrementOrd();
return rec;
rec.journal<<rec.prefix;
rec.journal<<rec.mes;
rec.journal<<endl;
rec.journal.flush();
rec.journal.incrementOrd();
return rec;
}
/*:14*/
#line 36 "./journal.cweb"
;
/*15:*/
#line 201 "./journal.cweb"
void Journal::printHeader()
{
(*this)<<"This is Dynare++, Copyright (C) 2004,2005 Michel Juillard, Ondra Kamenik\n";
(*this)<<"Dynare++ comes with ABSOLUTELY NO WARRANTY and is distributed under\n";
(*this)<<"General Public License, see http://www.gnu.org/license/gpl.html\n";
(*this)<<"\n\n";
(*this)<<"This is Dynare++, Copyright (C) 2004,2005 Michel Juillard, Ondra Kamenik\n";
(*this)<<"Dynare++ comes with ABSOLUTELY NO WARRANTY and is distributed under\n";
(*this)<<"General Public License, see http://www.gnu.org/license/gpl.html\n";
(*this)<<"\n\n";
#ifndef __MINGW32__
utsname info;
uname(&info);
(*this)<<"System info: ";
(*this)<<info.sysname<<" "<<info.release<<" "<<info.version<<" ";
(*this)<<info.machine<<", processors online: "<<_sysres.onlineProcessors();
(*this)<<"\n\nStart time: ";
char ts[100];
time_t curtime= time(NULL);
tm loctime;
localtime_r(&curtime,&loctime);
asctime_r(&loctime,ts);
(*this)<<ts<<"\n";
utsname info;
uname(&info);
(*this)<<"System info: ";
(*this)<<info.sysname<<" "<<info.release<<" "<<info.version<<" ";
(*this)<<info.machine<<", processors online: "<<_sysres.onlineProcessors();
(*this)<<"\n\nStart time: ";
char ts[100];
time_t curtime= time(NULL);
tm loctime;
localtime_r(&curtime,&loctime);
asctime_r(&loctime,ts);
(*this)<<ts<<"\n";
#else
(*this)<<"System info: (not implemented for MINGW)\n";
(*this)<<"Start time: (not implemented for MINGW)\n\n";
(*this)<<"System info: (not implemented for MINGW)\n";
(*this)<<"Start time: (not implemented for MINGW)\n\n";
#endif
(*this)<<" ------ elapsed time (seconds) \n";
(*this)<<" | ------ record unique identifier \n";
(*this)<<" | | ------ load average \n";
(*this)<<" | | | ------ available memory (MB) \n";
(*this)<<" | | | | ------ major faults (MB)\n";
(*this)<<" | | | | | \n";
(*this)<<" V V V V V \n";
(*this)<<"\n";
(*this)<<" ------ elapsed time (seconds) \n";
(*this)<<" | ------ record unique identifier \n";
(*this)<<" | | ------ load average \n";
(*this)<<" | | | ------ available memory (MB) \n";
(*this)<<" | | | | ------ major faults (MB)\n";
(*this)<<" | | | | | \n";
(*this)<<" V V V V V \n";
(*this)<<"\n";
}
/*:15*/
#line 37 "./journal.cweb"
;
/*:1*/

View File

@ -1,7 +1,6 @@
#define MAXLEN 1000 \
/*1:*/
#line 14 "./journal.hweb"
#ifndef JOURNAL_H
#define JOURNAL_H
@ -14,125 +13,115 @@
#include <fstream>
/*2:*/
#line 34 "./journal.hweb"
class SystemResources{
timeval start;
timeval start;
public:
SystemResources();
static long int pageSize();
static long int physicalPages();
static long int onlineProcessors();
static long int availableMemory();
void getRUS(double&load_avg,long int&pg_avail,double&utime,
double&stime,double&elapsed,long int&idrss,
long int&majflt);
SystemResources();
static long int pageSize();
static long int physicalPages();
static long int onlineProcessors();
static long int availableMemory();
void getRUS(double&load_avg,long int&pg_avail,double&utime,
double&stime,double&elapsed,long int&idrss,
long int&majflt);
};
/*:2*/
#line 25 "./journal.hweb"
;
/*3:*/
#line 49 "./journal.hweb"
struct SystemResourcesFlash{
double load_avg;
long int pg_avail;
double utime;
double stime;
double elapsed;
long int idrss;
long int majflt;
SystemResourcesFlash();
void diff(const SystemResourcesFlash&pre);
double load_avg;
long int pg_avail;
double utime;
double stime;
double elapsed;
long int idrss;
long int majflt;
SystemResourcesFlash();
void diff(const SystemResourcesFlash&pre);
};
/*:3*/
#line 26 "./journal.hweb"
;
/*6:*/
#line 113 "./journal.hweb"
class Journal:public ofstream{
int ord;
int depth;
int ord;
int depth;
public:
Journal(const char*fname)
:ofstream(fname),ord(0),depth(0)
{printHeader();}
~Journal()
{flush();}
void printHeader();
void incrementOrd()
{ord++;}
int getOrd()const
{return ord;}
void incrementDepth()
{depth++;}
void decrementDepth()
{depth--;}
int getDepth()const
{return depth;}
Journal(const char*fname)
:ofstream(fname),ord(0),depth(0)
{printHeader();}
~Journal()
{flush();}
void printHeader();
void incrementOrd()
{ord++;}
int getOrd()const
{return ord;}
void incrementDepth()
{depth++;}
void decrementDepth()
{depth--;}
int getDepth()const
{return depth;}
};
/*:6*/
#line 27 "./journal.hweb"
;
/*4:*/
#line 67 "./journal.hweb"
class JournalRecord;
JournalRecord&endrec(JournalRecord&);
class JournalRecord{
protected:
char recChar;
int ord;
char recChar;
int ord;
public:
Journal&journal;
char prefix[MAXLEN];
char mes[MAXLEN];
SystemResourcesFlash flash;
typedef JournalRecord&(*_Tfunc)(JournalRecord&);
JournalRecord(Journal&jr,char rc= 'M')
:recChar(rc),ord(jr.getOrd()),journal(jr)
{prefix[0]= '\0';mes[0]= '\0';writePrefix(flash);}
virtual~JournalRecord(){}
JournalRecord&operator<<(const IntSequence&s);
JournalRecord&operator<<(_Tfunc f)
{(*f)(*this);return*this;}
JournalRecord&operator<<(const char*s)
{strcat(mes,s);return*this;}
JournalRecord&operator<<(int i)
{sprintf(mes+strlen(mes),"%d",i);return*this;}
JournalRecord&operator<<(double d)
{sprintf(mes+strlen(mes),"%f",d);return*this;}
Journal&journal;
char prefix[MAXLEN];
char mes[MAXLEN];
SystemResourcesFlash flash;
typedef JournalRecord&(*_Tfunc)(JournalRecord&);
JournalRecord(Journal&jr,char rc= 'M')
:recChar(rc),ord(jr.getOrd()),journal(jr)
{prefix[0]= '\0';mes[0]= '\0';writePrefix(flash);}
virtual~JournalRecord(){}
JournalRecord&operator<<(const IntSequence&s);
JournalRecord&operator<<(_Tfunc f)
{(*f)(*this);return*this;}
JournalRecord&operator<<(const char*s)
{strcat(mes,s);return*this;}
JournalRecord&operator<<(int i)
{sprintf(mes+strlen(mes),"%d",i);return*this;}
JournalRecord&operator<<(double d)
{sprintf(mes+strlen(mes),"%f",d);return*this;}
protected:
void writePrefix(const SystemResourcesFlash&f);
void writePrefix(const SystemResourcesFlash&f);
};
/*:4*/
#line 28 "./journal.hweb"
;
/*5:*/
#line 100 "./journal.hweb"
class JournalRecordPair:public JournalRecord{
char prefix_end[MAXLEN];
char prefix_end[MAXLEN];
public:
JournalRecordPair(Journal&jr)
:JournalRecord(jr,'S')
{prefix_end[0]= '\0';journal.incrementDepth();}
~JournalRecordPair();
JournalRecordPair(Journal&jr)
:JournalRecord(jr,'S')
{prefix_end[0]= '\0';journal.incrementDepth();}
~JournalRecordPair();
private:
void writePrefixForEnd(const SystemResourcesFlash&f);
void writePrefixForEnd(const SystemResourcesFlash&f);
};
/*:5*/
#line 29 "./journal.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 9 "./kord_exception.hweb"
#ifndef KORD_EXCEPTION_H
#define KORD_EXCEPTION_H
@ -20,43 +19,39 @@ throw KordException(__FILE__, __LINE__, mes, c);
if (expr) throw KordException(__FILE__, __LINE__, mes, c);
/*2:*/
#line 34 "./kord_exception.hweb"
class KordException{
protected:
char fname[50];
int lnum;
char message[500];
int cd;
char fname[50];
int lnum;
char message[500];
int cd;
public:
KordException(const char*f,int l,const char*mes,int c= 255)
{
strncpy(fname,f,50);fname[49]= '\0';
strncpy(message,mes,500);message[499]= '\0';
lnum= l;
cd= c;
}
virtual~KordException(){}
virtual void print()const
{printf("At %s:%d:(%d):%s\n",fname,lnum,cd,message);}
virtual int code()const
{return cd;}
const char*get_message()const
{return message;}
KordException(const char*f,int l,const char*mes,int c= 255)
{
strncpy(fname,f,50);fname[49]= '\0';
strncpy(message,mes,500);message[499]= '\0';
lnum= l;
cd= c;
}
virtual~KordException(){}
virtual void print()const
{printf("At %s:%d:(%d):%s\n",fname,lnum,cd,message);}
virtual int code()const
{return cd;}
const char*get_message()const
{return message;}
};
/*:2*/
#line 28 "./kord_exception.hweb"
;
/*3:*/
#line 59 "./kord_exception.hweb"
#define KORD_FP_NOT_CONV 254
#define KORD_FP_NOT_FINITE 253
#define KORD_MD_NOT_STABLE 252
/*:3*/
#line 29 "./kord_exception.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./korder.cweb"
#include "kord_exception.h"
@ -8,108 +7,97 @@
#include "cpplapack.h"
/*2:*/
#line 25 "./korder.cweb"
PLUMatrix::PLUMatrix(const PLUMatrix&plu)
:TwoDMatrix(plu),inv(plu.inv),ipiv(new int[nrows()])
{
memcpy(ipiv,plu.ipiv,nrows()*sizeof(int));
memcpy(ipiv,plu.ipiv,nrows()*sizeof(int));
}
/*:2*/
#line 13 "./korder.cweb"
;
/*3:*/
#line 37 "./korder.cweb"
void PLUMatrix::calcPLU()
{
int info;
int rows= nrows();
inv= (const Vector&)getData();
LAPACK_dgetrf(&rows,&rows,inv.base(),&rows,ipiv,&info);
int info;
int rows= nrows();
inv= (const Vector&)getData();
LAPACK_dgetrf(&rows,&rows,inv.base(),&rows,ipiv,&info);
}
/*:3*/
#line 14 "./korder.cweb"
;
/*4:*/
#line 48 "./korder.cweb"
void PLUMatrix::multInv(TwoDMatrix&m)const
{
KORD_RAISE_IF(m.nrows()!=ncols(),
"The matrix is not square in PLUMatrix::multInv");
int info;
int mcols= m.ncols();
int mrows= m.nrows();
double*mbase= m.getData().base();
LAPACK_dgetrs("N",&mrows,&mcols,inv.base(),&mrows,ipiv,
mbase,&mrows,&info);
KORD_RAISE_IF(info!=0,
"Info!=0 in PLUMatrix::multInv");
KORD_RAISE_IF(m.nrows()!=ncols(),
"The matrix is not square in PLUMatrix::multInv");
int info;
int mcols= m.ncols();
int mrows= m.nrows();
double*mbase= m.getData().base();
LAPACK_dgetrs("N",&mrows,&mcols,inv.base(),&mrows,ipiv,
mbase,&mrows,&info);
KORD_RAISE_IF(info!=0,
"Info!=0 in PLUMatrix::multInv");
}
/*:4*/
#line 15 "./korder.cweb"
;
/*5:*/
#line 69 "./korder.cweb"
MatrixA::MatrixA(const FSSparseTensor&f,const IntSequence&ss,
const TwoDMatrix&gy,const PartitionY&ypart)
:PLUMatrix(ypart.ny())
const TwoDMatrix&gy,const PartitionY&ypart)
:PLUMatrix(ypart.ny())
{
zeros();
IntSequence c(1);c[0]= 1;
FGSTensor f_y(f,ss,c,TensorDimens(ss,c));
add(1.0,f_y);
ConstTwoDMatrix gss_ys(ypart.nstat+ypart.npred,ypart.nyss(),gy);
c[0]= 0;
FGSTensor f_yss(f,ss,c,TensorDimens(ss,c));
TwoDMatrix sub(*this,ypart.nstat,ypart.nys());
sub.multAndAdd(ConstTwoDMatrix(f_yss),gss_ys);
calcPLU();
zeros();
IntSequence c(1);c[0]= 1;
FGSTensor f_y(f,ss,c,TensorDimens(ss,c));
add(1.0,f_y);
ConstTwoDMatrix gss_ys(ypart.nstat+ypart.npred,ypart.nyss(),gy);
c[0]= 0;
FGSTensor f_yss(f,ss,c,TensorDimens(ss,c));
TwoDMatrix sub(*this,ypart.nstat,ypart.nys());
sub.multAndAdd(ConstTwoDMatrix(f_yss),gss_ys);
calcPLU();
}
/*:5*/
#line 16 "./korder.cweb"
;
/*6:*/
#line 97 "./korder.cweb"
MatrixS::MatrixS(const FSSparseTensor&f,const IntSequence&ss,
const TwoDMatrix&gy,const PartitionY&ypart)
:PLUMatrix(ypart.ny())
const TwoDMatrix&gy,const PartitionY&ypart)
:PLUMatrix(ypart.ny())
{
zeros();
IntSequence c(1);c[0]= 1;
FGSTensor f_y(f,ss,c,TensorDimens(ss,c));
add(1.0,f_y);
ConstTwoDMatrix gss_ys(ypart.nstat+ypart.npred,ypart.nyss(),gy);
c[0]= 0;
FGSTensor f_yss(f,ss,c,TensorDimens(ss,c));
TwoDMatrix sub(*this,ypart.nstat,ypart.nys());
sub.multAndAdd(ConstTwoDMatrix(f_yss),gss_ys);
TwoDMatrix sub2(*this,ypart.nstat+ypart.npred,ypart.nyss());
sub2.add(1.0,f_yss);
calcPLU();
zeros();
IntSequence c(1);c[0]= 1;
FGSTensor f_y(f,ss,c,TensorDimens(ss,c));
add(1.0,f_y);
ConstTwoDMatrix gss_ys(ypart.nstat+ypart.npred,ypart.nyss(),gy);
c[0]= 0;
FGSTensor f_yss(f,ss,c,TensorDimens(ss,c));
TwoDMatrix sub(*this,ypart.nstat,ypart.nys());
sub.multAndAdd(ConstTwoDMatrix(f_yss),gss_ys);
TwoDMatrix sub2(*this,ypart.nstat+ypart.npred,ypart.nyss());
sub2.add(1.0,f_yss);
calcPLU();
}
/*:6*/
#line 17 "./korder.cweb"
;
/*13:*/
#line 281 "./korder.cweb"
template<> ctraits<KOrder::unfold> ::Tg&KOrder::g<KOrder::unfold> ()
{return _ug;}
@ -170,157 +158,144 @@ template<> const ctraits<KOrder::fold> ::Tm&KOrder::m<KOrder::fold> ()const
/*:13*/
#line 18 "./korder.cweb"
;
/*10:*/
#line 211 "./korder.cweb"
template<>
void KOrder::sylvesterSolve<KOrder::unfold> (ctraits<unfold> ::Ttensor&der)const
{
JournalRecordPair pa(journal);
pa<<"Sylvester equation for dimension = "<<der.getSym()[0]<<endrec;
if(ypart.nys()> 0&&ypart.nyss()> 0){
KORD_RAISE_IF(!der.isFinite(),
"RHS of Sylverster is not finite");
TwoDMatrix gs_y(*(gs<unfold> ().get(Symmetry(1,0,0,0))));
GeneralSylvester sylv(der.getSym()[0],ny,ypart.nys(),
ypart.nstat+ypart.npred,
matA.getData().base(),matB.getData().base(),
gs_y.getData().base(),der.getData().base());
sylv.solve();
}else if(ypart.nys()> 0&&ypart.nyss()==0){
matA.multInv(der);
}
JournalRecordPair pa(journal);
pa<<"Sylvester equation for dimension = "<<der.getSym()[0]<<endrec;
if(ypart.nys()> 0&&ypart.nyss()> 0){
KORD_RAISE_IF(!der.isFinite(),
"RHS of Sylverster is not finite");
TwoDMatrix gs_y(*(gs<unfold> ().get(Symmetry(1,0,0,0))));
GeneralSylvester sylv(der.getSym()[0],ny,ypart.nys(),
ypart.nstat+ypart.npred,
matA.getData().base(),matB.getData().base(),
gs_y.getData().base(),der.getData().base());
sylv.solve();
}else if(ypart.nys()> 0&&ypart.nyss()==0){
matA.multInv(der);
}
}
/*:10*/
#line 19 "./korder.cweb"
;
/*11:*/
#line 235 "./korder.cweb"
template<>
void KOrder::sylvesterSolve<KOrder::fold> (ctraits<fold> ::Ttensor&der)const
{
ctraits<unfold> ::Ttensor tmp(der);
sylvesterSolve<unfold> (tmp);
ctraits<fold> ::Ttensor ftmp(tmp);
der.getData()= (const Vector&)(ftmp.getData());
ctraits<unfold> ::Ttensor tmp(der);
sylvesterSolve<unfold> (tmp);
ctraits<fold> ::Ttensor ftmp(tmp);
der.getData()= (const Vector&)(ftmp.getData());
}
/*:11*/
#line 20 "./korder.cweb"
;
/*12:*/
#line 246 "./korder.cweb"
void KOrder::switchToFolded()
{
JournalRecordPair pa(journal);
pa<<"Switching from unfolded to folded"<<endrec;
int maxdim= g<unfold> ().getMaxDim();
for(int dim= 1;dim<=maxdim;dim++){
SymmetrySet ss(dim,4);
for(symiterator si(ss);!si.isEnd();++si){
if((*si)[2]==0&&g<unfold> ().check(*si)){
FGSTensor*ft= new FGSTensor(*(g<unfold> ().get(*si)));
insertDerivative<fold> (ft);
if(dim> 1){
gss<unfold> ().remove(*si);
gs<unfold> ().remove(*si);
g<unfold> ().remove(*si);
}
}
if(G<unfold> ().check(*si)){
FGSTensor*ft= new FGSTensor(*(G<unfold> ().get(*si)));
G<fold> ().insert(ft);
if(dim> 1){
G<fold> ().remove(*si);
}
}
}
}
JournalRecordPair pa(journal);
pa<<"Switching from unfolded to folded"<<endrec;
int maxdim= g<unfold> ().getMaxDim();
for(int dim= 1;dim<=maxdim;dim++){
SymmetrySet ss(dim,4);
for(symiterator si(ss);!si.isEnd();++si){
if((*si)[2]==0&&g<unfold> ().check(*si)){
FGSTensor*ft= new FGSTensor(*(g<unfold> ().get(*si)));
insertDerivative<fold> (ft);
if(dim> 1){
gss<unfold> ().remove(*si);
gs<unfold> ().remove(*si);
g<unfold> ().remove(*si);
}
}
if(G<unfold> ().check(*si)){
FGSTensor*ft= new FGSTensor(*(G<unfold> ().get(*si)));
G<fold> ().insert(ft);
if(dim> 1){
G<fold> ().remove(*si);
}
}
}
}
}
/*:12*/
#line 21 "./korder.cweb"
;
/*7:*/
#line 133 "./korder.cweb"
KOrder::KOrder(int num_stat,int num_pred,int num_both,int num_forw,
const TensorContainer<FSSparseTensor> &fcont,
const TwoDMatrix&gy,const TwoDMatrix&gu,const TwoDMatrix&v,
Journal&jr)
:ypart(num_stat,num_pred,num_both,num_forw),
ny(ypart.ny()),nu(gu.ncols()),maxk(fcont.getMaxDim()),
nvs(4),
_ug(4),_fg(4),_ugs(4),_fgs(4),_ugss(4),_fgss(4),
_uG(4),_fG(4),
_uZstack(&_uG,ypart.nyss(),&_ug,ny,ypart.nys(),nu),
_fZstack(&_fG,ypart.nyss(),&_fg,ny,ypart.nys(),nu),
_uGstack(&_ugs,ypart.nys(),nu),
_fGstack(&_fgs,ypart.nys(),nu),
_um(maxk,v),_fm(_um),f(fcont),
matA(*(f.get(Symmetry(1))),_uZstack.getStackSizes(),gy,ypart),
matS(*(f.get(Symmetry(1))),_uZstack.getStackSizes(),gy,ypart),
matB(*(f.get(Symmetry(1))),_uZstack.getStackSizes()),
journal(jr)
const TensorContainer<FSSparseTensor> &fcont,
const TwoDMatrix&gy,const TwoDMatrix&gu,const TwoDMatrix&v,
Journal&jr)
:ypart(num_stat,num_pred,num_both,num_forw),
ny(ypart.ny()),nu(gu.ncols()),maxk(fcont.getMaxDim()),
nvs(4),
_ug(4),_fg(4),_ugs(4),_fgs(4),_ugss(4),_fgss(4),
_uG(4),_fG(4),
_uZstack(&_uG,ypart.nyss(),&_ug,ny,ypart.nys(),nu),
_fZstack(&_fG,ypart.nyss(),&_fg,ny,ypart.nys(),nu),
_uGstack(&_ugs,ypart.nys(),nu),
_fGstack(&_fgs,ypart.nys(),nu),
_um(maxk,v),_fm(_um),f(fcont),
matA(*(f.get(Symmetry(1))),_uZstack.getStackSizes(),gy,ypart),
matS(*(f.get(Symmetry(1))),_uZstack.getStackSizes(),gy,ypart),
matB(*(f.get(Symmetry(1))),_uZstack.getStackSizes()),
journal(jr)
{
KORD_RAISE_IF(gy.ncols()!=ypart.nys(),
"Wrong number of columns in gy in KOrder constructor");
KORD_RAISE_IF(v.ncols()!=nu,
"Wrong number of columns of Vcov in KOrder constructor");
KORD_RAISE_IF(nu!=v.nrows(),
"Wrong number of rows of Vcov in KOrder constructor");
KORD_RAISE_IF(maxk<2,
"Order of approximation must be at least 2 in KOrder constructor");
KORD_RAISE_IF(gy.nrows()!=ypart.ny(),
"Wrong number of rows in gy in KOrder constructor");
KORD_RAISE_IF(gu.nrows()!=ypart.ny(),
"Wrong number of rows in gu in KOrder constuctor");
KORD_RAISE_IF(gu.ncols()!=nu,
"Wrong number of columns in gu in KOrder constuctor");
nvs[0]= ypart.nys();nvs[1]= nu;nvs[2]= nu;nvs[3]= 1;
/*8:*/
#line 178 "./korder.cweb"
UGSTensor*tgy= new UGSTensor(ny,TensorDimens(Symmetry(1,0,0,0),nvs));
tgy->getData()= gy.getData();
insertDerivative<unfold> (tgy);
UGSTensor*tgu= new UGSTensor(ny,TensorDimens(Symmetry(0,1,0,0),nvs));
tgu->getData()= gu.getData();
insertDerivative<unfold> (tgu);
/*:8*/
#line 171 "./korder.cweb"
;
/*9:*/
#line 187 "./korder.cweb"
UGSTensor*tGy= faaDiBrunoG<unfold> (Symmetry(1,0,0,0));
G<unfold> ().insert(tGy);
UGSTensor*tGu= faaDiBrunoG<unfold> (Symmetry(0,1,0,0));
G<unfold> ().insert(tGu);
UGSTensor*tGup= faaDiBrunoG<unfold> (Symmetry(0,0,1,0));
G<unfold> ().insert(tGup);
/*:9*/
#line 172 "./korder.cweb"
;
KORD_RAISE_IF(gy.ncols()!=ypart.nys(),
"Wrong number of columns in gy in KOrder constructor");
KORD_RAISE_IF(v.ncols()!=nu,
"Wrong number of columns of Vcov in KOrder constructor");
KORD_RAISE_IF(nu!=v.nrows(),
"Wrong number of rows of Vcov in KOrder constructor");
KORD_RAISE_IF(maxk<2,
"Order of approximation must be at least 2 in KOrder constructor");
KORD_RAISE_IF(gy.nrows()!=ypart.ny(),
"Wrong number of rows in gy in KOrder constructor");
KORD_RAISE_IF(gu.nrows()!=ypart.ny(),
"Wrong number of rows in gu in KOrder constuctor");
KORD_RAISE_IF(gu.ncols()!=nu,
"Wrong number of columns in gu in KOrder constuctor");
nvs[0]= ypart.nys();nvs[1]= nu;nvs[2]= nu;nvs[3]= 1;
/*8:*/
UGSTensor*tgy= new UGSTensor(ny,TensorDimens(Symmetry(1,0,0,0),nvs));
tgy->getData()= gy.getData();
insertDerivative<unfold> (tgy);
UGSTensor*tgu= new UGSTensor(ny,TensorDimens(Symmetry(0,1,0,0),nvs));
tgu->getData()= gu.getData();
insertDerivative<unfold> (tgu);
/*:8*/
;
/*9:*/
UGSTensor*tGy= faaDiBrunoG<unfold> (Symmetry(1,0,0,0));
G<unfold> ().insert(tGy);
UGSTensor*tGu= faaDiBrunoG<unfold> (Symmetry(0,1,0,0));
G<unfold> ().insert(tGu);
UGSTensor*tGup= faaDiBrunoG<unfold> (Symmetry(0,0,1,0));
G<unfold> ().insert(tGup);
/*:9*/
;
}
/*:7*/
#line 22 "./korder.cweb"
;
/*:1*/

File diff suppressed because it is too large Load Diff

View File

@ -1,88 +1,80 @@
/*1:*/
#line 5 "./korder_stoch.cweb"
#include "korder_stoch.h"
/*2:*/
#line 14 "./korder_stoch.cweb"
MatrixAA::MatrixAA(const FSSparseTensor&f,const IntSequence&ss,
const TwoDMatrix&gss_ys,const PartitionY&ypart)
:PLUMatrix(ypart.ny())
const TwoDMatrix&gss_ys,const PartitionY&ypart)
:PLUMatrix(ypart.ny())
{
zeros();
IntSequence c(1);c[0]= 1;
FGSTensor f_y(f,ss,c,TensorDimens(ss,c));
add(1.0,f_y);
c[0]= 0;
FGSTensor f_yss(f,ss,c,TensorDimens(ss,c));
TwoDMatrix sub(*this,ypart.nstat,ypart.nys());
sub.multAndAdd(f_yss,gss_ys);
calcPLU();
zeros();
IntSequence c(1);c[0]= 1;
FGSTensor f_y(f,ss,c,TensorDimens(ss,c));
add(1.0,f_y);
c[0]= 0;
FGSTensor f_yss(f,ss,c,TensorDimens(ss,c));
TwoDMatrix sub(*this,ypart.nstat,ypart.nys());
sub.multAndAdd(f_yss,gss_ys);
calcPLU();
}
/*:2*/
#line 8 "./korder_stoch.cweb"
;
/*3:*/
#line 35 "./korder_stoch.cweb"
KOrderStoch::KOrderStoch(const PartitionY&yp,int nu,
const TensorContainer<FSSparseTensor> &fcont,
const FGSContainer&hh,Journal&jr)
:nvs(4),ypart(yp),journal(jr),
_ug(4),_fg(4),_ugs(4),_fgs(4),_uG(4),_fG(4),
_uh(NULL),_fh(&hh),
_uZstack(&_uG,ypart.nyss(),&_ug,ypart.ny(),ypart.nys(),nu),
_fZstack(&_fG,ypart.nyss(),&_fg,ypart.ny(),ypart.nys(),nu),
_uGstack(&_ugs,ypart.nys(),nu),
_fGstack(&_fgs,ypart.nys(),nu),
f(fcont),
matA(*(fcont.get(Symmetry(1))),_uZstack.getStackSizes(),*(hh.get(Symmetry(1,0,0,0))),
ypart)
const TensorContainer<FSSparseTensor> &fcont,
const FGSContainer&hh,Journal&jr)
:nvs(4),ypart(yp),journal(jr),
_ug(4),_fg(4),_ugs(4),_fgs(4),_uG(4),_fG(4),
_uh(NULL),_fh(&hh),
_uZstack(&_uG,ypart.nyss(),&_ug,ypart.ny(),ypart.nys(),nu),
_fZstack(&_fG,ypart.nyss(),&_fg,ypart.ny(),ypart.nys(),nu),
_uGstack(&_ugs,ypart.nys(),nu),
_fGstack(&_fgs,ypart.nys(),nu),
f(fcont),
matA(*(fcont.get(Symmetry(1))),_uZstack.getStackSizes(),*(hh.get(Symmetry(1,0,0,0))),
ypart)
{
nvs[0]= ypart.nys();
nvs[1]= nu;
nvs[2]= nu;
nvs[3]= 1;
nvs[0]= ypart.nys();
nvs[1]= nu;
nvs[2]= nu;
nvs[3]= 1;
}
/*:3*/
#line 9 "./korder_stoch.cweb"
;
/*4:*/
#line 57 "./korder_stoch.cweb"
KOrderStoch::KOrderStoch(const PartitionY&yp,int nu,
const TensorContainer<FSSparseTensor> &fcont,
const UGSContainer&hh,Journal&jr)
:nvs(4),ypart(yp),journal(jr),
_ug(4),_fg(4),_ugs(4),_fgs(4),_uG(4),_fG(4),
_uh(&hh),_fh(NULL),
_uZstack(&_uG,ypart.nyss(),&_ug,ypart.ny(),ypart.nys(),nu),
_fZstack(&_fG,ypart.nyss(),&_fg,ypart.ny(),ypart.nys(),nu),
_uGstack(&_ugs,ypart.nys(),nu),
_fGstack(&_fgs,ypart.nys(),nu),
f(fcont),
matA(*(fcont.get(Symmetry(1))),_uZstack.getStackSizes(),*(hh.get(Symmetry(1,0,0,0))),
ypart)
const TensorContainer<FSSparseTensor> &fcont,
const UGSContainer&hh,Journal&jr)
:nvs(4),ypart(yp),journal(jr),
_ug(4),_fg(4),_ugs(4),_fgs(4),_uG(4),_fG(4),
_uh(&hh),_fh(NULL),
_uZstack(&_uG,ypart.nyss(),&_ug,ypart.ny(),ypart.nys(),nu),
_fZstack(&_fG,ypart.nyss(),&_fg,ypart.ny(),ypart.nys(),nu),
_uGstack(&_ugs,ypart.nys(),nu),
_fGstack(&_fgs,ypart.nys(),nu),
f(fcont),
matA(*(fcont.get(Symmetry(1))),_uZstack.getStackSizes(),*(hh.get(Symmetry(1,0,0,0))),
ypart)
{
nvs[0]= ypart.nys();
nvs[1]= nu;
nvs[2]= nu;
nvs[3]= 1;
nvs[0]= ypart.nys();
nvs[1]= nu;
nvs[2]= nu;
nvs[3]= 1;
}
/*:4*/
#line 10 "./korder_stoch.cweb"
;
/*5:*/
#line 80 "./korder_stoch.cweb"
template<> ctraits<KOrder::unfold> ::Tg&KOrderStoch::g<KOrder::unfold> ()
{return _ug;}
@ -131,7 +123,6 @@ template<> const ctraits<KOrder::fold> ::TGXstack&KOrderStoch::Gstack<KOrder::fo
/*:5*/
#line 11 "./korder_stoch.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 40 "./korder_stoch.hweb"
#include "korder.h"
#include "faa_di_bruno.h"
@ -7,438 +6,392 @@
/*2:*/
#line 60 "./korder_stoch.hweb"
template<int t>
class IntegDerivs:public ctraits<t> ::Tgss{
public:
/*3:*/
#line 106 "./korder_stoch.hweb"
IntegDerivs(int r,const IntSequence&nvs,const _Tgss&g,const _Tm&mom,
double at_sigma)
:ctraits<t> ::Tgss(4)
{
int maxd= g.getMaxDim();
for(int d= 1;d<=maxd;d++){
for(int i= 0;i<=d;i++){
int p= d-i;
Symmetry sym(i,0,0,p);
_Ttensor*ten= new _Ttensor(r,TensorDimens(sym,nvs));
/*4:*/
#line 129 "./korder_stoch.hweb"
ten->zeros();
for(int n= 0;n<=p;n++){
int k= p-n;
int povern= Tensor::noverk(p,n);
int mfac= 1;
for(int m= 0;i+m+n+k<=maxd;m++,mfac*= m){
double mult= (pow(at_sigma,m)*povern)/mfac;
Symmetry sym_mn(i,m+n,0,k);
if(m+n==0&&g.check(sym_mn))
ten->add(mult,*(g.get(sym_mn)));
if(m+n> 0&&KOrder::is_even(m+n)&&g.check(sym_mn)){
_Ttensor gtmp(*(g.get(sym_mn)));
gtmp.mult(mult);
gtmp.contractAndAdd(1,*ten,*(mom.get(Symmetry(m+n))));
}
}
}
/*:4*/
#line 117 "./korder_stoch.hweb"
;
insert(ten);
}
}
}
/*:3*/
#line 64 "./korder_stoch.hweb"
;
/*3:*/
IntegDerivs(int r,const IntSequence&nvs,const _Tgss&g,const _Tm&mom,
double at_sigma)
:ctraits<t> ::Tgss(4)
{
int maxd= g.getMaxDim();
for(int d= 1;d<=maxd;d++){
for(int i= 0;i<=d;i++){
int p= d-i;
Symmetry sym(i,0,0,p);
_Ttensor*ten= new _Ttensor(r,TensorDimens(sym,nvs));
/*4:*/
ten->zeros();
for(int n= 0;n<=p;n++){
int k= p-n;
int povern= Tensor::noverk(p,n);
int mfac= 1;
for(int m= 0;i+m+n+k<=maxd;m++,mfac*= m){
double mult= (pow(at_sigma,m)*povern)/mfac;
Symmetry sym_mn(i,m+n,0,k);
if(m+n==0&&g.check(sym_mn))
ten->add(mult,*(g.get(sym_mn)));
if(m+n> 0&&KOrder::is_even(m+n)&&g.check(sym_mn)){
_Ttensor gtmp(*(g.get(sym_mn)));
gtmp.mult(mult);
gtmp.contractAndAdd(1,*ten,*(mom.get(Symmetry(m+n))));
}
}
}
/*:4*/
;
insert(ten);
}
}
}
/*:3*/
;
};
/*:2*/
#line 46 "./korder_stoch.hweb"
;
/*5:*/
#line 156 "./korder_stoch.hweb"
template<int t>
class StochForwardDerivs:public ctraits<t> ::Tgss{
public:
/*6:*/
#line 180 "./korder_stoch.hweb"
StochForwardDerivs(const PartitionY&ypart,int nu,
const _Tgss&g,const _Tm&m,
const Vector&ydelta,double sdelta,
double at_sigma)
:ctraits<t> ::Tgss(4)
{
int maxd= g.getMaxDim();
int r= ypart.nyss();
/*7:*/
#line 201 "./korder_stoch.hweb"
IntSequence nvs(4);
nvs[0]= ypart.nys();nvs[1]= 0;nvs[2]= 0;nvs[3]= 1;
IntegDerivs<t> g_int(r,nvs,g,m,at_sigma);
/*:7*/
#line 190 "./korder_stoch.hweb"
;
/*8:*/
#line 209 "./korder_stoch.hweb"
_Tpol g_int_sym(r,ypart.nys()+1);
for(int d= 1;d<=maxd;d++){
_Ttensym*ten= new _Ttensym(r,ypart.nys()+1,d);
ten->zeros();
for(int i= 0;i<=d;i++){
int k= d-i;
if(g_int.check(Symmetry(i,0,0,k)))
ten->addSubTensor(*(g_int.get(Symmetry(i,0,0,k))));
}
g_int_sym.insert(ten);
}
/*:8*/
#line 191 "./korder_stoch.hweb"
;
/*9:*/
#line 229 "./korder_stoch.hweb"
Vector delta(ypart.nys()+1);
Vector dy(delta,0,ypart.nys());
ConstVector dy_in(ydelta,ypart.nstat,ypart.nys());
dy= dy_in;
delta[ypart.nys()]= sdelta;
_Tpol g_int_cent(r,ypart.nys()+1);
for(int d= 1;d<=maxd;d++){
g_int_sym.derivative(d-1);
_Ttensym*der= g_int_sym.evalPartially(d,delta);
g_int_cent.insert(der);
}
/*:9*/
#line 192 "./korder_stoch.hweb"
;
/*10:*/
#line 245 "./korder_stoch.hweb"
IntSequence ss(4);
ss[0]= ypart.nys();ss[1]= 0;ss[2]= 0;ss[3]= 1;
IntSequence pp(4);
pp[0]= 0;pp[1]= 1;pp[2]= 2;pp[3]= 3;
IntSequence true_nvs(nvs);
true_nvs[1]= nu;true_nvs[2]= nu;
for(int d= 1;d<=maxd;d++){
if(g_int_cent.check(Symmetry(d))){
for(int i= 0;i<=d;i++){
Symmetry sym(i,0,0,d-i);
IntSequence coor(sym,pp);
_Ttensor*ten= new _Ttensor(*(g_int_cent.get(Symmetry(d))),ss,coor,
TensorDimens(sym,true_nvs));
insert(ten);
}
}
}
/*:10*/
#line 193 "./korder_stoch.hweb"
;
}
/*:6*/
#line 160 "./korder_stoch.hweb"
;
/*6:*/
StochForwardDerivs(const PartitionY&ypart,int nu,
const _Tgss&g,const _Tm&m,
const Vector&ydelta,double sdelta,
double at_sigma)
:ctraits<t> ::Tgss(4)
{
int maxd= g.getMaxDim();
int r= ypart.nyss();
/*7:*/
IntSequence nvs(4);
nvs[0]= ypart.nys();nvs[1]= 0;nvs[2]= 0;nvs[3]= 1;
IntegDerivs<t> g_int(r,nvs,g,m,at_sigma);
/*:7*/
;
/*8:*/
_Tpol g_int_sym(r,ypart.nys()+1);
for(int d= 1;d<=maxd;d++){
_Ttensym*ten= new _Ttensym(r,ypart.nys()+1,d);
ten->zeros();
for(int i= 0;i<=d;i++){
int k= d-i;
if(g_int.check(Symmetry(i,0,0,k)))
ten->addSubTensor(*(g_int.get(Symmetry(i,0,0,k))));
}
g_int_sym.insert(ten);
}
/*:8*/
;
/*9:*/
Vector delta(ypart.nys()+1);
Vector dy(delta,0,ypart.nys());
ConstVector dy_in(ydelta,ypart.nstat,ypart.nys());
dy= dy_in;
delta[ypart.nys()]= sdelta;
_Tpol g_int_cent(r,ypart.nys()+1);
for(int d= 1;d<=maxd;d++){
g_int_sym.derivative(d-1);
_Ttensym*der= g_int_sym.evalPartially(d,delta);
g_int_cent.insert(der);
}
/*:9*/
;
/*10:*/
IntSequence ss(4);
ss[0]= ypart.nys();ss[1]= 0;ss[2]= 0;ss[3]= 1;
IntSequence pp(4);
pp[0]= 0;pp[1]= 1;pp[2]= 2;pp[3]= 3;
IntSequence true_nvs(nvs);
true_nvs[1]= nu;true_nvs[2]= nu;
for(int d= 1;d<=maxd;d++){
if(g_int_cent.check(Symmetry(d))){
for(int i= 0;i<=d;i++){
Symmetry sym(i,0,0,d-i);
IntSequence coor(sym,pp);
_Ttensor*ten= new _Ttensor(*(g_int_cent.get(Symmetry(d))),ss,coor,
TensorDimens(sym,true_nvs));
insert(ten);
}
}
}
/*:10*/
;
}
/*:6*/
;
};
/*:5*/
#line 47 "./korder_stoch.hweb"
;
/*11:*/
#line 270 "./korder_stoch.hweb"
template<class _Ttype>
class GXContainer:public GContainer<_Ttype> {
public:
typedef StackContainerInterface<_Ttype> _Stype;
typedef typename StackContainer<_Ttype> ::_Ctype _Ctype;
typedef typename StackContainer<_Ttype> ::itype itype;
GXContainer(const _Ctype*gs,int ngs,int nu)
:GContainer<_Ttype> (gs,ngs,nu){}
/*12:*/
#line 285 "./korder_stoch.hweb"
itype getType(int i,const Symmetry&s)const
{
if(i==0)
if(s[2]> 0)
return _Stype::zero;
else
return _Stype::matrix;
if(i==1)
return _Stype::zero;
if(i==2)
return _Stype::zero;
if(i==3)
if(s==Symmetry(0,0,0,1))
return _Stype::unit;
else
return _Stype::zero;
KORD_RAISE("Wrong stack index in GXContainer::getType");
}
/*:12*/
#line 279 "./korder_stoch.hweb"
;
typedef StackContainerInterface<_Ttype> _Stype;
typedef typename StackContainer<_Ttype> ::_Ctype _Ctype;
typedef typename StackContainer<_Ttype> ::itype itype;
GXContainer(const _Ctype*gs,int ngs,int nu)
:GContainer<_Ttype> (gs,ngs,nu){}
/*12:*/
itype getType(int i,const Symmetry&s)const
{
if(i==0)
if(s[2]> 0)
return _Stype::zero;
else
return _Stype::matrix;
if(i==1)
return _Stype::zero;
if(i==2)
return _Stype::zero;
if(i==3)
if(s==Symmetry(0,0,0,1))
return _Stype::unit;
else
return _Stype::zero;
KORD_RAISE("Wrong stack index in GXContainer::getType");
}
/*:12*/
;
};
/*:11*/
#line 48 "./korder_stoch.hweb"
;
/*13:*/
#line 312 "./korder_stoch.hweb"
template<class _Ttype>
class ZXContainer:public ZContainer<_Ttype> {
public:
typedef StackContainerInterface<_Ttype> _Stype;
typedef typename StackContainer<_Ttype> ::_Ctype _Ctype;
typedef typename StackContainer<_Ttype> ::itype itype;
ZXContainer(const _Ctype*gss,int ngss,const _Ctype*g,int ng,int ny,int nu)
:ZContainer<_Ttype> (gss,ngss,g,ng,ny,nu){}
/*14:*/
#line 327 "./korder_stoch.hweb"
itype getType(int i,const Symmetry&s)const
{
if(i==0)
if(s[2]> 0)
return _Stype::zero;
else
return _Stype::matrix;
if(i==1)
if(s[2]> 0)
return _Stype::zero;
else
return _Stype::matrix;
if(i==2)
if(s==Symmetry(1,0,0,0))
return _Stype::unit;
else
return _Stype::zero;
if(i==3)
if(s==Symmetry(0,1,0,0))
return _Stype::unit;
else
return _Stype::zero;
KORD_RAISE("Wrong stack index in ZXContainer::getType");
}
/*:14*/
#line 321 "./korder_stoch.hweb"
;
typedef StackContainerInterface<_Ttype> _Stype;
typedef typename StackContainer<_Ttype> ::_Ctype _Ctype;
typedef typename StackContainer<_Ttype> ::itype itype;
ZXContainer(const _Ctype*gss,int ngss,const _Ctype*g,int ng,int ny,int nu)
:ZContainer<_Ttype> (gss,ngss,g,ng,ny,nu){}
/*14:*/
itype getType(int i,const Symmetry&s)const
{
if(i==0)
if(s[2]> 0)
return _Stype::zero;
else
return _Stype::matrix;
if(i==1)
if(s[2]> 0)
return _Stype::zero;
else
return _Stype::matrix;
if(i==2)
if(s==Symmetry(1,0,0,0))
return _Stype::unit;
else
return _Stype::zero;
if(i==3)
if(s==Symmetry(0,1,0,0))
return _Stype::unit;
else
return _Stype::zero;
KORD_RAISE("Wrong stack index in ZXContainer::getType");
}
/*:14*/
;
};
/*:13*/
#line 49 "./korder_stoch.hweb"
;
/*15:*/
#line 355 "./korder_stoch.hweb"
class UnfoldedGXContainer:public GXContainer<UGSTensor> ,public UnfoldedStackContainer{
public:
typedef TensorContainer<UGSTensor> _Ctype;
UnfoldedGXContainer(const _Ctype*gs,int ngs,int nu)
:GXContainer<UGSTensor> (gs,ngs,nu){}
typedef TensorContainer<UGSTensor> _Ctype;
UnfoldedGXContainer(const _Ctype*gs,int ngs,int nu)
:GXContainer<UGSTensor> (gs,ngs,nu){}
};
/*:15*/
#line 50 "./korder_stoch.hweb"
;
/*16:*/
#line 364 "./korder_stoch.hweb"
class FoldedGXContainer:public GXContainer<FGSTensor> ,public FoldedStackContainer{
public:
typedef TensorContainer<FGSTensor> _Ctype;
FoldedGXContainer(const _Ctype*gs,int ngs,int nu)
:GXContainer<FGSTensor> (gs,ngs,nu){}
typedef TensorContainer<FGSTensor> _Ctype;
FoldedGXContainer(const _Ctype*gs,int ngs,int nu)
:GXContainer<FGSTensor> (gs,ngs,nu){}
};
/*:16*/
#line 51 "./korder_stoch.hweb"
;
/*17:*/
#line 373 "./korder_stoch.hweb"
class UnfoldedZXContainer:public ZXContainer<UGSTensor> ,public UnfoldedStackContainer{
public:
typedef TensorContainer<UGSTensor> _Ctype;
UnfoldedZXContainer(const _Ctype*gss,int ngss,const _Ctype*g,int ng,int ny,int nu)
:ZXContainer<UGSTensor> (gss,ngss,g,ng,ny,nu){}
typedef TensorContainer<UGSTensor> _Ctype;
UnfoldedZXContainer(const _Ctype*gss,int ngss,const _Ctype*g,int ng,int ny,int nu)
:ZXContainer<UGSTensor> (gss,ngss,g,ng,ny,nu){}
};
/*:17*/
#line 52 "./korder_stoch.hweb"
;
/*18:*/
#line 382 "./korder_stoch.hweb"
class FoldedZXContainer:public ZXContainer<FGSTensor> ,public FoldedStackContainer{
public:
typedef TensorContainer<FGSTensor> _Ctype;
FoldedZXContainer(const _Ctype*gss,int ngss,const _Ctype*g,int ng,int ny,int nu)
:ZXContainer<FGSTensor> (gss,ngss,g,ng,ny,nu){}
typedef TensorContainer<FGSTensor> _Ctype;
FoldedZXContainer(const _Ctype*gss,int ngss,const _Ctype*g,int ng,int ny,int nu)
:ZXContainer<FGSTensor> (gss,ngss,g,ng,ny,nu){}
};
/*:18*/
#line 53 "./korder_stoch.hweb"
;
/*19:*/
#line 397 "./korder_stoch.hweb"
class MatrixAA:public PLUMatrix{
public:
MatrixAA(const FSSparseTensor&f,const IntSequence&ss,
const TwoDMatrix&gyss,const PartitionY&ypart);
MatrixAA(const FSSparseTensor&f,const IntSequence&ss,
const TwoDMatrix&gyss,const PartitionY&ypart);
};
/*:19*/
#line 54 "./korder_stoch.hweb"
;
/*20:*/
#line 418 "./korder_stoch.hweb"
class KOrderStoch{
protected:
IntSequence nvs;
PartitionY ypart;
Journal&journal;
UGSContainer _ug;
FGSContainer _fg;
UGSContainer _ugs;
FGSContainer _fgs;
UGSContainer _uG;
FGSContainer _fG;
const UGSContainer*_uh;
const FGSContainer*_fh;
UnfoldedZXContainer _uZstack;
FoldedZXContainer _fZstack;
UnfoldedGXContainer _uGstack;
FoldedGXContainer _fGstack;
const TensorContainer<FSSparseTensor> &f;
MatrixAA matA;
IntSequence nvs;
PartitionY ypart;
Journal&journal;
UGSContainer _ug;
FGSContainer _fg;
UGSContainer _ugs;
FGSContainer _fgs;
UGSContainer _uG;
FGSContainer _fG;
const UGSContainer*_uh;
const FGSContainer*_fh;
UnfoldedZXContainer _uZstack;
FoldedZXContainer _fZstack;
UnfoldedGXContainer _uGstack;
FoldedGXContainer _fGstack;
const TensorContainer<FSSparseTensor> &f;
MatrixAA matA;
public:
KOrderStoch(const PartitionY&ypart,int nu,const TensorContainer<FSSparseTensor> &fcont,
const FGSContainer&hh,Journal&jr);
KOrderStoch(const PartitionY&ypart,int nu,const TensorContainer<FSSparseTensor> &fcont,
const UGSContainer&hh,Journal&jr);
/*23:*/
#line 496 "./korder_stoch.hweb"
template<int t>
void performStep(int order)
{
int maxd= g<t> ().getMaxDim();
KORD_RAISE_IF(order-1!=maxd&&(order!=1||maxd!=-1),
"Wrong order for KOrderStoch::performStep");
SymmetrySet ss(order,4);
for(symiterator si(ss);!si.isEnd();++si){
if((*si)[2]==0){
JournalRecordPair pa(journal);
pa<<"Recovering symmetry "<<*si<<endrec;
_Ttensor*G_sym= faaDiBrunoG<t> (*si);
G<t> ().insert(G_sym);
_Ttensor*g_sym= faaDiBrunoZ<t> (*si);
g_sym->mult(-1.0);
matA.multInv(*g_sym);
g<t> ().insert(g_sym);
gs<t> ().insert(new _Ttensor(ypart.nstat,ypart.nys(),*g_sym));
Gstack<t> ().multAndAdd(1,h<t> (),*G_sym);
}
}
}
/*:23*/
#line 443 "./korder_stoch.hweb"
;
const FGSContainer&getFoldDers()const
{return _fg;}
const UGSContainer&getUnfoldDers()const
{return _ug;}
KOrderStoch(const PartitionY&ypart,int nu,const TensorContainer<FSSparseTensor> &fcont,
const FGSContainer&hh,Journal&jr);
KOrderStoch(const PartitionY&ypart,int nu,const TensorContainer<FSSparseTensor> &fcont,
const UGSContainer&hh,Journal&jr);
/*23:*/
template<int t>
void performStep(int order)
{
int maxd= g<t> ().getMaxDim();
KORD_RAISE_IF(order-1!=maxd&&(order!=1||maxd!=-1),
"Wrong order for KOrderStoch::performStep");
SymmetrySet ss(order,4);
for(symiterator si(ss);!si.isEnd();++si){
if((*si)[2]==0){
JournalRecordPair pa(journal);
pa<<"Recovering symmetry "<<*si<<endrec;
_Ttensor*G_sym= faaDiBrunoG<t> (*si);
G<t> ().insert(G_sym);
_Ttensor*g_sym= faaDiBrunoZ<t> (*si);
g_sym->mult(-1.0);
matA.multInv(*g_sym);
g<t> ().insert(g_sym);
gs<t> ().insert(new _Ttensor(ypart.nstat,ypart.nys(),*g_sym));
Gstack<t> ().multAndAdd(1,h<t> (),*G_sym);
}
}
}
/*:23*/
;
const FGSContainer&getFoldDers()const
{return _fg;}
const UGSContainer&getUnfoldDers()const
{return _ug;}
protected:
/*21:*/
#line 457 "./korder_stoch.hweb"
template<int t>
_Ttensor*faaDiBrunoZ(const Symmetry&sym)const
{
JournalRecordPair pa(journal);
pa<<"Faa Di Bruno ZX container for "<<sym<<endrec;
_Ttensor*res= new _Ttensor(ypart.ny(),TensorDimens(sym,nvs));
FaaDiBruno bruno(journal);
bruno.calculate(Zstack<t> (),f,*res);
return res;
}
/*:21*/
#line 449 "./korder_stoch.hweb"
;
/*22:*/
#line 472 "./korder_stoch.hweb"
template<int t>
_Ttensor*faaDiBrunoG(const Symmetry&sym)const
{
JournalRecordPair pa(journal);
pa<<"Faa Di Bruno GX container for "<<sym<<endrec;
TensorDimens tdims(sym,nvs);
_Ttensor*res= new _Ttensor(ypart.nyss(),tdims);
FaaDiBruno bruno(journal);
bruno.calculate(Gstack<t> (),h<t> (),*res);
return res;
}
/*:22*/
#line 450 "./korder_stoch.hweb"
;
/*24:*/
#line 524 "./korder_stoch.hweb"
template<int t> _Tg&g();
template<int t> const _Tg&g()const;
template<int t> _Tgs&gs();
template<int t> const _Tgs&gs()const;
template<int t> const _Tgss&h()const;
template<int t> _TG&G();
template<int t> const _TG&G()const;
template<int t> _TZXstack&Zstack();
template<int t> const _TZXstack&Zstack()const;
template<int t> _TGXstack&Gstack();
template<int t> const _TGXstack&Gstack()const;
/*:24*/
#line 451 "./korder_stoch.hweb"
;
/*21:*/
template<int t>
_Ttensor*faaDiBrunoZ(const Symmetry&sym)const
{
JournalRecordPair pa(journal);
pa<<"Faa Di Bruno ZX container for "<<sym<<endrec;
_Ttensor*res= new _Ttensor(ypart.ny(),TensorDimens(sym,nvs));
FaaDiBruno bruno(journal);
bruno.calculate(Zstack<t> (),f,*res);
return res;
}
/*:21*/
;
/*22:*/
template<int t>
_Ttensor*faaDiBrunoG(const Symmetry&sym)const
{
JournalRecordPair pa(journal);
pa<<"Faa Di Bruno GX container for "<<sym<<endrec;
TensorDimens tdims(sym,nvs);
_Ttensor*res= new _Ttensor(ypart.nyss(),tdims);
FaaDiBruno bruno(journal);
bruno.calculate(Gstack<t> (),h<t> (),*res);
return res;
}
/*:22*/
;
/*24:*/
template<int t> _Tg&g();
template<int t> const _Tg&g()const;
template<int t> _Tgs&gs();
template<int t> const _Tgs&gs()const;
template<int t> const _Tgss&h()const;
template<int t> _TG&G();
template<int t> const _TG&G()const;
template<int t> _TZXstack&Zstack();
template<int t> const _TZXstack&Zstack()const;
template<int t> _TGXstack&Gstack();
template<int t> const _TGXstack&Gstack()const;
/*:24*/
;
};
/*:20*/
#line 55 "./korder_stoch.hweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 14 "./mersenne_twister.hweb"
#ifndef MERSENNE_TWISTER_H
#define MERSENNE_TWISTER_H
@ -8,148 +7,130 @@
#include <string.h>
/*2:*/
#line 27 "./mersenne_twister.hweb"
class MersenneTwister:public RandomGenerator{
protected:
typedef unsigned int uint32;
enum{STATE_SIZE= 624};
enum{RECUR_OFFSET= 397};
uint32 statevec[STATE_SIZE];
int stateptr;
typedef unsigned int uint32;
enum{STATE_SIZE= 624};
enum{RECUR_OFFSET= 397};
uint32 statevec[STATE_SIZE];
int stateptr;
public:
MersenneTwister(uint32 iseed);
MersenneTwister(const MersenneTwister&mt);
virtual~MersenneTwister(){}
uint32 lrand();
double drand();
double uniform()
{return drand();}
MersenneTwister(uint32 iseed);
MersenneTwister(const MersenneTwister&mt);
virtual~MersenneTwister(){}
uint32 lrand();
double drand();
double uniform()
{return drand();}
protected:
void seed(uint32 iseed);
void refresh();
void seed(uint32 iseed);
void refresh();
private:
/*3:*/
#line 51 "./mersenne_twister.hweb"
static uint32 hibit(uint32 u)
{return u&0x80000000UL;}
static uint32 lobit(uint32 u)
{return u&0x00000001UL;}
static uint32 lobits(uint32 u)
{return u&0x7fffffffUL;}
static uint32 mixbits(uint32 u,uint32 v)
{return hibit(u)|lobits(v);}
static uint32 twist(uint32 m,uint32 s0,uint32 s1)
{return m^(mixbits(s0,s1)>>1)^(-lobit(s1)&0x9908b0dfUL);}
/*:3*/
#line 47 "./mersenne_twister.hweb"
;
/*3:*/
static uint32 hibit(uint32 u)
{return u&0x80000000UL;}
static uint32 lobit(uint32 u)
{return u&0x00000001UL;}
static uint32 lobits(uint32 u)
{return u&0x7fffffffUL;}
static uint32 mixbits(uint32 u,uint32 v)
{return hibit(u)|lobits(v);}
static uint32 twist(uint32 m,uint32 s0,uint32 s1)
{return m^(mixbits(s0,s1)>>1)^(-lobit(s1)&0x9908b0dfUL);}
/*:3*/
;
};
/*:2*/
#line 21 "./mersenne_twister.hweb"
;
/*4:*/
#line 65 "./mersenne_twister.hweb"
/*5:*/
#line 74 "./mersenne_twister.hweb"
inline MersenneTwister::MersenneTwister(uint32 iseed)
{
seed(iseed);
seed(iseed);
}
/*:5*/
#line 66 "./mersenne_twister.hweb"
;
/*6:*/
#line 81 "./mersenne_twister.hweb"
inline MersenneTwister::MersenneTwister(const MersenneTwister&mt)
:stateptr(mt.stateptr)
{
memcpy(statevec,mt.statevec,sizeof(uint32)*STATE_SIZE);
memcpy(statevec,mt.statevec,sizeof(uint32)*STATE_SIZE);
}
/*:6*/
#line 67 "./mersenne_twister.hweb"
;
/*7:*/
#line 89 "./mersenne_twister.hweb"
inline MersenneTwister::uint32 MersenneTwister::lrand()
{
if(stateptr>=STATE_SIZE)
refresh();
register uint32 v= statevec[stateptr++];
v^= v>>11;
v^= (v<<7)&0x9d2c5680;
v^= (v<<15)&0xefc60000;
return(v^(v>>18));
if(stateptr>=STATE_SIZE)
refresh();
register uint32 v= statevec[stateptr++];
v^= v>>11;
v^= (v<<7)&0x9d2c5680;
v^= (v<<15)&0xefc60000;
return(v^(v>>18));
}
/*:7*/
#line 68 "./mersenne_twister.hweb"
;
/*8:*/
#line 103 "./mersenne_twister.hweb"
inline double MersenneTwister::drand()
{
uint32 a= lrand()>>5;
uint32 b= lrand()>>6;
return(a*67108864.0+b)*(1.0/9007199254740992.0);
uint32 a= lrand()>>5;
uint32 b= lrand()>>6;
return(a*67108864.0+b)*(1.0/9007199254740992.0);
}
/*:8*/
#line 69 "./mersenne_twister.hweb"
;
/*9:*/
#line 112 "./mersenne_twister.hweb"
inline void MersenneTwister::seed(uint32 iseed)
{
statevec[0]= iseed&0xffffffffUL;
for(int i= 1;i<STATE_SIZE;i++){
register uint32 val= statevec[i-1]>>30;
val^= statevec[i-1];
val*= 1812433253ul;
val+= i;
statevec[i]= val&0xffffffffUL;
}
refresh();
statevec[0]= iseed&0xffffffffUL;
for(int i= 1;i<STATE_SIZE;i++){
register uint32 val= statevec[i-1]>>30;
val^= statevec[i-1];
val*= 1812433253ul;
val+= i;
statevec[i]= val&0xffffffffUL;
}
refresh();
}
/*:9*/
#line 70 "./mersenne_twister.hweb"
;
/*10:*/
#line 128 "./mersenne_twister.hweb"
inline void MersenneTwister::refresh()
{
register uint32*p= statevec;
for(int i= STATE_SIZE-RECUR_OFFSET;i--;++p)
*p= twist(p[RECUR_OFFSET],p[0],p[1]);
for(int i= RECUR_OFFSET;--i;++p)
*p= twist(p[RECUR_OFFSET-STATE_SIZE],p[0],p[1]);
*p= twist(p[RECUR_OFFSET-STATE_SIZE],p[0],statevec[0]);
stateptr= 0;
register uint32*p= statevec;
for(int i= STATE_SIZE-RECUR_OFFSET;i--;++p)
*p= twist(p[RECUR_OFFSET],p[0],p[1]);
for(int i= RECUR_OFFSET;--i;++p)
*p= twist(p[RECUR_OFFSET-STATE_SIZE],p[0],p[1]);
*p= twist(p[RECUR_OFFSET-STATE_SIZE],p[0],statevec[0]);
stateptr= 0;
}
/*:10*/
#line 71 "./mersenne_twister.hweb"
;
/*:4*/
#line 22 "./mersenne_twister.hweb"
;
#endif

View File

@ -1,47 +1,41 @@
/*1:*/
#line 6 "./normal_conjugate.cweb"
#include "normal_conjugate.h"
#include "kord_exception.h"
/*2:*/
#line 20 "./normal_conjugate.cweb"
NormalConj::NormalConj(int d)
:mu(d),kappa(0),nu(-1),lambda(d,d)
{
mu.zeros();
lambda.zeros();
mu.zeros();
lambda.zeros();
}
/*:2*/
#line 11 "./normal_conjugate.cweb"
;
/*3:*/
#line 29 "./normal_conjugate.cweb"
NormalConj::NormalConj(const ConstTwoDMatrix&ydata)
:mu(ydata.numRows()),kappa(ydata.numCols()),nu(ydata.numCols()-1),
lambda(ydata.numRows(),ydata.numRows())
{
mu.zeros();
for(int i= 0;i<ydata.numCols();i++)
mu.add(1.0/ydata.numCols(),ConstVector(ydata,i));
lambda.zeros();
for(int i= 0;i<ydata.numCols();i++){
Vector diff(ConstVector(ydata,i));
diff.add(-1,mu);
lambda.addOuter(diff);
}
mu.zeros();
for(int i= 0;i<ydata.numCols();i++)
mu.add(1.0/ydata.numCols(),ConstVector(ydata,i));
lambda.zeros();
for(int i= 0;i<ydata.numCols();i++){
Vector diff(ConstVector(ydata,i));
diff.add(-1,mu);
lambda.addOuter(diff);
}
}
/*:3*/
#line 12 "./normal_conjugate.cweb"
;
/*4:*/
#line 47 "./normal_conjugate.cweb"
NormalConj::NormalConj(const NormalConj&nc)
:mu(nc.mu),kappa(nc.kappa),nu(nc.nu),lambda(nc.lambda)
@ -49,82 +43,73 @@ NormalConj::NormalConj(const NormalConj&nc)
}
/*:4*/
#line 13 "./normal_conjugate.cweb"
;
/*5:*/
#line 61 "./normal_conjugate.cweb"
void NormalConj::update(const ConstVector&y)
{
KORD_RAISE_IF(y.length()!=mu.length(),
"Wrong length of a vector in NormalConj::update");
mu.mult(kappa/(1.0+kappa));
mu.add(1.0/(1.0+kappa),y);
Vector diff(y);
diff.add(-1,mu);
lambda.addOuter(diff,kappa/(1.0+kappa));
kappa++;
nu++;
KORD_RAISE_IF(y.length()!=mu.length(),
"Wrong length of a vector in NormalConj::update");
mu.mult(kappa/(1.0+kappa));
mu.add(1.0/(1.0+kappa),y);
Vector diff(y);
diff.add(-1,mu);
lambda.addOuter(diff,kappa/(1.0+kappa));
kappa++;
nu++;
}
/*:5*/
#line 14 "./normal_conjugate.cweb"
;
/*6:*/
#line 80 "./normal_conjugate.cweb"
void NormalConj::update(const ConstTwoDMatrix&ydata)
{
NormalConj nc(ydata);
update(nc);
NormalConj nc(ydata);
update(nc);
}
/*:6*/
#line 15 "./normal_conjugate.cweb"
;
/*7:*/
#line 89 "./normal_conjugate.cweb"
void NormalConj::update(const NormalConj&nc)
{
double wold= ((double)kappa)/(kappa+nc.kappa);
double wnew= 1-wold;
mu.mult(wold);
mu.add(wnew,nc.mu);
Vector diff(nc.mu);
diff.add(-1,mu);
lambda.add(1.0,nc.lambda);
lambda.addOuter(diff);
kappa= kappa+nc.kappa;
nu= nu+nc.kappa;
double wold= ((double)kappa)/(kappa+nc.kappa);
double wnew= 1-wold;
mu.mult(wold);
mu.add(wnew,nc.mu);
Vector diff(nc.mu);
diff.add(-1,mu);
lambda.add(1.0,nc.lambda);
lambda.addOuter(diff);
kappa= kappa+nc.kappa;
nu= nu+nc.kappa;
}
/*:7*/
#line 16 "./normal_conjugate.cweb"
;
/*8:*/
#line 112 "./normal_conjugate.cweb"
void NormalConj::getVariance(TwoDMatrix&v)const
{
if(nu> getDim()+1){
v= (const TwoDMatrix&)lambda;
v.mult(1.0/(nu-getDim()-1));
}else
v.nans();
if(nu> getDim()+1){
v= (const TwoDMatrix&)lambda;
v.mult(1.0/(nu-getDim()-1));
}else
v.nans();
}
/*:8*/
#line 17 "./normal_conjugate.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 37 "./normal_conjugate.hweb"
#ifndef NORMAL_CONJUGATE_H
#define NORMAL_CONJUGATE_H
@ -7,39 +6,35 @@
#include "twod_matrix.h"
/*2:*/
#line 49 "./normal_conjugate.hweb"
class NormalConj{
protected:
Vector mu;
int kappa;
int nu;
TwoDMatrix lambda;
Vector mu;
int kappa;
int nu;
TwoDMatrix lambda;
public:
/*3:*/
#line 76 "./normal_conjugate.hweb"
NormalConj(int d);
NormalConj(const ConstTwoDMatrix&ydata);
NormalConj(const NormalConj&nc);
/*:3*/
#line 57 "./normal_conjugate.hweb"
;
virtual~NormalConj(){}
void update(const ConstVector&y);
void update(const ConstTwoDMatrix&ydata);
void update(const NormalConj&nc);
int getDim()const
{return mu.length();}
const Vector&getMean()const
{return mu;}
void getVariance(TwoDMatrix&v)const;
/*3:*/
NormalConj(int d);
NormalConj(const ConstTwoDMatrix&ydata);
NormalConj(const NormalConj&nc);
/*:3*/
;
virtual~NormalConj(){}
void update(const ConstVector&y);
void update(const ConstTwoDMatrix&ydata);
void update(const NormalConj&nc);
int getDim()const
{return mu.length();}
const Vector&getMean()const
{return mu;}
void getVariance(TwoDMatrix&v)const;
};
/*:2*/
#line 43 "./normal_conjugate.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 5 "./random.cweb"
#include "random.h"
@ -9,65 +8,57 @@
#include <cmath>
/*2:*/
#line 20 "./random.cweb"
int RandomGenerator::int_uniform()
{
double s= std::numeric_limits<int> ::max()*uniform();
return(int)s;
double s= std::numeric_limits<int> ::max()*uniform();
return(int)s;
}
/*:2*/
#line 13 "./random.cweb"
;
/*3:*/
#line 28 "./random.cweb"
double RandomGenerator::normal()
{
double x1,x2;
double w;
do{
x1= 2*uniform()-1;
x2= 2*uniform()-1;
w= x1*x1+x2*x2;
}while(w>=1.0||w<1.0e-30);
return x1*std::sqrt((-2.0*std::log(w))/w);
double x1,x2;
double w;
do{
x1= 2*uniform()-1;
x2= 2*uniform()-1;
w= x1*x1+x2*x2;
}while(w>=1.0||w<1.0e-30);
return x1*std::sqrt((-2.0*std::log(w))/w);
}
/*:3*/
#line 14 "./random.cweb"
;
SystemRandomGenerator system_random_generator;
/*4:*/
#line 42 "./random.cweb"
double SystemRandomGenerator::uniform()
{
#ifndef __MINGW32__
return drand48();
return drand48();
#else
return((double)rand())/RAND_MAX;
return((double)rand())/RAND_MAX;
#endif
}
/*:4*/
#line 16 "./random.cweb"
;
/*5:*/
#line 53 "./random.cweb"
void SystemRandomGenerator::initSeed(int seed)
{
#ifndef __MINGW32__
srand48(seed);
srand48(seed);
#else
srand(seed);
srand(seed);
#endif
}
/*:5*/
#line 17 "./random.cweb"
;
/*:1*/

View File

@ -1,33 +1,28 @@
/*1:*/
#line 9 "./random.hweb"
#ifndef RANDOM_H
#define RANDOM_H
/*2:*/
#line 22 "./random.hweb"
class RandomGenerator{
public:
virtual double uniform()= 0;
int int_uniform();
double normal();
virtual double uniform()= 0;
int int_uniform();
double normal();
};
/*:2*/
#line 13 "./random.hweb"
;
/*3:*/
#line 32 "./random.hweb"
class SystemRandomGenerator:public RandomGenerator{
public:
double uniform();
void initSeed(int seed);
double uniform();
void initSeed(int seed);
};
/*:3*/
#line 14 "./random.hweb"
;
extern SystemRandomGenerator system_random_generator;

View File

@ -38,9 +38,9 @@ bool Rand::discrete(double prob)
struct SparseGenerator {
static FSSparseTensor* makeTensor(int dim, int nv, int r,
double fill, double m);
double fill, double m);
static void fillContainer(TensorContainer<FSSparseTensor>& c,
int maxdim, int nv, int r, double m);
int maxdim, int nv, int r, double m);
};
FSSparseTensor* SparseGenerator::makeTensor(int dim, int nv, int r,
@ -73,139 +73,139 @@ void SparseGenerator::fillContainer(TensorContainer<FSSparseTensor>& c,
const double vdata [] = { // 3x3
0.1307870268, 0.1241940078, 0.1356703123,
0.1241940078, 0.1986920419, 0.2010160581,
0.1356703123, 0.2010160581, 0.2160336975
0.1241940078, 0.1986920419, 0.2010160581,
0.1356703123, 0.2010160581, 0.2160336975
};
const double gy_data [] = { // 8x4
0.3985178619, -0.5688233582, 0.9572900437, -0.6606847776, 0.1453004017,
0.3025310675, -0.8627437750, -0.6903410191, 0.4751910580, -0.7270018589,
-0.0939612498, -0.1463831989, 0.6742110220, 0.6046671043, 0.5215893126,
-1.0412969986, -0.3524898417, -1.0986703430, 0.8006531522, 0.8879776376,
-0.1037608317, -0.5587378073, -0.1010366945, 0.9462411248, -0.2439199881,
1.3420621236, -0.7820285935, 0.3205293447, 0.3606124791, 0.2975422208,
-0.5452861965, 1.6320340279
0.3025310675, -0.8627437750, -0.6903410191, 0.4751910580, -0.7270018589,
-0.0939612498, -0.1463831989, 0.6742110220, 0.6046671043, 0.5215893126,
-1.0412969986, -0.3524898417, -1.0986703430, 0.8006531522, 0.8879776376,
-0.1037608317, -0.5587378073, -0.1010366945, 0.9462411248, -0.2439199881,
1.3420621236, -0.7820285935, 0.3205293447, 0.3606124791, 0.2975422208,
-0.5452861965, 1.6320340279
};
const double gu_data [] = { // just some numbers, no structure
1.8415286914, -0.2638743845, 1.7690713274, 0.9668585956, 0.2303143646,
-0.2229624279, -0.4381991822, 1.0082401405, -0.3186555860, -0.0624691529,
-0.5189085756, 1.4269672156, 0.1163282969, 1.4020183445, -0.0952660426,
0.2099097124, 0.6912400502, -0.5180935114, 0.5288316624, 0.2188053448,
0.5715516767, 0.7813893410, -0.6385073106, 0.8335131513, 0.3605202168,
-1.1167944865, -1.2263750934, 0.6113636081, 0.6964915482, -0.6451217688,
0.4062810500, -2.0552251116, -1.6383406284, 0.0198915095, 0.0111014458,
-1.2421792262, -1.0724161722, -0.4276904972, 0.1801494950, -2.0716473264
-0.2229624279, -0.4381991822, 1.0082401405, -0.3186555860, -0.0624691529,
-0.5189085756, 1.4269672156, 0.1163282969, 1.4020183445, -0.0952660426,
0.2099097124, 0.6912400502, -0.5180935114, 0.5288316624, 0.2188053448,
0.5715516767, 0.7813893410, -0.6385073106, 0.8335131513, 0.3605202168,
-1.1167944865, -1.2263750934, 0.6113636081, 0.6964915482, -0.6451217688,
0.4062810500, -2.0552251116, -1.6383406284, 0.0198915095, 0.0111014458,
-1.2421792262, -1.0724161722, -0.4276904972, 0.1801494950, -2.0716473264
};
const double vdata2 [] = { // 10x10 positive definite
0.79666, -0.15536, 0.05667, -0.21026, 0.20262, 0.28505, 0.60341, -0.09703, 0.32363, 0.13299,
-0.15536, 0.64380, -0.01131, 0.00980, 0.03755, 0.43791, 0.21784, -0.31755, -0.55911, -0.29655,
0.05667, -0.01131, 0.56165, -0.34357, -0.40584, 0.20990, 0.28348, 0.20398, -0.19856, 0.35820,
-0.21026, 0.00980, -0.34357, 0.56147, 0.10972, -0.34146, -0.49906, -0.19685, 0.21088, -0.31560,
0.20262, 0.03755, -0.40584, 0.10972, 0.72278, 0.02155, 0.04089, -0.19696, 0.03446, -0.12919,
0.28505, 0.43791, 0.20990, -0.34146, 0.02155, 0.75867, 0.77699, -0.31125, -0.55141, -0.02155,
0.60341, 0.21784, 0.28348, -0.49906, 0.04089, 0.77699, 1.34553, -0.18613, -0.25811, -0.19016,
-0.09703, -0.31755, 0.20398, -0.19685, -0.19696, -0.31125, -0.18613, 0.59470, 0.08386, 0.41750,
0.32363, -0.55911, -0.19856, 0.21088, 0.03446, -0.55141, -0.25811, 0.08386, 0.98917, -0.12992,
0.13299, -0.29655, 0.35820, -0.31560, -0.12919, -0.02155, -0.19016, 0.41750, -0.12992, 0.89608
-0.15536, 0.64380, -0.01131, 0.00980, 0.03755, 0.43791, 0.21784, -0.31755, -0.55911, -0.29655,
0.05667, -0.01131, 0.56165, -0.34357, -0.40584, 0.20990, 0.28348, 0.20398, -0.19856, 0.35820,
-0.21026, 0.00980, -0.34357, 0.56147, 0.10972, -0.34146, -0.49906, -0.19685, 0.21088, -0.31560,
0.20262, 0.03755, -0.40584, 0.10972, 0.72278, 0.02155, 0.04089, -0.19696, 0.03446, -0.12919,
0.28505, 0.43791, 0.20990, -0.34146, 0.02155, 0.75867, 0.77699, -0.31125, -0.55141, -0.02155,
0.60341, 0.21784, 0.28348, -0.49906, 0.04089, 0.77699, 1.34553, -0.18613, -0.25811, -0.19016,
-0.09703, -0.31755, 0.20398, -0.19685, -0.19696, -0.31125, -0.18613, 0.59470, 0.08386, 0.41750,
0.32363, -0.55911, -0.19856, 0.21088, 0.03446, -0.55141, -0.25811, 0.08386, 0.98917, -0.12992,
0.13299, -0.29655, 0.35820, -0.31560, -0.12919, -0.02155, -0.19016, 0.41750, -0.12992, 0.89608
};
const double gy_data2 [] = { // 600 items make gy 30x20, whose gy(6:25,:) has spectrum within unit
0.39414, -0.29766, 0.08948, -0.19204, -0.00750, 0.21159, 0.05494, 0.06225, 0.01771, 0.21913,
-0.01373, 0.20086, -0.06086, -0.10955, 0.14424, -0.08390, 0.03948, -0.14713, 0.11674, 0.05091,
0.24039, 0.28307, -0.11835, 0.13030, 0.11682, -0.27444, -0.19311, -0.16654, 0.12867, 0.25116,
-0.19781, 0.45242, -0.15862, 0.24428, -0.11966, 0.11483, -0.32279, 0.29727, 0.20934, -0.18190,
-0.15080, -0.09477, -0.30551, -0.02672, -0.26919, 0.11165, -0.06390, 0.03449, -0.26622, 0.22197,
0.45141, -0.41683, 0.09760, 0.31094, -0.01652, 0.05809, -0.04514, -0.05645, 0.00554, 0.47980,
0.11726, 0.42459, -0.13136, -0.30902, -0.14648, 0.11455, 0.02947, -0.03835, -0.04044, 0.03559,
-0.26575, -0.01783, 0.31243, -0.14412, -0.13218, -0.05080, 0.18576, 0.13840, -0.05560, 0.35530,
-0.25573, -0.11560, 0.15187, -0.18431, 0.08193, -0.32278, 0.17560, -0.05529, -0.10020, -0.23088,
-0.20979, -0.49245, 0.09915, -0.16909, -0.03443, 0.19497, 0.18473, 0.25662, 0.29605, -0.20531,
-0.39244, -0.43369, 0.05588, 0.24823, -0.14236, -0.08311, 0.16371, -0.19975, 0.30605, -0.17087,
-0.01270, 0.00123, -0.22426, -0.13810, 0.05079, 0.06971, 0.01922, -0.09952, -0.23177, -0.41962,
-0.41991, 0.41430, -0.04247, -0.13706, -0.12048, -0.28906, -0.22813, -0.25057, -0.18579, -0.20642,
-0.47976, 0.25490, -0.05138, -0.30794, 0.31651, 0.02034, 0.12954, -0.20110, 0.13336, -0.40775,
-0.30195, -0.13704, 0.12396, 0.28152, 0.02986, 0.27669, 0.24623, 0.08635, -0.11956, -0.02949,
0.37401, 0.20838, 0.24801, -0.26872, 0.11195, 0.00315, -0.19069, 0.12839, -0.23036, -0.48228,
0.08434, -0.39872, -0.28896, -0.28754, 0.24668, 0.23285, 0.25437, 0.10456, -0.14124, 0.20483,
-0.19117, -0.33836, -0.24875, 0.08207, -0.03930, 0.20364, 0.15384, -0.15270, 0.24372, -0.11199,
-0.46591, 0.30319, 0.05745, 0.09084, 0.06058, 0.31884, 0.05071, -0.28899, -0.30793, -0.03566,
0.02286, 0.28178, 0.00736, -0.31378, -0.18144, -0.22346, -0.27239, 0.31043, -0.26228, 0.22181,
-0.15096, -0.36953, -0.06032, 0.21496, 0.29545, -0.13112, 0.16420, -0.07573, -0.43111, -0.43057,
0.26716, -0.31209, -0.05866, -0.29101, -0.27437, -0.18727, 0.28732, -0.19014, 0.08837, 0.30405,
0.06103, -0.35612, 0.00173, 0.25134, -0.08987, -0.22766, -0.03254, -0.18662, -0.08491, 0.49401,
-0.12145, -0.02961, -0.03668, -0.30043, -0.08555, 0.01701, -0.12544, 0.10969, -0.48202, 0.07245,
0.20673, 0.11408, 0.04343, -0.01815, -0.31594, -0.23632, -0.06258, -0.27474, 0.12180, 0.16613,
-0.37931, 0.30219, 0.15765, 0.25489, 0.17529, -0.17020, -0.30060, 0.22058, -0.02450, -0.42143,
0.49642, 0.46899, -0.28552, -0.22549, -0.01333, 0.21567, 0.22251, 0.21639, -0.19194, -0.19140,
-0.24106, 0.10952, -0.11019, 0.29763, -0.02039, -0.25748, 0.23169, 0.01357, 0.09802, -0.19022,
0.37604, -0.40777, 0.18131, -0.10258, 0.29573, -0.31773, 0.09069, -0.02198, -0.26594, 0.48302,
-0.10041, 0.20210, -0.05609, -0.01169, -0.17339, 0.17862, -0.22502, 0.29009, -0.45160, 0.19771,
0.27634, 0.31695, -0.09993, 0.17167, 0.12394, 0.28088, -0.12502, -0.16967, -0.06296, -0.17036,
0.27320, 0.01595, 0.16955, 0.30146, -0.15173, -0.29807, 0.08178, -0.06811, 0.21655, 0.26348,
0.06316, 0.45661, -0.29756, -0.05742, -0.14715, -0.03037, -0.16656, -0.08768, 0.38078, 0.40679,
-0.32779, -0.09106, 0.16107, -0.07301, 0.07700, -0.22694, -0.15692, -0.02548, 0.38749, -0.12203,
-0.02980, -0.22067, 0.00680, -0.23058, -0.29112, 0.23032, -0.16026, 0.23392, -0.09990, 0.03628,
-0.42592, -0.33474, -0.09499, -0.17442, -0.20110, 0.24618, -0.06418, -0.06715, 0.40754, 0.29377,
0.29543, -0.16832, -0.08468, 0.06491, -0.01410, 0.19988, 0.24950, 0.14626, -0.27851, 0.06079,
0.48134, -0.13475, 0.25398, 0.11738, 0.23369, -0.00661, -0.16811, -0.04557, -0.12030, -0.39527,
-0.35760, 0.01840, -0.15941, 0.03290, 0.09988, -0.08307, 0.06644, -0.24637, 0.34112, -0.08026,
0.00951, 0.27656, 0.16247, 0.28217, 0.17198, -0.16389, -0.03835, -0.02675, -0.08032, -0.21045,
-0.38946, 0.23207, 0.10987, -0.31674, -0.28653, -0.27430, -0.29109, -0.00648, 0.38431, -0.38478,
-0.41195, -0.19364, -0.20977, -0.05524, 0.05558, -0.20109, 0.11803, -0.19884, 0.43318, -0.39255,
0.26612, -0.21771, 0.12471, 0.12856, -0.15104, -0.11676, 0.17582, -0.25330, 0.00298, -0.31712,
0.21532, -0.20319, 0.14507, -0.04588, -0.22995, -0.06470, 0.18849, -0.13444, 0.37107, 0.07387,
-0.14008, 0.09896, 0.13727, -0.28417, -0.09461, -0.18703, 0.04080, 0.02343, -0.49988, 0.17993,
0.23189, -0.30581, -0.18334, -0.09667, -0.27699, -0.05998, 0.09118, -0.32453, 0.46251, 0.41500,
-0.45314, -0.00544, 0.08529, 0.29099, -0.00937, -0.31650, 0.26163, 0.14506, 0.37498, -0.16454,
0.35215, 0.31642, -0.09161, -0.31452, -0.04792, -0.04677, -0.19523, 0.27998, 0.05491, 0.44461,
-0.01258, -0.27887, 0.18361, -0.04539, -0.02977, 0.30821, 0.29454, -0.17932, 0.16193, 0.23934,
0.47923, 0.25373, 0.23258, 0.31484, -0.17958, -0.01136, 0.17681, 0.12869, 0.03235, 0.43762,
0.13734, -0.09433, -0.03735, 0.17949, 0.14122, -0.17814, 0.06359, 0.16044, 0.12249, -0.22314,
0.40775, 0.05147, 0.12389, 0.04290, -0.01642, 0.00082, -0.18056, 0.02875, 0.32690, 0.17712,
0.34001, -0.21581, -0.01086, -0.18180, 0.17480, -0.17774, -0.07503, 0.28438, -0.19747, 0.29595,
-0.28002, -0.02073, -0.16522, -0.18234, -0.20565, 0.29620, 0.07502, 0.01429, -0.31418, 0.43693,
-0.12212, 0.11178, -0.28503, 0.04683, 0.00072, 0.05566, 0.18857, 0.26101, -0.38891, -0.21216,
-0.21850, -0.15147, -0.30749, -0.23762, 0.14984, 0.03535, -0.02862, -0.00105, -0.39907, -0.06909,
-0.36094, 0.21717, 0.15930, -0.18924, 0.13741, 0.01039, 0.13613, 0.00659, 0.07676, -0.13711,
0.24285, -0.07564, -0.28349, -0.15658, 0.03135, -0.30909, -0.22534, 0.17363, -0.19376, 0.26038,
0.05546, -0.22607, 0.32420, -0.02552, -0.05400, 0.13388, 0.04643, -0.31535, -0.06181, 0.30237,
-0.04680, -0.29441, 0.12231, 0.03960, -0.01188, 0.01406, 0.25402, 0.03315, 0.25026, -0.10922
-0.01373, 0.20086, -0.06086, -0.10955, 0.14424, -0.08390, 0.03948, -0.14713, 0.11674, 0.05091,
0.24039, 0.28307, -0.11835, 0.13030, 0.11682, -0.27444, -0.19311, -0.16654, 0.12867, 0.25116,
-0.19781, 0.45242, -0.15862, 0.24428, -0.11966, 0.11483, -0.32279, 0.29727, 0.20934, -0.18190,
-0.15080, -0.09477, -0.30551, -0.02672, -0.26919, 0.11165, -0.06390, 0.03449, -0.26622, 0.22197,
0.45141, -0.41683, 0.09760, 0.31094, -0.01652, 0.05809, -0.04514, -0.05645, 0.00554, 0.47980,
0.11726, 0.42459, -0.13136, -0.30902, -0.14648, 0.11455, 0.02947, -0.03835, -0.04044, 0.03559,
-0.26575, -0.01783, 0.31243, -0.14412, -0.13218, -0.05080, 0.18576, 0.13840, -0.05560, 0.35530,
-0.25573, -0.11560, 0.15187, -0.18431, 0.08193, -0.32278, 0.17560, -0.05529, -0.10020, -0.23088,
-0.20979, -0.49245, 0.09915, -0.16909, -0.03443, 0.19497, 0.18473, 0.25662, 0.29605, -0.20531,
-0.39244, -0.43369, 0.05588, 0.24823, -0.14236, -0.08311, 0.16371, -0.19975, 0.30605, -0.17087,
-0.01270, 0.00123, -0.22426, -0.13810, 0.05079, 0.06971, 0.01922, -0.09952, -0.23177, -0.41962,
-0.41991, 0.41430, -0.04247, -0.13706, -0.12048, -0.28906, -0.22813, -0.25057, -0.18579, -0.20642,
-0.47976, 0.25490, -0.05138, -0.30794, 0.31651, 0.02034, 0.12954, -0.20110, 0.13336, -0.40775,
-0.30195, -0.13704, 0.12396, 0.28152, 0.02986, 0.27669, 0.24623, 0.08635, -0.11956, -0.02949,
0.37401, 0.20838, 0.24801, -0.26872, 0.11195, 0.00315, -0.19069, 0.12839, -0.23036, -0.48228,
0.08434, -0.39872, -0.28896, -0.28754, 0.24668, 0.23285, 0.25437, 0.10456, -0.14124, 0.20483,
-0.19117, -0.33836, -0.24875, 0.08207, -0.03930, 0.20364, 0.15384, -0.15270, 0.24372, -0.11199,
-0.46591, 0.30319, 0.05745, 0.09084, 0.06058, 0.31884, 0.05071, -0.28899, -0.30793, -0.03566,
0.02286, 0.28178, 0.00736, -0.31378, -0.18144, -0.22346, -0.27239, 0.31043, -0.26228, 0.22181,
-0.15096, -0.36953, -0.06032, 0.21496, 0.29545, -0.13112, 0.16420, -0.07573, -0.43111, -0.43057,
0.26716, -0.31209, -0.05866, -0.29101, -0.27437, -0.18727, 0.28732, -0.19014, 0.08837, 0.30405,
0.06103, -0.35612, 0.00173, 0.25134, -0.08987, -0.22766, -0.03254, -0.18662, -0.08491, 0.49401,
-0.12145, -0.02961, -0.03668, -0.30043, -0.08555, 0.01701, -0.12544, 0.10969, -0.48202, 0.07245,
0.20673, 0.11408, 0.04343, -0.01815, -0.31594, -0.23632, -0.06258, -0.27474, 0.12180, 0.16613,
-0.37931, 0.30219, 0.15765, 0.25489, 0.17529, -0.17020, -0.30060, 0.22058, -0.02450, -0.42143,
0.49642, 0.46899, -0.28552, -0.22549, -0.01333, 0.21567, 0.22251, 0.21639, -0.19194, -0.19140,
-0.24106, 0.10952, -0.11019, 0.29763, -0.02039, -0.25748, 0.23169, 0.01357, 0.09802, -0.19022,
0.37604, -0.40777, 0.18131, -0.10258, 0.29573, -0.31773, 0.09069, -0.02198, -0.26594, 0.48302,
-0.10041, 0.20210, -0.05609, -0.01169, -0.17339, 0.17862, -0.22502, 0.29009, -0.45160, 0.19771,
0.27634, 0.31695, -0.09993, 0.17167, 0.12394, 0.28088, -0.12502, -0.16967, -0.06296, -0.17036,
0.27320, 0.01595, 0.16955, 0.30146, -0.15173, -0.29807, 0.08178, -0.06811, 0.21655, 0.26348,
0.06316, 0.45661, -0.29756, -0.05742, -0.14715, -0.03037, -0.16656, -0.08768, 0.38078, 0.40679,
-0.32779, -0.09106, 0.16107, -0.07301, 0.07700, -0.22694, -0.15692, -0.02548, 0.38749, -0.12203,
-0.02980, -0.22067, 0.00680, -0.23058, -0.29112, 0.23032, -0.16026, 0.23392, -0.09990, 0.03628,
-0.42592, -0.33474, -0.09499, -0.17442, -0.20110, 0.24618, -0.06418, -0.06715, 0.40754, 0.29377,
0.29543, -0.16832, -0.08468, 0.06491, -0.01410, 0.19988, 0.24950, 0.14626, -0.27851, 0.06079,
0.48134, -0.13475, 0.25398, 0.11738, 0.23369, -0.00661, -0.16811, -0.04557, -0.12030, -0.39527,
-0.35760, 0.01840, -0.15941, 0.03290, 0.09988, -0.08307, 0.06644, -0.24637, 0.34112, -0.08026,
0.00951, 0.27656, 0.16247, 0.28217, 0.17198, -0.16389, -0.03835, -0.02675, -0.08032, -0.21045,
-0.38946, 0.23207, 0.10987, -0.31674, -0.28653, -0.27430, -0.29109, -0.00648, 0.38431, -0.38478,
-0.41195, -0.19364, -0.20977, -0.05524, 0.05558, -0.20109, 0.11803, -0.19884, 0.43318, -0.39255,
0.26612, -0.21771, 0.12471, 0.12856, -0.15104, -0.11676, 0.17582, -0.25330, 0.00298, -0.31712,
0.21532, -0.20319, 0.14507, -0.04588, -0.22995, -0.06470, 0.18849, -0.13444, 0.37107, 0.07387,
-0.14008, 0.09896, 0.13727, -0.28417, -0.09461, -0.18703, 0.04080, 0.02343, -0.49988, 0.17993,
0.23189, -0.30581, -0.18334, -0.09667, -0.27699, -0.05998, 0.09118, -0.32453, 0.46251, 0.41500,
-0.45314, -0.00544, 0.08529, 0.29099, -0.00937, -0.31650, 0.26163, 0.14506, 0.37498, -0.16454,
0.35215, 0.31642, -0.09161, -0.31452, -0.04792, -0.04677, -0.19523, 0.27998, 0.05491, 0.44461,
-0.01258, -0.27887, 0.18361, -0.04539, -0.02977, 0.30821, 0.29454, -0.17932, 0.16193, 0.23934,
0.47923, 0.25373, 0.23258, 0.31484, -0.17958, -0.01136, 0.17681, 0.12869, 0.03235, 0.43762,
0.13734, -0.09433, -0.03735, 0.17949, 0.14122, -0.17814, 0.06359, 0.16044, 0.12249, -0.22314,
0.40775, 0.05147, 0.12389, 0.04290, -0.01642, 0.00082, -0.18056, 0.02875, 0.32690, 0.17712,
0.34001, -0.21581, -0.01086, -0.18180, 0.17480, -0.17774, -0.07503, 0.28438, -0.19747, 0.29595,
-0.28002, -0.02073, -0.16522, -0.18234, -0.20565, 0.29620, 0.07502, 0.01429, -0.31418, 0.43693,
-0.12212, 0.11178, -0.28503, 0.04683, 0.00072, 0.05566, 0.18857, 0.26101, -0.38891, -0.21216,
-0.21850, -0.15147, -0.30749, -0.23762, 0.14984, 0.03535, -0.02862, -0.00105, -0.39907, -0.06909,
-0.36094, 0.21717, 0.15930, -0.18924, 0.13741, 0.01039, 0.13613, 0.00659, 0.07676, -0.13711,
0.24285, -0.07564, -0.28349, -0.15658, 0.03135, -0.30909, -0.22534, 0.17363, -0.19376, 0.26038,
0.05546, -0.22607, 0.32420, -0.02552, -0.05400, 0.13388, 0.04643, -0.31535, -0.06181, 0.30237,
-0.04680, -0.29441, 0.12231, 0.03960, -0.01188, 0.01406, 0.25402, 0.03315, 0.25026, -0.10922
};
const double gu_data2 [] = { // raw data 300 items
0.26599, 0.41329, 0.31846, 0.92590, 0.43050, 0.17466, 0.02322, 0.72621, 0.37921, 0.70597,
0.97098, 0.14023, 0.57619, 0.09938, 0.02281, 0.92341, 0.72654, 0.71000, 0.76687, 0.70182,
0.88752, 0.49524, 0.42549, 0.42806, 0.57615, 0.76051, 0.15341, 0.47457, 0.60066, 0.40880,
0.20668, 0.41949, 0.97620, 0.94318, 0.71491, 0.56402, 0.23553, 0.94387, 0.78567, 0.06362,
0.85252, 0.86262, 0.25190, 0.03274, 0.93216, 0.37971, 0.08797, 0.14596, 0.73871, 0.06574,
0.67447, 0.28575, 0.43911, 0.92133, 0.12327, 0.87762, 0.71060, 0.07141, 0.55443, 0.53310,
0.91529, 0.25121, 0.07593, 0.94490, 0.28656, 0.82174, 0.68887, 0.67337, 0.99291, 0.03316,
0.02849, 0.33891, 0.25594, 0.90071, 0.01248, 0.67871, 0.65953, 0.65369, 0.97574, 0.31578,
0.23678, 0.39220, 0.06706, 0.80943, 0.57694, 0.08220, 0.18151, 0.19969, 0.37096, 0.37858,
0.70153, 0.46816, 0.76511, 0.02520, 0.39387, 0.25527, 0.39050, 0.60141, 0.30322, 0.46195,
0.12025, 0.33616, 0.04174, 0.00196, 0.68886, 0.74445, 0.15869, 0.18994, 0.95195, 0.62874,
0.82874, 0.53369, 0.34383, 0.50752, 0.97023, 0.22695, 0.62407, 0.25840, 0.71279, 0.28785,
0.31611, 0.20391, 0.19702, 0.40760, 0.85158, 0.68369, 0.63760, 0.09879, 0.11924, 0.32920,
0.53052, 0.15900, 0.21229, 0.84080, 0.33933, 0.93651, 0.42705, 0.06199, 0.50092, 0.47192,
0.57152, 0.01818, 0.31404, 0.50173, 0.87725, 0.50530, 0.10717, 0.04035, 0.32901, 0.33538,
0.04780, 0.40984, 0.78216, 0.91288, 0.11314, 0.25248, 0.23823, 0.74001, 0.48089, 0.55531,
0.82486, 0.01058, 0.05409, 0.44357, 0.52641, 0.68188, 0.94629, 0.61627, 0.33037, 0.11961,
0.57988, 0.19653, 0.91902, 0.59838, 0.52974, 0.28364, 0.45767, 0.65836, 0.63045, 0.76140,
0.27918, 0.27256, 0.46035, 0.77418, 0.92918, 0.14095, 0.89645, 0.25146, 0.21172, 0.47910,
0.95451, 0.34377, 0.29927, 0.79220, 0.97654, 0.67591, 0.44385, 0.38434, 0.44860, 0.28170,
0.90712, 0.20337, 0.00292, 0.55046, 0.62255, 0.45127, 0.80896, 0.43965, 0.59145, 0.23801,
0.33601, 0.30119, 0.89935, 0.40850, 0.98226, 0.75430, 0.68318, 0.65407, 0.68067, 0.32942,
0.11756, 0.27626, 0.83879, 0.72174, 0.75430, 0.13702, 0.03402, 0.58781, 0.07393, 0.23067,
0.92537, 0.29445, 0.43437, 0.47685, 0.54548, 0.66082, 0.23805, 0.60208, 0.94337, 0.21363,
0.72637, 0.57181, 0.77679, 0.63931, 0.72860, 0.38901, 0.94920, 0.04535, 0.12863, 0.40550,
0.90095, 0.21418, 0.13953, 0.99639, 0.02526, 0.70018, 0.21828, 0.20294, 0.20191, 0.30954,
0.39490, 0.68955, 0.11506, 0.15748, 0.40252, 0.91680, 0.61547, 0.78443, 0.19693, 0.67630,
0.56552, 0.58556, 0.53554, 0.53507, 0.09831, 0.21229, 0.83135, 0.26375, 0.89287, 0.97069,
0.70615, 0.42041, 0.43117, 0.21291, 0.26086, 0.26978, 0.77340, 0.43833, 0.46179, 0.54418,
0.67878, 0.42776, 0.61454, 0.55915, 0.36363, 0.31999, 0.42442, 0.86649, 0.62513, 0.02047
0.97098, 0.14023, 0.57619, 0.09938, 0.02281, 0.92341, 0.72654, 0.71000, 0.76687, 0.70182,
0.88752, 0.49524, 0.42549, 0.42806, 0.57615, 0.76051, 0.15341, 0.47457, 0.60066, 0.40880,
0.20668, 0.41949, 0.97620, 0.94318, 0.71491, 0.56402, 0.23553, 0.94387, 0.78567, 0.06362,
0.85252, 0.86262, 0.25190, 0.03274, 0.93216, 0.37971, 0.08797, 0.14596, 0.73871, 0.06574,
0.67447, 0.28575, 0.43911, 0.92133, 0.12327, 0.87762, 0.71060, 0.07141, 0.55443, 0.53310,
0.91529, 0.25121, 0.07593, 0.94490, 0.28656, 0.82174, 0.68887, 0.67337, 0.99291, 0.03316,
0.02849, 0.33891, 0.25594, 0.90071, 0.01248, 0.67871, 0.65953, 0.65369, 0.97574, 0.31578,
0.23678, 0.39220, 0.06706, 0.80943, 0.57694, 0.08220, 0.18151, 0.19969, 0.37096, 0.37858,
0.70153, 0.46816, 0.76511, 0.02520, 0.39387, 0.25527, 0.39050, 0.60141, 0.30322, 0.46195,
0.12025, 0.33616, 0.04174, 0.00196, 0.68886, 0.74445, 0.15869, 0.18994, 0.95195, 0.62874,
0.82874, 0.53369, 0.34383, 0.50752, 0.97023, 0.22695, 0.62407, 0.25840, 0.71279, 0.28785,
0.31611, 0.20391, 0.19702, 0.40760, 0.85158, 0.68369, 0.63760, 0.09879, 0.11924, 0.32920,
0.53052, 0.15900, 0.21229, 0.84080, 0.33933, 0.93651, 0.42705, 0.06199, 0.50092, 0.47192,
0.57152, 0.01818, 0.31404, 0.50173, 0.87725, 0.50530, 0.10717, 0.04035, 0.32901, 0.33538,
0.04780, 0.40984, 0.78216, 0.91288, 0.11314, 0.25248, 0.23823, 0.74001, 0.48089, 0.55531,
0.82486, 0.01058, 0.05409, 0.44357, 0.52641, 0.68188, 0.94629, 0.61627, 0.33037, 0.11961,
0.57988, 0.19653, 0.91902, 0.59838, 0.52974, 0.28364, 0.45767, 0.65836, 0.63045, 0.76140,
0.27918, 0.27256, 0.46035, 0.77418, 0.92918, 0.14095, 0.89645, 0.25146, 0.21172, 0.47910,
0.95451, 0.34377, 0.29927, 0.79220, 0.97654, 0.67591, 0.44385, 0.38434, 0.44860, 0.28170,
0.90712, 0.20337, 0.00292, 0.55046, 0.62255, 0.45127, 0.80896, 0.43965, 0.59145, 0.23801,
0.33601, 0.30119, 0.89935, 0.40850, 0.98226, 0.75430, 0.68318, 0.65407, 0.68067, 0.32942,
0.11756, 0.27626, 0.83879, 0.72174, 0.75430, 0.13702, 0.03402, 0.58781, 0.07393, 0.23067,
0.92537, 0.29445, 0.43437, 0.47685, 0.54548, 0.66082, 0.23805, 0.60208, 0.94337, 0.21363,
0.72637, 0.57181, 0.77679, 0.63931, 0.72860, 0.38901, 0.94920, 0.04535, 0.12863, 0.40550,
0.90095, 0.21418, 0.13953, 0.99639, 0.02526, 0.70018, 0.21828, 0.20294, 0.20191, 0.30954,
0.39490, 0.68955, 0.11506, 0.15748, 0.40252, 0.91680, 0.61547, 0.78443, 0.19693, 0.67630,
0.56552, 0.58556, 0.53554, 0.53507, 0.09831, 0.21229, 0.83135, 0.26375, 0.89287, 0.97069,
0.70615, 0.42041, 0.43117, 0.21291, 0.26086, 0.26978, 0.77340, 0.43833, 0.46179, 0.54418,
0.67878, 0.42776, 0.61454, 0.55915, 0.36363, 0.31999, 0.42442, 0.86649, 0.62513, 0.02047
};
class TestRunnable {
@ -215,16 +215,16 @@ public:
int nvar; // number of variable of the solved problem
TestRunnable(const char* n, int d, int nv)
: dim(d), nvar(nv)
{strncpy(name, n, 100);}
{strncpy(name, n, 100);}
bool test() const;
virtual bool run() const =0;
const char* getName() const
{return name;}
{return name;}
protected:
static double korder_unfold_fold(int maxdim, int unfold_dim,
int nstat, int npred, int nboth, int forw,
const TwoDMatrix& gy, const TwoDMatrix& gu,
const TwoDMatrix& v);
int nstat, int npred, int nboth, int forw,
const TwoDMatrix& gy, const TwoDMatrix& gu,
const TwoDMatrix& v);
};
@ -235,7 +235,7 @@ bool TestRunnable::test() const
bool passed = run();
clock_t end = clock();
printf("CPU time %8.4g (CPU seconds)..................",
((double)(end-start))/CLOCKS_PER_SEC);
((double)(end-start))/CLOCKS_PER_SEC);
if (passed) {
printf("passed\n\n");
return passed;
@ -257,7 +257,7 @@ double TestRunnable::korder_unfold_fold(int maxdim, int unfold_dim,
SparseGenerator::fillContainer(c, maxdim, nz, ny, 5.0);
for (int d = 1; d <= maxdim; d++) {
printf("\ttensor fill for dim=%d is: %3.2f %%\n",
d, c.get(Symmetry(d))->getFillFactor()*100.0);
d, c.get(Symmetry(d))->getFillFactor()*100.0);
}
Journal jr("out.txt");
KOrder kord(nstat, npred, nboth, nforw, c, gy, gu, v, jr);
@ -268,14 +268,14 @@ double TestRunnable::korder_unfold_fold(int maxdim, int unfold_dim,
kord.performStep<KOrder::unfold>(d);
pertime = clock()-pertime;
printf("\ttime for unfolded step dim=%d: %8.4g\n",
d, ((double)(pertime))/CLOCKS_PER_SEC);
d, ((double)(pertime))/CLOCKS_PER_SEC);
clock_t checktime = clock();
double err = kord.check<KOrder::unfold>(d);
checktime = clock()-checktime;
printf("\ttime for step check dim=%d: %8.4g\n",
d, ((double)(checktime))/CLOCKS_PER_SEC);
d, ((double)(checktime))/CLOCKS_PER_SEC);
printf("\tmax error in step dim=%d: %10.6g\n",
d, err);
d, err);
if (maxerror < err)
maxerror = err;
}
@ -285,21 +285,21 @@ double TestRunnable::korder_unfold_fold(int maxdim, int unfold_dim,
kord.switchToFolded();
swtime = clock()-swtime;
printf("\ttime for switching dim=%d: %8.4g\n",
unfold_dim, ((double)(swtime))/CLOCKS_PER_SEC);
unfold_dim, ((double)(swtime))/CLOCKS_PER_SEC);
for (int d = unfold_dim+1; d <= maxdim; d++) {
clock_t pertime = clock();
kord.performStep<KOrder::fold>(d);
pertime = clock()-pertime;
printf("\ttime for folded step dim=%d: %8.4g\n",
d, ((double)(pertime))/CLOCKS_PER_SEC);
d, ((double)(pertime))/CLOCKS_PER_SEC);
clock_t checktime = clock();
double err = kord.check<KOrder::fold>(d);
checktime = clock()-checktime;
printf("\ttime for step check dim=%d: %8.4g\n",
d, ((double)(checktime))/CLOCKS_PER_SEC);
d, ((double)(checktime))/CLOCKS_PER_SEC);
printf("\tmax error in step dim=%d: %10.6g\n",
d, err);
d, err);
if (maxerror < err)
maxerror = err;
}
@ -312,17 +312,17 @@ public:
UnfoldKOrderSmall()
: TestRunnable("unfold-3 fold-4 korder (stat=2,pred=3,both=1,forw=2,u=3,dim=4)",
4, 18) {}
bool run() const
{
TwoDMatrix gy(8, 4, gy_data);
TwoDMatrix gu(8, 3, gu_data);
TwoDMatrix v(3, 3, vdata);
double err = korder_unfold_fold(4, 3, 2, 3, 1, 2,
gy, gu, v);
return err < 0.08;
}
{
TwoDMatrix gy(8, 4, gy_data);
TwoDMatrix gu(8, 3, gu_data);
TwoDMatrix v(3, 3, vdata);
double err = korder_unfold_fold(4, 3, 2, 3, 1, 2,
gy, gu, v);
return err < 0.08;
}
};
// same dimension as Smets & Wouters
@ -331,19 +331,19 @@ public:
UnfoldKOrderSW()
: TestRunnable("unfold S&W korder (stat=5,pred=12,both=8,forw=5,u=10,dim=4)",
4, 73) {}
bool run() const
{
TwoDMatrix gy(30, 20, gy_data2);
TwoDMatrix gu(30, 10, gu_data2);
TwoDMatrix v(10, 10, vdata2);
v.mult(0.001);
gu.mult(.01);
double err = korder_unfold_fold(4, 4, 5, 12, 8, 5,
gy, gu, v);
return err < 0.08;
}
{
TwoDMatrix gy(30, 20, gy_data2);
TwoDMatrix gu(30, 10, gu_data2);
TwoDMatrix v(10, 10, vdata2);
v.mult(0.001);
gu.mult(.01);
double err = korder_unfold_fold(4, 4, 5, 12, 8, 5,
gy, gu, v);
return err < 0.08;
}
};
class UnfoldFoldKOrderSW : public TestRunnable {
@ -351,19 +351,19 @@ public:
UnfoldFoldKOrderSW()
: TestRunnable("unfold-2 fold-3 S&W korder (stat=5,pred=12,both=8,forw=5,u=10,dim=3)",
4, 73) {}
bool run() const
{
TwoDMatrix gy(30, 20, gy_data2);
TwoDMatrix gu(30, 10, gu_data2);
TwoDMatrix v(10, 10, vdata2);
v.mult(0.001);
gu.mult(.01);
double err = korder_unfold_fold(4, 3, 5, 12, 8, 5,
gy, gu, v);
return err < 0.08;
}
{
TwoDMatrix gy(30, 20, gy_data2);
TwoDMatrix gu(30, 10, gu_data2);
TwoDMatrix v(10, 10, vdata2);
v.mult(0.001);
gu.mult(.01);
double err = korder_unfold_fold(4, 3, 5, 12, 8, 5,
gy, gu, v);
return err < 0.08;
}
};
int main()
@ -374,7 +374,7 @@ int main()
all_tests[num_tests++] = new UnfoldKOrderSmall();
all_tests[num_tests++] = new UnfoldKOrderSW();
all_tests[num_tests++] = new UnfoldFoldKOrderSW();
// find maximum dimension and maximum nvar
int dmax=0;
int nvmax = 0;
@ -385,7 +385,7 @@ int main()
nvmax = all_tests[i]->nvar;
}
tls.init(dmax, nvmax); // initialize library
// launch the tests
int success = 0;
for (int i = 0; i < num_tests; i++) {
@ -400,14 +400,14 @@ int main()
e.printMessage();
}
}
printf("There were %d tests that failed out of %d tests run.\n",
num_tests - success, num_tests);
num_tests - success, num_tests);
// destroy
for (int i = 0; i < num_tests; i++) {
delete all_tests[i];
}
return 0;
}

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./equivalence.cweb"
#include "equivalence.h"
#include "permutation.h"
@ -8,159 +7,139 @@
#include <string.h>
/*2:*/
#line 19 "./equivalence.cweb"
/*6:*/
#line 57 "./equivalence.cweb"
int OrdSequence::operator[](int i)const
{
TL_RAISE_IF((i<0||i>=length()),
"Index out of range in OrdSequence::operator[]");
return data[i];
TL_RAISE_IF((i<0||i>=length()),
"Index out of range in OrdSequence::operator[]");
return data[i];
}
/*:6*/
#line 20 "./equivalence.cweb"
;
/*7:*/
#line 69 "./equivalence.cweb"
bool OrdSequence::operator<(const OrdSequence&s)const
{
double ta= average();
double sa= s.average();
return(ta<sa||((ta==sa)&&(operator[](0)> s[0])));
double ta= average();
double sa= s.average();
return(ta<sa||((ta==sa)&&(operator[](0)> s[0])));
}
/*:7*/
#line 21 "./equivalence.cweb"
;
/*8:*/
#line 78 "./equivalence.cweb"
bool OrdSequence::operator==(const OrdSequence&s)const
{
if(length()!=s.length())
return false;
int i= 0;
while(i<length()&&operator[](i)==s[i])
i++;
return(i==length());
if(length()!=s.length())
return false;
int i= 0;
while(i<length()&&operator[](i)==s[i])
i++;
return(i==length());
}
/*:8*/
#line 22 "./equivalence.cweb"
;
/*9:*/
#line 96 "./equivalence.cweb"
void OrdSequence::add(int i)
{
vector<int> ::iterator vit= data.begin();
while(vit!=data.end()&&*vit<i)
++vit;
if(vit!=data.end()&&*vit==i)
return;
data.insert(vit,i);
vector<int> ::iterator vit= data.begin();
while(vit!=data.end()&&*vit<i)
++vit;
if(vit!=data.end()&&*vit==i)
return;
data.insert(vit,i);
}
void OrdSequence::add(const OrdSequence&s)
{
vector<int> ::const_iterator vit= s.data.begin();
while(vit!=s.data.end()){
add(*vit);
++vit;
}
vector<int> ::const_iterator vit= s.data.begin();
while(vit!=s.data.end()){
add(*vit);
++vit;
}
}
/*:9*/
#line 23 "./equivalence.cweb"
;
/*10:*/
#line 117 "./equivalence.cweb"
bool OrdSequence::has(int i)const
{
vector<int> ::const_iterator vit= data.begin();
while(vit!=data.end()){
if(*vit==i)
return true;
++vit;
}
return false;
vector<int> ::const_iterator vit= data.begin();
while(vit!=data.end()){
if(*vit==i)
return true;
++vit;
}
return false;
}
/*:10*/
#line 24 "./equivalence.cweb"
;
/*11:*/
#line 130 "./equivalence.cweb"
double OrdSequence::average()const
{
double res= 0;
for(unsigned int i= 0;i<data.size();i++)
res+= data[i];
TL_RAISE_IF(data.size()==0,
"Attempt to take average of empty class in OrdSequence::average");
return res/data.size();
double res= 0;
for(unsigned int i= 0;i<data.size();i++)
res+= data[i];
TL_RAISE_IF(data.size()==0,
"Attempt to take average of empty class in OrdSequence::average");
return res/data.size();
}
/*:11*/
#line 25 "./equivalence.cweb"
;
/*12:*/
#line 142 "./equivalence.cweb"
void OrdSequence::print(const char*prefix)const
{
printf("%s",prefix);
for(unsigned int i= 0;i<data.size();i++)
printf("%d ",data[i]);
printf("\n");
printf("%s",prefix);
for(unsigned int i= 0;i<data.size();i++)
printf("%d ",data[i]);
printf("\n");
}
/*:12*/
#line 26 "./equivalence.cweb"
;
/*:2*/
#line 13 "./equivalence.cweb"
;
/*3:*/
#line 29 "./equivalence.cweb"
/*13:*/
#line 152 "./equivalence.cweb"
Equivalence::Equivalence(int num)
:n(num)
{
for(int i= 0;i<num;i++){
OrdSequence s;
s.add(i);
classes.push_back(s);
}
for(int i= 0;i<num;i++){
OrdSequence s;
s.add(i);
classes.push_back(s);
}
}
Equivalence::Equivalence(int num,const char*dummy)
:n(num)
{
OrdSequence s;
for(int i= 0;i<num;i++)
s.add(i);
classes.push_back(s);
OrdSequence s;
for(int i= 0;i<num;i++)
s.add(i);
classes.push_back(s);
}
/*:13*/
#line 30 "./equivalence.cweb"
;
/*14:*/
#line 173 "./equivalence.cweb"
Equivalence::Equivalence(const Equivalence&e)
:n(e.n),
@ -172,342 +151,304 @@ Equivalence::Equivalence(const Equivalence&e,int i1,int i2)
:n(e.n),
classes(e.classes)
{
seqit s1= find(i1);
seqit s2= find(i2);
if(s1!=s2){
OrdSequence ns(*s1);
ns.add(*s2);
classes.erase(s1);
classes.erase(s2);
insert(ns);
}
seqit s1= find(i1);
seqit s2= find(i2);
if(s1!=s2){
OrdSequence ns(*s1);
ns.add(*s2);
classes.erase(s1);
classes.erase(s2);
insert(ns);
}
}
/*:14*/
#line 31 "./equivalence.cweb"
;
/*17:*/
#line 220 "./equivalence.cweb"
Equivalence::const_seqit Equivalence::findHaving(int i)const
{
const_seqit si= classes.begin();
while(si!=classes.end()){
if((*si).has(i))
return si;
++si;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::findHaving");
return si;
const_seqit si= classes.begin();
while(si!=classes.end()){
if((*si).has(i))
return si;
++si;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::findHaving");
return si;
}
Equivalence::seqit Equivalence::findHaving(int i)
{
seqit si= classes.begin();
while(si!=classes.end()){
if((*si).has(i))
return si;
++si;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::findHaving");
return si;
seqit si= classes.begin();
while(si!=classes.end()){
if((*si).has(i))
return si;
++si;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::findHaving");
return si;
}
/*:17*/
#line 32 "./equivalence.cweb"
;
/*18:*/
#line 249 "./equivalence.cweb"
Equivalence::const_seqit Equivalence::find(int j)const
{
const_seqit si= classes.begin();
int i= 0;
while(si!=classes.end()&&i<j){
++si;
i++;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::find");
return si;
const_seqit si= classes.begin();
int i= 0;
while(si!=classes.end()&&i<j){
++si;
i++;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::find");
return si;
}
Equivalence::seqit Equivalence::find(int j)
{
seqit si= classes.begin();
int i= 0;
while(si!=classes.end()&&i<j){
++si;
i++;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::find");
return si;
seqit si= classes.begin();
int i= 0;
while(si!=classes.end()&&i<j){
++si;
i++;
}
TL_RAISE_IF(si==classes.end(),
"Couldn't find equivalence class in Equivalence::find");
return si;
}
/*:18*/
#line 33 "./equivalence.cweb"
;
/*19:*/
#line 278 "./equivalence.cweb"
void Equivalence::insert(const OrdSequence&s)
{
seqit si= classes.begin();
while(si!=classes.end()&&*si<s)
++si;
classes.insert(si,s);
seqit si= classes.begin();
while(si!=classes.end()&&*si<s)
++si;
classes.insert(si,s);
}
/*:19*/
#line 34 "./equivalence.cweb"
;
/*15:*/
#line 196 "./equivalence.cweb"
const Equivalence&Equivalence::operator= (const Equivalence&e)
{
classes.clear();
n= e.n;
classes= e.classes;
return*this;
classes.clear();
n= e.n;
classes= e.classes;
return*this;
}
/*:15*/
#line 35 "./equivalence.cweb"
;
/*16:*/
#line 206 "./equivalence.cweb"
bool Equivalence::operator==(const Equivalence&e)const
{
if(!std::operator==(classes,e.classes))
return false;
if(n!=e.n)
return false;
return true;
if(!std::operator==(classes,e.classes))
return false;
if(n!=e.n)
return false;
return true;
}
/*:16*/
#line 36 "./equivalence.cweb"
;
/*20:*/
#line 293 "./equivalence.cweb"
void Equivalence::trace(IntSequence&out,int num)const
{
int i= 0;
int nc= 0;
for(const_seqit it= begin();it!=end()&&nc<num;++it,++nc)
for(int j= 0;j<(*it).length();j++,i++){
TL_RAISE_IF(i>=out.size(),
"Wrong size of output sequence in Equivalence::trace");
out[i]= (*it)[j];
}
int i= 0;
int nc= 0;
for(const_seqit it= begin();it!=end()&&nc<num;++it,++nc)
for(int j= 0;j<(*it).length();j++,i++){
TL_RAISE_IF(i>=out.size(),
"Wrong size of output sequence in Equivalence::trace");
out[i]= (*it)[j];
}
}
/*:20*/
#line 37 "./equivalence.cweb"
;
/*21:*/
#line 307 "./equivalence.cweb"
void Equivalence::trace(IntSequence&out,const Permutation&per)const
{
TL_RAISE_IF(out.size()!=n,
"Wrong size of output sequence in Equivalence::trace");
TL_RAISE_IF(per.size()!=numClasses(),
"Wrong permutation for permuted Equivalence::trace");
int i= 0;
for(int iclass= 0;iclass<numClasses();iclass++){
const_seqit itper= find(per.getMap()[iclass]);
for(int j= 0;j<(*itper).length();j++,i++)
out[i]= (*itper)[j];
}
TL_RAISE_IF(out.size()!=n,
"Wrong size of output sequence in Equivalence::trace");
TL_RAISE_IF(per.size()!=numClasses(),
"Wrong permutation for permuted Equivalence::trace");
int i= 0;
for(int iclass= 0;iclass<numClasses();iclass++){
const_seqit itper= find(per.getMap()[iclass]);
for(int j= 0;j<(*itper).length();j++,i++)
out[i]= (*itper)[j];
}
}
/*:21*/
#line 38 "./equivalence.cweb"
;
/*22:*/
#line 324 "./equivalence.cweb"
void Equivalence::print(const char*prefix)const
{
int i= 0;
for(const_seqit it= classes.begin();
it!=classes.end();
++it,i++){
printf("%sclass %d: ",prefix,i);
(*it).print("");
}
int i= 0;
for(const_seqit it= classes.begin();
it!=classes.end();
++it,i++){
printf("%sclass %d: ",prefix,i);
(*it).print("");
}
}
/*:22*/
#line 39 "./equivalence.cweb"
;
/*:3*/
#line 14 "./equivalence.cweb"
;
/*4:*/
#line 42 "./equivalence.cweb"
/*23:*/
#line 357 "./equivalence.cweb"
EquivalenceSet::EquivalenceSet(int num)
:n(num),
equis()
{
list<Equivalence> added;
Equivalence first(n);
equis.push_back(first);
addParents(first,added);
while(!added.empty()){
addParents(added.front(),added);
added.pop_front();
}
if(n> 1){
Equivalence last(n,"");
equis.push_back(last);
}
list<Equivalence> added;
Equivalence first(n);
equis.push_back(first);
addParents(first,added);
while(!added.empty()){
addParents(added.front(),added);
added.pop_front();
}
if(n> 1){
Equivalence last(n,"");
equis.push_back(last);
}
}
/*:23*/
#line 43 "./equivalence.cweb"
;
/*24:*/
#line 386 "./equivalence.cweb"
bool EquivalenceSet::has(const Equivalence&e)const
{
list<Equivalence> ::const_reverse_iterator rit= equis.rbegin();
while(rit!=equis.rend()&&*rit!=e)
++rit;
if(rit!=equis.rend())
return true;
return false;
list<Equivalence> ::const_reverse_iterator rit= equis.rbegin();
while(rit!=equis.rend()&&*rit!=e)
++rit;
if(rit!=equis.rend())
return true;
return false;
}
/*:24*/
#line 44 "./equivalence.cweb"
;
/*25:*/
#line 404 "./equivalence.cweb"
void EquivalenceSet::addParents(const Equivalence&e,
list<Equivalence> &added)
list<Equivalence> &added)
{
if(e.numClasses()==2||e.numClasses()==1)
return;
for(int i1= 0;i1<e.numClasses();i1++)
for(int i2= i1+1;i2<e.numClasses();i2++){
Equivalence ns(e,i1,i2);
if(!has(ns)){
added.push_back(ns);
equis.push_back(ns);
}
}
if(e.numClasses()==2||e.numClasses()==1)
return;
for(int i1= 0;i1<e.numClasses();i1++)
for(int i2= i1+1;i2<e.numClasses();i2++){
Equivalence ns(e,i1,i2);
if(!has(ns)){
added.push_back(ns);
equis.push_back(ns);
}
}
}
/*:25*/
#line 45 "./equivalence.cweb"
;
/*26:*/
#line 422 "./equivalence.cweb"
void EquivalenceSet::print(const char*prefix)const
{
char tmp[100];
strcpy(tmp,prefix);
strcat(tmp," ");
int i= 0;
for(list<Equivalence> ::const_iterator it= equis.begin();
it!=equis.end();
++it,i++){
printf("%sequivalence %d:(classes %d)\n",prefix,i,(*it).numClasses());
(*it).print(tmp);
}
char tmp[100];
strcpy(tmp,prefix);
strcat(tmp," ");
int i= 0;
for(list<Equivalence> ::const_iterator it= equis.begin();
it!=equis.end();
++it,i++){
printf("%sequivalence %d:(classes %d)\n",prefix,i,(*it).numClasses());
(*it).print(tmp);
}
}
/*:26*/
#line 46 "./equivalence.cweb"
;
/*:4*/
#line 15 "./equivalence.cweb"
;
/*5:*/
#line 49 "./equivalence.cweb"
/*27:*/
#line 438 "./equivalence.cweb"
EquivalenceBundle::EquivalenceBundle(int nmax)
{
nmax= max(nmax,1);
generateUpTo(nmax);
nmax= max(nmax,1);
generateUpTo(nmax);
}
/*:27*/
#line 50 "./equivalence.cweb"
;
/*28:*/
#line 446 "./equivalence.cweb"
EquivalenceBundle::~EquivalenceBundle()
{
for(unsigned int i= 0;i<bundle.size();i++)
delete bundle[i];
for(unsigned int i= 0;i<bundle.size();i++)
delete bundle[i];
}
/*:28*/
#line 51 "./equivalence.cweb"
;
/*29:*/
#line 454 "./equivalence.cweb"
const EquivalenceSet&EquivalenceBundle::get(int n)const
{
if(n> (int)(bundle.size())||n<1){
TL_RAISE("Equivalence set not found in EquivalenceBundle::get");
return*(bundle[0]);
}else{
return*(bundle[n-1]);
}
if(n> (int)(bundle.size())||n<1){
TL_RAISE("Equivalence set not found in EquivalenceBundle::get");
return*(bundle[0]);
}else{
return*(bundle[n-1]);
}
}
/*:29*/
#line 52 "./equivalence.cweb"
;
/*30:*/
#line 468 "./equivalence.cweb"
void EquivalenceBundle::generateUpTo(int nmax)
{
int curmax= bundle.size();
for(int i= curmax+1;i<=nmax;i++)
bundle.push_back(new EquivalenceSet(i));
int curmax= bundle.size();
for(int i= curmax+1;i<=nmax;i++)
bundle.push_back(new EquivalenceSet(i));
}
/*:30*/
#line 53 "./equivalence.cweb"
;
/*:5*/
#line 16 "./equivalence.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 46 "./equivalence.hweb"
#ifndef EQUIVALENCE_H
#define EQUIVALENCE_H
@ -12,132 +11,118 @@
using namespace std;
/*2:*/
#line 72 "./equivalence.hweb"
class OrdSequence{
vector<int> data;
vector<int> data;
public:
OrdSequence():data(){}
OrdSequence(const OrdSequence&s):data(s.data){}
const OrdSequence&operator= (const OrdSequence&s)
{data= s.data;return*this;}
bool operator==(const OrdSequence&s)const;
int operator[](int i)const;
bool operator<(const OrdSequence&s)const;
const vector<int> &getData()const
{return data;}
int length()const{return data.size();}
void add(int i);
void add(const OrdSequence&s);
bool has(int i)const;
void print(const char*prefix)const;
OrdSequence():data(){}
OrdSequence(const OrdSequence&s):data(s.data){}
const OrdSequence&operator= (const OrdSequence&s)
{data= s.data;return*this;}
bool operator==(const OrdSequence&s)const;
int operator[](int i)const;
bool operator<(const OrdSequence&s)const;
const vector<int> &getData()const
{return data;}
int length()const{return data.size();}
void add(int i);
void add(const OrdSequence&s);
bool has(int i)const;
void print(const char*prefix)const;
private:
double average()const;
double average()const;
};
/*:2*/
#line 57 "./equivalence.hweb"
;
/*3:*/
#line 101 "./equivalence.hweb"
class Permutation;
class Equivalence{
private:
int n;
list<OrdSequence> classes;
int n;
list<OrdSequence> classes;
public:
typedef list<OrdSequence> ::const_iterator const_seqit;
typedef list<OrdSequence> ::iterator seqit;
/*6:*/
#line 178 "./equivalence.hweb"
Equivalence(int num);
Equivalence(int num,const char*dummy);
Equivalence(const Equivalence&e);
Equivalence(const Equivalence&e,int i1,int i2);
/*:6*/
#line 111 "./equivalence.hweb"
;
const Equivalence&operator= (const Equivalence&e);
bool operator==(const Equivalence&e)const;
bool operator!=(const Equivalence&e)const
{return!operator==(e);}
int getN()const{return n;}
int numClasses()const{return classes.size();}
void trace(IntSequence&out,int n)const;
void trace(IntSequence&out)const
{trace(out,numClasses());}
void trace(IntSequence&out,const Permutation&per)const;
void print(const char*prefix)const;
/*7:*/
#line 185 "./equivalence.hweb"
seqit begin(){return classes.begin();}
const_seqit begin()const{return classes.begin();}
seqit end(){return classes.end();}
const_seqit end()const{return classes.end();}
/*:7*/
#line 123 "./equivalence.hweb"
;
const_seqit find(int i)const;
seqit find(int i);
typedef list<OrdSequence> ::const_iterator const_seqit;
typedef list<OrdSequence> ::iterator seqit;
/*6:*/
Equivalence(int num);
Equivalence(int num,const char*dummy);
Equivalence(const Equivalence&e);
Equivalence(const Equivalence&e,int i1,int i2);
/*:6*/
;
const Equivalence&operator= (const Equivalence&e);
bool operator==(const Equivalence&e)const;
bool operator!=(const Equivalence&e)const
{return!operator==(e);}
int getN()const{return n;}
int numClasses()const{return classes.size();}
void trace(IntSequence&out,int n)const;
void trace(IntSequence&out)const
{trace(out,numClasses());}
void trace(IntSequence&out,const Permutation&per)const;
void print(const char*prefix)const;
/*7:*/
seqit begin(){return classes.begin();}
const_seqit begin()const{return classes.begin();}
seqit end(){return classes.end();}
const_seqit end()const{return classes.end();}
/*:7*/
;
const_seqit find(int i)const;
seqit find(int i);
protected:
/*8:*/
#line 198 "./equivalence.hweb"
const_seqit findHaving(int i)const;
seqit findHaving(int i);
void insert(const OrdSequence&s);
/*:8*/
#line 127 "./equivalence.hweb"
;
/*8:*/
const_seqit findHaving(int i)const;
seqit findHaving(int i);
void insert(const OrdSequence&s);
/*:8*/
;
};
/*:3*/
#line 58 "./equivalence.hweb"
;
/*4:*/
#line 137 "./equivalence.hweb"
class EquivalenceSet{
int n;
list<Equivalence> equis;
int n;
list<Equivalence> equis;
public:
typedef list<Equivalence> ::const_iterator const_iterator;
EquivalenceSet(int num);
void print(const char*prefix)const;
const_iterator begin()const
{return equis.begin();}
const_iterator end()const
{return equis.end();}
typedef list<Equivalence> ::const_iterator const_iterator;
EquivalenceSet(int num);
void print(const char*prefix)const;
const_iterator begin()const
{return equis.begin();}
const_iterator end()const
{return equis.end();}
private:
bool has(const Equivalence&e)const;
void addParents(const Equivalence&e,list<Equivalence> &added);
bool has(const Equivalence&e)const;
void addParents(const Equivalence&e,list<Equivalence> &added);
};
/*:4*/
#line 59 "./equivalence.hweb"
;
/*5:*/
#line 161 "./equivalence.hweb"
class EquivalenceBundle{
vector<EquivalenceSet*> bundle;
vector<EquivalenceSet*> bundle;
public:
EquivalenceBundle(int nmax);
~EquivalenceBundle();
const EquivalenceSet&get(int n)const;
void generateUpTo(int nmax);
EquivalenceBundle(int nmax);
~EquivalenceBundle();
const EquivalenceSet&get(int n)const;
void generateUpTo(int nmax);
};
/*:5*/
#line 60 "./equivalence.hweb"
;
#endif

View File

@ -1,39 +1,36 @@
/*1:*/
#line 6 "./fine_container.cweb"
#include "fine_container.h"
#include <math.h>
/*2:*/
#line 16 "./fine_container.cweb"
SizeRefinement::SizeRefinement(const IntSequence&s,int nc,int max)
{
new_nc= 0;
for(int i= 0;i<nc;i++){
int nr= s[i]/max;
if(s[i]%max!=0)
nr++;
int ss= (nr> 0)?(int)round(((double)s[i])/nr):0;
for(int j= 0;j<nr-1;j++){
rsizes.push_back(ss);
ind_map.push_back(i);
new_nc++;
}
rsizes.push_back(s[i]-(nr-1)*ss);
ind_map.push_back(i);
new_nc++;
}
for(int i= nc;i<s.size();i++){
rsizes.push_back(s[i]);
ind_map.push_back(i);
}
new_nc= 0;
for(int i= 0;i<nc;i++){
int nr= s[i]/max;
if(s[i]%max!=0)
nr++;
int ss= (nr> 0)?(int)round(((double)s[i])/nr):0;
for(int j= 0;j<nr-1;j++){
rsizes.push_back(ss);
ind_map.push_back(i);
new_nc++;
}
rsizes.push_back(s[i]-(nr-1)*ss);
ind_map.push_back(i);
new_nc++;
}
for(int i= nc;i<s.size();i++){
rsizes.push_back(s[i]);
ind_map.push_back(i);
}
}
/*:2*/
#line 11 "./fine_container.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 33 "./fine_container.hweb"
#ifndef FINE_CONTAINER_H
#define FINE_CONTAINER_H
@ -9,120 +8,108 @@
#include <vector>
/*2:*/
#line 54 "./fine_container.hweb"
class SizeRefinement{
vector<int> rsizes;
vector<int> ind_map;
int new_nc;
vector<int> rsizes;
vector<int> ind_map;
int new_nc;
public:
SizeRefinement(const IntSequence&s,int nc,int max);
int getRefSize(int i)const
{return rsizes[i];}
int numRefinements()const
{return rsizes.size();}
int getOldIndex(int i)const
{return ind_map[i];}
int getNC()const
{return new_nc;}
SizeRefinement(const IntSequence&s,int nc,int max);
int getRefSize(int i)const
{return rsizes[i];}
int numRefinements()const
{return rsizes.size();}
int getOldIndex(int i)const
{return ind_map[i];}
int getNC()const
{return new_nc;}
};
/*:2*/
#line 41 "./fine_container.hweb"
;
/*3:*/
#line 77 "./fine_container.hweb"
template<class _Ttype>
class FineContainer:public SizeRefinement,public StackContainer<_Ttype> {
protected:
typedef StackContainer<_Ttype> _Stype;
typedef typename StackContainerInterface<_Ttype> ::_Ctype _Ctype;
typedef typename StackContainerInterface<_Ttype> ::itype itype;
_Ctype**const ref_conts;
const _Stype&stack_cont;
typedef StackContainer<_Ttype> _Stype;
typedef typename StackContainerInterface<_Ttype> ::_Ctype _Ctype;
typedef typename StackContainerInterface<_Ttype> ::itype itype;
_Ctype**const ref_conts;
const _Stype&stack_cont;
public:
/*4:*/
#line 109 "./fine_container.hweb"
FineContainer(const _Stype&sc,int max)
:SizeRefinement(sc.getStackSizes(),sc.numConts(),max),
StackContainer<_Ttype> (numRefinements(),getNC()),
ref_conts(new _Ctype*[getNC()]),
stack_cont(sc)
{
for(int i= 0;i<numRefinements();i++)
_Stype::stack_sizes[i]= getRefSize(i);
_Stype::calculateOffsets();
int last_cont= -1;
int last_row= 0;
for(int i= 0;i<getNC();i++){
if(getOldIndex(i)!=last_cont){
last_cont= getOldIndex(i);
last_row= 0;
}
union{const _Ctype*c;_Ctype*n;}convert;
convert.c= stack_cont.getCont(last_cont);
ref_conts[i]= new _Ctype(last_row,_Stype::stack_sizes[i],
*(convert.n));
_Stype::conts[i]= ref_conts[i];
last_row+= _Stype::stack_sizes[i];
}
}
/*:4*/
#line 87 "./fine_container.hweb"
;
/*5:*/
#line 137 "./fine_container.hweb"
virtual~FineContainer()
{
for(int i= 0;i<_Stype::numConts();i++)
delete ref_conts[i];
delete[]ref_conts;
}
/*:5*/
#line 88 "./fine_container.hweb"
;
itype getType(int i,const Symmetry&s)const
{return stack_cont.getType(getOldIndex(i),s);}
/*4:*/
FineContainer(const _Stype&sc,int max)
:SizeRefinement(sc.getStackSizes(),sc.numConts(),max),
StackContainer<_Ttype> (numRefinements(),getNC()),
ref_conts(new _Ctype*[getNC()]),
stack_cont(sc)
{
for(int i= 0;i<numRefinements();i++)
_Stype::stack_sizes[i]= getRefSize(i);
_Stype::calculateOffsets();
int last_cont= -1;
int last_row= 0;
for(int i= 0;i<getNC();i++){
if(getOldIndex(i)!=last_cont){
last_cont= getOldIndex(i);
last_row= 0;
}
union{const _Ctype*c;_Ctype*n;}convert;
convert.c= stack_cont.getCont(last_cont);
ref_conts[i]= new _Ctype(last_row,_Stype::stack_sizes[i],
*(convert.n));
_Stype::conts[i]= ref_conts[i];
last_row+= _Stype::stack_sizes[i];
}
}
/*:4*/
;
/*5:*/
virtual~FineContainer()
{
for(int i= 0;i<_Stype::numConts();i++)
delete ref_conts[i];
delete[]ref_conts;
}
/*:5*/
;
itype getType(int i,const Symmetry&s)const
{return stack_cont.getType(getOldIndex(i),s);}
};
/*:3*/
#line 42 "./fine_container.hweb"
;
/*6:*/
#line 148 "./fine_container.hweb"
class FoldedFineContainer:public FineContainer<FGSTensor> ,public FoldedStackContainer{
public:
FoldedFineContainer(const StackContainer<FGSTensor> &sc,int max)
:FineContainer<FGSTensor> (sc,max){}
FoldedFineContainer(const StackContainer<FGSTensor> &sc,int max)
:FineContainer<FGSTensor> (sc,max){}
};
/*:6*/
#line 43 "./fine_container.hweb"
;
/*7:*/
#line 156 "./fine_container.hweb"
class UnfoldedFineContainer:public FineContainer<UGSTensor> ,public UnfoldedStackContainer{
public:
UnfoldedFineContainer(const StackContainer<UGSTensor> &sc,int max)
:FineContainer<UGSTensor> (sc,max){}
UnfoldedFineContainer(const StackContainer<UGSTensor> &sc,int max)
:FineContainer<UGSTensor> (sc,max){}
};
/*:7*/
#line 44 "./fine_container.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./fs_tensor.cweb"
#include "fs_tensor.h"
#include "gs_tensor.h"
@ -8,314 +7,278 @@
#include "tl_exception.h"
/*2:*/
#line 39 "./fs_tensor.cweb"
FFSTensor::FFSTensor(const FFSTensor&t,const ConstVector&x)
:FTensor(along_col,IntSequence(t.dimen()-1,t.nvar()),
t.nrows(),calcMaxOffset(t.nvar(),t.dimen()-1),t.dimen()-1),
nv(t.nvar())
t.nrows(),calcMaxOffset(t.nvar(),t.dimen()-1),t.dimen()-1),
nv(t.nvar())
{
TL_RAISE_IF(t.dimen()<1,
"Wrong dimension for tensor contraction of FFSTensor");
TL_RAISE_IF(t.nvar()!=x.length(),
"Wrong number of variables for tensor contraction of FFSTensor");
zeros();
for(Tensor::index to= begin();to!=end();++to){
for(int i= 0;i<nvar();i++){
IntSequence from_ind(i,to.getCoor());
Tensor::index from(&t,from_ind);
addColumn(x[i],t,*from,*to);
}
}
TL_RAISE_IF(t.dimen()<1,
"Wrong dimension for tensor contraction of FFSTensor");
TL_RAISE_IF(t.nvar()!=x.length(),
"Wrong number of variables for tensor contraction of FFSTensor");
zeros();
for(Tensor::index to= begin();to!=end();++to){
for(int i= 0;i<nvar();i++){
IntSequence from_ind(i,to.getCoor());
Tensor::index from(&t,from_ind);
addColumn(x[i],t,*from,*to);
}
}
}
/*:2*/
#line 13 "./fs_tensor.cweb"
;
/*3:*/
#line 66 "./fs_tensor.cweb"
int FFSTensor::calcMaxOffset(int nvar,int d)
{
if(nvar==0&&d==0)
return 1;
if(nvar==0&&d> 0)
return 0;
return noverk(nvar+d-1,d);
if(nvar==0&&d==0)
return 1;
if(nvar==0&&d> 0)
return 0;
return noverk(nvar+d-1,d);
}
/*:3*/
#line 14 "./fs_tensor.cweb"
;
/*4:*/
#line 78 "./fs_tensor.cweb"
FFSTensor::FFSTensor(const FSSparseTensor&t)
:FTensor(along_col,IntSequence(t.dimen(),t.nvar()),
t.nrows(),calcMaxOffset(t.nvar(),t.dimen()),t.dimen()),
nv(t.nvar())
t.nrows(),calcMaxOffset(t.nvar(),t.dimen()),t.dimen()),
nv(t.nvar())
{
zeros();
for(FSSparseTensor::const_iterator it= t.getMap().begin();
it!=t.getMap().end();++it){
index ind(this,(*it).first);
get((*it).second.first,*ind)= (*it).second.second;
}
zeros();
for(FSSparseTensor::const_iterator it= t.getMap().begin();
it!=t.getMap().end();++it){
index ind(this,(*it).first);
get((*it).second.first,*ind)= (*it).second.second;
}
}
/*:4*/
#line 15 "./fs_tensor.cweb"
;
/*5:*/
#line 98 "./fs_tensor.cweb"
FFSTensor::FFSTensor(const UFSTensor&ut)
:FTensor(along_col,IntSequence(ut.dimen(),ut.nvar()),
ut.nrows(),calcMaxOffset(ut.nvar(),ut.dimen()),ut.dimen()),
nv(ut.nvar())
ut.nrows(),calcMaxOffset(ut.nvar(),ut.dimen()),ut.dimen()),
nv(ut.nvar())
{
for(index in= begin();in!=end();++in){
index src(&ut,in.getCoor());
copyColumn(ut,*src,*in);
}
for(index in= begin();in!=end();++in){
index src(&ut,in.getCoor());
copyColumn(ut,*src,*in);
}
}
/*:5*/
#line 16 "./fs_tensor.cweb"
;
/*6:*/
#line 111 "./fs_tensor.cweb"
UTensor&FFSTensor::unfold()const
{
return*(new UFSTensor(*this));
return*(new UFSTensor(*this));
}
/*:6*/
#line 17 "./fs_tensor.cweb"
;
/*7:*/
#line 122 "./fs_tensor.cweb"
void FFSTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FFSTensor::increment");
UTensor::increment(v,nv);
v.monotone();
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FFSTensor::increment");
UTensor::increment(v,nv);
v.monotone();
}
/*:7*/
#line 18 "./fs_tensor.cweb"
;
/*8:*/
#line 134 "./fs_tensor.cweb"
void FFSTensor::decrement(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FFSTensor::decrement");
FTensor::decrement(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FFSTensor::decrement");
FTensor::decrement(v,nv);
}
/*:8*/
#line 19 "./fs_tensor.cweb"
;
/*9:*/
#line 144 "./fs_tensor.cweb"
int FFSTensor::getOffset(const IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in FFSTensor::getOffset");
return FTensor::getOffset(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in FFSTensor::getOffset");
return FTensor::getOffset(v,nv);
}
/*:9*/
#line 20 "./fs_tensor.cweb"
;
/*10:*/
#line 163 "./fs_tensor.cweb"
void FFSTensor::addSubTensor(const FGSTensor&t)
{
TL_RAISE_IF(dimen()!=t.getDims().dimen(),
"Wrong dimensions for FFSTensor::addSubTensor");
TL_RAISE_IF(nvar()!=t.getDims().getNVS().sum(),
"Wrong nvs for FFSTensor::addSubTensor");
/*11:*/
#line 182 "./fs_tensor.cweb"
IntSequence shift_pre(t.getSym().num(),0);
for(int i= 1;i<t.getSym().num();i++)
shift_pre[i]= shift_pre[i-1]+t.getDims().getNVS()[i-1];
IntSequence shift(t.getSym(),shift_pre);
/*:11*/
#line 171 "./fs_tensor.cweb"
;
for(Tensor::index ind= t.begin();ind!=t.end();++ind){
IntSequence c(ind.getCoor());
c.add(1,shift);
c.sort();
Tensor::index tar(this,c);
addColumn(t,*ind,*tar);
}
TL_RAISE_IF(dimen()!=t.getDims().dimen(),
"Wrong dimensions for FFSTensor::addSubTensor");
TL_RAISE_IF(nvar()!=t.getDims().getNVS().sum(),
"Wrong nvs for FFSTensor::addSubTensor");
/*11:*/
IntSequence shift_pre(t.getSym().num(),0);
for(int i= 1;i<t.getSym().num();i++)
shift_pre[i]= shift_pre[i-1]+t.getDims().getNVS()[i-1];
IntSequence shift(t.getSym(),shift_pre);
/*:11*/
;
for(Tensor::index ind= t.begin();ind!=t.end();++ind){
IntSequence c(ind.getCoor());
c.add(1,shift);
c.sort();
Tensor::index tar(this,c);
addColumn(t,*ind,*tar);
}
}
/*:10*/
#line 21 "./fs_tensor.cweb"
;
/*12:*/
#line 192 "./fs_tensor.cweb"
UFSTensor::UFSTensor(const UFSTensor&t,const ConstVector&x)
:UTensor(along_col,IntSequence(t.dimen()-1,t.nvar()),
t.nrows(),calcMaxOffset(t.nvar(),t.dimen()-1),t.dimen()-1),
nv(t.nvar())
t.nrows(),calcMaxOffset(t.nvar(),t.dimen()-1),t.dimen()-1),
nv(t.nvar())
{
TL_RAISE_IF(t.dimen()<1,
"Wrong dimension for tensor contraction of UFSTensor");
TL_RAISE_IF(t.nvar()!=x.length(),
"Wrong number of variables for tensor contraction of UFSTensor");
zeros();
for(int i= 0;i<ncols();i++){
ConstTwoDMatrix tpart(t,i*nvar(),nvar());
Vector outcol(*this,i);
tpart.multaVec(outcol,x);
}
TL_RAISE_IF(t.dimen()<1,
"Wrong dimension for tensor contraction of UFSTensor");
TL_RAISE_IF(t.nvar()!=x.length(),
"Wrong number of variables for tensor contraction of UFSTensor");
zeros();
for(int i= 0;i<ncols();i++){
ConstTwoDMatrix tpart(t,i*nvar(),nvar());
Vector outcol(*this,i);
tpart.multaVec(outcol,x);
}
}
/*:12*/
#line 22 "./fs_tensor.cweb"
;
/*13:*/
#line 215 "./fs_tensor.cweb"
UFSTensor::UFSTensor(const FFSTensor&ft)
:UTensor(along_col,IntSequence(ft.dimen(),ft.nvar()),
ft.nrows(),calcMaxOffset(ft.nvar(),ft.dimen()),ft.dimen()),
nv(ft.nvar())
ft.nrows(),calcMaxOffset(ft.nvar(),ft.dimen()),ft.dimen()),
nv(ft.nvar())
{
for(index src= ft.begin();src!=ft.end();++src){
index in(this,src.getCoor());
copyColumn(ft,*src,*in);
}
unfoldData();
for(index src= ft.begin();src!=ft.end();++src){
index in(this,src.getCoor());
copyColumn(ft,*src,*in);
}
unfoldData();
}
/*:13*/
#line 23 "./fs_tensor.cweb"
;
/*14:*/
#line 229 "./fs_tensor.cweb"
FTensor&UFSTensor::fold()const
{
return*(new FFSTensor(*this));
return*(new FFSTensor(*this));
}
/*:14*/
#line 24 "./fs_tensor.cweb"
;
/*15:*/
#line 236 "./fs_tensor.cweb"
void UFSTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UFSTensor::increment");
UTensor::increment(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UFSTensor::increment");
UTensor::increment(v,nv);
}
void UFSTensor::decrement(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UFSTensor::decrement");
UTensor::decrement(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UFSTensor::decrement");
UTensor::decrement(v,nv);
}
/*:15*/
#line 25 "./fs_tensor.cweb"
;
/*16:*/
#line 254 "./fs_tensor.cweb"
int UFSTensor::getOffset(const IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in UFSTensor::getOffset");
return UTensor::getOffset(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in UFSTensor::getOffset");
return UTensor::getOffset(v,nv);
}
/*:16*/
#line 26 "./fs_tensor.cweb"
;
/*17:*/
#line 269 "./fs_tensor.cweb"
void UFSTensor::addSubTensor(const UGSTensor&t)
{
TL_RAISE_IF(dimen()!=t.getDims().dimen(),
"Wrong dimensions for UFSTensor::addSubTensor");
TL_RAISE_IF(nvar()!=t.getDims().getNVS().sum(),
"Wrong nvs for UFSTensor::addSubTensor");
/*11:*/
#line 182 "./fs_tensor.cweb"
IntSequence shift_pre(t.getSym().num(),0);
for(int i= 1;i<t.getSym().num();i++)
shift_pre[i]= shift_pre[i-1]+t.getDims().getNVS()[i-1];
IntSequence shift(t.getSym(),shift_pre);
/*:11*/
#line 277 "./fs_tensor.cweb"
;
for(Tensor::index tar= begin();tar!=end();++tar){
IntSequence c(tar.getCoor());
c.sort();
c.add(-1,shift);
if(c.isPositive()&&c.less(t.getDims().getNVX())){
Tensor::index from(&t,c);
addColumn(t,*from,*tar);
}
}
TL_RAISE_IF(dimen()!=t.getDims().dimen(),
"Wrong dimensions for UFSTensor::addSubTensor");
TL_RAISE_IF(nvar()!=t.getDims().getNVS().sum(),
"Wrong nvs for UFSTensor::addSubTensor");
/*11:*/
IntSequence shift_pre(t.getSym().num(),0);
for(int i= 1;i<t.getSym().num();i++)
shift_pre[i]= shift_pre[i-1]+t.getDims().getNVS()[i-1];
IntSequence shift(t.getSym(),shift_pre);
/*:11*/
;
for(Tensor::index tar= begin();tar!=end();++tar){
IntSequence c(tar.getCoor());
c.sort();
c.add(-1,shift);
if(c.isPositive()&&c.less(t.getDims().getNVX())){
Tensor::index from(&t,c);
addColumn(t,*from,*tar);
}
}
}
/*:17*/
#line 27 "./fs_tensor.cweb"
;
/*18:*/
#line 294 "./fs_tensor.cweb"
void UFSTensor::unfoldData()
{
for(index in= begin();in!=end();++in){
IntSequence v(in.getCoor());
v.sort();
index tmp(this,v);
copyColumn(*tmp,*in);
}
for(index in= begin();in!=end();++in){
IntSequence v(in.getCoor());
v.sort();
index tmp(this,v);
copyColumn(*tmp,*in);
}
}
/*:18*/
#line 28 "./fs_tensor.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 10 "./fs_tensor.hweb"
#ifndef FS_TENSOR_H
#define FS_TENSOR_H
@ -12,88 +11,80 @@ class UGSTensor;
class FRSingleTensor;
class FSSparseTensor;
/*2:*/
#line 42 "./fs_tensor.hweb"
class UFSTensor;
class FFSTensor:public FTensor{
int nv;
int nv;
public:
/*3:*/
#line 76 "./fs_tensor.hweb"
FFSTensor(int r,int nvar,int d)
:FTensor(along_col,IntSequence(d,nvar),
r,calcMaxOffset(nvar,d),d),nv(nvar){}
FFSTensor(const FFSTensor&t,const ConstVector&x);
FFSTensor(const FSSparseTensor&t);
FFSTensor(const FFSTensor&ft)
:FTensor(ft),nv(ft.nv){}
FFSTensor(const UFSTensor&ut);
FFSTensor(int first_row,int num,FFSTensor&t)
:FTensor(first_row,num,t),nv(t.nv){}
/*:3*/
#line 47 "./fs_tensor.hweb"
;
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const;
UTensor&unfold()const;
Symmetry getSym()const
{return Symmetry(dimen());}
int getOffset(const IntSequence&v)const;
void addSubTensor(const FGSTensor&t);
int nvar()const
{return nv;}
static int calcMaxOffset(int nvar,int d);
/*3:*/
FFSTensor(int r,int nvar,int d)
:FTensor(along_col,IntSequence(d,nvar),
r,calcMaxOffset(nvar,d),d),nv(nvar){}
FFSTensor(const FFSTensor&t,const ConstVector&x);
FFSTensor(const FSSparseTensor&t);
FFSTensor(const FFSTensor&ft)
:FTensor(ft),nv(ft.nv){}
FFSTensor(const UFSTensor&ut);
FFSTensor(int first_row,int num,FFSTensor&t)
:FTensor(first_row,num,t),nv(t.nv){}
/*:3*/
;
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const;
UTensor&unfold()const;
Symmetry getSym()const
{return Symmetry(dimen());}
int getOffset(const IntSequence&v)const;
void addSubTensor(const FGSTensor&t);
int nvar()const
{return nv;}
static int calcMaxOffset(int nvar,int d);
};
/*:2*/
#line 21 "./fs_tensor.hweb"
;
/*4:*/
#line 95 "./fs_tensor.hweb"
class UFSTensor:public UTensor{
int nv;
int nv;
public:
/*5:*/
#line 118 "./fs_tensor.hweb"
UFSTensor(int r,int nvar,int d)
:UTensor(along_col,IntSequence(d,nvar),
r,calcMaxOffset(nvar,d),d),nv(nvar){}
UFSTensor(const UFSTensor&t,const ConstVector&x);
UFSTensor(const UFSTensor&ut)
:UTensor(ut),nv(ut.nv){}
UFSTensor(const FFSTensor&ft);
UFSTensor(int first_row,int num,UFSTensor&t)
:UTensor(first_row,num,t),nv(t.nv){}
/*:5*/
#line 99 "./fs_tensor.hweb"
;
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const;
FTensor&fold()const;
Symmetry getSym()const
{return Symmetry(dimen());}
int getOffset(const IntSequence&v)const;
void addSubTensor(const UGSTensor&t);
int nvar()const
{return nv;}
static int calcMaxOffset(int nvar,int d)
{return power(nvar,d);}
/*5:*/
UFSTensor(int r,int nvar,int d)
:UTensor(along_col,IntSequence(d,nvar),
r,calcMaxOffset(nvar,d),d),nv(nvar){}
UFSTensor(const UFSTensor&t,const ConstVector&x);
UFSTensor(const UFSTensor&ut)
:UTensor(ut),nv(ut.nv){}
UFSTensor(const FFSTensor&ft);
UFSTensor(int first_row,int num,UFSTensor&t)
:UTensor(first_row,num,t),nv(t.nv){}
/*:5*/
;
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const;
FTensor&fold()const;
Symmetry getSym()const
{return Symmetry(dimen());}
int getOffset(const IntSequence&v)const;
void addSubTensor(const UGSTensor&t);
int nvar()const
{return nv;}
static int calcMaxOffset(int nvar,int d)
{return power(nvar,d);}
private:
void unfoldData();
void unfoldData();
};
/*:4*/
#line 22 "./fs_tensor.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./gs_tensor.cweb"
#include "gs_tensor.h"
#include "sparse_tensor.h"
@ -7,511 +6,457 @@
#include "kron_prod.h"
/*2:*/
#line 36 "./gs_tensor.cweb"
TensorDimens::TensorDimens(const IntSequence&ss,const IntSequence&coor)
:nvs(ss),
sym(ss.size(),""),
nvmax(coor.size(),0)
{
TL_RAISE_IF(!coor.isSorted(),
"Coordinates not sorted in TensorDimens slicing constructor");
TL_RAISE_IF(coor[0]<0||coor[coor.size()-1]>=ss.size(),
"A coordinate out of stack range in TensorDimens slicing constructor");
for(int i= 0;i<coor.size();i++){
sym[coor[i]]++;
nvmax[i]= ss[coor[i]];
}
TL_RAISE_IF(!coor.isSorted(),
"Coordinates not sorted in TensorDimens slicing constructor");
TL_RAISE_IF(coor[0]<0||coor[coor.size()-1]>=ss.size(),
"A coordinate out of stack range in TensorDimens slicing constructor");
for(int i= 0;i<coor.size();i++){
sym[coor[i]]++;
nvmax[i]= ss[coor[i]];
}
}
/*:2*/
#line 12 "./gs_tensor.cweb"
;
/*3:*/
#line 55 "./gs_tensor.cweb"
int TensorDimens::calcUnfoldMaxOffset()const
{
return nvmax.mult();
return nvmax.mult();
}
/*:3*/
#line 13 "./gs_tensor.cweb"
;
/*4:*/
#line 64 "./gs_tensor.cweb"
int TensorDimens::calcFoldMaxOffset()const
{
int res= 1;
for(int i= 0;i<nvs.size();i++){
if(nvs[i]==0&&sym[i]> 0)
return 0;
if(sym[i]> 0)
res*= Tensor::noverk(nvs[i]+sym[i]-1,sym[i]);
}
return res;
int res= 1;
for(int i= 0;i<nvs.size();i++){
if(nvs[i]==0&&sym[i]> 0)
return 0;
if(sym[i]> 0)
res*= Tensor::noverk(nvs[i]+sym[i]-1,sym[i]);
}
return res;
}
/*:4*/
#line 14 "./gs_tensor.cweb"
;
/*5:*/
#line 95 "./gs_tensor.cweb"
int TensorDimens::calcFoldOffset(const IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in TensorDimens::getFoldOffset");
int res= 0;
int pow= 1;
int blstart= v.size();
for(int ibl= getSym().num()-1;ibl>=0;ibl--){
int bldim= getSym()[ibl];
if(bldim> 0){
blstart-= bldim;
int blnvar= getNVX()[blstart];
IntSequence subv(v,blstart,blstart+bldim);
res+= FTensor::getOffset(subv,blnvar)*pow;
pow*= FFSTensor::calcMaxOffset(blnvar,bldim);
}
}
TL_RAISE_IF(blstart!=0,
"Error in tracing symmetry in TensorDimens::getFoldOffset");
return res;
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in TensorDimens::getFoldOffset");
int res= 0;
int pow= 1;
int blstart= v.size();
for(int ibl= getSym().num()-1;ibl>=0;ibl--){
int bldim= getSym()[ibl];
if(bldim> 0){
blstart-= bldim;
int blnvar= getNVX()[blstart];
IntSequence subv(v,blstart,blstart+bldim);
res+= FTensor::getOffset(subv,blnvar)*pow;
pow*= FFSTensor::calcMaxOffset(blnvar,bldim);
}
}
TL_RAISE_IF(blstart!=0,
"Error in tracing symmetry in TensorDimens::getFoldOffset");
return res;
}
/*:5*/
#line 15 "./gs_tensor.cweb"
;
/*6:*/
#line 136 "./gs_tensor.cweb"
void TensorDimens::decrement(IntSequence&v)const
{
TL_RAISE_IF(getNVX().size()!=v.size(),
"Wrong size of input/output sequence in TensorDimens::decrement");
int iblock= getSym().num()-1;
int block_last= v.size();
int block_first= block_last-getSym()[iblock];
/*7:*/
#line 150 "./gs_tensor.cweb"
while(iblock> 0&&v[block_last-1]==0){
for(int i= block_first;i<block_last;i++)
v[i]= getNVX(i);
iblock--;
block_last= block_first;
block_first-= getSym()[iblock];
}
/*:7*/
#line 145 "./gs_tensor.cweb"
;
/*8:*/
#line 160 "./gs_tensor.cweb"
IntSequence vtmp(v,block_first,block_last);
FTensor::decrement(vtmp,getNVX(block_first));
/*:8*/
#line 146 "./gs_tensor.cweb"
;
TL_RAISE_IF(getNVX().size()!=v.size(),
"Wrong size of input/output sequence in TensorDimens::decrement");
int iblock= getSym().num()-1;
int block_last= v.size();
int block_first= block_last-getSym()[iblock];
/*7:*/
while(iblock> 0&&v[block_last-1]==0){
for(int i= block_first;i<block_last;i++)
v[i]= getNVX(i);
iblock--;
block_last= block_first;
block_first-= getSym()[iblock];
}
/*:7*/
;
/*8:*/
IntSequence vtmp(v,block_first,block_last);
FTensor::decrement(vtmp,getNVX(block_first));
/*:8*/
;
}
/*:6*/
#line 16 "./gs_tensor.cweb"
;
/*9:*/
#line 169 "./gs_tensor.cweb"
FGSTensor::FGSTensor(const UGSTensor&ut)
:FTensor(along_col,ut.tdims.getNVX(),ut.nrows(),
ut.tdims.calcFoldMaxOffset(),ut.dimen()),
tdims(ut.tdims)
ut.tdims.calcFoldMaxOffset(),ut.dimen()),
tdims(ut.tdims)
{
for(index ti= begin();ti!=end();++ti){
index ui(&ut,ti.getCoor());
copyColumn(ut,*ui,*ti);
}
for(index ti= begin();ti!=end();++ti){
index ui(&ut,ti.getCoor());
copyColumn(ut,*ui,*ti);
}
}
/*:9*/
#line 17 "./gs_tensor.cweb"
;
/*10:*/
#line 190 "./gs_tensor.cweb"
FGSTensor::FGSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td)
:FTensor(along_col,td.getNVX(),t.nrows(),
td.calcFoldMaxOffset(),td.dimen()),
tdims(td)
const IntSequence&coor,const TensorDimens&td)
:FTensor(along_col,td.getNVX(),t.nrows(),
td.calcFoldMaxOffset(),td.dimen()),
tdims(td)
{
/*11:*/
#line 221 "./gs_tensor.cweb"
IntSequence s_offsets(ss.size(),0);
for(int i= 1;i<ss.size();i++)
s_offsets[i]= s_offsets[i-1]+ss[i-1];
IntSequence lb(coor.size());
IntSequence ub(coor.size());
for(int i= 0;i<coor.size();i++){
lb[i]= s_offsets[coor[i]];
ub[i]= s_offsets[coor[i]]+ss[coor[i]]-1;
}
/*:11*/
#line 197 "./gs_tensor.cweb"
;
zeros();
FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb.lessEq((*run).first)&&(*run).first.lessEq(ub)){
IntSequence c((*run).first);
c.add(-1,lb);
Tensor::index ind(this,c);
TL_RAISE_IF(*ind<0||*ind>=ncols(),
"Internal error in slicing constructor of FGSTensor");
get((*run).second.first,*ind)= (*run).second.second;
}
}
/*11:*/
IntSequence s_offsets(ss.size(),0);
for(int i= 1;i<ss.size();i++)
s_offsets[i]= s_offsets[i-1]+ss[i-1];
IntSequence lb(coor.size());
IntSequence ub(coor.size());
for(int i= 0;i<coor.size();i++){
lb[i]= s_offsets[coor[i]];
ub[i]= s_offsets[coor[i]]+ss[coor[i]]-1;
}
/*:11*/
;
zeros();
FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb.lessEq((*run).first)&&(*run).first.lessEq(ub)){
IntSequence c((*run).first);
c.add(-1,lb);
Tensor::index ind(this,c);
TL_RAISE_IF(*ind<0||*ind>=ncols(),
"Internal error in slicing constructor of FGSTensor");
get((*run).second.first,*ind)= (*run).second.second;
}
}
}
/*:10*/
#line 18 "./gs_tensor.cweb"
;
/*12:*/
#line 235 "./gs_tensor.cweb"
FGSTensor::FGSTensor(const FFSTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td)
:FTensor(along_col,td.getNVX(),t.nrows(),
td.calcFoldMaxOffset(),td.dimen()),
tdims(td)
const IntSequence&coor,const TensorDimens&td)
:FTensor(along_col,td.getNVX(),t.nrows(),
td.calcFoldMaxOffset(),td.dimen()),
tdims(td)
{
if(ncols()==0)
return;
/*11:*/
#line 221 "./gs_tensor.cweb"
IntSequence s_offsets(ss.size(),0);
for(int i= 1;i<ss.size();i++)
s_offsets[i]= s_offsets[i-1]+ss[i-1];
IntSequence lb(coor.size());
IntSequence ub(coor.size());
for(int i= 0;i<coor.size();i++){
lb[i]= s_offsets[coor[i]];
ub[i]= s_offsets[coor[i]]+ss[coor[i]]-1;
}
/*:11*/
#line 245 "./gs_tensor.cweb"
;
zeros();
Tensor::index lbi(&t,lb);
Tensor::index ubi(&t,ub);
++ubi;
for(Tensor::index run= lbi;run!=ubi;++run){
if(lb.lessEq(run.getCoor())&&run.getCoor().lessEq(ub)){
IntSequence c(run.getCoor());
c.add(-1,lb);
Tensor::index ind(this,c);
TL_RAISE_IF(*ind<0||*ind>=ncols(),
"Internal error in slicing constructor of FGSTensor");
copyColumn(t,*run,*ind);
}
}
if(ncols()==0)
return;
/*11:*/
IntSequence s_offsets(ss.size(),0);
for(int i= 1;i<ss.size();i++)
s_offsets[i]= s_offsets[i-1]+ss[i-1];
IntSequence lb(coor.size());
IntSequence ub(coor.size());
for(int i= 0;i<coor.size();i++){
lb[i]= s_offsets[coor[i]];
ub[i]= s_offsets[coor[i]]+ss[coor[i]]-1;
}
/*:11*/
;
zeros();
Tensor::index lbi(&t,lb);
Tensor::index ubi(&t,ub);
++ubi;
for(Tensor::index run= lbi;run!=ubi;++run){
if(lb.lessEq(run.getCoor())&&run.getCoor().lessEq(ub)){
IntSequence c(run.getCoor());
c.add(-1,lb);
Tensor::index ind(this,c);
TL_RAISE_IF(*ind<0||*ind>=ncols(),
"Internal error in slicing constructor of FGSTensor");
copyColumn(t,*run,*ind);
}
}
}
/*:12*/
#line 19 "./gs_tensor.cweb"
;
/*13:*/
#line 264 "./gs_tensor.cweb"
FGSTensor::FGSTensor(const GSSparseTensor&t)
:FTensor(along_col,t.getDims().getNVX(),t.nrows(),
t.getDims().calcFoldMaxOffset(),t.dimen()),tdims(t.getDims())
t.getDims().calcFoldMaxOffset(),t.dimen()),tdims(t.getDims())
{
zeros();
for(FSSparseTensor::const_iterator it= t.getMap().begin();
it!=t.getMap().end();++it){
index ind(this,(*it).first);
get((*it).second.first,*ind)= (*it).second.second;
}
zeros();
for(FSSparseTensor::const_iterator it= t.getMap().begin();
it!=t.getMap().end();++it){
index ind(this,(*it).first);
get((*it).second.first,*ind)= (*it).second.second;
}
}
/*:13*/
#line 20 "./gs_tensor.cweb"
;
/*14:*/
#line 281 "./gs_tensor.cweb"
void FGSTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FGSTensor::increment");
UTensor::increment(v,tdims.getNVX());
v.pmonotone(tdims.getSym());
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FGSTensor::increment");
UTensor::increment(v,tdims.getNVX());
v.pmonotone(tdims.getSym());
}
/*:14*/
#line 21 "./gs_tensor.cweb"
;
/*15:*/
#line 295 "./gs_tensor.cweb"
UTensor&FGSTensor::unfold()const
{
return*(new UGSTensor(*this));
return*(new UGSTensor(*this));
}
/*:15*/
#line 22 "./gs_tensor.cweb"
;
/*16:*/
#line 321 "./gs_tensor.cweb"
void FGSTensor::contractAndAdd(int i,FGSTensor&out,
const FRSingleTensor&col)const
const FRSingleTensor&col)const
{
TL_RAISE_IF(i<0||i>=getSym().num(),
"Wrong index for FGSTensor::contractAndAdd");
TL_RAISE_IF(getSym()[i]!=col.dimen()||tdims.getNVS()[i]!=col.nvar(),
"Wrong dimensions for FGSTensor::contractAndAdd");
/*17:*/
#line 349 "./gs_tensor.cweb"
Symmetry sym_left(getSym());
Symmetry sym_right(getSym());
for(int j= 0;j<getSym().num();j++){
if(j<=i)
sym_right[j]= 0;
if(j>=i)
sym_left[j]= 0;
}
/*:17*/
#line 331 "./gs_tensor.cweb"
;
int dleft= TensorDimens(sym_left,tdims.getNVS()).calcFoldMaxOffset();
int dright= TensorDimens(sym_right,tdims.getNVS()).calcFoldMaxOffset();
KronProdAll kp(3);
kp.setUnit(0,dleft);
kp.setMat(1,col);
kp.setUnit(2,dright);
FGSTensor tmp(out.nrows(),out.getDims());
kp.mult(*this,tmp);
out.add(1.0,tmp);
TL_RAISE_IF(i<0||i>=getSym().num(),
"Wrong index for FGSTensor::contractAndAdd");
TL_RAISE_IF(getSym()[i]!=col.dimen()||tdims.getNVS()[i]!=col.nvar(),
"Wrong dimensions for FGSTensor::contractAndAdd");
/*17:*/
Symmetry sym_left(getSym());
Symmetry sym_right(getSym());
for(int j= 0;j<getSym().num();j++){
if(j<=i)
sym_right[j]= 0;
if(j>=i)
sym_left[j]= 0;
}
/*:17*/
;
int dleft= TensorDimens(sym_left,tdims.getNVS()).calcFoldMaxOffset();
int dright= TensorDimens(sym_right,tdims.getNVS()).calcFoldMaxOffset();
KronProdAll kp(3);
kp.setUnit(0,dleft);
kp.setMat(1,col);
kp.setUnit(2,dright);
FGSTensor tmp(out.nrows(),out.getDims());
kp.mult(*this,tmp);
out.add(1.0,tmp);
}
/*:16*/
#line 23 "./gs_tensor.cweb"
;
/*18:*/
#line 364 "./gs_tensor.cweb"
UGSTensor::UGSTensor(const FGSTensor&ft)
:UTensor(along_col,ft.tdims.getNVX(),ft.nrows(),
ft.tdims.calcUnfoldMaxOffset(),ft.dimen()),
tdims(ft.tdims)
ft.tdims.calcUnfoldMaxOffset(),ft.dimen()),
tdims(ft.tdims)
{
for(index fi= ft.begin();fi!=ft.end();++fi){
index ui(this,fi.getCoor());
copyColumn(ft,*fi,*ui);
}
unfoldData();
for(index fi= ft.begin();fi!=ft.end();++fi){
index ui(this,fi.getCoor());
copyColumn(ft,*fi,*ui);
}
unfoldData();
}
/*:18*/
#line 24 "./gs_tensor.cweb"
;
/*19:*/
#line 378 "./gs_tensor.cweb"
UGSTensor::UGSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td)
:UTensor(along_col,td.getNVX(),t.nrows(),
td.calcUnfoldMaxOffset(),td.dimen()),
tdims(td)
const IntSequence&coor,const TensorDimens&td)
:UTensor(along_col,td.getNVX(),t.nrows(),
td.calcUnfoldMaxOffset(),td.dimen()),
tdims(td)
{
if(ncols()==0)
return;
FGSTensor ft(t,ss,coor,td);
for(index fi= ft.begin();fi!=ft.end();++fi){
index ui(this,fi.getCoor());
copyColumn(ft,*fi,*ui);
}
unfoldData();
if(ncols()==0)
return;
FGSTensor ft(t,ss,coor,td);
for(index fi= ft.begin();fi!=ft.end();++fi){
index ui(this,fi.getCoor());
copyColumn(ft,*fi,*ui);
}
unfoldData();
}
/*:19*/
#line 25 "./gs_tensor.cweb"
;
/*20:*/
#line 397 "./gs_tensor.cweb"
UGSTensor::UGSTensor(const UFSTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td)
:UTensor(along_col,td.getNVX(),t.nrows(),
td.calcUnfoldMaxOffset(),td.dimen()),
tdims(td)
const IntSequence&coor,const TensorDimens&td)
:UTensor(along_col,td.getNVX(),t.nrows(),
td.calcUnfoldMaxOffset(),td.dimen()),
tdims(td)
{
FFSTensor folded(t);
FGSTensor ft(folded,ss,coor,td);
for(index fi= ft.begin();fi!=ft.end();++fi){
index ui(this,fi.getCoor());
copyColumn(ft,*fi,*ui);
}
unfoldData();
FFSTensor folded(t);
FGSTensor ft(folded,ss,coor,td);
for(index fi= ft.begin();fi!=ft.end();++fi){
index ui(this,fi.getCoor());
copyColumn(ft,*fi,*ui);
}
unfoldData();
}
/*:20*/
#line 26 "./gs_tensor.cweb"
;
/*21:*/
#line 415 "./gs_tensor.cweb"
void UGSTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UGSTensor::increment");
UTensor::increment(v,tdims.getNVX());
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UGSTensor::increment");
UTensor::increment(v,tdims.getNVX());
}
void UGSTensor::decrement(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UGSTensor::decrement");
UTensor::decrement(v,tdims.getNVX());
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UGSTensor::decrement");
UTensor::decrement(v,tdims.getNVX());
}
/*:21*/
#line 27 "./gs_tensor.cweb"
;
/*22:*/
#line 434 "./gs_tensor.cweb"
FTensor&UGSTensor::fold()const
{
return*(new FGSTensor(*this));
return*(new FGSTensor(*this));
}
/*:22*/
#line 28 "./gs_tensor.cweb"
;
/*23:*/
#line 441 "./gs_tensor.cweb"
int UGSTensor::getOffset(const IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in UGSTensor::getOffset");
return UTensor::getOffset(v,tdims.getNVX());
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in UGSTensor::getOffset");
return UTensor::getOffset(v,tdims.getNVX());
}
/*:23*/
#line 29 "./gs_tensor.cweb"
;
/*24:*/
#line 453 "./gs_tensor.cweb"
void UGSTensor::unfoldData()
{
for(index in= begin();in!=end();++in)
copyColumn(*(getFirstIndexOf(in)),*in);
for(index in= begin();in!=end();++in)
copyColumn(*(getFirstIndexOf(in)),*in);
}
/*:24*/
#line 30 "./gs_tensor.cweb"
;
/*25:*/
#line 464 "./gs_tensor.cweb"
Tensor::index UGSTensor::getFirstIndexOf(const index&in)const
{
IntSequence v(in.getCoor());
int last= 0;
for(int i= 0;i<tdims.getSym().num();i++){
IntSequence vtmp(v,last,last+tdims.getSym()[i]);
vtmp.sort();
last+= tdims.getSym()[i];
}
return index(this,v);
IntSequence v(in.getCoor());
int last= 0;
for(int i= 0;i<tdims.getSym().num();i++){
IntSequence vtmp(v,last,last+tdims.getSym()[i]);
vtmp.sort();
last+= tdims.getSym()[i];
}
return index(this,v);
}
/*:25*/
#line 31 "./gs_tensor.cweb"
;
/*26:*/
#line 480 "./gs_tensor.cweb"
void UGSTensor::contractAndAdd(int i,UGSTensor&out,
const URSingleTensor&col)const
const URSingleTensor&col)const
{
TL_RAISE_IF(i<0||i>=getSym().num(),
"Wrong index for UGSTensor::contractAndAdd");
TL_RAISE_IF(getSym()[i]!=col.dimen()||tdims.getNVS()[i]!=col.nvar(),
"Wrong dimensions for UGSTensor::contractAndAdd");
/*17:*/
#line 349 "./gs_tensor.cweb"
Symmetry sym_left(getSym());
Symmetry sym_right(getSym());
for(int j= 0;j<getSym().num();j++){
if(j<=i)
sym_right[j]= 0;
if(j>=i)
sym_left[j]= 0;
}
/*:17*/
#line 489 "./gs_tensor.cweb"
;
int dleft= TensorDimens(sym_left,tdims.getNVS()).calcUnfoldMaxOffset();
int dright= TensorDimens(sym_right,tdims.getNVS()).calcUnfoldMaxOffset();
KronProdAll kp(3);
kp.setUnit(0,dleft);
kp.setMat(1,col);
kp.setUnit(2,dright);
UGSTensor tmp(out.nrows(),out.getDims());
kp.mult(*this,tmp);
out.add(1.0,tmp);
TL_RAISE_IF(i<0||i>=getSym().num(),
"Wrong index for UGSTensor::contractAndAdd");
TL_RAISE_IF(getSym()[i]!=col.dimen()||tdims.getNVS()[i]!=col.nvar(),
"Wrong dimensions for UGSTensor::contractAndAdd");
/*17:*/
Symmetry sym_left(getSym());
Symmetry sym_right(getSym());
for(int j= 0;j<getSym().num();j++){
if(j<=i)
sym_right[j]= 0;
if(j>=i)
sym_left[j]= 0;
}
/*:17*/
;
int dleft= TensorDimens(sym_left,tdims.getNVS()).calcUnfoldMaxOffset();
int dright= TensorDimens(sym_right,tdims.getNVS()).calcUnfoldMaxOffset();
KronProdAll kp(3);
kp.setUnit(0,dleft);
kp.setMat(1,col);
kp.setUnit(2,dright);
UGSTensor tmp(out.nrows(),out.getDims());
kp.mult(*this,tmp);
out.add(1.0,tmp);
}
/*:26*/
#line 32 "./gs_tensor.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 20 "./gs_tensor.hweb"
#ifndef GS_TENSOR_H
#define GS_TENSOR_H
@ -14,154 +13,144 @@ class UGSTensor;
class FSSparseTensor;
/*2:*/
#line 65 "./gs_tensor.hweb"
class TensorDimens{
protected:
IntSequence nvs;
Symmetry sym;
IntSequence nvmax;
IntSequence nvs;
Symmetry sym;
IntSequence nvmax;
public:
TensorDimens(const Symmetry&s,const IntSequence&nvars)
:nvs(nvars),sym(s),nvmax(sym,nvs){}
TensorDimens(int nvar,int dimen)
:nvs(1),sym(dimen),nvmax(dimen,nvar)
{nvs[0]= nvar;}
TensorDimens(const TensorDimens&td)
:nvs(td.nvs),sym(td.sym),nvmax(td.nvmax){}
virtual~TensorDimens(){}
TensorDimens(const IntSequence&ss,const IntSequence&coor);
const TensorDimens&operator= (const TensorDimens&td)
{nvs= td.nvs;sym= td.sym;nvmax= td.nvmax;return*this;}
bool operator==(const TensorDimens&td)const
{return nvs==td.nvs&&sym==td.sym;}
bool operator!=(const TensorDimens&td)const
{return!operator==(td);}
int dimen()const
{return sym.dimen();}
int getNVX(int i)const
{return nvmax[i];}
const IntSequence&getNVS()const
{return nvs;}
const IntSequence&getNVX()const
{return nvmax;}
const Symmetry&getSym()const
{return sym;}
int calcUnfoldMaxOffset()const;
int calcFoldMaxOffset()const;
int calcFoldOffset(const IntSequence&v)const;
void decrement(IntSequence&v)const;
TensorDimens(const Symmetry&s,const IntSequence&nvars)
:nvs(nvars),sym(s),nvmax(sym,nvs){}
TensorDimens(int nvar,int dimen)
:nvs(1),sym(dimen),nvmax(dimen,nvar)
{nvs[0]= nvar;}
TensorDimens(const TensorDimens&td)
:nvs(td.nvs),sym(td.sym),nvmax(td.nvmax){}
virtual~TensorDimens(){}
TensorDimens(const IntSequence&ss,const IntSequence&coor);
const TensorDimens&operator= (const TensorDimens&td)
{nvs= td.nvs;sym= td.sym;nvmax= td.nvmax;return*this;}
bool operator==(const TensorDimens&td)const
{return nvs==td.nvs&&sym==td.sym;}
bool operator!=(const TensorDimens&td)const
{return!operator==(td);}
int dimen()const
{return sym.dimen();}
int getNVX(int i)const
{return nvmax[i];}
const IntSequence&getNVS()const
{return nvs;}
const IntSequence&getNVX()const
{return nvmax;}
const Symmetry&getSym()const
{return sym;}
int calcUnfoldMaxOffset()const;
int calcFoldMaxOffset()const;
int calcFoldOffset(const IntSequence&v)const;
void decrement(IntSequence&v)const;
};
/*:2*/
#line 33 "./gs_tensor.hweb"
;
/*3:*/
#line 118 "./gs_tensor.hweb"
class GSSparseTensor;
class FGSTensor:public FTensor{
friend class UGSTensor;
const TensorDimens tdims;
friend class UGSTensor;
const TensorDimens tdims;
public:
/*4:*/
#line 149 "./gs_tensor.hweb"
FGSTensor(int r,const TensorDimens&td)
:FTensor(along_col,td.getNVX(),r,
td.calcFoldMaxOffset(),td.dimen()),tdims(td){}
FGSTensor(const FGSTensor&ft)
:FTensor(ft),tdims(ft.tdims){}
FGSTensor(const UGSTensor&ut);
FGSTensor(int first_row,int num,FGSTensor&t)
:FTensor(first_row,num,t),tdims(t.tdims){}
FGSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
FGSTensor(const FFSTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
FGSTensor(const GSSparseTensor&sp);
FGSTensor(FFSTensor&t)
:FTensor(0,t.nrows(),t),tdims(t.nvar(),t.dimen()){}
/*:4*/
#line 125 "./gs_tensor.hweb"
;
virtual~FGSTensor(){}
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const
{tdims.decrement(v);}
UTensor&unfold()const;
const TensorDimens&getDims()const
{return tdims;}
const Symmetry&getSym()const
{return getDims().getSym();}
void contractAndAdd(int i,FGSTensor&out,
const FRSingleTensor&col)const;
int getOffset(const IntSequence&v)const
{return tdims.calcFoldOffset(v);}
/*4:*/
FGSTensor(int r,const TensorDimens&td)
:FTensor(along_col,td.getNVX(),r,
td.calcFoldMaxOffset(),td.dimen()),tdims(td){}
FGSTensor(const FGSTensor&ft)
:FTensor(ft),tdims(ft.tdims){}
FGSTensor(const UGSTensor&ut);
FGSTensor(int first_row,int num,FGSTensor&t)
:FTensor(first_row,num,t),tdims(t.tdims){}
FGSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
FGSTensor(const FFSTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
FGSTensor(const GSSparseTensor&sp);
FGSTensor(FFSTensor&t)
:FTensor(0,t.nrows(),t),tdims(t.nvar(),t.dimen()){}
/*:4*/
;
virtual~FGSTensor(){}
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const
{tdims.decrement(v);}
UTensor&unfold()const;
const TensorDimens&getDims()const
{return tdims;}
const Symmetry&getSym()const
{return getDims().getSym();}
void contractAndAdd(int i,FGSTensor&out,
const FRSingleTensor&col)const;
int getOffset(const IntSequence&v)const
{return tdims.calcFoldOffset(v);}
};
/*:3*/
#line 34 "./gs_tensor.hweb"
;
/*5:*/
#line 172 "./gs_tensor.hweb"
class UGSTensor:public UTensor{
friend class FGSTensor;
const TensorDimens tdims;
friend class FGSTensor;
const TensorDimens tdims;
public:
/*6:*/
#line 205 "./gs_tensor.hweb"
UGSTensor(int r,const TensorDimens&td)
:UTensor(along_col,td.getNVX(),r,
td.calcUnfoldMaxOffset(),td.dimen()),tdims(td){}
UGSTensor(const UGSTensor&ut)
:UTensor(ut),tdims(ut.tdims){}
UGSTensor(const FGSTensor&ft);
UGSTensor(int first_row,int num,UGSTensor&t)
:UTensor(first_row,num,t),tdims(t.tdims){}
UGSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
UGSTensor(const UFSTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
UGSTensor(UFSTensor&t)
:UTensor(0,t.nrows(),t),tdims(t.nvar(),t.dimen()){}
/*:6*/
#line 178 "./gs_tensor.hweb"
;
virtual~UGSTensor(){}
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const;
FTensor&fold()const;
const TensorDimens&getDims()const
{return tdims;}
const Symmetry&getSym()const
{return getDims().getSym();}
void contractAndAdd(int i,UGSTensor&out,
const URSingleTensor&col)const;
int getOffset(const IntSequence&v)const;
/*6:*/
UGSTensor(int r,const TensorDimens&td)
:UTensor(along_col,td.getNVX(),r,
td.calcUnfoldMaxOffset(),td.dimen()),tdims(td){}
UGSTensor(const UGSTensor&ut)
:UTensor(ut),tdims(ut.tdims){}
UGSTensor(const FGSTensor&ft);
UGSTensor(int first_row,int num,UGSTensor&t)
:UTensor(first_row,num,t),tdims(t.tdims){}
UGSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
UGSTensor(const UFSTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td);
UGSTensor(UFSTensor&t)
:UTensor(0,t.nrows(),t),tdims(t.nvar(),t.dimen()){}
/*:6*/
;
virtual~UGSTensor(){}
void increment(IntSequence&v)const;
void decrement(IntSequence&v)const;
FTensor&fold()const;
const TensorDimens&getDims()const
{return tdims;}
const Symmetry&getSym()const
{return getDims().getSym();}
void contractAndAdd(int i,UGSTensor&out,
const URSingleTensor&col)const;
int getOffset(const IntSequence&v)const;
private:
void unfoldData();
void unfoldData();
public:
index getFirstIndexOf(const index&in)const;
index getFirstIndexOf(const index&in)const;
};
/*:5*/
#line 35 "./gs_tensor.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./int_sequence.cweb"
#include "int_sequence.h"
#include "symmetry.h"
@ -9,380 +8,334 @@
#include <limits.h>
/*2:*/
#line 42 "./int_sequence.cweb"
IntSequence::IntSequence(const Symmetry&sy,const IntSequence&se)
:data(new int[sy.dimen()]),length(sy.dimen()),destroy(true)
{
int k= 0;
for(int i= 0;i<sy.num();i++)
for(int j= 0;j<sy[i];j++,k++)
operator[](k)= se[i];
int k= 0;
for(int i= 0;i<sy.num();i++)
for(int j= 0;j<sy[i];j++,k++)
operator[](k)= se[i];
}
/*:2*/
#line 14 "./int_sequence.cweb"
;
/*3:*/
#line 63 "./int_sequence.cweb"
IntSequence::IntSequence(const Symmetry&sy,const vector<int> &se)
:data(new int[sy.num()]),length(sy.num()),destroy(true)
{
TL_RAISE_IF(sy.dimen()<=se[se.size()-1],
"Sequence is not reachable by symmetry in IntSequence()");
for(int i= 0;i<length;i++)
operator[](i)= 0;
for(unsigned int i= 0;i<se.size();i++)
operator[](sy.findClass(se[i]))++;
TL_RAISE_IF(sy.dimen()<=se[se.size()-1],
"Sequence is not reachable by symmetry in IntSequence()");
for(int i= 0;i<length;i++)
operator[](i)= 0;
for(unsigned int i= 0;i<se.size();i++)
operator[](sy.findClass(se[i]))++;
}
/*:3*/
#line 15 "./int_sequence.cweb"
;
/*4:*/
#line 79 "./int_sequence.cweb"
IntSequence::IntSequence(int i,const IntSequence&s)
:data(new int[s.size()+1]),length(s.size()+1),destroy(true)
{
int j= 0;
while(j<s.size()&&s[j]<i)
j++;
for(int jj= 0;jj<j;jj++)
operator[](jj)= s[jj];
operator[](j)= i;
for(int jj= j;jj<s.size();jj++)
operator[](jj+1)= s[jj];
int j= 0;
while(j<s.size()&&s[j]<i)
j++;
for(int jj= 0;jj<j;jj++)
operator[](jj)= s[jj];
operator[](j)= i;
for(int jj= j;jj<s.size();jj++)
operator[](jj+1)= s[jj];
}
/*:4*/
#line 16 "./int_sequence.cweb"
;
/*5:*/
#line 94 "./int_sequence.cweb"
IntSequence::IntSequence(int i,const IntSequence&s,int pos)
:data(new int[s.size()+1]),length(s.size()+1),destroy(true)
{
TL_RAISE_IF(pos<0||pos> s.size(),
"Wrong position for insertion IntSequence constructor");
for(int jj= 0;jj<pos;jj++)
operator[](jj)= s[jj];
operator[](pos)= i;
for(int jj= pos;jj<s.size();jj++)
operator[](jj+1)= s[jj];
TL_RAISE_IF(pos<0||pos> s.size(),
"Wrong position for insertion IntSequence constructor");
for(int jj= 0;jj<pos;jj++)
operator[](jj)= s[jj];
operator[](pos)= i;
for(int jj= pos;jj<s.size();jj++)
operator[](jj+1)= s[jj];
}
/*:5*/
#line 17 "./int_sequence.cweb"
;
/*6:*/
#line 108 "./int_sequence.cweb"
const IntSequence&IntSequence::operator= (const IntSequence&s)
{
TL_RAISE_IF(!destroy&&length!=s.length,
"Wrong length for in-place IntSequence::operator=");
if(destroy&&length!=s.length){
delete[]data;
data= new int[s.length];
destroy= true;
length= s.length;
}
memcpy(data,s.data,sizeof(int)*length);
return*this;
TL_RAISE_IF(!destroy&&length!=s.length,
"Wrong length for in-place IntSequence::operator=");
if(destroy&&length!=s.length){
delete[]data;
data= new int[s.length];
destroy= true;
length= s.length;
}
memcpy(data,s.data,sizeof(int)*length);
return*this;
}
/*:6*/
#line 18 "./int_sequence.cweb"
;
/*7:*/
#line 125 "./int_sequence.cweb"
bool IntSequence::operator==(const IntSequence&s)const
{
if(size()!=s.size())
return false;
int i= 0;
while(i<size()&&operator[](i)==s[i])
i++;
return i==size();
if(size()!=s.size())
return false;
int i= 0;
while(i<size()&&operator[](i)==s[i])
i++;
return i==size();
}
/*:7*/
#line 19 "./int_sequence.cweb"
;
/*8:*/
#line 139 "./int_sequence.cweb"
bool IntSequence::operator<(const IntSequence&s)const
{
int len= min(size(),s.size());
int i= 0;
while(i<len&&operator[](i)==s[i])
i++;
return(i<s.size()&&(i==size()||operator[](i)<s[i]));
int len= min(size(),s.size());
int i= 0;
while(i<len&&operator[](i)==s[i])
i++;
return(i<s.size()&&(i==size()||operator[](i)<s[i]));
}
/*:8*/
#line 20 "./int_sequence.cweb"
;
/*9:*/
#line 151 "./int_sequence.cweb"
bool IntSequence::lessEq(const IntSequence&s)const
{
TL_RAISE_IF(size()!=s.size(),
"Sequence with different lengths in IntSequence::lessEq");
int i= 0;
while(i<size()&&operator[](i)<=s[i])
i++;
return(i==size());
TL_RAISE_IF(size()!=s.size(),
"Sequence with different lengths in IntSequence::lessEq");
int i= 0;
while(i<size()&&operator[](i)<=s[i])
i++;
return(i==size());
}
/*:9*/
#line 21 "./int_sequence.cweb"
;
/*10:*/
#line 164 "./int_sequence.cweb"
bool IntSequence::less(const IntSequence&s)const
{
TL_RAISE_IF(size()!=s.size(),
"Sequence with different lengths in IntSequence::less");
int i= 0;
while(i<size()&&operator[](i)<s[i])
i++;
return(i==size());
TL_RAISE_IF(size()!=s.size(),
"Sequence with different lengths in IntSequence::less");
int i= 0;
while(i<size()&&operator[](i)<s[i])
i++;
return(i==size());
}
/*:10*/
#line 22 "./int_sequence.cweb"
;
/*11:*/
#line 179 "./int_sequence.cweb"
void IntSequence::sort()
{
for(int i= 0;i<length;i++){
int swaps= 0;
for(int j= 0;j<length-1;j++){
if(data[j]> data[j+1]){
int s= data[j+1];
data[j+1]= data[j];
data[j]= s;
swaps++;
}
}
if(swaps==0)
return;
}
for(int i= 0;i<length;i++){
int swaps= 0;
for(int j= 0;j<length-1;j++){
if(data[j]> data[j+1]){
int s= data[j+1];
data[j+1]= data[j];
data[j]= s;
swaps++;
}
}
if(swaps==0)
return;
}
}
/*:11*/
#line 23 "./int_sequence.cweb"
;
/*12:*/
#line 200 "./int_sequence.cweb"
void IntSequence::monotone()
{
for(int i= 1;i<length;i++)
if(data[i-1]> data[i])
data[i]= data[i-1];
for(int i= 1;i<length;i++)
if(data[i-1]> data[i])
data[i]= data[i-1];
}
/*:12*/
#line 24 "./int_sequence.cweb"
;
/*13:*/
#line 213 "./int_sequence.cweb"
void IntSequence::pmonotone(const Symmetry&s)
{
int cum= 0;
for(int i= 0;i<s.num();i++){
for(int j= cum+1;j<cum+s[i];j++)
if(data[j-1]> data[j])
data[j]= data[j-1];
cum+= s[i];
}
int cum= 0;
for(int i= 0;i<s.num();i++){
for(int j= cum+1;j<cum+s[i];j++)
if(data[j-1]> data[j])
data[j]= data[j-1];
cum+= s[i];
}
}
/*:13*/
#line 25 "./int_sequence.cweb"
;
/*14:*/
#line 226 "./int_sequence.cweb"
int IntSequence::sum()const
{
int res= 0;
for(int i= 0;i<length;i++)
res+= operator[](i);
return res;
int res= 0;
for(int i= 0;i<length;i++)
res+= operator[](i);
return res;
}
/*:14*/
#line 26 "./int_sequence.cweb"
;
/*15:*/
#line 238 "./int_sequence.cweb"
int IntSequence::mult(int i1,int i2)const
{
int res= 1;
for(int i= i1;i<i2;i++)
res*= operator[](i);
return res;
int res= 1;
for(int i= i1;i<i2;i++)
res*= operator[](i);
return res;
}
/*:15*/
#line 27 "./int_sequence.cweb"
;
/*16:*/
#line 248 "./int_sequence.cweb"
int IntSequence::getPrefixLength()const
{
int i= 0;
while(i+1<size()&&operator[](i+1)==operator[](0))
i++;
return i+1;
int i= 0;
while(i+1<size()&&operator[](i+1)==operator[](0))
i++;
return i+1;
}
/*:16*/
#line 28 "./int_sequence.cweb"
;
/*17:*/
#line 260 "./int_sequence.cweb"
int IntSequence::getNumDistinct()const
{
int res= 0;
if(size()> 0)
res++;
for(int i= 1;i<size();i++)
if(operator[](i)!=operator[](i-1))
res++;
return res;
int res= 0;
if(size()> 0)
res++;
for(int i= 1;i<size();i++)
if(operator[](i)!=operator[](i-1))
res++;
return res;
}
/*:17*/
#line 29 "./int_sequence.cweb"
;
/*18:*/
#line 275 "./int_sequence.cweb"
int IntSequence::getMax()const
{
int res= INT_MIN;
for(int i= 0;i<size();i++)
if(operator[](i)> res)
res= operator[](i);
return res;
int res= INT_MIN;
for(int i= 0;i<size();i++)
if(operator[](i)> res)
res= operator[](i);
return res;
}
/*:18*/
#line 30 "./int_sequence.cweb"
;
/*19:*/
#line 286 "./int_sequence.cweb"
void IntSequence::add(int i)
{
for(int j= 0;j<size();j++)
operator[](j)+= i;
for(int j= 0;j<size();j++)
operator[](j)+= i;
}
/*:19*/
#line 31 "./int_sequence.cweb"
;
/*20:*/
#line 294 "./int_sequence.cweb"
void IntSequence::add(int f,const IntSequence&s)
{
TL_RAISE_IF(size()!=s.size(),
"Wrong sequence length in IntSequence::add");
for(int j= 0;j<size();j++)
operator[](j)+= f*s[j];
TL_RAISE_IF(size()!=s.size(),
"Wrong sequence length in IntSequence::add");
for(int j= 0;j<size();j++)
operator[](j)+= f*s[j];
}
/*:20*/
#line 32 "./int_sequence.cweb"
;
/*21:*/
#line 304 "./int_sequence.cweb"
bool IntSequence::isPositive()const
{
int i= 0;
while(i<size()&&operator[](i)>=0)
i++;
return(i==size());
int i= 0;
while(i<size()&&operator[](i)>=0)
i++;
return(i==size());
}
/*:21*/
#line 33 "./int_sequence.cweb"
;
/*22:*/
#line 314 "./int_sequence.cweb"
bool IntSequence::isConstant()const
{
bool res= true;
int i= 1;
while(res&&i<size()){
res= res&&operator[](0)==operator[](i);
i++;
}
return res;
bool res= true;
int i= 1;
while(res&&i<size()){
res= res&&operator[](0)==operator[](i);
i++;
}
return res;
}
/*:22*/
#line 34 "./int_sequence.cweb"
;
/*23:*/
#line 327 "./int_sequence.cweb"
bool IntSequence::isSorted()const
{
bool res= true;
int i= 1;
while(res&&i<size()){
res= res&&operator[](i-1)<=operator[](i);
i++;
}
return res;
bool res= true;
int i= 1;
while(res&&i<size()){
res= res&&operator[](i-1)<=operator[](i);
i++;
}
return res;
}
/*:23*/
#line 35 "./int_sequence.cweb"
;
/*24:*/
#line 342 "./int_sequence.cweb"
void IntSequence::print()const
{
printf("[");
for(int i= 0;i<size();i++)
printf("%2d ",operator[](i));
printf("]\n");
printf("[");
for(int i= 0;i<size();i++)
printf("%2d ",operator[](i));
printf("]\n");
}
/*:24*/
#line 36 "./int_sequence.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 28 "./int_sequence.hweb"
#ifndef INT_SEQUENCE_H
#define INT_SEQUENCE_H
@ -11,94 +10,86 @@
using namespace std;
/*2:*/
#line 46 "./int_sequence.hweb"
class Symmetry;
class IntSequence{
int*data;
int length;
bool destroy;
int*data;
int length;
bool destroy;
public:
/*3:*/
#line 83 "./int_sequence.hweb"
IntSequence(int l)
:data(new int[l]),length(l),destroy(true){}
IntSequence(int l,int n)
:data(new int[l]),length(l),destroy(true)
{for(int i= 0;i<length;i++)data[i]= n;}
IntSequence(const IntSequence&s)
:data(new int[s.length]),length(s.length),destroy(true)
{memcpy(data,s.data,length*sizeof(int));}
IntSequence(IntSequence&s,int i1,int i2)
:data(s.data+i1),length(i2-i1),destroy(false){}
IntSequence(const IntSequence&s,int i1,int i2)
:data(new int[i2-i1]),length(i2-i1),destroy(true)
{memcpy(data,s.data+i1,sizeof(int)*length);}
IntSequence(const Symmetry&sy,const vector<int> &se);
IntSequence(const Symmetry&sy,const IntSequence&se);
IntSequence(int i,const IntSequence&s);
IntSequence(int i,const IntSequence&s,int pos);
IntSequence(int l,const int*d)
:data(new int[l]),length(l),destroy(true)
{memcpy(data,d,sizeof(int)*length);}
/*:3*/
#line 53 "./int_sequence.hweb"
;
/*4:*/
#line 107 "./int_sequence.hweb"
const IntSequence&operator= (const IntSequence&s);
virtual~IntSequence()
{if(destroy)delete[]data;}
bool operator==(const IntSequence&s)const;
bool operator!=(const IntSequence&s)const
{return!operator==(s);}
int&operator[](int i)
{return data[i];}
int operator[](int i)const
{return data[i];}
int size()const
{return length;}
/*:4*/
#line 54 "./int_sequence.hweb"
;
/*5:*/
#line 124 "./int_sequence.hweb"
bool operator<(const IntSequence&s)const;
bool operator<=(const IntSequence&s)const
{return(operator==(s)||operator<(s));}
bool lessEq(const IntSequence&s)const;
bool less(const IntSequence&s)const;
/*:5*/
#line 55 "./int_sequence.hweb"
;
void sort();
void monotone();
void pmonotone(const Symmetry&s);
int sum()const;
int mult(int i1,int i2)const;
int mult()const
{return mult(0,length);}
void add(int i);
void add(int f,const IntSequence&s);
int getPrefixLength()const;
int getNumDistinct()const;
int getMax()const;
bool isPositive()const;
bool isConstant()const;
bool isSorted()const;
void print()const;
/*3:*/
IntSequence(int l)
:data(new int[l]),length(l),destroy(true){}
IntSequence(int l,int n)
:data(new int[l]),length(l),destroy(true)
{for(int i= 0;i<length;i++)data[i]= n;}
IntSequence(const IntSequence&s)
:data(new int[s.length]),length(s.length),destroy(true)
{memcpy(data,s.data,length*sizeof(int));}
IntSequence(IntSequence&s,int i1,int i2)
:data(s.data+i1),length(i2-i1),destroy(false){}
IntSequence(const IntSequence&s,int i1,int i2)
:data(new int[i2-i1]),length(i2-i1),destroy(true)
{memcpy(data,s.data+i1,sizeof(int)*length);}
IntSequence(const Symmetry&sy,const vector<int> &se);
IntSequence(const Symmetry&sy,const IntSequence&se);
IntSequence(int i,const IntSequence&s);
IntSequence(int i,const IntSequence&s,int pos);
IntSequence(int l,const int*d)
:data(new int[l]),length(l),destroy(true)
{memcpy(data,d,sizeof(int)*length);}
/*:3*/
;
/*4:*/
const IntSequence&operator= (const IntSequence&s);
virtual~IntSequence()
{if(destroy)delete[]data;}
bool operator==(const IntSequence&s)const;
bool operator!=(const IntSequence&s)const
{return!operator==(s);}
int&operator[](int i)
{return data[i];}
int operator[](int i)const
{return data[i];}
int size()const
{return length;}
/*:4*/
;
/*5:*/
bool operator<(const IntSequence&s)const;
bool operator<=(const IntSequence&s)const
{return(operator==(s)||operator<(s));}
bool lessEq(const IntSequence&s)const;
bool less(const IntSequence&s)const;
/*:5*/
;
void sort();
void monotone();
void pmonotone(const Symmetry&s);
int sum()const;
int mult(int i1,int i2)const;
int mult()const
{return mult(0,length);}
void add(int i);
void add(int f,const IntSequence&s);
int getPrefixLength()const;
int getNumDistinct()const;
int getMax()const;
bool isPositive()const;
bool isConstant()const;
bool isSorted()const;
void print()const;
};
/*:2*/
#line 38 "./int_sequence.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 5 "./kron_prod.cweb"
#include "kron_prod.h"
#include "tl_exception.h"
@ -7,424 +6,372 @@
#include <stdio.h>
/*2:*/
#line 39 "./kron_prod.cweb"
KronProdDimens::KronProdDimens(const KronProdDimens&kd,int i)
:rows((i==0||i==kd.dimen()-1)?(2):(3)),
cols((i==0||i==kd.dimen()-1)?(2):(3))
{
TL_RAISE_IF(i<0||i>=kd.dimen(),
"Wrong index for pickup in KronProdDimens constructor");
int kdim= kd.dimen();
if(i==0){
/*3:*/
#line 61 "./kron_prod.cweb"
rows[0]= kd.rows[0];
rows[1]= kd.rows.mult(1,kdim);
cols[0]= kd.cols[0];
cols[1]= rows[1];
/*:3*/
#line 49 "./kron_prod.cweb"
;
}else if(i==kdim-1){
/*4:*/
#line 71 "./kron_prod.cweb"
rows[0]= kd.cols.mult(0,kdim-1);
rows[1]= kd.rows[kdim-1];
cols[0]= rows[0];
cols[1]= kd.cols[kdim-1];
/*:4*/
#line 51 "./kron_prod.cweb"
;
}else{
/*5:*/
#line 83 "./kron_prod.cweb"
rows[0]= kd.cols.mult(0,i);
cols[0]= rows[0];
rows[1]= kd.rows[i];
cols[1]= kd.cols[i];
cols[2]= kd.rows.mult(i+1,kdim);
rows[2]= cols[2];
/*:5*/
#line 53 "./kron_prod.cweb"
;
}
TL_RAISE_IF(i<0||i>=kd.dimen(),
"Wrong index for pickup in KronProdDimens constructor");
int kdim= kd.dimen();
if(i==0){
/*3:*/
rows[0]= kd.rows[0];
rows[1]= kd.rows.mult(1,kdim);
cols[0]= kd.cols[0];
cols[1]= rows[1];
/*:3*/
;
}else if(i==kdim-1){
/*4:*/
rows[0]= kd.cols.mult(0,kdim-1);
rows[1]= kd.rows[kdim-1];
cols[0]= rows[0];
cols[1]= kd.cols[kdim-1];
/*:4*/
;
}else{
/*5:*/
rows[0]= kd.cols.mult(0,i);
cols[0]= rows[0];
rows[1]= kd.rows[i];
cols[1]= kd.cols[i];
cols[2]= kd.rows.mult(i+1,kdim);
rows[2]= cols[2];
/*:5*/
;
}
}
/*:2*/
#line 11 "./kron_prod.cweb"
;
/*6:*/
#line 95 "./kron_prod.cweb"
void KronProd::checkDimForMult(const ConstTwoDMatrix&in,const TwoDMatrix&out)const
{
int my_rows;
int my_cols;
kpd.getRC(my_rows,my_cols);
TL_RAISE_IF(in.nrows()!=out.nrows()||in.ncols()!=my_rows,
"Wrong dimensions for KronProd in KronProd::checkDimForMult");
int my_rows;
int my_cols;
kpd.getRC(my_rows,my_cols);
TL_RAISE_IF(in.nrows()!=out.nrows()||in.ncols()!=my_rows,
"Wrong dimensions for KronProd in KronProd::checkDimForMult");
}
/*:6*/
#line 12 "./kron_prod.cweb"
;
/*7:*/
#line 108 "./kron_prod.cweb"
void KronProd::kronMult(const ConstVector&v1,const ConstVector&v2,
Vector&res)
Vector&res)
{
TL_RAISE_IF(res.length()!=v1.length()*v2.length(),
"Wrong vector lengths in KronProd::kronMult");
res.zeros();
for(int i= 0;i<v1.length();i++){
Vector sub(res,i*v2.length(),v2.length());
sub.add(v1[i],v2);
}
TL_RAISE_IF(res.length()!=v1.length()*v2.length(),
"Wrong vector lengths in KronProd::kronMult");
res.zeros();
for(int i= 0;i<v1.length();i++){
Vector sub(res,i*v2.length(),v2.length());
sub.add(v1[i],v2);
}
}
/*:7*/
#line 13 "./kron_prod.cweb"
;
/*8:*/
#line 123 "./kron_prod.cweb"
void KronProdAll::setMat(int i,const TwoDMatrix&m)
{
matlist[i]= &m;
kpd.setRC(i,m.nrows(),m.ncols());
matlist[i]= &m;
kpd.setRC(i,m.nrows(),m.ncols());
}
/*:8*/
#line 14 "./kron_prod.cweb"
;
/*9:*/
#line 131 "./kron_prod.cweb"
void KronProdAll::setUnit(int i,int n)
{
matlist[i]= NULL;
kpd.setRC(i,n,n);
matlist[i]= NULL;
kpd.setRC(i,n,n);
}
/*:9*/
#line 15 "./kron_prod.cweb"
;
/*10:*/
#line 139 "./kron_prod.cweb"
bool KronProdAll::isUnit()const
{
int i= 0;
while(i<dimen()&&matlist[i]==NULL)
i++;
return i==dimen();
int i= 0;
while(i<dimen()&&matlist[i]==NULL)
i++;
return i==dimen();
}
/*:10*/
#line 16 "./kron_prod.cweb"
;
/*22:*/
#line 359 "./kron_prod.cweb"
Vector*KronProdAll::multRows(const IntSequence&irows)const
{
TL_RAISE_IF(irows.size()!=dimen(),
"Wrong length of row indices in KronProdAll::multRows");
Vector*last= NULL;
ConstVector*row;
vector<Vector*> to_delete;
for(int i= 0;i<dimen();i++){
int j= dimen()-1-i;
/*23:*/
#line 387 "./kron_prod.cweb"
if(matlist[j])
row= new ConstVector(irows[j],*(matlist[j]));
else{
Vector*aux= new Vector(ncols(j));
aux->zeros();
(*aux)[irows[j]]= 1.0;
to_delete.push_back(aux);
row= new ConstVector(*aux);
}
/*:23*/
#line 370 "./kron_prod.cweb"
;
/*24:*/
#line 402 "./kron_prod.cweb"
if(last){
Vector*newlast;
newlast= new Vector(last->length()*row->length());
kronMult(*row,ConstVector(*last),*newlast);
delete last;
last= newlast;
}else{
last= new Vector(*row);
}
/*:24*/
#line 371 "./kron_prod.cweb"
;
delete row;
}
for(unsigned int i= 0;i<to_delete.size();i++)
delete to_delete[i];
return last;
TL_RAISE_IF(irows.size()!=dimen(),
"Wrong length of row indices in KronProdAll::multRows");
Vector*last= NULL;
ConstVector*row;
vector<Vector*> to_delete;
for(int i= 0;i<dimen();i++){
int j= dimen()-1-i;
/*23:*/
if(matlist[j])
row= new ConstVector(irows[j],*(matlist[j]));
else{
Vector*aux= new Vector(ncols(j));
aux->zeros();
(*aux)[irows[j]]= 1.0;
to_delete.push_back(aux);
row= new ConstVector(*aux);
}
/*:23*/
;
/*24:*/
if(last){
Vector*newlast;
newlast= new Vector(last->length()*row->length());
kronMult(*row,ConstVector(*last),*newlast);
delete last;
last= newlast;
}else{
last= new Vector(*row);
}
/*:24*/
;
delete row;
}
for(unsigned int i= 0;i<to_delete.size();i++)
delete to_delete[i];
return last;
}
/*:22*/
#line 17 "./kron_prod.cweb"
;
/*11:*/
#line 156 "./kron_prod.cweb"
void KronProdIA::mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const
{
checkDimForMult(in,out);
int id_cols= kpd.cols[0];
ConstTwoDMatrix a(mat);
for(int i= 0;i<id_cols;i++){
TwoDMatrix outi(out,i*a.ncols(),a.ncols());
ConstTwoDMatrix ini(in,i*a.nrows(),a.nrows());
outi.mult(ini,a);
}
checkDimForMult(in,out);
int id_cols= kpd.cols[0];
ConstTwoDMatrix a(mat);
for(int i= 0;i<id_cols;i++){
TwoDMatrix outi(out,i*a.ncols(),a.ncols());
ConstTwoDMatrix ini(in,i*a.nrows(),a.nrows());
outi.mult(ini,a);
}
}
/*:11*/
#line 18 "./kron_prod.cweb"
;
/*12:*/
#line 172 "./kron_prod.cweb"
KronProdAI::KronProdAI(const KronProdIAI&kpiai)
:KronProd(KronProdDimens(2)),mat(kpiai.mat)
{
kpd.rows[0]= mat.nrows();
kpd.cols[0]= mat.ncols();
kpd.rows[1]= kpiai.kpd.rows[2];
kpd.cols[1]= kpiai.kpd.cols[2];
kpd.rows[0]= mat.nrows();
kpd.cols[0]= mat.ncols();
kpd.rows[1]= kpiai.kpd.rows[2];
kpd.cols[1]= kpiai.kpd.cols[2];
}
/*:12*/
#line 19 "./kron_prod.cweb"
;
/*13:*/
#line 202 "./kron_prod.cweb"
void KronProdAI::mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const
{
checkDimForMult(in,out);
int id_cols= kpd.cols[1];
ConstTwoDMatrix a(mat);
if(in.getLD()==in.nrows()){
ConstTwoDMatrix in_resh(in.nrows()*id_cols,a.nrows(),in.getData().base());
TwoDMatrix out_resh(in.nrows()*id_cols,a.ncols(),out.getData().base());
out_resh.mult(in_resh,a);
}else{
out.zeros();
for(int i= 0;i<a.ncols();i++){
TwoDMatrix outi(out,i*id_cols,id_cols);
for(int j= 0;j<a.nrows();j++){
ConstTwoDMatrix ini(in,j*id_cols,id_cols);
outi.add(a.get(j,i),ini);
}
}
}
checkDimForMult(in,out);
int id_cols= kpd.cols[1];
ConstTwoDMatrix a(mat);
if(in.getLD()==in.nrows()){
ConstTwoDMatrix in_resh(in.nrows()*id_cols,a.nrows(),in.getData().base());
TwoDMatrix out_resh(in.nrows()*id_cols,a.ncols(),out.getData().base());
out_resh.mult(in_resh,a);
}else{
out.zeros();
for(int i= 0;i<a.ncols();i++){
TwoDMatrix outi(out,i*id_cols,id_cols);
for(int j= 0;j<a.nrows();j++){
ConstTwoDMatrix ini(in,j*id_cols,id_cols);
outi.add(a.get(j,i),ini);
}
}
}
}
/*:13*/
#line 20 "./kron_prod.cweb"
;
/*14:*/
#line 240 "./kron_prod.cweb"
void KronProdIAI::mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const
{
checkDimForMult(in,out);
int id_cols= kpd.cols[0];
KronProdAI akronid(*this);
int in_bl_width;
int out_bl_width;
akronid.kpd.getRC(in_bl_width,out_bl_width);
for(int i= 0;i<id_cols;i++){
TwoDMatrix outi(out,i*out_bl_width,out_bl_width);
ConstTwoDMatrix ini(in,i*in_bl_width,in_bl_width);
akronid.mult(ini,outi);
}
checkDimForMult(in,out);
int id_cols= kpd.cols[0];
KronProdAI akronid(*this);
int in_bl_width;
int out_bl_width;
akronid.kpd.getRC(in_bl_width,out_bl_width);
for(int i= 0;i<id_cols;i++){
TwoDMatrix outi(out,i*out_bl_width,out_bl_width);
ConstTwoDMatrix ini(in,i*in_bl_width,in_bl_width);
akronid.mult(ini,outi);
}
}
/*:14*/
#line 21 "./kron_prod.cweb"
;
/*15:*/
#line 273 "./kron_prod.cweb"
void KronProdAll::mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const
{
/*16:*/
#line 287 "./kron_prod.cweb"
if(isUnit()){
out.zeros();
out.add(1.0,in);
return;
}
/*:16*/
#line 276 "./kron_prod.cweb"
;
/*17:*/
#line 297 "./kron_prod.cweb"
bool is_zero= false;
for(int i= 0;i<dimen()&&!is_zero;i++)
is_zero= matlist[i]&&matlist[i]->isZero();
if(is_zero||in.isZero()){
out.zeros();
return;
}
/*:17*/
#line 277 "./kron_prod.cweb"
;
/*18:*/
#line 307 "./kron_prod.cweb"
if(dimen()==1){
if(matlist[0])
out.mult(in,ConstTwoDMatrix(*(matlist[0])));
return;
}
/*:18*/
#line 278 "./kron_prod.cweb"
;
int c;
TwoDMatrix*last= NULL;
/*19:*/
#line 317 "./kron_prod.cweb"
if(matlist[0]){
KronProdAI akronid(*this);
c= akronid.kpd.ncols();
last= new TwoDMatrix(in.nrows(),c);
akronid.mult(in,*last);
}else{
last= new TwoDMatrix(in.nrows(),in.ncols(),in.getData().base());
}
/*:19*/
#line 281 "./kron_prod.cweb"
;
/*20:*/
#line 331 "./kron_prod.cweb"
for(int i= 1;i<dimen()-1;i++){
if(matlist[i]){
KronProdIAI interkron(*this,i);
c= interkron.kpd.ncols();
TwoDMatrix*newlast= new TwoDMatrix(in.nrows(),c);
interkron.mult(*last,*newlast);
delete last;
last= newlast;
}
}
/*:20*/
#line 282 "./kron_prod.cweb"
;
/*21:*/
#line 346 "./kron_prod.cweb"
if(matlist[dimen()-1]){
KronProdIA idkrona(*this);
idkrona.mult(*last,out);
}else{
out= *last;
}
delete last;
/*:21*/
#line 283 "./kron_prod.cweb"
;
/*16:*/
if(isUnit()){
out.zeros();
out.add(1.0,in);
return;
}
/*:16*/
;
/*17:*/
bool is_zero= false;
for(int i= 0;i<dimen()&&!is_zero;i++)
is_zero= matlist[i]&&matlist[i]->isZero();
if(is_zero||in.isZero()){
out.zeros();
return;
}
/*:17*/
;
/*18:*/
if(dimen()==1){
if(matlist[0])
out.mult(in,ConstTwoDMatrix(*(matlist[0])));
return;
}
/*:18*/
;
int c;
TwoDMatrix*last= NULL;
/*19:*/
if(matlist[0]){
KronProdAI akronid(*this);
c= akronid.kpd.ncols();
last= new TwoDMatrix(in.nrows(),c);
akronid.mult(in,*last);
}else{
last= new TwoDMatrix(in.nrows(),in.ncols(),in.getData().base());
}
/*:19*/
;
/*20:*/
for(int i= 1;i<dimen()-1;i++){
if(matlist[i]){
KronProdIAI interkron(*this,i);
c= interkron.kpd.ncols();
TwoDMatrix*newlast= new TwoDMatrix(in.nrows(),c);
interkron.mult(*last,*newlast);
delete last;
last= newlast;
}
}
/*:20*/
;
/*21:*/
if(matlist[dimen()-1]){
KronProdIA idkrona(*this);
idkrona.mult(*last,out);
}else{
out= *last;
}
delete last;
/*:21*/
;
}
/*:15*/
#line 22 "./kron_prod.cweb"
;
/*25:*/
#line 420 "./kron_prod.cweb"
void KronProdAllOptim::optimizeOrder()
{
for(int i= 0;i<dimen();i++){
int swaps= 0;
for(int j= 0;j<dimen()-1;j++){
if(((double)kpd.rows[j])/kpd.cols[j]<((double)kpd.rows[j+1])/kpd.cols[j+1]){
/*26:*/
#line 438 "./kron_prod.cweb"
int s= kpd.rows[j+1];
kpd.rows[j+1]= kpd.rows[j];
kpd.rows[j]= s;
s= kpd.cols[j+1];
kpd.cols[j+1]= kpd.cols[j];
kpd.cols[j]= s;
const TwoDMatrix*m= matlist[j+1];
matlist[j+1]= matlist[j];
matlist[j]= m;
/*:26*/
#line 427 "./kron_prod.cweb"
;
/*27:*/
#line 450 "./kron_prod.cweb"
s= oper.getMap()[j+1];
oper.getMap()[j+1]= oper.getMap()[j];
oper.getMap()[j]= s;
swaps++;
/*:27*/
#line 428 "./kron_prod.cweb"
;
}
}
if(swaps==0){
return;
}
}
for(int i= 0;i<dimen();i++){
int swaps= 0;
for(int j= 0;j<dimen()-1;j++){
if(((double)kpd.rows[j])/kpd.cols[j]<((double)kpd.rows[j+1])/kpd.cols[j+1]){
/*26:*/
int s= kpd.rows[j+1];
kpd.rows[j+1]= kpd.rows[j];
kpd.rows[j]= s;
s= kpd.cols[j+1];
kpd.cols[j+1]= kpd.cols[j];
kpd.cols[j]= s;
const TwoDMatrix*m= matlist[j+1];
matlist[j+1]= matlist[j];
matlist[j]= m;
/*:26*/
;
/*27:*/
s= oper.getMap()[j+1];
oper.getMap()[j+1]= oper.getMap()[j];
oper.getMap()[j]= s;
swaps++;
/*:27*/
;
}
}
if(swaps==0){
return;
}
}
}
/*:25*/
#line 23 "./kron_prod.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 30 "./kron_prod.hweb"
#ifndef KRON_PROD_H
@ -16,211 +15,191 @@ class KronProdIAI;
class KronProdAI;
/*2:*/
#line 58 "./kron_prod.hweb"
class KronProdDimens{
friend class KronProdAll;
friend class KronProdAllOptim;
friend class KronProdIA;
friend class KronProdIAI;
friend class KronProdAI;
friend class KronProdAll;
friend class KronProdAllOptim;
friend class KronProdIA;
friend class KronProdIAI;
friend class KronProdAI;
private:
IntSequence rows;
IntSequence cols;
IntSequence rows;
IntSequence cols;
public:
/*3:*/
#line 86 "./kron_prod.hweb"
KronProdDimens(int dim)
:rows(dim,0),cols(dim,0){}
KronProdDimens(const KronProdDimens&kd)
:rows(kd.rows),cols(kd.cols){}
KronProdDimens(const KronProdDimens&kd,int i);
/*:3*/
#line 69 "./kron_prod.hweb"
;
/*4:*/
#line 94 "./kron_prod.hweb"
const KronProdDimens&operator= (const KronProdDimens&kd)
{rows= kd.rows;cols= kd.cols;return*this;}
bool operator==(const KronProdDimens&kd)const
{return rows==kd.rows&&cols==kd.cols;}
/*:4*/
#line 70 "./kron_prod.hweb"
;
/*5:*/
#line 101 "./kron_prod.hweb"
int dimen()const
{return rows.size();}
void setRC(int i,int r,int c)
{rows[i]= r;cols[i]= c;}
void getRC(int i,int&r,int&c)const
{r= rows[i];c= cols[i];}
void getRC(int&r,int&c)const
{r= rows.mult();c= cols.mult();}
int nrows()const
{return rows.mult();}
int ncols()const
{return cols.mult();}
int nrows(int i)const
{return rows[i];}
int ncols(int i)const
{return cols[i];}
/*:5*/
#line 71 "./kron_prod.hweb"
;
/*3:*/
KronProdDimens(int dim)
:rows(dim,0),cols(dim,0){}
KronProdDimens(const KronProdDimens&kd)
:rows(kd.rows),cols(kd.cols){}
KronProdDimens(const KronProdDimens&kd,int i);
/*:3*/
;
/*4:*/
const KronProdDimens&operator= (const KronProdDimens&kd)
{rows= kd.rows;cols= kd.cols;return*this;}
bool operator==(const KronProdDimens&kd)const
{return rows==kd.rows&&cols==kd.cols;}
/*:4*/
;
/*5:*/
int dimen()const
{return rows.size();}
void setRC(int i,int r,int c)
{rows[i]= r;cols[i]= c;}
void getRC(int i,int&r,int&c)const
{r= rows[i];c= cols[i];}
void getRC(int&r,int&c)const
{r= rows.mult();c= cols.mult();}
int nrows()const
{return rows.mult();}
int ncols()const
{return cols.mult();}
int nrows(int i)const
{return rows[i];}
int ncols(int i)const
{return cols[i];}
/*:5*/
;
};
/*:2*/
#line 45 "./kron_prod.hweb"
;
/*6:*/
#line 130 "./kron_prod.hweb"
class KronProd{
protected:
KronProdDimens kpd;
KronProdDimens kpd;
public:
KronProd(int dim)
:kpd(dim){}
KronProd(const KronProdDimens&kd)
:kpd(kd){}
KronProd(const KronProd&kp)
:kpd(kp.kpd){}
virtual~KronProd(){}
int dimen()const
{return kpd.dimen();}
virtual void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const= 0;
void mult(const TwoDMatrix&in,TwoDMatrix&out)const
{mult(ConstTwoDMatrix(in),out);}
void checkDimForMult(const ConstTwoDMatrix&in,const TwoDMatrix&out)const;
void checkDimForMult(const TwoDMatrix&in,const TwoDMatrix&out)const
{checkDimForMult(ConstTwoDMatrix(in),out);}
static void kronMult(const ConstVector&v1,const ConstVector&v2,
Vector&res);
int nrows()const
{return kpd.nrows();}
int ncols()const
{return kpd.ncols();}
int nrows(int i)const
{return kpd.nrows(i);}
int ncols(int i)const
{return kpd.ncols(i);}
KronProd(int dim)
:kpd(dim){}
KronProd(const KronProdDimens&kd)
:kpd(kd){}
KronProd(const KronProd&kp)
:kpd(kp.kpd){}
virtual~KronProd(){}
int dimen()const
{return kpd.dimen();}
virtual void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const= 0;
void mult(const TwoDMatrix&in,TwoDMatrix&out)const
{mult(ConstTwoDMatrix(in),out);}
void checkDimForMult(const ConstTwoDMatrix&in,const TwoDMatrix&out)const;
void checkDimForMult(const TwoDMatrix&in,const TwoDMatrix&out)const
{checkDimForMult(ConstTwoDMatrix(in),out);}
static void kronMult(const ConstVector&v1,const ConstVector&v2,
Vector&res);
int nrows()const
{return kpd.nrows();}
int ncols()const
{return kpd.ncols();}
int nrows(int i)const
{return kpd.nrows(i);}
int ncols(int i)const
{return kpd.ncols(i);}
};
/*:6*/
#line 46 "./kron_prod.hweb"
;
/*7:*/
#line 183 "./kron_prod.hweb"
class KronProdAll:public KronProd{
friend class KronProdIA;
friend class KronProdIAI;
friend class KronProdAI;
friend class KronProdIA;
friend class KronProdIAI;
friend class KronProdAI;
protected:
const TwoDMatrix**const matlist;
const TwoDMatrix**const matlist;
public:
KronProdAll(int dim)
:KronProd(dim),matlist(new const TwoDMatrix*[dim]){}
virtual~KronProdAll()
{delete[]matlist;}
void setMat(int i,const TwoDMatrix&m);
void setUnit(int i,int n);
const TwoDMatrix&getMat(int i)const
{return*(matlist[i]);}
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
Vector*multRows(const IntSequence&irows)const;
KronProdAll(int dim)
:KronProd(dim),matlist(new const TwoDMatrix*[dim]){}
virtual~KronProdAll()
{delete[]matlist;}
void setMat(int i,const TwoDMatrix&m);
void setUnit(int i,int n);
const TwoDMatrix&getMat(int i)const
{return*(matlist[i]);}
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
Vector*multRows(const IntSequence&irows)const;
private:
bool isUnit()const;
bool isUnit()const;
};
/*:7*/
#line 47 "./kron_prod.hweb"
;
/*8:*/
#line 235 "./kron_prod.hweb"
class KronProdAllOptim:public KronProdAll{
protected:
Permutation oper;
Permutation oper;
public:
KronProdAllOptim(int dim)
:KronProdAll(dim),oper(dim){}
void optimizeOrder();
const Permutation&getPer()const
{return oper;}
KronProdAllOptim(int dim)
:KronProdAll(dim),oper(dim){}
void optimizeOrder();
const Permutation&getPer()const
{return oper;}
};
/*:8*/
#line 48 "./kron_prod.hweb"
;
/*9:*/
#line 250 "./kron_prod.hweb"
class KronProdIA:public KronProd{
friend class KronProdAll;
const TwoDMatrix&mat;
friend class KronProdAll;
const TwoDMatrix&mat;
public:
KronProdIA(const KronProdAll&kpa)
:KronProd(KronProdDimens(kpa.kpd,kpa.dimen()-1)),
mat(kpa.getMat(kpa.dimen()-1))
{}
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
KronProdIA(const KronProdAll&kpa)
:KronProd(KronProdDimens(kpa.kpd,kpa.dimen()-1)),
mat(kpa.getMat(kpa.dimen()-1))
{}
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
};
/*:9*/
#line 49 "./kron_prod.hweb"
;
/*10:*/
#line 265 "./kron_prod.hweb"
class KronProdAI:public KronProd{
friend class KronProdIAI;
friend class KronProdAll;
const TwoDMatrix&mat;
friend class KronProdIAI;
friend class KronProdAll;
const TwoDMatrix&mat;
public:
KronProdAI(const KronProdAll&kpa)
:KronProd(KronProdDimens(kpa.kpd,0)),
mat(kpa.getMat(0))
{}
KronProdAI(const KronProdIAI&kpiai);
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
KronProdAI(const KronProdAll&kpa)
:KronProd(KronProdDimens(kpa.kpd,0)),
mat(kpa.getMat(0))
{}
KronProdAI(const KronProdIAI&kpiai);
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
};
/*:10*/
#line 50 "./kron_prod.hweb"
;
/*11:*/
#line 282 "./kron_prod.hweb"
class KronProdIAI:public KronProd{
friend class KronProdAI;
friend class KronProdAll;
const TwoDMatrix&mat;
friend class KronProdAI;
friend class KronProdAll;
const TwoDMatrix&mat;
public:
KronProdIAI(const KronProdAll&kpa,int i)
:KronProd(KronProdDimens(kpa.kpd,i)),
mat(kpa.getMat(i))
{}
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
KronProdIAI(const KronProdAll&kpa,int i)
:KronProd(KronProdDimens(kpa.kpd,i)),
mat(kpa.getMat(i))
{}
void mult(const ConstTwoDMatrix&in,TwoDMatrix&out)const;
};
/*:11*/
#line 51 "./kron_prod.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./normal_moments.cweb"
#include "normal_moments.h"
#include "permutation.h"
@ -7,105 +6,95 @@
#include "tl_static.h"
/*2:*/
#line 18 "./normal_moments.cweb"
UNormalMoments::UNormalMoments(int maxdim,const TwoDMatrix&v)
:TensorContainer<URSingleTensor> (1)
{
if(maxdim>=2)
generateMoments(maxdim,v);
if(maxdim>=2)
generateMoments(maxdim,v);
}
/*:2*/
#line 12 "./normal_moments.cweb"
;
/*3:*/
#line 35 "./normal_moments.cweb"
void UNormalMoments::generateMoments(int maxdim,const TwoDMatrix&v)
{
TL_RAISE_IF(v.nrows()!=v.ncols(),
"Variance-covariance matrix is not square in UNormalMoments constructor");
int nv= v.nrows();
URSingleTensor*mom2= new URSingleTensor(nv,2);
mom2->getData()= v.getData();
insert(mom2);
URSingleTensor*kronv= new URSingleTensor(nv,2);
kronv->getData()= v.getData();
for(int d= 4;d<=maxdim;d+= 2){
URSingleTensor*newkronv= new URSingleTensor(nv,d);
KronProd::kronMult(ConstVector(v.getData()),
ConstVector(kronv->getData()),
newkronv->getData());
delete kronv;
kronv= newkronv;
URSingleTensor*mom= new URSingleTensor(nv,d);
/*4:*/
#line 70 "./normal_moments.cweb"
mom->zeros();
const EquivalenceSet eset= ebundle.get(d);
for(EquivalenceSet::const_iterator cit= eset.begin();
cit!=eset.end();cit++){
if(selectEquiv(*cit)){
Permutation per(*cit);
per.inverse();
for(Tensor::index it= kronv->begin();it!=kronv->end();++it){
IntSequence ind(kronv->dimen());
per.apply(it.getCoor(),ind);
Tensor::index it2(mom,ind);
mom->get(*it2,0)+= kronv->get(*it,0);
}
}
}
/*:4*/
#line 55 "./normal_moments.cweb"
;
insert(mom);
}
delete kronv;
TL_RAISE_IF(v.nrows()!=v.ncols(),
"Variance-covariance matrix is not square in UNormalMoments constructor");
int nv= v.nrows();
URSingleTensor*mom2= new URSingleTensor(nv,2);
mom2->getData()= v.getData();
insert(mom2);
URSingleTensor*kronv= new URSingleTensor(nv,2);
kronv->getData()= v.getData();
for(int d= 4;d<=maxdim;d+= 2){
URSingleTensor*newkronv= new URSingleTensor(nv,d);
KronProd::kronMult(ConstVector(v.getData()),
ConstVector(kronv->getData()),
newkronv->getData());
delete kronv;
kronv= newkronv;
URSingleTensor*mom= new URSingleTensor(nv,d);
/*4:*/
mom->zeros();
const EquivalenceSet eset= ebundle.get(d);
for(EquivalenceSet::const_iterator cit= eset.begin();
cit!=eset.end();cit++){
if(selectEquiv(*cit)){
Permutation per(*cit);
per.inverse();
for(Tensor::index it= kronv->begin();it!=kronv->end();++it){
IntSequence ind(kronv->dimen());
per.apply(it.getCoor(),ind);
Tensor::index it2(mom,ind);
mom->get(*it2,0)+= kronv->get(*it,0);
}
}
}
/*:4*/
;
insert(mom);
}
delete kronv;
}
/*:3*/
#line 13 "./normal_moments.cweb"
;
/*5:*/
#line 88 "./normal_moments.cweb"
bool UNormalMoments::selectEquiv(const Equivalence&e)
{
if(2*e.numClasses()!=e.getN())
return false;
for(Equivalence::const_seqit si= e.begin();
si!=e.end();++si){
if((*si).length()!=2)
return false;
}
return true;
if(2*e.numClasses()!=e.getN())
return false;
for(Equivalence::const_seqit si= e.begin();
si!=e.end();++si){
if((*si).length()!=2)
return false;
}
return true;
}
/*:5*/
#line 14 "./normal_moments.cweb"
;
/*6:*/
#line 103 "./normal_moments.cweb"
FNormalMoments::FNormalMoments(const UNormalMoments&moms)
:TensorContainer<FRSingleTensor> (1)
{
for(UNormalMoments::const_iterator it= moms.begin();
it!=moms.end();++it){
FRSingleTensor*fm= new FRSingleTensor(*((*it).second));
insert(fm);
}
for(UNormalMoments::const_iterator it= moms.begin();
it!=moms.end();++it){
FRSingleTensor*fm= new FRSingleTensor(*((*it).second));
insert(fm);
}
}
/*:6*/
#line 15 "./normal_moments.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 110 "./normal_moments.hweb"
#ifndef NORMAL_MOMENTS_H
#define NORMAL_MOMENTS_H
@ -7,7 +6,6 @@
#include "t_container.h"
/*2:*/
#line 122 "./normal_moments.hweb"
class UNormalMoments:public TensorContainer<URSingleTensor> {
public:
@ -18,10 +16,8 @@ static bool selectEquiv(const Equivalence&e);
};
/*:2*/
#line 116 "./normal_moments.hweb"
;
/*3:*/
#line 132 "./normal_moments.hweb"
class FNormalMoments:public TensorContainer<FRSingleTensor> {
public:
@ -30,7 +26,6 @@ FNormalMoments(const UNormalMoments&moms);
/*:3*/
#line 117 "./normal_moments.hweb"
;
#endif

View File

@ -1,211 +1,186 @@
/*1:*/
#line 5 "./permutation.cweb"
#include "permutation.h"
#include "tl_exception.h"
/*2:*/
#line 25 "./permutation.cweb"
void Permutation::apply(const IntSequence&src,IntSequence&tar)const
{
TL_RAISE_IF(src.size()!=permap.size()||tar.size()!=permap.size(),
"Wrong sizes of input or output in Permutation::apply");
for(int i= 0;i<permap.size();i++)
tar[i]= src[permap[i]];
TL_RAISE_IF(src.size()!=permap.size()||tar.size()!=permap.size(),
"Wrong sizes of input or output in Permutation::apply");
for(int i= 0;i<permap.size();i++)
tar[i]= src[permap[i]];
}
void Permutation::apply(IntSequence&tar)const
{
IntSequence tmp(tar);
apply(tmp,tar);
IntSequence tmp(tar);
apply(tmp,tar);
}
/*:2*/
#line 10 "./permutation.cweb"
;
/*3:*/
#line 42 "./permutation.cweb"
void Permutation::inverse()
{
IntSequence former(permap);
for(int i= 0;i<size();i++)
permap[former[i]]= i;
IntSequence former(permap);
for(int i= 0;i<size();i++)
permap[former[i]]= i;
}
/*:3*/
#line 11 "./permutation.cweb"
;
/*4:*/
#line 54 "./permutation.cweb"
int Permutation::tailIdentity()const
{
int i= permap.size();
while(i> 0&&permap[i-1]==i-1)
i--;
return permap.size()-i;
int i= permap.size();
while(i> 0&&permap[i-1]==i-1)
i--;
return permap.size()-i;
}
/*:4*/
#line 12 "./permutation.cweb"
;
/*5:*/
#line 72 "./permutation.cweb"
void Permutation::computeSortingMap(const IntSequence&s)
{
IntSequence srt(s);
srt.sort();
IntSequence flags(s.size(),0);
for(int i= 0;i<s.size();i++){
int j= 0;
while(j<s.size()&&(flags[j]||srt[j]!=s[i]))
j++;
TL_RAISE_IF(j==s.size(),
"Internal algorithm error in Permutation::computeSortingMap");
flags[j]= 1;
permap[i]= j;
}
IntSequence srt(s);
srt.sort();
IntSequence flags(s.size(),0);
for(int i= 0;i<s.size();i++){
int j= 0;
while(j<s.size()&&(flags[j]||srt[j]!=s[i]))
j++;
TL_RAISE_IF(j==s.size(),
"Internal algorithm error in Permutation::computeSortingMap");
flags[j]= 1;
permap[i]= j;
}
}
/*:5*/
#line 13 "./permutation.cweb"
;
/*6:*/
#line 91 "./permutation.cweb"
PermutationSet::PermutationSet()
:order(1),size(1),pers(new const Permutation*[size])
{
pers[0]= new Permutation(1);
pers[0]= new Permutation(1);
}
/*:6*/
#line 14 "./permutation.cweb"
;
/*7:*/
#line 99 "./permutation.cweb"
PermutationSet::PermutationSet(const PermutationSet&sp,int n)
:order(n),size(n*sp.size),
pers(new const Permutation*[size])
{
for(int i= 0;i<size;i++)
pers[i]= NULL;
TL_RAISE_IF(n!=sp.order+1,
"Wrong new order in PermutationSet constructor");
int k= 0;
for(int i= 0;i<sp.size;i++){
for(int j= 0;j<order;j++,k++){
pers[k]= new Permutation(*(sp.pers[i]),j);
}
}
for(int i= 0;i<size;i++)
pers[i]= NULL;
TL_RAISE_IF(n!=sp.order+1,
"Wrong new order in PermutationSet constructor");
int k= 0;
for(int i= 0;i<sp.size;i++){
for(int j= 0;j<order;j++,k++){
pers[k]= new Permutation(*(sp.pers[i]),j);
}
}
}
/*:7*/
#line 15 "./permutation.cweb"
;
/*8:*/
#line 119 "./permutation.cweb"
PermutationSet::~PermutationSet()
{
for(int i= 0;i<size;i++)
if(pers[i])
delete pers[i];
delete[]pers;
for(int i= 0;i<size;i++)
if(pers[i])
delete pers[i];
delete[]pers;
}
/*:8*/
#line 16 "./permutation.cweb"
;
/*9:*/
#line 129 "./permutation.cweb"
vector<const Permutation*> PermutationSet::getPreserving(const IntSequence&s)const
{
TL_RAISE_IF(s.size()!=order,
"Wrong sequence length in PermutationSet::getPreserving");
vector<const Permutation*> res;
IntSequence tmp(s.size());
for(int i= 0;i<size;i++){
pers[i]->apply(s,tmp);
if(s==tmp){
res.push_back(pers[i]);
}
}
return res;
TL_RAISE_IF(s.size()!=order,
"Wrong sequence length in PermutationSet::getPreserving");
vector<const Permutation*> res;
IntSequence tmp(s.size());
for(int i= 0;i<size;i++){
pers[i]->apply(s,tmp);
if(s==tmp){
res.push_back(pers[i]);
}
}
return res;
}
/*:9*/
#line 17 "./permutation.cweb"
;
/*10:*/
#line 148 "./permutation.cweb"
PermutationBundle::PermutationBundle(int nmax)
{
nmax= max(nmax,1);
generateUpTo(nmax);
nmax= max(nmax,1);
generateUpTo(nmax);
}
/*:10*/
#line 18 "./permutation.cweb"
;
/*11:*/
#line 156 "./permutation.cweb"
PermutationBundle::~PermutationBundle()
{
for(unsigned int i= 0;i<bundle.size();i++)
delete bundle[i];
for(unsigned int i= 0;i<bundle.size();i++)
delete bundle[i];
}
/*:11*/
#line 19 "./permutation.cweb"
;
/*12:*/
#line 164 "./permutation.cweb"
const PermutationSet&PermutationBundle::get(int n)const
{
if(n> (int)(bundle.size())||n<1){
TL_RAISE("Permutation set not found in PermutationSet::get");
return*(bundle[0]);
}else{
return*(bundle[n-1]);
}
if(n> (int)(bundle.size())||n<1){
TL_RAISE("Permutation set not found in PermutationSet::get");
return*(bundle[0]);
}else{
return*(bundle[n-1]);
}
}
/*:12*/
#line 20 "./permutation.cweb"
;
/*13:*/
#line 176 "./permutation.cweb"
void PermutationBundle::generateUpTo(int nmax)
{
if(bundle.size()==0)
bundle.push_back(new PermutationSet());
int curmax= bundle.size();
for(int n= curmax+1;n<=nmax;n++){
bundle.push_back(new PermutationSet(*(bundle.back()),n));
}
if(bundle.size()==0)
bundle.push_back(new PermutationSet());
int curmax= bundle.size();
for(int n= curmax+1;n<=nmax;n++){
bundle.push_back(new PermutationSet(*(bundle.back()),n));
}
}
/*:13*/
#line 21 "./permutation.cweb"
;

View File

@ -1,5 +1,4 @@
/*1:*/
#line 38 "./permutation.hweb"
#ifndef PERMUTATION_H
#define PERMUTATION_H
@ -10,7 +9,6 @@
#include <vector>
/*2:*/
#line 65 "./permutation.hweb"
class Permutation{
protected:
@ -52,10 +50,8 @@ void computeSortingMap(const IntSequence&s);
/*:2*/
#line 47 "./permutation.hweb"
;
/*3:*/
#line 117 "./permutation.hweb"
class PermutationSet{
int order;
@ -74,10 +70,8 @@ vector<const Permutation*> getPreserving(const IntSequence&s)const;
/*:3*/
#line 48 "./permutation.hweb"
;
/*4:*/
#line 137 "./permutation.hweb"
class PermutationBundle{
vector<PermutationSet*> bundle;
@ -89,7 +83,6 @@ void generateUpTo(int nmax);
};
/*:4*/
#line 49 "./permutation.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 5 "./ps_tensor.cweb"
#include "ps_tensor.h"
#include "fs_tensor.h"
@ -8,385 +7,347 @@
#include "stack_container.h"
/*2:*/
#line 38 "./ps_tensor.cweb"
UPSTensor::fill_method UPSTensor::decideFillMethod(const FSSparseTensor&t)
{
if(t.getFillFactor()> 0.08)
return first;
else
return second;
if(t.getFillFactor()> 0.08)
return first;
else
return second;
}
/*:2*/
#line 12 "./ps_tensor.cweb"
;
/*3:*/
#line 49 "./ps_tensor.cweb"
UPSTensor::UPSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const PerTensorDimens&ptd)
:UTensor(along_col,ptd.getNVX(),
t.nrows(),ptd.calcUnfoldMaxOffset(),ptd.dimen()),
tdims(ptd)
const IntSequence&coor,const PerTensorDimens&ptd)
:UTensor(along_col,ptd.getNVX(),
t.nrows(),ptd.calcUnfoldMaxOffset(),ptd.dimen()),
tdims(ptd)
{
TL_RAISE_IF(coor.size()!=t.dimen(),
"Wrong coordinates length of stacks for UPSTensor slicing constructor");
TL_RAISE_IF(ss.sum()!=t.nvar(),
"Wrong length of stacks for UPSTensor slicing constructor");
if(first==decideFillMethod(t))
fillFromSparseOne(t,ss,coor);
else
fillFromSparseTwo(t,ss,coor);
TL_RAISE_IF(coor.size()!=t.dimen(),
"Wrong coordinates length of stacks for UPSTensor slicing constructor");
TL_RAISE_IF(ss.sum()!=t.nvar(),
"Wrong length of stacks for UPSTensor slicing constructor");
if(first==decideFillMethod(t))
fillFromSparseOne(t,ss,coor);
else
fillFromSparseTwo(t,ss,coor);
}
/*:3*/
#line 13 "./ps_tensor.cweb"
;
/*4:*/
#line 69 "./ps_tensor.cweb"
void UPSTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UPSTensor::increment");
UTensor::increment(v,tdims.getNVX());
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UPSTensor::increment");
UTensor::increment(v,tdims.getNVX());
}
void UPSTensor::decrement(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UPSTensor::decrement");
UTensor::decrement(v,tdims.getNVX());
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in UPSTensor::decrement");
UTensor::decrement(v,tdims.getNVX());
}
/*:4*/
#line 14 "./ps_tensor.cweb"
;
/*5:*/
#line 87 "./ps_tensor.cweb"
FTensor&UPSTensor::fold()const
{
TL_RAISE("Never should come to this place in UPSTensor::fold");
FFSTensor*nothing= new FFSTensor(0,0,0);
return*nothing;
TL_RAISE("Never should come to this place in UPSTensor::fold");
FFSTensor*nothing= new FFSTensor(0,0,0);
return*nothing;
}
/*:5*/
#line 15 "./ps_tensor.cweb"
;
/*6:*/
#line 97 "./ps_tensor.cweb"
int UPSTensor::getOffset(const IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in UPSTensor::getOffset");
return UTensor::getOffset(v,tdims.getNVX());
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in UPSTensor::getOffset");
return UTensor::getOffset(v,tdims.getNVX());
}
/*:6*/
#line 16 "./ps_tensor.cweb"
;
/*7:*/
#line 107 "./ps_tensor.cweb"
void UPSTensor::addTo(FGSTensor&out)const
{
TL_RAISE_IF(out.getDims()!=tdims,
"Tensors have incompatible dimens in UPSTensor::addTo");
for(index in= out.begin();in!=out.end();++in){
IntSequence vtmp(dimen());
tdims.getPer().apply(in.getCoor(),vtmp);
index tin(this,vtmp);
out.addColumn(*this,*tin,*in);
}
TL_RAISE_IF(out.getDims()!=tdims,
"Tensors have incompatible dimens in UPSTensor::addTo");
for(index in= out.begin();in!=out.end();++in){
IntSequence vtmp(dimen());
tdims.getPer().apply(in.getCoor(),vtmp);
index tin(this,vtmp);
out.addColumn(*this,*tin,*in);
}
}
/*:7*/
#line 17 "./ps_tensor.cweb"
;
/*8:*/
#line 143 "./ps_tensor.cweb"
void UPSTensor::addTo(UGSTensor&out)const
{
TL_RAISE_IF(out.getDims()!=tdims,
"Tensors have incompatible dimens in UPSTensor::addTo");
int cols= tailIdentitySize();
int off= tdims.tailIdentity();
IntSequence outrun(out.dimen(),0);
IntSequence outrun_part(outrun,0,out.dimen()-off);
IntSequence nvmax_part(out.getDims().getNVX(),0,out.dimen()-off);
for(int out_col= 0;out_col<out.ncols();out_col+= cols){
IntSequence perrun(out.dimen());
tdims.getPer().apply(outrun,perrun);
index from(this,perrun);
ConstTwoDMatrix subfrom(*this,*from,cols);
TwoDMatrix subout(out,out_col,cols);
subout.add(1,subfrom);
UTensor::increment(outrun_part,nvmax_part);
}
TL_RAISE_IF(out.getDims()!=tdims,
"Tensors have incompatible dimens in UPSTensor::addTo");
int cols= tailIdentitySize();
int off= tdims.tailIdentity();
IntSequence outrun(out.dimen(),0);
IntSequence outrun_part(outrun,0,out.dimen()-off);
IntSequence nvmax_part(out.getDims().getNVX(),0,out.dimen()-off);
for(int out_col= 0;out_col<out.ncols();out_col+= cols){
IntSequence perrun(out.dimen());
tdims.getPer().apply(outrun,perrun);
index from(this,perrun);
ConstTwoDMatrix subfrom(*this,*from,cols);
TwoDMatrix subout(out,out_col,cols);
subout.add(1,subfrom);
UTensor::increment(outrun_part,nvmax_part);
}
}
/*:8*/
#line 18 "./ps_tensor.cweb"
;
/*9:*/
#line 172 "./ps_tensor.cweb"
int UPSTensor::tailIdentitySize()const
{
return tdims.getNVX().mult(dimen()-tdims.tailIdentity(),dimen());
return tdims.getNVX().mult(dimen()-tdims.tailIdentity(),dimen());
}
/*:9*/
#line 19 "./ps_tensor.cweb"
;
/*10:*/
#line 184 "./ps_tensor.cweb"
void UPSTensor::fillFromSparseOne(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor)
const IntSequence&coor)
{
IntSequence cumtmp(ss.size());
cumtmp[0]= 0;
for(int i= 1;i<ss.size();i++)
cumtmp[i]= cumtmp[i-1]+ss[i-1];
IntSequence cum(coor.size());
for(int i= 0;i<coor.size();i++)
cum[i]= cumtmp[coor[i]];
zeros();
for(Tensor::index run= begin();run!=end();++run){
IntSequence c(run.getCoor());
c.add(1,cum);
c.sort();
FSSparseTensor::const_iterator sl= t.getMap().lower_bound(c);
if(sl!=t.getMap().end()){
FSSparseTensor::const_iterator su= t.getMap().upper_bound(c);
for(FSSparseTensor::const_iterator srun= sl;srun!=su;++srun)
get((*srun).second.first,*run)= (*srun).second.second;
}
}
IntSequence cumtmp(ss.size());
cumtmp[0]= 0;
for(int i= 1;i<ss.size();i++)
cumtmp[i]= cumtmp[i-1]+ss[i-1];
IntSequence cum(coor.size());
for(int i= 0;i<coor.size();i++)
cum[i]= cumtmp[coor[i]];
zeros();
for(Tensor::index run= begin();run!=end();++run){
IntSequence c(run.getCoor());
c.add(1,cum);
c.sort();
FSSparseTensor::const_iterator sl= t.getMap().lower_bound(c);
if(sl!=t.getMap().end()){
FSSparseTensor::const_iterator su= t.getMap().upper_bound(c);
for(FSSparseTensor::const_iterator srun= sl;srun!=su;++srun)
get((*srun).second.first,*run)= (*srun).second.second;
}
}
}
/*:10*/
#line 20 "./ps_tensor.cweb"
;
/*11:*/
#line 225 "./ps_tensor.cweb"
void UPSTensor::fillFromSparseTwo(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor)
const IntSequence&coor)
{
IntSequence coor_srt(coor);
coor_srt.sort();
IntSequence cum(ss.size());
cum[0]= 0;
for(int i= 1;i<ss.size();i++)
cum[i]= cum[i-1]+ss[i-1];
IntSequence lb_srt(coor.size());
IntSequence ub_srt(coor.size());
for(int i= 0;i<coor.size();i++){
lb_srt[i]= cum[coor_srt[i]];
ub_srt[i]= cum[coor_srt[i]]+ss[coor_srt[i]]-1;
}
const PermutationSet&pset= tls.pbundle->get(coor.size());
vector<const Permutation*> pp= pset.getPreserving(coor);
Permutation unsort(coor);
zeros();
FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb_srt);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub_srt);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb_srt.lessEq((*run).first)&&(*run).first.lessEq(ub_srt)){
IntSequence c((*run).first);
c.add(-1,lb_srt);
unsort.apply(c);
for(unsigned int i= 0;i<pp.size();i++){
IntSequence cp(coor.size());
pp[i]->apply(c,cp);
Tensor::index ind(this,cp);
TL_RAISE_IF(*ind<0||*ind>=ncols(),
"Internal error in slicing constructor of UPSTensor");
get((*run).second.first,*ind)= (*run).second.second;
}
}
}
IntSequence coor_srt(coor);
coor_srt.sort();
IntSequence cum(ss.size());
cum[0]= 0;
for(int i= 1;i<ss.size();i++)
cum[i]= cum[i-1]+ss[i-1];
IntSequence lb_srt(coor.size());
IntSequence ub_srt(coor.size());
for(int i= 0;i<coor.size();i++){
lb_srt[i]= cum[coor_srt[i]];
ub_srt[i]= cum[coor_srt[i]]+ss[coor_srt[i]]-1;
}
const PermutationSet&pset= tls.pbundle->get(coor.size());
vector<const Permutation*> pp= pset.getPreserving(coor);
Permutation unsort(coor);
zeros();
FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb_srt);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub_srt);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb_srt.lessEq((*run).first)&&(*run).first.lessEq(ub_srt)){
IntSequence c((*run).first);
c.add(-1,lb_srt);
unsort.apply(c);
for(unsigned int i= 0;i<pp.size();i++){
IntSequence cp(coor.size());
pp[i]->apply(c,cp);
Tensor::index ind(this,cp);
TL_RAISE_IF(*ind<0||*ind>=ncols(),
"Internal error in slicing constructor of UPSTensor");
get((*run).second.first,*ind)= (*run).second.second;
}
}
}
}
/*:11*/
#line 21 "./ps_tensor.cweb"
;
/*12:*/
#line 270 "./ps_tensor.cweb"
void PerTensorDimens2::setDimensionSizes()
{
const IntSequence&nvs= getNVS();
for(int i= 0;i<numSyms();i++){
TensorDimens td(syms[i],nvs);
ds[i]= td.calcFoldMaxOffset();
}
const IntSequence&nvs= getNVS();
for(int i= 0;i<numSyms();i++){
TensorDimens td(syms[i],nvs);
ds[i]= td.calcFoldMaxOffset();
}
}
/*:12*/
#line 22 "./ps_tensor.cweb"
;
/*13:*/
#line 287 "./ps_tensor.cweb"
int PerTensorDimens2::calcOffset(const IntSequence&coor)const
{
TL_RAISE_IF(coor.size()!=dimen(),
"Wrong length of coordinates in PerTensorDimens2::calcOffset");
IntSequence cc(coor);
int ret= 0;
int off= 0;
for(int i= 0;i<numSyms();i++){
TensorDimens td(syms[i],getNVS());
IntSequence c(cc,off,off+syms[i].dimen());
int a= td.calcFoldOffset(c);
ret= ret*ds[i]+a;
off+= syms[i].dimen();
}
return ret;
TL_RAISE_IF(coor.size()!=dimen(),
"Wrong length of coordinates in PerTensorDimens2::calcOffset");
IntSequence cc(coor);
int ret= 0;
int off= 0;
for(int i= 0;i<numSyms();i++){
TensorDimens td(syms[i],getNVS());
IntSequence c(cc,off,off+syms[i].dimen());
int a= td.calcFoldOffset(c);
ret= ret*ds[i]+a;
off+= syms[i].dimen();
}
return ret;
}
/*:13*/
#line 23 "./ps_tensor.cweb"
;
/*14:*/
#line 306 "./ps_tensor.cweb"
void PerTensorDimens2::print()const
{
printf("nvmax: ");nvmax.print();
printf("per: ");per.print();
printf("syms: ");syms.print();
printf("dims: ");ds.print();
printf("nvmax: ");nvmax.print();
printf("per: ");per.print();
printf("syms: ");syms.print();
printf("dims: ");ds.print();
}
/*:14*/
#line 24 "./ps_tensor.cweb"
;
/*15:*/
#line 320 "./ps_tensor.cweb"
void FPSTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong length of coordinates in FPSTensor::increment");
UTensor::increment(v,tdims.getNVX());
int off= 0;
for(int i= 0;i<tdims.numSyms();i++){
IntSequence c(v,off,off+tdims.getSym(i).dimen());
c.pmonotone(tdims.getSym(i));
off+= tdims.getSym(i).dimen();
}
TL_RAISE_IF(v.size()!=dimen(),
"Wrong length of coordinates in FPSTensor::increment");
UTensor::increment(v,tdims.getNVX());
int off= 0;
for(int i= 0;i<tdims.numSyms();i++){
IntSequence c(v,off,off+tdims.getSym(i).dimen());
c.pmonotone(tdims.getSym(i));
off+= tdims.getSym(i).dimen();
}
}
/*:15*/
#line 25 "./ps_tensor.cweb"
;
/*16:*/
#line 336 "./ps_tensor.cweb"
void FPSTensor::decrement(IntSequence&v)const
{
TL_RAISE("FPSTensor::decrement not implemented");
TL_RAISE("FPSTensor::decrement not implemented");
}
/*:16*/
#line 26 "./ps_tensor.cweb"
;
/*17:*/
#line 343 "./ps_tensor.cweb"
UTensor&FPSTensor::unfold()const
{
TL_RAISE("Unfolding of FPSTensor not implemented");
UFSTensor*nothing= new UFSTensor(0,0,0);
return*nothing;
TL_RAISE("Unfolding of FPSTensor not implemented");
UFSTensor*nothing= new UFSTensor(0,0,0);
return*nothing;
}
/*:17*/
#line 27 "./ps_tensor.cweb"
;
/*18:*/
#line 352 "./ps_tensor.cweb"
int FPSTensor::getOffset(const IntSequence&v)const
{
return tdims.calcOffset(v);
return tdims.calcOffset(v);
}
/*:18*/
#line 28 "./ps_tensor.cweb"
;
/*19:*/
#line 364 "./ps_tensor.cweb"
void FPSTensor::addTo(FGSTensor&out)const
{
for(index tar= out.begin();tar!=out.end();++tar){
IntSequence coor(dimen());
tdims.getPer().apply(tar.getCoor(),coor);
index src(this,coor);
out.addColumn(*this,*src,*tar);
}
for(index tar= out.begin();tar!=out.end();++tar){
IntSequence coor(dimen());
tdims.getPer().apply(tar.getCoor(),coor);
index src(this,coor);
out.addColumn(*this,*src,*tar);
}
}
/*:19*/
#line 29 "./ps_tensor.cweb"
;
/*20:*/
#line 395 "./ps_tensor.cweb"
FPSTensor::FPSTensor(const TensorDimens&td,const Equivalence&e,const Permutation&p,
const GSSparseTensor&a,const KronProdAll&kp)
:FTensor(along_col,PerTensorDimens(td,Permutation(e,p)).getNVX(),
a.nrows(),kp.ncols(),td.dimen()),
tdims(td,e,p)
const GSSparseTensor&a,const KronProdAll&kp)
:FTensor(along_col,PerTensorDimens(td,Permutation(e,p)).getNVX(),
a.nrows(),kp.ncols(),td.dimen()),
tdims(td,e,p)
{
zeros();
UGSTensor dummy(0,a.getDims());
for(Tensor::index run= dummy.begin();run!=dummy.end();++run){
Tensor::index fold_ind= dummy.getFirstIndexOf(run);
const IntSequence&c= fold_ind.getCoor();
GSSparseTensor::const_iterator sl= a.getMap().lower_bound(c);
if(sl!=a.getMap().end()){
Vector*row_prod= kp.multRows(run.getCoor());
GSSparseTensor::const_iterator su= a.getMap().upper_bound(c);
for(GSSparseTensor::const_iterator srun= sl;srun!=su;++srun){
Vector out_row((*srun).second.first,*this);
out_row.add((*srun).second.second,*row_prod);
}
delete row_prod;
}
}
zeros();
UGSTensor dummy(0,a.getDims());
for(Tensor::index run= dummy.begin();run!=dummy.end();++run){
Tensor::index fold_ind= dummy.getFirstIndexOf(run);
const IntSequence&c= fold_ind.getCoor();
GSSparseTensor::const_iterator sl= a.getMap().lower_bound(c);
if(sl!=a.getMap().end()){
Vector*row_prod= kp.multRows(run.getCoor());
GSSparseTensor::const_iterator su= a.getMap().upper_bound(c);
for(GSSparseTensor::const_iterator srun= sl;srun!=su;++srun){
Vector out_row((*srun).second.first,*this);
out_row.add((*srun).second.second,*row_prod);
}
delete row_prod;
}
}
}
/*:20*/
#line 30 "./ps_tensor.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 51 "./ps_tensor.hweb"
#ifndef PS_TENSOR_H
@ -13,7 +12,6 @@
#include "sparse_tensor.h"
/*2:*/
#line 73 "./ps_tensor.hweb"
class SortIntSequence:public IntSequence{
public:
@ -23,10 +21,8 @@ SortIntSequence(const IntSequence&s)
/*:2*/
#line 63 "./ps_tensor.hweb"
;
/*3:*/
#line 101 "./ps_tensor.hweb"
class PerTensorDimens:public TensorDimens{
protected:
@ -58,16 +54,13 @@ const Permutation&getPer()const
};
/*:3*/
#line 64 "./ps_tensor.hweb"
;
/*4:*/
#line 154 "./ps_tensor.hweb"
class UPSTensor:public UTensor{
const PerTensorDimens tdims;
public:
/*5:*/
#line 194 "./ps_tensor.hweb"
UPSTensor(const TensorDimens&td,const Equivalence&e,
const ConstTwoDMatrix&a,const KronProdAll&kp)
@ -91,7 +84,6 @@ a.nrows(),kp.ncols(),td.dimen()),tdims(td,Permutation(e,Permutation(p,kp.getPer(
{kp.mult(a,*this);}
/*:5*/
#line 158 "./ps_tensor.hweb"
;
UPSTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const PerTensorDimens&ptd);
@ -117,10 +109,8 @@ const IntSequence&coor);
};
/*:4*/
#line 65 "./ps_tensor.hweb"
;
/*6:*/
#line 231 "./ps_tensor.hweb"
class PerTensorDimens2:public PerTensorDimens{
InducedSymmetries syms;
@ -150,10 +140,8 @@ void setDimensionSizes();
};
/*:6*/
#line 66 "./ps_tensor.hweb"
;
/*7:*/
#line 298 "./ps_tensor.hweb"
template<typename _Ttype> class StackProduct;
@ -161,7 +149,6 @@ class FPSTensor:public FTensor{
const PerTensorDimens2 tdims;
public:
/*8:*/
#line 323 "./ps_tensor.hweb"
FPSTensor(const TensorDimens&td,const Equivalence&e,
const ConstTwoDMatrix&a,const KronProdAll&kp)
@ -191,7 +178,6 @@ FPSTensor(const FPSTensor&ft)
:FTensor(ft),tdims(ft.tdims){}
/*:8*/
#line 304 "./ps_tensor.hweb"
;
void increment(IntSequence&v)const;
@ -203,7 +189,6 @@ void addTo(FGSTensor&out)const;
};
/*:7*/
#line 67 "./ps_tensor.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 5 "./pyramid_prod.cweb"
#include "pyramid_prod.h"
@ -7,59 +6,55 @@
#include "tl_exception.h"
/*2:*/
#line 27 "./pyramid_prod.cweb"
USubTensor::USubTensor(const TensorDimens&bdims,
const TensorDimens&hdims,
const FGSContainer&cont,
const vector<IntSequence> &lst)
:URTensor(lst.size(),hdims.getNVX()[0],hdims.dimen())
const TensorDimens&hdims,
const FGSContainer&cont,
const vector<IntSequence> &lst)
:URTensor(lst.size(),hdims.getNVX()[0],hdims.dimen())
{
TL_RAISE_IF(!hdims.getNVX().isConstant(),
"Tensor has not full symmetry in USubTensor()");
const EquivalenceSet&eset= cont.getEqBundle().get(bdims.dimen());
zeros();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==hdims.dimen()){
Permutation per(*it);
vector<const FGSTensor*> ts=
cont.fetchTensors(bdims.getSym(),*it);
for(int i= 0;i<(int)lst.size();i++){
IntSequence perindex(lst[i].size());
per.apply(lst[i],perindex);
addKronColumn(i,ts,perindex);
}
}
}
TL_RAISE_IF(!hdims.getNVX().isConstant(),
"Tensor has not full symmetry in USubTensor()");
const EquivalenceSet&eset= cont.getEqBundle().get(bdims.dimen());
zeros();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==hdims.dimen()){
Permutation per(*it);
vector<const FGSTensor*> ts=
cont.fetchTensors(bdims.getSym(),*it);
for(int i= 0;i<(int)lst.size();i++){
IntSequence perindex(lst[i].size());
per.apply(lst[i],perindex);
addKronColumn(i,ts,perindex);
}
}
}
}
/*:2*/
#line 11 "./pyramid_prod.cweb"
;
/*3:*/
#line 67 "./pyramid_prod.cweb"
void USubTensor::addKronColumn(int i,const vector<const FGSTensor*> &ts,
const IntSequence&pindex)
const IntSequence&pindex)
{
vector<ConstVector> tmpcols;
int lastdim= 0;
for(unsigned int j= 0;j<ts.size();j++){
IntSequence ind(pindex,lastdim,lastdim+ts[j]->dimen());
lastdim+= ts[j]->dimen();
index in(ts[j],ind);
tmpcols.push_back(ConstVector(*(ts[j]),*in));
}
URSingleTensor kronmult(tmpcols);
Vector coli(*this,i);
coli.add(1.0,kronmult.getData());
vector<ConstVector> tmpcols;
int lastdim= 0;
for(unsigned int j= 0;j<ts.size();j++){
IntSequence ind(pindex,lastdim,lastdim+ts[j]->dimen());
lastdim+= ts[j]->dimen();
index in(ts[j],ind);
tmpcols.push_back(ConstVector(*(ts[j]),*in));
}
URSingleTensor kronmult(tmpcols);
Vector coli(*this,i);
coli.add(1.0,kronmult.getData());
}
/*:3*/
#line 12 "./pyramid_prod.cweb"
;

View File

@ -1,5 +1,4 @@
/*1:*/
#line 48 "./pyramid_prod.hweb"
#ifndef PYRAMID_PROD_H
#define PYRAMID_PROD_H
@ -14,7 +13,6 @@
using namespace std;
/*2:*/
#line 71 "./pyramid_prod.hweb"
class USubTensor:public URTensor{
public:
@ -25,7 +23,6 @@ const IntSequence&pindex);
};
/*:2*/
#line 61 "./pyramid_prod.hweb"
;
#endif

View File

@ -1,146 +1,129 @@
/*1:*/
#line 6 "./pyramid_prod2.cweb"
#include "pyramid_prod2.h"
#include "rfs_tensor.h"
/*2:*/
#line 21 "./pyramid_prod2.cweb"
IrregTensorHeader::IrregTensorHeader(const StackProduct<FGSTensor> &sp,
const IntSequence&c)
:nv(sp.getAllSize()),
unit_flag(sp.dimen()),
cols(new Vector*[sp.dimen()]),
end_seq(sp.dimen())
const IntSequence&c)
:nv(sp.getAllSize()),
unit_flag(sp.dimen()),
cols(new Vector*[sp.dimen()]),
end_seq(sp.dimen())
{
sp.createPackedColumns(c,cols,unit_flag);
for(int i= 0;i<sp.dimen();i++){
end_seq[i]= cols[i]->length();
if(unit_flag[i]!=-1)
end_seq[i]= unit_flag[i]+1;
}
sp.createPackedColumns(c,cols,unit_flag);
for(int i= 0;i<sp.dimen();i++){
end_seq[i]= cols[i]->length();
if(unit_flag[i]!=-1)
end_seq[i]= unit_flag[i]+1;
}
}
/*:2*/
#line 10 "./pyramid_prod2.cweb"
;
/*3:*/
#line 42 "./pyramid_prod2.cweb"
void IrregTensorHeader::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong size of coordinates in IrregTensorHeader::increment");
if(v.size()==0)
return;
int i= v.size()-1;
/*4:*/
#line 63 "./pyramid_prod2.cweb"
v[i]++;
if(unit_flag[i]!=-1&&v[i]==cols[i]->length()-1)
v[i]= unit_flag[i];
/*:4*/
#line 51 "./pyramid_prod2.cweb"
;
while(i> 0&&v[i]==end_seq[i]){
v[i]= 0;
i--;
/*4:*/
#line 63 "./pyramid_prod2.cweb"
v[i]++;
if(unit_flag[i]!=-1&&v[i]==cols[i]->length()-1)
v[i]= unit_flag[i];
/*:4*/
#line 55 "./pyramid_prod2.cweb"
;
}
TL_RAISE_IF(v.size()!=dimen(),
"Wrong size of coordinates in IrregTensorHeader::increment");
if(v.size()==0)
return;
int i= v.size()-1;
/*4:*/
v[i]++;
if(unit_flag[i]!=-1&&v[i]==cols[i]->length()-1)
v[i]= unit_flag[i];
/*:4*/
;
while(i> 0&&v[i]==end_seq[i]){
v[i]= 0;
i--;
/*4:*/
v[i]++;
if(unit_flag[i]!=-1&&v[i]==cols[i]->length()-1)
v[i]= unit_flag[i];
/*:4*/
;
}
}
/*:3*/
#line 11 "./pyramid_prod2.cweb"
;
/*5:*/
#line 70 "./pyramid_prod2.cweb"
IrregTensorHeader::~IrregTensorHeader()
{
for(int i= 0;i<dimen();i++)
delete cols[i];
delete[]cols;
for(int i= 0;i<dimen();i++)
delete cols[i];
delete[]cols;
}
/*:5*/
#line 12 "./pyramid_prod2.cweb"
;
/*6:*/
#line 79 "./pyramid_prod2.cweb"
int IrregTensorHeader::calcMaxOffset()const
{
int res= 1;
for(int i= 0;i<dimen();i++)
res*= cols[i]->length();
return res;
int res= 1;
for(int i= 0;i<dimen();i++)
res*= cols[i]->length();
return res;
}
/*:6*/
#line 13 "./pyramid_prod2.cweb"
;
/*7:*/
#line 92 "./pyramid_prod2.cweb"
IrregTensor::IrregTensor(const IrregTensorHeader&h)
:Tensor(along_row,IntSequence(h.dimen(),0),h.end_seq,
h.calcMaxOffset(),1,h.dimen()),
header(h)
h.calcMaxOffset(),1,h.dimen()),
header(h)
{
if(header.dimen()==1){
getData()= *(header.cols[0]);
return;
}
Vector*last= new Vector(*(header.cols[header.dimen()-1]));
for(int i= header.dimen()-2;i> 0;i--){
Vector*newlast= new Vector(last->length()*header.cols[i]->length());
KronProd::kronMult(ConstVector(*(header.cols[i])),
ConstVector(*last),*newlast);
delete last;
last= newlast;
}
KronProd::kronMult(ConstVector(*(header.cols[0])),
ConstVector(*last),getData());
delete last;
if(header.dimen()==1){
getData()= *(header.cols[0]);
return;
}
Vector*last= new Vector(*(header.cols[header.dimen()-1]));
for(int i= header.dimen()-2;i> 0;i--){
Vector*newlast= new Vector(last->length()*header.cols[i]->length());
KronProd::kronMult(ConstVector(*(header.cols[i])),
ConstVector(*last),*newlast);
delete last;
last= newlast;
}
KronProd::kronMult(ConstVector(*(header.cols[0])),
ConstVector(*last),getData());
delete last;
}
/*:7*/
#line 14 "./pyramid_prod2.cweb"
;
/*8:*/
#line 117 "./pyramid_prod2.cweb"
void IrregTensor::addTo(FRSingleTensor&out)const
{
for(index it= begin();it!=end();++it){
IntSequence tmp(it.getCoor());
tmp.sort();
Tensor::index ind(&out,tmp);
out.get(*ind,0)+= get(*it,0);
}
for(index it= begin();it!=end();++it){
IntSequence tmp(it.getCoor());
tmp.sort();
Tensor::index ind(&out,tmp);
out.get(*ind,0)+= get(*it,0);
}
}
/*:8*/
#line 15 "./pyramid_prod2.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 69 "./pyramid_prod2.hweb"
#ifndef PYRAMID_PROD2_H
#define PYRAMID_PROD2_H
@ -13,7 +12,6 @@
#include "Vector.h"
/*2:*/
#line 101 "./pyramid_prod2.hweb"
class IrregTensor;
class IrregTensorHeader{
@ -35,10 +33,8 @@ IrregTensorHeader(const IrregTensorHeader&);
/*:2*/
#line 81 "./pyramid_prod2.hweb"
;
/*3:*/
#line 137 "./pyramid_prod2.hweb"
class IrregTensor:public Tensor{
const IrregTensorHeader&header;
@ -54,7 +50,6 @@ int getOffset(const IntSequence&v)const
};
/*:3*/
#line 82 "./pyramid_prod2.hweb"
;
#endif

View File

@ -1,219 +1,194 @@
/*1:*/
#line 6 "./rfs_tensor.cweb"
#include "rfs_tensor.h"
#include "kron_prod.h"
#include "tl_exception.h"
/*2:*/
#line 29 "./rfs_tensor.cweb"
FRTensor::FRTensor(const URTensor&ut)
:FTensor(along_row,IntSequence(ut.dimen(),ut.nvar()),
FFSTensor::calcMaxOffset(ut.nvar(),ut.dimen()),ut.ncols(),
ut.dimen()),
nv(ut.nvar())
FFSTensor::calcMaxOffset(ut.nvar(),ut.dimen()),ut.ncols(),
ut.dimen()),
nv(ut.nvar())
{
zeros();
for(index in= ut.begin();in!=ut.end();++in){
IntSequence vtmp(in.getCoor());
vtmp.sort();
index tar(this,vtmp);
addRow(ut,*in,*tar);
}
zeros();
for(index in= ut.begin();in!=ut.end();++in){
IntSequence vtmp(in.getCoor());
vtmp.sort();
index tar(this,vtmp);
addRow(ut,*in,*tar);
}
}
/*:2*/
#line 11 "./rfs_tensor.cweb"
;
/*3:*/
#line 46 "./rfs_tensor.cweb"
UTensor&FRTensor::unfold()const
{
return*(new URTensor(*this));
return*(new URTensor(*this));
}
/*:3*/
#line 12 "./rfs_tensor.cweb"
;
/*4:*/
#line 54 "./rfs_tensor.cweb"
void FRTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FRTensor::increment");
UTensor::increment(v,nv);
v.monotone();
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FRTensor::increment");
UTensor::increment(v,nv);
v.monotone();
}
/*:4*/
#line 13 "./rfs_tensor.cweb"
;
/*5:*/
#line 66 "./rfs_tensor.cweb"
void FRTensor::decrement(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FRTensor::decrement");
FTensor::decrement(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in FRTensor::decrement");
FTensor::decrement(v,nv);
}
/*:5*/
#line 14 "./rfs_tensor.cweb"
;
/*6:*/
#line 81 "./rfs_tensor.cweb"
URTensor::URTensor(const FRTensor&ft)
:UTensor(along_row,IntSequence(ft.dimen(),ft.nvar()),
UFSTensor::calcMaxOffset(ft.nvar(),ft.dimen()),ft.ncols(),
ft.dimen()),
nv(ft.nvar())
UFSTensor::calcMaxOffset(ft.nvar(),ft.dimen()),ft.ncols(),
ft.dimen()),
nv(ft.nvar())
{
zeros();
for(index src= ft.begin();src!=ft.end();++src){
index in(this,src.getCoor());
copyRow(ft,*src,*in);
}
zeros();
for(index src= ft.begin();src!=ft.end();++src){
index in(this,src.getCoor());
copyRow(ft,*src,*in);
}
}
/*:6*/
#line 15 "./rfs_tensor.cweb"
;
/*7:*/
#line 96 "./rfs_tensor.cweb"
FTensor&URTensor::fold()const
{
return*(new FRTensor(*this));
return*(new FRTensor(*this));
}
/*:7*/
#line 16 "./rfs_tensor.cweb"
;
/*8:*/
#line 103 "./rfs_tensor.cweb"
void URTensor::increment(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in URTensor::increment");
UTensor::increment(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in URTensor::increment");
UTensor::increment(v,nv);
}
void URTensor::decrement(IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in URTensor::decrement");
UTensor::decrement(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input/output vector size in URTensor::decrement");
UTensor::decrement(v,nv);
}
/*:8*/
#line 17 "./rfs_tensor.cweb"
;
/*9:*/
#line 121 "./rfs_tensor.cweb"
int URTensor::getOffset(const IntSequence&v)const
{
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in URTensor::getOffset");
return UTensor::getOffset(v,nv);
TL_RAISE_IF(v.size()!=dimen(),
"Wrong input vector size in URTensor::getOffset");
return UTensor::getOffset(v,nv);
}
/*:9*/
#line 18 "./rfs_tensor.cweb"
;
/*10:*/
#line 133 "./rfs_tensor.cweb"
URSingleTensor::URSingleTensor(const vector<ConstVector> &cols)
:URTensor(1,cols[0].length(),cols.size())
{
if(dimen()==1){
getData()= cols[0];
return;
}
Vector*last= new Vector(cols[cols.size()-1]);
for(int i= cols.size()-2;i> 0;i--){
Vector*newlast= new Vector(Tensor::power(nvar(),cols.size()-i));
KronProd::kronMult(cols[i],ConstVector(*last),*newlast);
delete last;
last= newlast;
}
KronProd::kronMult(cols[0],ConstVector(*last),getData());
delete last;
if(dimen()==1){
getData()= cols[0];
return;
}
Vector*last= new Vector(cols[cols.size()-1]);
for(int i= cols.size()-2;i> 0;i--){
Vector*newlast= new Vector(Tensor::power(nvar(),cols.size()-i));
KronProd::kronMult(cols[i],ConstVector(*last),*newlast);
delete last;
last= newlast;
}
KronProd::kronMult(cols[0],ConstVector(*last),getData());
delete last;
}
/*:10*/
#line 19 "./rfs_tensor.cweb"
;
/*11:*/
#line 156 "./rfs_tensor.cweb"
URSingleTensor::URSingleTensor(const ConstVector&v,int d)
:URTensor(1,v.length(),d)
{
if(d==1){
getData()= v;
return;
}
Vector*last= new Vector(v);
for(int i= d-2;i> 0;i--){
Vector*newlast= new Vector(last->length()*v.length());
KronProd::kronMult(v,ConstVector(*last),*newlast);
delete last;
last= newlast;
}
KronProd::kronMult(v,ConstVector(*last),getData());
delete last;
if(d==1){
getData()= v;
return;
}
Vector*last= new Vector(v);
for(int i= d-2;i> 0;i--){
Vector*newlast= new Vector(last->length()*v.length());
KronProd::kronMult(v,ConstVector(*last),*newlast);
delete last;
last= newlast;
}
KronProd::kronMult(v,ConstVector(*last),getData());
delete last;
}
/*:11*/
#line 20 "./rfs_tensor.cweb"
;
/*12:*/
#line 179 "./rfs_tensor.cweb"
FTensor&URSingleTensor::fold()const
{
return*(new FRSingleTensor(*this));
return*(new FRSingleTensor(*this));
}
/*:12*/
#line 21 "./rfs_tensor.cweb"
;
/*13:*/
#line 191 "./rfs_tensor.cweb"
FRSingleTensor::FRSingleTensor(const URSingleTensor&ut)
:FRTensor(1,ut.nvar(),ut.dimen())
{
zeros();
for(index in= ut.begin();in!=ut.end();++in){
IntSequence vtmp(in.getCoor());
vtmp.sort();
index tar(this,vtmp);
get(*tar,0)+= ut.get(*in,0);
}
zeros();
for(index in= ut.begin();in!=ut.end();++in){
IntSequence vtmp(in.getCoor());
vtmp.sort();
index tar(this,vtmp);
get(*tar,0)+= ut.get(*in,0);
}
}
/*:13*/
#line 22 "./rfs_tensor.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 37 "./rfs_tensor.hweb"
#ifndef RFS_TENSOR_H
#define RFS_TENSOR_H
@ -9,14 +8,12 @@
#include "symmetry.h"
/*2:*/
#line 53 "./rfs_tensor.hweb"
class FRTensor;
class URTensor:public UTensor{
int nv;
public:
/*3:*/
#line 73 "./rfs_tensor.hweb"
URTensor(int c,int nvar,int d)
:UTensor(along_row,IntSequence(d,nvar),
@ -26,7 +23,6 @@ URTensor(const URTensor&ut)
URTensor(const FRTensor&ft);
/*:3*/
#line 58 "./rfs_tensor.hweb"
;
virtual~URTensor(){}
@ -42,16 +38,13 @@ Symmetry getSym()const
};
/*:2*/
#line 45 "./rfs_tensor.hweb"
;
/*4:*/
#line 82 "./rfs_tensor.hweb"
class FRTensor:public FTensor{
int nv;
public:
/*5:*/
#line 102 "./rfs_tensor.hweb"
FRTensor(int c,int nvar,int d)
:FTensor(along_row,IntSequence(d,nvar),
@ -61,7 +54,6 @@ FRTensor(const FRTensor&ft)
FRTensor(const URTensor&ut);
/*:5*/
#line 86 "./rfs_tensor.hweb"
;
virtual~FRTensor(){}
@ -78,10 +70,8 @@ Symmetry getSym()const
};
/*:4*/
#line 46 "./rfs_tensor.hweb"
;
/*6:*/
#line 117 "./rfs_tensor.hweb"
class URSingleTensor:public URTensor{
public:
@ -96,10 +86,8 @@ FTensor&fold()const;
};
/*:6*/
#line 47 "./rfs_tensor.hweb"
;
/*7:*/
#line 136 "./rfs_tensor.hweb"
class FRSingleTensor:public FRTensor{
public:
@ -113,7 +101,6 @@ virtual~FRSingleTensor(){}
/*:7*/
#line 48 "./rfs_tensor.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./sparse_tensor.cweb"
#include "sparse_tensor.h"
#include "fs_tensor.h"
@ -8,129 +7,116 @@
#include <cmath>
/*2:*/
#line 30 "./sparse_tensor.cweb"
void SparseTensor::insert(const IntSequence&key,int r,double c)
{
TL_RAISE_IF(r<0||r>=nr,
"Row number out of dimension of tensor in SparseTensor::insert");
TL_RAISE_IF(key.size()!=dimen(),
"Wrong length of key in SparseTensor::insert");
TL_RAISE_IF(!std::isfinite(c),
"Insertion of non-finite value in SparseTensor::insert");
iterator first_pos= m.lower_bound(key);
/*3:*/
#line 50 "./sparse_tensor.cweb"
iterator last_pos= m.upper_bound(key);
for(iterator it= first_pos;it!=last_pos;++it)
if((*it).second.first==r){
TL_RAISE("Duplicate <key, r> insertion in SparseTensor::insert");
return;
}
/*:3*/
#line 41 "./sparse_tensor.cweb"
;
m.insert(first_pos,Map::value_type(key,Item(r,c)));
if(first_nz_row> r)
first_nz_row= r;
if(last_nz_row<r)
last_nz_row= r;
TL_RAISE_IF(r<0||r>=nr,
"Row number out of dimension of tensor in SparseTensor::insert");
TL_RAISE_IF(key.size()!=dimen(),
"Wrong length of key in SparseTensor::insert");
TL_RAISE_IF(!std::isfinite(c),
"Insertion of non-finite value in SparseTensor::insert");
iterator first_pos= m.lower_bound(key);
/*3:*/
iterator last_pos= m.upper_bound(key);
for(iterator it= first_pos;it!=last_pos;++it)
if((*it).second.first==r){
TL_RAISE("Duplicate <key, r> insertion in SparseTensor::insert");
return;
}
/*:3*/
;
m.insert(first_pos,Map::value_type(key,Item(r,c)));
if(first_nz_row> r)
first_nz_row= r;
if(last_nz_row<r)
last_nz_row= r;
}
/*:2*/
#line 13 "./sparse_tensor.cweb"
;
/*4:*/
#line 59 "./sparse_tensor.cweb"
bool SparseTensor::isFinite()const
{
bool res= true;
const_iterator run= m.begin();
while(res&&run!=m.end()){
if(!std::isfinite((*run).second.second))
res= false;
++run;
}
return res;
bool res= true;
const_iterator run= m.begin();
while(res&&run!=m.end()){
if(!std::isfinite((*run).second.second))
res= false;
++run;
}
return res;
}
/*:4*/
#line 14 "./sparse_tensor.cweb"
;
/*5:*/
#line 75 "./sparse_tensor.cweb"
double SparseTensor::getFoldIndexFillFactor()const
{
int cnt= 0;
const_iterator start_col= m.begin();
while(start_col!=m.end()){
cnt++;
const IntSequence&key= (*start_col).first;
start_col= m.upper_bound(key);
}
return((double)cnt)/ncols();
int cnt= 0;
const_iterator start_col= m.begin();
while(start_col!=m.end()){
cnt++;
const IntSequence&key= (*start_col).first;
start_col= m.upper_bound(key);
}
return((double)cnt)/ncols();
}
/*:5*/
#line 15 "./sparse_tensor.cweb"
;
/*6:*/
#line 92 "./sparse_tensor.cweb"
double SparseTensor::getUnfoldIndexFillFactor()const
{
int cnt= 0;
const_iterator start_col= m.begin();
while(start_col!=m.end()){
const IntSequence&key= (*start_col).first;
Symmetry s(key);
cnt+= Tensor::noverseq(s);
start_col= m.upper_bound(key);
}
return((double)cnt)/ncols();
int cnt= 0;
const_iterator start_col= m.begin();
while(start_col!=m.end()){
const IntSequence&key= (*start_col).first;
Symmetry s(key);
cnt+= Tensor::noverseq(s);
start_col= m.upper_bound(key);
}
return((double)cnt)/ncols();
}
/*:6*/
#line 16 "./sparse_tensor.cweb"
;
/*7:*/
#line 110 "./sparse_tensor.cweb"
void SparseTensor::print()const
{
printf("Fill: %3.2f %%\n",100*getFillFactor());
const_iterator start_col= m.begin();
while(start_col!=m.end()){
const IntSequence&key= (*start_col).first;
printf("Column: ");key.print();
const_iterator end_col= m.upper_bound(key);
int cnt= 1;
for(const_iterator run= start_col;run!=end_col;++run,cnt++){
if((cnt/7)*7==cnt)
printf("\n");
printf("%d(%6.2g) ",(*run).second.first,(*run).second.second);
}
printf("\n");
start_col= end_col;
}
printf("Fill: %3.2f %%\n",100*getFillFactor());
const_iterator start_col= m.begin();
while(start_col!=m.end()){
const IntSequence&key= (*start_col).first;
printf("Column: ");key.print();
const_iterator end_col= m.upper_bound(key);
int cnt= 1;
for(const_iterator run= start_col;run!=end_col;++run,cnt++){
if((cnt/7)*7==cnt)
printf("\n");
printf("%d(%6.2g) ",(*run).second.first,(*run).second.second);
}
printf("\n");
start_col= end_col;
}
}
/*:7*/
#line 17 "./sparse_tensor.cweb"
;
/*8:*/
#line 133 "./sparse_tensor.cweb"
FSSparseTensor::FSSparseTensor(int d,int nvar,int r)
:SparseTensor(d,r,FFSTensor::calcMaxOffset(nvar,d)),
@ -138,10 +124,8 @@ nv(nvar),sym(d)
{}
/*:8*/
#line 18 "./sparse_tensor.cweb"
;
/*9:*/
#line 140 "./sparse_tensor.cweb"
FSSparseTensor::FSSparseTensor(const FSSparseTensor&t)
:SparseTensor(t),
@ -149,153 +133,134 @@ nv(t.nvar()),sym(t.sym)
{}
/*:9*/
#line 19 "./sparse_tensor.cweb"
;
/*10:*/
#line 147 "./sparse_tensor.cweb"
void FSSparseTensor::insert(const IntSequence&key,int r,double c)
{
TL_RAISE_IF(!key.isSorted(),
"Key is not sorted in FSSparseTensor::insert");
TL_RAISE_IF(key[key.size()-1]>=nv||key[0]<0,
"Wrong value of the key in FSSparseTensor::insert");
SparseTensor::insert(key,r,c);
TL_RAISE_IF(!key.isSorted(),
"Key is not sorted in FSSparseTensor::insert");
TL_RAISE_IF(key[key.size()-1]>=nv||key[0]<0,
"Wrong value of the key in FSSparseTensor::insert");
SparseTensor::insert(key,r,c);
}
/*:10*/
#line 20 "./sparse_tensor.cweb"
;
/*11:*/
#line 171 "./sparse_tensor.cweb"
void FSSparseTensor::multColumnAndAdd(const Tensor&t,Vector&v)const
{
/*12:*/
#line 195 "./sparse_tensor.cweb"
TL_RAISE_IF(v.length()!=nrows(),
"Wrong size of output vector in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.dimen()!=dimen(),
"Wrong dimension of tensor in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.ncols()!=1,
"The input tensor is not single-column in FSSparseTensor::multColumnAndAdd");
/*:12*/
#line 174 "./sparse_tensor.cweb"
;
for(Tensor::index it= t.begin();it!=t.end();++it){
int ind= *it;
double a= t.get(ind,0);
if(a!=0.0){
IntSequence key(it.getCoor());
key.sort();
/*13:*/
#line 205 "./sparse_tensor.cweb"
TL_RAISE_IF(key[0]<0||key[key.size()-1]>=nv,
"Wrong coordinates of index in FSSparseTensor::multColumnAndAdd");
/*:13*/
#line 181 "./sparse_tensor.cweb"
;
const_iterator first_pos= m.lower_bound(key);
const_iterator last_pos= m.upper_bound(key);
for(const_iterator cit= first_pos;cit!=last_pos;++cit){
int r= (*cit).second.first;
double c= (*cit).second.second;
v[r]+= c*a;
}
}
}
/*12:*/
TL_RAISE_IF(v.length()!=nrows(),
"Wrong size of output vector in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.dimen()!=dimen(),
"Wrong dimension of tensor in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.ncols()!=1,
"The input tensor is not single-column in FSSparseTensor::multColumnAndAdd");
/*:12*/
;
for(Tensor::index it= t.begin();it!=t.end();++it){
int ind= *it;
double a= t.get(ind,0);
if(a!=0.0){
IntSequence key(it.getCoor());
key.sort();
/*13:*/
TL_RAISE_IF(key[0]<0||key[key.size()-1]>=nv,
"Wrong coordinates of index in FSSparseTensor::multColumnAndAdd");
/*:13*/
;
const_iterator first_pos= m.lower_bound(key);
const_iterator last_pos= m.upper_bound(key);
for(const_iterator cit= first_pos;cit!=last_pos;++cit){
int r= (*cit).second.first;
double c= (*cit).second.second;
v[r]+= c*a;
}
}
}
}
/*:11*/
#line 21 "./sparse_tensor.cweb"
;
/*14:*/
#line 210 "./sparse_tensor.cweb"
void FSSparseTensor::print()const
{
printf("FS Sparse tensor: dim=%d, nv=%d, (%dx%d)\n",dim,nv,nr,nc);
SparseTensor::print();
printf("FS Sparse tensor: dim=%d, nv=%d, (%dx%d)\n",dim,nv,nr,nc);
SparseTensor::print();
}
/*:14*/
#line 22 "./sparse_tensor.cweb"
;
/*15:*/
#line 218 "./sparse_tensor.cweb"
GSSparseTensor::GSSparseTensor(const FSSparseTensor&t,const IntSequence&ss,
const IntSequence&coor,const TensorDimens&td)
:SparseTensor(td.dimen(),t.nrows(),td.calcFoldMaxOffset()),
tdims(td)
const IntSequence&coor,const TensorDimens&td)
:SparseTensor(td.dimen(),t.nrows(),td.calcFoldMaxOffset()),
tdims(td)
{
/*16:*/
#line 241 "./sparse_tensor.cweb"
IntSequence s_offsets(ss.size(),0);
for(int i= 1;i<ss.size();i++)
s_offsets[i]= s_offsets[i-1]+ss[i-1];
IntSequence lb(coor.size());
IntSequence ub(coor.size());
for(int i= 0;i<coor.size();i++){
lb[i]= s_offsets[coor[i]];
ub[i]= s_offsets[coor[i]]+ss[coor[i]]-1;
}
/*:16*/
#line 224 "./sparse_tensor.cweb"
;
FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb.lessEq((*run).first)&&(*run).first.lessEq(ub)){
IntSequence c((*run).first);
c.add(-1,lb);
insert(c,(*run).second.first,(*run).second.second);
}
}
/*16:*/
IntSequence s_offsets(ss.size(),0);
for(int i= 1;i<ss.size();i++)
s_offsets[i]= s_offsets[i-1]+ss[i-1];
IntSequence lb(coor.size());
IntSequence ub(coor.size());
for(int i= 0;i<coor.size();i++){
lb[i]= s_offsets[coor[i]];
ub[i]= s_offsets[coor[i]]+ss[coor[i]]-1;
}
/*:16*/
;
FSSparseTensor::const_iterator lbi= t.getMap().lower_bound(lb);
FSSparseTensor::const_iterator ubi= t.getMap().upper_bound(ub);
for(FSSparseTensor::const_iterator run= lbi;run!=ubi;++run){
if(lb.lessEq((*run).first)&&(*run).first.lessEq(ub)){
IntSequence c((*run).first);
c.add(-1,lb);
insert(c,(*run).second.first,(*run).second.second);
}
}
}
/*:15*/
#line 23 "./sparse_tensor.cweb"
;
/*17:*/
#line 255 "./sparse_tensor.cweb"
void GSSparseTensor::insert(const IntSequence&s,int r,double c)
{
TL_RAISE_IF(!s.less(tdims.getNVX()),
"Wrong coordinates of index in GSSparseTensor::insert");
SparseTensor::insert(s,r,c);
TL_RAISE_IF(!s.less(tdims.getNVX()),
"Wrong coordinates of index in GSSparseTensor::insert");
SparseTensor::insert(s,r,c);
}
/*:17*/
#line 24 "./sparse_tensor.cweb"
;
/*18:*/
#line 264 "./sparse_tensor.cweb"
void GSSparseTensor::print()const
{
printf("GS Sparse tensor: (%dx%d)\nSymmetry: ",nr,nc);
tdims.getSym().print();
printf("NVS: ");
tdims.getNVS().print();
SparseTensor::print();
printf("GS Sparse tensor: (%dx%d)\nSymmetry: ",nr,nc);
tdims.getSym().print();
printf("NVS: ");
tdims.getNVS().print();
SparseTensor::print();
}
/*:18*/
#line 25 "./sparse_tensor.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 29 "./sparse_tensor.hweb"
#ifndef SPARSE_TENSOR_H
#define SPARSE_TENSOR_H
@ -14,7 +13,6 @@
using namespace std;
/*2:*/
#line 50 "./sparse_tensor.hweb"
struct ltseq{
bool operator()(const IntSequence&s1,const IntSequence&s2)const
@ -22,10 +20,8 @@ bool operator()(const IntSequence&s1,const IntSequence&s2)const
};
/*:2*/
#line 42 "./sparse_tensor.hweb"
;
/*3:*/
#line 60 "./sparse_tensor.hweb"
class SparseTensor{
public:
@ -72,10 +68,8 @@ bool isFinite()const;
}
/*:3*/
#line 43 "./sparse_tensor.hweb"
;
/*4:*/
#line 109 "./sparse_tensor.hweb"
class FSSparseTensor:public SparseTensor{
public:
@ -97,10 +91,8 @@ void print()const;
/*:4*/
#line 44 "./sparse_tensor.hweb"
;
/*5:*/
#line 134 "./sparse_tensor.hweb"
class GSSparseTensor:public SparseTensor{
public:
@ -122,7 +114,6 @@ void print()const;
};
/*:5*/
#line 45 "./sparse_tensor.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./stack_container.cweb"
#include "stack_container.h"
#include "pyramid_prod2.h"
@ -8,562 +7,508 @@
double FoldedStackContainer::fill_threshold= 0.00005;
double UnfoldedStackContainer::fill_threshold= 0.00005;
/*2:*/
#line 59 "./stack_container.cweb"
void FoldedStackContainer::multAndAdd(const FSSparseTensor&t,
FGSTensor&out)const
FGSTensor&out)const
{
TL_RAISE_IF(t.nvar()!=getAllSize(),
"Wrong number of variables of tensor for FoldedStackContainer::multAndAdd");
multAndAddSparse2(t,out);
TL_RAISE_IF(t.nvar()!=getAllSize(),
"Wrong number of variables of tensor for FoldedStackContainer::multAndAdd");
multAndAddSparse2(t,out);
}
/*:2*/
#line 13 "./stack_container.cweb"
;
/*3:*/
#line 73 "./stack_container.cweb"
void FoldedStackContainer::multAndAdd(int dim,const FGSContainer&c,FGSTensor&out)const
{
TL_RAISE_IF(c.num()!=numStacks(),
"Wrong symmetry length of container for FoldedStackContainer::multAndAdd");
THREAD_GROUP gr;
SymmetrySet ss(dim,c.num());
for(symiterator si(ss);!si.isEnd();++si){
if(c.check(*si)){
THREAD*worker= new WorkerFoldMAADense(*this,*si,c,out);
gr.insert(worker);
}
}
gr.run();
TL_RAISE_IF(c.num()!=numStacks(),
"Wrong symmetry length of container for FoldedStackContainer::multAndAdd");
THREAD_GROUP gr;
SymmetrySet ss(dim,c.num());
for(symiterator si(ss);!si.isEnd();++si){
if(c.check(*si)){
THREAD*worker= new WorkerFoldMAADense(*this,*si,c,out);
gr.insert(worker);
}
}
gr.run();
}
/*:3*/
#line 14 "./stack_container.cweb"
;
/*4:*/
#line 93 "./stack_container.cweb"
void WorkerFoldMAADense::operator()()
{
Permutation iden(dense_cont.num());
IntSequence coor(sym,iden.getMap());
const FGSTensor*g= dense_cont.get(sym);
cont.multAndAddStacks(coor,*g,out,&out);
Permutation iden(dense_cont.num());
IntSequence coor(sym,iden.getMap());
const FGSTensor*g= dense_cont.get(sym);
cont.multAndAddStacks(coor,*g,out,&out);
}
/*:4*/
#line 15 "./stack_container.cweb"
;
/*5:*/
#line 103 "./stack_container.cweb"
WorkerFoldMAADense::WorkerFoldMAADense(const FoldedStackContainer&container,
const Symmetry&s,
const FGSContainer&dcontainer,
FGSTensor&outten)
:cont(container),sym(s),dense_cont(dcontainer),out(outten)
const Symmetry&s,
const FGSContainer&dcontainer,
FGSTensor&outten)
:cont(container),sym(s),dense_cont(dcontainer),out(outten)
{}
/*:5*/
#line 16 "./stack_container.cweb"
;
/*6:*/
#line 113 "./stack_container.cweb"
void FoldedStackContainer::multAndAddSparse1(const FSSparseTensor&t,
FGSTensor&out)const
FGSTensor&out)const
{
THREAD_GROUP gr;
UFSTensor dummy(0,numStacks(),t.dimen());
for(Tensor::index ui= dummy.begin();ui!=dummy.end();++ui){
THREAD*worker= new WorkerFoldMAASparse1(*this,t,out,ui.getCoor());
gr.insert(worker);
}
gr.run();
THREAD_GROUP gr;
UFSTensor dummy(0,numStacks(),t.dimen());
for(Tensor::index ui= dummy.begin();ui!=dummy.end();++ui){
THREAD*worker= new WorkerFoldMAASparse1(*this,t,out,ui.getCoor());
gr.insert(worker);
}
gr.run();
}
/*:6*/
#line 17 "./stack_container.cweb"
;
/*7:*/
#line 135 "./stack_container.cweb"
void WorkerFoldMAASparse1::operator()()
{
const EquivalenceSet&eset= ebundle.get(out.dimen());
const PermutationSet&pset= tls.pbundle->get(t.dimen());
Permutation iden(t.dimen());
UPSTensor slice(t,cont.getStackSizes(),coor,
PerTensorDimens(cont.getStackSizes(),coor));
for(int iper= 0;iper<pset.getNum();iper++){
const Permutation&per= pset.get(iper);
IntSequence percoor(coor.size());
per.apply(coor,percoor);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==t.dimen()){
StackProduct<FGSTensor> sp(cont,*it,out.getSym());
if(!sp.isZero(percoor)){
KronProdStack<FGSTensor> kp(sp,percoor);
kp.optimizeOrder();
const Permutation&oper= kp.getPer();
if(Permutation(oper,per)==iden){
FPSTensor fps(out.getDims(),*it,slice,kp);
{
SYNCHRO syn(&out,"WorkerUnfoldMAASparse1");
fps.addTo(out);
}
}
}
}
}
}
const EquivalenceSet&eset= ebundle.get(out.dimen());
const PermutationSet&pset= tls.pbundle->get(t.dimen());
Permutation iden(t.dimen());
UPSTensor slice(t,cont.getStackSizes(),coor,
PerTensorDimens(cont.getStackSizes(),coor));
for(int iper= 0;iper<pset.getNum();iper++){
const Permutation&per= pset.get(iper);
IntSequence percoor(coor.size());
per.apply(coor,percoor);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==t.dimen()){
StackProduct<FGSTensor> sp(cont,*it,out.getSym());
if(!sp.isZero(percoor)){
KronProdStack<FGSTensor> kp(sp,percoor);
kp.optimizeOrder();
const Permutation&oper= kp.getPer();
if(Permutation(oper,per)==iden){
FPSTensor fps(out.getDims(),*it,slice,kp);
{
SYNCHRO syn(&out,"WorkerUnfoldMAASparse1");
fps.addTo(out);
}
}
}
}
}
}
}
/*:7*/
#line 18 "./stack_container.cweb"
;
/*8:*/
#line 170 "./stack_container.cweb"
WorkerFoldMAASparse1::WorkerFoldMAASparse1(const FoldedStackContainer&container,
const FSSparseTensor&ten,
FGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c),ebundle(*(tls.ebundle)){}
const FSSparseTensor&ten,
FGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c),ebundle(*(tls.ebundle)){}
/*:8*/
#line 19 "./stack_container.cweb"
;
/*9:*/
#line 183 "./stack_container.cweb"
void FoldedStackContainer::multAndAddSparse2(const FSSparseTensor&t,
FGSTensor&out)const
FGSTensor&out)const
{
THREAD_GROUP gr;
FFSTensor dummy_f(0,numStacks(),t.dimen());
for(Tensor::index fi= dummy_f.begin();fi!=dummy_f.end();++fi){
THREAD*worker= new WorkerFoldMAASparse2(*this,t,out,fi.getCoor());
gr.insert(worker);
}
gr.run();
THREAD_GROUP gr;
FFSTensor dummy_f(0,numStacks(),t.dimen());
for(Tensor::index fi= dummy_f.begin();fi!=dummy_f.end();++fi){
THREAD*worker= new WorkerFoldMAASparse2(*this,t,out,fi.getCoor());
gr.insert(worker);
}
gr.run();
}
/*:9*/
#line 20 "./stack_container.cweb"
;
/*10:*/
#line 207 "./stack_container.cweb"
void WorkerFoldMAASparse2::operator()()
{
GSSparseTensor slice(t,cont.getStackSizes(),coor,
TensorDimens(cont.getStackSizes(),coor));
if(slice.getNumNonZero()){
if(slice.getUnfoldIndexFillFactor()> FoldedStackContainer::fill_threshold){
FGSTensor dense_slice(slice);
int r1= slice.getFirstNonZeroRow();
int r2= slice.getLastNonZeroRow();
FGSTensor dense_slice1(r1,r2-r1+1,dense_slice);
FGSTensor out1(r1,r2-r1+1,out);
cont.multAndAddStacks(coor,dense_slice1,out1,&out);
}else
cont.multAndAddStacks(coor,slice,out,&out);
}
GSSparseTensor slice(t,cont.getStackSizes(),coor,
TensorDimens(cont.getStackSizes(),coor));
if(slice.getNumNonZero()){
if(slice.getUnfoldIndexFillFactor()> FoldedStackContainer::fill_threshold){
FGSTensor dense_slice(slice);
int r1= slice.getFirstNonZeroRow();
int r2= slice.getLastNonZeroRow();
FGSTensor dense_slice1(r1,r2-r1+1,dense_slice);
FGSTensor out1(r1,r2-r1+1,out);
cont.multAndAddStacks(coor,dense_slice1,out1,&out);
}else
cont.multAndAddStacks(coor,slice,out,&out);
}
}
/*:10*/
#line 21 "./stack_container.cweb"
;
/*11:*/
#line 226 "./stack_container.cweb"
WorkerFoldMAASparse2::WorkerFoldMAASparse2(const FoldedStackContainer&container,
const FSSparseTensor&ten,
FGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c)
const FSSparseTensor&ten,
FGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c)
{}
/*:11*/
#line 22 "./stack_container.cweb"
;
/*12:*/
#line 248 "./stack_container.cweb"
void FoldedStackContainer::multAndAddSparse3(const FSSparseTensor&t,
FGSTensor&out)const
FGSTensor&out)const
{
const EquivalenceSet&eset= ebundle.get(out.dimen());
for(Tensor::index run= out.begin();run!=out.end();++run){
Vector outcol(out,*run);
FRSingleTensor sumcol(t.nvar(),t.dimen());
sumcol.zeros();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==t.dimen()){
StackProduct<FGSTensor> sp(*this,*it,out.getSym());
IrregTensorHeader header(sp,run.getCoor());
IrregTensor irten(header);
irten.addTo(sumcol);
}
}
t.multColumnAndAdd(sumcol,outcol);
}
const EquivalenceSet&eset= ebundle.get(out.dimen());
for(Tensor::index run= out.begin();run!=out.end();++run){
Vector outcol(out,*run);
FRSingleTensor sumcol(t.nvar(),t.dimen());
sumcol.zeros();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==t.dimen()){
StackProduct<FGSTensor> sp(*this,*it,out.getSym());
IrregTensorHeader header(sp,run.getCoor());
IrregTensor irten(header);
irten.addTo(sumcol);
}
}
t.multColumnAndAdd(sumcol,outcol);
}
}
/*:12*/
#line 23 "./stack_container.cweb"
;
/*13:*/
#line 278 "./stack_container.cweb"
void FoldedStackContainer::multAndAddSparse4(const FSSparseTensor&t,FGSTensor&out)const
{
THREAD_GROUP gr;
FFSTensor dummy_f(0,numStacks(),t.dimen());
for(Tensor::index fi= dummy_f.begin();fi!=dummy_f.end();++fi){
THREAD*worker= new WorkerFoldMAASparse4(*this,t,out,fi.getCoor());
gr.insert(worker);
}
gr.run();
THREAD_GROUP gr;
FFSTensor dummy_f(0,numStacks(),t.dimen());
for(Tensor::index fi= dummy_f.begin();fi!=dummy_f.end();++fi){
THREAD*worker= new WorkerFoldMAASparse4(*this,t,out,fi.getCoor());
gr.insert(worker);
}
gr.run();
}
/*:13*/
#line 24 "./stack_container.cweb"
;
/*14:*/
#line 294 "./stack_container.cweb"
void WorkerFoldMAASparse4::operator()()
{
GSSparseTensor slice(t,cont.getStackSizes(),coor,
TensorDimens(cont.getStackSizes(),coor));
if(slice.getNumNonZero())
cont.multAndAddStacks(coor,slice,out,&out);
GSSparseTensor slice(t,cont.getStackSizes(),coor,
TensorDimens(cont.getStackSizes(),coor));
if(slice.getNumNonZero())
cont.multAndAddStacks(coor,slice,out,&out);
}
/*:14*/
#line 25 "./stack_container.cweb"
;
/*15:*/
#line 304 "./stack_container.cweb"
WorkerFoldMAASparse4::WorkerFoldMAASparse4(const FoldedStackContainer&container,
const FSSparseTensor&ten,
FGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c)
const FSSparseTensor&ten,
FGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c)
{}
/*:15*/
#line 26 "./stack_container.cweb"
;
/*16:*/
#line 321 "./stack_container.cweb"
void FoldedStackContainer::multAndAddStacks(const IntSequence&coor,
const FGSTensor&g,
FGSTensor&out,const void*ad)const
const FGSTensor&g,
FGSTensor&out,const void*ad)const
{
const EquivalenceSet&eset= ebundle.get(out.dimen());
UGSTensor ug(g);
UFSTensor dummy_u(0,numStacks(),g.dimen());
for(Tensor::index ui= dummy_u.begin();ui!=dummy_u.end();++ui){
IntSequence tmp(ui.getCoor());
tmp.sort();
if(tmp==coor){
Permutation sort_per(ui.getCoor());
sort_per.inverse();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==g.dimen()){
StackProduct<FGSTensor> sp(*this,*it,sort_per,out.getSym());
if(!sp.isZero(coor)){
KronProdStack<FGSTensor> kp(sp,coor);
if(ug.getSym().isFull())
kp.optimizeOrder();
FPSTensor fps(out.getDims(),*it,sort_per,ug,kp);
{
SYNCHRO syn(ad,"multAndAddStacks");
fps.addTo(out);
}
}
}
}
}
}
const EquivalenceSet&eset= ebundle.get(out.dimen());
UGSTensor ug(g);
UFSTensor dummy_u(0,numStacks(),g.dimen());
for(Tensor::index ui= dummy_u.begin();ui!=dummy_u.end();++ui){
IntSequence tmp(ui.getCoor());
tmp.sort();
if(tmp==coor){
Permutation sort_per(ui.getCoor());
sort_per.inverse();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==g.dimen()){
StackProduct<FGSTensor> sp(*this,*it,sort_per,out.getSym());
if(!sp.isZero(coor)){
KronProdStack<FGSTensor> kp(sp,coor);
if(ug.getSym().isFull())
kp.optimizeOrder();
FPSTensor fps(out.getDims(),*it,sort_per,ug,kp);
{
SYNCHRO syn(ad,"multAndAddStacks");
fps.addTo(out);
}
}
}
}
}
}
}
/*:16*/
#line 27 "./stack_container.cweb"
;
/*17:*/
#line 362 "./stack_container.cweb"
void FoldedStackContainer::multAndAddStacks(const IntSequence&coor,
const GSSparseTensor&g,
FGSTensor&out,const void*ad)const
const GSSparseTensor&g,
FGSTensor&out,const void*ad)const
{
const EquivalenceSet&eset= ebundle.get(out.dimen());
UFSTensor dummy_u(0,numStacks(),g.dimen());
for(Tensor::index ui= dummy_u.begin();ui!=dummy_u.end();++ui){
IntSequence tmp(ui.getCoor());
tmp.sort();
if(tmp==coor){
Permutation sort_per(ui.getCoor());
sort_per.inverse();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==g.dimen()){
StackProduct<FGSTensor> sp(*this,*it,sort_per,out.getSym());
if(!sp.isZero(coor)){
KronProdStack<FGSTensor> kp(sp,coor);
FPSTensor fps(out.getDims(),*it,sort_per,g,kp);
{
SYNCHRO syn(ad,"multAndAddStacks");
fps.addTo(out);
}
}
}
}
}
}
const EquivalenceSet&eset= ebundle.get(out.dimen());
UFSTensor dummy_u(0,numStacks(),g.dimen());
for(Tensor::index ui= dummy_u.begin();ui!=dummy_u.end();++ui){
IntSequence tmp(ui.getCoor());
tmp.sort();
if(tmp==coor){
Permutation sort_per(ui.getCoor());
sort_per.inverse();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==g.dimen()){
StackProduct<FGSTensor> sp(*this,*it,sort_per,out.getSym());
if(!sp.isZero(coor)){
KronProdStack<FGSTensor> kp(sp,coor);
FPSTensor fps(out.getDims(),*it,sort_per,g,kp);
{
SYNCHRO syn(ad,"multAndAddStacks");
fps.addTo(out);
}
}
}
}
}
}
}
/*:17*/
#line 28 "./stack_container.cweb"
;
/*18:*/
#line 397 "./stack_container.cweb"
void UnfoldedStackContainer::multAndAdd(const FSSparseTensor&t,
UGSTensor&out)const
UGSTensor&out)const
{
TL_RAISE_IF(t.nvar()!=getAllSize(),
"Wrong number of variables of tensor for UnfoldedStackContainer::multAndAdd");
multAndAddSparse2(t,out);
TL_RAISE_IF(t.nvar()!=getAllSize(),
"Wrong number of variables of tensor for UnfoldedStackContainer::multAndAdd");
multAndAddSparse2(t,out);
}
/*:18*/
#line 30 "./stack_container.cweb"
;
/*19:*/
#line 419 "./stack_container.cweb"
void UnfoldedStackContainer::multAndAdd(int dim,const UGSContainer&c,
UGSTensor&out)const
UGSTensor&out)const
{
TL_RAISE_IF(c.num()!=numStacks(),
"Wrong symmetry length of container for UnfoldedStackContainer::multAndAdd");
THREAD_GROUP gr;
SymmetrySet ss(dim,c.num());
for(symiterator si(ss);!si.isEnd();++si){
if(c.check(*si)){
THREAD*worker= new WorkerUnfoldMAADense(*this,*si,c,out);
gr.insert(worker);
}
}
gr.run();
TL_RAISE_IF(c.num()!=numStacks(),
"Wrong symmetry length of container for UnfoldedStackContainer::multAndAdd");
THREAD_GROUP gr;
SymmetrySet ss(dim,c.num());
for(symiterator si(ss);!si.isEnd();++si){
if(c.check(*si)){
THREAD*worker= new WorkerUnfoldMAADense(*this,*si,c,out);
gr.insert(worker);
}
}
gr.run();
}
/*:19*/
#line 31 "./stack_container.cweb"
;
/*20:*/
#line 438 "./stack_container.cweb"
void WorkerUnfoldMAADense::operator()()
{
Permutation iden(dense_cont.num());
IntSequence coor(sym,iden.getMap());
const UGSTensor*g= dense_cont.get(sym);
cont.multAndAddStacks(coor,*g,out,&out);
Permutation iden(dense_cont.num());
IntSequence coor(sym,iden.getMap());
const UGSTensor*g= dense_cont.get(sym);
cont.multAndAddStacks(coor,*g,out,&out);
}
/*:20*/
#line 32 "./stack_container.cweb"
;
/*21:*/
#line 448 "./stack_container.cweb"
WorkerUnfoldMAADense::WorkerUnfoldMAADense(const UnfoldedStackContainer&container,
const Symmetry&s,
const UGSContainer&dcontainer,
UGSTensor&outten)
:cont(container),sym(s),dense_cont(dcontainer),out(outten){}
const Symmetry&s,
const UGSContainer&dcontainer,
UGSTensor&outten)
:cont(container),sym(s),dense_cont(dcontainer),out(outten){}
/*:21*/
#line 33 "./stack_container.cweb"
;
/*22:*/
#line 473 "./stack_container.cweb"
void UnfoldedStackContainer::multAndAddSparse1(const FSSparseTensor&t,
UGSTensor&out)const
UGSTensor&out)const
{
THREAD_GROUP gr;
UFSTensor dummy(0,numStacks(),t.dimen());
for(Tensor::index ui= dummy.begin();ui!=dummy.end();++ui){
THREAD*worker= new WorkerUnfoldMAASparse1(*this,t,out,ui.getCoor());
gr.insert(worker);
}
gr.run();
THREAD_GROUP gr;
UFSTensor dummy(0,numStacks(),t.dimen());
for(Tensor::index ui= dummy.begin();ui!=dummy.end();++ui){
THREAD*worker= new WorkerUnfoldMAASparse1(*this,t,out,ui.getCoor());
gr.insert(worker);
}
gr.run();
}
/*:22*/
#line 34 "./stack_container.cweb"
;
/*23:*/
#line 512 "./stack_container.cweb"
void WorkerUnfoldMAASparse1::operator()()
{
const EquivalenceSet&eset= ebundle.get(out.dimen());
const PermutationSet&pset= tls.pbundle->get(t.dimen());
Permutation iden(t.dimen());
UPSTensor slice(t,cont.getStackSizes(),coor,
PerTensorDimens(cont.getStackSizes(),coor));
for(int iper= 0;iper<pset.getNum();iper++){
const Permutation&per= pset.get(iper);
IntSequence percoor(coor.size());
per.apply(coor,percoor);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==t.dimen()){
StackProduct<UGSTensor> sp(cont,*it,out.getSym());
if(!sp.isZero(percoor)){
KronProdStack<UGSTensor> kp(sp,percoor);
kp.optimizeOrder();
const Permutation&oper= kp.getPer();
if(Permutation(oper,per)==iden){
UPSTensor ups(out.getDims(),*it,slice,kp);
{
SYNCHRO syn(&out,"WorkerUnfoldMAASparse1");
ups.addTo(out);
}
}
}
}
}
}
const EquivalenceSet&eset= ebundle.get(out.dimen());
const PermutationSet&pset= tls.pbundle->get(t.dimen());
Permutation iden(t.dimen());
UPSTensor slice(t,cont.getStackSizes(),coor,
PerTensorDimens(cont.getStackSizes(),coor));
for(int iper= 0;iper<pset.getNum();iper++){
const Permutation&per= pset.get(iper);
IntSequence percoor(coor.size());
per.apply(coor,percoor);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==t.dimen()){
StackProduct<UGSTensor> sp(cont,*it,out.getSym());
if(!sp.isZero(percoor)){
KronProdStack<UGSTensor> kp(sp,percoor);
kp.optimizeOrder();
const Permutation&oper= kp.getPer();
if(Permutation(oper,per)==iden){
UPSTensor ups(out.getDims(),*it,slice,kp);
{
SYNCHRO syn(&out,"WorkerUnfoldMAASparse1");
ups.addTo(out);
}
}
}
}
}
}
}
/*:23*/
#line 35 "./stack_container.cweb"
;
/*24:*/
#line 547 "./stack_container.cweb"
WorkerUnfoldMAASparse1::WorkerUnfoldMAASparse1(const UnfoldedStackContainer&container,
const FSSparseTensor&ten,
UGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c),ebundle(*(tls.ebundle)){}
const FSSparseTensor&ten,
UGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c),ebundle(*(tls.ebundle)){}
/*:24*/
#line 36 "./stack_container.cweb"
;
/*25:*/
#line 575 "./stack_container.cweb"
void UnfoldedStackContainer::multAndAddSparse2(const FSSparseTensor&t,
UGSTensor&out)const
UGSTensor&out)const
{
THREAD_GROUP gr;
FFSTensor dummy_f(0,numStacks(),t.dimen());
for(Tensor::index fi= dummy_f.begin();fi!=dummy_f.end();++fi){
THREAD*worker= new WorkerUnfoldMAASparse2(*this,t,out,fi.getCoor());
gr.insert(worker);
}
gr.run();
THREAD_GROUP gr;
FFSTensor dummy_f(0,numStacks(),t.dimen());
for(Tensor::index fi= dummy_f.begin();fi!=dummy_f.end();++fi){
THREAD*worker= new WorkerUnfoldMAASparse2(*this,t,out,fi.getCoor());
gr.insert(worker);
}
gr.run();
}
/*:25*/
#line 37 "./stack_container.cweb"
;
/*26:*/
#line 595 "./stack_container.cweb"
void WorkerUnfoldMAASparse2::operator()()
{
GSSparseTensor slice(t,cont.getStackSizes(),coor,
TensorDimens(cont.getStackSizes(),coor));
if(slice.getNumNonZero()){
FGSTensor fslice(slice);
UGSTensor dense_slice(fslice);
int r1= slice.getFirstNonZeroRow();
int r2= slice.getLastNonZeroRow();
UGSTensor dense_slice1(r1,r2-r1+1,dense_slice);
UGSTensor out1(r1,r2-r1+1,out);
cont.multAndAddStacks(coor,dense_slice1,out1,&out);
}
GSSparseTensor slice(t,cont.getStackSizes(),coor,
TensorDimens(cont.getStackSizes(),coor));
if(slice.getNumNonZero()){
FGSTensor fslice(slice);
UGSTensor dense_slice(fslice);
int r1= slice.getFirstNonZeroRow();
int r2= slice.getLastNonZeroRow();
UGSTensor dense_slice1(r1,r2-r1+1,dense_slice);
UGSTensor out1(r1,r2-r1+1,out);
cont.multAndAddStacks(coor,dense_slice1,out1,&out);
}
}
/*:26*/
#line 38 "./stack_container.cweb"
;
/*27:*/
#line 613 "./stack_container.cweb"
WorkerUnfoldMAASparse2::WorkerUnfoldMAASparse2(const UnfoldedStackContainer&container,
const FSSparseTensor&ten,
UGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c){}
const FSSparseTensor&ten,
UGSTensor&outten,const IntSequence&c)
:cont(container),t(ten),out(outten),coor(c){}
/*:27*/
#line 39 "./stack_container.cweb"
;
/*28:*/
#line 636 "./stack_container.cweb"
void UnfoldedStackContainer::multAndAddStacks(const IntSequence&fi,
const UGSTensor&g,
UGSTensor&out,const void*ad)const
const UGSTensor&g,
UGSTensor&out,const void*ad)const
{
const EquivalenceSet&eset= ebundle.get(out.dimen());
UFSTensor dummy_u(0,numStacks(),g.dimen());
for(Tensor::index ui= dummy_u.begin();ui!=dummy_u.end();++ui){
IntSequence tmp(ui.getCoor());
tmp.sort();
if(tmp==fi){
Permutation sort_per(ui.getCoor());
sort_per.inverse();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==g.dimen()){
StackProduct<UGSTensor> sp(*this,*it,sort_per,out.getSym());
if(!sp.isZero(fi)){
KronProdStack<UGSTensor> kp(sp,fi);
if(g.getSym().isFull())
kp.optimizeOrder();
UPSTensor ups(out.getDims(),*it,sort_per,g,kp);
{
SYNCHRO syn(ad,"multAndAddStacks");
ups.addTo(out);
}
}
}
}
}
}
const EquivalenceSet&eset= ebundle.get(out.dimen());
UFSTensor dummy_u(0,numStacks(),g.dimen());
for(Tensor::index ui= dummy_u.begin();ui!=dummy_u.end();++ui){
IntSequence tmp(ui.getCoor());
tmp.sort();
if(tmp==fi){
Permutation sort_per(ui.getCoor());
sort_per.inverse();
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==g.dimen()){
StackProduct<UGSTensor> sp(*this,*it,sort_per,out.getSym());
if(!sp.isZero(fi)){
KronProdStack<UGSTensor> kp(sp,fi);
if(g.getSym().isFull())
kp.optimizeOrder();
UPSTensor ups(out.getDims(),*it,sort_per,g,kp);
{
SYNCHRO syn(ad,"multAndAddStacks");
ups.addTo(out);
}
}
}
}
}
}
}
/*:28*/
#line 40 "./stack_container.cweb"
;

View File

@ -1,5 +1,4 @@
/*1:*/
#line 80 "./stack_container.hweb"
#ifndef STACK_CONTAINER_H
#define STACK_CONTAINER_H
@ -13,7 +12,6 @@
#include "sthread.h"
/*2:*/
#line 135 "./stack_container.hweb"
template<class _Ttype>
class StackContainerInterface{
@ -47,10 +45,8 @@ int getAllSize()const
};
/*:2*/
#line 92 "./stack_container.hweb"
;
/*3:*/
#line 171 "./stack_container.hweb"
template<class _Ttype>
class StackContainer:virtual public StackContainerInterface<_Ttype> {
@ -84,7 +80,6 @@ virtual itype getType(int i,const Symmetry&s)const= 0;
int numStacks()const
{return stack_sizes.size();}
/*4:*/
#line 213 "./stack_container.hweb"
bool isZero(int i,const Symmetry&s)const
{
@ -95,10 +90,8 @@ return(getType(i,s)==_Stype::zero||
}
/*:4*/
#line 203 "./stack_container.hweb"
;
/*5:*/
#line 223 "./stack_container.hweb"
const _Ttype*getMatrix(int i,const Symmetry&s)const
{
@ -108,10 +101,8 @@ return conts[i]->get(s);
}
/*:5*/
#line 204 "./stack_container.hweb"
;
/*6:*/
#line 232 "./stack_container.hweb"
int getLengthOfMatrixStacks(const Symmetry&s)const
{
@ -124,10 +115,8 @@ return res;
/*:6*/
#line 205 "./stack_container.hweb"
;
/*7:*/
#line 244 "./stack_container.hweb"
int getUnitPos(const Symmetry&s)const
{
@ -141,10 +130,8 @@ return i;
/*:7*/
#line 206 "./stack_container.hweb"
;
/*8:*/
#line 257 "./stack_container.hweb"
Vector*createPackedColumn(const Symmetry&s,
const IntSequence&coor,int&iu)const
@ -176,11 +163,9 @@ return res;
}
/*:8*/
#line 207 "./stack_container.hweb"
;
protected:
/*9:*/
#line 288 "./stack_container.hweb"
void calculateOffsets()
{
@ -190,15 +175,12 @@ stack_offsets[i]= stack_offsets[i-1]+stack_sizes[i-1];
}
/*:9*/
#line 209 "./stack_container.hweb"
;
};
/*:3*/
#line 93 "./stack_container.hweb"
;
/*10:*/
#line 297 "./stack_container.hweb"
class WorkerFoldMAADense;
class WorkerFoldMAASparse1;
@ -229,10 +211,8 @@ FGSTensor&out,const void*ad)const;
/*:10*/
#line 94 "./stack_container.hweb"
;
/*11:*/
#line 327 "./stack_container.hweb"
class WorkerUnfoldMAADense;
class WorkerUnfoldMAASparse1;
@ -256,10 +236,8 @@ UGSTensor&out,const void*ad)const;
};
/*:11*/
#line 95 "./stack_container.hweb"
;
/*12:*/
#line 360 "./stack_container.hweb"
template<class _Ttype>
class ZContainer:public StackContainer<_Ttype> {
@ -280,7 +258,6 @@ _Tparent::calculateOffsets();
}
/*13:*/
#line 385 "./stack_container.hweb"
itype getType(int i,const Symmetry&s)const
{
@ -307,15 +284,12 @@ return _Stype::zero;
}
/*:13*/
#line 379 "./stack_container.hweb"
;
};
/*:12*/
#line 96 "./stack_container.hweb"
;
/*14:*/
#line 411 "./stack_container.hweb"
class FoldedZContainer:public ZContainer<FGSTensor> ,
public FoldedStackContainer{
@ -327,10 +301,8 @@ int ny,int nu)
};
/*:14*/
#line 97 "./stack_container.hweb"
;
/*15:*/
#line 422 "./stack_container.hweb"
class UnfoldedZContainer:public ZContainer<UGSTensor> ,
public UnfoldedStackContainer{
@ -342,10 +314,8 @@ int ny,int nu)
};
/*:15*/
#line 98 "./stack_container.hweb"
;
/*16:*/
#line 442 "./stack_container.hweb"
template<class _Ttype>
class GContainer:public StackContainer<_Ttype> {
@ -364,7 +334,6 @@ _Tparent::calculateOffsets();
}
/*17:*/
#line 467 "./stack_container.hweb"
itype getType(int i,const Symmetry&s)const
{
@ -392,15 +361,12 @@ return _Stype::zero;
/*:17*/
#line 459 "./stack_container.hweb"
;
};
/*:16*/
#line 99 "./stack_container.hweb"
;
/*18:*/
#line 494 "./stack_container.hweb"
class FoldedGContainer:public GContainer<FGSTensor> ,
public FoldedStackContainer{
@ -411,10 +377,8 @@ FoldedGContainer(const _Ctype*gs,int ngs,int nu)
};
/*:18*/
#line 100 "./stack_container.hweb"
;
/*19:*/
#line 504 "./stack_container.hweb"
class UnfoldedGContainer:public GContainer<UGSTensor> ,
public UnfoldedStackContainer{
@ -426,10 +390,8 @@ UnfoldedGContainer(const _Ctype*gs,int ngs,int nu)
/*:19*/
#line 101 "./stack_container.hweb"
;
/*20:*/
#line 520 "./stack_container.hweb"
template<class _Ttype>
class StackProduct{
@ -455,7 +417,6 @@ int getAllSize()const
const Symmetry&getProdSym(int ip)const
{return syms[ip];}
/*21:*/
#line 553 "./stack_container.hweb"
bool isZero(const IntSequence&istacks)const
{
@ -470,10 +431,8 @@ return res;
}
/*:21*/
#line 544 "./stack_container.hweb"
;
/*22:*/
#line 567 "./stack_container.hweb"
itype getType(int is,int ip)const
{
@ -485,10 +444,8 @@ return stack_cont.getType(is,syms[ip]);
}
/*:22*/
#line 545 "./stack_container.hweb"
;
/*23:*/
#line 578 "./stack_container.hweb"
const _Ttype*getMatrix(int is,int ip)const
{
@ -496,10 +453,8 @@ return stack_cont.getMatrix(is,syms[ip]);
}
/*:23*/
#line 546 "./stack_container.hweb"
;
/*24:*/
#line 585 "./stack_container.hweb"
void createPackedColumns(const IntSequence&coor,
Vector**vs,IntSequence&iu)const
@ -519,10 +474,8 @@ off+= syms[i].dimen();
}
/*:24*/
#line 547 "./stack_container.hweb"
;
/*25:*/
#line 604 "./stack_container.hweb"
int getSize(int is)const
{
@ -531,10 +484,8 @@ return stack_cont.getStackSizes()[is];
/*:25*/
#line 548 "./stack_container.hweb"
;
/*26:*/
#line 612 "./stack_container.hweb"
int numMatrices(const IntSequence&istacks)const
{
@ -550,15 +501,12 @@ return ret;
}
/*:26*/
#line 549 "./stack_container.hweb"
;
};
/*:20*/
#line 102 "./stack_container.hweb"
;
/*27:*/
#line 629 "./stack_container.hweb"
template<class _Ttype>
class KronProdStack:public KronProdAllOptim{
@ -566,7 +514,6 @@ public:
typedef StackProduct<_Ttype> _Ptype;
typedef StackContainerInterface<_Ttype> _Stype;
/*28:*/
#line 646 "./stack_container.hweb"
KronProdStack(const _Ptype&sp,const IntSequence&istack)
:KronProdAllOptim(sp.dimen())
@ -590,15 +537,12 @@ setMat(i,*m);
/*:28*/
#line 635 "./stack_container.hweb"
;
};
/*:27*/
#line 103 "./stack_container.hweb"
;
/*29:*/
#line 669 "./stack_container.hweb"
class WorkerFoldMAADense:public THREAD{
const FoldedStackContainer&cont;
@ -614,10 +558,8 @@ void operator()();
};
/*:29*/
#line 104 "./stack_container.hweb"
;
/*30:*/
#line 684 "./stack_container.hweb"
class WorkerFoldMAASparse1:public THREAD{
const FoldedStackContainer&cont;
@ -633,10 +575,8 @@ void operator()();
};
/*:30*/
#line 105 "./stack_container.hweb"
;
/*31:*/
#line 699 "./stack_container.hweb"
class WorkerFoldMAASparse2:public THREAD{
const FoldedStackContainer&cont;
@ -651,10 +591,8 @@ void operator()();
};
/*:31*/
#line 106 "./stack_container.hweb"
;
/*32:*/
#line 713 "./stack_container.hweb"
class WorkerFoldMAASparse4:public THREAD{
const FoldedStackContainer&cont;
@ -669,10 +607,8 @@ void operator()();
};
/*:32*/
#line 107 "./stack_container.hweb"
;
/*33:*/
#line 727 "./stack_container.hweb"
class WorkerUnfoldMAADense:public THREAD{
const UnfoldedStackContainer&cont;
@ -688,10 +624,8 @@ void operator()();
};
/*:33*/
#line 108 "./stack_container.hweb"
;
/*34:*/
#line 742 "./stack_container.hweb"
class WorkerUnfoldMAASparse1:public THREAD{
const UnfoldedStackContainer&cont;
@ -707,10 +641,8 @@ void operator()();
};
/*:34*/
#line 109 "./stack_container.hweb"
;
/*35:*/
#line 757 "./stack_container.hweb"
class WorkerUnfoldMAASparse2:public THREAD{
const UnfoldedStackContainer&cont;
@ -726,7 +658,6 @@ void operator()();
/*:35*/
#line 110 "./stack_container.hweb"
;
#endif

View File

@ -1,234 +1,217 @@
/*1:*/
#line 9 "./sthread.cweb"
#include "sthread.h"
#ifdef POSIX_THREADS
namespace sthread{
template<>
int thread_group<posix> ::max_parallel_threads= 2;
template<>
int detach_thread_group<posix> ::max_parallel_threads= 2;
/*2:*/
#line 31 "./sthread.cweb"
/*3:*/
#line 40 "./sthread.cweb"
void*posix_thread_function(void*c);
template<>
void thread_traits<posix> ::run(_Ctype*c)
{
pthread_create(&(c->getThreadIden()),NULL,posix_thread_function,(void*)c);
}
void*posix_detach_thread_function(void*c);
template<>
void thread_traits<posix> ::detach_run(_Dtype*c)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&(c->getThreadIden()),&attr,posix_detach_thread_function,(void*)c);
pthread_attr_destroy(&attr);
}
template<>
void thread_traits<posix> ::exit()
{
pthread_exit(NULL);
}
template<>
void thread_traits<posix> ::join(_Ctype*c)
{
pthread_join(c->getThreadIden(),NULL);
}
/*:3*/
#line 32 "./sthread.cweb"
;
/*4:*/
#line 74 "./sthread.cweb"
template<>
void mutex_traits<posix> ::init(pthread_mutex_t&m)
{
pthread_mutex_init(&m,NULL);
}
template<>
void mutex_traits<posix> ::lock(pthread_mutex_t&m)
{
pthread_mutex_lock(&m);
}
template<>
void mutex_traits<posix> ::unlock(pthread_mutex_t&m)
{
pthread_mutex_unlock(&m);
}
/*:4*/
#line 33 "./sthread.cweb"
;
/*5:*/
#line 94 "./sthread.cweb"
template<>
void cond_traits<posix> ::init(_Tcond&cond)
{
pthread_cond_init(&cond,NULL);
}
template<>
void cond_traits<posix> ::broadcast(_Tcond&cond)
{
pthread_cond_broadcast(&cond);
}
template<>
void cond_traits<posix> ::wait(_Tcond&cond,_Tmutex&mutex)
{
pthread_cond_wait(&cond,&mutex);
}
template<>
void cond_traits<posix> ::destroy(_Tcond&cond)
{
pthread_cond_destroy(&cond);
}
/*:5*/
#line 34 "./sthread.cweb"
;
/*6:*/
#line 123 "./sthread.cweb"
static posix_synchro::mutex_map_t posix_mm;
PosixSynchro::PosixSynchro(const void*c,const char*id)
:posix_synchro(c,id,posix_mm){}
/*:6*/
#line 35 "./sthread.cweb"
;
/*7:*/
#line 131 "./sthread.cweb"
void*posix_thread_function(void*c)
{
thread_traits<posix> ::_Ctype*ct=
(thread_traits<posix> ::_Ctype*)c;
try{
ct->operator()();
}catch(...){
ct->exit();
}
return NULL;
}
/*:7*/
#line 36 "./sthread.cweb"
;
/*8:*/
#line 145 "./sthread.cweb"
void*posix_detach_thread_function(void*c)
{
thread_traits<posix> ::_Dtype*ct=
(thread_traits<posix> ::_Dtype*)c;
condition_counter<posix> *counter= ct->counter;
try{
ct->operator()();
}catch(...){
ct->exit();
}
if(counter)
counter->decrease();
return NULL;
}
/*:8*/
#line 37 "./sthread.cweb"
;
/*:2*/
#line 18 "./sthread.cweb"
;
template<>
int thread_group<posix> ::max_parallel_threads= 2;
template<>
int detach_thread_group<posix> ::max_parallel_threads= 2;
/*2:*/
/*3:*/
void*posix_thread_function(void*c);
template<>
void thread_traits<posix> ::run(_Ctype*c)
{
pthread_create(&(c->getThreadIden()),NULL,posix_thread_function,(void*)c);
}
void*posix_detach_thread_function(void*c);
template<>
void thread_traits<posix> ::detach_run(_Dtype*c)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&(c->getThreadIden()),&attr,posix_detach_thread_function,(void*)c);
pthread_attr_destroy(&attr);
}
template<>
void thread_traits<posix> ::exit()
{
pthread_exit(NULL);
}
template<>
void thread_traits<posix> ::join(_Ctype*c)
{
pthread_join(c->getThreadIden(),NULL);
}
/*:3*/
;
/*4:*/
template<>
void mutex_traits<posix> ::init(pthread_mutex_t&m)
{
pthread_mutex_init(&m,NULL);
}
template<>
void mutex_traits<posix> ::lock(pthread_mutex_t&m)
{
pthread_mutex_lock(&m);
}
template<>
void mutex_traits<posix> ::unlock(pthread_mutex_t&m)
{
pthread_mutex_unlock(&m);
}
/*:4*/
;
/*5:*/
template<>
void cond_traits<posix> ::init(_Tcond&cond)
{
pthread_cond_init(&cond,NULL);
}
template<>
void cond_traits<posix> ::broadcast(_Tcond&cond)
{
pthread_cond_broadcast(&cond);
}
template<>
void cond_traits<posix> ::wait(_Tcond&cond,_Tmutex&mutex)
{
pthread_cond_wait(&cond,&mutex);
}
template<>
void cond_traits<posix> ::destroy(_Tcond&cond)
{
pthread_cond_destroy(&cond);
}
/*:5*/
;
/*6:*/
static posix_synchro::mutex_map_t posix_mm;
PosixSynchro::PosixSynchro(const void*c,const char*id)
:posix_synchro(c,id,posix_mm){}
/*:6*/
;
/*7:*/
void*posix_thread_function(void*c)
{
thread_traits<posix> ::_Ctype*ct=
(thread_traits<posix> ::_Ctype*)c;
try{
ct->operator()();
}catch(...){
ct->exit();
}
return NULL;
}
/*:7*/
;
/*8:*/
void*posix_detach_thread_function(void*c)
{
thread_traits<posix> ::_Dtype*ct=
(thread_traits<posix> ::_Dtype*)c;
condition_counter<posix> *counter= ct->counter;
try{
ct->operator()();
}catch(...){
ct->exit();
}
if(counter)
counter->decrease();
return NULL;
}
/*:8*/
;
/*:2*/
;
}
#else
namespace sthread{
template<>
int thread_group<empty> ::max_parallel_threads= 1;
template<>
int detach_thread_group<empty> ::max_parallel_threads= 1;
/*9:*/
#line 166 "./sthread.cweb"
template<>
void thread_traits<empty> ::run(_Ctype*c)
{
c->operator()();
}
template<>
void thread_traits<empty> ::detach_run(_Dtype*c)
{
c->operator()();
}
template<>
void thread_traits<empty> ::exit()
{
}
template<>
void thread_traits<empty> ::join(_Ctype*c)
{
}
template<>
void mutex_traits<empty> ::init(Empty&m)
{
}
template<>
void mutex_traits<empty> ::lock(Empty&m)
{
}
template<>
void mutex_traits<empty> ::unlock(Empty&m)
{
}
template<>
void cond_traits<empty> ::init(_Tcond&cond)
{
}
template<>
void cond_traits<empty> ::broadcast(_Tcond&cond)
{
}
template<>
void cond_traits<empty> ::wait(_Tcond&cond,_Tmutex&mutex)
{
}
template<>
void cond_traits<empty> ::destroy(_Tcond&cond)
{
}
/*:9*/
#line 26 "./sthread.cweb"
;
template<>
int thread_group<empty> ::max_parallel_threads= 1;
template<>
int detach_thread_group<empty> ::max_parallel_threads= 1;
/*9:*/
template<>
void thread_traits<empty> ::run(_Ctype*c)
{
c->operator()();
}
template<>
void thread_traits<empty> ::detach_run(_Dtype*c)
{
c->operator()();
}
template<>
void thread_traits<empty> ::exit()
{
}
template<>
void thread_traits<empty> ::join(_Ctype*c)
{
}
template<>
void mutex_traits<empty> ::init(Empty&m)
{
}
template<>
void mutex_traits<empty> ::lock(Empty&m)
{
}
template<>
void mutex_traits<empty> ::unlock(Empty&m)
{
}
template<>
void cond_traits<empty> ::init(_Tcond&cond)
{
}
template<>
void cond_traits<empty> ::broadcast(_Tcond&cond)
{
}
template<>
void cond_traits<empty> ::wait(_Tcond&cond,_Tmutex&mutex)
{
}
template<>
void cond_traits<empty> ::destroy(_Tcond&cond)
{
}
/*:9*/
;
}
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 77 "./sthread.hweb"
#ifndef STHREAD_H
#define STHREAD_H
@ -17,7 +16,6 @@ using namespace std;
class Empty{};
/*2:*/
#line 117 "./sthread.hweb"
template<bool condition,class Then,class Else>
struct IF{
@ -32,13 +30,11 @@ typedef Else RET;
/*:2*/
#line 93 "./sthread.hweb"
;
enum{posix,empty};
template<int> class thread_traits;
template<int> class detach_thread;
/*3:*/
#line 134 "./sthread.hweb"
template<int thread_impl>
class thread{
@ -61,10 +57,8 @@ void exit()
};
/*:3*/
#line 97 "./sthread.hweb"
;
/*4:*/
#line 160 "./sthread.hweb"
template<int thread_impl>
class thread_group{
@ -77,7 +71,6 @@ static int max_parallel_threads;
void insert(_Ctype*c)
{tlist.push_back(c);}
/*5:*/
#line 180 "./sthread.hweb"
~thread_group()
{
@ -88,10 +81,8 @@ tlist.pop_front();
}
/*:5*/
#line 171 "./sthread.hweb"
;
/*7:*/
#line 216 "./sthread.hweb"
void run()
{
@ -112,11 +103,9 @@ run_portion(pfirst,rem);
/*:7*/
#line 172 "./sthread.hweb"
;
private:
/*6:*/
#line 192 "./sthread.hweb"
iterator run_portion(iterator start,int n)
{
@ -134,15 +123,12 @@ return ret;
/*:6*/
#line 174 "./sthread.hweb"
;
};
/*:4*/
#line 98 "./sthread.hweb"
;
/*8:*/
#line 238 "./sthread.hweb"
template<int thread_impl>
struct thread_traits{
@ -156,10 +142,8 @@ static void join(_Ctype*c);
};
/*:8*/
#line 99 "./sthread.hweb"
;
/*9:*/
#line 251 "./sthread.hweb"
struct ltmmkey;
typedef pair<const void*,const char*> mmkey;
@ -174,10 +158,8 @@ static void unlock(_Tmutex&m);
};
/*:9*/
#line 100 "./sthread.hweb"
;
/*10:*/
#line 283 "./sthread.hweb"
struct ltmmkey{
bool operator()(const mmkey&k1,const mmkey&k2)const
@ -204,7 +186,6 @@ void insert(const void*c,const char*id,const _Tmutex&m)
bool check(const void*c,const char*id)const
{return _Tparent::find(mmkey(c,id))!=_Tparent::end();}
/*11:*/
#line 318 "./sthread.hweb"
mmval*get(const void*c,const char*id)
{
@ -215,10 +196,8 @@ return&((*it).second);
}
/*:11*/
#line 308 "./sthread.hweb"
;
/*12:*/
#line 331 "./sthread.hweb"
void remove(const void*c,const char*id)
{
@ -228,7 +207,6 @@ erase(it);
}
/*:12*/
#line 309 "./sthread.hweb"
;
void lock_map()
{_Mtraits::lock(m);}
@ -238,10 +216,8 @@ void unlock_map()
};
/*:10*/
#line 101 "./sthread.hweb"
;
/*13:*/
#line 346 "./sthread.hweb"
template<int thread_impl>
class synchro{
@ -261,7 +237,6 @@ synchro(const void*c,const char*id,mutex_map_t&mmap)
{unlock();}
private:
/*14:*/
#line 373 "./sthread.hweb"
void lock(){
mutmap.lock_map();
@ -276,10 +251,8 @@ _Mtraits::lock(mutmap.get(caller,iden)->first);
}
/*:14*/
#line 364 "./sthread.hweb"
;
/*15:*/
#line 390 "./sthread.hweb"
void unlock(){
mutmap.lock_map();
@ -293,15 +266,12 @@ mutmap.unlock_map();
}
/*:15*/
#line 365 "./sthread.hweb"
;
};
/*:13*/
#line 102 "./sthread.hweb"
;
/*16:*/
#line 405 "./sthread.hweb"
template<int thread_impl>
struct cond_traits{
@ -314,10 +284,8 @@ static void destroy(_Tcond&cond);
};
/*:16*/
#line 103 "./sthread.hweb"
;
/*17:*/
#line 425 "./sthread.hweb"
template<int thread_impl>
class condition_counter{
@ -329,7 +297,6 @@ _Tcond cond;
bool changed;
public:
/*18:*/
#line 445 "./sthread.hweb"
condition_counter()
:counter(0),changed(true)
@ -339,10 +306,8 @@ cond_traits<thread_impl> ::init(cond);
}
/*:18*/
#line 435 "./sthread.hweb"
;
/*19:*/
#line 456 "./sthread.hweb"
~condition_counter()
{
@ -350,10 +315,8 @@ cond_traits<thread_impl> ::destroy(cond);
}
/*:19*/
#line 436 "./sthread.hweb"
;
/*20:*/
#line 465 "./sthread.hweb"
void increase()
{
@ -365,10 +328,8 @@ mutex_traits<thread_impl> ::unlock(mut);
}
/*:20*/
#line 437 "./sthread.hweb"
;
/*21:*/
#line 476 "./sthread.hweb"
void decrease()
{
@ -380,10 +341,8 @@ mutex_traits<thread_impl> ::unlock(mut);
}
/*:21*/
#line 438 "./sthread.hweb"
;
/*22:*/
#line 490 "./sthread.hweb"
int waitForChange()
{
@ -399,15 +358,12 @@ return res;
/*:22*/
#line 439 "./sthread.hweb"
;
};
/*:17*/
#line 104 "./sthread.hweb"
;
/*23:*/
#line 509 "./sthread.hweb"
template<int thread_impl>
class detach_thread:public thread<thread_impl> {
@ -421,10 +377,8 @@ void run()
};
/*:23*/
#line 105 "./sthread.hweb"
;
/*24:*/
#line 525 "./sthread.hweb"
template<int thread_impl>
class detach_thread_group{
@ -437,7 +391,6 @@ condition_counter<thread_impl> counter;
public:
static int max_parallel_threads;
/*25:*/
#line 542 "./sthread.hweb"
void insert(_Ctype*c)
{
@ -446,10 +399,8 @@ c->installCounter(&counter);
}
/*:25*/
#line 536 "./sthread.hweb"
;
/*26:*/
#line 550 "./sthread.hweb"
~detach_thread_group()
{
@ -460,10 +411,8 @@ tlist.pop_front();
}
/*:26*/
#line 537 "./sthread.hweb"
;
/*27:*/
#line 566 "./sthread.hweb"
void run()
{
@ -481,16 +430,13 @@ while(counter.waitForChange()> 0){}
/*:27*/
#line 538 "./sthread.hweb"
;
};
/*:24*/
#line 106 "./sthread.hweb"
;
#ifdef POSIX_THREADS
/*28:*/
#line 586 "./sthread.hweb"
typedef detach_thread<posix> PosixThread;
typedef detach_thread_group<posix> PosixThreadGroup;
@ -505,11 +451,9 @@ PosixSynchro(const void*c,const char*id);
#define SYNCHRO sthread::PosixSynchro
/*:28*/
#line 108 "./sthread.hweb"
;
#else
/*29:*/
#line 604 "./sthread.hweb"
typedef thread<empty> NoThread;
typedef thread_group<empty> NoThreadGroup;
@ -525,7 +469,6 @@ NoSynchro(const void*c,const char*id){}
#define SYNCHRO sthread::NoSynchro
/*:29*/
#line 110 "./sthread.hweb"
;
#endif
};

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./symmetry.cweb"
#include "symmetry.h"
#include "permutation.h"
@ -7,159 +6,141 @@
#include <stdio.h>
/*2:*/
#line 24 "./symmetry.cweb"
Symmetry::Symmetry(const IntSequence&s)
:IntSequence(s.getNumDistinct(),0)
{
int p= 0;
if(s.size()> 0)
operator[](p)= 1;
for(int i= 1;i<s.size();i++){
if(s[i]!=s[i-1])
p++;
operator[](p)++;
}
int p= 0;
if(s.size()> 0)
operator[](p)= 1;
for(int i= 1;i<s.size();i++){
if(s[i]!=s[i-1])
p++;
operator[](p)++;
}
}
/*:2*/
#line 12 "./symmetry.cweb"
;
/*3:*/
#line 39 "./symmetry.cweb"
int Symmetry::findClass(int i)const
{
int j= 0;
int sum= 0;
do{
sum+= operator[](j);
j++;
}while(j<size()&&sum<=i);
return j-1;
int j= 0;
int sum= 0;
do{
sum+= operator[](j);
j++;
}while(j<size()&&sum<=i);
return j-1;
}
/*:3*/
#line 13 "./symmetry.cweb"
;
/*4:*/
#line 55 "./symmetry.cweb"
bool Symmetry::isFull()const
{
int count= 0;
for(int i= 0;i<num();i++)
if(operator[](i)!=0)
count++;
return count<=1;
int count= 0;
for(int i= 0;i<num();i++)
if(operator[](i)!=0)
count++;
return count<=1;
}
/*:4*/
#line 14 "./symmetry.cweb"
;
/*5:*/
#line 71 "./symmetry.cweb"
symiterator::symiterator(SymmetrySet&ss)
:s(ss),subit(NULL),subs(NULL),end_flag(false)
{
s.sym()[0]= 0;
if(s.size()==2){
s.sym()[1]= s.dimen();
}else{
subs= new SymmetrySet(s,s.dimen());
subit= new symiterator(*subs);
}
s.sym()[0]= 0;
if(s.size()==2){
s.sym()[1]= s.dimen();
}else{
subs= new SymmetrySet(s,s.dimen());
subit= new symiterator(*subs);
}
}
/*:5*/
#line 15 "./symmetry.cweb"
;
/*6:*/
#line 86 "./symmetry.cweb"
symiterator::~symiterator()
{
if(subit)
delete subit;
if(subs)
delete subs;
if(subit)
delete subit;
if(subs)
delete subs;
}
/*:6*/
#line 16 "./symmetry.cweb"
;
/*7:*/
#line 102 "./symmetry.cweb"
symiterator&symiterator::operator++()
{
if(!end_flag){
if(s.size()==2){
s.sym()[0]++;
s.sym()[1]--;
}else{
++(*subit);
if(subit->isEnd()){
delete subit;
delete subs;
s.sym()[0]++;
subs= new SymmetrySet(s,s.dimen()-s.sym()[0]);
subit= new symiterator(*subs);
}
}
if(s.sym()[0]==s.dimen()+1)
end_flag= true;
}
return*this;
if(!end_flag){
if(s.size()==2){
s.sym()[0]++;
s.sym()[1]--;
}else{
++(*subit);
if(subit->isEnd()){
delete subit;
delete subs;
s.sym()[0]++;
subs= new SymmetrySet(s,s.dimen()-s.sym()[0]);
subit= new symiterator(*subs);
}
}
if(s.sym()[0]==s.dimen()+1)
end_flag= true;
}
return*this;
}
/*:7*/
#line 17 "./symmetry.cweb"
;
/*8:*/
#line 126 "./symmetry.cweb"
InducedSymmetries::InducedSymmetries(const Equivalence&e,const Symmetry&s)
{
for(Equivalence::const_seqit i= e.begin();i!=e.end();++i){
push_back(Symmetry(s,*i));
}
for(Equivalence::const_seqit i= e.begin();i!=e.end();++i){
push_back(Symmetry(s,*i));
}
}
/*:8*/
#line 18 "./symmetry.cweb"
;
/*9:*/
#line 135 "./symmetry.cweb"
InducedSymmetries::InducedSymmetries(const Equivalence&e,const Permutation&p,
const Symmetry&s)
const Symmetry&s)
{
for(int i= 0;i<e.numClasses();i++){
Equivalence::const_seqit it= e.find(p.getMap()[i]);
push_back(Symmetry(s,*it));
}
for(int i= 0;i<e.numClasses();i++){
Equivalence::const_seqit it= e.find(p.getMap()[i]);
push_back(Symmetry(s,*it));
}
}
/*:9*/
#line 19 "./symmetry.cweb"
;
/*10:*/
#line 146 "./symmetry.cweb"
void InducedSymmetries::print()const
{
printf("Induced symmetries: %d\n",size());
for(unsigned int i= 0;i<size();i++)
operator[](i).print();
printf("Induced symmetries: %d\n",size());
for(unsigned int i= 0;i<size();i++)
operator[](i).print();
}
/*:10*/
#line 20 "./symmetry.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 49 "./symmetry.hweb"
#ifndef SYMMETRY_H
#define SYMMETRY_H
@ -11,12 +10,10 @@
#include <vector>
/*2:*/
#line 69 "./symmetry.hweb"
class Symmetry:public IntSequence{
public:
/*3:*/
#line 93 "./symmetry.hweb"
Symmetry(int len,const char*dummy)
:IntSequence(len,0){}
@ -48,7 +45,6 @@ Symmetry(Symmetry&s,int len)
Symmetry(const IntSequence&s);
/*:3*/
#line 72 "./symmetry.hweb"
;
int num()const
{return size();}
@ -59,10 +55,8 @@ bool isFull()const;
};
/*:2*/
#line 59 "./symmetry.hweb"
;
/*4:*/
#line 148 "./symmetry.hweb"
class SymmetrySet{
Symmetry run;
@ -83,10 +77,8 @@ int size()const
};
/*:4*/
#line 60 "./symmetry.hweb"
;
/*5:*/
#line 177 "./symmetry.hweb"
class symiterator{
SymmetrySet&s;
@ -105,10 +97,8 @@ const Symmetry&operator*()const
/*:5*/
#line 61 "./symmetry.hweb"
;
/*6:*/
#line 198 "./symmetry.hweb"
class InducedSymmetries:public vector<Symmetry> {
public:
@ -120,7 +110,6 @@ void print()const;
/*:6*/
#line 62 "./symmetry.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 6 "./t_container.cweb"
#include "t_container.h"
#include "kron_prod.h"
@ -8,127 +7,115 @@
const int FGSContainer::num_one_time= 10;
/*2:*/
#line 21 "./t_container.cweb"
UGSContainer::UGSContainer(const FGSContainer&c)
:TensorContainer<UGSTensor> (c.num())
{
for(FGSContainer::const_iterator it= c.begin();
it!=c.end();++it){
UGSTensor*unfolded= new UGSTensor(*((*it).second));
insert(unfolded);
}
for(FGSContainer::const_iterator it= c.begin();
it!=c.end();++it){
UGSTensor*unfolded= new UGSTensor(*((*it).second));
insert(unfolded);
}
}
/*:2*/
#line 13 "./t_container.cweb"
;
/*3:*/
#line 44 "./t_container.cweb"
void UGSContainer::multAndAdd(const UGSTensor&t,UGSTensor&out)const
{
int l= t.dimen();
int k= out.dimen();
const EquivalenceSet&eset= ebundle.get(k);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==l){
vector<const UGSTensor*> ts=
fetchTensors(out.getSym(),*it);
KronProdAllOptim kp(l);
for(int i= 0;i<l;i++)
kp.setMat(i,*(ts[i]));
kp.optimizeOrder();
UPSTensor ups(out.getDims(),*it,t,kp);
ups.addTo(out);
}
}
int l= t.dimen();
int k= out.dimen();
const EquivalenceSet&eset= ebundle.get(k);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==l){
vector<const UGSTensor*> ts=
fetchTensors(out.getSym(),*it);
KronProdAllOptim kp(l);
for(int i= 0;i<l;i++)
kp.setMat(i,*(ts[i]));
kp.optimizeOrder();
UPSTensor ups(out.getDims(),*it,t,kp);
ups.addTo(out);
}
}
}
/*:3*/
#line 14 "./t_container.cweb"
;
/*4:*/
#line 67 "./t_container.cweb"
FGSContainer::FGSContainer(const UGSContainer&c)
:TensorContainer<FGSTensor> (c.num())
{
for(UGSContainer::const_iterator it= c.begin();
it!=c.end();++it){
FGSTensor*folded= new FGSTensor(*((*it).second));
insert(folded);
}
for(UGSContainer::const_iterator it= c.begin();
it!=c.end();++it){
FGSTensor*folded= new FGSTensor(*((*it).second));
insert(folded);
}
}
/*:4*/
#line 15 "./t_container.cweb"
;
/*5:*/
#line 81 "./t_container.cweb"
void FGSContainer::multAndAdd(const FGSTensor&t,FGSTensor&out)const
{
UGSTensor ut(t);
multAndAdd(ut,out);
UGSTensor ut(t);
multAndAdd(ut,out);
}
/*:5*/
#line 16 "./t_container.cweb"
;
/*6:*/
#line 92 "./t_container.cweb"
void FGSContainer::multAndAdd(const UGSTensor&t,FGSTensor&out)const
{
int l= t.dimen();
int k= out.dimen();
const EquivalenceSet&eset= ebundle.get(k);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==l){
vector<const FGSTensor*> ts=
fetchTensors(out.getSym(),*it);
KronProdAllOptim kp(l);
for(int i= 0;i<l;i++)
kp.setMat(i,*(ts[i]));
kp.optimizeOrder();
FPSTensor fps(out.getDims(),*it,t,kp);
fps.addTo(out);
}
}
int l= t.dimen();
int k= out.dimen();
const EquivalenceSet&eset= ebundle.get(k);
for(EquivalenceSet::const_iterator it= eset.begin();
it!=eset.end();++it){
if((*it).numClasses()==l){
vector<const FGSTensor*> ts=
fetchTensors(out.getSym(),*it);
KronProdAllOptim kp(l);
for(int i= 0;i<l;i++)
kp.setMat(i,*(ts[i]));
kp.optimizeOrder();
FPSTensor fps(out.getDims(),*it,t,kp);
fps.addTo(out);
}
}
}
/*:6*/
#line 17 "./t_container.cweb"
;
/*7:*/
#line 120 "./t_container.cweb"
Tensor::index
FGSContainer::getIndices(int num,vector<IntSequence> &out,
const Tensor::index&start,
const Tensor::index&end)
const Tensor::index&start,
const Tensor::index&end)
{
out.clear();
int i= 0;
Tensor::index run= start;
while(i<num&&run!=end){
out.push_back(run.getCoor());
i++;
++run;
}
return run;
out.clear();
int i= 0;
Tensor::index run= start;
while(i<num&&run!=end){
out.push_back(run.getCoor());
i++;
++run;
}
return run;
}
/*:7*/
#line 18 "./t_container.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 48 "./t_container.hweb"
#ifndef T_CONTAINER_H
#define T_CONTAINER_H
@ -16,7 +15,6 @@
#include <map>
/*2:*/
#line 71 "./t_container.hweb"
struct ltsym{
bool operator()(const Symmetry&s1,const Symmetry&s2)const
@ -24,10 +22,8 @@ bool operator()(const Symmetry&s1,const Symmetry&s2)const
};
/*:2*/
#line 63 "./t_container.hweb"
;
/*3:*/
#line 97 "./t_container.hweb"
template<class _Ttype> class TensorContainer{
protected:
@ -47,7 +43,6 @@ public:
TensorContainer(int nn)
:n(nn),ebundle(*(tls.ebundle)){}
/*5:*/
#line 150 "./t_container.hweb"
TensorContainer(const TensorContainer<_Ttype> &c)
:n(c.n),m(),ebundle(c.ebundle)
@ -59,10 +54,8 @@ insert(ten);
}
/*:5*/
#line 115 "./t_container.hweb"
;
/*6:*/
#line 163 "./t_container.hweb"
TensorContainer(int first_row,int num,TensorContainer<_Ttype> &c)
:n(c.n),ebundle(*(tls.ebundle))
@ -75,10 +68,8 @@ insert(t);
/*:6*/
#line 116 "./t_container.hweb"
;
/*7:*/
#line 175 "./t_container.hweb"
_const_ptr get(const Symmetry&s)const
{
@ -108,10 +99,8 @@ return(*it).second;
}
/*:7*/
#line 117 "./t_container.hweb"
;
/*8:*/
#line 204 "./t_container.hweb"
bool check(const Symmetry&s)const
{
@ -122,10 +111,8 @@ return it!=m.end();
}
/*:8*/
#line 118 "./t_container.hweb"
;
/*9:*/
#line 214 "./t_container.hweb"
void insert(_ptr t)
{
@ -140,10 +127,8 @@ throw TLException(__FILE__,__LINE__,"NaN or Inf asserted in TensorContainer::ins
}
/*:9*/
#line 119 "./t_container.hweb"
;
/*10:*/
#line 228 "./t_container.hweb"
void remove(const Symmetry&s)
{
@ -157,10 +142,8 @@ delete t;
/*:10*/
#line 120 "./t_container.hweb"
;
/*11:*/
#line 241 "./t_container.hweb"
void clear()
{
@ -171,10 +154,8 @@ m.erase(m.begin());
}
/*:11*/
#line 121 "./t_container.hweb"
;
/*15:*/
#line 299 "./t_container.hweb"
vector<_const_ptr>
fetchTensors(const Symmetry&rsym,const Equivalence&e)const
@ -190,10 +171,8 @@ return res;
}
/*:15*/
#line 122 "./t_container.hweb"
;
/*12:*/
#line 251 "./t_container.hweb"
int getMaxDim()const
{
@ -208,10 +187,8 @@ return res;
/*:12*/
#line 123 "./t_container.hweb"
;
/*13:*/
#line 265 "./t_container.hweb"
void print()const
{
@ -224,10 +201,8 @@ printf("Symmetry: ");
}
/*:13*/
#line 124 "./t_container.hweb"
;
/*14:*/
#line 277 "./t_container.hweb"
void writeMat4(FILE*fd,const char*prefix)const
{
@ -247,14 +222,12 @@ m.writeMat4(fd,lname);
/*:14*/
#line 125 "./t_container.hweb"
;
virtual~TensorContainer()
{clear();}
/*4:*/
#line 134 "./t_container.hweb"
int num()const
{return n;}
@ -271,15 +244,12 @@ iterator end()
{return m.end();}
/*:4*/
#line 130 "./t_container.hweb"
;
};
/*:3*/
#line 64 "./t_container.hweb"
;
/*16:*/
#line 315 "./t_container.hweb"
class FGSContainer;
class UGSContainer:public TensorContainer<UGSTensor> {
@ -294,10 +264,8 @@ void multAndAdd(const UGSTensor&t,UGSTensor&out)const;
/*:16*/
#line 65 "./t_container.hweb"
;
/*17:*/
#line 342 "./t_container.hweb"
class FGSContainer:public TensorContainer<FGSTensor> {
static const int num_one_time;
@ -318,7 +286,6 @@ const Tensor::index&end);
/*:17*/
#line 66 "./t_container.hweb"
;
#endif

View File

@ -1,87 +1,76 @@
/*1:*/
#line 6 "./t_polynomial.cweb"
#include "t_polynomial.h"
#include "kron_prod.h"
/*2:*/
#line 20 "./t_polynomial.cweb"
const URSingleTensor&PowerProvider::getNext(const URSingleTensor*dummy)
{
if(ut){
URSingleTensor*ut_new= new URSingleTensor(nv,ut->dimen()+1);
KronProd::kronMult(ConstVector(origv),ConstVector(ut->getData()),ut_new->getData());
delete ut;
ut= ut_new;
}else{
ut= new URSingleTensor(nv,1);
ut->getData()= origv;
}
return*ut;
if(ut){
URSingleTensor*ut_new= new URSingleTensor(nv,ut->dimen()+1);
KronProd::kronMult(ConstVector(origv),ConstVector(ut->getData()),ut_new->getData());
delete ut;
ut= ut_new;
}else{
ut= new URSingleTensor(nv,1);
ut->getData()= origv;
}
return*ut;
}
/*:2*/
#line 10 "./t_polynomial.cweb"
;
/*3:*/
#line 38 "./t_polynomial.cweb"
const FRSingleTensor&PowerProvider::getNext(const FRSingleTensor*dummy)
{
getNext(ut);
if(ft)
delete ft;
ft= new FRSingleTensor(*ut);
return*ft;
getNext(ut);
if(ft)
delete ft;
ft= new FRSingleTensor(*ut);
return*ft;
}
/*:3*/
#line 11 "./t_polynomial.cweb"
;
/*4:*/
#line 49 "./t_polynomial.cweb"
PowerProvider::~PowerProvider()
{
if(ut)
delete ut;
if(ft)
delete ft;
if(ut)
delete ut;
if(ft)
delete ft;
}
/*:4*/
#line 12 "./t_polynomial.cweb"
;
/*5:*/
#line 59 "./t_polynomial.cweb"
UTensorPolynomial::UTensorPolynomial(const FTensorPolynomial&fp)
:TensorPolynomial<UFSTensor,UGSTensor,URSingleTensor> (fp.nrows(),fp.nvars())
{
for(FTensorPolynomial::const_iterator it= fp.begin();
it!=fp.end();++it){
insert(new UFSTensor(*((*it).second)));
}
for(FTensorPolynomial::const_iterator it= fp.begin();
it!=fp.end();++it){
insert(new UFSTensor(*((*it).second)));
}
}
/*:5*/
#line 13 "./t_polynomial.cweb"
;
/*6:*/
#line 70 "./t_polynomial.cweb"
FTensorPolynomial::FTensorPolynomial(const UTensorPolynomial&up)
:TensorPolynomial<FFSTensor,FGSTensor,FRSingleTensor> (up.nrows(),up.nvars())
{
for(UTensorPolynomial::const_iterator it= up.begin();
it!=up.end();++it){
insert(new FFSTensor(*((*it).second)));
}
for(UTensorPolynomial::const_iterator it= up.begin();
it!=up.end();++it){
insert(new FFSTensor(*((*it).second)));
}
}
/*:6*/
#line 14 "./t_polynomial.cweb"
;

View File

@ -1,5 +1,4 @@
/*1:*/
#line 47 "./t_polynomial.hweb"
#include "t_container.h"
#include "fs_tensor.h"
@ -7,7 +6,6 @@
#include"tl_static.h"
/*2:*/
#line 76 "./t_polynomial.hweb"
class PowerProvider{
Vector origv;
@ -23,10 +21,8 @@ const FRSingleTensor&getNext(const FRSingleTensor*dummy);
};
/*:2*/
#line 53 "./t_polynomial.hweb"
;
/*3:*/
#line 111 "./t_polynomial.hweb"
template<class _Ttype,class _TGStype,class _Stype>
class TensorPolynomial:public TensorContainer<_Ttype> {
@ -46,7 +42,6 @@ TensorPolynomial(int first_row,int num,TensorPolynomial<_Ttype,_TGStype,_Stype>
:TensorContainer<_Ttype> (first_row,num,tp),
nr(num),nv(tp.nv),maxdim(tp.maxdim){}
/*4:*/
#line 162 "./t_polynomial.hweb"
TensorPolynomial(const TensorPolynomial<_Ttype,_TGStype,_Stype> &tp,const Vector&xval)
:TensorContainer<_Ttype> (1),
@ -58,7 +53,6 @@ IntSequence ss(2);ss[0]= xval.length();ss[1]= nvars();
IntSequence pp(2);pp[0]= 0;pp[1]= 1;
/*5:*/
#line 193 "./t_polynomial.hweb"
PowerProvider pwp(xval);
for(int i= 1;i<=tp.maxdim;i++){
@ -66,7 +60,6 @@ const _Stype&xpow= pwp.getNext((const _Stype*)NULL);
for(int j= 0;j<=tp.maxdim-i;j++){
if(tp.check(Symmetry(i+j))){
/*7:*/
#line 227 "./t_polynomial.hweb"
_Ttype*ten;
if(_Tparent::check(Symmetry(j))){
@ -79,7 +72,6 @@ insert(ten);
/*:7*/
#line 199 "./t_polynomial.hweb"
;
Symmetry sym(i,j);
IntSequence coor(sym,pp);
@ -92,15 +84,12 @@ slice.contractAndAdd(0,tmp,xpow);
}
/*:5*/
#line 172 "./t_polynomial.hweb"
;
/*6:*/
#line 214 "./t_polynomial.hweb"
for(int j= 0;j<=tp.maxdim;j++){
if(tp.check(Symmetry(j))){
/*7:*/
#line 227 "./t_polynomial.hweb"
_Ttype*ten;
if(_Tparent::check(Symmetry(j))){
@ -113,7 +102,6 @@ insert(ten);
/*:7*/
#line 217 "./t_polynomial.hweb"
;
Symmetry sym(0,j);
IntSequence coor(sym,pp);
@ -124,12 +112,10 @@ ten->add(1.0,slice);
/*:6*/
#line 173 "./t_polynomial.hweb"
;
}
/*:4*/
#line 129 "./t_polynomial.hweb"
;
TensorPolynomial(const TensorPolynomial&tp)
:TensorContainer<_Ttype> (tp),nr(tp.nr),nv(tp.nv),maxdim(tp.maxdim){}
@ -138,7 +124,6 @@ int nrows()const
int nvars()const
{return nv;}
/*8:*/
#line 242 "./t_polynomial.hweb"
void evalTrad(Vector&out,const ConstVector&v)const
{
@ -159,10 +144,8 @@ t->multaVec(out,p.getData());
}
/*:8*/
#line 136 "./t_polynomial.hweb"
;
/*9:*/
#line 264 "./t_polynomial.hweb"
void evalHorner(Vector&out,const ConstVector&v)const
{
@ -196,10 +179,8 @@ delete last;
}
/*:9*/
#line 137 "./t_polynomial.hweb"
;
/*10:*/
#line 299 "./t_polynomial.hweb"
void insert(_ptr t)
{
@ -213,10 +194,8 @@ maxdim= t->dimen();
}
/*:10*/
#line 138 "./t_polynomial.hweb"
;
/*11:*/
#line 323 "./t_polynomial.hweb"
void derivative(int k)
{
@ -229,10 +208,8 @@ ten->mult((double)max((d-k),0));
}
/*:11*/
#line 139 "./t_polynomial.hweb"
;
/*12:*/
#line 345 "./t_polynomial.hweb"
_Ttype*evalPartially(int s,const ConstVector&v)
{
@ -268,16 +245,13 @@ return res;
}
/*:12*/
#line 140 "./t_polynomial.hweb"
;
};
/*:3*/
#line 54 "./t_polynomial.hweb"
;
/*13:*/
#line 380 "./t_polynomial.hweb"
class FTensorPolynomial;
class UTensorPolynomial:public TensorPolynomial<UFSTensor,UGSTensor,URSingleTensor> {
@ -294,10 +268,8 @@ UTensorPolynomial(int first_row,int num,UTensorPolynomial&tp)
};
/*:13*/
#line 55 "./t_polynomial.hweb"
;
/*14:*/
#line 396 "./t_polynomial.hweb"
class FTensorPolynomial:public TensorPolynomial<FFSTensor,FGSTensor,FRSingleTensor> {
public:
@ -313,16 +285,13 @@ FTensorPolynomial(int first_row,int num,FTensorPolynomial&tp)
};
/*:14*/
#line 56 "./t_polynomial.hweb"
;
/*15:*/
#line 414 "./t_polynomial.hweb"
template<class _Ttype,class _TGStype,class _Stype>
class CompactPolynomial:public _Ttype{
public:
/*16:*/
#line 434 "./t_polynomial.hweb"
CompactPolynomial(const TensorPolynomial<_Ttype,_TGStype,_Stype> &pol)
:_Ttype(pol.nrows(),pol.nvars()+1,pol.getMaxDim())
@ -349,10 +318,8 @@ offset+= dumgs.ncols();
/*:16*/
#line 418 "./t_polynomial.hweb"
;
/*17:*/
#line 463 "./t_polynomial.hweb"
void eval(Vector&out,const ConstVector&v)const
{
@ -378,15 +345,12 @@ multVec(0.0,out,1.0,xpow);
}
/*:17*/
#line 419 "./t_polynomial.hweb"
;
};
/*:15*/
#line 57 "./t_polynomial.hweb"
;
/*18:*/
#line 488 "./t_polynomial.hweb"
class UCompactPolynomial:public CompactPolynomial<UFSTensor,UGSTensor,URSingleTensor> {
public:
@ -395,10 +359,8 @@ UCompactPolynomial(const UTensorPolynomial&upol)
};
/*:18*/
#line 58 "./t_polynomial.hweb"
;
/*19:*/
#line 496 "./t_polynomial.hweb"
class FCompactPolynomial:public CompactPolynomial<FFSTensor,FGSTensor,FRSingleTensor> {
public:
@ -409,7 +371,6 @@ FCompactPolynomial(const FTensorPolynomial&fpol)
/*:19*/
#line 59 "./t_polynomial.hweb"
;
/*:1*/

View File

@ -1,184 +1,163 @@
/*1:*/
#line 6 "./tensor.cweb"
#include "tensor.h"
#include "tl_exception.h"
#include "tl_static.h"
/*2:*/
#line 27 "./tensor.cweb"
int Tensor::noverk(int n,int k)
{
return tls.ptriang->noverk(n,k);
return tls.ptriang->noverk(n,k);
}
int Tensor::power(int a,int b)
{
int res= 1;
for(int i= 0;i<b;i++)
res*= a;
return res;
int res= 1;
for(int i= 0;i<b;i++)
res*= a;
return res;
}
/*:2*/
#line 11 "./tensor.cweb"
;
/*3:*/
#line 51 "./tensor.cweb"
int Tensor::noverseq_ip(IntSequence&s)
{
if(s.size()==0||s.size()==1)
return 1;
s[1]+= s[0];
return noverk(s[1],s[0])*noverseq(IntSequence(s,1,s.size()));
if(s.size()==0||s.size()==1)
return 1;
s[1]+= s[0];
return noverk(s[1],s[0])*noverseq(IntSequence(s,1,s.size()));
}
/*:3*/
#line 12 "./tensor.cweb"
;
/*4:*/
#line 65 "./tensor.cweb"
void UTensor::increment(IntSequence&v,int nv)
{
if(v.size()==0)
return;
int i= v.size()-1;
v[i]++;
while(i> 0&&v[i]==nv){
v[i]= 0;
v[--i]++;
}
if(v.size()==0)
return;
int i= v.size()-1;
v[i]++;
while(i> 0&&v[i]==nv){
v[i]= 0;
v[--i]++;
}
}
/*:4*/
#line 13 "./tensor.cweb"
;
/*5:*/
#line 80 "./tensor.cweb"
void UTensor::decrement(IntSequence&v,int nv)
{
if(v.size()==0)
return;
int i= v.size()-1;
v[i]--;
while(i> 0&&v[i]==-1){
v[i]= nv-1;
v[--i]--;
}
if(v.size()==0)
return;
int i= v.size()-1;
v[i]--;
while(i> 0&&v[i]==-1){
v[i]= nv-1;
v[--i]--;
}
}
/*:5*/
#line 14 "./tensor.cweb"
;
/*6:*/
#line 99 "./tensor.cweb"
void UTensor::increment(IntSequence&v,const IntSequence&nvmx)
{
if(v.size()==0)
return;
int i= v.size()-1;
v[i]++;
while(i> 0&&v[i]==nvmx[i]){
v[i]= 0;
v[--i]++;
}
if(v.size()==0)
return;
int i= v.size()-1;
v[i]++;
while(i> 0&&v[i]==nvmx[i]){
v[i]= 0;
v[--i]++;
}
}
/*:6*/
#line 15 "./tensor.cweb"
;
/*7:*/
#line 115 "./tensor.cweb"
void UTensor::decrement(IntSequence&v,const IntSequence&nvmx)
{
if(v.size()==0)
return;
int i= v.size()-1;
v[i]--;
while(i> 0&&v[i]==-1){
v[i]= nvmx[i]-1;
v[--i]--;
}
if(v.size()==0)
return;
int i= v.size()-1;
v[i]--;
while(i> 0&&v[i]==-1){
v[i]= nvmx[i]-1;
v[--i]--;
}
}
/*:7*/
#line 16 "./tensor.cweb"
;
/*8:*/
#line 131 "./tensor.cweb"
int UTensor::getOffset(const IntSequence&v,int nv)
{
int pow= 1;
int res= 0;
for(int i= v.size()-1;i>=0;i--){
res+= v[i]*pow;
pow*= nv;
}
return res;
int pow= 1;
int res= 0;
for(int i= v.size()-1;i>=0;i--){
res+= v[i]*pow;
pow*= nv;
}
return res;
}
/*:8*/
#line 17 "./tensor.cweb"
;
/*9:*/
#line 144 "./tensor.cweb"
int UTensor::getOffset(const IntSequence&v,const IntSequence&nvmx)
{
int pow= 1;
int res= 0;
for(int i= v.size()-1;i>=0;i--){
res+= v[i]*pow;
pow*= nvmx[i];
}
return res;
int pow= 1;
int res= 0;
for(int i= v.size()-1;i>=0;i--){
res+= v[i]*pow;
pow*= nvmx[i];
}
return res;
}
/*:9*/
#line 18 "./tensor.cweb"
;
/*10:*/
#line 164 "./tensor.cweb"
void FTensor::decrement(IntSequence&v,int nv)
{
int i= v.size()-1;
while(i> 0&&v[i-1]==v[i])
i--;
v[i]--;
for(int j= i+1;j<v.size();j++)
v[j]= nv-1;
int i= v.size()-1;
while(i> 0&&v[i-1]==v[i])
i--;
v[i]--;
for(int j= i+1;j<v.size();j++)
v[j]= nv-1;
}
/*:10*/
#line 19 "./tensor.cweb"
;
/*11:*/
#line 215 "./tensor.cweb"
int FTensor::getOffsetRecurse(IntSequence&v,int nv)
{
if(v.size()==0)return 0;
int prefix= v.getPrefixLength();
int m= v[0];
int k= v.size();
int s1= noverk(nv+k-1,k)-noverk(nv-m+k-1,k);
IntSequence subv(v,prefix,k);
subv.add(-m);
int s2= getOffsetRecurse(subv,nv-m);
return s1+s2;
if(v.size()==0)return 0;
int prefix= v.getPrefixLength();
int m= v[0];
int k= v.size();
int s1= noverk(nv+k-1,k)-noverk(nv-m+k-1,k);
IntSequence subv(v,prefix,k);
subv.add(-m);
int s2= getOffsetRecurse(subv,nv-m);
return s1+s2;
}
/*:11*/
#line 20 "./tensor.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 47 "./tensor.hweb"
#ifndef TENSOR_H
@ -9,7 +8,6 @@
#include "twod_matrix.h"
/*2:*/
#line 89 "./tensor.hweb"
template<class _Tptr> class _index{
typedef _index<_Tptr> _Self;
@ -45,10 +43,8 @@ void print()const
};
/*:2*/
#line 55 "./tensor.hweb"
;
/*3:*/
#line 143 "./tensor.hweb"
class Tensor:public TwoDMatrix{
public:
@ -104,10 +100,8 @@ static int noverseq_ip(IntSequence&s);
};
/*:3*/
#line 56 "./tensor.hweb"
;
/*4:*/
#line 203 "./tensor.hweb"
class FTensor;
class UTensor:public Tensor{
@ -130,10 +124,8 @@ static int getOffset(const IntSequence&v,const IntSequence&nvmx);
};
/*:4*/
#line 57 "./tensor.hweb"
;
/*5:*/
#line 233 "./tensor.hweb"
class FTensor:public Tensor{
public:
@ -154,7 +146,6 @@ static int getOffsetRecurse(IntSequence&v,int nv);
};
/*:5*/
#line 58 "./tensor.hweb"
;
#endif

View File

@ -1,5 +1,4 @@
/*1:*/
#line 14 "./tl_exception.hweb"
#ifndef TL_EXCEPTION_H
#define TL_EXCEPTION_H
@ -8,7 +7,6 @@
#include <stdio.h>
/*2:*/
#line 43 "./tl_exception.hweb"
#ifndef TL_DEBUG_EXCEPTION
#define TL_DEBUG_EXCEPTION 1
@ -25,7 +23,6 @@ if (TL_DEBUG >= TL_DEBUG_EXCEPTION) throw TLException(__FILE__, __LINE__, mes);
if (TL_DEBUG >= TL_DEBUG_EXCEPTION && (expr)) throw TLException(__FILE__, __LINE__, mes);
/*3:*/
#line 61 "./tl_exception.hweb"
class TLException{
char fname[50];
@ -45,11 +42,9 @@ virtual void print()const
/*:3*/
#line 58 "./tl_exception.hweb"
;
/*:2*/
#line 21 "./tl_exception.hweb"
;
#endif

View File

@ -1,88 +1,81 @@
/*1:*/
#line 5 "./tl_static.cweb"
#include "tl_static.h"
#include "tl_exception.h"
TLStatic tls;
/*2:*/
#line 17 "./tl_static.cweb"
TLStatic::TLStatic()
{
ebundle= NULL;
pbundle= NULL;
ptriang= NULL;
ebundle= NULL;
pbundle= NULL;
ptriang= NULL;
}
TLStatic::~TLStatic()
{
if(ebundle)
delete ebundle;
if(pbundle)
delete pbundle;
if(ptriang)
delete ptriang;
if(ebundle)
delete ebundle;
if(pbundle)
delete pbundle;
if(ptriang)
delete ptriang;
}
void TLStatic::init(int dim,int nvar)
{
if(ebundle)
ebundle->generateUpTo(dim);
else
ebundle= new EquivalenceBundle(dim);
if(pbundle)
pbundle->generateUpTo(dim);
else
pbundle= new PermutationBundle(dim);
if(ptriang)
delete ptriang;
ptriang= new PascalTriangle(nvar,dim);
if(ebundle)
ebundle->generateUpTo(dim);
else
ebundle= new EquivalenceBundle(dim);
if(pbundle)
pbundle->generateUpTo(dim);
else
pbundle= new PermutationBundle(dim);
if(ptriang)
delete ptriang;
ptriang= new PascalTriangle(nvar,dim);
}
/*:2*/
#line 10 "./tl_static.cweb"
;
/*3:*/
#line 59 "./tl_static.cweb"
PascalTriangle::PascalTriangle(int n,int k)
:data(new int[(n+1)*(k+1)]),kmax(k),nmax(n)
{
for(int i= 0;i<=n;i++)
data[i]= 1;
for(int j= 1;j<=k;j++){
data[j*(nmax+1)]= 1;
for(int i= 1;i<=n;i++)
data[j*(nmax+1)+i]= noverk(i+j-1,j)+noverk(i+j-1,j-1);
}
for(int i= 0;i<=n;i++)
data[i]= 1;
for(int j= 1;j<=k;j++){
data[j*(nmax+1)]= 1;
for(int i= 1;i<=n;i++)
data[j*(nmax+1)+i]= noverk(i+j-1,j)+noverk(i+j-1,j-1);
}
}
/*:3*/
#line 11 "./tl_static.cweb"
;
/*4:*/
#line 73 "./tl_static.cweb"
int PascalTriangle::noverk(int n,int k)const
{
TL_RAISE_IF(k> n||n<0,
"Wrong arguments for PascalTriangle::noverk");
if(k<=kmax&&n-k<=nmax)
return data[k*(nmax+1)+n-k];
if(n-k<=kmax&&k<=nmax)
return data[(n-k)*(nmax+1)+k];
TL_RAISE("n or k out of range in PascalTriangle::noverk");
return 0;
TL_RAISE_IF(k> n||n<0,
"Wrong arguments for PascalTriangle::noverk");
if(k<=kmax&&n-k<=nmax)
return data[k*(nmax+1)+n-k];
if(n-k<=kmax&&k<=nmax)
return data[(n-k)*(nmax+1)+k];
TL_RAISE("n or k out of range in PascalTriangle::noverk");
return 0;
}
/*:4*/
#line 12 "./tl_static.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 19 "./tl_static.hweb"
#ifndef TL_STATIC_H
#define TL_STATIC_H
@ -8,39 +7,35 @@
#include "permutation.h"
/*2:*/
#line 36 "./tl_static.hweb"
class PascalTriangle{
int*data;
int kmax;
int nmax;
int*data;
int kmax;
int nmax;
public:
PascalTriangle(int n,int k);
~PascalTriangle()
{delete[]data;}
int noverk(int n,int k)const;
PascalTriangle(int n,int k);
~PascalTriangle()
{delete[]data;}
int noverk(int n,int k)const;
};
/*:2*/
#line 26 "./tl_static.hweb"
;
/*3:*/
#line 50 "./tl_static.hweb"
struct TLStatic{
EquivalenceBundle*ebundle;
PermutationBundle*pbundle;
PascalTriangle*ptriang;
TLStatic();
~TLStatic();
void init(int dim,int nvar);
EquivalenceBundle*ebundle;
PermutationBundle*pbundle;
PascalTriangle*ptriang;
TLStatic();
~TLStatic();
void init(int dim,int nvar);
};
/*:3*/
#line 27 "./tl_static.hweb"
;
extern TLStatic tls;

View File

@ -1,12 +1,10 @@
/*1:*/
#line 5 "./twod_matrix.cweb"
#include "twod_matrix.h"
#include "tl_exception.h"
/*2:*/
#line 20 "./twod_matrix.cweb"
ConstTwoDMatrix::ConstTwoDMatrix(const TwoDMatrix&m)
:ConstGeneralMatrix(m){}
@ -24,98 +22,88 @@ ConstTwoDMatrix::ConstTwoDMatrix(int first_row,int num,const ConstTwoDMatrix&m)
:ConstGeneralMatrix(m,first_row,0,num,m.ncols()){}
/*:2*/
#line 10 "./twod_matrix.cweb"
;
/*3:*/
#line 37 "./twod_matrix.cweb"
void ConstTwoDMatrix::writeMat4(FILE*fd,const char*vname)const
{
Mat4Header header(*this,vname);
header.write(fd);
for(int j= 0;j<ncols();j++)
for(int i= 0;i<nrows();i++)
fwrite(&(get(i,j)),sizeof(double),1,fd);
Mat4Header header(*this,vname);
header.write(fd);
for(int j= 0;j<ncols();j++)
for(int i= 0;i<nrows();i++)
fwrite(&(get(i,j)),sizeof(double),1,fd);
}
/*:3*/
#line 11 "./twod_matrix.cweb"
;
/*4:*/
#line 48 "./twod_matrix.cweb"
void TwoDMatrix::copyRow(int from,int to)
{
if(from!=to)
copyRow(ConstTwoDMatrix(*this),from,to);
if(from!=to)
copyRow(ConstTwoDMatrix(*this),from,to);
}
void TwoDMatrix::copyRow(const ConstTwoDMatrix&m,int from,int to)
{
ConstVector fr_row(from,m);
Vector to_row(to,*this);
to_row= fr_row;
ConstVector fr_row(from,m);
Vector to_row(to,*this);
to_row= fr_row;
}
void TwoDMatrix::addRow(double d,const ConstTwoDMatrix&m,int from,int to)
{
ConstVector fr_row(from,m);
Vector to_row(to,*this);
to_row.add(d,fr_row);
ConstVector fr_row(from,m);
Vector to_row(to,*this);
to_row.add(d,fr_row);
}
/*:4*/
#line 12 "./twod_matrix.cweb"
;
/*5:*/
#line 71 "./twod_matrix.cweb"
void TwoDMatrix::copyColumn(int from,int to)
{
if(from!=to)
copyColumn(ConstTwoDMatrix(*this),from,to);
if(from!=to)
copyColumn(ConstTwoDMatrix(*this),from,to);
}
void TwoDMatrix::copyColumn(const ConstTwoDMatrix&m,int from,int to)
{
ConstVector fr_col(m,from);
Vector to_col(*this,to);
to_col= fr_col;
ConstVector fr_col(m,from);
Vector to_col(*this,to);
to_col= fr_col;
}
void TwoDMatrix::addColumn(double d,const ConstTwoDMatrix&m,int from,int to)
{
ConstVector fr_col(m,from);
Vector to_col(*this,to);
to_col.add(d,fr_col);
ConstVector fr_col(m,from);
Vector to_col(*this,to);
to_col.add(d,fr_col);
}
/*:5*/
#line 13 "./twod_matrix.cweb"
;
/*6:*/
#line 93 "./twod_matrix.cweb"
void TwoDMatrix::save(const char*fname)const
{
FILE*fd;
if(NULL==(fd= fopen(fname,"w"))){
TL_RAISE("Cannot open file for writing in TwoDMatrix::save");
}
for(int row= 0;row<nrows();row++){
for(int col= 0;col<ncols();col++)
fprintf(fd," %20.10g",get(row,col));
fprintf(fd,"\n");
}
fclose(fd);
FILE*fd;
if(NULL==(fd= fopen(fname,"w"))){
TL_RAISE("Cannot open file for writing in TwoDMatrix::save");
}
for(int row= 0;row<nrows();row++){
for(int col= 0;col<ncols();col++)
fprintf(fd," %20.10g",get(row,col));
fprintf(fd,"\n");
}
fclose(fd);
}
/*:6*/
#line 14 "./twod_matrix.cweb"
;
/*7:*/
#line 109 "./twod_matrix.cweb"
Mat4Header::Mat4Header(const ConstTwoDMatrix&m,const char*vn)
:type(0),rows(m.nrows()),cols(m.ncols()),imagf(0),namelen(strlen(vn)+1),
@ -124,10 +112,8 @@ vname(vn)
/*:7*/
#line 15 "./twod_matrix.cweb"
;
/*8:*/
#line 117 "./twod_matrix.cweb"
Mat4Header::Mat4Header(const ConstTwoDMatrix&m,const char*vn,const char*dummy)
:type(1),rows(m.nrows()),cols(m.ncols()),imagf(0),namelen(strlen(vn)+1),
@ -136,24 +122,21 @@ vname(vn)
/*:8*/
#line 16 "./twod_matrix.cweb"
;
/*9:*/
#line 125 "./twod_matrix.cweb"
void Mat4Header::write(FILE*fd)const
{
fwrite(&type,sizeof(int),1,fd);
fwrite(&rows,sizeof(int),1,fd);
fwrite(&cols,sizeof(int),1,fd);
fwrite(&imagf,sizeof(int),1,fd);
fwrite(&namelen,sizeof(int),1,fd);
fwrite(vname,1,namelen,fd);
fwrite(&type,sizeof(int),1,fd);
fwrite(&rows,sizeof(int),1,fd);
fwrite(&cols,sizeof(int),1,fd);
fwrite(&imagf,sizeof(int),1,fd);
fwrite(&namelen,sizeof(int),1,fd);
fwrite(vname,1,namelen,fd);
}
/*:9*/
#line 17 "./twod_matrix.cweb"
;
/*:1*/

View File

@ -1,5 +1,4 @@
/*1:*/
#line 23 "./twod_matrix.hweb"
#ifndef TWOD_MATRIX_H
#define TWOD_MATRIX_H
@ -10,132 +9,122 @@
class TwoDMatrix;
/*2:*/
#line 43 "./twod_matrix.hweb"
class ConstTwoDMatrix:public ConstGeneralMatrix{
public:
ConstTwoDMatrix(int m,int n,const double*d)
:ConstGeneralMatrix(d,m,n){}
ConstTwoDMatrix(const TwoDMatrix&m);
ConstTwoDMatrix(const TwoDMatrix&m,int first_col,int num);
ConstTwoDMatrix(const ConstTwoDMatrix&m,int first_col,int num);
ConstTwoDMatrix(int first_row,int num,const TwoDMatrix&m);
ConstTwoDMatrix(int first_row,int num,const ConstTwoDMatrix&m);
ConstTwoDMatrix(const ConstTwoDMatrix&m,int first_row,int first_col,int rows,int cols)
:ConstGeneralMatrix(m,first_row,first_col,rows,cols){}
virtual~ConstTwoDMatrix(){}
int nrows()const
{return numRows();}
int ncols()const
{return numCols();}
void writeMat4(FILE*fd,const char*vname)const;
ConstTwoDMatrix(int m,int n,const double*d)
:ConstGeneralMatrix(d,m,n){}
ConstTwoDMatrix(const TwoDMatrix&m);
ConstTwoDMatrix(const TwoDMatrix&m,int first_col,int num);
ConstTwoDMatrix(const ConstTwoDMatrix&m,int first_col,int num);
ConstTwoDMatrix(int first_row,int num,const TwoDMatrix&m);
ConstTwoDMatrix(int first_row,int num,const ConstTwoDMatrix&m);
ConstTwoDMatrix(const ConstTwoDMatrix&m,int first_row,int first_col,int rows,int cols)
:ConstGeneralMatrix(m,first_row,first_col,rows,cols){}
virtual~ConstTwoDMatrix(){}
int nrows()const
{return numRows();}
int ncols()const
{return numCols();}
void writeMat4(FILE*fd,const char*vname)const;
};
/*:2*/
#line 32 "./twod_matrix.hweb"
;
/*3:*/
#line 70 "./twod_matrix.hweb"
class TwoDMatrix:public GeneralMatrix{
public:
TwoDMatrix(int r,int c)
:GeneralMatrix(r,c){}
TwoDMatrix(int r,int c,double*d)
:GeneralMatrix(d,r,c){}
TwoDMatrix(int r,int c,const double*d)
:GeneralMatrix(d,r,c){}
TwoDMatrix(const GeneralMatrix&m)
:GeneralMatrix(m){}
TwoDMatrix(const GeneralMatrix&m,char*dummy)
:GeneralMatrix(m,dummy){}
TwoDMatrix(const TwoDMatrix&m,int first_col,int num)
:GeneralMatrix(m,0,first_col,m.numRows(),num){}
TwoDMatrix(TwoDMatrix&m,int first_col,int num)
:GeneralMatrix(m,0,first_col,m.numRows(),num){}
TwoDMatrix(int first_row,int num,const TwoDMatrix&m)
:GeneralMatrix(m,first_row,0,num,m.ncols()){}
TwoDMatrix(int first_row,int num,TwoDMatrix&m)
:GeneralMatrix(m,first_row,0,num,m.ncols()){}
TwoDMatrix(TwoDMatrix&m,int first_row,int first_col,int rows,int cols)
:GeneralMatrix(m,first_row,first_col,rows,cols){}
TwoDMatrix(const TwoDMatrix&m,int first_row,int first_col,int rows,int cols)
:GeneralMatrix(m,first_row,first_col,rows,cols){}
TwoDMatrix(const ConstTwoDMatrix&a,const ConstTwoDMatrix&b)
:GeneralMatrix(a,b){}
virtual~TwoDMatrix(){}
int nrows()const
{return numRows();}
int ncols()const
{return numCols();}
/*4:*/
#line 112 "./twod_matrix.hweb"
void copyRow(int from,int to);
void copyRow(const ConstTwoDMatrix&m,int from,int to);
void copyRow(const TwoDMatrix&m,int from,int to)
{copyRow(ConstTwoDMatrix(m),from,to);}
void addRow(const ConstTwoDMatrix&m,int from,int to)
{addRow(1.0,m,from,to);}
void addRow(const TwoDMatrix&m,int from,int to)
{addRow(1.0,ConstTwoDMatrix(m),from,to);}
void addRow(double d,const ConstTwoDMatrix&m,int from,int to);
void addRow(double d,const TwoDMatrix&m,int from,int to)
{addRow(d,ConstTwoDMatrix(m),from,to);}
/*:4*/
#line 104 "./twod_matrix.hweb"
;
/*5:*/
#line 127 "./twod_matrix.hweb"
void copyColumn(int from,int to);
void copyColumn(const ConstTwoDMatrix&m,int from,int to);
void copyColumn(const TwoDMatrix&m,int from,int to)
{copyColumn(ConstTwoDMatrix(m),from,to);}
void addColumn(const ConstTwoDMatrix&m,int from,int to)
{addColumn(1.0,ConstTwoDMatrix(m),from,to);}
void addColumn(const TwoDMatrix&m,int from,int to)
{addColumn(1.0,ConstTwoDMatrix(m),from,to);}
void addColumn(double d,const ConstTwoDMatrix&m,int from,int to);
void addColumn(double d,const TwoDMatrix&m,int from,int to)
{addColumn(d,ConstTwoDMatrix(m),from,to);}
/*:5*/
#line 105 "./twod_matrix.hweb"
;
void save(const char*fname)const;
void writeMat4(FILE*fd,const char*vname)const
{ConstTwoDMatrix(*this).writeMat4(fd,vname);}
TwoDMatrix(int r,int c)
:GeneralMatrix(r,c){}
TwoDMatrix(int r,int c,double*d)
:GeneralMatrix(d,r,c){}
TwoDMatrix(int r,int c,const double*d)
:GeneralMatrix(d,r,c){}
TwoDMatrix(const GeneralMatrix&m)
:GeneralMatrix(m){}
TwoDMatrix(const GeneralMatrix&m,char*dummy)
:GeneralMatrix(m,dummy){}
TwoDMatrix(const TwoDMatrix&m,int first_col,int num)
:GeneralMatrix(m,0,first_col,m.numRows(),num){}
TwoDMatrix(TwoDMatrix&m,int first_col,int num)
:GeneralMatrix(m,0,first_col,m.numRows(),num){}
TwoDMatrix(int first_row,int num,const TwoDMatrix&m)
:GeneralMatrix(m,first_row,0,num,m.ncols()){}
TwoDMatrix(int first_row,int num,TwoDMatrix&m)
:GeneralMatrix(m,first_row,0,num,m.ncols()){}
TwoDMatrix(TwoDMatrix&m,int first_row,int first_col,int rows,int cols)
:GeneralMatrix(m,first_row,first_col,rows,cols){}
TwoDMatrix(const TwoDMatrix&m,int first_row,int first_col,int rows,int cols)
:GeneralMatrix(m,first_row,first_col,rows,cols){}
TwoDMatrix(const ConstTwoDMatrix&a,const ConstTwoDMatrix&b)
:GeneralMatrix(a,b){}
virtual~TwoDMatrix(){}
int nrows()const
{return numRows();}
int ncols()const
{return numCols();}
/*4:*/
void copyRow(int from,int to);
void copyRow(const ConstTwoDMatrix&m,int from,int to);
void copyRow(const TwoDMatrix&m,int from,int to)
{copyRow(ConstTwoDMatrix(m),from,to);}
void addRow(const ConstTwoDMatrix&m,int from,int to)
{addRow(1.0,m,from,to);}
void addRow(const TwoDMatrix&m,int from,int to)
{addRow(1.0,ConstTwoDMatrix(m),from,to);}
void addRow(double d,const ConstTwoDMatrix&m,int from,int to);
void addRow(double d,const TwoDMatrix&m,int from,int to)
{addRow(d,ConstTwoDMatrix(m),from,to);}
/*:4*/
;
/*5:*/
void copyColumn(int from,int to);
void copyColumn(const ConstTwoDMatrix&m,int from,int to);
void copyColumn(const TwoDMatrix&m,int from,int to)
{copyColumn(ConstTwoDMatrix(m),from,to);}
void addColumn(const ConstTwoDMatrix&m,int from,int to)
{addColumn(1.0,ConstTwoDMatrix(m),from,to);}
void addColumn(const TwoDMatrix&m,int from,int to)
{addColumn(1.0,ConstTwoDMatrix(m),from,to);}
void addColumn(double d,const ConstTwoDMatrix&m,int from,int to);
void addColumn(double d,const TwoDMatrix&m,int from,int to)
{addColumn(d,ConstTwoDMatrix(m),from,to);}
/*:5*/
;
void save(const char*fname)const;
void writeMat4(FILE*fd,const char*vname)const
{ConstTwoDMatrix(*this).writeMat4(fd,vname);}
};
/*:3*/
#line 33 "./twod_matrix.hweb"
;
/*6:*/
#line 141 "./twod_matrix.hweb"
class Mat4Header{
int type;
int rows;
int cols;
int imagf;
int namelen;
const char*vname;
int type;
int rows;
int cols;
int imagf;
int namelen;
const char*vname;
public:
Mat4Header(const ConstTwoDMatrix&m,const char*vname);
Mat4Header(const ConstTwoDMatrix&m,const char*vname,const char*dummy);
void write(FILE*fd)const;
Mat4Header(const ConstTwoDMatrix&m,const char*vname);
Mat4Header(const ConstTwoDMatrix&m,const char*vname,const char*dummy);
void write(FILE*fd)const;
};
/*:6*/
#line 34 "./twod_matrix.hweb"
;
#endif