How to use mathjs - 10 common examples

To help you get started, we’ve selected a few mathjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tensorflow / tfjs / tfjs / integration_tests / models / benchmarks.ts View on Github external
// Format data for predict().
          tfjsRun = {
            taskId,
            taskType,
            modelFormat,
            modelName,
            // TODO(cais): Add modelId.
            functionName,
            batchSize: pyRun.batchSize,
            versionSetId,
            environmentId: tfjsEnvironmentId,
            numWarmUpIterations: pyRun.numWarmUpIterations,
            numBenchmarkedIterations: pyRun.numBenchmarkedIterations,
            timesMs: ts,
            averageTimeMs: math.mean(ts),
            endingTimestampMs: new Date().getTime()
          };
          console.log(
              `  (taskId=${taskId}) predict(): averageTimeMs: ` +
              `py=${pyRun.averageTimeMs.toFixed(3)}, ` +
              `tfjs=${tfjsRun.averageTimeMs.toFixed(3)}`);
        } else if (functionName === 'fit') {
          if (model instanceof tfconverter.GraphModel) {
            throw new Error('GraphModel does not support training');
          }
          const pyFitLog = pyRun as ModelTrainingBenchmarkRun;
          model.compile({
            loss: LOSS_MAP[pyFitLog.loss],
            optimizer: OPTIMIZER_MAP[pyFitLog.optimizer]
          });
github josdejong / mathjs / examples / units.js View on Github external
// load math.js (using node.js)
const math = require('mathjs')

// units can be created by providing a value and unit name, or by providing
// a string with a valued unit.
console.log('create units')
const a = math.unit(45, 'cm')
const b = math.unit('0.1m')
print(a) // 45 cm
print(b) // 0.1 m
console.log()

// units can be added, subtracted, and multiplied or divided by numbers and by other units
console.log('perform operations')
print(math.add(a, b)) // 55 cm
print(math.multiply(b, 2)) // 0.2 m
print(math.divide(math.unit('1 m'), math.unit('1 s'))) // 1 m / s
print(math.pow(math.unit('12 in'), 3)) // 1728 in^3
console.log()

// units can be converted to a specific type, or to a number
console.log('convert to another type or to a number')
print(b.to('cm')) // 10 cm  Alternatively: math.to(b, 'cm')
print(math.to(b, 'inch')) // 3.9370078740157 inch
print(b.toNumber('cm')) // 10
print(math.number(b, 'cm')) // 10
console.log()

// the expression parser supports units too
console.log('parse expressions')
print(math.eval('2 inch to cm')) // 5.08 cm
print(math.eval('cos(45 deg)')) // 0.70710678118655
github josdejong / mathjs / examples / basic_usage.js View on Github external
console.log()

// chained operations
console.log('chained operations')
const a = math.chain(3)
  .add(4)
  .multiply(2)
  .done()
print(a) // 14
console.log()

// mixed use of different data types in functions
console.log('mixed use of data types')
print(math.add(4, [5, 6])) // number + Array, [9, 10]
print(math.multiply(math.unit('5 mm'), 3)) // Unit * number,  15 mm
print(math.subtract([2, 3, 4], 5)) // Array - number, [-3, -2, -1]
print(math.add(math.matrix([2, 3]), [4, 5])) // Matrix + Array, [6, 8]
console.log()

/**
 * Helper function to output a value in the console. Value will be formatted.
 * @param {*} value
 */
function print (value) {
  const precision = 14
  console.log(math.format(value, precision))
}
github josdejong / mathjs / examples / units.js View on Github external
// units can be created by providing a value and unit name, or by providing
// a string with a valued unit.
console.log('create units')
const a = math.unit(45, 'cm')
const b = math.unit('0.1m')
print(a) // 45 cm
print(b) // 0.1 m
console.log()

// units can be added, subtracted, and multiplied or divided by numbers and by other units
console.log('perform operations')
print(math.add(a, b)) // 55 cm
print(math.multiply(b, 2)) // 0.2 m
print(math.divide(math.unit('1 m'), math.unit('1 s'))) // 1 m / s
print(math.pow(math.unit('12 in'), 3)) // 1728 in^3
console.log()

// units can be converted to a specific type, or to a number
console.log('convert to another type or to a number')
print(b.to('cm')) // 10 cm  Alternatively: math.to(b, 'cm')
print(math.to(b, 'inch')) // 3.9370078740157 inch
print(b.toNumber('cm')) // 10
print(math.number(b, 'cm')) // 10
console.log()

// the expression parser supports units too
console.log('parse expressions')
print(math.eval('2 inch to cm')) // 5.08 cm
print(math.eval('cos(45 deg)')) // 0.70710678118655
print(math.eval('90 km/h to m/s')) // 25 m / s
console.log()
github josdejong / mathjs / examples / units.js View on Github external
// convert a unit to a number
// A second parameter with the unit for the exported number must be provided
print(math.eval('number(5 cm, mm)')) // number, 50
console.log()

// simplify units
console.log('simplify units')
print(math.eval('100000 N / m^2')) // 100 kPa
print(math.eval('9.81 m/s^2 * 100 kg * 40 m')) // 39.24 kJ
console.log()

// example engineering calculations
console.log('compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol')
const Rg = math.unit('8.314 N m / (mol K)')
const T = math.unit('65 degF')
const P = math.unit('14.7 psi')
const v = math.divide(math.multiply(Rg, T), P)
console.log('gas constant (Rg) = ', format(Rg))
console.log('P = ' + format(P))
console.log('T = ' + format(T))
console.log('v = Rg * T / P = ' + format(math.to(v, 'L/mol')))
// 23.910432393453 L / mol
console.log()

console.log('compute speed of fluid flowing out of hole in a container')
const g = math.unit('9.81 m / s^2')
const h = math.unit('1 m')
const v2 = math.pow(math.multiply(2, math.multiply(g, h)), 0.5) // Can also use math.sqrt
console.log('g = ' + format(g))
console.log('h = ' + format(h))
console.log('v = (2 g h) ^ 0.5 = ' + format(v2))
github gkjohnson / webgl-gpu-power-estimation / scripts / fetch-techpowerup-specs.js View on Github external
parsedTmus,
                parsedRops,
            ] = shadersTmusRops.split(/[\\/]/g).map(s => parseInt(s));

        } catch (e) {

            console.error(e);
            console.error(`${ name }, ${ shadersTmusRops }`);
            console.error('');

        }

        // parse clock speed
        let parsedClock = null;
        try {
            parsedClock = math.unit(clock).toNumber('MHz');
        } catch (e) {
            console.error(e);
            console.error(`${ name }, ${ clock }`);
            console.error('');
        }

        // parse memory clock speed
        let parsedMemoryClock = null;
        try {
            parsedMemoryClock = math.unit(memoryClock).toNumber('MHz');
        } catch (e) {
            console.error(e);
            console.error(`${ name }, ${ memoryClock }`);
            console.error('');
        }
github josdejong / mathjs / examples / units.js View on Github external
// convert a unit to a number
// A second parameter with the unit for the exported number must be provided
print(math.eval('number(5 cm, mm)')) // number, 50
console.log()

// simplify units
console.log('simplify units')
print(math.eval('100000 N / m^2')) // 100 kPa
print(math.eval('9.81 m/s^2 * 100 kg * 40 m')) // 39.24 kJ
console.log()

// example engineering calculations
console.log('compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol')
const Rg = math.unit('8.314 N m / (mol K)')
const T = math.unit('65 degF')
const P = math.unit('14.7 psi')
const v = math.divide(math.multiply(Rg, T), P)
console.log('gas constant (Rg) = ', format(Rg))
console.log('P = ' + format(P))
console.log('T = ' + format(T))
console.log('v = Rg * T / P = ' + format(math.to(v, 'L/mol')))
// 23.910432393453 L / mol
console.log()

console.log('compute speed of fluid flowing out of hole in a container')
const g = math.unit('9.81 m / s^2')
const h = math.unit('1 m')
const v2 = math.pow(math.multiply(2, math.multiply(g, h)), 0.5) // Can also use math.sqrt
console.log('g = ' + format(g))
console.log('h = ' + format(h))
console.log('v = (2 g h) ^ 0.5 = ' + format(v2))
// 4.42944691807 m / s
github josdejong / mathjs / examples / units.js View on Github external
// example engineering calculations
console.log('compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol')
const Rg = math.unit('8.314 N m / (mol K)')
const T = math.unit('65 degF')
const P = math.unit('14.7 psi')
const v = math.divide(math.multiply(Rg, T), P)
console.log('gas constant (Rg) = ', format(Rg))
console.log('P = ' + format(P))
console.log('T = ' + format(T))
console.log('v = Rg * T / P = ' + format(math.to(v, 'L/mol')))
// 23.910432393453 L / mol
console.log()

console.log('compute speed of fluid flowing out of hole in a container')
const g = math.unit('9.81 m / s^2')
const h = math.unit('1 m')
const v2 = math.pow(math.multiply(2, math.multiply(g, h)), 0.5) // Can also use math.sqrt
console.log('g = ' + format(g))
console.log('h = ' + format(h))
console.log('v = (2 g h) ^ 0.5 = ' + format(v2))
// 4.42944691807 m / s
console.log()

console.log('electrical power consumption:')
const expr1 = '460 V * 20 A * 30 days to kWh'
console.log(expr1 + ' = ' + math.eval(expr1)) // 6624 kWh
console.log()

console.log('circuit design:')
const expr2 = '24 V / (6 mA)'
console.log(expr2 + ' = ' + math.eval(expr2)) // 4 kohm
console.log()
github heedy / heedy / 0.3 / frontend / js / sagas / analysis.js View on Github external
if (analysis.xdataset) {
    if (
      analysis.stream.length === 0 || analysis.stream.split("/").length != 3
    ) {
      yield put({
        type: "ANALYSIS_ERROR",
        value: "Invalid correlation stream name"
      });
      return;
    }
    query.stream = analysis.stream;
    query.transform = analysis.transform;
  } else {
    // Make sure that dt is a number
    try {
      let dt = math.eval(analysis.dt);
      if (isNaN(dt) || dt < 0.001) {
        yield put({
          type: "ANALYSIS_ERROR",
          value: "Invalid time delta (" + analysis.dt + ")"
        });
        return;
      }
      query.dt = dt;
    } catch (e) {
      yield put({
        type: "ANALYSIS_ERROR",
        value: "Invalid time delta (" + analysis.dt + ")"
      });
      return;
    }
  }
github firepick1 / firenodejs / js / firestep-driver.js View on Github external
var pulses = {
                p1: mpo["1"],
                p2: mpo["2"],
                p3: mpo["3"]
            };
            var xyz = that.delta.calcXYZ(pulses);
            that.mpoPlanSetPulses(mpo["1"], mpo["2"], mpo["3"], {
                log: "FireStepDriver.onIdle(initialized)"
            });
        } else {
            console.log("TTY\t: FireStepDriver.onIdle(waiting) ...");
        }
        if (that.mpoPlan) {
            that.model.mpo = JSON.parse(JSON.stringify(that.mpoPlan));
            // round for archival
            that.model.mpo.x = math.round(that.model.mpo.x, 3);
            that.model.mpo.y = math.round(that.model.mpo.y, 3);
            that.model.mpo.z = math.round(that.model.mpo.z, 3);
        }
        return that;
    };

mathjs

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif

Apache-2.0
Latest version published 8 days ago

Package Health Score

92 / 100
Full package analysis