本文整理汇总了C#中sparsematrix类的典型用法代码示例。如果您正苦于以下问题:C# sparsematrix类的具体用法?C# sparsematrix怎么用?C# sparsematrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
sparsematrix类属于命名空间,在下文中一共展示了sparsematrix类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: SparseCopyToSKSBuf
/*************************************************************************
This function performs in-place conversion to SKS format.
INPUT PARAMETERS
S - sparse matrix in any format.
OUTPUT PARAMETERS
S - sparse matrix in SKS format.
NOTE: this function has no effect when called with matrix which is
already in SKS mode.
NOTE: in-place conversion involves allocation of temporary arrays. If you
perform a lot of repeated in- place conversions, it may lead to
memory fragmentation. Consider using out-of-place SparseCopyToSKSBuf()
function in this case.
-- ALGLIB PROJECT --
Copyright 15.01.2014 by Bochkanov Sergey
*************************************************************************/
public static void sparseconverttosks(sparsematrix s)
{
int[] tridx = new int[0];
int[] tdidx = new int[0];
int[] tuidx = new int[0];
double[] tvals = new double[0];
int n = 0;
int t0 = 0;
int t1 = 0;
int i = 0;
int j = 0;
int k = 0;
double v = 0;
alglib.ap.assert((s.matrixtype==0 || s.matrixtype==1) || s.matrixtype==2, "SparseConvertToSKS: invalid matrix type");
alglib.ap.assert(s.m==s.n, "SparseConvertToSKS: rectangular matrices are not supported");
n = s.n;
if( s.matrixtype==2 )
{
//
// Already in SKS mode
//
return;
}
//
// Generate internal copy of SKS matrix
//
apserv.ivectorsetlengthatleast(ref tdidx, n+1);
apserv.ivectorsetlengthatleast(ref tuidx, n+1);
for(i=0; i<=n; i++)
{
tdidx[i] = 0;
tuidx[i] = 0;
}
t0 = 0;
t1 = 0;
while( sparseenumerate(s, ref t0, ref t1, ref i, ref j, ref v) )
{
if( j<i )
{
tdidx[i] = Math.Max(tdidx[i], i-j);
}
else
{
tuidx[j] = Math.Max(tuidx[j], j-i);
}
}
apserv.ivectorsetlengthatleast(ref tridx, n+1);
tridx[0] = 0;
for(i=1; i<=n; i++)
{
tridx[i] = tridx[i-1]+tdidx[i-1]+1+tuidx[i-1];
}
apserv.rvectorsetlengthatleast(ref tvals, tridx[n]);
k = tridx[n];
for(i=0; i<=k-1; i++)
{
tvals[i] = 0.0;
}
t0 = 0;
t1 = 0;
while( sparseenumerate(s, ref t0, ref t1, ref i, ref j, ref v) )
{
if( j<=i )
{
tvals[tridx[i]+tdidx[i]-(i-j)] = v;
}
else
{
tvals[tridx[j+1]-(j-i)] = v;
}
}
for(i=0; i<=n-1; i++)
{
tdidx[n] = Math.Max(tdidx[n], tdidx[i]);
tuidx[n] = Math.Max(tuidx[n], tuidx[i]);
}
s.matrixtype = 2;
//.........这里部分代码省略.........
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:101,代码来源:linalg.cs
示例2: format
/*************************************************************************
This function calculates matrix-matrix product S*A, when S is symmetric
matrix. Matrix S must be stored in CRS format (exception will be
thrown otherwise).
INPUT PARAMETERS
S - sparse M*M matrix in CRS format (you MUST convert it
to CRS before calling this function).
A - array[N][K], input dense matrix. For performance reasons
we make only quick checks - we check that array size is
at least N, but we do not check for NAN's or INF's.
K - number of columns of matrix (A).
B - output buffer, possibly preallocated. In case buffer
size is too small to store result, this buffer is
automatically resized.
OUTPUT PARAMETERS
B - array[M][K], S*A
NOTE: this function throws exception when called for non-CRS matrix. You
must convert your matrix with SparseConvertToCRS() before using this
function.
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
public static void sparsesmm(sparsematrix s,
bool isupper,
double[,] a,
int k,
ref double[,] b)
{
int i = 0;
int j = 0;
int k0 = 0;
int id = 0;
int lt = 0;
int rt = 0;
double v = 0;
double vb = 0;
double va = 0;
int i_ = 0;
alglib.ap.assert(s.matrixtype == 1, "SparseSMM: incorrect matrix type (convert your matrix to CRS)");
alglib.ap.assert(s.ninitialized == s.ridx[s.m], "SparseSMM: some rows/elements of the CRS matrix were not initialized (you must initialize everything you promised to SparseCreateCRS)");
alglib.ap.assert(alglib.ap.rows(a) >= s.n, "SparseSMM: Rows(X)<N");
alglib.ap.assert(s.m == s.n, "SparseSMM: matrix is non-square");
apserv.rmatrixsetlengthatleast(ref b, s.m, k);
for (i = 0; i <= s.m - 1; i++)
{
for (j = 0; j <= k - 1; j++)
{
b[i, j] = 0;
}
}
if (k > linalgswitch)
{
for (i = 0; i <= s.m - 1; i++)
{
for (j = 0; j <= k - 1; j++)
{
if (s.didx[i] != s.uidx[i])
{
id = s.didx[i];
b[i, j] = b[i, j] + s.vals[id] * a[s.idx[id], j];
}
if (isupper)
{
lt = s.uidx[i];
rt = s.ridx[i + 1];
vb = 0;
va = a[i, j];
for (k0 = lt; k0 <= rt - 1; k0++)
{
id = s.idx[k0];
v = s.vals[k0];
vb = vb + a[id, j] * v;
b[id, j] = b[id, j] + va * v;
}
b[i, j] = b[i, j] + vb;
}
else
{
lt = s.ridx[i];
rt = s.didx[i];
vb = 0;
va = a[i, j];
for (k0 = lt; k0 <= rt - 1; k0++)
{
id = s.idx[k0];
v = s.vals[k0];
vb = vb + a[id, j] * v;
b[id, j] = b[id, j] + va * v;
}
b[i, j] = b[i, j] + vb;
}
}
}
}
else
//.........这里部分代码省略.........
开发者ID:tablee,项目名称:TabBox,代码行数:101,代码来源:linalg.cs
示例3: sparsegetaveragelengthofchain
/*************************************************************************
This function return average length of chain at hash-table.
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
public static double sparsegetaveragelengthofchain(sparsematrix s)
{
double result = 0;
int nchains = 0;
int talc = 0;
int l = 0;
int i = 0;
int ind0 = 0;
int ind1 = 0;
int hashcode = 0;
//
//if matrix represent in CRS then return zero and exit
//
if (s.matrixtype == 1)
{
result = 0;
return result;
}
nchains = 0;
talc = 0;
l = alglib.ap.len(s.vals);
for (i = 0; i <= l - 1; i++)
{
ind0 = 2 * i;
if (s.idx[ind0] != -1)
{
nchains = nchains + 1;
hashcode = hash(s.idx[ind0], s.idx[ind0 + 1], l);
while (true)
{
talc = talc + 1;
ind1 = 2 * hashcode;
if (s.idx[ind0] == s.idx[ind1] && s.idx[ind0 + 1] == s.idx[ind1 + 1])
{
break;
}
hashcode = (hashcode + 1) % l;
}
}
}
if (nchains == 0)
{
result = 0;
}
else
{
result = (double)talc / (double)nchains;
}
return result;
}
开发者ID:tablee,项目名称:TabBox,代码行数:58,代码来源:linalg.cs
示例4: square
/*************************************************************************
This function simultaneously calculates two matrix-vector products:
S*x and S^T*x.
S must be square (non-rectangular) matrix stored in CRS format (exception
will be thrown otherwise).
INPUT PARAMETERS
S - sparse N*N matrix in CRS format (you MUST convert it
to CRS before calling this function).
X - array[N], input vector. For performance reasons we
make only quick checks - we check that array size is
at least N, but we do not check for NAN's or INF's.
Y0 - output buffer, possibly preallocated. In case buffer
size is too small to store result, this buffer is
automatically resized.
Y1 - output buffer, possibly preallocated. In case buffer
size is too small to store result, this buffer is
automatically resized.
OUTPUT PARAMETERS
Y0 - array[N], S*x
Y1 - array[N], S^T*x
NOTE: this function throws exception when called for non-CRS matrix. You
must convert your matrix with SparseConvertToCRS() before using this
function. It also throws exception when S is non-square.
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
public static void sparsemv2(sparsematrix s,
double[] x,
ref double[] y0,
ref double[] y1)
{
int l = 0;
double tval = 0;
int i = 0;
int j = 0;
double vx = 0;
double vs = 0;
int vi = 0;
int j0 = 0;
int j1 = 0;
alglib.ap.assert(s.matrixtype == 1, "SparseMV2: incorrect matrix type (convert your matrix to CRS)");
alglib.ap.assert(s.ninitialized == s.ridx[s.m], "SparseMV: some rows/elements of the CRS matrix were not initialized (you must initialize everything you promised to SparseCreateCRS)");
alglib.ap.assert(s.m == s.n, "SparseMV2: matrix is non-square");
l = alglib.ap.len(x);
alglib.ap.assert(l >= s.n, "SparseMV2: Length(X)<N");
apserv.rvectorsetlengthatleast(ref y0, l);
apserv.rvectorsetlengthatleast(ref y1, l);
for (i = 0; i <= s.n - 1; i++)
{
y1[i] = 0;
}
for (i = 0; i <= s.m - 1; i++)
{
tval = 0;
vx = x[i];
j0 = s.ridx[i];
j1 = s.ridx[i + 1] - 1;
for (j = j0; j <= j1; j++)
{
vi = s.idx[j];
vs = s.vals[j];
tval = tval + x[vi] * vs;
y1[vi] = y1[vi] + vx * vs;
}
y0[i] = tval;
}
}
开发者ID:tablee,项目名称:TabBox,代码行数:72,代码来源:linalg.cs
示例5: mode
/*************************************************************************
This function returns S[i,j] - element of the sparse matrix. Matrix can
be in any mode (Hash-Table or CRS), but this function is less efficient
for CRS matrices. Hash-Table matrices can find element in O(1) time,
while CRS matrices need O(RS) time, where RS is an number of non-zero
elements in a row.
INPUT PARAMETERS
S - sparse M*N matrix in Hash-Table representation.
Exception will be thrown for CRS matrix.
I - row index of the element to modify, 0<=I<M
J - column index of the element to modify, 0<=J<N
RESULT
value of S[I,J] or zero (in case no element with such index is found)
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
public static double sparseget(sparsematrix s,
int i,
int j)
{
double result = 0;
int hashcode = 0;
int k = 0;
int k0 = 0;
int k1 = 0;
alglib.ap.assert(i >= 0, "SparseGet: I<0");
alglib.ap.assert(i < s.m, "SparseGet: I>=M");
alglib.ap.assert(j >= 0, "SparseGet: J<0");
alglib.ap.assert(j < s.n, "SparseGet: J>=N");
k = alglib.ap.len(s.vals);
result = 0;
if (s.matrixtype == 0)
{
hashcode = hash(i, j, k);
while (true)
{
if (s.idx[2 * hashcode] == -1)
{
return result;
}
if (s.idx[2 * hashcode] == i && s.idx[2 * hashcode + 1] == j)
{
result = s.vals[hashcode];
return result;
}
hashcode = (hashcode + 1) % k;
}
}
if (s.matrixtype == 1)
{
alglib.ap.assert(s.ninitialized == s.ridx[s.m], "SparseGet: some rows/elements of the CRS matrix were not initialized (you must initialize everything you promised to SparseCreateCRS)");
k0 = s.ridx[i];
k1 = s.ridx[i + 1] - 1;
for (k = k0; k <= k1; k++)
{
if (s.idx[k] == j)
{
result = s.vals[k];
return result;
}
}
return result;
}
return result;
}
开发者ID:tablee,项目名称:TabBox,代码行数:69,代码来源:linalg.cs
示例6: sparsegetnrows
/*************************************************************************
The function returns number of rows of a sparse matrix.
RESULT: number of rows of a sparse matrix.
-- ALGLIB PROJECT --
Copyright 23.08.2012 by Bochkanov Sergey
*************************************************************************/
public static int sparsegetnrows(sparsematrix s)
{
int result = 0;
result = s.m;
return result;
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:15,代码来源:linalg.cs
示例7: sparsegetncols
/*************************************************************************
The function returns number of columns of a sparse matrix.
RESULT: number of columns of a sparse matrix.
-- ALGLIB PROJECT --
Copyright 23.08.2012 by Bochkanov Sergey
*************************************************************************/
public static int sparsegetncols(sparsematrix s)
{
int result = 0;
result = s.n;
return result;
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:15,代码来源:linalg.cs
示例8: sparseissks
/*************************************************************************
This function checks matrix storage format and returns True when matrix is
stored using SKS representation.
INPUT PARAMETERS:
S - sparse matrix.
RESULT:
True if matrix type is SKS
False if matrix type is not SKS
-- ALGLIB PROJECT --
Copyright 20.07.2012 by Bochkanov Sergey
*************************************************************************/
public static bool sparseissks(sparsematrix s)
{
bool result = new bool();
alglib.ap.assert((s.matrixtype==0 || s.matrixtype==1) || s.matrixtype==2, "SparseIsSKS: invalid matrix type");
result = s.matrixtype==2;
return result;
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:22,代码来源:linalg.cs
示例9: sparsefree
/*************************************************************************
The function frees all memory occupied by sparse matrix. Sparse matrix
structure becomes unusable after this call.
OUTPUT PARAMETERS
S - sparse matrix to delete
-- ALGLIB PROJECT --
Copyright 24.07.2012 by Bochkanov Sergey
*************************************************************************/
public static void sparsefree(sparsematrix s)
{
s.matrixtype = -1;
s.m = 0;
s.n = 0;
s.nfree = 0;
s.ninitialized = 0;
s.tablesize = 0;
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:19,代码来源:linalg.cs
示例10: CRS
/*************************************************************************
This function returns type of the matrix storage format.
INPUT PARAMETERS:
S - sparse matrix.
RESULT:
sparse storage format used by matrix:
0 - Hash-table
1 - CRS (compressed row storage)
2 - SKS (skyline)
NOTE: future versions of ALGLIB may include additional sparse storage
formats.
-- ALGLIB PROJECT --
Copyright 20.07.2012 by Bochkanov Sergey
*************************************************************************/
public static int sparsegetmatrixtype(sparsematrix s)
{
int result = 0;
alglib.ap.assert((s.matrixtype==0 || s.matrixtype==1) || s.matrixtype==2, "SparseGetMatrixType: invalid matrix type");
result = s.matrixtype;
return result;
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:27,代码来源:linalg.cs
示例11: sparsecopytosksbuf
/*************************************************************************
This function performs out-of-place conversion to SKS format. S0 is
copied to S1 and converted on-the-fly. Memory allocated in S1 is reused
to maximum extent possible.
INPUT PARAMETERS
S0 - sparse matrix in any format.
OUTPUT PARAMETERS
S1 - sparse matrix in SKS format.
NOTE: if S0 is stored as SKS, it is just copied without conversion.
-- ALGLIB PROJECT --
Copyright 20.07.2012 by Bochkanov Sergey
*************************************************************************/
public static void sparsecopytosksbuf(sparsematrix s0,
sparsematrix s1)
{
double v = 0;
int n = 0;
int t0 = 0;
int t1 = 0;
int i = 0;
int j = 0;
int k = 0;
alglib.ap.assert((s0.matrixtype==0 || s0.matrixtype==1) || s0.matrixtype==2, "SparseCopyToSKSBuf: invalid matrix type");
alglib.ap.assert(s0.m==s0.n, "SparseCopyToSKSBuf: rectangular matrices are not supported");
n = s0.n;
if( s0.matrixtype==2 )
{
//
// Already SKS, just copy
//
sparsecopybuf(s0, s1);
return;
}
//
// Generate copy of matrix in the SKS format
//
apserv.ivectorsetlengthatleast(ref s1.didx, n+1);
apserv.ivectorsetlengthatleast(ref s1.uidx, n+1);
for(i=0; i<=n; i++)
{
s1.didx[i] = 0;
s1.uidx[i] = 0;
}
t0 = 0;
t1 = 0;
while( sparseenumerate(s0, ref t0, ref t1, ref i, ref j, ref v) )
{
if( j<i )
{
s1.didx[i] = Math.Max(s1.didx[i], i-j);
}
else
{
s1.uidx[j] = Math.Max(s1.uidx[j], j-i);
}
}
apserv.ivectorsetlengthatleast(ref s1.ridx, n+1);
s1.ridx[0] = 0;
for(i=1; i<=n; i++)
{
s1.ridx[i] = s1.ridx[i-1]+s1.didx[i-1]+1+s1.uidx[i-1];
}
apserv.rvectorsetlengthatleast(ref s1.vals, s1.ridx[n]);
k = s1.ridx[n];
for(i=0; i<=k-1; i++)
{
s1.vals[i] = 0.0;
}
t0 = 0;
t1 = 0;
while( sparseenumerate(s0, ref t0, ref t1, ref i, ref j, ref v) )
{
if( j<=i )
{
s1.vals[s1.ridx[i]+s1.didx[i]-(i-j)] = v;
}
else
{
s1.vals[s1.ridx[j+1]-(j-i)] = v;
}
}
for(i=0; i<=n-1; i++)
{
s1.didx[n] = Math.Max(s1.didx[n], s1.didx[i]);
s1.uidx[n] = Math.Max(s1.uidx[n], s1.uidx[i]);
}
s1.matrixtype = 2;
s1.ninitialized = 0;
s1.nfree = 0;
s1.m = n;
s1.n = n;
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:99,代码来源:linalg.cs
示例12: sparseadd
/*************************************************************************
This function adds value to S[i,j] - element of the sparse matrix. Matrix
must be in a Hash-Table mode.
In case S[i,j] already exists in the table, V i added to its value. In
case S[i,j] is non-existent, it is inserted in the table. Table
automatically grows when necessary.
INPUT PARAMETERS
S - sparse M*N matrix in Hash-Table representation.
Exception will be thrown for CRS matrix.
I - row index of the element to modify, 0<=I<M
J - column index of the element to modify, 0<=J<N
V - value to add, must be finite number
OUTPUT PARAMETERS
S - modified matrix
NOTE 1: when S[i,j] is exactly zero after modification, it is deleted
from the table.
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
public static void sparseadd(sparsematrix s,
int i,
int j,
double v)
{
int hashcode = 0;
int tcode = 0;
int k = 0;
alglib.ap.assert(s.matrixtype == 0, "SparseAdd: matrix must be in the Hash-Table mode to do this operation");
alglib.ap.assert(i >= 0, "SparseAdd: I<0");
alglib.ap.assert(i < s.m, "SparseAdd: I>=M");
alglib.ap.assert(j >= 0, "SparseAdd: J<0");
alglib.ap.assert(j < s.n, "SparseAdd: J>=N");
alglib.ap.assert(math.isfinite(v), "SparseAdd: V is not finite number");
if ((double)(v) == (double)(0))
{
return;
}
tcode = -1;
k = alglib.ap.len(s.vals);
if ((double)((1 - maxloadfactor) * k) >= (double)(s.nfree))
{
sparseresizematrix(s);
k = alglib.ap.len(s.vals);
}
hashcode = hash(i, j, k);
while (true)
{
if (s.idx[2 * hashcode] == -1)
{
if (tcode != -1)
{
hashcode = tcode;
}
s.vals[hashcode] = v;
s.idx[2 * hashcode] = i;
s.idx[2 * hashcode + 1] = j;
if (tcode == -1)
{
s.nfree = s.nfree - 1;
}
return;
}
else
{
if (s.idx[2 * hashcode] == i && s.idx[2 * hashcode + 1] == j)
{
s.vals[hashcode] = s.vals[hashcode] + v;
if ((double)(s.vals[hashcode]) == (double)(0))
{
s.idx[2 * hashcode] = -2;
}
return;
}
//
//is it deleted element?
//
if (tcode == -1 && s.idx[2 * hashcode] == -2)
{
tcode = hashcode;
}
//
//next step
//
hashcode = (hashcode + 1) % k;
}
}
}
开发者ID:tablee,项目名称:TabBox,代码行数:95,代码来源:linalg.cs
示例13: O
/*************************************************************************
The function returns number of strictly lower triangular non-zero elements
in the matrix. It counts SYMBOLICALLY non-zero elements, i.e. entries
in the sparse matrix data structure. If some element has zero numerical
value, it is still counted.
This function has different cost for different types of matrices:
* for hash-based matrices it involves complete pass over entire hash-table
with O(NNZ) cost, where NNZ is number of non-zero elements
* for CRS and SKS matrix types cost of counting is O(N) (N - matrix size).
RESULT: number of non-zero elements strictly below main diagonal
-- ALGLIB PROJECT --
Copyright 12.02.2014 by Bochkanov Sergey
*************************************************************************/
public static int sparsegetlowercount(sparsematrix s)
{
int result = 0;
int sz = 0;
int i0 = 0;
int i = 0;
result = -1;
if( s.matrixtype==0 )
{
//
// Hash-table matrix
//
result = 0;
sz = s.tablesize;
for(i0=0; i0<=sz-1; i0++)
{
i = s.idx[2*i0];
if( i>=0 && s.idx[2*i0+1]<i )
{
result = result+1;
}
}
return result;
}
if( s.matrixtype==1 )
{
//
// CRS matrix
//
alglib.ap.assert(s.ninitialized==s.ridx[s.m], "SparseGetUpperCount: some rows/elements of the CRS matrix were not initialized (you must initialize everything you promised to SparseCreateCRS)");
result = 0;
sz = s.m;
for(i=0; i<=sz-1; i++)
{
result = result+(s.didx[i]-s.ridx[i]);
}
return result;
}
if( s.matrixtype==2 )
{
//
// SKS matrix
//
alglib.ap.assert(s.m==s.n, "SparseGetUpperCount: non-square SKS matrices are not supported");
result = 0;
sz = s.m;
for(i=0; i<=sz-1; i++)
{
result = result+s.didx[i];
}
return result;
}
alglib.ap.assert(false, "SparseGetUpperCount: internal error");
return result;
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:75,代码来源:linalg.cs
示例14: sparseset
/*************************************************************************
This function modifies S[i,j] - element of the sparse matrix. Matrix must
be in a Hash-Table mode.
In case new value of S[i,j] is zero, this element is deleted from the
table.
INPUT PARAMETERS
S - sparse M*N matrix in Hash-Table representation.
Exception will be thrown for CRS matrix.
I - row index of the element to modify, 0<=I<M
J - column index of the element to modify, 0<=J<N
V - value to set, must be finite number, can be zero
OUTPUT PARAMETERS
S - modified matrix
NOTE: this function has no effect when called with zero V for non-
existent element.
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
public static void sparseset(sparsematrix s,
int i,
int j,
double v)
{
int hashcode = 0;
int tcode = 0;
int k = 0;
alglib.ap.assert(i >= 0, "SparseSet: I<0");
alglib.ap.assert(i < s.m, "SparseSet: I>=M");
alglib.ap.assert(j >= 0, "SparseSet: J<0");
alglib.ap.assert(j < s.n, "SparseSet: J>=N");
alglib.ap.assert(math.isfinite(v), "SparseSet: V is not finite number");
//
// Hash-table matrix
//
if (s.matrixtype == 0)
{
tcode = -1;
k = alglib.ap.len(s.vals);
if ((double)((1 - maxloadfactor) * k) >= (double)(s.nfree))
{
sparseresizematrix(s);
k = alglib.ap.len(s.vals);
}
hashcode = hash(i, j, k);
while (true)
{
if (s.idx[2 * hashcode] == -1)
{
if ((double)(v) != (double)(0))
{
if (tcode != -1)
{
hashcode = tcode;
}
s.vals[hashcode] = v;
s.idx[2 * hashcode] = i;
s.idx[2 * hashcode + 1] = j;
if (tcode == -1)
{
s.nfree = s.nfree - 1;
}
}
return;
}
else
{
if (s.idx[2 * hashcode] == i && s.idx[2 * hashcode + 1] == j)
{
if ((double)(v) == (double)(0))
{
s.idx[2 * hashcode] = -2;
}
else
{
s.vals[hashcode] = v;
}
return;
}
if (tcode == -1 && s.idx[2 * hashcode] == -2)
{
tcode = hashcode;
}
//
// next step
//
hashcode = (hashcode + 1) % k;
}
}
}
//
// CRS matrix
//.........这里部分代码省略.........
开发者ID:tablee,项目名称:TabBox,代码行数:101,代码来源:linalg.cs
示例15: sparseinitduidx
/*************************************************************************
Procedure for initialization 'S.DIdx' and 'S.UIdx'
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
private static void sparseinitduidx(sparsematrix s)
{
int i = 0;
int j = 0;
int lt = 0;
int rt = 0;
alglib.ap.assert(s.matrixtype==1, "SparseInitDUIdx: internal error, incorrect matrix type");
apserv.ivectorsetlengthatleast(ref s.didx, s.m);
apserv.ivectorsetlengthatleast(ref s.uidx, s.m);
for(i=0; i<=s.m-1; i++)
{
s.uidx[i] = -1;
s.didx[i] = -1;
lt = s.ridx[i];
rt = s.ridx[i+1];
for(j=lt; j<=rt-1; j++)
{
if( i<s.idx[j] && s.uidx[i]==-1 )
{
s.uidx[i] = j;
break;
}
else
{
if( i==s.idx[j] )
{
s.didx[i] = j;
}
}
}
if( s.uidx[i]==-1 )
{
s.uidx[i] = s.ridx[i+1];
}
if( s.didx[i]==-1 )
{
s.didx[i] = s.uidx[i];
}
}
}
开发者ID:Kerbas-ad-astra,项目名称:MechJeb2,代码行数:48,代码来源:linalg.cs
示例16: algorithms
/*************************************************************************
This function converts matrix to CRS format.
Some algorithms (linear algebra ones, for example) require matrices in
CRS format.
INPUT PARAMETERS
S - sparse M*N matrix.
OUTPUT PARAMETERS
S - matrix in CRS format
NOTE: this function has no effect when called with matrix which is
already in CRS mode.
-- ALGLIB PROJECT --
Copyright 14.10.2011 by Bochkanov Sergey
*************************************************************************/
public static void sparseconverttocrs(sparsematrix s)
{
int i = 0;
double[] tvals = new double[0];
int[] tidx = new int[0];
int[] temp = new int[0];
int nonne = 0;
int k = 0;
alglib.ap.assert(s.matrixtype == 0 || s.matrixtype == 1, "SparseConvertToCRS: invalid matrix type");
if (s.matrixtype == 1)
{
return;
}
s.matrixtype = 1;
nonne = 0;
k = alglib.ap.len(s.vals);
alglib.ap.swap(ref s.vals, ref tvals);
alglib.ap.swap(ref s.idx, ref tidx);
s.ridx = new int[s.m + 1];
for (i = 0; i <= s.m; i++)
{
s.ridx[i] = 0;
}
temp = new int[s.m];
for (i = 0; i <= s.m - 1; i++)
{
temp[i] = 0;
}
//
// Number of elements per row
//
for (i = 0; i <= k - 1; i++)
{
if (tidx[2 * i] >= 0)
{
s.ridx[tidx[2 * i] + 1] = s.ridx[tidx[2 * i] + 1] + 1;
nonne = nonne + 1;
}
}
//
// Fill RIdx (offsets of rows)
//
for (i = 0; i <= s.m - 1; i++)
{
s.ridx[i + 1] = s.ridx[i + 1] + s.ridx[i];
}
//
// Allocate memory
//
s.vals = new double[nonne];
s.idx = new int[nonne];
for (i = 0; i <= k - 1; i++)
{
if (tidx[2 * i] >= 0)
{
s.vals[s.ridx[tidx[2 * i]] + temp[tidx[2 * i]]] = tvals[i];
s.idx[s.ridx[tidx[2 * i]] + temp[tidx[2 * i]]] = tidx[2 * i + 1];
temp[tidx[2 * i]] = temp[tidx[2 * i]] + 1;
}
}
//
// Set NInitialized
//
s.ninitialized = s.ridx[s.m];
//
//sorting of elements
//
for (i = 0; i <= s.m - 1; i++)
{
tsort.tagsortmiddleir(ref s.idx, ref s.vals, s.ridx[i], s.ridx[i + 1] - s.ridx[i]);
}
//
//initialization 'S.UIdx' and 'S.DIdx'
//
sparseinitduidx(s);
//.........这里部分代码省略.........
开发者ID:tablee,项目名称:TabBox,代码行数:101,代码来源:linalg.cs
示例17: SparseEnumerate
/*************************************************************************
This function is used to enumerate all elements of the sparse matrix.
Before first call user initializes T0 and T1 counters by zero. These
counters are used to remember current position in a matrix; after each
call they are updated by the function.
Subsequent calls to this function return non-zero elements of the sparse
matrix, one by one. If you enumerate CRS matrix, matrix is traversed from
left to right, from top to bottom. In case you enumerate
|
请发表评论