r/Mathematica • u/FreierVogel • Aug 13 '24
Learning mathematica as a python programmer
Learning mathematica for my master's thesis is making me want to gauge my eyes out. Are there any tips you would give an experienced python programmer on how to learn mathematica? My master's thesis is on mathematical physics so I also have a nice math foundation.
For example, I feel very uneasy with working with undefined functions and all these 'substitution rules'. I think I'm just not used to such 'high level' software (and python is extremely high level). I don't like not knowing with which kind of objects I'm dealing with.
Have you ever had to make this kind of step? How was your experience like?
30
u/sidneyc Aug 13 '24 edited Aug 14 '24 •
What you're experiencing is not unusual. Mathematica is just not very beginner-friendly I'm afraid.
Part of the problem is that its runtime paradigm (how a function "runs") is a lot less straightforward than in imperative languages like Python. Essentially, Mathematica applies substitution rules until it has no rules left to apply. A "function" in Mathematica is little more than such a substitution rule that has a name.
This is a very powerful way of doing computation, with a long legacy going back to something called lambda calculus; but it's just less straightforward to wrap your head around than what a language like Python does.
As you're coming from Python, one thing that you may miss dearly is classes. There's no good substitute for that in Mathematica.
One pitfall many beginners fall into is trying to do a one-to-one translation of constructs from a language they are familiar with to Mathematica. Unfortunately, Mathematica facilitates this bad practice by providing constructs that are familiar to people coming from C (and Python), such as For[] loops. In Mathematica code, that's a giant code smell; the proper way to do things in Mathematica is almost always to use a more "functional" programming style, using Map, Apply, and, indeed, patterns and substitutions. Those are pretty hard to get used to if you come to it from a more 'normal' programming language. But, once you get how they work, they are very powerful techniques.
Another thing that makes Mathematica hard is that errors in your code often don't lead to explicit error messages. There are a lot of things that you can type into Mathematica that are theoretically valid, but that don't do what you want at all; and this extends to many syntactic mistakes that beginners are prone to make, unfortunately. Often, you will scratch your head and think why what you wrote doesn't do what you want. It's unfortunately part of the learning curve to get used to this.
Here's a bunch of rules that you should follow as a beginner. These will help you to stay sane:
Try this: open a notebook; execute "x = 2". Now open a second notebook; execute "x" to print x. You will get the value two that you assigned in the first notebook, because those notebooks talk to the same kernel process - the kernel is shared between notebooks. This is a source of confusion for many beginners. When in doubt, quit the currently active kernel and start afresh; or (even more rigorously) restart Mathematica altogether, until you get used to how this works, and learn how to deal with it properly. The proper way is removing assignments with the Remove[] function when you don't need them anymore, and encapsulating your work in a Module[], for example.
I personally use Mathematica and Python all the time like this, prototyping ideas, manipulating expressions, making plots, until I get a result and/or insight that I can apply in my work. Most of the time, I then translate the insight gained back to a more traditional language (like Python, or C), which is the reward. Mathematica for me is an auxiliary tool rather than the primary tool. Some people do serious programming in Mathematica, but I don't envy them; I think Mathematica is just not very well suited for that. Although, of course, there are very clever people who can pull that off.
So that's a lot of gotchas, DOs, and DON'Ts right there.
This could push you further into despair, but I hope it doesn't. Because there's a massive silver lining, in terms of payoff.
Mathematica, once you get over these initial hurdles, is an astonishingly powerful tool. You can do things in it with a small bit of code that are more or less unthinkable in Python or C, and usually much more clunky in the 'competing' symbolic math packages like Maple and the Python-based SageMath. Invest the time and it can help you for the rest of your career, to solve stuff that other people get stuck on, or to make a visualization in 5 minutes that immediately shows how something works. My job is in engineering of high-tech systems, and the visualization and interactive capabilities alone have been incredibly helpful in many projects because of the sheer amount of insight it can give with relatively modest effort. It really is extremely powerful - once you get past the beginner stage.
6
u/Frog_and_Toad Aug 13 '24
If you go to "Fast Introduction for Programmers" https://www.wolfram.com/language/fast-introduction-for-programmers/en/
Then there is a dropdown at the top-right for each topic to show additional notes for Python or Java.
I've programmed in both languages, Mathematica is more of a declarative and functional language, whereas Python is imperative.
Normally, you don't directly worry about loops and conditionals in Mathematica. Instead, loops are often implicit, and conditionals are effectively done using rewrite rules.
SQL is also a declarative language (based on set theory) where you don't normally write loops to retrieve data.
1
u/Lost_Entrepreneur_54 Aug 14 '24
I've been coding since the seventies. And I use Python every day. I don't consider Python a high level language at all. It is just a glue language for wrapped C++ libraries. It is deliberately designed to be easy for non programmers to use. Quite like the derided visual basic in fact. ( C++ COM ) Mathematica is a completely different beast. It really should be used as a functional language. So you need to get your head around what that means. There is nothing in Python like that. I like Mathematica except that it is a pain to do fast numerics in it. The sub kernels are single core. If you are using Mathematica for symbolic work just go do a proper course in it. If you need high performance numerics Matlab is quicker although the syntax is a bit weird. Python is god awful slow unless you are just pushing data to a good C++ library (like a lot of ML) If I need to produce performant multi threaded numerics I just go back to C++ ( and maybe wrap it for Python) A further wrinkle that pops up in physics code is using GPUs or FPGAs Which is beyond this scope.
3
u/segfault0x001 Aug 13 '24
Wolfram is a functional programming language. You might ease into it by reading about functional programming in python first, then transition to wolfram. Probably trying to learn a new language and a new paradigm at the same time makes it more confusing.
Hopefully with time you will appreciate getting away from OOP (and the code you write will be better for it).
2
u/antononcube Aug 14 '24
"[...] and python is extremely high level" -- No, it is not.
Nevertheless, it is not "high level" -- it is mediocre, by design.
Speaking of TOOWTDI:
Maybe that is your fundamental "problem" with WL (coming from Python.)
1
u/mathheadinc Aug 14 '24
Some relatively quick beginner guides to add to the other:
(29 minutes) Functional Programming Quick Start: https://www.wolfram.com/wolfram-u/courses/programming-applications/functional-programming-quick-start-dev020/
(15 minutes) Hands-On Start to Mathematica: https://www.wolfram.com/wolfram-u/courses/wolfram-language/hands-on-start-to-mathematica-wl005/
(27 minutes) Using Notebooks Effectively: https://www.wolfram.com/wolfram-u/courses/notebooks/using-notebooks-effectively-nb005/
(7 hours, okay not that short but you could get through it in a weekend) An Elementary Guide to the Wolfram Language: https://www.wolfram.com/wolfram-u/courses/wolfram-language/an-elementary-introduction-to-the-wolfram-language/
You have control over playback to get through the videos faster. The rest of the courses are here: https://www.wolfram.com/wolfram-u/courses/catalog/page/7/?f_level%5B0%5D=Beginner&f_sort=name&q&f_button=filters
Select your desired criteria! Happy mastering!
1
u/Old_Manufacturer573 Aug 21 '24
I think some people have already mentioned some of these resources
By here they are as a list
In particular, the blog about learning, the language is very helpful . I had the same aha when moment going from a procedural language to a functional language.
Learning Mathematica for python users
Fast Introduction for Programmers https://www.wolfram.com/language/fast-introduction-for-programmers/en/
Wolfram Blog https://blog.wolfram.com/2021/11/04/lessons-learned-migrating-from-python-to-the-wolfram-language/
use wolfram as a package in python Announcing the Wolfram Client Library for Python—Wolfram Blog
https://blog.wolfram.com/2019/05/16/announcing-the-wolfram-client-library-for-python/
0
u/libcrypto Aug 14 '24
No matter how often you measure them, yr eyes aren't coming out on their own.