Skip to content

Commit 0d6ac33

Browse files
committed
Help for imported functions
1 parent 0c1e573 commit 0d6ac33

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

ToDo.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ ToDo
1515
- [x] plot types
1616
- [ ] doc types
1717
- [x] result type
18-
- [ ] Document plots
19-
- [ ] Add plotly
18+
- [x] Document plots
19+
- [x] Add plotly
2020
- [x] More tests
2121
- [ ] Format plot output
2222

@@ -25,7 +25,7 @@ ToDo
2525
- [ ] Variables in scope
2626
- [ ] Help files
2727
- [x] Eval expressions vs blocks
28-
- [ ] Custom help
28+
- [x] Custom help
2929
- [ ] Function finder
3030
- [ ] Variable finder
3131
- [x] Drag and Drop documents
@@ -39,4 +39,4 @@ ToDo
3939
- [ ] IO
4040
- [ ] New nomenclature like iodide
4141
- [ ] File names
42-
- [ ] Dropdown or tabs
42+
- [ ] Dropdown or tabs

public/mathWorker.js

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,95 @@ importScripts(
77
const parser = math.parser()
88
const digits = 14
99

10+
function createNewHelp() {
11+
const oldHelp = math.help
12+
const newDocs = {
13+
props: {
14+
name: 'props',
15+
category: 'physics',
16+
description: 'Get properties of pure fluids, pseudo-pure fluids and mixtures according to coolprop.',
17+
syntax: [
18+
"props(desired property, name of fluid, an object with two physical properties)"
19+
],
20+
examples: [
21+
"props('D','Water',{T:0 degC,'P|liquid':1atm })",
22+
"props('D', 'CO2', {T:298.15 K, P:100e5 Pa})"
23+
],
24+
seealso: ['HAprops', 'phase', 'MM']
25+
},
26+
HAprops: {
27+
name: 'HAprops',
28+
category: 'physics',
29+
description: 'Get properties of pure humid air according to coolprop.',
30+
syntax: [
31+
"HAprops(desired property, an object with three physical properties)"
32+
],
33+
examples: [
34+
"HAprops('H',{T:298.15 K, P:101325 Pa, R:0.5})",
35+
"HAprops('T',{P:1 atm, H:50 kJ/kg, R:1.0})",
36+
"HAprops('T',{H: 50 kJ/kg, R:50%, P:1 atm})"
37+
],
38+
seealso: ['props', 'phase', 'MM']
39+
},
40+
phase: {
41+
name: 'phase',
42+
category: 'physics',
43+
description: 'Get the phase of pure fluids, pseudo-pure fluids and mixtures according to coolprop.',
44+
syntax: [
45+
"phase( name of fluid, an object with two physical properties)"
46+
],
47+
examples: [
48+
"phase('Water',{T:0 degC,'P|liquid':1atm })",
49+
"phase('CO2', {T:298.15 K, P:100e5 Pa})"
50+
],
51+
seealso: ['props', 'HAprops', 'MM']
52+
},
53+
MM: {
54+
name: 'MM',
55+
category: 'chemistry',
56+
description: 'Get the molecular mass of a chemical formula and a list of properties.',
57+
syntax: [
58+
"MM(chemical formula)"
59+
],
60+
examples: [
61+
"water = MM('H2O')",
62+
"water.fraction.O",
63+
"MM('C6H12O6')"
64+
],
65+
seealso: ['props', 'HAprops', 'phase']
66+
},
67+
plot: {
68+
name: 'plot',
69+
category: 'plotting',
70+
description: 'Create a plot according to ploty syntax.',
71+
syntax: [
72+
"plot(data, layout, config)",
73+
"plot(data, layout)",
74+
"plot(data)"
75+
],
76+
examples: [
77+
"plot([{x: [1, 2, 3], y: [2, 1, 3]}])",
78+
"plot([{x: [1, 2, 3], y: [2, 1, 3]}], {title: 'My plot'})",
79+
"plot([{x: [1, 2, 3], y: [2, 1, 3]}], {title: 'My plot'}, {responsive: true})"
80+
],
81+
seealso: ['sin', 'cos', 'tan']
82+
}
83+
}
84+
85+
return function help(x) {
86+
const nameString = typeof x === 'function' ? x.name : x
87+
if (nameString in newDocs) {
88+
return new math.Help(newDocs[nameString])
89+
} else {
90+
return oldHelp(x)
91+
}
92+
}
93+
}
94+
95+
math.import({
96+
help: createNewHelp()
97+
}, { override: true })
98+
1099
/**
11100
* Applies a given function to each element of an array or matrix.
12101
*
@@ -32,13 +121,13 @@ const functionsToVectorize =
32121
math.import(
33122
{
34123
props, HAprops, phase, MM,
35-
plot: math.typed({
124+
plot: math.typed('plot',{
36125
'Array, Object, Object': plot,
37126
'Array, Object': (data, layout) => plot(data, layout, {}),
38127
'Array': data => plot(data, {}, {}),
39128
}),
40129
...Object.fromEntries(functionsToVectorize.map(f => [f, mapped(math[f])])),
41-
log: math.typed({
130+
log: math.typed('log',{
42131
'Array | Matrix': x => math.map(x, x1 => math.log(x1, math.e)),
43132
'Array | Matrix, number': (x, base) => math.map(x, x1 => math.log(x1, base))
44133
})

0 commit comments

Comments
 (0)