@@ -23,6 +23,7 @@ internal static TDelegate Compile<TDelegate>(
2323 ) where TDelegate : Delegate
2424 {
2525 bool IsMatrixArgument ( Variable variable ) => typesAndNames . Any ( c => c . variable == variable && TensorCompilation . IsMatrixType ( c . type ) ) ;
26+ Type ? TypeOf ( Variable variable ) => typesAndNames . FirstOrDefault ( c => c . variable == variable ) . type ;
2627 // A matrix has no compiled form, but an expression built out of matrices often
2728 // has a value that is an ordinary number -- [0, 1]T * [[a, b], [c, d]] * [1, 0]
2829 // is c -- and there is nothing to stop that being compiled. Nothing simplified
@@ -36,7 +37,9 @@ internal static TDelegate Compile<TDelegate>(
3637 StatementPositions . Check ( expr ) ;
3738 // Two matrices whose shapes are written into the expression and do not fit are refused here, rather than by
3839 // a delegate that throws at every point it is called at. https://github.com/asc-community/AngouriMath/issues/526
39- TensorCompilation . CheckShapes ( expr , IsMatrixArgument ) ;
40+ Func < Variable , ( int Rows , int Columns ) ? > argumentShape = variable => TensorCompilation . ShapeOfArgument ( TypeOf ( variable ) ) ;
41+ TensorCompilation . CheckShapes ( expr , argumentShape ) ;
42+ CheckResultShape ( expr , returnType , argumentShape ) ;
4043 if ( MathS . Settings . RefuseNonRealCompilation && returnType is { } real && IsRealType ( real )
4144 && typesAndNames . All ( c => IsRealType ( c . type ) ) && ! IsRealWhereverDefined ( expr , typesAndNames ) )
4245 throw new UncompilableNodeException (
@@ -77,11 +80,15 @@ static bool IsComplex(Type? type)
7780 var constantType = typesAndNames . Any ( c => IsComplex ( c . type ) ) || IsComplex ( returnType )
7881 ? typeof ( System . Numerics . Complex )
7982 : matrixElements . Count == 1 ? matrixElements [ 0 ] : null ;
83+ // Only a matrix argument can be a vector whose shape is not written out: abs of a written one is its norm
84+ // after simplification already.
85+ if ( typesAndNames . Any ( c => TensorCompilation . IsMatrixType ( c . type ) ) )
86+ BuildNorms ( expr , argumentShape , subexpressionsCache , variableAssignments , localVars , protocol , constantType ) ;
8087 var tree = BuildTree ( expr , subexpressionsCache , variableAssignments , localVars , protocol , constantType ) ;
8188 // A matrix known to be 1 x 1 asked for as a number is its entry, as evaluation has it. Any other matrix
8289 // is refused as a number by ConvertType.
8390 if ( TensorCompilation . IsTensor ( tree . Type ) && returnType is not null && ! TensorCompilation . IsMatrixType ( returnType )
84- && TensorCompilation . IsOneByOne ( expr , IsMatrixArgument ) )
91+ && TensorCompilation . IsOneByOne ( expr , argumentShape ) )
8592 tree = TensorCompilation . ToScalar ( tree ) ;
8693 var treeWithLocals = Expression . Block ( localVars , variableAssignments . Append ( tree ) ) ;
8794 Expression entireExpression = returnType is not null ? protocol . ConvertType ( treeWithLocals , returnType ) : treeWithLocals ;
@@ -98,6 +105,43 @@ or NotSupportedException
98105 }
99106 }
100107
108+ /// <summary>
109+ /// Every <c>abs</c> of a column vector, compiled to its norm ahead of the rest and cached, so that the tree
110+ /// built after it reads the number. Of a vector, <c>abs</c> is a number, and of any other matrix a matrix,
111+ /// and only the shape known when compiling can tell which.
112+ /// https://github.com/asc-community/AngouriMath/issues/2423
113+ /// </summary>
114+ private static void BuildNorms ( Entity expr , Func < Variable , ( int Rows , int Columns ) ? > argumentShape ,
115+ Dictionary < Entity , ParameterExpression > cachedSubexpressions , List < Expression > variableAssignments ,
116+ List < ParameterExpression > newLocalVars , CompilationProtocol protocol , Type ? constantType )
117+ {
118+ foreach ( var norm in TensorCompilation . Norms ( expr , argumentShape ) )
119+ {
120+ if ( cachedSubexpressions . ContainsKey ( norm ) )
121+ continue ;
122+ var vector = BuildTree ( norm . Argument , cachedSubexpressions , variableAssignments , newLocalVars , protocol , constantType ) ;
123+ if ( ! TensorCompilation . IsTensor ( vector . Type ) )
124+ continue ;
125+ var value = Expression . Variable ( typeof ( double ) ) ;
126+ variableAssignments . Add ( Expression . Assign ( value , TensorCompilation . Norm ( vector ) ) ) ;
127+ newLocalVars . Add ( value ) ;
128+ cachedSubexpressions [ norm ] = value ;
129+ }
130+ }
131+
132+ /// <summary>
133+ /// A matrix known when compiling not to be a column, asked for as a vector, is refused then rather than by a
134+ /// delegate that throws at every call.
135+ /// </summary>
136+ private static void CheckResultShape ( Entity expr , Type ? returnType , Func < Variable , ( int Rows , int Columns ) ? > argumentShape )
137+ {
138+ if ( returnType is null || ! TensorCompilation . IsVectorArray ( returnType ) || TensorCompilation . Shape ( expr , argumentShape ) is not { } shape )
139+ return ;
140+ if ( shape . Columns > 1 )
141+ throw new InvalidMatrixOperationException (
142+ $ "`{ expr . Stringize ( ) } ` is a matrix of { shape . Columns } columns, not a column vector, and cannot be compiled as { returnType } ") ;
143+ }
144+
101145 private static bool IsRealType ( Type type )
102146 => type == typeof ( double ) || type == typeof ( float ) || type == typeof ( int ) || type == typeof ( long )
103147 || type == typeof ( System . Numerics . BigInteger ) ;
0 commit comments