public static void cmatrixrndcond(int n,
double c,
ref complex[,] a)
{
int i = 0;
int j = 0;
double l1 = 0;
double l2 = 0;
hqrnd.hqrndstate state = new hqrnd.hqrndstate();
complex v = 0;
a = new complex[0,0];
ap.assert(n>=1 & (double)(c)>=(double)(1), "CMatrixRndCond: N<1 or C<1!");
a = new complex[n, n];
if( n==1 )
{
//
// special case
//
hqrnd.hqrndrandomize(state);
hqrnd.hqrndunit2(state, ref v.x, ref v.y);
a[0,0] = v;
return;
}
l1 = 0;
l2 = Math.Log(1/c);
for(i=0; i<=n-1; i++)
{
for(j=0; j<=n-1; j++)
{
a[i,j] = 0;
}
}
a[0,0] = Math.Exp(l1);
for(i=1; i<=n-2; i++)
{
a[i,i] = Math.Exp(math.randomreal()*(l2-l1)+l1);
}
a[n-1,n-1] = Math.Exp(l2);
cmatrixrndorthogonalfromtheleft(ref a, n, n);
cmatrixrndorthogonalfromtheright(ref a, n, n);
}
开发者ID:Ring-r,项目名称:opt,代码行数:44,代码来源:linalg.cs
示例6: cmatrixrndorthogonalfromtheleft
public static void cmatrixrndorthogonalfromtheleft(ref complex[,] a,
int m,
int n)
{
complex tau = 0;
complex lambdav = 0;
int s = 0;
int i = 0;
int j = 0;
complex[] w = new complex[0];
complex[] v = new complex[0];
hqrnd.hqrndstate state = new hqrnd.hqrndstate();
int i_ = 0;
ap.assert(n>=1 & m>=1, "CMatrixRndOrthogonalFromTheRight: N<1 or M<1!");
if( m==1 )
{
//
// special case
//
hqrnd.hqrndrandomize(state);
hqrnd.hqrndunit2(state, ref tau.x, ref tau.y);
for(j=0; j<=n-1; j++)
{
a[0,j] = a[0,j]*tau;
}
return;
}
//
// General case.
// First pass.
//
w = new complex[n];
v = new complex[m+1];
hqrnd.hqrndrandomize(state);
for(s=2; s<=m; s++)
{
//
// Prepare random normal v
//
do
{
for(i=1; i<=s; i++)
{
hqrnd.hqrndnormal2(state, ref tau.x, ref tau.y);
v[i] = tau;
}
lambdav = 0.0;
for(i_=1; i_<=s;i_++)
{
lambdav += v[i_]*math.conj(v[i_]);
}
}
while( lambdav==0 );
//
// Prepare and apply reflection
//
creflections.complexgeneratereflection(ref v, s, ref tau);
v[1] = 1;
creflections.complexapplyreflectionfromtheleft(ref a, tau, v, m-s, m-1, 0, n-1, ref w);
}
//
// Second pass.
//
for(i=0; i<=m-1; i++)
{
hqrnd.hqrndunit2(state, ref tau.x, ref tau.y);
for(i_=0; i_<=n-1;i_++)
{
a[i,i_] = tau*a[i,i_];
}
}
}
开发者ID:Ring-r,项目名称:opt,代码行数:78,代码来源:linalg.cs
示例7: init
public override void init()
{
bestparameters = new double[0];
network = new mlpbase.multilayerperceptron();
optimizer = new minlbfgs.minlbfgsstate();
optimizerrep = new minlbfgs.minlbfgsreport();
wbuf0 = new double[0];
wbuf1 = new double[0];
allminibatches = new int[0];
currentminibatch = new int[0];
rstate = new rcommstate();
generator = new hqrnd.hqrndstate();
}
/*************************************************************************
This is hash function.
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
private static int hash(int i,
int j,
int tabsize)
{
int result = 0;
hqrnd.hqrndstate r = new hqrnd.hqrndstate();
hqrnd.hqrndseed(i, j, r);
result = hqrnd.hqrnduniformi(r, tabsize);
return result;
}
开发者ID:tablee,项目名称:TabBox,代码行数:17,代码来源:linalg.cs
示例9: dfbuildinternal
public static void dfbuildinternal(double[,] xy,
int npoints,
int nvars,
int nclasses,
int ntrees,
int samplesize,
int nfeatures,
int flags,
ref int info,
decisionforest df,
dfreport rep)
{
int i = 0;
int j = 0;
int k = 0;
int tmpi = 0;
int lasttreeoffs = 0;
int offs = 0;
int ooboffs = 0;
int treesize = 0;
int nvarsinpool = 0;
bool useevs = new bool();
dfinternalbuffers bufs = new dfinternalbuffers();
int[] permbuf = new int[0];
double[] oobbuf = new double[0];
int[] oobcntbuf = new int[0];
double[,] xys = new double[0,0];
double[] x = new double[0];
double[] y = new double[0];
int oobcnt = 0;
int oobrelcnt = 0;
double v = 0;
double vmin = 0;
double vmax = 0;
bool bflag = new bool();
hqrnd.hqrndstate rs = new hqrnd.hqrndstate();
int i_ = 0;
int i1_ = 0;
info = 0;
//
// Test for inputs
//
if( (((((npoints<1 || samplesize<1) || samplesize>npoints) || nvars<1) || nclasses<1) || ntrees<1) || nfeatures<1 )
{
info = -1;
return;
}
if( nclasses>1 )
{
for(i=0; i<=npoints-1; i++)
{
if( (int)Math.Round(xy[i,nvars])<0 || (int)Math.Round(xy[i,nvars])>=nclasses )
{
info = -2;
return;
}
}
}
info = 1;
//
// Flags
//
useevs = flags/dfuseevs%2!=0;
//
// Allocate data, prepare header
//
treesize = 1+innernodewidth*(samplesize-1)+leafnodewidth*samplesize;
permbuf = new int[npoints-1+1];
bufs.treebuf = new double[treesize-1+1];
bufs.idxbuf = new int[npoints-1+1];
bufs.tmpbufr = new double[npoints-1+1];
bufs.tmpbufr2 = new double[npoints-1+1];
bufs.tmpbufi = new int[npoints-1+1];
bufs.sortrbuf = new double[npoints];
bufs.sortrbuf2 = new double[npoints];
bufs.sortibuf = new int[npoints];
bufs.varpool = new int[nvars-1+1];
bufs.evsbin = new bool[nvars-1+1];
bufs.evssplits = new double[nvars-1+1];
bufs.classibuf = new int[2*nclasses-1+1];
oobbuf = new double[nclasses*npoints-1+1];
oobcntbuf = new int[npoints-1+1];
df.trees = new double[ntrees*treesize-1+1];
xys = new double[samplesize-1+1, nvars+1];
x = new double[nvars-1+1];
y = new double[nclasses-1+1];
for(i=0; i<=npoints-1; i++)
{
permbuf[i] = i;
}
for(i=0; i<=npoints*nclasses-1; i++)
{
oobbuf[i] = 0;
}
for(i=0; i<=npoints-1; i++)
//.........这里部分代码省略.........
/*************************************************************************
Randomization of neural network weights
-- ALGLIB --
Copyright 06.11.2007 by Bochkanov Sergey
*************************************************************************/
public static void mlprandomize(multilayerperceptron network)
{
int nin = 0;
int nout = 0;
int wcount = 0;
int ntotal = 0;
int istart = 0;
hqrnd.hqrndstate r = new hqrnd.hqrndstate();
int entrysize = 0;
int entryoffs = 0;
int neuronidx = 0;
int neurontype = 0;
double vmean = 0;
double vvar = 0;
int i = 0;
int n1 = 0;
int n2 = 0;
double desiredsigma = 0;
int montecarlocnt = 0;
double ef = 0;
double ef2 = 0;
double v = 0;
double wscale = 0;
hqrnd.hqrndrandomize(r);
mlpproperties(network, ref nin, ref nout, ref wcount);
ntotal = network.structinfo[3];
istart = network.structinfo[5];
desiredsigma = 0.5;
montecarlocnt = 20;
//
// Stage 1:
// * Network.Weights is filled by standard deviation of weights
// * default values: sigma=1
//
for(i=0; i<=wcount-1; i++)
{
network.weights[i] = 1.0;
}
//
// Stage 2:
// * assume that input neurons have zero mean and unit standard deviation
// * assume that constant neurons have zero standard deviation
// * perform forward pass along neurons
// * for each non-input non-constant neuron:
// * calculate mean and standard deviation of neuron's output
// assuming that we know means/deviations of neurons which feed it
// and assuming that weights has unit variance and zero mean.
// * for each nonlinear neuron additionally we perform backward pass:
// * scale variances of weights which feed it in such way that neuron's
// input has unit standard deviation
//
// NOTE: this algorithm assumes that each connection feeds at most one
// non-linear neuron. This assumption can be incorrect in upcoming
// architectures with strong neurons. However, algorithm should
// work smoothly even in this case.
//
// During this stage we use Network.RndBuf, which is grouped into NTotal
// entries, each of them having following format:
//
// Buf[Offset+0] mean value of neuron's output
// Buf[Offset+1] standard deviation of neuron's output
//
//
//
entrysize = 2;
apserv.rvectorsetlengthatleast(ref network.rndbuf, entrysize*ntotal);
for(neuronidx=0; neuronidx<=ntotal-1; neuronidx++)
{
neurontype = network.structinfo[istart+neuronidx*nfieldwidth+0];
entryoffs = entrysize*neuronidx;
if( neurontype==-2 )
{
//
// Input neuron: zero mean, unit variance.
//
network.rndbuf[entryoffs+0] = 0.0;
network.rndbuf[entryoffs+1] = 1.0;
continue;
}
if( neurontype==-3 )
{
//
// "-1" neuron: mean=-1, zero variance.
//
network.rndbuf[entryoffs+0] = -1.0;
network.rndbuf[entryoffs+1] = 0.0;
continue;
}
if( neurontype==-4 )
//.........这里部分代码省略.........
/*************************************************************************
This function selects initial centers according to specified initialization
algorithm.
IMPORTANT: this function provides no guarantees regarding selection of
DIFFERENT centers. Centers returned by this function may
include duplicates (say, when random sampling is used). It is
also possible that some centers are empty.
Algorithm which uses this function must be able to deal with it.
Say, you may want to use FixCenters() in order to fix empty centers.
INPUT PARAMETERS:
XY - dataset, array [0..NPoints-1,0..NVars-1].
NPoints - points count
NVars - number of variables, NVars>=1
InitAlgo - initialization algorithm:
* 0 - automatic selection of best algorithm
* 1 - random selection
* 2 - k-means++
* 3 - fast-greedy init
*-1 - first K rows of dataset are used (debug algorithm)
K - number of centers, K>=1
CT - possibly preallocated output buffer, resized if needed
InitBuf - internal buffer, possibly unitialized instance of
APBuffers. It is recommended to use this instance only
with SelectInitialCenters() and FixCenters() functions,
because these functions may allocate really large storage.
UpdatePool - shared pool seeded with instance of APBuffers structure
(seed instance can be unitialized). Used internally with
KMeansUpdateDistances() function. It is recommended
to use this pool ONLY with KMeansUpdateDistances()
function.
OUTPUT PARAMETERS:
CT - set of K clusters, one per row
RESULT:
True on success, False on failure (impossible to create K independent clusters)
-- ALGLIB --
Copyright 21.01.2015 by Bochkanov Sergey
*************************************************************************/
private static void selectinitialcenters(double[,] xy,
int npoints,
int nvars,
int initalgo,
int k,
ref double[,] ct,
apserv.apbuffers initbuf,
alglib.smp.shared_pool updatepool)
{
int cidx = 0;
int i = 0;
int j = 0;
double v = 0;
double vv = 0;
double s = 0;
int lastnz = 0;
int ptidx = 0;
int samplesize = 0;
int samplescntnew = 0;
int samplescntall = 0;
double samplescale = 0;
hqrnd.hqrndstate rs = new hqrnd.hqrndstate();
int i_ = 0;
hqrnd.hqrndrandomize(rs);
//
// Check parameters
//
alglib.ap.assert(npoints>0, "SelectInitialCenters: internal error");
alglib.ap.assert(nvars>0, "SelectInitialCenters: internal error");
alglib.ap.assert(k>0, "SelectInitialCenters: internal error");
if( initalgo==0 )
{
initalgo = 3;
}
apserv.rmatrixsetlengthatleast(ref ct, k, nvars);
//
// Random initialization
//
if( initalgo==-1 )
{
for(i=0; i<=k-1; i++)
{
for(i_=0; i_<=nvars-1;i_++)
{
ct[i,i_] = xy[i%npoints,i_];
}
}
return;
}
//
// Random initialization
//
if( initalgo==1 )
{
//.........这里部分代码省略.........
/*************************************************************************
K-means++ clusterization
INPUT PARAMETERS:
XY - dataset, array [0..NPoints-1,0..NVars-1].
NPoints - dataset size, NPoints>=K
NVars - number of variables, NVars>=1
K - desired number of clusters, K>=1
InitAlgo - initialization algorithm:
* 0 - automatic selection of best algorithm
* 1 - random selection of centers
* 2 - k-means++
* 3 - fast-greedy init
*-1 - first K rows of dataset are used
(special debug algorithm)
MaxIts - iterations limit or zero for no limit
Restarts - number of restarts, Restarts>=1
KMeansDbgNoIts- debug flag; if set, Lloyd's iteration is not performed,
only initialization phase.
Buf - special reusable structure which stores previously allocated
memory, intended to avoid memory fragmentation when solving
multiple subsequent problems:
* MUST BE INITIALIZED WITH KMeansInitBuffers() CALL BEFORE
FIRST PASS TO THIS FUNCTION!
* subsequent passes must be made without re-initialization
OUTPUT PARAMETERS:
Info - return code:
* -3, if task is degenerate (number of distinct points is
less than K)
* -1, if incorrect NPoints/NFeatures/K/Restarts was passed
* 1, if subroutine finished successfully
IterationsCount- actual number of iterations performed by clusterizer
CCol - array[0..NVars-1,0..K-1].matrix whose columns store
cluster's centers
NeedCCol - True in case caller requires to store result in CCol
CRow - array[0..K-1,0..NVars-1], same as CCol, but centers are
stored in rows
NeedCRow - True in case caller requires to store result in CCol
XYC - array[NPoints], which contains cluster indexes
Energy - merit function of clusterization
-- ALGLIB --
Copyright 21.03.2009 by Bochkanov Sergey
*************************************************************************/
public static void kmeansgenerateinternal(double[,] xy,
int npoints,
int nvars,
int k,
int initalgo,
int maxits,
int restarts,
bool kmeansdbgnoits,
ref int info,
ref int iterationscount,
ref double[,] ccol,
bool needccol,
ref double[,] crow,
bool needcrow,
ref int[] xyc,
ref double energy,
kmeansbuffers buf)
{
int i = 0;
int j = 0;
int i1 = 0;
double e = 0;
double eprev = 0;
double v = 0;
double vv = 0;
bool waschanges = new bool();
bool zerosizeclusters = new bool();
int pass = 0;
int itcnt = 0;
hqrnd.hqrndstate rs = new hqrnd.hqrndstate();
int i_ = 0;
info = 0;
iterationscount = 0;
ccol = new double[0,0];
crow = new double[0,0];
xyc = new int[0];
energy = 0;
//
// Test parameters
//
if( ((npoints<k || nvars<1) || k<1) || restarts<1 )
{
info = -1;
iterationscount = 0;
return;
}
//
// TODO: special case K=1
// TODO: special case K=NPoints
//
info = 1;
//.........这里部分代码省略.........
/*************************************************************************
K-means++ clusterization
INPUT PARAMETERS:
XY - dataset, array [0..NPoints-1,0..NVars-1].
NPoints - dataset size, NPoints>=K
NVars - number of variables, NVars>=1
K - desired number of clusters, K>=1
Restarts - number of restarts, Restarts>=1
OUTPUT PARAMETERS:
Info - return code:
* -3, if task is degenerate (number of distinct points is
less than K)
* -1, if incorrect NPoints/NFeatures/K/Restarts was passed
* 1, if subroutine finished successfully
CCol - array[0..NVars-1,0..K-1].matrix whose columns store
cluster's centers
NeedCCol - True in case caller requires to store result in CCol
CRow - array[0..K-1,0..NVars-1], same as CCol, but centers are
stored in rows
NeedCRow - True in case caller requires to store result in CCol
XYC - array[NPoints], which contains cluster indexes
-- ALGLIB --
Copyright 21.03.2009 by Bochkanov Sergey
*************************************************************************/
public static void kmeansgenerateinternal(double[,] xy,
int npoints,
int nvars,
int k,
int maxits,
int restarts,
ref int info,
ref double[,] ccol,
bool needccol,
ref double[,] crow,
bool needcrow,
ref int[] xyc)
{
int i = 0;
int j = 0;
double[,] ct = new double[0, 0];
double[,] ctbest = new double[0, 0];
int[] xycbest = new int[0];
double e = 0;
double eprev = 0;
double ebest = 0;
double[] x = new double[0];
double[] tmp = new double[0];
double[] d2 = new double[0];
double[] p = new double[0];
int[] csizes = new int[0];
bool[] cbusy = new bool[0];
double v = 0;
int cclosest = 0;
double dclosest = 0;
double[] work = new double[0];
bool waschanges = new bool();
bool zerosizeclusters = new bool();
int pass = 0;
int itcnt = 0;
hqrnd.hqrndstate rs = new hqrnd.hqrndstate();
int i_ = 0;
info = 0;
ccol = new double[0, 0];
crow = new double[0, 0];
xyc = new int[0];
//
// Test parameters
//
if (((npoints < k || nvars < 1) || k < 1) || restarts < 1)
{
info = -1;
return;
}
//
// TODO: special case K=1
// TODO: special case K=NPoints
//
info = 1;
//
// Multiple passes of k-means++ algorithm
//
ct = new double[k, nvars];
ctbest = new double[k, nvars];
xyc = new int[npoints];
xycbest = new int[npoints];
d2 = new double[npoints];
p = new double[npoints];
tmp = new double[nvars];
csizes = new int[k];
cbusy = new bool[k];
ebest = math.maxrealnumber;
hqrnd.hqrndrandomize(rs);
//.........这里部分代码省略.........
//.........这里部分代码省略.........
position
RESULT:
If the matrix is positive-definite, the function returns True.
Otherwise, the function returns False. Contents of C is not determined
in such case.
NOTE: for performance reasons this function does NOT check that input
matrix includes only finite values. It is your responsibility to
make sure that there are no infinite or NAN values in the matrix.
-- ALGLIB routine --
16.01.2014
Bochkanov Sergey
*************************************************************************/
public static bool sparsecholeskyx(sparse.sparsematrix a,
int n,
bool isupper,
ref int[] p0,
ref int[] p1,
int ordering,
int algo,
int fmt,
sparse.sparsebuffers buf,
sparse.sparsematrix c)
{
bool result = new bool();
int i = 0;
int j = 0;
int k = 0;
int t0 = 0;
int t1 = 0;
double v = 0;
hqrnd.hqrndstate rs = new hqrnd.hqrndstate();
alglib.ap.assert(n>=0, "SparseMatrixCholeskyBuf: N<0");
alglib.ap.assert(sparse.sparsegetnrows(a)>=n, "SparseMatrixCholeskyBuf: rows(A)<N");
alglib.ap.assert(sparse.sparsegetncols(a)>=n, "SparseMatrixCholeskyBuf: cols(A)<N");
alglib.ap.assert(ordering>=-3 && ordering<=0, "SparseMatrixCholeskyBuf: invalid Ordering parameter");
alglib.ap.assert(algo>=0 && algo<=2, "SparseMatrixCholeskyBuf: invalid Algo parameter");
hqrnd.hqrndrandomize(rs);
//
// Perform some quick checks.
// Because sparse matrices are expensive data structures, these
// checks are better to perform during early stages of the factorization.
//
result = false;
if( n<1 )
{
return result;
}
for(i=0; i<=n-1; i++)
{
if( (double)(sparse.sparsegetdiagonal(a, i))<=(double)(0) )
{
return result;
}
}
//
// First, determine appropriate ordering:
// * for SKS inputs, Ordering=-1 is automatically chosen (overrides user settings)
//
if( ordering==0 )
{
//.........这里部分代码省略.........
with NRestarts random starts. Thus, FoldsCount*NRestarts networks
are trained in total.
NOTE: Rep.RelCLSError/Rep.AvgCE are zero on regression problems.
NOTE: on classification problems Rep.RMSError/Rep.AvgError/Rep.AvgRelError
contain errors in prediction of posterior probabilities.
-- ALGLIB --
Copyright 23.07.2012 by Bochkanov Sergey
*************************************************************************/
public static void mlpkfoldcv(mlptrainer s,
mlpbase.multilayerperceptron network,
int nrestarts,
int foldscount,
mlpreport rep)
{
alglib.smp.shared_pool pooldatacv = new alglib.smp.shared_pool();
mlpparallelizationcv datacv = new mlpparallelizationcv();
mlpparallelizationcv sdatacv = null;
double[,] cvy = new double[0,0];
int[] folds = new int[0];
double[] buf = new double[0];
double[] dy = new double[0];
int nin = 0;
int nout = 0;
int wcount = 0;
int rowsize = 0;
int ntype = 0;
int ttype = 0;
int i = 0;
int j = 0;
int k = 0;
hqrnd.hqrndstate rs = new hqrnd.hqrndstate();
int i_ = 0;
int i1_ = 0;
if( !mlpbase.mlpissoftmax(network) )
{
ntype = 0;
}
else
{
ntype = 1;
}
if( s.rcpar )
{
ttype = 0;
}
else
{
ttype = 1;
}
alglib.ap.assert(ntype==ttype, "MLPKFoldCV: type of input network is not similar to network type in trainer object");
alglib.ap.assert(s.npoints>=0, "MLPKFoldCV: possible trainer S is not initialized(S.NPoints<0)");
mlpbase.mlpproperties(network, ref nin, ref nout, ref wcount);
alglib.ap.assert(s.nin==nin, "MLPKFoldCV: number of inputs in trainer is not equal to number of inputs in network");
alglib.ap.assert(s.nout==nout, "MLPKFoldCV: number of outputs in trainer is not equal to number of outputs in network");
alglib.ap.assert(nrestarts>=0, "MLPKFoldCV: NRestarts<0");
alglib.ap.assert(foldscount>=2, "MLPKFoldCV: FoldsCount<2");
if( foldscount>s.npoints )
{
foldscount = s.npoints;
}
rep.relclserror = 0;
rep.avgce = 0;
public override void init()
{
x0 = new double[0];
x1 = new double[0];
t = new double[0];
xbest = new double[0];
r = new hqrnd.hqrndstate();
x = new double[0];
mv = new double[0];
mtv = new double[0];
rstate = new rcommstate();
}
请发表评论