Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3bc998d

Browse files
committed
refactor: re-introduce '(a^b)^c -> a^(b*c)' in canonicalPower
1 parent339c751 commit3bc998d

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

‎src/compute-engine/boxed-expression/arithmetic-power.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,19 @@ export function canonicalPower(
6060
constunchanged=()=>
6161
ce._fn('Power',[a,b],{canonical:fullyCanonical});
6262

63-
// Exclude cases - which may otherwise be valid - of the exponent either: being a function (e.g.
64-
// '0 + 0'), a symbol, or non-numeric expr. type (e.g. string).
63+
if(a.operator==='Power'){
64+
const[base,aPow]=a.ops!;
65+
returnce._fn('Power',[
66+
base,
67+
ce.box(['Multiply',aPow,b],{
68+
canonical:fullyCanonical||'Power',
69+
}),
70+
]);
71+
}
72+
73+
// Onwards, the focus on operations is where is a *numeric* exponent.
74+
// Therefore, exclude cases - which may otherwise be valid - of the exponent either: being a function (e.g.
75+
// '0 + 0'), a symbol, or of a non-numeric type.
6576
//
6677
//@consider:possible exceptions where function-expressions are reasonable :Rational,Half,
6778
// Negate... (However, provided that canonicalNumber provided prior, should not be missing anything
@@ -280,7 +291,9 @@ export function pow(
280291
if(typeofexp!=='number')exp=exp.canonical;
281292

282293
// 'canonicalPower' deals with a set of basic operations.
283-
// If the result is not 'Power', can assume an op. has taken place
294+
// If the result is not 'Power', can assume an op. has occurred
295+
// In some cases, an op. may apply, but a 'Power' expr. is still the result ('(a^b)^c -> a^(b*c)'
296+
// for instance). For these cases, proceed.
284297
constcanonicalResult=canonicalPower(x,ce.box(exp));
285298
if(canonicalResult.operator!=='Power')returncanonicalResult;
286299

‎test/compute-engine/canonical-form.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,44 @@ describe('CANONICAL FORMS', () => {
665665
canonical = ["Root", ["Multiply", ["Rational", 1, 3], "a"], 3]
666666
`);
667667
});
668+
669+
test(`(a^b)^c -> a^(b*c)`,()=>{
670+
// expect(checkPower('')).toMatchInlineSnapshot();
671+
expect(checkPower('{a^3}^4')).toMatchInlineSnapshot(`
672+
box = ["Power", ["Power", "a", 3], 4]
673+
canonForms = ["Power", "a", ["Multiply", 3, 4]]
674+
canonical = ["Power", "a", ["Multiply", 3, 4]]
675+
`);
676+
//note: 'Multiply' args. are ordered in the output JSON: but the result 'Power'
677+
//BoxedExpression still has (ordered) operands [b, c].
678+
expect(checkPower('{a^{{b^2}^e}}^{0.5*\\pi}')).toMatchInlineSnapshot(`
679+
box = [
680+
"Power",
681+
["Power", "a", ["Power", ["Square", "b"], "e"]],
682+
["Multiply", 0.5, "Pi"]
683+
]
684+
canonForms = [
685+
"Power",
686+
"a",
687+
[
688+
"Multiply",
689+
0.5,
690+
"Pi",
691+
["Power", "b", ["Multiply", 2, "ExponentialE"]]
692+
]
693+
]
694+
canonical = [
695+
"Power",
696+
"a",
697+
[
698+
"Multiply",
699+
0.5,
700+
"Pi",
701+
["Power", "b", ["Multiply", 2, "ExponentialE"]]
702+
]
703+
]
704+
`);
705+
});
668706
});
669707

670708
describe('Number',()=>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp