Made formulas comparable and more efficient

This commit is contained in:
thepaperpilot 2023-01-15 00:06:54 -06:00
parent 7593fea512
commit 773401069a
3 changed files with 807 additions and 865 deletions

File diff suppressed because it is too large Load diff

View file

@ -802,6 +802,10 @@ export default class Decimal {
return D(value).pow10(); return D(value).pow10();
} }
public static pow_base(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).pow_base(other);
}
public static root(value: DecimalSource, other: DecimalSource): Decimal { public static root(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).root(other); return D(value).root(other);
} }

View file

@ -1,20 +1,24 @@
import Formula, { FormulaSource, InvertibleFormula } from "game/formulas"; import Formula, { GenericFormula, InvertibleFormula, unrefFormulaSource } from "game/formulas";
import Decimal, { DecimalSource, format } from "util/bignum"; import Decimal, { DecimalSource, format } from "util/bignum";
import { beforeAll, describe, expect, test, vi } from "vitest"; import { beforeAll, describe, expect, test } from "vitest";
import { ref } from "vue"; import { ref } from "vue";
type FormulaFunctions = keyof Formula & keyof typeof Formula & keyof typeof Decimal; type FormulaFunctions = keyof GenericFormula & keyof typeof Formula & keyof typeof Decimal;
interface FixedLengthArray<T, L extends number> extends ArrayLike<T> {
length: L;
}
expect.extend({ expect.extend({
compare_tolerance(received, expected) { compare_tolerance(received, expected) {
const { isNot } = this; const { isNot } = this;
let pass = false;
if (!Decimal.isFinite(expected)) {
pass = !Decimal.isFinite(received);
} else if (Decimal.isNaN(expected)) {
pass = Decimal.isNaN(received);
} else {
pass = Decimal.eq_tolerance(received, expected);
}
return { return {
// do not alter your "pass" based on isNot. Vitest does it for you // do not alter your "pass" based on isNot. Vitest does it for you
pass: Decimal.eq_tolerance(received, expected), pass,
message: () => message: () =>
`Expected ${received} to${ `Expected ${received} to${
(isNot as boolean) ? " not" : "" (isNot as boolean) ? " not" : ""
@ -45,113 +49,93 @@ function testConstant(
expectedValue: DecimalSource = 10 expectedValue: DecimalSource = 10
) { ) {
describe(desc, () => { describe(desc, () => {
let formula: Formula; let formula: GenericFormula;
beforeAll(() => { beforeAll(() => {
formula = formulaFunc(); formula = formulaFunc();
}); });
test("evaluates correctly", async () => test("evaluates correctly", () =>
expect(formula.evaluate()).compare_tolerance(expectedValue)); expect(formula.evaluate()).compare_tolerance(expectedValue));
test("invert is pass-through", async () => test("invert is pass-through", () => expect(formula.invert(25)).compare_tolerance(25));
expect(formula.invert(25)).compare_tolerance(25)); test("is not marked as having a variable", () => expect(formula.hasVariable()).toBe(false));
test("is invertible", async () => expect(formula.invertible).toBe(true));
test("is not marked as having a variable", async () =>
expect(formula.hasVariable).toBe(false));
}); });
} }
function testFormula<T extends FormulaFunctions>(
functionName: T,
args: Readonly<Parameters<typeof Formula[T]>>,
invertible = true
) {
let formula: GenericFormula;
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
formula = Formula[functionName](...args);
});
test("Formula is not marked as having a variable", () =>
expect(formula.hasVariable()).toBe(false));
test(`Formula is${invertible ? "" : " not"} invertible`, () =>
expect(formula.isInvertible()).toBe(invertible));
if (invertible) {
test(`Formula throws if inverting without any variables`, () =>
expect(() => formula.invert(10)).toThrow());
}
}
// Utility function that will test all the different // Utility function that will test all the different
// It's a lot of tests, but I'd rather be exhaustive // It's a lot of tests, but I'd rather be exhaustive
function testFormula<T extends FormulaFunctions>( function testFormulaCall<T extends FormulaFunctions>(
functionName: T, functionName: T,
args: Readonly<FixedLengthArray<number, Parameters<typeof Formula[T]>["length"]>>, args: Readonly<Parameters<typeof Formula[T]>>
invertible = true
) { ) {
let value: Decimal;
beforeAll(() => {
value = testValueFormulas[args[0]].evaluate();
});
let testName = functionName + "("; let testName = functionName + "(";
for (let i = 0; i < args.length; i++) { for (let i = 0; i < args.length; i++) {
if (i !== 0) { if (i !== 0) {
testName += ", "; testName += ", ";
} }
testName += testValues[args[i]]; testName += args[i];
}
testName += ")";
describe(testName, () => {
let expectedEvaluation: Decimal | undefined;
const formulaArgs: Formula[] = [];
let staticFormula: Formula;
let instanceFormula: Formula;
beforeAll(() => {
for (let i = 0; i < args.length; i++) {
formulaArgs.push(testValueFormulas[args[i]]);
} }
testName += ") evaluates correctly";
test(testName, () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
staticFormula = Formula[functionName](...formulaArgs); const formula = Formula[functionName](...args);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
instanceFormula = formulaArgs[0][functionName](...formulaArgs.slice(1));
try { try {
const expectedEvaluation = Decimal[functionName](
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
expectedEvaluation = Decimal[functionName](...args); ...args.map(i => unrefFormulaSource(i))
);
if (expectedEvaluation != null) {
expect(formula.evaluate()).compare_tolerance(expectedEvaluation);
}
} catch { } catch {
// If this is an invalid Decimal operation, then ignore this test case // If this is an invalid Decimal operation, then ignore this test case
return;
}
});
test("Static formula is not marked as having a variable", async () =>
expect(staticFormula.hasVariable).toBe(false));
test("Static function evaluates correctly", async () =>
expectedEvaluation != null &&
expect(staticFormula.evaluate()).compare_tolerance(expectedEvaluation));
test("Static function invertible", async () =>
expect(staticFormula.invertible).toBe(invertible));
if (invertible) {
test("Static function inverts correctly", async () =>
expectedEvaluation != null &&
!Decimal.isNaN(expectedEvaluation) &&
expect(staticFormula.invert(expectedEvaluation)).compare_tolerance(value));
}
// Do those tests again but for non-static methods
test("Instance formula is not marked as having a variable", async () =>
expect(instanceFormula.hasVariable).toBe(false));
test("Instance function evaluates correctly", async () =>
expectedEvaluation != null &&
expect(instanceFormula.evaluate()).compare_tolerance(expectedEvaluation));
test("Instance function invertible", async () =>
expect(instanceFormula.invertible).toBe(invertible));
if (invertible) {
test("Instance function inverts correctly", async () =>
expectedEvaluation != null &&
!Decimal.isNaN(expectedEvaluation) &&
expect(instanceFormula.invert(expectedEvaluation)).compare_tolerance(value));
} }
}); });
} }
function testAliases<T extends FormulaFunctions[]>( function testAliases<T extends FormulaFunctions>(
formula: Formula, aliases: T[],
aliases: T, args: Parameters<typeof Formula[T]>
args: FormulaSource[]
) { ) {
const spy = vi.spyOn(formula, aliases[0]); describe(aliases[0], () => {
expect(spy).not.toHaveBeenCalled(); let formula: GenericFormula;
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
aliases.slice(1).forEach(name => formula[name](...args)); formula = Formula[aliases[0]](...args);
expect(spy).toHaveBeenCalledTimes(aliases.length - 1); });
aliases.slice(1).forEach(alias => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
test(alias, () => expect(Formula[alias](...args).equals(formula)).toBe(true));
});
});
} }
const testValues = [-2.5, -1, -0.1, 0, 0.1, 1, 2.5] as const; const testValues = ["-1e400", 0, 0.25] as const;
let testValueFormulas: InvertibleFormula[] = [];
const invertibleZeroParamFunctionNames = [ const invertibleZeroParamFunctionNames = [
"neg", "neg",
@ -220,11 +204,64 @@ const invertibleTwoParamFunctionNames = ["tetrate", "layeradd", "iteratedexp"] a
const nonInvertibleTwoParamFunctionNames = ["clamp", "iteratedlog", "pentate"] as const; const nonInvertibleTwoParamFunctionNames = ["clamp", "iteratedlog", "pentate"] as const;
describe.concurrent("Creating Formulas", () => { describe("Formula Equality Checking", () => {
beforeAll(() => { describe("Equality Checks", () => {
testValueFormulas = testValues.map(v => Formula.constant(v)); test("Equals", () => Formula.add(1, 1).equals(Formula.add(1, 1)));
test("Not Equals due to inputs", () => Formula.add(1, 1).equals(Formula.add(1, 0)));
test("Not Equals due to functions", () => Formula.add(1, 1).equals(Formula.sub(1, 1)));
test("Not Equals due to hasVariable", () =>
Formula.constant(1).equals(Formula.variable(1)));
}); });
describe("Formula aliases", () => {
testAliases(["neg", "negate", "negated"], [1]);
testAliases(["recip", "reciprocal", "reciprocate"], [1]);
testAliases(["sign", "sgn"], [1]);
testAliases(["add", "plus"], [1, 1]);
testAliases(["sub", "subtract", "minus"], [1, 1]);
testAliases(["mul", "multiply", "times"], [1, 1]);
testAliases(["div", "divide"], [1, 1]);
testAliases(["log", "logarithm"], [1, 1]);
});
describe("Instance vs Static methods", () => {
let formula: GenericFormula;
beforeAll(() => {
formula = Formula.constant(10);
});
[...invertibleZeroParamFunctionNames, ...nonInvertibleZeroParamFunctionNames].forEach(
name => {
test(name, () => {
const instanceFormula = formula[name]();
const staticFormula = Formula[name](formula);
expect(instanceFormula.equals(staticFormula)).toBe(true);
});
}
);
[...invertibleOneParamFunctionNames, ...nonInvertibleOneParamFunctionNames].forEach(
name => {
test(name, () => {
const instanceFormula = formula[name](10);
const staticFormula = Formula[name](formula, 10);
expect(instanceFormula.equals(staticFormula)).toBe(true);
});
}
);
[...invertibleTwoParamFunctionNames, ...nonInvertibleTwoParamFunctionNames].forEach(
name => {
test(name, () => {
const instanceFormula = formula[name](1, 1);
const staticFormula = Formula[name](formula, 1, 1);
expect(instanceFormula.equals(staticFormula)).toBe(true);
});
}
);
});
});
describe("Creating Formulas", () => {
describe("Constants", () => { describe("Constants", () => {
testConstant("number", () => Formula.constant(10)); testConstant("number", () => Formula.constant(10));
testConstant("string", () => Formula.constant("10")); testConstant("string", () => Formula.constant("10"));
@ -233,101 +270,91 @@ describe.concurrent("Creating Formulas", () => {
testConstant("ref", () => Formula.constant(ref(10))); testConstant("ref", () => Formula.constant(ref(10)));
}); });
// Test that these are just pass-throughts so we don't need to test each one everywhere else
describe("Function aliases", () => {
let formula: Formula;
beforeAll(() => {
formula = Formula.constant(10);
});
test("neg", async () => testAliases(formula, ["neg", "negate", "negated"], [0]));
test("recip", async () =>
testAliases(formula, ["recip", "reciprocal", "reciprocate"], [0]));
test("sign", async () => testAliases(formula, ["sign", "sgn"], [0]));
test("add", async () => testAliases(formula, ["add", "plus"], [0]));
test("sub", async () => testAliases(formula, ["sub", "subtract", "minus"], [0]));
test("mul", async () => testAliases(formula, ["mul", "multiply", "times"], [0]));
test("div", async () => testAliases(formula, ["div", "divide"], [1]));
test("log", async () => testAliases(formula, ["log", "logarithm"], [0]));
});
describe("Invertible 0-param", () => { describe("Invertible 0-param", () => {
invertibleZeroParamFunctionNames.forEach(names => { invertibleZeroParamFunctionNames.forEach(names =>
for (let i = 0; i < testValues.length; i++) { describe(names, () => {
testFormula(names, [i] as const); testFormula(names, [0] as const);
} testValues.forEach(i => testFormulaCall(names, [i] as const));
}); })
);
}); });
describe("Non-Invertible 0-param", () => { describe("Non-Invertible 0-param", () => {
nonInvertibleZeroParamFunctionNames.forEach(names => { nonInvertibleZeroParamFunctionNames.forEach(names =>
for (let i = 0; i < testValues.length; i++) { describe(names, () => {
testFormula(names, [i] as const, false); testFormula(names, [0] as const, false);
} testValues.forEach(i => testFormulaCall(names, [i] as const));
}); })
);
}); });
describe("Invertible 1-param", () => { describe("Invertible 1-param", () => {
invertibleOneParamFunctionNames.forEach(names => { invertibleOneParamFunctionNames.forEach(names =>
for (let i = 0; i < testValues.length; i++) { describe(names, () => {
for (let j = 0; j < testValues.length; j++) { testFormula(names, [0, 0] as const);
testFormula(names, [i, j] as const); testValues.forEach(i =>
} testValues.forEach(j => testFormulaCall(names, [i, j] as const))
} );
}); })
);
}); });
describe("Non-Invertible 1-param", () => { describe("Non-Invertible 1-param", () => {
nonInvertibleOneParamFunctionNames.forEach(names => { nonInvertibleOneParamFunctionNames.forEach(names =>
for (let i = 0; i < testValues.length; i++) { describe(names, () => {
for (let j = 0; j < testValues.length; j++) { testFormula(names, [0, 0] as const, false);
testFormula(names, [i, j] as const, false); testValues.forEach(i =>
} testValues.forEach(j => testFormulaCall(names, [i, j] as const))
} );
}); })
);
}); });
describe("Invertible 2-param", () => { describe("Invertible 2-param", () => {
invertibleTwoParamFunctionNames.forEach(names => { invertibleTwoParamFunctionNames.forEach(names =>
for (let i = 0; i < testValues.length; i++) { describe(names, () => {
for (let j = 0; j < testValues.length; j++) { testFormula(names, [0, 0, 0] as const);
for (let k = 0; k < testValues.length; k++) { testValues.forEach(i =>
testFormula(names, [i, j, k] as const); testValues.forEach(j =>
} testValues.forEach(k => testFormulaCall(names, [i, j, k] as const))
} )
} );
}); })
);
}); });
describe("Non-Invertible 2-param", () => { describe("Non-Invertible 2-param", () => {
nonInvertibleTwoParamFunctionNames.forEach(names => { nonInvertibleTwoParamFunctionNames.forEach(names =>
for (let i = 0; i < testValues.length; i++) { describe(names, () => {
for (let j = 0; j < testValues.length; j++) { testFormula(names, [0, 0, 0] as const, false);
for (let k = 0; k < testValues.length; k++) { testValues.forEach(i =>
testFormula(names, [i, j, k] as const, false); testValues.forEach(j =>
} testValues.forEach(k => testFormulaCall(names, [i, j, k] as const))
} )
} );
}); })
);
}); });
}); });
describe("Variables", () => { describe("Variables", () => {
let variable: Formula; let variable: GenericFormula;
let constant: Formula; let constant: GenericFormula;
beforeAll(() => { beforeAll(() => {
variable = Formula.variable(10); variable = Formula.variable(10);
constant = Formula.constant(10); constant = Formula.constant(10);
}); });
test("Created variable is marked as a variable", () => expect(variable.hasVariable).toBe(true)); test("Created variable is marked as a variable", () =>
expect(variable.hasVariable()).toBe(true));
test("Evaluate() returns variable's value", () => test("Evaluate() returns variable's value", () =>
expect(variable.evaluate()).compare_tolerance(10)); expect(variable.evaluate()).compare_tolerance(10));
test("Invert() is pass-through", () => expect(variable.invert(100)).compare_tolerance(100)); test("Invert() is pass-through", () => expect(variable.invert(100)).compare_tolerance(100));
test("Nested variable is marked as having a variable", () => test("Nested variable is marked as having a variable", () =>
expect(variable.add(10).div(3).pow(2).hasVariable).toBe(true)); expect(variable.add(10).div(3).pow(2).hasVariable()).toBe(true));
test("Nested non-variable is marked as not having a variable", () => test("Nested non-variable is marked as not having a variable", () =>
expect(constant.add(10).div(3).pow(2).hasVariable).toBe(false)); expect(constant.add(10).div(3).pow(2).hasVariable()).toBe(false));
describe("Invertible Formulas correctly calculate when they contain a variable", () => { describe("Invertible Formulas correctly calculate when they contain a variable", () => {
function checkFormula(formula: Formula, expectedBool = true) { function checkFormula(formula: GenericFormula, expectedBool = true) {
expect(formula.invertible).toBe(expectedBool); expect(formula.isInvertible()).toBe(expectedBool);
expect(formula.hasVariable).toBe(expectedBool); expect(formula.hasVariable()).toBe(expectedBool);
} }
invertibleZeroParamFunctionNames.forEach(name => { invertibleZeroParamFunctionNames.forEach(name => {
describe(name, () => { describe(name, () => {
@ -366,9 +393,9 @@ describe("Variables", () => {
}); });
describe("Non-Invertible Formulas never marked as having a variable", () => { describe("Non-Invertible Formulas never marked as having a variable", () => {
function checkFormula(formula: Formula) { function checkFormula(formula: GenericFormula) {
expect(formula.invertible).toBe(false); expect(formula.isInvertible()).toBe(false);
expect(formula.hasVariable).toBe(false); expect(formula.hasVariable()).toBe(false);
} }
nonInvertibleZeroParamFunctionNames.forEach(name => { nonInvertibleZeroParamFunctionNames.forEach(name => {
describe(name, () => { describe(name, () => {
@ -407,8 +434,8 @@ describe("Variables", () => {
}); });
describe("Inverting calculates the value of the variable", () => { describe("Inverting calculates the value of the variable", () => {
let variable: Formula; let variable: GenericFormula;
let constant: Formula; let constant: GenericFormula;
beforeAll(() => { beforeAll(() => {
variable = Formula.variable(2); variable = Formula.variable(2);
constant = Formula.constant(3); constant = Formula.constant(3);