lib/sbm.js
function(source, dimension) {
    // create the matrix
    var mat = {}
    // if dimension is an integer, the matrix is square
    if (_.isFunction(source)
        && _.isInteger(dimension) && dimension >= 0) {
          dimension = { x:dimension, y:dimension }
    }

    // source can be a 2-var predicate function or a line based matrix
    if (_.isFunction(source)
        && dimension
        && _.isInteger(dimension.x) && dimension.x >= 0
        && _.isInteger(dimension.y) && dimension.y >= 0) {
          // creating the matrix
          mat.x = dimension.x
          mat.y = dimension.y
          mat.data = []
          // populate
          for (var i = 0; i < mat.x; i++) {
            mat.data.push([])
            for (var j = 0; j < mat.y; j++) {
              if (source(i, j)) {
                mat.data[i].push(j)
              }
            }
          }
    } else if (_.isArray(source) && source[0] && _.isArray(source[0])) {
      // creating the matrix
      mat.x = source.length
      mat.y = source[0].length
      mat.data = []
      // populate
      for (var i = 0; i < mat.x; i++) {
        mat.data.push([])
        for (var j = 0; j < mat.y; j++) {
          if (source[i][j]) {
            mat.data[i].push(j)
          }
        }
      }
    }
    // return the matrix
    return mat
  }
function(source, dimension) {
    // create the matrix
    var mat = {}
    // if dimension is an integer, the matrix is square
    if (_.isFunction(source)
        && _.isInteger(dimension) && dimension >= 0) {
          dimension = { x:dimension, y:dimension }
    }

    // source can be a 2-var predicate function or a line based matrix
    if (_.isFunction(source)
        && dimension
        && _.isInteger(dimension.x) && dimension.x >= 0
        && _.isInteger(dimension.y) && dimension.y >= 0) {
          // creating the matrix
          mat.x = dimension.x
          mat.y = dimension.y
          mat.data = []
          // populate
          for (var i = 0; i < mat.x; i++) {
            mat.data.push([])
            for (var j = 0; j < mat.y; j++) {
              if (source(i, j)) {
                mat.data[i].push(j)
              }
            }
          }
    } else if (_.isArray(source) && source[0] && _.isArray(source[0])) {
      // creating the matrix
      mat.x = source.length
      mat.y = source[0].length
      mat.data = []
      // populate
      for (var i = 0; i < mat.x; i++) {
        mat.data.push([])
        for (var j = 0; j < mat.y; j++) {
          if (source[i][j]) {
            mat.data[i].push(j)
          }
        }
      }
    }
    // return the matrix
    return mat
  }
function ( source , dimension ) { // create the matrix var mat = { } // if dimension is an integer, the matrix is square if ( _ . isFunction ( source ) && _ . isInteger ( dimension ) && dimension >= 0 ) { dimension = { x : dimension , y : dimension } } // source can be a 2-var predicate function or a line based matrix if ( _ . isFunction ( source ) && dimension && _ . isInteger ( dimension . x ) && dimension . x >= 0 && _ . isInteger ( dimension . y ) && dimension . y >= 0 ) { // creating the matrix mat . x = dimension . x mat . y = dimension . y mat . data = [ ] // populate for ( var i = 0 ; i < mat . x ; i ++ ) { mat . data . push ( [ ] ) for ( var j = 0 ; j < mat . y ; j ++ ) { if ( source ( i , j ) ) { mat . data [ i ] . push ( j ) } } } } else if ( _ . isArray ( source ) && source [ 0 ] && _ . isArray ( source [ 0 ] ) ) { // creating the matrix mat . x = source . length mat . y = source [ 0 ] . length mat . data = [ ] // populate for ( var i = 0 ; i < mat . x ; i ++ ) { mat . data . push ( [ ] ) for ( var j = 0 ; j < mat . y ; j ++ ) { if ( source [ i ] [ j ] ) { mat . data [ i ] . push ( j ) } } } } // return the matrix return mat }
creates a sparse binary matrix
creates a sparse binary matrix