{ Analytica Model Flattening_triangula, encoding="UTF-8" } SoftwareVersion 6.0.9 { System Variables with non-default values: } SampleSize := 100 TypeChecking := 1 Checking := 1 SaveOptions := 2 SaveValues := 0 {!40300|Sys_DomainSelfIndex := 1} {!40400|Sys_AllNullTreatment := 1} {!50016|PlusOnTextReturnsNaN := 0} {!50400|Sys_UseLegacyColors := 1} {!-50299|DiagramColor Model: 65535,65535,65535} {!-50299|DiagramColor Module: 65535,65535,65535} {!-50299|DiagramColor LinkModule: 65535,65535,65535} {!-50299|DiagramColor Library: 65535,65535,65535} {!-50299|DiagramColor LinkLibrary: 65535,65535,65535} {!-50299|DiagramColor Form: 65535,65535,65535} NodeColor Text: 65535,65535,65535 {!-60000|Attribute AcpStyles} Model Flattening_triangula Title: Flattening triangular matrix Description: Challenge Problem: Flattening a triangular matrix, keeping on those elements above the diagonal. Author: Lonnie Chrisman Date: Thu, Aug 30, 2007 12:32 PM DefaultSize: 48,24 DiagState: 2,1,0,483,345,17 DiagramColor: 52428,52428,52428 FontStyle: Arial, 15 FileInfo: 0,Model Flattening_triangula,2,2,0,0,C:\Temp\Flattening_triangular_matrix.ana {!40400|Att_clearTypeFonts: 0} Index I Title: I Definition: 1..5 NodeLocation: 96,56,1 NodeSize: 48,24 Index J Title: J Definition: 1..5 NodeLocation: 208,56,1 NodeSize: 48,24 Variable A Title: A Definition: min([I,J])*10 + max([I,J]) NodeLocation: 96,120,1 NodeSize: 48,24 ValueState: 2,45,8,416,303,0,MIDM Aliases: Alias Al641068275 ReformDef: [J,I] ReformVal: [J,I] Text Te1 Description: Challenge: Define B to be a vector containing the elements of A above the diagonal. Define C to be a 1-D array that includes the diagonal and the elements above. NodeLocation: 384,110,-1 NodeSize: 88,74 Variable B Title: B NodeLocation: 208,120,1 NodeSize: 48,24 Variable C Title: C NodeLocation: 208,176,1 NodeSize: 48,24 Module Solution_1 Title: Solution 1 Description: This solution flattens the entire 2-D array, then subsets to keep only those elements above the diagonal. Author: Lonnie Chrisman Date: Thu, Jan 30, 2025 11:32 AM DefaultSize: 48,24 NodeLocation: 96,272,1 NodeSize: 48,24 DiagState: 2,617,40,534,165,17 Variable B1 Title: B Definition: LocalIndex K1 := 1..size(I)*size(J);~ LocalIndex L := ['I','J','A'];~ Local full [K1,L] := MdArrayToTable(A,K1,L);~ Localindex K2 := subset( full[ L='I'] < full [L='J'] );~ { This step is optional -- but just producing informative labels in K }~ Localindex K := full[K1=K2,L='I'] & "," & full[K1=K2,L='J'];~ full[ L='A', K1=K2, @K2=@K ] NodeLocation: 88,64,1 NodeSize: 48,24 ValueState: 2,493,54,416,303,0,MIDM ReformVal: [Sys_LocalIndex('K'),Sys_LocalIndex('L')] Variable C1 Title: C Definition: LocalIndex K1 := 1..size(I)*size(J);~ LocalIndex L := ['I','J','A'];~ Local full [K1,L] := MdArrayToTable(A,K1,L);~ Localindex K2 := subset( full[ L='I'] <= full [L='J'] );~ { This step is optional -- but just producing informative labels in K }~ LocalIndex K := full[K1=K2,L='I'] & "," & full[K1=K2,L='J'];~ full[ L='A', K1=K2, @K2=@K ] NodeLocation: 200,64,1 NodeSize: 48,24 ReformVal: [Sys_LocalIndex('L'),Sys_LocalIndex('K')] Close Solution_1 Module Solution_2 Title: Solution 2 Description: This solution accesses the elements directly by computing the I and J coordinates of the K'th element in the result. Author: Lonnie Chrisman Date: Thu, Jan 30, 2025 11:32 AM DefaultSize: 48,24 NodeLocation: 208,272,1 NodeSize: 48,24 DiagState: 2,460,39,534,261,17 WindState: 2,102,90,474,224 Index Kth Title: Kth Description: Indexes for the elements of B, not including the diagonal. Definition: 1.. (size(J)-1 + size(J)-size(I)) * size(I)/2 NodeLocation: 72,56,1 NodeSize: 48,24 Variable I_pos Title: i pos Definition: StepInterp( cumulate( size(J)-@I, I), @I, Kth, I ) NodeLocation: 184,56,1 NodeSize: 48,24 ValueState: 2,568,339,416,303,0,MIDM Variable J_pos Title: j pos Definition: Local before := cumulate( size(J)-@I, I);~ before := before - before[@I=1];~ @Kth-before[I=i_pos]+1 NodeLocation: 288,56,1 NodeSize: 48,24 ValueState: 2,105,324,647,303,0,MIDM ReformVal: [Kth,I] Variable B2 Title: B Definition: A[@i=i_pos,@j=j_pos] NodeLocation: 184,112,1 NodeSize: 48,24 ValueState: 2,545,293,416,303,0,MIDM Index Kth2 Title: Kth2 Description: Position index for element on or above the diagonal. This one includes the diagonal Definition: 1.. (size(J) + size(J)-size(I)+1) * size(I)/2 NodeLocation: 72,192,1 NodeSize: 48,24 WindState: 2,37,447,476,224 Variable I_pos2 Title: i_pos2 Definition: StepInterp( cumulate( size(J)-@I+1, I), @I, Kth2, I ) NodeLocation: 192,192,1 NodeSize: 48,24 ValueState: 2,545,352,416,303,0,MIDM Variable J_pos2 Title: j pos2 Definition: Local before := cumulate( size(J)-@I+1, I);~ before := before - before[@I=1];~ @Kth2-before[I=i_pos2] NodeLocation: 296,192,1 NodeSize: 48,24 Variable C2 Title: C Definition: A[ @I=i_pos2, @J=j_pos2 ] NodeLocation: 192,248,1 NodeSize: 48,24 Close Solution_2 Module Solution_3 Title: Solution 3 Description: A third solution (most elegant) using the «condition» parameter of Flatten. Author: Lonnie Chrisman, Ph.D.~ Lumina Decision Systems Date: Thu, Jan 30, 2025 11:38 AM NodeLocation: 320,272,1 NodeSize: 48,24 DiagState: 2,593,25,491,293,17 Variable B3 Title: B Description: Flattened elements above the diagonal, uses a local index .K. Definition: Flatten( A, I,J, condition: I