1
1
using System . Linq ;
2
+ using System . Text . RegularExpressions ;
2
3
using Xunit ;
3
4
using AngouriMath ;
4
5
@@ -10,14 +11,18 @@ MathList ParseLaTeX(string latex) =>
10
11
Evaluation . MathItem ParseMath ( string latex ) =>
11
12
Evaluation . Evaluate ( ParseLaTeX ( latex ) ) . Match ( entity => entity , e => throw new Xunit . Sdk . XunitException ( e ) ) ;
12
13
void Test ( string input , string converted , string ? result ) {
13
- var math = ParseMath ( input ) ;
14
- Assert . NotNull ( math ) ;
15
- Assert . Equal ( converted , LaTeXParser . MathListToLaTeX ( Evaluation . Parse ( math ) ) . ToString ( ) ) ;
16
- // Ensure that the converted entity is valid by simplifying it
17
- if ( result != null )
18
- Assert . Equal ( result ,
19
- LaTeXParser . MathListToLaTeX ( Evaluation . Parse ( Assert . IsType < Evaluation . MathItem . Entity > ( math ) . Content . Simplify ( ) ) ) . ToString ( ) ) ;
20
- else Assert . IsNotType < Evaluation . MathItem . Entity > ( result ) ;
14
+ void Test ( string input ) {
15
+ var math = ParseMath ( input ) ;
16
+ Assert . NotNull ( math ) ;
17
+ Assert . Equal ( converted , LaTeXParser . MathListToLaTeX ( Evaluation . Parse ( math ) ) . ToString ( ) ) ;
18
+ // Ensure that the converted entity is valid by simplifying it
19
+ if ( result != null )
20
+ Assert . Equal ( result ,
21
+ LaTeXParser . MathListToLaTeX ( Evaluation . Parse ( Assert . IsType < Evaluation . MathItem . Entity > ( math ) . Content . Simplify ( ) ) ) . ToString ( ) ) ;
22
+ else Assert . IsNotType < Evaluation . MathItem . Entity > ( result ) ;
23
+ }
24
+ Test ( input ) ;
25
+ Test ( Regex . Replace ( input , @"(?<!\\sqrt)(\(|\[|\\\{)((?:(?!\(|\[|\\\\{|\)|\]|\\\\}).|(?<open>\(|\[|\\\\{)|(?<-open>\)|\]|\\\\}))+(?(open)(?!)))(\)|\]|\\\\})" , @"\left$1$2\right$3" ) ) ;
21
26
}
22
27
[ Theory ]
23
28
[ InlineData ( "1" , "1" ) ]
@@ -27,6 +32,7 @@ void Test(string input, string converted, string? result) {
27
32
[ InlineData ( ".5678" , "0.5678" ) ]
28
33
[ InlineData ( ".9876543210" , "0.987654321" ) ]
29
34
[ InlineData ( "1234.5678" , "1234.5678" ) ]
35
+ [ InlineData ( @"\infty" , @"\infty " ) ]
30
36
public void Numbers ( string number , string output ) =>
31
37
Test ( number , output , output ) ;
32
38
[ Theory ]
@@ -100,8 +106,20 @@ public void Numbers(string number, string output) =>
100
106
[ InlineData ( @"\frac ab*2" , @"\frac{a}{b}\times 2" , @"\frac{2\times a}{b}" ) ]
101
107
[ InlineData ( @"2/\frac ab" , @"\frac{2}{\frac{a}{b}}" , @"\frac{2\times b}{a}" ) ]
102
108
[ InlineData ( @"\frac ab/2" , @"\frac{\frac{a}{b}}{2}" , @"\frac{\frac{a}{2}}{b}" ) ]
109
+ [ InlineData ( @"1+i" , @"1+i" , @"1+i" ) ]
110
+ [ InlineData ( @"1-i" , @"1-i" , @"1-i" ) ]
111
+ [ InlineData ( @"i+1" , @"i+1" , @"1+i" ) ]
112
+ [ InlineData ( @"i-1" , @"i-1" , @"-1+i" ) ]
113
+ [ InlineData ( @"i\times i" , @"i\times i" , @"-1" ) ]
114
+ [ InlineData ( @"\infty+1" , @"\infty +1" , @"\infty " ) ]
115
+ [ InlineData ( @"i+\infty" , @"i+\infty " , @"\infty +i" ) ]
116
+ [ InlineData ( @"\infty+\infty" , @"\infty +\infty " , @"\infty " ) ]
117
+ [ InlineData ( @"\frac2\infty" , @"\frac{2}{\infty }" , @"0" ) ]
118
+ [ InlineData ( @"\frac{-2}\infty" , @"\frac{-2}{\infty }" , @"0" ) ]
103
119
public void BinaryOperators ( string latex , string converted , string result ) => Test ( latex , converted , result ) ;
104
120
[ Theory ]
121
+ [ InlineData ( "+i" , "i" , "i" ) ]
122
+ [ InlineData ( "-i" , "-i" , "-i" ) ]
105
123
[ InlineData ( "+a" , "a" , "a" ) ]
106
124
[ InlineData ( "-a" , "-a" , "-a" ) ]
107
125
[ InlineData ( "++a" , "a" , "a" ) ]
@@ -171,6 +189,14 @@ public void Numbers(string number, string output) =>
171
189
[ InlineData ( "0^x" , @"0^x" , @"0" ) ]
172
190
[ InlineData ( "1^x" , @"1^x" , @"1" ) ]
173
191
[ InlineData ( "x^0" , @"x^0" , @"1" ) ]
192
+ [ InlineData ( "-i^{-1}" , @"-i^{-1}" , @"i" ) ]
193
+ [ InlineData ( "i^{-1}" , @"i^{-1}" , @"-i" ) ]
194
+ [ InlineData ( "i^0" , @"i^0" , @"1" ) ]
195
+ [ InlineData ( "i^1" , @"i^1" , @"i" ) ]
196
+ [ InlineData ( "i^2" , @"i^2" , @"-1" ) ]
197
+ [ InlineData ( "i^3" , @"i^3" , @"-i" ) ]
198
+ [ InlineData ( "i^4" , @"i^4" , @"1" ) ]
199
+ [ InlineData ( "i^5" , @"i^5" , @"i" ) ]
174
200
[ InlineData ( @"{\frac 12}^4" , @"\left( \frac{1}{2}\right) ^4" , "0.0625" ) ]
175
201
[ InlineData ( @"\sqrt2" , @"\sqrt{2}" , "1.4142135623730951" ) ]
176
202
[ InlineData ( @"\sqrt2^2" , @"\left( \sqrt{2}\right) ^2" , "2.0000000000000004" ) ]
@@ -354,31 +380,27 @@ public void Numbers(string number, string output) =>
354
380
[ InlineData ( @"\sin^a\ (x)^2(x)" , @"\sin \left( x\right) ^{a\times 2}\times x" , @"\sin \left( x\right) ^{2\times a}\times x" ) ]
355
381
[ InlineData ( @"\sin^a\; (x)^2(x)" , @"\sin \left( x\right) ^{a\times 2}\times x" , @"\sin \left( x\right) ^{2\times a}\times x" ) ]
356
382
[ InlineData ( @"\sin^a\ \; (x)^2(x)" , @"\sin \left( x\right) ^{a\times 2}\times x" , @"\sin \left( x\right) ^{2\times a}\times x" ) ]
357
- public void Parentheses ( string latex , string converted , string result ) {
358
- Test ( latex , converted , result ) ;
359
- Test ( latex . Replace ( "(" , @"\left(" ) . Replace ( ")" , @"\right)" ) , converted , result ) ;
360
- }
383
+ public void Parentheses ( string latex , string converted , string result ) => Test ( latex , converted , result ) ;
361
384
[ Theory ]
385
+ [ InlineData ( @"\begin{matrix}1\end{matrix}" , @"\left( \begin{matrix}1\end{matrix}\right) " , @"\left( \begin{matrix}1\end{matrix}\right) " ) ]
362
386
[ InlineData ( @"\begin{pmatrix}1\end{pmatrix}" , @"\left( \begin{matrix}1\end{matrix}\right) " , @"\left( \begin{matrix}1\end{matrix}\right) " ) ]
363
387
[ InlineData ( @"\begin{pmatrix}1\end{pmatrix}^2" , @"\left( \begin{matrix}1\end{matrix}\right) ^2" , @"\left( \begin{matrix}1\end{matrix}\right) ^2" ) ]
364
- public void Vectors ( string latex , string converted , string result ) =>
365
- Test ( latex , converted , result ) ;
388
+ public void Vectors ( string latex , string converted , string result ) => Test ( latex , converted , result ) ;
366
389
[ Theory ]
390
+ [ InlineData ( @"\begin{matrix}1&2\\3&4\end{matrix}" , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) " , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) " ) ]
367
391
[ InlineData ( @"\begin{pmatrix}1&2\\3&4\end{pmatrix}" , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) " , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) " ) ]
368
392
[ InlineData ( @"\begin{pmatrix}1&2\\3&4\end{pmatrix}^2" , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) ^2" , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) ^2" ) ]
369
393
[ InlineData ( @"\begin{pmatrix}1&2\\3&4\end{pmatrix}+\begin{pmatrix}1&2\\3&5\end{pmatrix}" , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) +\left( \begin{matrix}1&2\\ 3&5\end{matrix}\right) " , @"\left( \begin{matrix}1&2\\ 3&4\end{matrix}\right) +\left( \begin{matrix}1&2\\ 3&5\end{matrix}\right) " ) ]
370
- public void Matrices ( string latex , string converted , string result ) =>
371
- Test ( latex , converted , result ) ;
394
+ public void Matrices ( string latex , string converted , string result ) => Test ( latex , converted , result ) ;
372
395
[ Theory ]
373
396
[ InlineData ( @"1,2" , @"1,2" ) ]
374
397
[ InlineData ( @"1,2,3" , @"1,2,3" ) ]
375
398
[ InlineData ( @"a,b,c,d" , @"a,b,c,d" ) ]
376
399
[ InlineData ( @"\sqrt2,\sqrt[3]2,\frac34" , @"\sqrt{2},2^{\frac{1}{3}},\frac{3}{4}" ) ]
377
400
[ InlineData ( @"\sin a,\cos b^2,\tan c_3,\cot de,\sec 12f,\csc g+h" ,
378
401
@"\sin \left( a\right) ,\cos \left( b^2\right) ,\tan \left( c_3\right) ,\cot \left( d\times e\right) ,\frac{1}{\cos \left( 12\times f\right) },\frac{1}{\sin \left( g\right) }+h" ) ]
379
- public void Comma ( string latex , string converted ) =>
380
- Test ( latex , converted , null ) ;
381
- [ Theory ( Skip = "https://github.com/asc-community/AngouriMath/pull/94" ) ]
402
+ public void Comma ( string latex , string converted ) => Test ( latex , converted , null ) ;
403
+ [ Theory ( Skip = "https://github.com/asc-community/AngouriMath/pull/97" ) ]
382
404
[ InlineData ( @"\emptyset" , @"\emptyset " ) ]
383
405
[ InlineData ( @"\mathbb R" , @"\emptyset " ) ] // wip
384
406
[ InlineData ( @"\mathbb C" , @"\emptyset " ) ] // wip
@@ -387,27 +409,18 @@ public void Comma(string latex, string converted) =>
387
409
[ InlineData ( @"\{1,2\}" , @"\left\{ 1,2\right\} " ) ]
388
410
[ InlineData ( @"\{x,y\}" , @"\left\{ x,y\right\} " ) ]
389
411
[ InlineData ( @"\{\sqrt[3]2,\frac34,\sin^2x\}" , @"\left\{ 2^{\frac{1}{3}},\frac{3}{4},\sin \left( x\right) ^2\right\} " ) ]
390
- public void Sets ( string latex , string converted ) {
391
- Test ( latex , converted , null ) ;
392
- Test ( latex . Replace ( @"\{" , @"\left\{" ) . Replace ( @"\}" , @"\right\}" ) , converted , null ) ;
393
- }
412
+ public void Sets ( string latex , string converted ) => Test ( latex , converted , null ) ;
394
413
[ Theory ]
395
414
[ InlineData ( @"\emptyset\cup\{2\}" , @"\left\{ 2\right\} " ) ]
396
415
[ InlineData ( @"\{1\}\cup\{2\}" , @"\left\{ 1,2\right\} " ) ]
397
- [ InlineData ( @"\{3,4\}\cap\emptyset" , @"\emptyset " ) ]
416
+ [ InlineData ( @"\{3,4\}\cap\emptyset" , @"\left( \left\{ 3,4\right\} \cap \right) \left( \ emptyset \right) " ) ]
398
417
[ InlineData ( @"\{3,4\}\cap\{4,5\}" , @"\left\{ 4\right\} " ) ]
399
418
[ InlineData ( @"\{2,3,4\}\setminus\{4\}" , @"\left\{ 2,3\right\} " ) ]
400
419
//[InlineData(@"\{3\}^\complement", @"\left\{ 3\right\} ^\complement")] // wip
401
- public void SetOperations ( string latex , string converted ) {
402
- Test ( latex , converted , null ) ;
403
- Test ( latex . Replace ( @"\{" , @"\left\{" ) . Replace ( @"\}" , @"\right\}" ) , converted , null ) ;
404
- }
405
- [ Theory ( Skip = "https://github.com/asc-community/AngouriMath/pull/93" ) ]
420
+ public void SetOperations ( string latex , string converted ) => Test ( latex , converted , null ) ;
421
+ [ Theory ( Skip = "https://github.com/asc-community/AngouriMath/pull/97" ) ]
406
422
[ InlineData ( @"(1,2)" , @"\left\{ \left( 1,2\right) \right\} " ) ] // wip
407
- public void Intervals ( string latex , string converted ) {
408
- Test ( latex , converted , null ) ;
409
- Test ( latex . Replace ( "(" , @"\left(" ) . Replace ( ")" , @"\right)" ) , converted , null ) ;
410
- }
423
+ public void Intervals ( string latex , string converted ) => Test ( latex , converted , null ) ;
411
424
[ Theory ]
412
425
[ InlineData ( @"" , "There is nothing to evaluate" ) ]
413
426
[ InlineData ( @"\ " , "There is nothing to evaluate" ) ]
0 commit comments