@matheos/core 0.2.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ import { type Shown } from './printer.js';
2
+ /** Checks that `to` can apply to a value (units.md wording) and converts; vectors and matrices element by element. */
3
+ export declare function convert(v: unknown, to?: string): unknown;
4
+ /** A value as it reads on its result line, in its display unit (`to`, else the units.md rule), `digits` significant, with
5
+ * the facts the substituted line asks for. Throws the units.md wording when `to` cannot apply, so a statement fails
6
+ * before its name is bound. */
7
+ export declare function shown(v: unknown, to?: string, digits?: number): Shown;
8
+ /** Result-line LaTeX of a value: `shown` without the facts. */
9
+ export declare function display(v: unknown, to?: string, digits?: number): string;
@@ -0,0 +1,74 @@
1
+ import { math, isUnit, isNA, isForeignUnit, isDimensionless, isMatrixLike, displayUnit, displayName, unitLatex } from './units.js';
2
+ import { print, formatNumber, text } from './printer.js';
3
+ /** Checks that `to` can apply to a value (units.md wording) and converts; vectors and matrices element by element. */
4
+ export function convert(v, to) {
5
+ if (!to || isNA(v))
6
+ return v;
7
+ if (to === '%') {
8
+ if (typeof v === 'number' || isDimensionless(v))
9
+ return v;
10
+ throw new Error('only a dimensionless value can be shown in %');
11
+ }
12
+ for (const sym of math.parse(to).filter((x) => x.type === 'SymbolNode'))
13
+ if (isForeignUnit(sym.name) || !math.Unit.isValuelessUnit(sym.name))
14
+ throw new Error(`unknown unit "${sym.name}"`); // outside the table, or no unit at all
15
+ if (isMatrixLike(v))
16
+ return math.map(v, (e) => convert(e, to));
17
+ if (!isUnit(v))
18
+ throw new Error(`cannot convert a number to ${to}`);
19
+ try {
20
+ return v.to(to);
21
+ }
22
+ catch {
23
+ throw new Error(`cannot convert ${displayName(v.formatUnits())} to ${to}`);
24
+ }
25
+ }
26
+ /** A value as it reads on its result line, in its display unit (`to`, else the units.md rule), `digits` significant, with
27
+ * the facts the substituted line asks for. Throws the units.md wording when `to` cannot apply, so a statement fails
28
+ * before its name is bound. */
29
+ export function shown(v, to, digits = 4) {
30
+ v = convert(v, to);
31
+ if (to === '%') {
32
+ const n = typeof v === 'number' ? v : isDimensionless(v) ? Number(v.value) : NaN;
33
+ return { tex: `${formatNumber(n * 100, digits)}\\,\\%`, negative: n < 0, unit: true };
34
+ }
35
+ if (typeof v === 'number')
36
+ return { tex: formatNumber(v, digits), negative: v < 0 };
37
+ if (typeof v === 'boolean')
38
+ return { tex: `\\mathrm{${v}}` };
39
+ if (isNA(v))
40
+ return { tex: '\\text{n/a}' };
41
+ if (typeof v === 'function')
42
+ return { tex: `\\text{${v.syntax ?? 'function'}}` }; // a bare function name: its signature, not the compiled JS
43
+ if (typeof v === 'string')
44
+ return { tex: text(v) };
45
+ if (typeof v === 'object' && v !== null && v.isNode) { // derivative / simplify: a symbolic result, every symbol a variable
46
+ const tex = print(v, 'formula', { isVar: () => true });
47
+ return { tex, negative: tex.startsWith('-') }; // ponytail: read from its own fresh print; a symbolic result substituted into a product needs brackets as a whole, add when it comes up
48
+ }
49
+ if (isUnit(v)) {
50
+ const unit = displayUnit(v, to);
51
+ const n = v.toNumber(unit);
52
+ return { tex: `${formatNumber(n, digits)}\\,${unitLatex(unit)}`, negative: n < 0, unit: true };
53
+ }
54
+ if (isMatrixLike(v)) {
55
+ const rows = (Array.isArray(v) ? v : v.toArray());
56
+ // one unit shared by every element is written once, after the bracket: [5.6 0.0066] kN/mm
57
+ const flat = rows.flat();
58
+ const units = flat.map((x) => (isUnit(x) ? displayUnit(x, to) : null));
59
+ const shared = flat.length && units.every((u) => u !== null && u === units[0]) ? units[0] : undefined;
60
+ const cell = (x) => (shared ? formatNumber(x.toNumber(shared), digits) : display(x, to, digits));
61
+ const row = (r) => (Array.isArray(r) ? r : [r]).map(cell).join(' & ');
62
+ const body = !rows.some(Array.isArray) ? row(rows) : rows.map(row).join(' \\\\ '); // a vector reads as a row
63
+ return { tex: `\\begin{bmatrix} ${body} \\end{bmatrix}${shared ? `\\,${unitLatex(shared)}` : ''}`, unit: !!shared };
64
+ }
65
+ if (typeof v === 'object' && v !== null) { // a catalogue row: its name, or the first keys
66
+ const o = v;
67
+ const name = o['Name'] ?? o['name'] ?? o['id'];
68
+ return { tex: text(String(name ?? `${Object.keys(o).slice(0, 4).join(', ')}, …`)) };
69
+ }
70
+ return { tex: String(v) };
71
+ }
72
+ /** Result-line LaTeX of a value: `shown` without the facts. */
73
+ export function display(v, to, digits = 4) { return shown(v, to, digits).tex; }
74
+ //# sourceMappingURL=display.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"display.js","sourceRoot":"","sources":["../src/display.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnI,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,IAAI,EAAc,MAAM,cAAc,CAAC;AAErE,sHAAsH;AACtH,MAAM,UAAU,OAAO,CAAC,CAAU,EAAE,EAAW;IAC7C,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC;IAC7B,IAAI,EAAE,KAAK,GAAG,EAAE,CAAC;QACf,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,eAAe,CAAC,CAAC,CAAC;YAAE,OAAO,CAAC,CAAC;QAC1D,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAClE,CAAC;IACD,KAAK,MAAM,GAAG,IAAK,IAAI,CAAC,KAAK,CAAC,EAAE,CAAc,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,YAAY,CAAkC;QAAE,IAAI,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,M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AAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAG,0BAA0B;QAC/G,OAAO,EAAE,GAAG,EAAE,oBAAoB,IAAI,kBAAkB,MAAM,CAAC,CAAC,CAAC,MAAM,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC;IACtH,CAAC;IACD,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAG,+CAA+C;QAC1F,MAAM,CAAC,GAAG,CAA4B,CAAC;QACvC,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;QAC/C,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;IACtF,CAAC;IACD,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;AAC5B,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,OAAO,CAAC,CAAU,EAAE,EAAW,EAAE,MAAM,GAAG,CAAC,IAAY,OAAO,KAAK,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC"}
@@ -8,6 +8,40 @@ export interface EvalOptions {
8
8
  /** `c_z'` in an expression: the prime is part of the name (`c_z$p` for math.js, which would read `'` as transpose); strings are left alone. */
9
9
  export declare function primes(s: string): string;
10
10
  export declare function evaluate(doc: Document, options?: EvalOptions): Rendered;
11
+ /** Front-matter catalogues → bindings: each JSON table (an array, or `{ rows, units }`) becomes an object of rows keyed by
12
+ * the key column (`Name`, else name / id / position), numeric columns carrying the units given (the table's own, then the
13
+ * front matter's). Errors are messages; the caller places them. */
14
+ export declare function catalogueTables(catalogs: Record<string, {
15
+ file: string;
16
+ units?: Record<string, string>;
17
+ key?: string;
18
+ }>, resolve?: EvalOptions['resolve']): {
19
+ bindings: Map<string, unknown>;
20
+ errors: string[];
21
+ };
22
+ /** The second pass's source: every prose loop's expansion in place of its lines, and the map from a line of the spliced
23
+ * text back to the source line (every expanded line points at the loop's first line) for the preview and the CLI. */
24
+ export declare function spliceLoops(source: string, loops: {
25
+ start: number;
26
+ end: number;
27
+ text: string;
28
+ }[]): {
29
+ text: string;
30
+ line: (n: number) => number;
31
+ };
32
+ /** The `?`-marked inputs of a document, in order, inside branches and loops too: what the parameters view edits in place. */
33
+ export declare function parameters(doc: Document): {
34
+ name: string;
35
+ expr: string;
36
+ caption?: string;
37
+ line: number;
38
+ options?: string[];
39
+ }[];
40
+ /** The loop variable as text, inside `{…}` placeholders and inside fenced math/plot lines; prose text is left alone. */
41
+ export declare function substituteVar(body: string, variable: string, value: number): string;
11
42
  /** Every rendered statement, loops included, in document order. */
12
43
  export declare function statements(r: Rendered): RenderedStatement[];
44
+ /** The statement shown for each source line. A `for` loop renders a line once per iteration: the last one stands for the
45
+ * line, in the editor and in the dependency graph alike. */
46
+ export declare function byLine(items: RenderedStatement[]): Map<number, RenderedStatement>;
13
47
  export type { Pos };