📄 ap.cpp
字号:
void ap::vsub(ap::complex *vdst, const ap::complex *vsrc, int N, double alpha)
{
ap::_vsub<ap::complex,double>(vdst, vsrc, N, alpha);
}
void ap::vsub(ap::complex *vdst, const ap::complex *vsrc, int N, ap::complex alpha)
{
ap::_vsub<ap::complex,ap::complex>(vdst, vsrc, N, alpha);
}
void ap::vmul(double *vdst, int N, double alpha)
{
#ifdef AP_WIN32
if( dmuls1!=NULL )
{
dmuls1(vdst, N, alpha);
return;
}
#endif
ap::_vmul<double,double>(vdst, N, alpha);
}
void ap::vmul(ap::complex *vdst, int N, double alpha)
{
ap::_vmul<ap::complex,double>(vdst, N, alpha);
}
void ap::vmul(ap::complex *vdst, int N, ap::complex alpha)
{
ap::_vmul<ap::complex,ap::complex>(vdst, N, alpha);
}
/********************************************************************
standard functions
********************************************************************/
int ap::sign(double x)
{
if( x>0 ) return 1;
if( x<0 ) return -1;
return 0;
}
double ap::randomreal()
{
int i1 = rand();
int i2 = rand();
while(i1==RAND_MAX)
i1 =rand();
while(i2==RAND_MAX)
i2 =rand();
double mx = RAND_MAX;
return (i1+i2/mx)/mx;
}
int ap::randominteger(int maxv)
{ return rand()%maxv; }
int ap::round(double x)
{ return int(floor(x+0.5)); }
int ap::trunc(double x)
{ return int(x>0 ? floor(x) : ceil(x)); }
int ap::ifloor(double x)
{ return int(floor(x)); }
int ap::iceil(double x)
{ return int(ceil(x)); }
double ap::pi()
{ return 3.14159265358979323846; }
double ap::sqr(double x)
{ return x*x; }
int ap::maxint(int m1, int m2)
{
return m1>m2 ? m1 : m2;
}
int ap::minint(int m1, int m2)
{
return m1>m2 ? m2 : m1;
}
double ap::maxreal(double m1, double m2)
{
return m1>m2 ? m1 : m2;
}
double ap::minreal(double m1, double m2)
{
return m1>m2 ? m2 : m1;
}
/********************************************************************
Dataset functions
********************************************************************/
bool ap::readstrings(std::string file, std::list<std::string> *pOutput)
{
return readstrings(file, pOutput, "");
}
bool ap::readstrings(std::string file, std::list<std::string> *pOutput, std::string comment)
{
std::string cmd, s;
FILE *f;
char buf[1024];
char *str;
f = fopen(file.c_str(), "rb");
if( !f )
return false;
s = "";
pOutput->clear();
while( (str=fgets(buf, sizeof(buf), f)) || (s.length()!=0) )
{
if( str!=NULL )
{
s += str;
if( strchr(buf, '\n')==NULL )
continue;
}
if( comment.length()>0 )
if( strncmp(s.c_str(), comment.c_str(), comment.length())==0 )
{
s = "";
continue;
}
if( s.length()<1 )
{
fclose(f);
throw ap::ap_error("internal error in read_strings");
}
if( s.length()==1 )
{
// no empty strings
// pOutput->push_back(std::string(s,0,s.length()-1));
}
if( s.length()>1 )
{
if( s[s.length()-2]=='\r' )
{
if( s.length()>2 ) // no empty strings
pOutput->push_back(std::string(s,0,s.length()-2));
}
else
pOutput->push_back(std::string(s,0,s.length()-1));
}
s = "";
}
fclose(f);
return true;
}
void ap::explodestring(std::string s, char sep, std::vector<std::string> *pOutput)
{
std::string tmp;
int i;
tmp = "";
pOutput->clear();
for(i=0; i<s.length(); i++)
{
if( s[i]!=sep )
{
tmp += s[i];
continue;
}
//if( tmp.length()!=0 )
pOutput->push_back(tmp);
tmp = "";
}
if( tmp.length()!=0 )
pOutput->push_back(tmp);
}
/*bool ap::opendataset(std::string file, dataset *pdataset)
{
std::list<std::string> Lines;
std::vector<std::string> Values;
std::list<std::string>::iterator i;
int nCol, nRow, nSplitted;
int nColumns, nRows;
//
// Read data
//
if( pdataset==NULL )
return false;
if( !readstrings(file, &Lines, "//") )
return false;
i = Lines.begin();
*pdataset = dataset();
//
// Read columns info
//
if( i==Lines.end() )
return false;
if( sscanf(i->c_str(), " columns = %d %d ", &pdataset->nin, &pdataset->nout)!=2 )
return false;
if( pdataset->nin<=0 || pdataset->nout==0 || pdataset->nout==-1)
return false;
if( pdataset->nout<0 )
{
pdataset->nclasses = -pdataset->nout;
pdataset->nout = 1;
pdataset->iscls = true;
}
else
{
pdataset->isreg = true;
}
nColumns = pdataset->nin+pdataset->nout;
i++;
//
// Read rows info
//
if( i==Lines.end() )
return false;
if( sscanf(i->c_str(), " rows = %d %d %d ", &pdataset->trnsize, &pdataset->valsize, &pdataset->tstsize)!=3 )
return false;
if( (pdataset->trnsize<0) || (pdataset->valsize<0) || (pdataset->tstsize<0) )
return false;
if( (pdataset->trnsize==0) && (pdataset->valsize==0) && (pdataset->tstsize==0) )
return false;
nRows = pdataset->trnsize+pdataset->valsize+pdataset->tstsize;
pdataset->size = nRows;
if( Lines.size()!=nRows+2 )
return false;
i++;
//
// Read all cases
//
ap::real_2d_array &arr = pdataset->all;
arr.setbounds(0, nRows-1, 0, nColumns-1);
for(nRow=0; nRow<nRows; nRow++)
{
ap::ap_error::make_assertion(i!=Lines.end());
explodestring(*i, '\t', &Values);
if( Values.size()!=nColumns )
return false;
for(nCol=0; nCol<nColumns; nCol++)
{
double v;
if( sscanf(Values[nCol].c_str(), "%lg", &v)!=1 )
return false;
if( (nCol==nColumns-1) && pdataset->iscls && ((round(v)<0) || (round(v)>=pdataset->nclasses)) )
return false;
if( (nCol==nColumns-1) && pdataset->iscls )
arr(nRow, nCol) = round(v);
else
arr(nRow, nCol) = v;
}
i++;
}
//
// Split to training, validation and test sets
//
if( pdataset->trnsize>0 )
pdataset->trn.setbounds(0, pdataset->trnsize-1, 0, nColumns-1);
if( pdataset->valsize>0 )
pdataset->val.setbounds(0, pdataset->valsize-1, 0, nColumns-1);
if( pdataset->tstsize>0 )
pdataset->tst.setbounds(0, pdataset->tstsize-1, 0, nColumns-1);
nSplitted=0;
for(nRow=0; nRow<=pdataset->trnsize-1; nRow++, nSplitted++)
for(nCol=0; nCol<=nColumns-1; nCol++)
pdataset->trn(nRow,nCol) = arr(nSplitted,nCol);
for(nRow=0; nRow<=pdataset->valsize-1; nRow++, nSplitted++)
for(nCol=0; nCol<=nColumns-1; nCol++)
pdataset->val(nRow,nCol) = arr(nSplitted,nCol);
for(nRow=0; nRow<=pdataset->tstsize-1; nRow++, nSplitted++)
for(nCol=0; nCol<=nColumns-1; nCol++)
pdataset->tst(nRow,nCol) = arr(nSplitted,nCol);
return true;
}*/
/********************************************************************
Service routines:
********************************************************************/
void* ap::amalloc(size_t size, size_t alignment)
{
if( alignment<=1 )
{
//
// no alignment, just call malloc
//
void *block = malloc(sizeof(void*)+size);
void **p = (void**)block;
*p = block;
return (void*)((char*)block+sizeof(void*));
}
else
{
//
// align.
//
void *block = malloc(alignment-1+sizeof(void*)+size);
char *result = (char*)block+sizeof(void*);
//if( ((unsigned int)(result))%alignment!=0 )
// result += alignment - ((unsigned int)(result))%alignment;
if( (result-(char*)0)%alignment!=0 )
result += alignment - (result-(char*)0)%alignment;
*((void**)(result-sizeof(void*))) = block;
return result;
}
}
void ap::afree(void *block)
{
void *p = *((void**)((char*)block-sizeof(void*)));
free(p);
}
int ap::vlen(int n1, int n2)
{
return n2-n1+1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -