// Aarhus University / Physics / Subatomic Physics / Nuclear Theory >

Programming. Spring 2014.


NB:
Schedule:
The plan:
  1. C Programming: Introduction. Why C? Why Unix POSIX? Why terminal?
    Exercises:
    • Install Ubuntu version 12.04 or 13.10 on your laptop and bring it to the class. That's the easiest way to get through this course, I think. Alternatively, get yourself an account on a POSIX server and bring a laptop from which you can ssh to your account.
    • Learn how to start a terminal.
    • Learn the following commands (file utilities) from GNU Core Utilities: cd, cp, ls, mkdir, mv, rm.
    • Read about the command line completion.
    • Install the Nano text editor with the command sudo apt-get install nano (or your other favourite editor) and learn how to create, save, and edit text files with it.
    • Install the GNU C compiler with the command sudo apt-get install gcc.
    • Make the exercise C Programming/A taste of C.
  2. Basics of compilation; Structure of a C program; Variables and basic data types (char, int, float, double, long double, complex); Operators (+,-,*,/); Simple output with the printf function;
    • Read C Programming, chapter Beginning C until math.
    • Do the "Variables" and "Simple I/O / Input" exercises from the Beginning C/Exercises chapter.
    • Do Problems 1 exercises.
  3. Contol flow: conditional statements (if, switch) and loops (while, for, do-while); math functions, math library (remember -lm); type-generic math tgmath.h.
    1. Read the C Programming/Beginning C/Program Flow section.
    2. Learn the arithmetic, comparison, and compound assignment operators from the list of operators in C and C++. Learn also the ternary conditional operator from the same table.
    3. Learn the C iteration statements (loops); and the C selection statements.
    4. Do Problems 2 exercises.
  4. Functions, blocks, and scope of variables; Arrays and pointers; Static, variable-length, and dynamic arrays; memory allocation with malloc; passing arrays as parameters to functions.
    1. Reading: pointers in computer programming (in particular, C-pointers and C-arrays); C syntax→Arrays (including dynamic arrays); C syntax→Pointers; C dynamic memory allocation.
    2. Do Problems 3 exercises.
  5. User-defined datatypes: structs and unions; simple input and output; streams and redirection.
    • Reading:
      • C syntax → Structures and Unions (concentrate on structures, we won't be using unions).
      • C syntax → Functions → Global Structure → main invocation.
      • C syntax → Miscellaneous → Command-line arguments.
      • atoi (ascii to integer) and atof (ascii to double) functions from <stdlib.h>.
      • Simple input with scanf function.
      • Unix I/O redirection.
    • Problems 4.
  6. Make utility and makefiles; passing functions as parameters to other functions; C-preprocessor; building a program from functions in different C-files.
  7. Gnuplot and/or Pyxplot.
    • Install Gnuplot and/or Pyxplot on your box with the command(s)
      sudo apt-get --yes install gnuplot
      sudo apt-get --yes install pyxplot gv
      
      The --yes option automatically answers 'yes' on all questions apt-get might ask.

      Some of you have managed to install a special version of gnuplot, called gnuplot-nox where 'nox' means 'no X-window'. This version, predictably, has no x-window terminal. If you plan to use gnuplot interactively, you have to install 'gnuplot', not 'gnuplot-nox'.

      The two plotting utilities have very similar scripting languages but otherwise are somewhat different. Pyxplot uses LaTeX to render all text on the plot which is rather convenient. Gnuplot has more terminals. Look at their web-pages, gnuplot and pyxplot (specifically, the examples), and choose the one you like most (or both).

    • Read Pyxplot users' guide → "first steps with Pyxplot". Most of it applies also to Gnuplot. Reading the Gnuplot manual is a daunting task, admittedly, nevetheless try read the "Plotting" section.
    • Pyxplot example.
    • Problems 6. Hints.
  8. GSL: installation, including, linking.
    • Install GSL on your box with the command
      sudo apt-get install libgsl0-dev gsl-bin
      
      if I remember correctly.
    • Alternatively, you may download the source code and build the library on your box. It will probably take more time though.
    • Read the GSL manual chapters 'Introduction', 'Using the library', 'Vectors and Matrices', and 'Eigensystems/Real Symmetric Matrices'.
    • GSL Bessel function example.
    • Problems 7. Hints.
  9. GSL: vectors and matrices; vector and matrix operations; BLAS; Ordinary Differential Equations. Example: C vs Matlab benchmark (harmonic series).
    • Read the corresponding chapters in the GSL manual; in particular, learn
      • The interface to the BLAS routines.
      • The data type gsl_odeiv2_system with the fields function, jacobian, dimension, params to hold an Ordinary Differential Equations Initial Value System.
      • The names of the available stepping algorithms: gsl_odeiv2_step_rk2, gsl_odeiv2_step_rk4, gsl_odeiv2_step_rk45, ... , gsl_odeiv2_step_bsimp. Note which of the algorithms require the Jacobian of the system.
      • The driver functions gsl_odeiv2_driver_alloc_y_new gsl_odeiv2_driver_apply
    • gsl_odeiv2 example.
    • Problems 8. Hints.
  10. GSL: numerical integration; multidimensional minimization.
    • In the Numerical Integration chapter read about the following things:
      • gsl_function
      • gsl_integration_qags
      • gsl_integration_qagiu
      • gsl_integration_qagil
    • Look at the 'Numerical Integration Examples'.
    • In the Multidimensional Minimization chapter read about the following things:
      • gsl_multimin_function
      • gsl_multimin_fminimizer
      • gsl_multimin_fminimizer_nmsimplex2
      • gsl_multimin_fminimizer_alloc
      • gsl_multimin_fminimizer_set
      • gsl_multimin_fminimizer_iterate
      • gsl_multimin_fminimizer_size
      • gsl_multimin_fminimizer_test_size
    • Look at the Nelder-Mead Simplex example in 'Multimin Examples'.
    • Problems 9. Examples.
  11. GSL: multidimensional root-finding, algorithms without derivatives;
    • Read the multidimensional root-finding chapter in GSL manual, specifically the 'algorithms without derivatives' chapter.
    • You can read more about the Newton's and Broyden's algorithms in the Newton's method article, and the Broyden's method article.
    • Specifically, learn the following objects from the 'multidimensional root-finding' chapter:
      • gsl_multiroot_function
      • gsl_multiroot_fsolver_alloc
      • gsl_multiroot_fsolver_free
      • gsl_multiroot_fsolver_set
      • gsl_multiroot_fsolver_iterate
      • gsl_multiroot_test_residual
    • Problems 10. Examples.
  12. Latex: basics; document structure; importing graphics.
  13. Latex: math; displayed equation; floatings: figures, tables; cross-references; the bibliography.
    • Browse through the sections 'Mathematics', 'Advanced mathematics', 'Floats, Figures and Captions', 'Labels and Cross-referencing', and 'Bibliography Management/Embedded system' in the LaTeX wikibook.
    • Problems 12. Examples.
  14. Tips on programming and debugging; FAQ.
    • Good coding style:
      1. The code is segmented into small methods/functions spanning no more than a screenful.
      2. The names of functions, structures.members, and variables are meaningful and self-describing.
      3. The logic of the code is easy to follow and easy to modify (by somebody else); only a small number of comments are needed in a good code.
      4. Defensive programming: #include<assert.h> and use assertions plentifully: you can easily disable all assertions by simply recompiling the code with -DNDEBUG c-flag:
        CFLAGS += -DNDEBUG
        
    • Simple printf(...) fprintf(stderr,...) debugging amounts to

      ...inserting printf commands that output more or less carefully chosen status information at key points in the program flow, observing that information and deducing what's wrong based on that information. ...Although often looked down on by users of modern debugging software, printf() debugging continues to prove itself indispensable.

      1. Switch on the necessary warnings (e.g. -Wall -Werror).
      2. Read carefully the error/warning messages from the compiler and act upon them.
      3. Insert fprintf(stderr,...) command at the strategic points of the program and localize the source of the error.
      4. It is of advantage to define a TRACE macro (read the C preprocessor chapter again) and use it instead of directly using fprintf function. The macro can then be simply disabled after debugging. For example:
        #ifndef NDEBUG
        #define TRACE fprintf
        #else
        #define TRACE(...)
        #endif
        
        or, more elegantly,
        #ifndef NDEBUG
        #define TRACE(...) fprintf(stderr,__VA_ARGS__)
        #else
        #define TRACE(...)
        #endif
        
    • Problems 13. Examples.
Lecturer:
Dmitri Fedorov
Literature:
  1. Wikibooks, C programming (the link also provides a PDF version).
  2. GNU Scientific Library manual.
  3. Gnuplot documentation.
  4. Pyxplot User's Manual.
  5. Wikibooks, LaTeX.
Links:
Wikipedia

Copyleft © D.V.Fedorov (fedorov @ phys au dk)