Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, elems, Range[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, elems, Range[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, elems, Range[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]
deleted 516 characters in body
Source Link
Mr.Wizard
  • 237
  • 1
  • 6

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

This is a different approach that I was hoping would be more readable, but instead it seems more opaque. Perhaps the method can inform a cleaner form that I could not see.

LowerTriangularize[
 Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s
] ~ArrayPad~ {{1, 0}, {0, 1}} /. n_?Positive :> elems[[n]]

alternatively:

LowerTriangularize[
 elems[[#]] & /@
 (Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s)
]~ArrayPad~{{1, 0}, {0, 1}}

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, elems, Range[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

This is a different approach that I was hoping would be more readable, but instead it seems more opaque. Perhaps the method can inform a cleaner form that I could not see.

LowerTriangularize[
 Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s
] ~ArrayPad~ {{1, 0}, {0, 1}} /. n_?Positive :> elems[[n]]

alternatively:

LowerTriangularize[
 elems[[#]] & /@
 (Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s)
]~ArrayPad~{{1, 0}, {0, 1}}

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, elems, Range[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, elems, Range[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]
added 212 characters in body
Source Link
Mr.Wizard
  • 237
  • 1
  • 6

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

This is a different approach that I was hoping would be more readable, but instead it seems more opaque. Perhaps the method can inform a cleaner form that I could not see.

LowerTriangularize[
 Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s
] ~ArrayPad~ {{1, 0}, {0, 1}} /. n_?Positive :> elems[[n]] // MatrixForm

alternatively:

LowerTriangularize[
 elems[[#]] & /@
 (Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s)
]~ArrayPad~{{1, 0}, {0, 1}}

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, //elems, MatrixFormRange[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

This is a different approach that I was hoping would be more readable, but instead it seems more opaque. Perhaps the method can inform a cleaner form that I could not see.

LowerTriangularize[
 Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s
] ~ArrayPad~ {{1, 0}, {0, 1}} /. n_?Positive :> elems[[n]] // MatrixForm

alternatively:

LowerTriangularize[
 elems[[#]] & /@
 (Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s)
]~ArrayPad~{{1, 0}, {0, 1}} // MatrixForm

Here are a few methods for your consideration and feedback.


This one uses the core of my "Dynamic Partition" function. It is the fastest method I know for this problem. Also, perhaps refactoring the code like this makes it more intelligible.

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p]
#~PadRight~s & /@ {{}} ~Join~ dynP[elems, Range[s-1]]

This one is slightly shorter, using a different way to construct the indices, but also slightly slower, and perhaps less transparent.

#~PadRight~s & /@ 
 Prepend[
 elems~Take~# & /@ 
 Array[{1 + # (# - 1)/2, # (# + 1)/2} &, s - 1],
 {}
 ]

This one is terse, but slow. Legibility is debatable.

SparseArray[Join @@ Table[{i, j}, {i, s}, {j, i - 1}] -> elems, s] // MatrixForm

This is a different approach that I was hoping would be more readable, but instead it seems more opaque. Perhaps the method can inform a cleaner form that I could not see.

LowerTriangularize[
 Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s
] ~ArrayPad~ {{1, 0}, {0, 1}} /. n_?Positive :> elems[[n]]

alternatively:

LowerTriangularize[
 elems[[#]] & /@
 (Range[s^2]~Partition~s - Accumulate@Range[s, 1, -1] + s)
]~ArrayPad~{{1, 0}, {0, 1}}

Here is one I just came up with, and I am quite pleased with it. I think it may be more understandable than most of the ones above.

Take[FoldList[RotateLeft, elems, Range[0, s - 1]] ~LowerTriangularize~ -1, All, s + 1]
added 336 characters in body
Source Link
Mr.Wizard
  • 237
  • 1
  • 6
Loading
added 10 characters in body
Source Link
Mr.Wizard
  • 237
  • 1
  • 6
Loading
added 277 characters in body
Source Link
Mr.Wizard
  • 237
  • 1
  • 6
Loading
Source Link
Mr.Wizard
  • 237
  • 1
  • 6
Loading
default

AltStyle によって変換されたページ (->オリジナル) /