Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

(Apologies for the long question title.)

One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can be confronted with the sudden realisation that there was an alternative syntax to do the same thing. There seem to be a number of less-well-known parts of the language hiding in plain sight as additional arguments to some very well known functions. Consider the following examples:

  1. Total: Total[somematrix, {2}] is equivalent to
    Total /@ somematrix
  2. Partition: Divide @@@ Partition[somevector, 2, 1] is equivalent to
    Most[somevector]/Rest[somevector]
  3. Join: Join[somematrixwith10rows, Array[f, {10, 1}], 2] is equivalent to some complicated thing involving Append or Transpose to add a column at the end.

I am sure there are others.

The common thread between all three of the first example in each pair above is an additional argument that is often not well known, at least by me.

Are there reasons for preferring one style over the other? It has been suggested to mebeen suggested to me that at least in the first example, the additional-argument version doesn't unpack and therefore saves memory over the case using Map. But are there other reasons, for example the ability to Compile, or other efficiency improvements?

If it all down to personal taste and knowledge, I worry that it makes it hard to understand and learn from other people's code. And if there are best practices to follow, it would be good to make them more widely known.

(Apologies for the long question title.)

One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can be confronted with the sudden realisation that there was an alternative syntax to do the same thing. There seem to be a number of less-well-known parts of the language hiding in plain sight as additional arguments to some very well known functions. Consider the following examples:

  1. Total: Total[somematrix, {2}] is equivalent to
    Total /@ somematrix
  2. Partition: Divide @@@ Partition[somevector, 2, 1] is equivalent to
    Most[somevector]/Rest[somevector]
  3. Join: Join[somematrixwith10rows, Array[f, {10, 1}], 2] is equivalent to some complicated thing involving Append or Transpose to add a column at the end.

I am sure there are others.

The common thread between all three of the first example in each pair above is an additional argument that is often not well known, at least by me.

Are there reasons for preferring one style over the other? It has been suggested to me that at least in the first example, the additional-argument version doesn't unpack and therefore saves memory over the case using Map. But are there other reasons, for example the ability to Compile, or other efficiency improvements?

If it all down to personal taste and knowledge, I worry that it makes it hard to understand and learn from other people's code. And if there are best practices to follow, it would be good to make them more widely known.

(Apologies for the long question title.)

One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can be confronted with the sudden realisation that there was an alternative syntax to do the same thing. There seem to be a number of less-well-known parts of the language hiding in plain sight as additional arguments to some very well known functions. Consider the following examples:

  1. Total: Total[somematrix, {2}] is equivalent to
    Total /@ somematrix
  2. Partition: Divide @@@ Partition[somevector, 2, 1] is equivalent to
    Most[somevector]/Rest[somevector]
  3. Join: Join[somematrixwith10rows, Array[f, {10, 1}], 2] is equivalent to some complicated thing involving Append or Transpose to add a column at the end.

I am sure there are others.

The common thread between all three of the first example in each pair above is an additional argument that is often not well known, at least by me.

Are there reasons for preferring one style over the other? It has been suggested to me that at least in the first example, the additional-argument version doesn't unpack and therefore saves memory over the case using Map. But are there other reasons, for example the ability to Compile, or other efficiency improvements?

If it all down to personal taste and knowledge, I worry that it makes it hard to understand and learn from other people's code. And if there are best practices to follow, it would be good to make them more widely known.

Tweeted twitter.com/#!/StackMma/status/178043327558594560
Source Link
Verbeia
  • 34.5k
  • 10
  • 110
  • 225

Are there advantages to using additional arguments of common functions rather than alternative ways of calculating with lists?

(Apologies for the long question title.)

One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can be confronted with the sudden realisation that there was an alternative syntax to do the same thing. There seem to be a number of less-well-known parts of the language hiding in plain sight as additional arguments to some very well known functions. Consider the following examples:

  1. Total: Total[somematrix, {2}] is equivalent to
    Total /@ somematrix
  2. Partition: Divide @@@ Partition[somevector, 2, 1] is equivalent to
    Most[somevector]/Rest[somevector]
  3. Join: Join[somematrixwith10rows, Array[f, {10, 1}], 2] is equivalent to some complicated thing involving Append or Transpose to add a column at the end.

I am sure there are others.

The common thread between all three of the first example in each pair above is an additional argument that is often not well known, at least by me.

Are there reasons for preferring one style over the other? It has been suggested to me that at least in the first example, the additional-argument version doesn't unpack and therefore saves memory over the case using Map. But are there other reasons, for example the ability to Compile, or other efficiency improvements?

If it all down to personal taste and knowledge, I worry that it makes it hard to understand and learn from other people's code. And if there are best practices to follow, it would be good to make them more widely known.