Skip to main content
added 166 characters in body
Source Link
thorimur
  • 9.1k
  • 18
  • 32

Note that f[#1, #2, #3, #4] & is just f—this substitution by itself saves some time—and that Apply (@@@) has a secret levelspec argument! You want {2}, i.e. Apply[f, ATensor, {2}].

Note also that you've accidentally pseudo-0-indexed ATensor with the {i, 0, n} iterator. This means that you lose a row when writing /@ Range[n], since Range[n] is {1, ... , n}, and list indices are likewise 1-indexed (so you lose the last row, not the first). However, you should never have to map over indices like this (well, almost never); all your mapping should be doable with Map and Apply directly, both of which take levelspec args. You may also find functions like TensorTranspose and ArrayReduce (and, in that context, the operator form Apply[f]) helpful. Hope this helps!

Edit, after seeing the edits to the question: turns out f @@@ ATensor[[#]] & /@ Range[n] is slightly faster than Apply[f, ATensor, {2}]! I'm pretty surprised.

Note that f[#1, #2, #3, #4] & is just f—this substitution by itself saves some time—and that Apply (@@@) has a secret levelspec argument! You want {2}, i.e. Apply[f, ATensor, {2}].

Note also that you've accidentally pseudo-0-indexed ATensor with the {i, 0, n} iterator. This means that you lose a row when writing /@ Range[n], since Range[n] is {1, ... , n}, and list indices are likewise 1-indexed (so you lose the last row, not the first). However, you should never have to map over indices like this (well, almost never); all your mapping should be doable with Map and Apply directly, both of which take levelspec args. You may also find functions like TensorTranspose and ArrayReduce (and, in that context, the operator form Apply[f]) helpful. Hope this helps!

Note that f[#1, #2, #3, #4] & is just f—this substitution by itself saves some time—and that Apply (@@@) has a secret levelspec argument! You want {2}, i.e. Apply[f, ATensor, {2}].

Note also that you've accidentally pseudo-0-indexed ATensor with the {i, 0, n} iterator. This means that you lose a row when writing /@ Range[n], since Range[n] is {1, ... , n}, and list indices are likewise 1-indexed (so you lose the last row, not the first). However, you should never have to map over indices like this (well, almost never); all your mapping should be doable with Map and Apply directly, both of which take levelspec args. You may also find functions like TensorTranspose and ArrayReduce (and, in that context, the operator form Apply[f]) helpful. Hope this helps!

Edit, after seeing the edits to the question: turns out f @@@ ATensor[[#]] & /@ Range[n] is slightly faster than Apply[f, ATensor, {2}]! I'm pretty surprised.

added 17 characters in body
Source Link
thorimur
  • 9.1k
  • 18
  • 32

Note that f[#1, #2, #3, #4] & is just f—this substitution by itself saves some time—and that Apply (@@@) has a secret levelspec argument! You want {2}, i.e. Apply[f, ATensor, {2}].

Note also that you've accidentally pseudo-0-indexed ATensor with the {i, 0, n} iterator. This means that you lose a row when writing /@ Range[n], since Range[n] is {1, ... , n}, and list indices are likewise 1-indexed (so you lose the last row, not the first). However, you should never have to map over indices like this (well, almost never); all your mapping should be doable with Map and Apply directly, both of which take levelspec args. You may also find functions like TensorTranspose and ArrayReduce (and, in that context, the operator form Apply[f]) helpful. Hope this helps!

Note that f[#1, #2, #3, #4] & is just f—this substitution by itself saves some time—and that Apply (@@@) has a secret levelspec argument! You want {2}, i.e. Apply[f, ATensor, {2}].

Note also that you've accidentally pseudo-0-indexed ATensor with the {i, 0, n} iterator. This means that you lose a row when writing /@ Range[n], since Range[n] is {1, ... , n}, and list indices are likewise 1-indexed (so you lose the last row, not the first). However, you should never have to map over indices like this (well, almost never); all your mapping should be doable with Map and Apply directly, both of which take levelspec args.

Note that f[#1, #2, #3, #4] & is just f—this substitution by itself saves some time—and that Apply (@@@) has a secret levelspec argument! You want {2}, i.e. Apply[f, ATensor, {2}].

Note also that you've accidentally pseudo-0-indexed ATensor with the {i, 0, n} iterator. This means that you lose a row when writing /@ Range[n], since Range[n] is {1, ... , n}, and list indices are likewise 1-indexed (so you lose the last row, not the first). However, you should never have to map over indices like this (well, almost never); all your mapping should be doable with Map and Apply directly, both of which take levelspec args. You may also find functions like TensorTranspose and ArrayReduce (and, in that context, the operator form Apply[f]) helpful. Hope this helps!

Source Link
thorimur
  • 9.1k
  • 18
  • 32

Note that f[#1, #2, #3, #4] & is just f—this substitution by itself saves some time—and that Apply (@@@) has a secret levelspec argument! You want {2}, i.e. Apply[f, ATensor, {2}].

Note also that you've accidentally pseudo-0-indexed ATensor with the {i, 0, n} iterator. This means that you lose a row when writing /@ Range[n], since Range[n] is {1, ... , n}, and list indices are likewise 1-indexed (so you lose the last row, not the first). However, you should never have to map over indices like this (well, almost never); all your mapping should be doable with Map and Apply directly, both of which take levelspec args.