Friday, April 15, 2016

C++ (programming language): Why do people who come from a physics background


2 Answers
Sam Bancroft
Sam BancroftProgramming for 25 years. From machine code to .NET and beyond.
160 Views
C++ is a GPL (General Purpose Language) whereas R is a statistical (and graphical) language. A (good?) analogy would be the difference between a pick up truck a fork lift, they both have wheels and can carry things but have very different purposes. You could use your forklift to transport a load five miles down the road, but why would you, you have a pick up truck for that. Also you can do a lot more with the pick up truck but, importantly, it cannot lift things.
Elizabeth Porco
Elizabeth PorcoI have a masters in CS, I've done dev, QA, and IT
135 Views
C++,  a building with the outside finished but not the inside, a bunch of of wire of different guages, a bunch of junction boxes, a bunch of outlets for both 120 and 240, and a bunch of breakers and switches.  R, an already wired building with outlets you can plug stuff into.  The finished  building is vary useful for whatever purpose the builder wired it for as R is for statistics on datasets that aren't too big, but can't be wired to do many different things easily the way the unfinished building could be.


C++ (programming language): Why do people who come from a physics background tend to use C++?


I've come across loads of people with a physics background who are really prominent in C++ but not so much other languages. What is it about C++ which makes it so relevant for this discipline?
13 Answers


George Hagstrom
George HagstromI have a Phd in physics from the University of Texas at Austin, the topic of ...
2.6k Views • George has 30+ answers in Physics

This phenomenon that you note is entirely due to performance issues, mixed with the demands of scientific computing. 

I would wager that nearly every single physicist with a Phd has familiarity with at least one slow, but easy to use language, such as python/numpy or matlab. Those who need to run big codes on large machines (which is many many people) must know C/C++ and sometimes need to know Fortran or even CUDA/openCL. It is hard to overemphasize the importance of computation to modern science, and C/C++/Fortran are the engines that drive the biggest simulations.

Akshat Mahajan
Akshat Mahajanwriter on Quora
3.1k Views • Upvoted by George HagstromI have a Phd in physics from the University of Texas at Austin, the topic of my thesis was plasma p… and Alex SergeevPhD in Physics

C++ is fast. It's very useful when you have a load of data to sort through and aren't particularly interested in allowing for latency. CERN generates 50,000 GBs in one run of the LHC - a lot of data you need to sift through in reasonable time. Python, in comparison, is slow, and not suitable for memory-bound operations.

C++ is optimizable. It's close enough to the hardware to allow for remarkably good optimization, and it's explicit memory management means you can have some seriously fast speedups. It's not as low-level as C while not being so high-level that it loses track of what it sets out to achieve.

C++ is popular. Many systems survive on legacy code, built well before Java and Python became all the rage. The first physicists who built those systems used C++, taught it to their pupils, who in turn taught it to there pupils, and so on. When you're a physicist and you're first learning to code, you're much more likely to adopt a language your lab has ample support for.

C++ is everywhere. Many data collection systems just need something slightly higher than assembler. Physicists aren't just involved in coding - they're also busy making the instruments that eventually fly out to space or sit in underground labs. That means you need microprocessor systems, and you need to be able to code those microprocessor systems, and very often those microprocessor systems lend themselves to easy C++ use - Arduinos, for instance, use C++ as their default language. Why stay aloof when your experiment depends on something like this?

That being said, it's not as common as you're making it out to be. I myself know no C++, and most of the theoretical physicists I know are far more comfortable using something like Mathematica, Sage or Python. Experimental physicists tend to be all over the spectrum, using C++ and anything at hand to really get their systems up and running, while those who work with large datasets eventually end up using MATLAB, IDL or C++. A handful of physicists know Java, particularly if they're doing outreach or making mobile applications (really, you'd be amazed how useful a push notification can be if you've got sensitive lab equipment running). Using shell and Linux processes is not uncommon. Particle physicists, in particular, are notorious for using ROOT, a plotting platform that's Linux-based and a pain to use if you don't know what you're doing.

Physicists, like software engineers, are all over the coding and software spectrum. Some are really good at implementing multithreaded processes; others at numerical, high-speed algorithms; others at impressive visualisations; others at collecting data. Sampling bias is probably at fault in giving you this mistaken impression of the C++-only physicist.

Sang Young Noh
Sang Young NohPhD candidiate in Computational Chemistry.
1.7k Views • Sang has 60+ answers in Physics

C++ (and to a lesser extent C) is anything but a friendly language to begin with, and I believe this has reason to do with C++ being pointed out specifically for contention. FORTRAN comes into this too.  

A big reason for why C++ and FORTRAN is still used is because of legacy software. Most of the programs that have been designed for research have their origins in the 1980s/1990s, and the optimal language for developing any program back then were the two mentioned. Back then, how to manage the memory usage in a program was a real concern, and PCs simply weren't powerful enough to bulldoze the calculations.

At least when working on biophysics related calculations, a lot of the data output is simply too large to deal with using higher level languages like python or MATLAB.

One thing you'll notice with a lot of Physics programs is that they still use a lot of C conventions - ** and & notations, malloc rather than new, and printf rather than cout. I remember personally using C++ as a slapon to originally C-programs simply because I found a lot of the C++ terminology turgid and inconvenient, contrary to their original purpose.

Whilst I'm not experienced in this (I wish I was!) C/C++ is also closely linked to systems programming and unix in general, and this tips the edge for physicists to use.

Having said that, I do like Python and R too. Their libraries make things an order of magnitude easier. But for any hardcore number crunching, the C family is still the most reliable one, along with FORTRAN.

Marc Serra
Marc Serra
2k Views • Marc has 30+ answers in Physics

I'm a physicist and I do use C++. Perhaps the main reason why I use C++ is because it was what they taught me in school, probably under the belief that C++ was the only "real" programming language. At that time Python was not yet cool. It was only afterwards that I learned MATLAB. 

At first, I was very, very skeptical about MATLAB. The main reason is that I mistrust black-box algorithms. With C++ I can tell which exact operations led to the results I'm trying to publish. Eventually I started to use it, and it works really well for 95% of the problems. Since it is very tied to double precision math, it cannot solve problems that require extended precision (long double) types. I encounter a couple of these problems a year. In C++ you can also hack your numeric algorithms to use specific information about your problem. This, combined with the native nature of the language and the availability of very good compilers (The Intel compiler is particularly good), makes some C++ numeric algorithms run like 10x faster than their MATLAB counterparts. This is critical for algorithms that take a week to run in C++.

C++ is a handy tool. Once you've developed some libraries (such as wrappers for GNUPLOT and to communicate with instruments through GPIB, serial and USBTMC), you can hack out a code for an experiment literally in the time it takes for LabView to start. Furthermore, proprietary software has licensing issues (you cannot just install copies into all of your computers, so usually a machine ends up tied to an experiment), it's restricted to certain platforms, and likes to close without saving changes when the network connection fails and it loses communication with the licensing server. This is terrible for long experiments.

So for these reasons I use C++ quite often and I support continuing to teach it to physics graduates.

Jacob VanWagoner
Jacob VanWagonerEngineer with a focus on physics. Or am I a physicist with an engineering degree?
2.1k Views • Jacob has 480+ answers and 17 endorsements in Physics

It may just be from your local area.  In my area, they're far more likely to use MATLAB for all their math-related programming, and some of them learn LabVIEW for instrument control.  Only a few of them know C++, but they rarely use it, preferring Python instead.

If you look at old-school physicists (think 1970's) they all used FORTRAN.

John Purcell
John PurcellCreator of CaveOfProgramming.com
1.2k Views • John has 660+ answers in Computer Programming

I took a degree in physics and then learnt C++ .... I still prefer it to other computer languages, but I resort to others because C++ isn't right for everything.

I think it's that C++ gives you a feeling of being close to the machine, and dealing with bits and bytes directly. Physics types like machines, of course. It's more technically demanding than many other languages, which for a person who likes physics isn't a problem. If you're very technically-minded, other languages can somehow feel too "soft" and like they're pandering to you at the expense of efficiency.

If you go a step further and work with assembly language, you're actually doing what almost feels similar to some of the mathematics you'll probably have encountered in a physics degree, e.g. in solid state physics.

I don't think it's necessarily relevant to the discipline though, except that you can process a lot of data quickly with it.

Ladjici Ahmed Amine
Ladjici Ahmed AmineElectrical Engineer
823 Views

It's true that C++ is widely used for people from physic or engineering background. When I was in the engineering school (mid 90) the prominent language was fortran. Fortran is a great languages for scientific calculation, fast and reliable. Fortran is a very conservative language haven't evolved at all from 1977 (FORTRAN 77) to 1995 and compilers were expensive, in the meantime C was gaining more adepts, C++ was viewed as a natural evolution of C with comparable performance, so C++ was rapidly adopted, java was viewed as an evolution of C++. But java was very slow and in engineering performance is an issue,  so we stick with C and C++. The other language that gaining more popularity is python maybe it will be the next C++.

Páll Haraldsson
Páll HaraldssonUsed to program in C++, [never again,] now I would use Julia.
567 Views

It used to be Fortran, now (or both) C++. But the future is Julia language.

To quote George Hagstrom "nearly every single physicist with a Phd has familiarity with at least one slow, but easy to use language, such as python/numpy or matlab. Those who need to run big codes on large machines (which is many many people) must know C/C++ and sometimes need to know Fortran or even CUDA/openCL."

With Julia you can have it all in a single language. Explained by Professor Alan Edelman of MIT in under 4 minutes why Julia is great, such as for physics and math (but I would argue most things):


[Note: he's listed as one of the designers, I guess he was Jeff Bezanson PhD supervisor (another of the 5 original designers (now in hundreds?) or shouldn't I say the main designer/devoloper?), his thesis is on Julia language/why its great, a good read..]

Anonymous
Anonymous
729 Views

C++:
1) It's general purpose. Scientific computing implies not only number crunching, but user interface, pre-processing, and post-processing tasks. You have tons of libraries available for C++ (and C) to do these tasks (numerical and others), thus your program can be linked easily.

2) Faster. Compiled C++ (C and Fortran, too) code is closer to what the machine understands (e.g. int, double must be declared) and thus can be optimized by the compiler. Many Matlab/Python implementations (SciPy, NumPy) wrap around C/C++/Fortran libraries (BLAS, LAPACK). Add also parallelization, MPI and OpenMP only support and interact with C/C++ and Fortran programs.

3) Production quality software. With the latest C++11 standard and powerful template libraries (STL), I found programming quality code in C++ much easier. STL containers (vector, maps, hash known as unordered_map), iterators, I/O streams and strings handling, algorithms, smart pointers, try/catch,  etc. take care of potential user introduced dangers and things like memory leaks, segmentation faults, exceptions, dynamic reallocations. Fortran is great for number crunching, but lacks many of these C++ standard features (many available in boost pre-C++11).
Thus, you don't have to reinvent the wheel as in Fortran.

4) Maintainability: C++ objects, lambdas and Templates make code easier to maintain. Objects adds modularity to the code, whereas using lambdas and templates can reduce the number of lines of codes dramatically.

5) Matlab and Python are easy to learn and could possibly satisfy your needs. But for many applications involving number crunching you need something more powerful (mostly C/C++/Fortran). Like I heard before, Python/Matlab are like a bicycle (easy to learn, takes you to point A to point B with little learning curve), whereas C++ is like a car (steeper learning curve but takes you farther and is faster). I think Fortran is the old vintage powerful car that works and nobody wants to touch, but only maintain as it would be more expensive to redesign it (ain't broke, ain't fix).

Jon Harrop
Jon Harropprogramming for over 34 years.
690 Views

Because they believe C++ is fast for what they consider to be scientific computing. Very few physicists try to pioneer what scientific computing is and, consequently, very few need to learn any other tools.

Alec Cawley
Alec CawleyWrote Pascal full time for over ten years
876 Views

In my case, it is because I went into a engineering type of programming: making stuff happen, connected to real world inputs and outputs. And C++ dominates in that field, both for performance and because it is often the only language available in an embedded environment.

I think that kind of task appeals to physics types, and they do relatively little human interface type programming or data mining, for which C++ is less favoured.

Anonymous
Anonymous
530 Views

I agree with most of the people here I would also like to mention it is easy to write code that is easy to read and have the compiler do the work with things like operator overloading and templates.
For example take adding two 4X4 maticies a and b. you can create a matrix class and overload the + operator and simply do "a + b" where is other langauges you probably have a function to do the adding by taking in the two objects

Todd Clifford
Todd Clifford
362 Views

I initially learned ADA at the UW CS programming classes and I used it for my first semester of Numerical Analysis.  However, after my DS class ended, I didn't have access to ADA anymore and I switched to C.  C++ isn't as good as ADA for doing numerical analysis, but it is much better than fortran, even though Fortran used to be the primary scientific language.  Also, C++ is cheap and easy to acquire and allows for object based programming.

No comments:

Post a Comment