simple-squiggle

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

zeros.js (3623B)


      1 import { isBigNumber } from '../../utils/is.js';
      2 import { isInteger } from '../../utils/number.js';
      3 import { resize } from '../../utils/array.js';
      4 import { factory } from '../../utils/factory.js';
      5 var name = 'zeros';
      6 var dependencies = ['typed', 'config', 'matrix', 'BigNumber'];
      7 export var createZeros = /* #__PURE__ */factory(name, dependencies, _ref => {
      8   var {
      9     typed,
     10     config,
     11     matrix,
     12     BigNumber
     13   } = _ref;
     14 
     15   /**
     16    * Create a matrix filled with zeros. The created matrix can have one or
     17    * multiple dimensions.
     18    *
     19    * Syntax:
     20    *
     21    *    math.zeros(m)
     22    *    math.zeros(m, format)
     23    *    math.zeros(m, n)
     24    *    math.zeros(m, n, format)
     25    *    math.zeros([m, n])
     26    *    math.zeros([m, n], format)
     27    *
     28    * Examples:
     29    *
     30    *    math.zeros(3)                  // returns [0, 0, 0]
     31    *    math.zeros(3, 2)               // returns [[0, 0], [0, 0], [0, 0]]
     32    *    math.zeros(3, 'dense')         // returns [0, 0, 0]
     33    *
     34    *    const A = [[1, 2, 3], [4, 5, 6]]
     35    *    math.zeros(math.size(A))       // returns [[0, 0, 0], [0, 0, 0]]
     36    *
     37    * See also:
     38    *
     39    *    ones, identity, size, range
     40    *
     41    * @param {...number | Array} size    The size of each dimension of the matrix
     42    * @param {string} [format]           The Matrix storage format
     43    *
     44    * @return {Array | Matrix}           A matrix filled with zeros
     45    */
     46   return typed(name, {
     47     '': function _() {
     48       return config.matrix === 'Array' ? _zeros([]) : _zeros([], 'default');
     49     },
     50     // math.zeros(m, n, p, ..., format)
     51     // TODO: more accurate signature '...number | BigNumber, string' as soon as typed-function supports this
     52     '...number | BigNumber | string': function numberBigNumberString(size) {
     53       var last = size[size.length - 1];
     54 
     55       if (typeof last === 'string') {
     56         var format = size.pop();
     57         return _zeros(size, format);
     58       } else if (config.matrix === 'Array') {
     59         return _zeros(size);
     60       } else {
     61         return _zeros(size, 'default');
     62       }
     63     },
     64     Array: _zeros,
     65     Matrix: function Matrix(size) {
     66       var format = size.storage();
     67       return _zeros(size.valueOf(), format);
     68     },
     69     'Array | Matrix, string': function ArrayMatrixString(size, format) {
     70       return _zeros(size.valueOf(), format);
     71     }
     72   });
     73   /**
     74    * Create an Array or Matrix with zeros
     75    * @param {Array} size
     76    * @param {string} [format='default']
     77    * @return {Array | Matrix}
     78    * @private
     79    */
     80 
     81   function _zeros(size, format) {
     82     var hasBigNumbers = _normalize(size);
     83 
     84     var defaultValue = hasBigNumbers ? new BigNumber(0) : 0;
     85 
     86     _validate(size);
     87 
     88     if (format) {
     89       // return a matrix
     90       var m = matrix(format);
     91 
     92       if (size.length > 0) {
     93         return m.resize(size, defaultValue);
     94       }
     95 
     96       return m;
     97     } else {
     98       // return an Array
     99       var arr = [];
    100 
    101       if (size.length > 0) {
    102         return resize(arr, size, defaultValue);
    103       }
    104 
    105       return arr;
    106     }
    107   } // replace BigNumbers with numbers, returns true if size contained BigNumbers
    108 
    109 
    110   function _normalize(size) {
    111     var hasBigNumbers = false;
    112     size.forEach(function (value, index, arr) {
    113       if (isBigNumber(value)) {
    114         hasBigNumbers = true;
    115         arr[index] = value.toNumber();
    116       }
    117     });
    118     return hasBigNumbers;
    119   } // validate arguments
    120 
    121 
    122   function _validate(size) {
    123     size.forEach(function (value) {
    124       if (typeof value !== 'number' || !isInteger(value) || value < 0) {
    125         throw new Error('Parameters in function zeros must be positive integers');
    126       }
    127     });
    128   }
    129 }); // TODO: zeros contains almost the same code as ones. Reuse this?