Docsity

Scientific Computing Languages: C++, Julia, Python, R, and Fortran, Study Guides, Projects, Research of Programming Languages

Partial preview of the text

Download Scientific Computing Languages: C++, Julia, Python, R, and Fortran and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

Scientific Computing Languages

(Lectures on High-performance Computing for Economists V)

Jes´us Fern´andez-Villaverde^1 and Pablo Guerr´on^2 January 15, 2022 (^1) University of Pennsylvania (^2) Boston College

Programming languages for scientific computation

  • General-purpose languages (GPL):
    1. C++.
    2. Python.
  • Domain-specific languages (DSL):
    1. Julia.
    2. R.
    3. Matlab.
  • If you want to undertake research on computational-intensive papers, learning a GPL is probably worthwhile.
  • Moreover, knowing a GPL will make you a better user of a DSL.

C/C++

  • C/C++ is the infrastructure of much of the modern computing world.
  • If you know Unix and C/C++, you can probably master everything else easily (think of Latin and Romance languages!).
  • In some sense, C++ is really a “federation” of languages.
  • What is the difference between C and C++?
  • C++ introduced full OOP support.

C++

  • General-purpose, multi-paradigm, static partially inferred typed, compiled language.
  • Current standard is C++20 (published in December 2020). C++23 in preview release.
  • Developed by Bjarne Stroustrup at Bells Labs in the early 1980s.
  • C++ has a more modern design and approach to programming than predecessor languages. Enormously influential in successor languages.
  • If you know C++, you will be able to read C legacy code without much problem.
  • In fact, nearly all C programs are valid C++ programs (the converse is not true).
  • But you should try to “think” in C++20, not in C or even in older C++ standards.

C++: advantages

  1. Powerful language: you can code anything you can imagine in C++.
  2. Continuously evolving but with a standard: new implementations have support for meta-programming, functional programming,...
  3. Rock-solid design.
  4. Top performance in terms of speed.
  5. Wide community of users.
  6. Excellent open-source compilers and associated tools.
  7. One of most detailed object-orientation programming (OOP) implementation available.
  8. Easy integration with multiprocessor programming (OpenMP, MPI, CUDA, OpenCL, ...).
  9. Allows low-level memory manipulation.

C++: disadvantages

  1. Hard language to learn, even harder to master (although for scientific computation –and if you start with control structures, arrays, and functions before pointers, objects, and classes– you can get away with a small subset).
  2. Large specification: C++20, Standard Template Library, ...
  3. This causes, at times, portability issues.
  4. Legacies from the past (you even have a goto!).
  5. Minimal optimization of OO features.
  6. Notation is far away from standard mathematical notation (you can fix much of this with libraries, but then you need to learn them).
  7. (Personal preference): matrix indexing starts at zero.

Additional resources I

Books with focus on C++20 and later implementations and on scientific computing.

  • Basic:
    1. Starting Out with C++ from Control Structures to Objects (9th Edition), by Tony Gaddis.
    2. Guide to Scientific Computing in C++ (2nd Edition), by Joe Pitt Francis and Jonathan Whiteley.
  • Intermediate:
    1. C++ High Performance: Master the art of optimizing the functioning of your C++ code (2nd Edition) by by Bj¨orn Andrist and Viktor Sehr.
    2. The C++ Programming Language (4th Edition), by Bjarne Stroustrup.

Python

Python

  • General-purpose, multi-paradigm, dynamically typed, interpreted language.
  • Designed by Guido von Rossum.
  • Name inspired by Monty Python’s Flying Circus.
  • Open source.
  • Current flavor: 3.10.1. A lot of old code in Python 2.x.x. Do not use it!
  • Simple and with full OOP support.
  • Elegant and intuitive language.
  • Ideal, for instance, to teach high school students or a class in introductory CS.

Python: advantages

  1. Great for prototyping: dynamic typing and REPL.
  2. Rich ecosystem: 2.1 Scientific computation modules: NumPy, SciPy, and SymPy. 2.2 Statistics modules: Pandas. 2.3 Plotting modules: matplotlib and ggplot.
  3. Easy unit testing: doctest is a default module.
  4. Manipulates strings surprisingly well (regular expressions)⇒natural language processing, artificial intelligence, big data.
  5. Excellent interaction with other languages.
  6. If you have a Mac, it is already preinstalled (although, likely, an older version)

Python: disadvantages

  • Considerable time penalty.
  • Reference distribution by Guido van Rossum: CPython with IDLE and Python Launcher at http://www.python.org/.
  • Better distribution for economists: Anaconda, with plenty of extras, at https://www.anaconda.com/.
  • Jython is an implementation in the JVM, at https://www.jython.org.
  • High-performance routes:
    1. Numba is an just-in-time specializing compiler which compiles annotated Python and NumPy code to LLVM.
    2. Pypy is an implementation with a JIT compiler.
    3. Cython is a superset of Python with an interface for invoking C/C++ routines.
  • Limitations of these routes. 14

Additional resources

  • Books:
    1. Learning Python (5th Edition), by Mark Lutz.
    2. Python Crash Course (2nd Edition), by Eric Matthes.
    3. High Performance Python (2nd Edition), by Micha Gorelick and Ian Ozsvald.
  • Web page of the language: http://www.python.org/

Julia