Skip to content

Utilities for functions and definitions (needed for polypad user-defined fns support) #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export abstract class ExprElement {
*/
recursiveSubstitute(vars: ExprMap): ExprElement {
const varList = Object.keys(vars);
if (!this.unknowns.filter(v => varList.includes(v)).length) return this;
const unknown = [...this.unknowns, ...this.functions];
if (!unknown.filter(v => varList.includes(v)).length) return this;
return this.substitute(vars).recursiveSubstitute(vars);
}

Expand Down
104 changes: 100 additions & 4 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export class ExprFunction extends ExprElement {

if (this.fn in vars) {
const fn = vars[this.fn];
if (fn instanceof ExprFunctionDefinition) {
const nextMap = {...vars};
for (const [position, paramName] of fn.params.entries()) {
nextMap[paramName] = args[position];
}
return fn.evaluate(nextMap);
}
if (typeof fn === 'function') return fn(...args);
if (typeof fn === 'number' && args.length === 1) return evaluate.mul(fn, args[0]);
throw ExprError.uncallableExpression(this.fn);
Expand Down Expand Up @@ -86,13 +93,22 @@ export class ExprFunction extends ExprElement {
throw ExprError.undefinedFunction(this.fn);
}

substitute(vars: ExprMap = {}) {
return new ExprFunction(this.fn, this.args.map(a => a.substitute(vars)));
substitute(vars: ExprMap = {}): ExprElement {
const args = this.args.map(a => a.substitute(vars));
if (this.fn in vars && vars[this.fn] instanceof ExprFunctionDefinition) {
return (vars[this.fn] as ExprFunctionDefinition).applyExpressions(args);
}
return new ExprFunction(this.fn, args);
}

collapse() {
collapse(): ExprElement {
if (this.fn === '(') return this.args[0].collapse();
return new ExprFunction(this.fn, this.args.map(a => a.collapse()));
const collapsedArgs = this.args.map(a => a.collapse());
const arg0 = collapsedArgs[0];
if (this.fn === '=' && isFunctionHead(arg0)) {
return new ExprFunctionDefinition(arg0.fn, arg0.unknowns, collapsedArgs[1]);
}
return new ExprFunction(this.fn, collapsedArgs);
}

get simplified() {
Expand Down Expand Up @@ -133,6 +149,18 @@ export class ExprFunction extends ExprElement {
return `${this.fn}(${args.join(', ')})`;
}

partialEvaluate(vars: ExprMap = {}) {
const base = this.substitute(vars);
const fn: CustomFunction = (...args) => {
const vm: Record<string, number> = {};
for (const [index, arg] of base.unknowns.entries()) {
vm[arg] = args[index];
}
return base.evaluate(vm);
};
return fn;
}

toMathML(custom: MathMLMap = {}) {
const args = this.args.map(a => a.toMathML(custom));
const argsF = this.args.map((a, i) => addMFence(a, this.fn, args[i]));
Expand Down Expand Up @@ -246,6 +274,74 @@ export class ExprFunction extends ExprElement {
}
}

const LEGAL_CUSTOM_FUNCTION_NAME = new RegExp('^\\p{L}+$', 'u');

function isFunctionHead<E extends ExprElement>(expr: E | ExprFunction): expr is ExprFunction {
return expr instanceof ExprFunction && LEGAL_CUSTOM_FUNCTION_NAME.test(expr.fn);
}

export class ExprFunctionDefinition extends ExprElement {
private _body: ExprElement;
constructor(readonly name: string, readonly params: string[], body: ExprElement) {
super();
this._body = body;
}

get body() {
return this._body;
}

private set body(b: ExprElement) {
this._body = b;
}

get unknowns() {
return this.body.unknowns.filter(u => !this.params.includes(u));
}

get variables() {
return this.body.variables.filter(v => !this.params.includes(v));
}

get functions() {
return this.body.functions;
}

withSubstitutedBody(vars: ExprMap) {
const n = new ExprFunctionDefinition(this.name, this.params, this.body);
n.bodySubstitute(vars);
return n;
}

bodySubstitute(vars: ExprMap) { // TODO: ensure that param vars dont get substituted
this.body = this.body.substitute(vars);
}

applyExpressions(args: ExprElement[]) {
const exprMap: ExprMap = {};
for (const [index, param] of this.params.entries()) {
exprMap[param] = args[index];
}
return this.body.substitute(exprMap);
}

applyVals = (...args: number[]) => {
const varMap: VarMap = {};
for (const [index, param] of this.params.entries()) {
varMap[param] = args[index];
}
return this.body.evaluate(varMap);
};

evaluate(vars: VarMap) {
return this.body.evaluate(vars);
}

toString() {
return `${this.name}(${this.params.join(',')})=${this.body}`;
}
}

// -----------------------------------------------------------------------------

export class ExprTerm extends ExprElement {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
export {ExprError} from './errors';
export {Expression} from './expression';
export {ExprElement, ExprIdentifier, ExprNumber, ExprOperator} from './elements';
export {ExprFunction} from './functions';
export {ExprFunction, ExprFunctionDefinition} from './functions';
export {CONSTANTS as HILBERT_CONSTANTS, SPECIAL_IDENTIFIERS, isSpecialFunction} from './symbols';
export {hasZero, Interval, isWhole, width} from './eval';