simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

csPermute.js (1618B)


      1 /**
      2  * Permutes a sparse matrix C = P * A * Q
      3  *
      4  * @param {SparseMatrix}  a         The Matrix A
      5  * @param {Array}   pinv            The row permutation vector
      6  * @param {Array}   q               The column permutation vector
      7  * @param {boolean} values          Create a pattern matrix (false), values and pattern otherwise
      8  *
      9  * @return {Matrix}                 C = P * A * Q, null on error
     10  *
     11  * Reference: http://faculty.cse.tamu.edu/davis/publications.html
     12  */
     13 export function csPermute(a, pinv, q, values) {
     14   // a arrays
     15   var avalues = a._values;
     16   var aindex = a._index;
     17   var aptr = a._ptr;
     18   var asize = a._size;
     19   var adt = a._datatype; // rows & columns
     20 
     21   var m = asize[0];
     22   var n = asize[1]; // c arrays
     23 
     24   var cvalues = values && a._values ? [] : null;
     25   var cindex = []; // (aptr[n])
     26 
     27   var cptr = []; // (n + 1)
     28   // initialize vars
     29 
     30   var nz = 0; // loop columns
     31 
     32   for (var k = 0; k < n; k++) {
     33     // column k of C is column q[k] of A
     34     cptr[k] = nz; // apply column permutation
     35 
     36     var j = q ? q[k] : k; // loop values in column j of A
     37 
     38     for (var t0 = aptr[j], t1 = aptr[j + 1], t = t0; t < t1; t++) {
     39       // row i of A is row pinv[i] of C
     40       var r = pinv ? pinv[aindex[t]] : aindex[t]; // index
     41 
     42       cindex[nz] = r; // check we need to populate values
     43 
     44       if (cvalues) {
     45         cvalues[nz] = avalues[t];
     46       } // increment number of nonzero elements
     47 
     48 
     49       nz++;
     50     }
     51   } // finalize the last column of C
     52 
     53 
     54   cptr[n] = nz; // return C matrix
     55 
     56   return a.createSparseMatrix({
     57     values: cvalues,
     58     index: cindex,
     59     ptr: cptr,
     60     size: [m, n],
     61     datatype: adt
     62   });
     63 }