Hacker News new | ask | show | jobs
by rayxi271828 894 days ago
Ask HN: I've never met someone who uses Mathematica, I imagine its users are even rarer outside the academic circles. I've met many who use Matlab, R, Python, Excel, etc.

If you're using it, what are you using it for exactly? In what way is it irreplaceable by other tools out there, if at all?

22 comments

I do.

I started using Mathematica in middle school and continued from there. My initial use case was simply double-checking I did my math homework correctly. A lot of Solve, DSolve, FindInstance, Reduce, FullSimplify, etc. I did a lot of plotting to visualize things: not just plotting functions of one variable, but parametric curves, inequalities, functions of multiple variables. When I studied linear algebra, I implemented Gaussian elimination myself as a learning exercise and I was very proud of it: the nice thing was that although the algorithm worked on matrices containing known numbers, it automatically worked for matrices containing unknowns thanks to its symbolic computation. When I studied basic image processing tasks like edge detection or the like, it was again of great help. When I got into personal investing, I did yet more calculations using the FinancialData function to retrieve financial time series and backtested many kinds of portfolio. When I got into trading options, it was of tremendous help to learn options from first principles, starting from the log-normal distributions, implementing Black–Scholes modeling, and then implemented the option greeks (delta, gamma, theta, etc) from scratch. Even as a regular software engineer, when I needed to work on algorithms, Mathematica is great help when I needed to do complexities analysis more sophisticated than interview-level big-O notations. I even used it as a SAT solver in a pinch, or a linear programming solver, when I knew there are other tools, but they won't be as nice as Mathematica or have higher learning curves than Mathematica's builtin documentation.

Wolfram would like to say that he invented Mathematica all by himself, but in its core, it is basically a lisp: everything is an expression, and mathematics are just transformations of the expressions. Afaik this makes it the best tool (conceptually and practically) for generic symbolic manipulations. For example, `1 + 1` in Mathematica is just syntactic sugar for `Plus[1, 1]`, and `a = 1` is `Set[a, 1]`.

I am a PhD student in theoretical physics and almost everyone in our field has no choice but to use it (I do know one or two people that use maple, but the overwhelming majority chooses mathematica).

Despite its elegant design, many people hate it with a passion, as it has grown to be a huge bloated mess that takes forever to run. Also, due to the closed source nature, it is very hard to debug when something goes wrong. For example, it is quite often for the basic functions like `Simplify` and `Integrate` to get stuck running forever, but there is no way to keep track of the internal transformations that mathematica is doing, since everything is sealed up.

Re "it is a lisp" and "everything is an expression", I would like to add a bit of clarification.Or, given that you use Mathematica regularly while I was just reading surface docs (for purposes of doing some stuff with Wolfram Alpha), rather a question if my perspective is well-founded.

Based on my understanding of how expression evaluation works, the slightly more revealing statements would be "it is a lot of lisp macros" and "everything is an s-expression". Which means, a big mess. Let me expand:

As a functional programmer, "everything is an expression" sounds comforting, and I would expect there are clear transformation rules on how expressions are evaluated (and, maaybe, type signatures).

Instead, what you get is, "you can throw in some random form of expressions into this function, and it will do something with them". As in, it takes an AST input, and transforms them in some loosely specified way. There doesn't seem to be a type system, so you don't have types to guide you, rather you likely need to figure what kind of expressions work with which functions.

Now, if I'm wrong about this, and the functions behave consistently in what they take and how they transform it, then I'm more than open to be corrected. It is just that my high expectations (based on marketing of the lang) and the subsequent realization left me a bit bitter.

It's really, really difficult to come up with a type system for mathematics. Let's just talk about Plus, the symbol for using the plus sign. What's its type? You might say it takes a few numbers and returns a new number. But what kind of number does it return? It is capable of returning machine precision numbers or their custom high precision numbers. It can return integers, rational numbers, real numbers or complex numbers, as the case may be. It is capable of working on lists of numbers and matrices of numbers, and it returns lists of numbers or matrices of numbers. But wait Mathematica doesn't require a list's elements' types to be homogeneous, so it can return different types of numbers for each element of the returned list. It is capable of working on completely undefined symbols, much like in real mathematics you expect a teenager to be able to reason about the expression `x+x+x` and simplify it to `3x` without knowing what `x` might be. It could very well leave everything the same, for example when you add two undefined symbols `x+y` and get back `x+y`.

So I personally think it is perhaps not productive to think about type systems and type signatures when working with Mathematica. But you can definitively think in terms of transformation rules. And Mathematica either documents these rules or makes these rules intuitive.

It is possible to put filters on function arguments, e.g., the definition

    f[x_Integer] := ...
will define a rule for f[] that only matches expressions where the argument to f[] has the head "Integer". It is even possible to use arbitrary predicates:

    vec3Q[v_] := VectorQ[v, NumberQ]&&Length[v]==3
    f[v_?vec3Q] := ...
This lets you sort-of have type-checking. This is entirely opt-in, so you have to be somewhat rigorous about its use or it does not do any good. Also, in practice if any invocation of f[] does not have arguments which match the types for which you have defined it, the expression just remains unevaluated, which can create a mess (but maybe less of a mess than evaluating the function on input of the wrong form). The performance impact (particularly of the predicate version) is also non-zero, but my experience is that the biggest performance limitations come from trying to keep your machine from grinding to a halt when a runaway expression applied to the wrong thing explodes in complexity and eats all of your RAM... and this helps avoid that.

While I have found this to be very helpful for writing and debugging hairy expressions, I used Mathematica for years before I even knew this was a thing. In reality almost no one does this, certainly not with any consistency, and the situation is as bad as you fear it would be.

The language is a term rewriting language. https://reference.wolfram.com/language/tutorial/Evaluation.h... covers most of the evaluation process. The documentation for functions lists out the different forms they expect.

Lisp-style macros are actually difficult to write because of the infinite evaluation of the language. I was able to write a quasiquote package for myself to help with that that though.

These are all valid criticisms. There is no type system, although some safeguards can be implemented through pattern matching and conditions (see the answer by @derf_ above). For quick and dirty transforms on symbolic math expressions, these are often good enough, but it is indeed a mess to use as a full fledge programming language.

I do like that the lispy language itself closely mirrors math expressions, and it is consistently accessible throughout the user interface. For example, the mathematica notebook frontend (IDE) is simply some `MakeBoxes[]` of the expressions, which are all valid mathematica code themselves. I tried sympy a while ago, which I believe took an object-oriented approach, and it was very clumsy when compared to mathematica.

Still, I would not recommend using mathematica for general programming, precisely because of the mentioned shortcomings. By default, it is also impure and not lazy (eager eval, although it can be forced to be lazy on a case by case basis using `Hold` or `Unevaluated`).

> I am a PhD student in theoretical physics and almost everyone in our field has no choice but to use it

I got my theoretical physics Ph.D. in 1987. I’ve never met anyone who uses Mathematica, except to try it out a bit out of curiosity. Perhaps you’re referring to a particular research group?

Ah, to be more precise, I work in high energy theory (hep-th): https://arxiv.org/archive/hep-th. I shouldn't have spoken for theoretical physics in general, but in hep-th, mathematica is basically a requirement (nowadays, can't say for 1987). I believe the same applies to high energy phenomenology (hep-ph) in which some fundamental packages (e.g. feyncalc) are written in mathematica.
That’s not one of the areas I’ve worked in, so that’s interesting to know.
Background: I did a PhD in computational physics.

Starting as an undergrad, I extensively used mathematica to help or double check homework problems, plotting functions etc. For more "numerical" type of work, we extensively used matlab, so typically we used mathematica for more symbolical type problems. Later on, when working in physics, I often used mathematica, again mostly for doing things like symbolical integration, or things like quickly calculating symbolical gradients that I could copy-paste into some numerical software etc.

I no longer work in academia so I don't have access to a mathematica license, but similar free tools are Sympy, Maxima, which are good for basic stuff but in my experience are not nearly as good as Mathematica for more complicated stuff. Or just the online wolframalpha.

I use it for hobby tinkering, it's quite fun to play around with. Excellent documentation, best-in-class symbolic capabilities, great visualizations/charting, everything you need is in the box (no fussing with dependencies). And once you get the hang of it, you feel very crafty doing complex functional-style transformations with minimal code.

For example, my house experienced some flooding last year after exceptionally heavy rainfall. But how exceptional was it, really? I pulled out Mathematica and in a few minutes I had an interactive chart showing historical rainfall stats for my city over different time periods. The charting, interactivity, and weather APIs were all just built in.

Can you post a code snippet for that plot, if you still happen to have it?
I don't have that one handy right this minute, but I just remembered that I did a different weather exploration with Mathematica about a year ago.

I had a hunch that Ironman triathlon was advertising their races as having cooler weather than they actually do. Turns out I was right -- here's my [slowtwitch post](https://forum.slowtwitch.com/forum/Slowtwitch_Forums_C1/Tria...), and the [associated code](https://gist.github.com/latkin/470a2f06056ee0a8e3f4da837af10...).

AFAIK physicists use it very heavily, but I barely know any mathematicians use it. To me this is a great tool for people who use Math heavily as a tool but not study Math itself.

I use it for symbolic calculation, solve differential equations, and many complicated integrals, and its visualizaion build upon those with easy parametrize support is very nice. Starting from my ungrad sophomore year as physics major, we have courses require us to finish some homeworks with Mathematica.

I can hardly find any other tool to replace mathematica in terms of symbolic calculation and doing complicated integrals (there is a joke by calling mathematica "large-scale integral table")

Agree, Wolfram Alpha is heavily used by some students of physics to do their homework. We sometimes joked that was the main purpose of the service.

Mathematicians probably have trust issues and use tools with a code base that is 3 orders of magnitude smaller.

I've seen some people use it like you would use Matlab, R or Python (I wouldn't recommend it...), but it can (mostly) do symbolic stuff quite easily. I've seen it used in maths and physics, mostly by theorists. If you've made it part of your workflow, I suspect it's irreplaceable, but the best way to think about it is its one of those tools that gets used because the topic is small/bespoke enough that it's hard to build a replacement without having all the existing features (there a number of these in physics/engineering).

Its biggest flaw is how much it wants to act as a black box, which means when something goes wrong, or isn't exactly what you want, you spend more time trying to fix it than solving the original problem.

I'm a software developer and use it for one-off tasks like image processing (create an SVG from a folder of images), quick visualization of data (read a giant JSON file and create bar charts from certain keys), file preparation, and much more. It's taken me several years to really feel like a power user, where I intuitively know which functions to use and how to compose/customize them. When I worked at Wolfram Research (over a decade ago), I even made a proof-of-concept for programming Arduino microcontrollers and controlling them directly from the notebook interface.

It's a powerful tool with a steep learning curve - hopefully the LLM assistant will help with this.

Its "batteries included" philosophy (well, more like the entire power plant) – for example, very convenient and broad visualization and declarative UI libraries; very good online documentation as well; of course, the OG interactive notebook interface, too.

If the cost of that is shining Stephen Wolfram's dome, well, what can you do.

I used to use it when I was at University and I kept a copy around for nostalgic reasons. Once in a blue moon I get an excuse to use it for work related reasons, and then I gleefully spin it up just so I can pretend to be a real mathematician / scientist type person. Generally this is superfluous, but I do it anyway, otherwise my tertiary education feels like a waste of time and money.

Other times I use it as a replacement for a calculator app, which feels exactly like cracking a walnut with a 500-ton industrial press.

It does have some reasonably unique capabilities that I did use more heavily in the past. E.g.:

- Simplifying the vector/matrix mathematics used in 3D graphics. It can eliminate redundant expressions, which is especially useful when you know that some of the inputs are constants such as 0 or 1.

- Non-linear curve fitting. If you have some complicated mathematical model you want to fit to noisy data, Mathematica will "just do it". With every other tool out there, this is... sss... hard.

- It missed the AI boat, but it has mostly caught up and could now be a viable alternative to the Python-based AI ecosystem, especially for certain areas of research.

- Complicated plotting requirements where I just can't be bothered spinning up some dedicated log analytics "tool" or subscribing to a "cloud service" and learning an entire query language just to draw a 3D histogram or whatever.

Imagine jupyter notebooks with a nice lispish language and the most complete standard library ever developed. Haven’t used it for almost 15 years, it was great back then, nowadays when I need something more than a simple calculator I go to wolframalpha - it’s basically Mathematica, but with one line of input instead of a notebook.
It's noteworthy that Mathematica invented the notebook UI that Jupyter ended up popularizing.

It has some strengths, but since its syntax highlighting is coupled to the kernel state it didn't have an undo function for the longest time. Also as big vim fan it's disappointing to not being able to use your favorite editor.

Emacs has EIN which allows you to edit and run Jupyter Notebooks. Combining that with a vim keybindings mode like Evil, you can use vim bindings on notebooks.

Edit: The github page for EIN says that development has stopped. Despite this, I was able to edit a notebook with only minor inconveniences very recently.

An actively developed alternative is emacs-jupyter, which allows you to use an org file similarly to a notebook.
No. Incorrect. There was a precursor that had the idea Of notebook but didn’t call it that. By your logic Wolfram invented symbolic computation, Computational complexity and many other things. Let’s Not go down that route please.
Mathcad has the notebook metaphor (calculations embedded in live formatted documents) by 1986. Mathcad predates Mathematica by 1 year. [1]

[1] Mathcad 2.0 Ad from 1987, the oldest I have found in 10 min. https://books.google.es/books?id=sc4TnHAYBSUC&pg=PA42

Thanks for looking into it. Yes this is correct. Knowing what I hear about wolfram, even if there is a record in history wolfram claims He is the first.
I may have been incorrect because it's hard to know about everything and I have no issue to stand corrected, but please do not attack my logic by building strawmen. You could have simply stated the name of that predecessor, ideally with a link.

The basis my comment for this was this thread: https://news.ycombinator.com/item?id=22278637

Unfortunately the Atlantic article is now paywalled.

Archive from 20220805:

https://archive.is/4l509

Don’t trust the PR coming out of Wolfram. His wiki entry was paid for, his puppets on the net do the rest propogating disinformation And misinformation. He has connection to writers from New York Times and others.
I used a bit of Mathematica but settled on Waterloo Maple because it had a cheaper academic license. Mathematica has stronger algorithms than Maple (like cylindrical algebraic decomposition), but for what I was using it for, they were both equally capable.

I was working on mathematical models (large scale optimization). These are usually solved numerically, and in numerical mathematics, how you write an equation matters tremendously (for instance, the equality x/y = z is much worse than x = y * z for solvers especially if y is a variable that can take on 0 as a value because during iteration this might create a lot of NaNs in your Jacobian or Hessian matrices).

I was using symbolic math to find better (but mathematically equivalent) ways to pose equations that would be numerically expedient. One example is using Groebner bases to do the equivalent of Gaussian elimination on a system of polynomial, which produces a row-echelon form and has many nice properties.

It’s certainly the best of the symbolic computational tools that I’ve tried. However, my problem with such tools is that that they work great for simple examples in my experience but scaling them to work with non-trivial systems is rough. It may be user error on my part, but the path between simple and complex is non-obvious.
I wonder if LLMs will provide a robust path from the solid symbolic computation of Mathematica to production or productionizable code. I've seen stories show up on HN about using LLMs to e.g. transform Cobol -> Java, and have my suspicions about how and where that kind of translation could fall down with today's LLMs.

Nevertheless, I assume LLM enabled translators will improve rapidly, and that a product like Mathematica could be very well suited for translating from a prototype to a robust implementation for e.g. HPC.

It's a great tool when you are a jack of all trades, master of none, though oftentimes better than master of one. You can use to decode phase-modulated signals, calculate stress distribution in some mechanical part, get an idea of how the orbit of a particular asteroid will look two hundred years for now, or just make a diagram of a set of events, among many, many other things.
I know that Citibank's foreign exchange market making desk was using Mathematica, at least they were 15 years ago.

It's unusual in the quant world though. I think they had hired a bunch of PhDs who had spent too much time in academia.

Find it pretty helpful as a math-adjacent academic. It's great at coming up with counterexamples to inequalities with FindInstance. I don't know how irreplaceable it is, but in general I find the UI for manipulating symbolic equations nicer to use than anything else I've tried.
The MIT Mystery Hunt starts today, and Mathematica is my go-to language and environment for puzzle hunts. As the saying goes, it's the second-best tool for everything. Fast iteration on ill-specified problems, trivial visualisation and interactivity and so on, an unparalleled range of built-ins to perform extremely complex tasks, building up huge blobs of personal state that you're going to throw away entirely in an hour.
I used it a lot in maths grad school for manipulating wretchedly large algebraic expressions. Just maths notation being well-supported and the interface for editing everything being nice made it the best tool for me. And it wasn't hard to use, not at all - bearing in mind I was just doing algebra. (This was all more than ten years ago).
It is a very integrated environment with access to lots of mathematical tools. It is just a very nice and polished tool. I often just start it up to use it as a calculator!
I love Mathematica and I use it a lot. It's like what happens if you take Matlab, R, or Python (ie any programming language primarily used for math), and turn it into a real functional programming language. It's not irreplaceable at all, but it's a lot nicer than the alternatives.

It's generally pretty nice for any sort of mathematical programming, from designing control systems to statistics to simple graphing. It's also a pretty good language for basic scripting and data manipulation. Most of the mathematical work on my blog is done in Mathematica.

You could replace the symbolic solving capabilities with wxmaxima[1] (which is free and opensource) and you would also find the linear algebra is waaay faster than mathematica, but the downside is maxima is weird and a bit user-hostile and its visualisation capabilities are kinda janky by comparison to mathematica which produces really nice visualisations.

[1] https://wxmaxima-developers.github.io/wxmaxima/download.html

Symbolic computation. Yeah, I guess you can do it in SymPy, but it's more painful.

It's a really pleasant environment for certain type of work.

The only thing missing is better type systems.

I’m big on types and am sympathetic to your last point, but wouldn’t introducing types in any nontrivial way break compatibility on a catastrophic scale?
I mean it can be gradual. There already are ways of compiling functions which require type annotations, just make those less of a pain in the ass.
How does it compare to Lisp for symbolic programming?
When people speak of "symbolic computation" in the context of mathematica, it's usually not about the Mathematica programming language itself, but rather about using Mathematica to do symbolical mathematics. A bit like how you did math with pen and paper in high school or university, except having Mathematica do all the hard stuff.
A lot of Lisp constructs like map and apply and macros have dedicated syntax in Mathematica, so they feel more like a fluent language. And the standard library is very large and impressively self-consistent. The default format is notebooks which helps make your work presentable.
My understanding (from people who properly learnt Mathematica, and understood the language) is it is a lisp, but it's never taught that way, never explained, you're just searching for the magical function that does the thing you want.
Mathematica is basically the M-Expression version of lisp that never developed. The real power isn't just in the symbolic capabilities but the mathematical library.
Lisp is at its core an evaluator for expressions. The routine for that is called EVAL.

Mathematica is a computer algebra system at its core and is a rule-based rewrite system for expressions.

An example in Lisp notation:

In a computer algebra system (CAS) one may enter 5a - 2a

  > (- (* 5 a) (* 2 a))
The CAS would answer with:

  (* 3 a)
It has used rules to simplify the expression and uses some default form. It could have printed a + a + a or 3 * a. It sees that it can't further simplify it, because a has no value and thus returns this simplified expression as it is.

In Lisp things are differently. It takes an expression and tries to compute a value:

   > (- (* 5 a) (* 2 a))
The result in Lisp is "Error: unbound variable a". It can't compute a value, because during evaluation it sees that the variable a has no value. Evaluation of the unbound variable a is an error.

Now you could write an expression simplifier in Lisp: let's call it simplify. Lisp has a quote operator, which returns the embedded thing as it is -> it is not evaluated. We can embed an expression inside quote and thus call simplify with that unevaluated expression as an argument.

   > (simplify (quote (- (* 5 a) (* 2 a))))
The result then could be

   (* 3 a)
One then could write a input loop in Lisp

   (loop (print (simplify (read)))
which then would not be a read-eval-print-loop, but a read-simplify-print-loop.

   (defun read-simplify-print-loop ()
     (loop (print (simplify (read))))
This interactive loop would read expressions and print simplified expressions...

Actually something like that has been done with computer algebra systems written in Lisp, like Macsyma/Maxima and Reduce. But they also then switched to infix syntax for input/output to make it easier for humans to enter mathematical expressions.

Peter Norvig gave in his book "Paradigms of AI Programming, Case Studies in Common Lisp" extensive examples how to implement such a thing in Lisp:

https://github.com/norvig/paip-lisp/blob/main/docs/chapter8....

and

https://github.com/norvig/paip-lisp/blob/main/docs/chapter15...

The advantage of the Mathematica language compared to Lisp is that it can compute with expressions via rules out of the box. Additionally Mathematica is so much more than that: it is an environment, a collection of mathematical knowledge, a cloud service, a specific product on can buy/rent, ...

The drawback is that the semantics are murky and Mathematica is a two-language system: the fast internal code (and much of the environment) is written in C++ and the expressive language is on top.

Lisp OTOH is often much more efficiently compiled with clear(er) semantics.

> In a computer algebra system (CAS) one may enter 5a - 2a

> > (- (* 5 a) (* 2 a))

> The CAS would answer with:

> (* 2 a)

Not sure I'd want to use such a CAS. Hint: 5-2 != 2. ;-)

Thanks, edited!
For solving math symbolically when Sympy and Maxima fail. I don't like it at all though.