private void sprsax(VecDoub sa, VecLong ija, VecDoub x, VecDoub b, int n) { if (ija[1] != n + 2) { new Exception("sprsax: mismatched vector and matrix"); } int i; long k; for (i = 0; i < n; i++) { b[i] = sa[i] * x[i]; //Diagonal entries for (k = ija[i]; k <= ija[i + 1] - 1; k++) //TODO: Verify this. { b[i] += sa[(int)k] * x[(int)ija[(int)k]]; //Off diagonal entries. Multiply the entry of the matrix by the x of the corresponding column. } //TODO: Find out how to reference arrays in C# with some thing that could be > 2 billion (max value of integer). } }
private void sprstx(VecDoub sa, VecLong ija, VecDoub x, VecDoub b, int n) { int i, j; long k; if (ija[1] != n + 2) { new Exception("mismatched vector and matrix in sprstx"); } for (i = 0; i < n; i++) { b[i] = sa[i] * x[i]; //First come the diagonal terms } for (i = 0; i < n; i++) {//Now loop over the off diagonal terms. for (k = ija[i]; k <= ija[i + 1] - 1; k++) { j = (int)ija[(int)k]; b[j] += sa[(int)k] * x[i]; //Because this is multiplication by the transpose, the indices of b and x interchange. } } }
public Sprsin(MatDoub a)//TODO: make a VecULong type to further save on space. { /************************************ * Converts a square matrix a[1..n][1..n] into row-indexed sparse storage mode. * Only elements of a with magnitude ≥thresh are retained. Output is in two linear arrays * with dimension nmax (an input parameter): sa[1..] contains array values, indexed by ija[1..]. * The number of elements filled of sa and ija on output are both ija[ija[1]-1]-1 ************************************/ sa = new VecDoub(new double[nmax]); ija = new VecLong(18, new long[nmax], 0); n = a.nrows(); int i, j, k; for (j = 0; j < n; j++) { sa[j] = a[j][j]; //Store the diagonal elements. } ija[0] = n + 1; //Index to the first off diagonal element if any. k = n; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (Math.Abs(a[i][j]) >= thresh && i != j) { if (++k > (int)nmax) { throw new Exception("sprsin: nmax too small"); } sa[k] = a[i][j]; //Store off diagonal elements. ija[k] = j; //And their column indices. } } ija[i + 1] = k + 1; //As each row is completed, store index to next } this.numelements = k + 1; }