simple-squiggle

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

cos.js (1514B)


      1 import { factory } from '../../utils/factory.js';
      2 import { deepMap } from '../../utils/collection.js';
      3 var name = 'cos';
      4 var dependencies = ['typed'];
      5 export var createCos = /* #__PURE__ */factory(name, dependencies, _ref => {
      6   var {
      7     typed
      8   } = _ref;
      9 
     10   /**
     11    * Calculate the cosine of a value.
     12    *
     13    * For matrices, the function is evaluated element wise.
     14    *
     15    * Syntax:
     16    *
     17    *    math.cos(x)
     18    *
     19    * Examples:
     20    *
     21    *    math.cos(2)                      // returns number -0.4161468365471422
     22    *    math.cos(math.pi / 4)            // returns number  0.7071067811865475
     23    *    math.cos(math.unit(180, 'deg'))  // returns number -1
     24    *    math.cos(math.unit(60, 'deg'))   // returns number  0.5
     25    *
     26    *    const angle = 0.2
     27    *    math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2) // returns number ~1
     28    *
     29    * See also:
     30    *
     31    *    cos, tan
     32    *
     33    * @param {number | BigNumber | Complex | Unit | Array | Matrix} x  Function input
     34    * @return {number | BigNumber | Complex | Array | Matrix} Cosine of x
     35    */
     36   return typed(name, {
     37     number: Math.cos,
     38     Complex: function Complex(x) {
     39       return x.cos();
     40     },
     41     BigNumber: function BigNumber(x) {
     42       return x.cos();
     43     },
     44     Unit: function Unit(x) {
     45       if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) {
     46         throw new TypeError('Unit in function cos is no angle');
     47       }
     48 
     49       return this(x.value);
     50     },
     51     'Array | Matrix': function ArrayMatrix(x) {
     52       return deepMap(x, this);
     53     }
     54   });
     55 });