simple-squiggle

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

sign.js (2115B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createSign = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _collection = require("../../utils/collection.js");
     11 
     12 var _index = require("../../plain/number/index.js");
     13 
     14 var name = 'sign';
     15 var dependencies = ['typed', 'BigNumber', 'Fraction', 'complex'];
     16 var createSign = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed,
     18       _BigNumber = _ref.BigNumber,
     19       complex = _ref.complex,
     20       _Fraction = _ref.Fraction;
     21 
     22   /**
     23    * Compute the sign of a value. The sign of a value x is:
     24    *
     25    * -  1 when x > 0
     26    * - -1 when x < 0
     27    * -  0 when x == 0
     28    *
     29    * For matrices, the function is evaluated element wise.
     30    *
     31    * Syntax:
     32    *
     33    *    math.sign(x)
     34    *
     35    * Examples:
     36    *
     37    *    math.sign(3.5)               // returns 1
     38    *    math.sign(-4.2)              // returns -1
     39    *    math.sign(0)                 // returns 0
     40    *
     41    *    math.sign([3, 5, -2, 0, 2])  // returns [1, 1, -1, 0, 1]
     42    *
     43    * See also:
     44    *
     45    *    abs
     46    *
     47    * @param  {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x
     48    *            The number for which to determine the sign
     49    * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}e
     50    *            The sign of `x`
     51    */
     52   return typed(name, {
     53     number: _index.signNumber,
     54     Complex: function Complex(x) {
     55       return x.im === 0 ? complex((0, _index.signNumber)(x.re)) : x.sign();
     56     },
     57     BigNumber: function BigNumber(x) {
     58       return new _BigNumber(x.cmp(0));
     59     },
     60     Fraction: function Fraction(x) {
     61       return new _Fraction(x.s, 1);
     62     },
     63     'Array | Matrix': function ArrayMatrix(x) {
     64       // deep map collection, skip zeros since sign(0) = 0
     65       return (0, _collection.deepMap)(x, this, true);
     66     },
     67     Unit: function Unit(x) {
     68       if (!x._isDerived() && x.units[0].unit.offset !== 0) {
     69         throw new TypeError('sign is ambiguous for units with offset');
     70       }
     71 
     72       return this(x.value);
     73     }
     74   });
     75 });
     76 exports.createSign = createSign;