-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
74 lines (68 loc) · 1.98 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { MathNode, derivative as _derive, simplify, parse } from 'mathjs';
import dotenv from "dotenv";
dotenv.config();
import WolframAlpha from "wolfram-alpha-api";
export const wolfram = new WolframAlpha(process.env.WOLFRAM_ID); // Create a new Wolfram Alpha API instance woth the correct key
function includes(arr: MathNode[], func: MathNode) {
for (const foundFunc of arr) {
if (foundFunc.equals(func)) return true;
}
return false;
}
export function derivatives(
f: MathNode,
independentVariable: string
): MathNode[] {
let derivatives: MathNode[] = [];
let func = f;
do {
derivatives.push(func);
func = _derive(func, independentVariable, {
simplify: true,
});
} while (!includes(derivatives, func));
return derivatives;
}
export function derive(
f: MathNode,
degree: number,
independentVariable: string
) {
if (degree === 0) return f;
return _derive(
derive(f, degree - 1, independentVariable),
independentVariable,
{
simplify: true,
}
);
}
export function equal(a: MathNode, b: MathNode) {}
export function isConstant(node: MathNode) {
if (node.isConstantNode) return true;
if ((node.isFunctionNode || node.isOperatorNode) && 'args' in node) {
for (const n of node.args) {
if (!isConstant(n)) return false;
}
return true;
}
return false;
}
/**
* Puts a mathematical function in its most expanded form
* @param func The function to be expanded
* @returns The expanded function
*/
export function expand(func: MathNode) {
return simplify(func, [
'log(e) -> 1',
'1 * n1 -> n1',
'0 + n1 -> n1',
'n1^ (n2 + n3) -> n1 ^ n2 * n1 ^ n3',
'n1 ^ 0 -> 1',
'n1 ^ 1 -> n1',
'(n1+n2)*n3 -> n1*n3 + n2*n3',
'c1 * n1+ c2 * n1 -> (c1+c2) * n1',
(simplify as any).rules.filter((r) => typeof r === 'function')[1], // simplifies constants
]);
}