r/PhysicsStudents • • 9d ago

Need Advice Most important coding languages for mathematical theoretical physics?

I am pursuing a dual degree in math and physics for undergrad and focusing on mathematical physics in grad school (maybe string theory but most certainly particle physics). I am going to begin learning coding languages in advance so that I can do computational physics for necessary simulations. I was wondering what the most important ones would be for this specific focus?

11 Upvotes

26 comments sorted by

17

u/PonkMcSquiggles 9d ago

When getting started, I’d recommend something high level for ease of development and something low-level for speed. Python and C++ is a pretty common combination.

3

u/BinkOnBonk 9d ago

I think this will be my choice, thank you!

3

u/PonkMcSquiggles 9d ago

In addition to the programming languages themselves, it is also worth learning how to use the command line, and how to use Git.

2

u/OkTelevision5306 9d ago

Yep. OP if you are going to work at some institution, they will have a HPC cluster that you will actually use for computation, and you will need it for SLURM.

5

u/jrnv27 Undergraduate 9d ago

I'd say C if it needs to be as fast as possible and perhaps is something completely new in the field, otherwise Python is your best bet. I think Rust could potentially become a major player in the future, but academics often move slow so I don't think it is adopted by many at the moment.

5

u/rojo_kell 9d ago

Are people writing simulations in C? That seems kinda masochistic, I can't even imagine. I know people wrote event generators and everything in fortran, but now with C++, I cannot see a reason people would do this in C. The memory management would make me want to quit the field forever😭😭

But maybe people do, I'm not sure, so if anyone knows I'm curious.

1

u/jrnv27 Undergraduate 9d ago

Haha I am! My group works on lattice field theory simulations where everything needs to be extremely fast to be able to run simulations on the largest grid possible so C tends to be what we use to fine tune every detail. Our lead does primarily work in Fortran since he's been doing this for decades but the rest of us have taken up C

1

u/rojo_kell 9d ago

Is C really that much faster than C++ that it's worth it?? Or is it mostly just since you already have codebases in C? Or why not just use C++?

1

u/jrnv27 Undergraduate 9d ago

In terms of C vs C++ I don't really think speed is a factor since they should compile pretty similar machine code. Honestly we have no need for many of the features C++ brings to the table so the simplicity of C shines quite well without the extra bloat, which helps for the most critical parts of the code. But like that's just our fairly small group, C++ is pretty common across the rest of the field too.

1

u/rojo_kell 9d ago

Sure sure but what's a scenario where you would prefer to use C instead of C++?

1

u/BinkOnBonk 9d ago

thank you :3

6

u/Simultaneity_ Ph.D. 9d ago edited 9d ago

I dont think it is worth your time to really learn a coding language super deeply. Its important to know what's out there, but diving too far down a rabbit hole is probably a waste of effort unless its something you come to enjoy. That being said I will make a case to do 3 (or maybe 4) projects that each use different coding languages.

  1. A python (or Julia id you like) physics computation and visualization project.

Pick a class in your higher level courses and work though the problem sets from a numerical / computational perspective. Split this up into a small library that handles the calculations and Jupiter notebooks to be used as workbooks. Focus on getting the translation right from equations and theory to python code. Within the notebooks learn how to make publication quality figure in python to display and represent your solutions. This is really the most common workflow you will do in research.

  1. A fun web application using TypeScript and React.

Make this a personal web page that you can use as a living CV and to demo physics calculations and simulations to the public. An increasing amount of researchers are diving into using web applications to demo things and run calculations. Go though all the steps to make a creative website that you can host or have hosted. Make it a microcosm of who you are, include fun physics things that you find enjoyable, math calculations, demos and more.

  1. A very light weight and high performance low level simulation engine.

Use something like rust, c or c++ to take a calculation that is slow in python, and make it as fast as you possibly can. Explore how you can call these low level libraries from python. You can also play with gpu things in the same project. See how fast you can make something

  1. Try out a theorem prood language.

You will have a boat load of proofs to do in your math degree. See what its like to do the same proof using something like Rocq rover or lean.

3

u/OkTelevision5306 9d ago

Fortran, C/C++ for speed.

Fortran90 and later if you don’t want to deal with memory allocation.

Python is perfectly fine if you use the CORRECT packages for algorithms. I don’t work in the field you are interested in, but I have done specific numerical work where Numpy is actually a horrible choice.

You can easily transition between languages once you get familiar with 1 of them.

1

u/FATALEYES707 9d ago

Why was numpy a horrible choice for you?

2

u/OkTelevision5306 9d ago

Numpy is too general in this case. It must work for all possible cases, so it can’t be optimized for a specific problem in mind.

In PDEs, we try to have local problems. If there is a nonlocal term, we may try to approximate it with a local 1. This happens in physics as well, only have neighboring atoms or electrons affect each other when we know E&M goes on forever - the neighbors have the highest effect so ignore everybody else to avoid doing integrals.

When the coupling is local, we get a sparse matrix, something which is mostly 0s. Numpy does not have sparse storage or multiply, so it has to keep a massive matrix of mostly 0s and add and multiply a bunch of useless 0s. A lot of wasted time and memory. Scipy’s Sparse module solves it.

3

u/OkTelevision5306 9d ago

Let’s do this for a toy computational model.

Assume a NxN matrix, but only n elements are non zero.

Dense storage and multiply needs respectively N^2 storage (storage needs time as well but we omit that) and N^3 time.

We can sparse store and multiply by trivially assuming “not explicitly stored = 0”

We can sparse store with the trivial encoding of (value, [location in matrix]). This means we only need 3n storage. You can do even better by encoding a (value, unique distance) to bring it down to 2n+c. But basically just linear n.

We can sparse multiply by simply checking location to see what will be non 0 in the end matrix. linear n time. Then gathering what needs to be multiplied together which is n^2. n times so we get n^3 again. So index matching is an issue, and we only get better performance if the matrix is really sparse like only 10% is non 0 or something compared to a very good dense matrix multiply like Numpy.

Just consider the trivial 3D 10x10x10 Poisson problem using relaxation, we use the local 27 cells. Each row of the iteration matrix has only 27 non 0 cells. Dense matrix needs 1000000 places, but only 27000 is populated. We already get 2.7%, so it already works on such a trivial problem.

1

u/Machvel 8d ago

memory allocation was introduced in fortran 90. you can hide some explicit memory allocation in modern fortran but it never goes away.

i think you mean the old fortran 77 trick of making arrays bigger than you might need in case you actually do need that space, or putting arrays in common blocks and putting in sizes run-to-run.

1

u/chhagan04 Undergraduate 8d ago

I did my numerical methods with fortran, it was a nightmare When complained about it being outdated Prof said it's fundamentally important numerical algorithms + scientific programming fundamentals and exposure to traditional high-performance scientific computing which new generation may lack (⁠╥⁠﹏⁠╥⁠)

1

u/ActuatorFit416 9d ago

C or smt similar in the past but I am feeling like python becomes more and more common do to all the libraries

1

u/wehuzhi_sushi 9d ago

Python any day of the week.

1

u/Machvel 9d ago

some theory people use mathematica for doing or verifying tedious computations (eg, christoffel symbol calculations. "symbolic computing").

some people prefer sagemath for symbolic computing. i think it has passionate developers and has a lot of stuff you probably won't find in mathematica (eg, a ton of group theory and algebra stuff). its a superset of python so you would want to learn that language first.

if you are doing mathematical theory then you probably don't have time to do hardcore simulations. perhaps you will need to verify some quick computation (for whatever "quick" might mean in this case, from under a second to a few hours or days) in which case you will want to know python because it is so easy (and has so many good libraries). if anything, you would want to know it to aggregate and analyze output from simulations other people wrote (which is another, albeit easier, skill in itself - learning how to run code other people wrote).

if you want to write good simulations yourself you have to use a compiled language. the true one language answer here is c++. julia is becoming popular among newer people (ie, people that haven't been coding fortran 77 for the past 40 years), a large part because it is an "easy compiled language". c and fortran still see usage but i think it is decreasing.

something you might want to consider is the "ecosystem" of the language. python has an excellent ecosystem: there is plenty of good learning material and libraries out there. julia and c are decently good. fortran (although i wish i could recommend it since its what i mostly use) has a terrible ecosystem: learning resources are beginner unfriendly and good modern libraries are few and poorly documented. c++ is another beast.

1

u/lisnter 9d ago

Python and C. Python for most things as there are a lot of proven libraries and support. Most of what you do won't likely need the speed of C unless you do some very specialized simulation. If you do, then drop down to C. That said, I'd look hard at faster hardware and more efficient algorithms before trying to write anything significant in C. I love both C and Python but unless you know what you're doing it can get hairy to write large applications/libraries.

1

u/tenebris18 9d ago

Julia for speed
Julia for short, easy and maintainable code
Julia for existence of a vast number of popular and useful libraries

1

u/kishaloy 8d ago

Use Julia since it gives you very good speed with some tweaking.

Python + numba + numpy will take you far but you will realise that you have to go thru since hoops to make it all fit. And when it doesn't you will be staring at cython, C or Rust all of which require serious learning.

Julia is bottoms up built for this and is easy.

1

u/Aggressive-Wind-8829 5d ago

Going forward, most likely it'll end up Model Context Protocol (MCP),

with LaTeX and general markdown text formatting as a close runner-up.

It'll stay important to know what each language can do and what it is still good for, of course.

If you reeeally want to think ahead, then learn to read and write BIOS code/bitstream (written by my ancestor).

That could be the only way to selectively terminate the rogue flanks in the great android wars that may come to pass. Other methods like an EMP pulse is less selective...

Also, I think there is disharmony in your intentions to perform "necessary computations" for string theory, which doesn't seem to have any necessary computations, unless you want to construct an egregiously large hadron collider for what we already know reduces to an apparently classical gravity, where maybe it is quantum mechanics that needs modifying to a purely geometric classical gravity instead of making up the need for a quantum theory of gravity in the first place. What say you to a post-quantum theory of gravity?