Skip to content

Commit

Permalink
hoist isAdd helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinb-khan committed Oct 17, 2024
1 parent 8170b68 commit d9d350e
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions packages/kas/src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function isExpr(arg: string | Expr): arg is Expr {
return arg instanceof Expr;
}

const isAdd = function (term: Expr): term is Add {
return term instanceof Add;
};

function isRational(arg: Expr): arg is Rational {
return arg instanceof Rational;
}
Expand Down Expand Up @@ -997,10 +1001,6 @@ export class Mul extends Seq {

// expand numerator and denominator separately
expand(): Expr {
const isAdd = function (term: Expr): term is Add {
return term instanceof Add;
};

const isInverse = function (term: Expr): term is Pow {
return term instanceof Pow && term.exp.isNegative();
};
Expand Down Expand Up @@ -2742,20 +2742,19 @@ export class Eq extends Expr {
// TODO(alex): Make it an option to only divide by variables/expressions
// guaranteed to be nonzero
divideThrough(expr: Expr) {
var isInequality = !this.isEquality();
const isInequality = !this.isEquality();

var simplified = expr.simplify({once: true});
var factored = simplified.factor({keepNegative: isInequality});
const simplified = expr.simplify({once: true});
const factored = simplified.factor({keepNegative: isInequality});

if (!(factored instanceof Mul)) {
return expr;
}

var terms = factored.terms;
const terms = factored.terms;

var isAdd = (term: Expr): term is Add => term instanceof Add;
var hasVar = (term: Expr) => !!term.getVars().length;
var isOne = (term: Expr) => term.equals(NumOne);
const hasVar = (term: Expr) => !!term.getVars().length;
const isOne = (term: Expr) => term.equals(NumOne);

const [adds, others] = partition(terms, isAdd);

Expand Down

0 comments on commit d9d350e

Please sign in to comment.