|
1 | | -# API Reference |
| 1 | +# Context API |
2 | 2 |
|
3 | | -## evaluate() |
| 3 | +MathFlow configuration is managed through the properties of a context. |
4 | 4 |
|
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. |
6 | 6 |
|
7 | | -- **Type** |
| 7 | +## Creating a context |
8 | 8 |
|
9 | | - ```ts |
10 | | - type Scope = { |
11 | | - variables: Record<string, number>; |
12 | | - }; |
| 9 | +- **Type**: `function createContext(options?: Partial<ContextOptions>): ContextAPI` |
13 | 10 |
|
14 | | - type Result = { |
15 | | - value: number; |
16 | | - scope: Scope; |
17 | | - solution: string[]; |
18 | | - }; |
| 11 | +Use `createContext` to manage isolated or persistent variable scopes: |
19 | 12 |
|
20 | | - declare function evaluate(code: string): Result; |
21 | | - ``` |
| 13 | +```ts |
| 14 | +import { createContext } from 'mathflow'; |
22 | 15 |
|
23 | | -- **Details** |
| 16 | +const ctx = createContext({ |
| 17 | + // ... |
| 18 | +}) |
| 19 | +``` |
24 | 20 |
|
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. |
26 | 23 |
|
27 | | -- **Example** |
| 24 | +```ts |
| 25 | +const ctx = createContext({ |
| 26 | + preferences: { |
| 27 | + // key: value |
| 28 | + } |
| 29 | +}) |
| 30 | +``` |
28 | 31 |
|
29 | | - Adding two numbers: `1` and `2` |
| 32 | +```ts |
| 33 | +type Preferences = { |
| 34 | + angles: 'rad' | 'deg'; |
| 35 | + fractionDigits: number; |
| 36 | + precision: number; |
| 37 | +}; |
| 38 | +``` |
30 | 39 |
|
31 | | - ```ts |
32 | | - import { evaluate } from 'mathflow'; |
| 40 | +#### angles |
33 | 41 |
|
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` |
43 | 44 |
|
44 | | - const result = evaluate(script); |
| 45 | +Mode of angles - trignometric functions depend on `angles` to handle arguments and return values. |
45 | 46 |
|
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 | +}) |
49 | 53 |
|
50 | | -- **See also:** [Guide - Getting Started](../guide/getting-started.md) or [Guide - Syntax](../guide/basics.md) for details. |
| 54 | +// or |
51 | 55 |
|
52 | | -## config |
| 56 | +ctx.preferences.angles = 'deg' |
53 | 57 |
|
54 | | -Configure the behaviour of the compiler |
| 58 | +// example: sin(30) = 0.5 in degrees mode |
| 59 | +``` |
55 | 60 |
|
56 | | -- **Type** |
| 61 | +#### fractionDigits |
57 | 62 |
|
58 | | - ```ts |
59 | | - type Config = { |
60 | | - fractionDigits: number; |
61 | | - }; |
62 | | - ``` |
| 63 | +- **Type**: `number` |
| 64 | +- **Default**: `15` |
63 | 65 |
|
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. |
72 | 67 |
|
73 | | - config.fractionDigits = 3; |
| 68 | +```ts |
| 69 | +const ctx = createContext({ |
| 70 | + preferences: { |
| 71 | + fractionDigits: 10 |
| 72 | + } |
| 73 | +}) |
74 | 74 |
|
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] |
0 commit comments