ComPhy

Computer Science

[things to learn first]

1. Programming Languages

If you are new to programming, you need to understand that there are many different programming languages. Some are simpler, others are hard; some are fast, whereas others are slow. Programming languages can be segregated into different types by various criteria but we are not going to go deep into that for now, even though I could talk about this for ages. Here is a list of languages that are more common in scientific programming:

  1. the Julia Language (website) - my absolute favourite. It is fast, elegant, and very simple. Furthermore, it was designed specifically for physicists and mathematicians. Can interact with other languages like Python (see below), C, and Fortran.
    official setup tutorials
    Linear Algebra in Julia
    the Julia Language in one video (with setup)
    Parallel Computing and Scientific Machine Learning Course - first videos are introductory, others are basically about Computational Physics
  2. Python (website) - designed to be simple and readable by non-programmers. Python is very slow but popular these days because of simplicity and some tricks to speed it up a bit. Probably a good one to be one's first programming language.
    4.5 Hours of Python (with setup). Highly recommended.
    12 Hours of Python (you won't need many of the mentioned things at first)
    Jupyter Notebooks Tutorial. Jupyter is a widely used environment that is also used in further tutorials. It generally simplifies one's experience with graphics and some other scientific tools.
    MatPlotLib Tutorial. MatPlotLib is a library (tool) for plotting and visualizing data in Python.
    NumPy Tutorial. NumPy is basically one of those tricks Python uses to perform faster computations and operate with mathematical objects.
    SciPy Tutorial. SciPy is a tool for scientific programming in Python. Crucial for Computational Physics.
    Mathematical Python
  3. R (website) - a language for statistical computing. I heard that some people use it for physics too.
  4. MATLAB (website) - a relatively popular language designed to be used by mathematicians. I cannot recommend it, though, inasmuch as it is not entirely free.

Of course, other languages (C, C++, Java, etc.) can be used too, I only mentioned those that are most practical and common for scientific programming.

2. Theory

I am only going to mention the two most prominent theoretical areas that everyone is likely to face.

  1. Graph Theory is a widely used area of mathematics for computer modeling.
    Graph Theory course on Tutorialspoint
    Algortihms on Graphs by GeeksForGeeks
  2. Computational and Algorithmic Complexity helps to study and understand some properties of algorithms and tasks.
    Algorithmic Complexity Basics
    Algorithms and Complexity (Introduction) with Python
    "Computational Complexity Theory" on Stanford Encyclopedia of Philosophy

3. Some Tips on Programming

It is always extremely important to write optimal code, therefore, this section is mainly about some general tips on code optimization.

There are two main resources that your program uses: time and memory. Sometimes one can reduce execution and/or algorithmic time by using more computer memory, or do the opposite, that is, to save memory by building a slower algorithm (the latter is not a common approach, however). The key rules for beginners:

  1. The less loops and iterations, the better: it is always faster to perform, let's say, n/2 actions rather than n.
  2. Avoid conditions when possible: to be honest, most of the modern compilers are somewhat good at reducing useless operations, however it is always good to omit any chance of your program running slow. Thus, if you, suppose, have a condition clause where the "if"-branch and the "else"-branch only differ by a single number (a frequently occurring situation), you should remove this condition entirely.
  3. Don't store big pieces of data that you will not use: Suppose, you had a huge array of a million integers. You have processed it and your algorithm has already derived the needed information from it. Don't let this array remain in your computer's memory.

Let us now address the first two rules a bit further. Here are the examples (in pseudocode, that is, in a schematic unreal programing language).

        # RULE 1
        # suppose we have a n×n matrix A and we want to iterate through its diagonal
        # BAD (n^2 iterations + condition):
        for i in 1...n
          for j in 1...n
            if i == j
              print(A[i][j])
            endif
          endfor
        endfor

        # GOOD (only n necessary iterations):
        for i in 1...n
          print(A[i][i])
        endfor


        # RULE 2
        # suppose we have the following situation
        # B is a boolean control variable (true or false) or it can be an arbitrary logical expression
        # R is the variable for the result
        if B
          R := 0
        else
          R := 1
        endif

        # this can obviously be simplified to
        R := not B
        # one can prove that the results R are equal, if that is not obvious
        # moreover, one can generalize this trick if we recall that true == 1 and false == 0

        # now,
        # B is an INTEGER control variable (1 instead of true and 0 instead of false)
        # R is the variable for the result
        # F is some function, operation or algorithm
        # n, m are some variables.
        # Depending on B, we want to choose either m or n to run F on, and then save the result to R
        # BAD:
        if B
          R := F(m)
        else
          R := F(n)
        endif

        # GOOD (no conditions):
        array := [n, m]
        R := F(array[B])
        # indexing with B is bow possible as we made it an integer
        # if the language allows, we can skip the conversion from boolean to integer
        # you can generalize it even further, but this is left as an exercise to the reader.
      

To sum up, your code should be designed to perform the task in as few actions as possible (it sounds easier than it is for the vast majority of beginners).

Atell Krasnopolski, 2022