Skip to content

Commit 1dcc6ca

Browse files
committed
docs: update to match new mathflow api
this commit adds a drafted documentation for `refactor/code` branch - [refactor/code](mathflowjs/mathflow@247acc8)
1 parent 0450bb1 commit 1dcc6ca

File tree

14 files changed

+432
-231
lines changed

14 files changed

+432
-231
lines changed

docs/.vitepress/config.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export default defineConfig({
3636
items: [
3737
{ text: 'Introduction', link: '/guide/' },
3838
{ text: 'Getting Started', link: '/guide/getting-started' },
39-
{ text: 'Configuration', link: '/guide/config' }
4039
]
4140
},
4241
{
@@ -49,15 +48,19 @@ export default defineConfig({
4948
]
5049
},
5150
{
52-
text: 'Examples',
51+
text: 'Reference',
5352
items: [
54-
{ text: 'Scripts', link: '/guide/scripts' },
55-
{ text: 'Programs', link: '/guide/programs' }
53+
{ text: 'Context API', link: '/api/' },
54+
{ text: 'Procedural API', link: '/api/procedural' },
55+
{ text: 'Error Handling', link: '/api/errors' },
56+
{ text: 'Rendering', link: '/api/rendering' },
5657
]
5758
},
5859
{
59-
text: 'API Reference',
60-
link: '/api/'
60+
text: 'Examples',
61+
items: [
62+
{ text: 'Samples', link: '/examples/' }
63+
]
6164
}
6265
],
6366

@@ -68,17 +71,17 @@ export default defineConfig({
6871
editLink: {
6972
text: 'Edit this page on GitHub',
7073
pattern:
71-
'https://github.com/mathflowjs/mathflow/edit/master/:path'
74+
'https://github.com/mathflowjs/mathflowjs.github.io/edit/master/docs/:path'
7275
},
7376

7477
footer: {
7578
message:
76-
'Released under the <a href="https://github.com/mathflowjs/mathflow/blob/master/LICENSE.md">MIT License</a>.',
79+
'Released under the <a href="https://github.com/mathflowjs/mathflow/blob/master/LICENSE.txt">MIT License</a>.',
7780
copyright:
78-
'Copyright &copy; 2024, <a href="https://github.com/henryhale">Henry Hale</a> - 2024-present MathFlow.'
81+
'Copyright &copy; 2024-present, <a href="https://github.com/henryhale">Henry Hale</a>.'
7982
}
8083
},
8184
markdown: {
82-
lineNumbers: true
85+
lineNumbers: false
8386
}
8487
});

docs/.vitepress/theme/assets/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:root {
22
--vp-c-brand-1: #4169e1;
33
--vp-c-brand-2: #747bff;
4-
--vp-home-hero-image-background-image: linear-gradient(180deg, var(--vp-home-hero-name-color) 50%,#27332a 50%);
4+
--vp-home-hero-image-background-image: linear-gradient(180deg, var(--vp-home-hero-name-color), var(--vp-home-hero-name-color));
55
--vp-home-hero-image-filter: blur(40px);
66
}
77

docs/api/errors.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Error Handling
2+
3+
## Overview
4+
5+
MathFlow throws errors for invalid syntax or unsupported operations. Use _try/catch_ to handle them:
6+
7+
```ts
8+
import { createContext } from 'mathflow';
9+
10+
const ctx = createContext();
11+
12+
try {
13+
ctx.solve('invalid = ');
14+
} catch (err) {
15+
console.error('MathFlow error:', err);
16+
}
17+
```
18+

docs/api/index.md

Lines changed: 181 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,203 @@
1-
# API Reference
1+
# Context API
22

3-
## evaluate()
3+
MathFlow configuration is managed through the properties of a context.
44

5-
Evaluate a source code written using MathFlow syntax
5+
Use the `ContextAPI` for most tasks. This provides a simple interface for solving, rendering, and managing variables/functions/constants.
66

7-
- **Type**
7+
## Creating a context
88

9-
```ts
10-
type Scope = {
11-
variables: Record<string, number>;
12-
};
9+
- **Type**: `function createContext(options?: Partial<ContextOptions>): ContextAPI`
1310

14-
type Result = {
15-
value: number;
16-
scope: Scope;
17-
solution: string[];
18-
};
11+
Use `createContext` to manage isolated or persistent variable scopes:
1912

20-
declare function evaluate(code: string): Result;
21-
```
13+
```ts
14+
import { createContext } from 'mathflow';
2215

23-
- **Details**
16+
const ctx = createContext({
17+
// ...
18+
})
19+
```
2420

25-
It takes one argument that must be a string containing MathFlow expressions.
21+
### preferences
22+
You can set preferences when creating a context, or update them at any time.
2623

27-
- **Example**
24+
```ts
25+
const ctx = createContext({
26+
preferences: {
27+
// key: value
28+
}
29+
})
30+
```
2831

29-
Adding two numbers: `1` and `2`
32+
```ts
33+
type Preferences = {
34+
angles: 'rad' | 'deg';
35+
fractionDigits: number;
36+
precision: number;
37+
};
38+
```
3039

31-
```ts
32-
import { evaluate } from 'mathflow';
40+
#### angles
3341

34-
const script = `
35-
# declare variables
36-
a = 1
37-
b = 2
38-
# compute sum
39-
c = a + b
40-
# return value
41-
c
42-
`;
42+
- **Type**: `rad` - radians | `deg` - degrees
43+
- **Default**: `rad`
4344

44-
const result = evaluate(script);
45+
Mode of angles - trignometric functions depend on `angles` to handle arguments and return values.
4546

46-
console.log(result);
47-
// Output: { value: 3, scope: { variables: { a: 1, b: 2, c: 3 } }, solution: ['3'] }
48-
```
47+
```ts
48+
const ctx = createContext({
49+
preferences: {
50+
angles: 'deg'
51+
}
52+
})
4953

50-
- **See also:** [Guide - Getting Started](../guide/getting-started.md) or [Guide - Syntax](../guide/basics.md) for details.
54+
// or
5155

52-
## config
56+
ctx.preferences.angles = 'deg'
5357

54-
Configure the behaviour of the compiler
58+
// example: sin(30) = 0.5 in degrees mode
59+
```
5560

56-
- **Type**
61+
#### fractionDigits
5762

58-
```ts
59-
type Config = {
60-
fractionDigits: number;
61-
};
62-
```
63+
- **Type**: `number`
64+
- **Default**: `15`
6365

64-
- **Details**
65-
66-
- `config.fractionDigits` sets the number of digits after the floating point.
67-
68-
- **Example**
69-
70-
```ts
71-
import { config, evaluate } from 'mathflow';
66+
Number of digits after the decimal point. Must be in the range `0` - `20`, inclusive.
7267

73-
config.fractionDigits = 3;
68+
```ts
69+
const ctx = createContext({
70+
preferences: {
71+
fractionDigits: 10
72+
}
73+
})
7474

75-
console.log(
76-
evaluate(`1/6`).value
77-
);
78-
//Output: 0.167
79-
```
75+
// or
76+
77+
ctx.preferences.fractionDigits = 'deg'
78+
```
79+
80+
#### precision
81+
82+
- **Type**: `number`
83+
- **Default**: `15`
84+
85+
Number of significant digits. Must be in the range 1 - 21, inclusive.
86+
87+
```ts
88+
const ctx = createContext({
89+
preferences: {
90+
precision: 20
91+
}
92+
})
93+
94+
// or
95+
96+
ctx.preferences.precision = 'deg'
97+
```
98+
99+
### variables
100+
You can provide predefined variables to the context at its creation:
101+
102+
- **Type**: `Record<string, number>`
103+
104+
```ts
105+
const ctx = createContext({
106+
variables: {
107+
x: 1,
108+
}
109+
})
110+
```
111+
112+
For an existing context, use:
113+
114+
- **Type**: `Map<string, number>`
115+
116+
```ts
117+
ctx.variables.set('x', 1)
118+
```
119+
120+
### constants
121+
122+
Like [variables](#variables), the constants can be defined in a similar way:
123+
124+
```ts
125+
const ctx = createContext({
126+
constants: {
127+
g: 9.81
128+
}
129+
})
130+
131+
// or
132+
133+
ctx.constants.set('g', 9.81)
134+
```
135+
136+
### functions
137+
138+
Add custom functions to a context.
139+
140+
- **Type**: `(...args: number[]) => number`
141+
142+
```ts
143+
const ctx = createContext({
144+
functions: {
145+
double: (x) => 2 * x,
146+
}
147+
})
148+
```
149+
150+
For an existing context, use:
151+
152+
- **Type**: `Map<string, number>`
153+
154+
```ts
155+
ctx.functions.set('double', (x) => 2 * x)
156+
```
157+
158+
## Solving expressions
159+
160+
### solve
161+
162+
Evaluate a single mathematical expression string
163+
164+
- **Type**: `(code: string) => ({ value: number, solution: string[] })`
165+
166+
```js
167+
ctx.solve('1 + 3 + 5')
168+
// { value: 9, solution: ['4 + 5', '9'] }
169+
```
170+
171+
### solveBatch
172+
173+
Evaluate multiple mathematical expressions at once
174+
175+
- **Type**: `(code: string) => ({ value: number, solution: string[] }[])`
176+
177+
```ts
178+
ctx.solveBatch(`x=2\ny=3x\nz=x+y`)
179+
// [
180+
// { value: 2, solution: ['2'] },
181+
// { value: 6, solution: ['3 * 2', '6'] },
182+
// { value: 8, solution: ['2 + 6', '8'] },
183+
// ]
184+
```
185+
186+
## Rendering expressions
187+
188+
### renderAsHTML
189+
190+
- **Type**:
191+
- [default]
192+
- [description]
193+
- [example]
194+
- [see-also]
195+
196+
### renderAsLaTeX
197+
198+
199+
- **Type**:
200+
- [default]
201+
- [description]
202+
- [example]
203+
- [see-also]

docs/api/procedural.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Procedural API
2+
3+
For full control, use the lower-level API. This is useful for step-by-step evaluation, custom solution tracking, or building your own tools.
4+
5+
## Overview
6+
7+
8+
### tokenize
9+
10+
### parse
11+
12+
### createSolutionStack
13+
14+
### evaluate
15+
16+
17+
## Step-by-step Example
18+
```ts
19+
import { createContext, tokenize, parse, evaluate, createSolutionStack } from 'mathflow';
20+
const ctx = createContext({ variables: { x: 1 } });
21+
const solution = createSolutionStack();
22+
const tokens = tokenize(ctx, '1+2+x');
23+
const ast = parse(tokens);
24+
const value = evaluate(ctx, ast.body[0], solution);
25+
console.log(value); // 4
26+
console.log(solution.steps); // step-by-step solution
27+
```

0 commit comments

Comments
 (0)