Skip to content

Commit 8a6c97b

Browse files
committed
lint
1 parent 2c2ebbe commit 8a6c97b

7 files changed

Lines changed: 88 additions & 54 deletions

File tree

src/compute-engine/boxed-expression/match.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,12 @@ function matchOnce(
186186

187187
// Special case: Match Divide(1, x) against a Power pattern
188188
// This handles cases like x^-1 which is canonicalized as 1/x
189-
if (
190-
operator === 'Power' &&
191-
expr.operator === 'Divide' &&
192-
expr.op1.is(1)
193-
) {
189+
if (operator === 'Power' && expr.operator === 'Divide' && expr.op1.is(1)) {
194190
// Create a synthetic Power expression: Power(x, -1)
195-
const powerExpr = ce.function(
196-
'Power',
197-
[expr.op2, ce.number(-1)],
198-
{ canonical: false, structural: true }
199-
);
191+
const powerExpr = ce.function('Power', [expr.op2, ce.number(-1)], {
192+
canonical: false,
193+
structural: true,
194+
});
200195
const result = matchArguments(
201196
powerExpr,
202197
pattern.ops,

src/compute-engine/latex-syntax/parse-symbol.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,11 @@ export function parseSymbol(parser: Parser): MathJsonSymbol | null {
302302
// only simple tokens (letters, digits, and nested subscripts).
303303
// If it starts with '(' or contains operators, it's an expression.
304304
const firstToken = parser.peek;
305-
if (firstToken === '(' || firstToken === '\\lparen' || firstToken === '\\left') {
305+
if (
306+
firstToken === '(' ||
307+
firstToken === '\\lparen' ||
308+
firstToken === '\\left'
309+
) {
306310
// Starts with parenthesis - it's an expression, not a symbol
307311
parser.index = underscoreIndex;
308312
break;
@@ -315,7 +319,12 @@ export function parseSymbol(parser: Parser): MathJsonSymbol | null {
315319
// - contains operators (converted to 'plus', 'minus', etc. by parseSymbolBody)
316320
// - doesn't end with closing brace
317321
const hasOperators = sub !== null && /plus|minus|times|ast/.test(sub);
318-
if (sub === null || sub.includes(',') || hasOperators || parser.peek !== '<}>') {
322+
if (
323+
sub === null ||
324+
sub.includes(',') ||
325+
hasOperators ||
326+
parser.peek !== '<}>'
327+
) {
319328
parser.index = underscoreIndex;
320329
break;
321330
}

src/compute-engine/latex-syntax/parse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,6 @@ export class _Parser implements Parser {
16211621
if (this.atEnd) return lhs;
16221622
console.assert(lhs !== null);
16231623

1624-
16251624
const index = this.index;
16261625
this.skipSpace();
16271626

src/compute-engine/library/arithmetic.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ export const ARITHMETIC_LIBRARY: SymbolDefinitions[] = [
507507
// Digamma function ψ(x) = d/dx ln(Γ(x)) = Γ'(x)/Γ(x)
508508
// Also known as the psi function
509509
Digamma: {
510-
description: 'Digamma function, the logarithmic derivative of the gamma function',
510+
description:
511+
'Digamma function, the logarithmic derivative of the gamma function',
511512
wikidata: 'Q1142755',
512513
complexity: 8200,
513514
broadcastable: true,
@@ -530,7 +531,8 @@ export const ARITHMETIC_LIBRARY: SymbolDefinitions[] = [
530531
// The n-th derivative of the digamma function
531532
// PolyGamma(0, x) = Digamma(x), PolyGamma(1, x) = Trigamma(x)
532533
PolyGamma: {
533-
description: 'Polygamma function, the n-th derivative of the digamma function',
534+
description:
535+
'Polygamma function, the n-th derivative of the digamma function',
534536
wikidata: 'Q1817679',
535537
complexity: 8500,
536538
broadcastable: true,

src/compute-engine/library/calculus.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ volumes
162162
if (f?.operator === 'D') return f;
163163
// Avoid evaluating symbolic derivative applications like Digamma'(x)
164164
// which would incorrectly evaluate to 0
165-
if (
166-
f?.operator === 'Apply' &&
167-
f.op1?.operator === 'Derivative'
168-
)
165+
if (f?.operator === 'Apply' && f.op1?.operator === 'Derivative')
169166
return f;
170167
// If the result contains symbolic transcendentals (like ln(2)),
171168
// return it without full evaluation to preserve the symbolic form

src/compute-engine/symbolic/simplify-sum.ts

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { BoxedExpression, RuleStep } from '../global-types';
77
export function simplifySum(x: BoxedExpression): RuleStep | undefined {
88
if (x.operator !== 'Sum') return undefined;
99

10-
let body = x.op1;
10+
const body = x.op1;
1111
const limits = x.op2;
1212
if (!body || !limits || limits.operator !== 'Limits') return undefined;
1313

@@ -78,7 +78,10 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
7878
// = b(b+1)/2 - (a-1)a/2 = (b(b+1) - a(a-1)) / 2
7979
const a = lower;
8080
const b = upper;
81-
const result = b.mul(b.add(ce.One)).sub(a.mul(a.sub(ce.One))).div(2);
81+
const result = b
82+
.mul(b.add(ce.One))
83+
.sub(a.mul(a.sub(ce.One)))
84+
.div(2);
8285
return { value: result.simplify(), because: 'triangular number' };
8386
}
8487

@@ -149,7 +152,10 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
149152
const result = ce.function('Multiply', [
150153
ce.function('Power', [ce.number(-1), b]),
151154
ce.function('Floor', [
152-
ce.function('Divide', [ce.function('Add', [b, ce.One]), ce.number(2)]),
155+
ce.function('Divide', [
156+
ce.function('Add', [b, ce.One]),
157+
ce.number(2),
158+
]),
153159
]),
154160
]);
155161
return { value: result, because: 'alternating linear series' };
@@ -177,8 +183,7 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
177183
// c * n form - extract coefficient
178184
const coef = term.ops!.filter((op) => op.symbol !== index);
179185
if (coef.length === term.ops!.length - 1) {
180-
const c =
181-
coef.length === 1 ? coef[0] : ce.function('Multiply', coef);
186+
const c = coef.length === 1 ? coef[0] : ce.function('Multiply', coef);
182187
coefficient = coefficient ? coefficient.add(c) : c;
183188
}
184189
} else {
@@ -274,10 +279,7 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
274279
let binomialN: BoxedExpression | null = null;
275280

276281
for (const op of body.ops) {
277-
if (
278-
op.operator === 'Binomial' &&
279-
op.op2?.symbol === index
280-
) {
282+
if (op.operator === 'Binomial' && op.op2?.symbol === index) {
281283
hasBinomial = true;
282284
binomialN = op.op1 ?? null;
283285
} else if (
@@ -303,16 +305,19 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
303305
for (const op of body.ops) {
304306
if (op.symbol === index) {
305307
hasIndex = true;
306-
} else if (
307-
op.operator === 'Binomial' &&
308-
op.op2?.symbol === index
309-
) {
308+
} else if (op.operator === 'Binomial' && op.op2?.symbol === index) {
310309
hasBinomial = true;
311310
binomialN = op.op1 ?? null;
312311
}
313312
}
314313

315-
if (hasIndex && hasBinomial && binomialN && upper.isSame(binomialN) && body.ops.length === 2) {
314+
if (
315+
hasIndex &&
316+
hasBinomial &&
317+
binomialN &&
318+
upper.isSame(binomialN) &&
319+
body.ops.length === 2
320+
) {
316321
// n * 2^(n-1)
317322
const n = binomialN;
318323
const result = ce.function('Multiply', [
@@ -328,18 +333,25 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
328333
hasBinomial = false;
329334

330335
for (const op of body.ops) {
331-
if (op.operator === 'Power' && op.op1?.symbol === index && op.op2?.is(2)) {
332-
hasIndexSquared = true;
333-
} else if (
334-
op.operator === 'Binomial' &&
335-
op.op2?.symbol === index
336+
if (
337+
op.operator === 'Power' &&
338+
op.op1?.symbol === index &&
339+
op.op2?.is(2)
336340
) {
341+
hasIndexSquared = true;
342+
} else if (op.operator === 'Binomial' && op.op2?.symbol === index) {
337343
hasBinomial = true;
338344
binomialN = op.op1 ?? null;
339345
}
340346
}
341347

342-
if (hasIndexSquared && hasBinomial && binomialN && upper.isSame(binomialN) && body.ops.length === 2) {
348+
if (
349+
hasIndexSquared &&
350+
hasBinomial &&
351+
binomialN &&
352+
upper.isSame(binomialN) &&
353+
body.ops.length === 2
354+
) {
343355
// n(n+1) * 2^(n-2)
344356
const n = binomialN;
345357
const result = ce.function('Multiply', [
@@ -356,18 +368,25 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
356368
hasBinomial = false;
357369

358370
for (const op of body.ops) {
359-
if (op.operator === 'Power' && op.op1?.symbol === index && op.op2?.is(3)) {
360-
hasIndexCubed = true;
361-
} else if (
362-
op.operator === 'Binomial' &&
363-
op.op2?.symbol === index
371+
if (
372+
op.operator === 'Power' &&
373+
op.op1?.symbol === index &&
374+
op.op2?.is(3)
364375
) {
376+
hasIndexCubed = true;
377+
} else if (op.operator === 'Binomial' && op.op2?.symbol === index) {
365378
hasBinomial = true;
366379
binomialN = op.op1 ?? null;
367380
}
368381
}
369382

370-
if (hasIndexCubed && hasBinomial && binomialN && upper.isSame(binomialN) && body.ops.length === 2) {
383+
if (
384+
hasIndexCubed &&
385+
hasBinomial &&
386+
binomialN &&
387+
upper.isSame(binomialN) &&
388+
body.ops.length === 2
389+
) {
371390
// n²(n+3) * 2^(n-3)
372391
const n = binomialN;
373392
const result = ce.function('Multiply', [
@@ -385,7 +404,11 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
385404
hasBinomial = false;
386405

387406
for (const op of body.ops) {
388-
if (op.operator === 'Power' && op.op1?.is(-1) && op.op2?.symbol === index) {
407+
if (
408+
op.operator === 'Power' &&
409+
op.op1?.is(-1) &&
410+
op.op2?.symbol === index
411+
) {
389412
hasAltTerm = true;
390413
} else if (op.symbol === index) {
391414
hasIndexTerm = true;
@@ -395,7 +418,14 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
395418
}
396419
}
397420

398-
if (hasAltTerm && hasIndexTerm && hasBinomial && binomialN && upper.isSame(binomialN) && body.ops.length === 3) {
421+
if (
422+
hasAltTerm &&
423+
hasIndexTerm &&
424+
hasBinomial &&
425+
binomialN &&
426+
upper.isSame(binomialN) &&
427+
body.ops.length === 3
428+
) {
399429
// For n >= 2, sum = 0
400430
return { value: ce.Zero, because: 'alternating weighted binomial sum' };
401431
}
@@ -422,11 +452,7 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
422452
}
423453

424454
// Sum of k*(k+1): Sum(k*(k+1), [k, 1, n]) → n(n+1)(n+2)/3
425-
if (
426-
body.operator === 'Multiply' &&
427-
body.ops?.length === 2 &&
428-
lower.is(1)
429-
) {
455+
if (body.operator === 'Multiply' && body.ops?.length === 2 && lower.is(1)) {
430456
const [op1, op2] = body.ops;
431457
// Check for k * (k+1) pattern
432458
const isKTimesKPlus1 =
@@ -506,7 +532,10 @@ export function simplifySum(x: BoxedExpression): RuleStep | undefined {
506532
// (n - 1) / n
507533
const n = upper;
508534
const result = n.sub(ce.One).div(n);
509-
return { value: result, because: 'partial fractions (telescoping k*(k-1))' };
535+
return {
536+
value: result,
537+
because: 'partial fractions (telescoping k*(k-1))',
538+
};
510539
}
511540
}
512541
}

src/compute-engine/tensor/tensors.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ export abstract class AbstractTensor<
336336
// Trace is the sum of the diagonal entries of a square matrix.
337337
// `\operatorname{tr}(A) = \sum_{i=1}^n a_{ii}`
338338
// For rank > 2, returns a tensor of traces over the last two axes (batch trace)
339-
trace(axis1?: number, axis2?: number): undefined | DataTypeMap[DT] | AbstractTensor<DT> {
339+
trace(
340+
axis1?: number,
341+
axis2?: number
342+
): undefined | DataTypeMap[DT] | AbstractTensor<DT> {
340343
const rank = this.rank;
341344

342345
// For rank < 2, trace is not defined

0 commit comments

Comments
 (0)