@@ -32,7 +32,7 @@ private protected MathItem() { }
32
32
public static explicit operator AngouriMath . Entity ( MathItem item ) => ( ( Entity ) item ) . Content ;
33
33
public static implicit operator MathItem ( SetNode content ) => new Set ( content ) ;
34
34
public static explicit operator SetNode ( MathItem item ) => ( ( Set ) item ) . Content ;
35
- /// <summary>An real number, complex number, variable or function call</summary>
35
+ /// <summary>A real number, complex number, variable, function call, vector, matrix or higher-dimensional tensor </summary>
36
36
public sealed class Entity : MathItem {
37
37
public Entity ( AngouriMath . Entity content ) => Content = content ;
38
38
public AngouriMath . Entity Content { get ; }
@@ -79,6 +79,18 @@ result is { } r
79
79
int i = 0 ;
80
80
return Transform ( mathList , ref i , Precedence . DefaultContext ) ;
81
81
}
82
+ static Result < Entity [ ] > ExpectEntities ( this Result < MathItem ? > result , string itemName ) =>
83
+ result . Bind ( item => item switch {
84
+ null => Array . Empty < Entity > ( ) ,
85
+ MathItem . Entity { Content : var e } => new [ ] { e } ,
86
+ MathItem . Comma c =>
87
+ c . Aggregate ( ( Result : Result . Ok ( new Entity [ c . Count ( ) ] ) , Index : 0 ) , ( acc , item ) =>
88
+ ( acc . Result . Bind ( s => item . AsEntity ( itemName ) . Bind ( i => { s [ acc . Index ] = i ; return s ; } ) ) , acc . Index + 1 ) ,
89
+ acc => acc . Result ) ,
90
+ var notEntity => Result . Err ( item . GetType ( ) . Name + " cannot be " + itemName )
91
+ } ) ;
92
+ static Result < Entity [ ] > AsEntities ( this MathItem ? item , string itemName ) =>
93
+ Result . Ok ( item ) . ExpectEntities ( itemName ) ;
82
94
static Result < Entity ? > ExpectEntityOrNull ( this Result < MathItem ? > result , string itemName ) =>
83
95
result . Bind ( item => item switch {
84
96
null => Result . Ok ( ( Entity ? ) null ) ,
@@ -149,22 +161,15 @@ static Result<MathItem> TryMakeSet(MathItem.Comma c, bool leftClosed, bool right
149
161
MathItem . Comma c => TryMakeSet ( c , true , true ) ,
150
162
_ => "Unrecognized bracket pair [ ]"
151
163
} } ,
152
- { ( "{" , "}" ) , item => item switch {
153
- null => ( MathItem ) MathS . Sets . Empty ( ) ,
154
- MathItem . Entity { Content : var e } => ( MathItem ) MathS . Sets . Finite ( e ) ,
155
- MathItem . Comma c =>
156
- c . Aggregate ( Result . Ok ( MathS . Sets . Empty ( ) ) , ( set , item ) =>
157
- set . Bind ( s => item . AsEntity ( "set element" ) . Bind ( i => { s . Add ( i ) ; return s ; } ) ) ,
158
- set => set . Bind ( s => ( MathItem ) s ) ) ,
159
- _ => item . GetType ( ) . Name + " cannot be a set element"
160
- } } ,
164
+ { ( "{" , "}" ) , item => item . AsEntities ( "set element" ) . Bind ( entities => ( MathItem ) MathS . Sets . Finite ( entities ) ) }
161
165
} ;
162
166
static Result < MathItem ? > Transform ( MathList mathList , ref int i , Precedence prec ) {
163
167
MathItem ? prev = null ;
164
168
MathItem ? next ;
165
169
string ? error ;
166
170
Precedence handlePrecendence ;
167
171
Func < Entity , Entity > handlePrefix , handlePostfix , handleFunction , handleFunctionInverse ;
172
+ Func < Entity [ ] , Entity > handleFunctionN , handleFunctionInverseN ;
168
173
Func < Entity , Entity , Entity > handleBinary ;
169
174
Func < SetNode , SetNode > handlePrefixSet , handlePostfixSet , handleFunctionSet , handleFunctionInverseSet ;
170
175
Func < SetNode , SetNode , SetNode > handleBinarySet ;
@@ -233,6 +238,9 @@ _ when LaTeXSettings.CommandForAtom(atom) is string s => MathS.Var(s + subscript
233
238
} ;
234
239
v . Subscript . Clear ( ) ;
235
240
goto handleThis ;
241
+ case Atoms . Ordinary { Nucleus : "∞" } :
242
+ @this = new NumberEntity ( MathS . Num ( double . PositiveInfinity ) ) ;
243
+ goto handleThis ;
236
244
case Atoms . Ordinary { Nucleus : "∅" } :
237
245
@this = MathS . Sets . Empty ( ) ;
238
246
goto handleThis ;
@@ -399,6 +407,17 @@ _ when LaTeXSettings.CommandForAtom(atom) is string s => MathS.Var(s + subscript
399
407
handlePrecendence = Precedence . PercentDegree ;
400
408
handlePostfix = x => x * MathS . pi / 180 ;
401
409
goto handlePostfix ;
410
+ case Atoms . Table { Environment : "matrix" , NRows : var rows , NColumns : var cols , Cells : var cells } :
411
+ var matrixElements = new Entity [ rows * cols ] ;
412
+ for ( var row = 0 ; row < rows ; row ++ )
413
+ for ( var col = 0 ; col < cols ; col ++ ) {
414
+ if ( cells [ row ] . Count <= col )
415
+ return $ "There are empty slots in the { rows } ×{ cols } matrix";
416
+ ( matrixElements [ row * cols + col ] , error ) = Transform ( cells [ row ] [ col ] ) . ExpectEntity ( "matrix element" ) ;
417
+ if ( error != null ) return error ;
418
+ }
419
+ @this = MathS . Matrices . Matrix ( rows , cols , matrixElements ) ;
420
+ goto handleThis ;
402
421
case Atoms . Punctuation { Nucleus : "," } :
403
422
if ( prec <= Precedence . Comma ) {
404
423
if ( prev is null ) return "Missing left operand for comma" ;
@@ -426,17 +445,32 @@ _ when LaTeXSettings.CommandForAtom(atom) is string s => MathS.Var(s + subscript
426
445
handleBinarySet = ( l , r ) => l - r ;
427
446
goto handleBinarySet ;
428
447
case Atoms . Space _:
448
+ case Atoms . Style _:
429
449
case Atoms . Ordinary { Nucleus : var nucleus } when string . IsNullOrWhiteSpace ( nucleus ) :
450
+ if ( atom . Superscript . Count > 0 )
451
+ return $ "Exponentiation is unsupported for { atom . TypeName } ";
452
+ if ( atom . Subscript . Count > 0 )
453
+ return $ "Subscripts are unsupported for { atom . TypeName } ";
430
454
continue ;
455
+ case Atoms . Table table :
456
+ return $ "Unsupported table environment { table . Environment } ";
431
457
default :
432
458
return $ "Unsupported { atom . TypeName } { atom . Nucleus } ";
433
459
460
+ #pragma warning disable CS0162 // Unreachable code detected
461
+ #pragma warning disable CS0164 // This label has not been referenced
434
462
handleFunction :
435
463
handleFunctionInner = ( itemName , item ) =>
436
464
item . AsEntity ( itemName ) . Bind ( e => ( MathItem ) handleFunction ( e ) ) ;
437
465
handleFunctionInverseInner = ( itemName , item ) =>
438
466
item . AsEntity ( itemName ) . Bind ( e => ( MathItem ) handleFunctionInverse ( e ) ) ;
439
467
goto handleFunctionInner ;
468
+ handleFunctionN :
469
+ handleFunctionInner = ( itemName , item ) =>
470
+ item . AsEntities ( itemName ) . Bind ( e => ( MathItem ) handleFunctionN ( e ) ) ;
471
+ handleFunctionInverseInner = ( itemName , item ) =>
472
+ item . AsEntities ( itemName ) . Bind ( e => ( MathItem ) handleFunctionInverseN ( e ) ) ;
473
+ goto handleFunctionInner ;
440
474
handleFunctionSet :
441
475
handleFunctionInner = ( itemName , item ) =>
442
476
item . AsSet ( itemName ) . Bind ( set => ( MathItem ) handleFunctionSet ( set ) ) ;
@@ -591,6 +625,8 @@ _ when LaTeXSettings.CommandForAtom(atom) is string s => MathS.Var(s + subscript
591
625
i -- ;
592
626
return prev ;
593
627
}
628
+ #pragma warning restore CS0162 // Unreachable code detected
629
+ #pragma warning restore CS0164 // This label has not been referenced
594
630
595
631
handleThis :
596
632
if ( atom . Subscript . Count > 0 )
0 commit comments