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

Commitb962bcb

Browse files
committed
2 parents9d8c6e9 +bb1d1b2 commitb962bcb

File tree

4 files changed

+370
-208
lines changed

4 files changed

+370
-208
lines changed

‎src/compute-engine/global-types.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,24 +1915,6 @@ export type Sign =
19151915
/** The expression is not equal to 0 (possibly with an imaginary part) and isPositive, isNegative, isUnsigned are all false or undefined */
19161916
|'not-zero'
19171917

1918-
/** The expression has no imaginary part and a non-zero real part and isPositive and isNegative are false or undefined*/
1919-
|'real-not-zero'
1920-
1921-
/** The expression has no imaginary part and isNotZero,isPositive,isNegative,isNonNegative,isNonPositive,isZero are either false or undefined*/
1922-
|'real'
1923-
1924-
/** The expression is NaN */
1925-
|'nan'
1926-
1927-
/** The expression is +∞ */
1928-
|'positive-infinity'
1929-
1930-
/** The expression is -∞ */
1931-
|'negative-infinity'
1932-
1933-
/** The expression is ~∞ */
1934-
|'complex-infinity'
1935-
19361918
/** The expression has an imaginary part or is NaN */
19371919
|'unsigned';
19381920

‎src/compute-engine/library/arithmetic.ts‎

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
167167
type:([x])=>x.type,
168168
sgn:([x])=>{
169169
if(x.is(0))return'zero';
170-
returnx.isNumberLiteral ?'positive' :'non-negative';
170+
if(x.isNumberLiteral)return'positive';
171+
return'non-negative';//|x^2+1| fails
171172
},
172173
evaluate:([x])=>evaluateAbs(x),
173174
},
@@ -187,14 +188,12 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
187188
type:addType,
188189

189190
sgn:(ops)=>{
190-
if(ops.some((x)=>x.isNaN))return'nan';
191-
if(ops.some((x)=>x.isReal===false))return'unsigned';
191+
if(ops.some((x)=>x.isNaN))return'unsigned';
192192
if(ops.every((x)=>x.is(0)))return'zero';
193193
if(ops.every((x)=>x.isNonNegative))
194194
returnops.some((x)=>x.isPositive) ?'positive' :'non-negative';
195195
if(ops.every((x)=>x.isNonPositive))
196196
returnops.some((x)=>x.isNegative) ?'negative' :'non-positive';
197-
if(ops.every((x)=>x.isReal))return'real';
198197
returnundefined;
199198
},
200199

@@ -280,11 +279,12 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
280279
sgn:(ops)=>{
281280
const[n,d]=[ops[0],ops[1]];
282281
if(d.is(0))return'unsigned';
283-
if(d.is(0)===false&&n.is(0))return'zero';
284282
if(d.isPositive)returnn.sgn;
285283
if(d.isNegative)returnoppositeSgn(n.sgn);
286-
if(n.is(0)||(n.isFinite&&d.isInfinity))return'zero';
287-
if(!n.is(0)&&!d.is(0))return'not-zero';
284+
consts=d.sgn;
285+
if((n.is(0)&&s==='not-zero')||(n.isFinite&&d.isInfinity))
286+
return'zero';
287+
if(n.sgn==='not-zero'&&s==='not-zero')return'not-zero';
288288
returnundefined;
289289
},
290290

@@ -669,7 +669,10 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
669669
:undefined;
670670
if(
671671
ops.some((x)=>x.isFinite===false||x.isFinite===undefined)&&
672-
ops.some((x)=>x.is(0)===undefined)
672+
ops.some((x)=>{
673+
consts=x.sgn;
674+
s!=='positive'&&s!=='negative'&&s!=='not-zero';
675+
})
673676
)
674677
returnundefined;
675678
if(ops.every((x)=>x.isPositive||x.isNegative)){
@@ -686,11 +689,17 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
686689
});
687690
returnsumNeg%2===0 ?'non-positive' :'non-negative';
688691
}
689-
if(ops.every((x)=>!x.is(0)))return'not-zero';
690-
if(ops.every((x)=>x.isReal))return'real';
692+
if(
693+
ops.every(
694+
(x)=>
695+
x.sgn==='not-zero'||
696+
x.sgn==='positive'||
697+
x.sgn==='negative'
698+
)
699+
)
700+
return'not-zero';
691701
returnundefined;
692702
},
693-
694703
evaluate:(ops,{ numericApproximation})=>
695704
// Use evaluate i both cases: do not introduce premature rounding errors
696705
numericApproximation
@@ -776,8 +785,13 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
776785
returna.sgn;
777786

778787
if(b.numerator.isEven&&b.denominator.isOdd){
779-
if(a.isReal)return!a.is(0) ?'positive' :'non-negative';
780-
if(a.type.is('imaginary'))return'negative';
788+
if(a.isReal){
789+
lets=a.sgn;
790+
returns==='positive'||s==='not-zero'||s==='negative'
791+
?'positive'
792+
:'non-negative';
793+
}
794+
if(a.type.matches('complex'))return'negative';
781795
return!a.is(0) ?'not-zero' :undefined;//already accounted for a.is(0)
782796
}
783797

@@ -868,10 +882,18 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
868882
sgn:([x,n])=>{
869883
// Note: we can't simplify this to a power, then get the sgn of that because this may cause an infinite loop
870884
if(x.isReal===false||n.isReal===false)return'unsigned';
871-
if(x.is(0))returnn.is(0) ?'unsigned' :'zero';
885+
if(x.is(0)){
886+
if(n.isNonPositive){
887+
return'unsigned';
888+
}
889+
if(n.isPositive)return'zero';
890+
}
872891
if(x.isPositive===true)return'positive';
873-
if(n.isOdd===true)return'negative';
874-
if(n.isEven===true)return'unsigned';
892+
if(x.isNonNegative===true)return'non-negative';
893+
if(n.isOdd===true||(n.numerator.isOdd&&n.denominator.isOdd)){
894+
returnx.sgn;
895+
}
896+
if(x.isNegative&&n.isOdd===false)return'unsigned';
875897
returnundefined;
876898
},
877899
canonical:(args,{ engine})=>{
@@ -905,7 +927,6 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
905927
if(x.isLess(0.5)&&x.isGreater(-0.5))return'zero';
906928
if(x.isNonNegative)return'non-negative';
907929
if(x.isNonPositive)return'non-positive';
908-
if(x.isReal)return'real';
909930
returnundefined;
910931
},
911932
evaluate:([x])=>
@@ -982,7 +1003,7 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
9821003
if(x.isPositive)return'positive';
9831004
if(x.isNegative)return'unsigned';
9841005
if(x.isNonNegative)return'non-negative';
985-
if(!x.is(0))return'not-zero';
1006+
if(x.sgn==='not-zero')return'not-zero';
9861007
returnundefined;
9871008
},
9881009
evaluate:([x],{ numericApproximation, engine})=>{
@@ -1003,9 +1024,14 @@ export const ARITHMETIC_LIBRARY: IdentifierDefinitions[] = [
10031024
signature:'number -> number',
10041025
sgn:([x])=>{
10051026
if(x.is(0))return'zero';
1006-
if(x.isReal)return!x.is(0) ?'positive' :'non-negative';
1027+
if(x.isReal){
1028+
lets=x.sgn;
1029+
returns==='not-zero'||s==='positive'||s==='negative'
1030+
?'positive'
1031+
:'non-negative';
1032+
}
10071033
if(x.type.matches('complex'))return'negative';
1008-
if(x.isReal===false||x.isNaN)return'unsigned';
1034+
if(x.isReal==false||x.isNaN)return'unsigned';
10091035
returnundefined;
10101036
},
10111037
canonical:(args,{ engine})=>{

‎test/compute-engine/latex-syntax/matchfix.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ describe('MATCHFIX abs and norm', () => {
100100
expect(check('|(1+|a|+2)|')).toMatchInlineSnapshot(`
101101
box = ["Abs", ["Delimiter", ["Add", 1, ["Abs", "a"], 2]]]
102102
canonical = ["Abs", ["Add", ["Abs", "a"], 1, 2]]
103-
simplify = ||a| + 3|
104-
`));//@fixme: simplify should be |a| + 3
103+
simplify = |a| + 3
104+
`));
105105

106106
test('|1+|a|+2|',()=>
107107
expect(check('|1+|a|+2|')).toMatchInlineSnapshot(`
108108
box = ["Abs", ["Add", 1, ["Abs", "a"], 2]]
109109
canonical = ["Abs", ["Add", ["Abs", "a"], 1, 2]]
110-
simplify = ||a| + 3|
111-
`));//@fixme: simplify should be |a| + 3
110+
simplify = |a| + 3
111+
`));
112112

113113
test('||a||',()=>
114114
expect(check('||a||')).toMatchInlineSnapshot(`["Norm", "a"]`));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp