@khanacademy/kas
Version:
A lightweight JavaScript CAS for comparing expressions and equations.
447 lines (446 loc) • 11.9 kB
TypeScript
type Hints = {
parens?: boolean;
divide?: boolean;
root?: boolean;
subtract?: boolean;
fraction?: boolean;
entered?: boolean;
negate?: boolean;
open?: boolean;
};
type Vars = Record<string, string>;
type Options = {
preciseFloats?: boolean;
once?: boolean;
};
declare abstract class Expr {
hints: Hints;
constructor();
abstract func: {
new (...args: any[]): any;
name: string;
};
abstract args(): (number | string | Expr | undefined)[];
construct(args: any[]): any;
recurse(method: string, ...passed: any[]): this;
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
compile(): (vars: Expr[]) => string;
abstract print(): string;
abstract tex(): string;
asTex(options: any): string;
name(): string;
repr(): string;
strip(): Expr;
normalize(): Expr;
expand(): Expr;
factor(options: any): Expr;
collect(options?: Options): Expr;
equals(other: Expr): boolean;
simplify(options?: Options): any;
isSimplified(): boolean;
exprArgs(): Expr[];
getVars(excludeFunc?: boolean): any[];
getConsts(): any[];
getUnits(): any[];
is(func: any): boolean;
has(func: any): boolean;
raiseToThe(exp: Expr, options?: {
preciseFloats?: boolean;
}): Expr;
isSubtract(): boolean;
isDivide(): boolean;
isRoot(): boolean;
needsExplicitMul(): boolean;
sameVars(other: Expr): {
equal: boolean;
equalIgnoringCase: boolean;
};
compare(other: Expr): boolean;
partialEval(vars: Vars): Expr;
sameForm(other: Expr): boolean;
findGCD(factor: Expr): Expr;
getDenominator(): Expr;
asMul(): Mul;
isPositive(): boolean;
isNegative(): boolean;
asPositiveFactor(): Expr;
addHint(hint: keyof Hints): any;
completeParse(): Expr;
abs(): Expr;
negate(): Expr;
}
declare abstract class Seq extends Expr {
terms: Expr[];
constructor(...args: any[]);
args(): Expr[];
normalize(): any;
expand(): Expr;
partition(): [Seq, Seq];
flatten(): Expr;
abstract reduce(options?: Options): Expr;
isPositive(): boolean;
replace(oldTerm: Expr | number, newTerm?: Expr | Expr[]): any;
remove(term: number | Expr): any;
getDenominator(): Expr;
}
export declare class Add extends Seq {
identity: Num;
func: typeof Add;
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
print(): string;
tex(): string;
collect(options?: Options): Expr;
factor(options?: {
keepNegative: boolean;
}): Expr;
reduce(options?: Options): Expr;
needsExplicitMul(): boolean;
isNegative(): boolean;
negate(): Add;
static createOrAppend(left: Expr, right: Expr): Add;
}
export declare class Mul extends Seq {
identity: Num;
func: typeof Mul;
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
print(): string;
getUnits(): any[];
tex(): string;
strip(): Expr;
expand(): Expr;
factor(options: any): Expr;
collect(options?: Options): Expr;
isSubtract(): boolean;
factorIn(hint: keyof Hints): Expr;
factorOut(): Expr | Mul;
reduce(options?: {
preciseFloats: boolean;
}): Num;
findGCD(factor: Expr): Expr;
asMul(): this;
asPositiveFactor(): Expr;
isNegative(): boolean;
fold(): Expr;
negate(): any;
static handleNegative(expr: Expr, hint?: any): Expr;
static handleDivide(left: Expr, right: Expr): Expr;
static fold(expr: Expr): Expr;
static createOrAppend(left: Expr, right: Expr): Mul;
}
export declare class Pow extends Expr {
base: Expr;
exp: Expr;
constructor(base: any, exp: any);
func: typeof Pow;
args(): Expr[];
eval(vars?: Vars, options?: ParseOptions): number;
getUnits(): {
unit: any;
pow: number;
}[];
codegen(): string;
print(): string;
tex(): string;
needsExplicitMul(): boolean;
expand(): any;
factor(): Mul | this;
collect(options?: Options): Expr;
isDivide(): boolean;
asDivide(): Expr | Pow;
isRoot(): boolean;
isSquaredTrig(): boolean;
getDenominator(): Expr | Int;
findGCD(factor: Expr): Expr;
isPositive(): boolean;
asPositiveFactor(): Expr;
static sqrt(arg: Expr): Pow;
static nthroot: (radicand: Expr, degree: Expr) => Pow;
}
export declare class Log extends Expr {
base: Expr;
power: Expr;
constructor(base: Expr, power: Expr);
func: typeof Log;
args(): Expr[];
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
print(): string;
tex(): string;
collect(options?: Options): Expr;
expand(): Expr | Add | this;
isPositive(): boolean;
needsExplicitMul(): boolean;
isNatural(): boolean;
static natural(): Const;
static common(): Int;
static create(base: any, power: any): Log;
}
type TrigFunc = {
eval: (arg: number) => number;
codegen: string | ((argStr: string) => string);
tex: string;
expand?: () => Expr;
};
export declare class Trig extends Expr {
type: string;
arg: Expr;
exp?: Expr;
constructor(type: string, arg: Expr);
func: typeof Trig;
args(): (string | Expr)[];
functions: Record<string, TrigFunc>;
isEven(): boolean;
isInverse(): boolean;
isBasic(): boolean;
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
print(): string;
tex(): string;
texSplit(): [func: string, arg: string];
isPositive(): boolean;
completeParse(): Expr;
needsExplicitMul(): boolean;
expand(): any;
collect(options?: Options): Expr;
static create(pair: any, arg: any): Trig;
static sin(arg: Expr): Trig;
static cos(arg: Expr): Trig;
static sinh(arg: Expr): Trig;
static cosh(arg: Expr): Trig;
}
export declare class Abs extends Expr {
arg: Expr;
constructor(arg: Expr);
func: typeof Abs;
args(): Expr[];
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
print(): string;
tex(): string;
collect(options: any): Expr;
expand(): Expr;
isPositive(): boolean;
}
export declare class Eq extends Expr {
left: Expr;
type: string;
right: Expr;
constructor(left: Expr, type: string, right: Expr);
func: typeof Eq;
args(): (string | Expr)[];
needsExplicitMul(): boolean;
print(): string;
signs: {
"=": string;
"<": string;
">": string;
"<>": string;
"<=": string;
">=": string;
};
tex(): string;
normalize(): Eq;
asExpr(unfactored?: boolean): Expr;
divideThrough(expr: Expr): any;
isEquality(): boolean;
compare(other: Eq): any;
sameForm(other: any): boolean;
isSimplified(): boolean;
solveLinearEquationForVariable(variable: any): any;
}
declare abstract class Sym extends Expr {
needsExplicitMul(): boolean;
findGCD(factor: Expr): Expr;
}
export declare class Func extends Sym {
symbol: string;
arg: Expr;
constructor(symbol: string, arg: Expr);
func: typeof Func;
args(): (string | Expr)[];
print(): string;
tex(): string;
eval(vars?: Vars, options?: ParseOptions): any;
codegen(): string;
getUnits(): any[];
getVars(excludeFunc: boolean): any[];
getConsts(): any[];
}
export declare class Var extends Sym {
symbol: string;
subscript?: Expr;
constructor(symbol: string, subscript?: Expr);
func: typeof Var;
args(): (string | Expr | undefined)[];
exprArgs(): never[];
recurse(): this;
print(): string;
prettyPrint(): string;
tex(): string;
repr(): string;
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
getVars(): string[];
isPositive(): boolean;
}
export declare class Const extends Sym {
symbol: string;
constructor(symbol: string);
func: typeof Const;
args(): string[];
recurse(): this;
eval(vars?: Vars, options?: ParseOptions): number;
codegen(): string;
print(): string;
tex(): string;
isPositive(): boolean;
abs(): Expr | this;
getConsts(): string[];
static e: Const;
static pi: Const;
}
declare abstract class Num extends Expr {
n: number;
constructor();
repr(): string;
strip(): Num;
recurse(): this;
codegen(): string;
abstract add(other: Expr, options?: Options): Expr;
abstract mul(other: Expr, options?: Options): Expr;
abstract negate(): Expr;
isSubtract(): boolean;
abstract abs(): Num;
needsExplicitMul(): boolean;
abstract findGCD(factor: Expr): Expr;
isPositive(): boolean;
isNegative(): boolean;
asPositiveFactor(): Num;
abstract isSimple(): boolean;
getDecimalPlaces(): number;
abstract asRational(): Rational;
static negativeOne(hint: any): any;
static findGCD(a: any, b: any): any;
static min(...args: Expr[]): number | Expr;
static max(...args: Expr[]): number | Expr;
}
export declare class Rational extends Num {
n: number;
d: number;
constructor(numerator: number, denominator: number);
func: typeof Rational;
args(): number[];
eval(): number;
print(): string;
tex(): string;
add(num: Num, options?: {
preciseFloats: boolean;
}): any;
mul(num: Num, options?: {
preciseFloats: boolean;
}): any;
collect(): any;
negate(): Rational;
abs(): Rational;
findGCD(factor: Expr): Expr;
raiseToThe(exp: Expr): any;
getDenominator(): Int;
isSimple(): boolean;
asRational(): Rational;
}
export declare class Int extends Rational {
constructor(number: number);
func: typeof Int;
args(): number[];
print(): string;
tex(): string;
negate(): Int;
abs(): Int;
isSimple(): boolean;
findGCD(factor: Expr): Expr;
static create(n: number): any;
}
export declare class Float extends Num {
n: number;
constructor(number: number);
func: typeof Float;
args(): number[];
eval(): number;
print(): string;
tex(): string;
add(num: Num, options?: {
preciseFloats: boolean;
}): Num;
mul(num: Num, options?: {
preciseFloats: boolean;
}): Num;
collect(options?: Options): Float;
negate(): Float;
abs(): Float;
findGCD(factor: Expr): Expr;
raiseToThe(exp: Expr, options?: {
preciseFloats?: boolean;
}): Float;
asRational(): Rational;
getDenominator(): Int;
isSimple(): boolean;
static create(n: any): any;
static toDecimalPlaces(n: any, places: any): Float;
}
type ParseOptions = {
functions?: ReadonlyArray<string>;
decimal_separator?: string;
};
export declare const parse: (input: string, options?: ParseOptions) => {
parsed: boolean;
expr: any;
error?: undefined;
} | {
parsed: boolean;
error: string;
expr?: undefined;
};
export declare class Unit extends Sym {
symbol: string;
constructor(symbol: string);
func: typeof Unit;
args(): string[];
recurse(): this;
eval(vars?: Vars, options?: ParseOptions): number;
getUnits(): {
unit: string;
pow: number;
}[];
codegen(): string;
print(): string;
tex(): string;
collect(options?: Options): any;
}
export declare const unitParse: (input: string) => {
parsed: boolean;
unit: Expr;
expr: Mul;
coefficient: any;
type: any;
error?: undefined;
} | {
parsed: boolean;
unit: Expr;
type: any;
expr?: undefined;
coefficient?: undefined;
error?: undefined;
} | {
parsed: boolean;
error: string;
unit?: undefined;
expr?: undefined;
coefficient?: undefined;
type?: undefined;
};
export declare const Zero: Int;
export declare const One: Int;
export {};