25
$\begingroup$

Going to teach students of undergraduate level a course titled Introduction to Computer Programming. I am confused a bit. In Computational Physics scientists use C/C++ or Python or Fortran,CUDA etc..... this is time to build their base. What should I use? I know you can learn new programming language anytime in your life but which is wiser choice for me to elaborate them all basic programming concepts and OOP concepts later on.

$\endgroup$
8
  • 9
    $\begingroup$ @k20: I hope your suggestion was tongue-in-cheek; otherwise it'd be a serious breach of academic ethics. $\endgroup$ Mar 9, 2014 at 10:38
  • 6
    $\begingroup$ @k20: All this is off-topic, but choosing software based on kickbacks (of any kind) to the teacher and not on the content to be taught is definitely unethical. (Just to make it clear, it's the "swag" I object to.) What is usually done is that companies make available software at significantly reduced cost (or for free). $\endgroup$ Mar 10, 2014 at 17:10
  • 3
    $\begingroup$ @k20: Also, keep in mind that the software company’s motivation is not entirely altruistic: Your students may get the software for free now, but it’s also likely that they have to buy the software some time later in their life (or learn a new software). $\endgroup$
    – Wrzlprmft
    Mar 11, 2014 at 22:13
  • 3
    $\begingroup$ @k20 Matlab and Mathematica aren't really used much for serious scientific computation. They are more exploratory tools for trying out ideas. If the purpose of the course is to teach basic algorithms, then these might be suitable (especially Matlab), but if it is general programming, then you want to go with a more general programming language like Python of C++. $\endgroup$ Mar 13, 2014 at 16:47
  • 2
    $\begingroup$ MATLAB has a design (if I was not diplomatic I would say flawed :) ) that requires what in other languages are bad practices. Many other things are just different from anything else, so "going out" can be difficult. Mathematica has, in my opinion, a beautiful design, but it is nothing similar to any other language (except perhaps Lisp, but it is not used in science), so what you learned is mostly useless to learn another language. OTOH, going C <-> Python is much easier. $\endgroup$
    – Davidmh
    Mar 27, 2014 at 12:16

5 Answers 5

35
$\begingroup$

First, if your undergraduates are like ours and had no prior introduction to computers, expect to spend some time teaching them how to use basic stuff like using a proper editor (i.e., not MS Word), the command line, etc.

I think the answer somewhat depends on where you set the focus of your course (or what you are required to teach). For example: How relevant are the internal workings of the computer? Do you need classes and other advanced OOP structures? Do you want to teach them how to produce efficient programs or are you happy if they produce working programs at all? Also, do not forget that you most probably will need capable tutors.

But now something to advantages and disadvantages of the languages, I am familiar with. Note that this is mainly from my experience as a computational physicist and some of this may depend on the particular field, workgroup, university, etc.

Python

I generally recommend using Numpy from almost the very beginning and I am assuming it to be used in the following.

Advantages:

  • It’s easy to learn and so is reading other people’s code (e.g., your example code, but also the students’ code for the tutors).
  • Input and output (which should not be the focus of your course) can be fully covered by print, Numpy’s savetxt and loadtxt, and maybe sys.argv. It can be introduced on the fly and it does not eat much programming time.
  • You do not need to deal with or only need to deal little with such details as number representation, memory management, data types. Thus it’s fast to program and you can focus on the actual algorithms.
  • It‘s not a compiled language. This has two advantages: Students do not need to deal with a compiler and students can test stuff directly in the console without having to compile, restart and rerun the program. Relatedly, debugging is easier.
  • There are easy-to-use libraries for almost everything.
  • You do not need to learn additional script languages like shell scripts, Make, Gnuplot and so on – all this can be done from Python.
  • There are a lot of good tutorials (for free).

Disadvantages:

  • It’s not compiled. Therefore Python programs may be drastically slower than compiled programs in some cases relevant to computational physics. In other cases, however, libraries (especially Numpy) can yield a comparable performance. Another way, to get good performances with Python is to write the relevant code snippets in another language like C¹. Obviously you need to learn this language for this, but this can be done later and your time learning Python is not wasted.
  • It’s more difficult to teach such details as number representation, memory management, data types and their pitfalls, since they are somewhat obfuscated.

C/C++

Advantages:

  • It is compiled and therefore it’s easier to produce efficient code.
  • You are directly dealing with number representation, memory management, data types and thus it is more intuitive to teach these – your students will get closer to what is really happening in their computer.
  • There are libraries for basically everything but understanding and using a library takes some work.
  • There is a relevant amount of existing code in C/C++ and thus students need to learn the language if they want to work with this code.
  • If you already know C/C++, you can learn Python (for example) very fast.

Disadvantages:

  • It is compiled and your students have to deal with the compiler, the preprocessor, headers and so on. You would be surprised how much students fail at this step, even at the end of the semester.
  • It is slower too learn and it takes longer to produce working code.
  • Dealing with marginal stuff such as input and output takes some time as well in teaching as in programming. In C++, there is an extra syntax for input and output.
  • Compiler and operating-system dependencies.
  • You have to deal with the C/C++ confusion.
  • Reading the code of other’s especially in C++ can be quite difficult due to the vast amount of syntax features.

The main advantages of C++ over C (Classes, templates) should not be relevant for your course and are only becoming relevant for larger projects. Therefore I would choose C of the two, since it is more concise.

Others

Some comments on the other languages:

  • Fortran: This is still used by a lot of groups and there is a lot of legacy code, but you cannot get around dealing with the old standards and their huge limitations and pitfalls (a lot of people are still working with Fortran 77). Also, it will be much harder to find tutorials, help on the Internet and so on.
  • Matlab/Mathematica: All the problems of proprietary software. Consider in particular that your students are likely to collaborate with people who do not have access to this software and the ensuing problems.
  • Cuda: This is only relevant for certain problems, if performance matters. Also, after all I know, you do not want to learn programming this way.

¹ Which is the standard workflow at least in our group.

$\endgroup$
5
  • $\begingroup$ Very precise answer $\endgroup$ Mar 8, 2014 at 5:23
  • 1
    $\begingroup$ I would also chime in to say one of the unmentioned bonuses of Python is that there are a number of scientific distributions (Anaconda/Enthought/PythonXY/SAGE) that really smooth the process of getting everyone in the same computing page. Also, even cooler are the web based approaches (Wakari and SAGE) that provide it all via a browser aka 0 installed software. Teaching undergrads c++ or Fortran will result in more time lost fighting a compiler than time gained in code-speed. $\endgroup$
    – meawoppl
    Mar 9, 2014 at 2:55
  • $\begingroup$ There is an amazing ecosystem in python for computational physics. Numpy, scipy for providing basic infrastructure, mayavi, tvtk for visualizations. Python is pretty mature in scientific computing community. I do use C++ in production but no matter what it is a pain to use. $\endgroup$
    – Sai Venkat
    Mar 9, 2014 at 13:07
  • $\begingroup$ @meawoppl: “Teaching undergrads c++ or Fortran will result in more time lost fighting a compiler than time gained in code-speed.“ – It’s not the code speed for the exercises for the course that matters (programs will be very fast either way, unless exercises are specifically made such that they aren’t), but the code speed of the programs they will write for real life or similar. And there is some stuff that just cannot be done efficiently in Python only. $\endgroup$
    – Wrzlprmft
    Mar 9, 2014 at 17:26
  • 1
    $\begingroup$ I would say CUDA is out of the question for a general purpose course, as it requires hardware that not everybody has. And if you only have a laptop without it, there is almost no way you can install one. $\endgroup$
    – Davidmh
    Mar 27, 2014 at 12:08
24
$\begingroup$

In 2014, I would've said Python. In 2017, I wholeheartedly believe that the language to teach undergraduates is Julia.

Teaching is always about a tradeoff. On one hand, you want to choose something that is simple enough that it is easy to grasp. But secondly, you want to teach something that has staying power, i.e. something that can grow with you. The common dynamic languages (Python/MATLAB/R) all easily fall into category 1 due to their non-existent boilerplate code and the ease of opening up an interpreter and spitting out code, while C/C++/Fortran fall into the second category as the languages with which the core highly-performant software of today's world were written.

But there are issues with using a language which doesn't fully capture the other category. When using a language like Python, it nicely abstracts away things like types and integer overflow. This is nice for teaching the first semester computing, but as you want to dig deeper and deeper into how things are actually working, Python's language is too far abstracted away from the underlying metal to be a good teaching tool. But C/C++/Fortran (or Java... I learned Java first...) all have such a big startup cost that the hardest thing to learn is just how to get headers setup and main compiled, which distracts from actually learning to program.

Enter Julia. When you first use Julia, you can abstract away the entire idea of types and use it just like MATLAB or Python. But as you want to learn more, there is a "rabbit hole" of depth to the language. Since it's really an abstraction layer based on a type system + multiple dispatch over LLVM, it is essentially "an easy way to write statically compiled code" (and type-stable functions can actually be statically compiled). What this means is that the details of C/C++ are also accessible. You can learn how to write simple loops and functions without boilerplate code, and then dig into the function pointers. Julia's metaprogramming features let you directly access the AST, and there are macros which show every part of the compilation chain. Also, as a Lisp, is amenable to functional programming styles. And it has a lot of parallel computing capabilities. Ideas like parametric typing and type-stability are fairly unique and deep in Julia.

If you want to study programming languages themselves, you can learn the steps of how compilation works by using @code_lowered to see what lowering is, see the typed-AST with @code_typed, see LLVM IR with @code_llvm, and finally the native assembly code with @code_native. This can be used to show what the cost of dynamic variables is and exactly how "variable boxing" works, and this blog post shows how these introspection tools can be used to teach how compiler optimizations can/cannot occur.

There are not just computer science and software engineering ideas to explore, but also rich mathematical ideas. Since Julia's main libraries are written with generic typing in mind, it's trivial to create matrix-free operators and use IterativeSolvers.jl to perform GMRES using them. You can use introspection tools like @which to show you exactly how anything was implemented. For example, how does \ work?

@which rand(10,10)\rand(10)
#\(A::AbstractArray{T,2} where T, B::Union{AbstractArray{T,1}, AbstractArray{T,2}} where T) in Base.LinAlg at linalg\generic.jl:805

That points me straight to the definition of \. It is implemented in Julia, so someone who knows Julia can then learn the algorithm and how it works through identifying matrix subtypes and specializing when possible (falling back to Gaussian elimination). Since Julia's code is MIT licensed (and almost all packages are MIT licensed), students are then free to use these ideas in their own code (with attribution) (when code is GPL licensed, as is the case with most MATLAB and R packages, they need to be careful about licensing issues!).

Since the language core is built with a very active open source community, there is also a rich resource on the history of the language development: its Github issues. Understanding language questions like what really is a matrix transpose? can be very enlightening for understanding these mathematical objects in greater detail.

But lastly, in the end, you want to teach your students how to create. Sadly, learning Python or R doesn't necessarily mean you have what it takes to "develop Python/R" since most of the widely-used and well-optimized packages have a substantial amount of C/C++/Fortran code in them in order to get performance. Thus, for these students to be able to contribute to the scientific ecosystems for these languages, they will eventually have to learn another language at some point. While that's not entirely awful, it's sub-optimal now that Julia exists. Since type-stable Julia is able to achieve the speed of C/Fortran, most of the packages in the Julia ecosystem are pure Julia code. Learning Julia means one has learned to develop Julia. And since Base Julia is also mostly Julia code (just a few primitives and the parser isn't), they can also contribute there as well.

That said, there are some downsides to choice of Julia. For one, it is much newer than these other languages and so it's a bit more scarce on resources. You'll have to come up with a lot of teaching tools on your own, or pull from resources on the web which are listed on the Julia website. Also, the language details aren't quite settled, though 1.0 is coming out soon (by the end of 2017). And it's also quite likely that you, the potential teacher of a course in Julia, might not have that much experience with the language yourself. However, these are the kinds of problems which go away over time, whereas the benefits of Julia that I mentioned above are much more core to the languages themselves.

$\endgroup$
4
  • $\begingroup$ Wonder if using a little used language makes sense as the knowledge of the syntax will be likely to be lost and it is not guarantee that the students will be learn several languages. Maybe in such point of view, python is still a good take. $\endgroup$ Jul 15, 2017 at 2:28
  • 1
    $\begingroup$ That's why I wouldn't've said Julia before, but now that it's pretty common you might as well use it. $\endgroup$ Jul 15, 2017 at 3:00
  • $\begingroup$ I think Julia is still too new. In industry, students will be expected to know Python, C++, (ew) MATLAB, and R. I think it's better as a second or third language to learn, as an enriching experience. Students are unlikely to use Julia again in the near future. $\endgroup$ Jul 25, 2017 at 3:26
  • 2
    $\begingroup$ Students should learn transferable skills, not a template to copy and paste. In that sense, Python/MATLAB/R abstracts too far from the computer to be a good teaching tool beyond the simplest programming, but C++ is too low level to be a good teaching tool. Sure, if you're going to have a language on the side like in a numerical analysis course, do what you'll use because the course isn't about programming. But if it's about programming concepts, Julia is pretty much the only simple language which actually has most of the concepts in its design. $\endgroup$ Jul 25, 2017 at 4:42
2
$\begingroup$

Speaking as a not-too-far-removed undergraduate, and assuming that you are not teaching in the CS department, I think it would be a disaster to introduce students to computer programming with something like C, C++, or Fortran (or god forbid CUDA), even though others have pointed out that they are probably the status quo in scientific computing.

If you're expecting to teach students scientific computing and introduce them to programming in the same course, I would bet that's too much to cover in a semester unless you stick to an interpreted language like matlab or python. In my experience, most classes in scientific computing at the undergraduate level are taught in one of those two any how, and python is becoming more and more useful as a production level language every day, so it still has some utility as a practical skill (beyond just teaching programming basics, I mean).

Just my two cents.

$\endgroup$
2
  • 4
    $\begingroup$ Disaster is too strong a word to use to describe teaching students C, C++, or Fortran. Any of these languages (C, C++, Fortran, or Python) can be fine for teaching programming and scientific computing depending on how you do it. $\endgroup$
    – Bill Barth
    Mar 7, 2014 at 21:30
  • 1
    $\begingroup$ From my experience of a class by the CS department (C++), and a class for astronomers (Fortran 77), C/C++/Fortran don't provide sufficient help compared to python for completely new programmers (segfaults vs exceptions). Using C/C++/Fortran either implies learning about how to use a debugger (or use of an IDE), whereas python can be used on it's own. $\endgroup$ Mar 12, 2014 at 6:04
1
$\begingroup$

C,C++, & Fortran (listed in no particular order) are the three main programming languages used for computational mathematics/physics if you want to solve large problems on supercomputers. I think CUDA is considered a library that is used in conjunction with other languages for accelerated GPU computing. Matlab and python are great to learn for running output diagnostics and creating prototype models. They are also easier to learn and may be better for a course where you want to get across algorithms verses learning how to program.

Thus, if your course is purely about programming I would choose C++ or, if this is the students first time programming, Python. Both of those languages have high utility outside the world of scientific computing. If the course is centred around learning algorithms to solve physics based problems then I think Matlab is undoubtedly the winner.

$\endgroup$
0
$\begingroup$

short: Take into account that scientific computing is complicated itself. Do you really want the programming language to came into play?

Mathematics uses abstraction to solve problems that cannot be solved by intuition. Therefore concepts have the tendency to be abstract. This why it is not trivial to understand what concepts to encapsulate. In scientific computing the usual examples for classes like "Animal" "Vehicle" are rather useless. This is true for Object oriented programming, but I believe that reproducing abstract concepts on a computer is not trivial in Imperative programming as well.

This is why I believe that here we are dealing with two different efforts: programming, on one side, and scientific computing on the other. At undergraduate level, where students come from heterogeneous backgrounds, you may end up teaching two different things at the same time.

If your goal is teaching scientific computing I think that is difficult enough. Having an additional barrier as the programming language (we all agree that C++ requires training) would demotivate a good chunk of students, this is why I suggest to go with python.

If your course is "Introduction to SC" I believe that python has the best result/effort ratio.

PS: now we have rather good computers, we do not really need to look for efficency at an undergraduate level.

$\endgroup$
2
  • $\begingroup$ Regarding your PS: Why does performance not matter for undergraduates only? Apart from the fact that it is easy to conceive tasks for undergraduates where performance does matter, it’s not those tasks they are learning performance for but real life. Also, the computer’s speed may have become higher, but so have our expectations. $\endgroup$
    – Wrzlprmft
    Sep 24, 2014 at 15:28
  • $\begingroup$ Sorry I was too sharp. Let me rephrase it in "students can run satisfactory big applications with an interpreted language, before digging into code optimization and then going to a compiled language". $\endgroup$ Sep 24, 2014 at 16:31

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