@@ -858,6 +858,86 @@ public static void SetInPlaceReplace(Matrix matrix, Matrix value)
858858 /// <param name="sectionsPerDimension">The number of sections per dimension.</param>
859859 /// <returns>The broken up matrix.</returns>
860860 public static Matrix [ , ] BreakIntoSections ( Matrix matrix , int sectionsPerDimension )
861+ {
862+ int sectionRowCount = matrix . Rows / sectionsPerDimension ;
863+ int sectionColCount = matrix . Cols / 2 / sectionsPerDimension ; // Halve the column count for magnitude/angle split.
864+ Matrix [ , ] sections = new Matrix [ sectionsPerDimension , sectionsPerDimension ] ;
865+
866+ for ( int i = 0 ; i < sectionsPerDimension ; i ++ )
867+ {
868+ for ( int j = 0 ; j < sectionsPerDimension ; j ++ )
869+ {
870+ // Calculate section bounds for magnitudes and angles in a single loop.
871+ int startRow = i * sectionRowCount ;
872+ int endRow = ( i + 1 ) * sectionRowCount - 1 ;
873+ int startMagnitudeCol = j * sectionColCount ;
874+ int endMagnitudeCol = ( j + 1 ) * sectionColCount - 1 ;
875+ int startAngleCol = matrix . Cols / 2 + startMagnitudeCol ; // Adjust starting column for angles.
876+ int endAngleCol = startAngleCol + sectionColCount - 1 ;
877+
878+ // Extract magnitude and angle sections simultaneously.
879+ var magnitudes = matrix . GetSection ( startRow , startMagnitudeCol , endRow , endMagnitudeCol ) ;
880+ var angles = matrix . GetSection ( startRow , startAngleCol , endRow , endAngleCol ) ;
881+
882+ // Concatenate magnitude and angle sections into a single matrix for this section.
883+ sections [ i , j ] = magnitudes . ConcatenateColumns ( angles ) ;
884+ }
885+ }
886+
887+ return sections ;
888+ }
889+
890+ /// <summary>
891+ /// Combine sections.
892+ /// </summary>
893+ /// <param name="sections">The sections to combine.</param>
894+ /// <returns>The result.</returns>
895+ public static Matrix PieceTogether ( Matrix [ , ] sections )
896+ {
897+ int sectionsPerDimension = sections . GetLength ( 0 ) ;
898+ // Assuming each section is of equal size, use the first section to calculate the total size.
899+ int sectionRows = sections [ 0 , 0 ] . Rows ;
900+ int sectionCols = sections [ 0 , 0 ] . Cols / 2 ; // Half the columns in each section are for magnitudes, half for angles.
901+
902+ // Calculate the size of the combined matrix.
903+ int totalRows = sectionRows * sectionsPerDimension ;
904+ int totalCols = sectionCols * 2 * sectionsPerDimension ; // Double the columns for magnitudes and angles.
905+
906+ Matrix combined = new Matrix ( totalRows , totalCols ) ;
907+
908+ for ( int i = 0 ; i < sectionsPerDimension ; i ++ )
909+ {
910+ for ( int j = 0 ; j < sectionsPerDimension ; j ++ )
911+ {
912+ Matrix section = sections [ i , j ] ;
913+ // Calculate where to place the magnitude and angle parts in the combined matrix.
914+ int startRow = i * sectionRows ;
915+ int startMagnitudeCol = j * sectionCols ;
916+ int startAngleCol = ( totalCols / 2 ) + ( j * sectionCols ) ;
917+
918+ for ( int row = 0 ; row < sectionRows ; row ++ )
919+ {
920+ for ( int col = 0 ; col < sectionCols ; col ++ )
921+ {
922+ // Place magnitude part in the left half of the combined matrix.
923+ combined [ startRow + row , startMagnitudeCol + col ] = section [ row , col ] ;
924+ // Place angle part in the right half of the combined matrix.
925+ combined [ startRow + row , startAngleCol + col ] = section [ row , col + sectionCols ] ;
926+ }
927+ }
928+ }
929+ }
930+
931+ return combined ;
932+ }
933+
934+ /// <summary>
935+ /// Break a matrix into sections.
936+ /// </summary>
937+ /// <param name="matrix">The matrix to break.</param>
938+ /// <param name="sectionsPerDimension">The number of sections per dimension.</param>
939+ /// <returns>The broken up matrix.</returns>
940+ public static Matrix [ , ] BreakIntoSectionsExactly ( Matrix matrix , int sectionsPerDimension )
861941 {
862942 int sectionRowCount = matrix . Rows / sectionsPerDimension ;
863943 int sectionColCount = matrix . Cols / sectionsPerDimension ;
@@ -885,7 +965,7 @@ public static void SetInPlaceReplace(Matrix matrix, Matrix value)
885965 /// </summary>
886966 /// <param name="sections">The sections.</param>
887967 /// <returns>The combined matrix.</returns>
888- public static Matrix PieceTogether ( Matrix [ , ] sections )
968+ public static Matrix PieceTogetherExactly ( Matrix [ , ] sections )
889969 {
890970 int totalRows = sections . GetLength ( 0 ) * sections [ 0 , 0 ] . Rows ;
891971 int totalColumns = sections . GetLength ( 1 ) * sections [ 0 , 0 ] . Cols ;
0 commit comments