Skip to content

Commit

Permalink
add some more types
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-taskbase committed Mar 2, 2021
1 parent 41d40b9 commit 1068867
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 114 deletions.
85 changes: 0 additions & 85 deletions .eslintrc.js

This file was deleted.

1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ node_js:
notifications:
email: false
script:
- npm run lint
- npm test
14 changes: 10 additions & 4 deletions lib/TreeSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,27 @@ export function search(simplificationFunction, node, preOrder) {
}

class TreeSearchImpl {
// Returns a function that performs a preorder search on the tree for the given
// simplification function

/**
* Returns a function that performs a preorder search on the tree for the given
* simplification function
* */
preOrder(simplificationFunction) {
return function (node) {
return search(simplificationFunction, node, true);
};
}

// Returns a function that performs a postorder search on the tree for the given
// simplification function
/**
* Returns a function that performs a postorder search on the tree for the given
* simplification function
* */
postOrder(simplificationFunction) {
return function (node) {
return search(simplificationFunction, node, false);
};
}

}

export const TreeSearch = new TreeSearchImpl();
2 changes: 1 addition & 1 deletion lib/factor/FactorString.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as math from "mathjs";
import { stepThrough } from "./stepThrough";
import {emptyResponse} from '../util/empty-response';
import { emptyResponse } from "../util/empty-response";

export function factorString(expressionString, debug = false) {
try {
Expand Down
4 changes: 1 addition & 3 deletions lib/factor/stepThrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@ import { isQuadratic } from "../checks/isQuadratic";
* */
export function stepThrough(node, debug = false) {
if (debug) {
// eslint-disable-next-line
console.log("\n\nFactoring: " + printAscii(node, false));
}

if (hasUnsupportedNodes(node)) {
return [];
}

let nodeStatus;
const steps = [];

node = flattenOperands(node);
node = removeUnnecessaryParens(node, true);
if (isQuadratic(node)) {
nodeStatus = factorQuadratic(node);
const nodeStatus = factorQuadratic(node);
if (nodeStatus.hasChanged()) {
steps.push(nodeStatus);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/node/NodeStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChangeTypes } from "../ChangeTypes";
import { NodeType } from "./NodeType";
import {StepNode} from './StepNode';

/**
* This represents the current (sub)expression we're simplifying.
Expand Down Expand Up @@ -46,7 +47,7 @@ export class NodeStatus {
* A wrapper around the Status constructor for the case where node hasn't
* been changed.
* */
static noChange(node) {
static noChange(node: StepNode) {
return new NodeStatus(ChangeTypes.NO_CHANGE, null, node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { evaluate } from "../../util/evaluate";
import { NodeType } from "../../node/NodeType";
import { NodeStatus } from "../../node/NodeStatus";
import { NodeCreator } from "../../node/Creator";
import {StepNode} from '../../node/StepNode';

// Adds a constant to a fraction by:
// - collapsing the fraction to decimal if the constant is not an integer
// e.g. 5.3 + 1/2 -> 5.3 + 0.2
// - turning the constant into a fraction with the same denominator if it is
// an integer, e.g. 5 + 1/2 -> 10/2 + 1/2
export function addConstantAndFraction(node) {
export function addConstantAndFraction(node: StepNode): NodeStatus {
if (!NodeType.isOperator(node) || node.op !== "+" || node.args.length !== 2) {
return NodeStatus.noChange(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { evaluate } from "../../util/evaluate";
import { NodeType } from "../../node/NodeType";
import { NodeStatus } from "../../node/NodeStatus";
import { NodeCreator } from "../../node/Creator";
import {StepNode} from '../../node/StepNode';

// Adds constant fractions -- can start from either step 1 or 2
// 1A. Find the LCD if denominators are different and multiplies to make
Expand All @@ -15,7 +16,7 @@ import { NodeCreator } from "../../node/Creator";
// 2A. Combines numerators, e.g. 4/6 + 4/6 -> e.g. 2/5 + 4/5 --> (2+4)/5
// 2B. Adds numerators together, e.g. (2+4)/5 -> 6/5
// Returns a Status object with substeps
export function addConstantFractions(node) {
export function addConstantFractions(node: StepNode): NodeStatus {
let newNode = node.cloneDeep();

if (!NodeType.isOperator(node) || node.op !== "+") {
Expand Down
3 changes: 2 additions & 1 deletion lib/simplifyExpression/fractionsSearch/cancelLikeTerms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { NodeStatus } from "../../node/NodeStatus";
import { NodeCreator } from "../../node/Creator";
import { printAscii } from "../../util/print";
import { PolynomialTerm } from "../../node/PolynomialTerm";
import {StepNode} from '../../node/StepNode';

// Used for cancelTerms to return a (possibly updated) numerator and denominator
class CancelOutStatus {
Expand All @@ -24,7 +25,7 @@ class CancelOutStatus {
// Cancels like terms in a fraction node
// e.g. (2x^2 * 5) / 2x^2 => 5 / 1
// Returns a Status object
export function cancelLikeTerms(node) {
export function cancelLikeTerms(node: StepNode): NodeStatus {
if (!NodeType.isOperator(node) || node.op !== "/") {
return NodeStatus.noChange(node);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/simplifyExpression/fractionsSearch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import { simplifyPolynomialFraction } from "./simplifyPolynomialFraction";
import { cancelLikeTerms } from "./cancelLikeTerms";
import { simplifyFractionSigns } from "./simplifyFractionSigns";
import { NodeStatus } from "../../node/NodeStatus";
import {factorString} from '../../factor/FactorString';
import {StepNode} from '../../node/StepNode';

const SIMPLIFICATION_FUNCTIONS: ReadonlyArray<(node: StepNode) => NodeStatus> = [

const SIMPLIFICATION_FUNCTIONS = [
// e.g. 2/3 + 5/6
addConstantFractions,

Expand Down
15 changes: 8 additions & 7 deletions lib/simplifyExpression/fractionsSearch/simplifyFractionSigns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import { Negative } from "../../Negative";
import { NodeType } from "../../node/NodeType";
import { NodeStatus } from "../../node/NodeStatus";
import { NodeCreator } from "../../node/Creator";
import {StepNode} from '../../node/StepNode';

// Simplifies negative signs if possible
// e.g. -1/-3 --> 1/3 4/-5 --> -4/5
// Note that -4/5 doesn't need to be simplified.
// Note that our goal is for the denominator to always be positive. If it
// isn't, we can simplify signs.
// Returns a Status object
export function simplifyFractionSigns(fraction) {
if (!NodeType.isOperator(fraction) || fraction.op !== "/") {
return NodeStatus.noChange(fraction);
export function simplifyFractionSigns(node: StepNode): NodeStatus {
if (!NodeType.isOperator(node) || node.op !== "/") {
return NodeStatus.noChange(node);
}
const oldFraction = fraction.cloneDeep();
let numerator = fraction.args[0];
let denominator = fraction.args[1];
const oldFraction = node.cloneDeep();
let numerator = node.args[0];
let denominator = node.args[1];
// The denominator should never be negative.
if (Negative.isNegative(denominator)) {
denominator = Negative.negate(denominator);
Expand All @@ -27,6 +28,6 @@ export function simplifyFractionSigns(fraction) {
const newFraction = NodeCreator.operator("/", [numerator, denominator]);
return NodeStatus.nodeChanged(changeType, oldFraction, newFraction);
} else {
return NodeStatus.noChange(fraction);
return NodeStatus.noChange(node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { PolynomialTerm } from "../../node/PolynomialTerm";
import { NodeStatus } from "../../node/NodeStatus";
import { NodeCreator } from "../../node/Creator";
import { arithmeticSearch } from "../arithmeticSearch/ArithmeticSearch";
import {StepNode} from '../../node/StepNode';

// Simplifies a polynomial term with a fraction as its coefficients.
// e.g. 2x/4 --> x/2 10x/5 --> 2x
// Also simplified negative signs
// e.g. -y/-3 --> y/3 4x/-5 --> -4x/5
// returns the new simplified node in a Status object
export function simplifyPolynomialFraction(node) {
export function simplifyPolynomialFraction(node: StepNode): NodeStatus {
if (!PolynomialTerm.isPolynomialTerm(node)) {
return NodeStatus.noChange(node);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/simplifyExpression/stepThrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { arithmeticSearch } from "./arithmeticSearch/ArithmeticSearch";
import { functionsSearch } from "./functionsSearch";
import { fractionsSearch } from "./fractionsSearch";
import { basicsSearch } from "./basicsSearch";
import {emptyResponse} from '../util/empty-response';
import { emptyResponse } from "../util/empty-response";

// Given a mathjs expression node, steps through simplifying the expression.
// Returns a list of details about each step.
Expand Down
3 changes: 2 additions & 1 deletion lib/util/removeUnnecessaryParens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PolynomialTerm } from "../node/PolynomialTerm";
import { LikeTermCollector } from "../simplifyExpression/collectAndCombineSearch/LikeTermCollector";
import { resolvesToConstant } from "../checks/resolvesToConstant";
import { canSimplifyPolynomialTerms } from "../checks/canSimplifyPolynomialTerms";
import {StepNode} from '../node/StepNode';

// Removes any parenthesis around nodes that can't be resolved further.
// Input must be a top level expression.
Expand All @@ -25,7 +26,7 @@ export function removeUnnecessaryParens(node, rootNode = false) {
// it doesn't change the value of the expression. Returns a node.
// NOTE: after this function is called, every parenthesis node in the
// tree should always have an operator node or unary minus as its child.
function removeUnnecessaryParensSearch(node) {
function removeUnnecessaryParensSearch(node: StepNode): StepNode {
if (NodeType.isOperator(node)) {
return removeUnnecessaryParensInOperatorNode(node);
} else if (NodeType.isFunction(node)) {
Expand Down
2 changes: 1 addition & 1 deletion test/simplifyExpression/simplify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("mixed cases", () => {
// ["(a^2+2*a+1)/(a+1)", "a+1"] not working
];
tests.forEach((t) => testSimplify(t[0], t[1]));
})
});

describe("simplify (arithmetic)", () => {
const tests = [
Expand Down
6 changes: 2 additions & 4 deletions test/solveEquation/solveEquation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ function testSolve(equationString, outputStr, debug = false) {
}

describe("mixed cases", () => {
const tests = [
["3(x+2)=12", "x = 2"]
];
const tests = [["3(x+2)=12", "x = 2"]];
tests.forEach((t) => testSolve(t[0], t[1]));
})
});

describe("solveEquation for =", function () {
const tests = [
Expand Down

0 comments on commit 1068867

Please sign in to comment.