simple-squiggle

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

tanh.js (1601B)


      1 import { factory } from '../../utils/factory.js';
      2 import { deepMap } from '../../utils/collection.js';
      3 import { tanh as _tanh } from '../../utils/number.js';
      4 var name = 'tanh';
      5 var dependencies = ['typed'];
      6 export var createTanh = /* #__PURE__ */factory(name, dependencies, _ref => {
      7   var {
      8     typed
      9   } = _ref;
     10 
     11   /**
     12    * Calculate the hyperbolic tangent of a value,
     13    * defined as `tanh(x) = (exp(2 * x) - 1) / (exp(2 * x) + 1)`.
     14    *
     15    * For matrices, the function is evaluated element wise.
     16    *
     17    * Syntax:
     18    *
     19    *    math.tanh(x)
     20    *
     21    * Examples:
     22    *
     23    *    // tanh(x) = sinh(x) / cosh(x) = 1 / coth(x)
     24    *    math.tanh(0.5)                   // returns 0.46211715726000974
     25    *    math.sinh(0.5) / math.cosh(0.5)  // returns 0.46211715726000974
     26    *    1 / math.coth(0.5)               // returns 0.46211715726000974
     27    *
     28    * See also:
     29    *
     30    *    sinh, cosh, coth
     31    *
     32    * @param {number | BigNumber | Complex | Unit | Array | Matrix} x  Function input
     33    * @return {number | BigNumber | Complex | Array | Matrix} Hyperbolic tangent of x
     34    */
     35   return typed('tanh', {
     36     number: _tanh,
     37     Complex: function Complex(x) {
     38       return x.tanh();
     39     },
     40     BigNumber: function BigNumber(x) {
     41       return x.tanh();
     42     },
     43     Unit: function Unit(x) {
     44       if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) {
     45         throw new TypeError('Unit in function tanh is no angle');
     46       }
     47 
     48       return this(x.value);
     49     },
     50     'Array | Matrix': function ArrayMatrix(x) {
     51       // deep map collection, skip zeros since tanh(0) = 0
     52       return deepMap(x, this, true);
     53     }
     54   });
     55 });