3
$\begingroup$

As a python programmer, numpy tools usually come to my mind if I want to manipulate arrays (list, matrices, ...). Is there any reference (e.g., dictionary) of how to translate numpy syntax to Mathematica syntax? For instance, assuming a is an array, I found the following translations (python code as quotes, Mathematica code as code):

  • linspace / logspace

a = np.linspace(0,10,11)

a = Array[# &, 11, {0, 10}]
(* {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
  • element selection based on condition

a[a>0]

Select[a, # > 0 &]
(* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
  • element selection based on other boolean array

s = a>0

a[s]

s = # > 0 & /@ a
Pick[a, s]
(* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
  • slicing

a[3:-2:2]

a[[4;;-3;;2]]
(* {3, 5, 7} *)

Such a correspondence list is what I am looking for. Does this already exist? If not, I believe it would be helpful to extend this Q&A post to continuously build such a list.

One thing I am trying to use just now is np.roll. Using the example from above, I am searching for a command that yields

EquivalentOfNpRoll[a,2]
(* {9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8}

How would I do this? Permute seems to be an overkill. At least it was far from obvious to me how to implement this rolling permutation in a general but compact way.

Update:

From the comments:

np.roll(a,2)

RotateRight[a, 2]
(* {9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8} *)

a=np.arange(0,11,1)

a = Range[0, 10, 1]
$\endgroup$
  • $\begingroup$ You're looking for RotateRight[] and RotateLeft[] in the last question, I think. $\endgroup$ – Michael E2 Jan 19 '17 at 1:37
  • $\begingroup$ Yes, indeed, RotateRight[] seems to be the equivalent of np.roll(), thanks. I am still interested in a "dictionary" for python/numpy users on the topic of list manipulations. $\endgroup$ – Felix Jan 19 '17 at 1:45
  • 1
    $\begingroup$ Have tried googling? I could be wrong, but I doubt the majority of Mathematica users have Python backgrounds, so I don't think there has been much motivation to create the kind document you are looking for. $\endgroup$ – m_goldberg Jan 19 '17 at 1:52
  • 3
    $\begingroup$ I have just started learning Python. I would be, therefore, interested in such a dictionary, but another way around: from Wolfram to Python. Please keep me informed. $\endgroup$ – Alexei Boulbitch Jan 19 '17 at 8:09
  • 1
    $\begingroup$ @AlexeiBoulbitch Generally speaking, Python sucks. I avoid it as much as I can. Unfortunately, almost anyone I work with who is under 35 and claiming to be a data scientist uses Python... Interestingly, I was inclined to make a dictionary like that for R <-> Mathematica, but I after I learned R to a point, I decided it is not worth the effort. (Granted Mathematica and R are fairly similar within the realm of programming languages.) $\endgroup$ – Anton Antonov Aug 25 '17 at 14:37
3
$\begingroup$

I think that both Python and Mathematica are great programming languages. They provide a lot of built-in 'stuff' to work with as a beginner and allow you to grow along with the language itself. My experience with Mathematica is definitely greater than what I have with Python so I would never claim this answer as definitive.

Having said that, I will have to note that any aspiring programmer that wants to migrate from Python to Mathematica would not be better-off by using a 'cook book' or 'dictionary' approach.

I can understand the appeal in the cook-book approach (I have sought for it myself on different occasions). It provides a quick solution for the (any) problem at hand and it also allows one to build a progressive understanding of the target language.

I don't think there's anything wrong with having 'quick solutions' available per se neither is 'progressively' moving towards a goal a bad thing. The problem I see in using a dictionary to translate from one language to another is, simply, that meaning is something more than syntax. In the context of translating Python to Mathematica, you might get an easy way out but that might not play out for you in the end because of what would be missing.

As a more concrete example I will use the case of Python's (NumPy's) linspace. Scrolling down to the examples,

np.linspace(2.0, 3.0, num=5)

can be translated in Mathematica as Array[# &, 5, {2, 3}]. This is an accurate translation and it's useful for some things but not for others.

Someone using the dictionary, would not know that the output of Array as used, is a mix of rationals and integers. Now, that might not sound as a huge problem for someone wanting to achieve a rough first translation. But it could be a problem for another person.

Also, you could achieve the same result when using Table[i, {i, 2, 3, 0.25}]. Only this time, the results are real numbers.

And there are issues of 'efficiency'. Supposedly, Array is faster than Table and I think that this has to do with the fact that the later uses indexing.

I haven't even scratched the surface of the issue, but that was not my intention. I understand the need for having fast solutions. I don't think that it pays in the long-run to switch from Python to Mathematica that way. I understand it's uncomfortable (I have experienced it and I still experience it with Python), but to me it seems that the best way to migrate to a language is to actually use it.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.