1 |
|
2 |
|
3 |
|
4 | const math = require('../index')
|
5 |
|
6 |
|
7 |
|
8 | console.log('create units')
|
9 | const a = math.unit(45, 'cm')
|
10 | const b = math.unit('0.1m')
|
11 | print(a)
|
12 | print(b)
|
13 | console.log()
|
14 |
|
15 |
|
16 | console.log('perform operations')
|
17 | print(math.add(a, b))
|
18 | print(math.multiply(b, 2))
|
19 | print(math.divide(math.unit('1 m'), math.unit('1 s')))
|
20 | print(math.pow(math.unit('12 in'), 3))
|
21 | console.log()
|
22 |
|
23 |
|
24 | console.log('convert to another type or to a number')
|
25 | print(b.to('cm'))
|
26 | print(math.to(b, 'inch'))
|
27 | print(b.toNumber('cm'))
|
28 | print(math.number(b, 'cm'))
|
29 | console.log()
|
30 |
|
31 |
|
32 | console.log('parse expressions')
|
33 | print(math.eval('2 inch to cm'))
|
34 | print(math.eval('cos(45 deg)'))
|
35 | print(math.eval('90 km/h to m/s'))
|
36 | console.log()
|
37 |
|
38 |
|
39 |
|
40 | print(math.eval('number(5 cm, mm)'))
|
41 | console.log()
|
42 |
|
43 |
|
44 | console.log('simplify units')
|
45 | print(math.eval('100000 N / m^2'))
|
46 | print(math.eval('9.81 m/s^2 * 100 kg * 40 m'))
|
47 | console.log()
|
48 |
|
49 |
|
50 | console.log('compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol')
|
51 | const Rg = math.unit('8.314 N m / (mol K)')
|
52 | const T = math.unit('65 degF')
|
53 | const P = math.unit('14.7 psi')
|
54 | const v = math.divide(math.multiply(Rg, T), P)
|
55 | console.log('gas constant (Rg) = ', format(Rg))
|
56 | console.log('P = ' + format(P))
|
57 | console.log('T = ' + format(T))
|
58 | console.log('v = Rg * T / P = ' + format(math.to(v, 'L/mol')))
|
59 |
|
60 | console.log()
|
61 |
|
62 | console.log('compute speed of fluid flowing out of hole in a container')
|
63 | const g = math.unit('9.81 m / s^2')
|
64 | const h = math.unit('1 m')
|
65 | const v2 = math.pow(math.multiply(2, math.multiply(g, h)), 0.5)
|
66 | console.log('g = ' + format(g))
|
67 | console.log('h = ' + format(h))
|
68 | console.log('v = (2 g h) ^ 0.5 = ' + format(v2))
|
69 |
|
70 | console.log()
|
71 |
|
72 | console.log('electrical power consumption:')
|
73 | const expr1 = '460 V * 20 A * 30 days to kWh'
|
74 | console.log(expr1 + ' = ' + math.eval(expr1))
|
75 | console.log()
|
76 |
|
77 | console.log('circuit design:')
|
78 | const expr2 = '24 V / (6 mA)'
|
79 | console.log(expr2 + ' = ' + math.eval(expr2))
|
80 | console.log()
|
81 |
|
82 | console.log('operations on arrays:')
|
83 | const B = math.eval('[1, 0, 0] T')
|
84 | const v3 = math.eval('[0, 1, 0] m/s')
|
85 | const q = math.eval('1 C')
|
86 | const F = math.multiply(q, math.cross(v3, B))
|
87 | console.log('B (magnetic field strength) = ' + format(B))
|
88 | console.log('v (particle velocity) = ' + format(v3))
|
89 | console.log('q (particle charge) = ' + format(q))
|
90 | console.log('F (force) = q (v cross B) = ' + format(F))
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | function print (value) {
|
97 | console.log(format(value))
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function format (value) {
|
106 | const precision = 14
|
107 | return math.format(value, precision)
|
108 | }
|