simple-squiggle

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

column.js (1783B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createColumn = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _object = require("../../utils/object.js");
     11 
     12 var _array = require("../../utils/array.js");
     13 
     14 var name = 'column';
     15 var dependencies = ['typed', 'Index', 'matrix', 'range'];
     16 var createColumn = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed,
     18       Index = _ref.Index,
     19       matrix = _ref.matrix,
     20       range = _ref.range;
     21 
     22   /**
     23    * Return a column from a Matrix.
     24    *
     25    * Syntax:
     26    *
     27    *     math.column(value, index)
     28    *
     29    * Example:
     30    *
     31    *     // get a column
     32    *     const d = [[1, 2], [3, 4]]
     33    *     math.column(d, 1) // returns [[2], [4]]
     34    *
     35    * See also:
     36    *
     37    *     row
     38    *
     39    * @param {Array | Matrix } value   An array or matrix
     40    * @param {number} column           The index of the column
     41    * @return {Array | Matrix}         The retrieved column
     42    */
     43   return typed(name, {
     44     'Matrix, number': _column,
     45     'Array, number': function ArrayNumber(value, column) {
     46       return _column(matrix((0, _object.clone)(value)), column).valueOf();
     47     }
     48   });
     49   /**
     50    * Retrieve a column of a matrix
     51    * @param {Matrix } value  A matrix
     52    * @param {number} column  The index of the column
     53    * @return {Matrix}        The retrieved column
     54    */
     55 
     56   function _column(value, column) {
     57     // check dimensions
     58     if (value.size().length !== 2) {
     59       throw new Error('Only two dimensional matrix is supported');
     60     }
     61 
     62     (0, _array.validateIndex)(column, value.size()[1]);
     63     var rowRange = range(0, value.size()[0]);
     64     var index = new Index(rowRange, column);
     65     return value.subset(index);
     66   }
     67 });
     68 exports.createColumn = createColumn;