UNPKG

3.75 kBJavaScriptView Raw
1// units
2
3// load math.js (using node.js)
4var math = require('../index');
5
6// units can be created by providing a value and unit name, or by providing
7// a string with a valued unit.
8console.log('create units');
9var a = math.unit(45, 'cm');
10var b = math.unit('0.1m');
11print(a); // 45 cm
12print(b); // 0.1 m
13console.log();
14
15// units can be added, subtracted, and multiplied or divided by numbers and by other units
16console.log('perform operations');
17print(math.add(a, b)); // 55 cm
18print(math.multiply(b, 2)); // 0.2 m
19print(math.divide(math.unit('1 m'), math.unit('1 s'))); // 1 m / s
20print(math.pow(math.unit('12 in'), 3)); // 1728 in^3
21console.log();
22
23// units can be converted to a specific type, or to a number
24console.log('convert to another type or to a number');
25print(b.to('cm')); // 10 cm Alternatively: math.to(b, 'cm')
26print(math.to(b, 'inch')); // 3.9370078740157 inch
27print(b.toNumber('cm')); // 10
28print(math.number(b, 'cm')); // 10
29console.log();
30
31// the expression parser supports units too
32console.log('parse expressions');
33print(math.eval('2 inch to cm')); // 5.08 cm
34print(math.eval('cos(45 deg)')); // 0.70710678118655
35print(math.eval('90 km/h to m/s')); // 25 m / s
36console.log();
37
38// convert a unit to a number
39// A second parameter with the unit for the exported number must be provided
40print(math.eval('number(5 cm, mm)')); // number, 50
41console.log();
42
43// simplify units
44console.log('simplify units');
45print(math.eval('100000 N / m^2')); // 100 kPa
46print(math.eval('9.81 m/s^2 * 100 kg * 40 m')); // 39.24 kJ
47console.log();
48
49// example engineering calculations
50console.log('compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol');
51var Rg = math.unit('8.314 N m / (mol K)');
52var T = math.unit('65 degF');
53var P = math.unit('14.7 psi');
54var v = math.divide(math.multiply(Rg, T), P);
55console.log('gas constant (Rg) = ', format(Rg));
56console.log('P = ' + format(P));
57console.log('T = ' + format(T));
58console.log('v = Rg * T / P = ' + format(math.to(v, 'L/mol')));
59 // 23.910432393453 L / mol
60console.log();
61
62console.log('compute speed of fluid flowing out of hole in a container');
63var g = math.unit('9.81 m / s^2');
64var h = math.unit('1 m');
65var v = math.pow(math.multiply(2, math.multiply(g, h)), 0.5); // Can also use math.sqrt
66console.log('g = ' + format(g));
67console.log('h = ' + format(h));
68console.log('v = (2 g h) ^ 0.5 = ' + format(v));
69 // 4.42944691807 m / s
70console.log();
71
72console.log('electrical power consumption:');
73var expr = '460 V * 20 A * 30 days to kWh';
74console.log(expr + ' = ' + math.eval(expr)); // 6624 kWh
75console.log();
76
77console.log('circuit design:');
78var expr = '24 V / (6 mA)';
79console.log(expr + ' = ' + math.eval(expr)); // 4 kohm
80console.log();
81
82console.log('operations on arrays:');
83var B = math.eval('[1, 0, 0] T');
84var v = math.eval('[0, 1, 0] m/s');
85var q = math.eval('1 C');
86var F = math.multiply(q, math.cross(v, B));
87console.log('B (magnetic field strength) = ' + format(B)); // [1 T, 0 T, 0 T]
88console.log('v (particle velocity) = ' + format(v)); // [0 m / s, 1 m / s, 0 m / s]
89console.log('q (particle charge) = ' + format(q)); // 1 C
90console.log('F (force) = q (v cross B) = ' + format(F)); // [0 N, 0 N, -1 N]
91
92/**
93 * Helper function to output a value in the console. Value will be formatted.
94 * @param {*} value
95 */
96function print (value) {
97 console.log(format(value));
98}
99
100/**
101 * Helper function to format an output a value.
102 * @param {*} value
103 * @return {string} Returns the formatted value
104 */
105function format (value) {
106 var precision = 14;
107 return math.format(value, precision);
108}