Datatypes and Numbers¶

you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=TX9BIDey5FM&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=8

In [1]:
import ROOT
In [2]:
import fortranmagic
%load_ext fortranmagic
import os
import sys

import numpy as np

if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
New default arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'

Hello world¶

C++¶

The // introduces a comment. And the meaning of each line are described by the comment.

In [3]:
%%cpp
/* this is a 
mulitline comment
*/
#include <iostream>                 //include input output functions, this is not needed in the jupyter notebook
using namespace std;                //this line allows to write cout instead of std::cout for printing
//int main(int argc,char *argv[])   //the main function that is called only in compiled code is commented out here
{                                   //the curly brackets introduces a scope, more on it later.
    cout<<"Hello World!"<<endl;     //here we print "Hello World!"
    return 0;                       //this functions ends the program this is only needed in compiled code not here
}
Hello World!
In [4]:
%%cpp
//multiline code works with backlash
cout<<"Hello Wor\
ld!"\
<<" Konnichiwa!"<<endl;  
Hello World! Konnichiwa!

C¶

In [5]:
%%cpp
#include <stdio.h>                 //include input output functions, this is not needed in the jupyter notebook
/* this is a 
mulitline comment
*/
//int main(int argc,char *argv[])   //the main function that is called only in compiled code is commented out here
{                                   //the curly brackets introduces a scope, more on it later.
    printf("Hello World!");         //here we print "Hello World!"
    return 0;                       //this functions ends the program this is only needed in compiled code not here
}
Hello World!
In [6]:
%%cpp

printf("Hello \
World!");         //here we print "Hello World!"
Hello World!

Javascript¶

In [7]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

/* multiline strings are 
the same in javascript */

console.log("Hello World!")  //this is a comment you can add semicolons
console.log("Hello World!");console.log("Hello World!")  //different to c/c++ semicolon ; 
                                                         //is only needed between commands
In [8]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

// multiline code are done as in c cpp
console.log("Hello \
World!") 
// or with multiline strings
console.log(`Hello
World!`)

Python¶

In [9]:
#multiline code works if it is obvious by the brackets:
print("Hello World! "
     +"Konnichiwa!")  
#or with backlash
print("Hello \
World!")
#or with multiline strings
print("""Hello
World!
Konnichiwa""")
Hello World! Konnichiwa!
Hello World!
Hello
World!
Konnichiwa

Fortran 95¶

https://www.intel.com/content/www/us/en/developer/tools/oneapi/toolkits.html#base-kit

install OneApi base toolkit and Intel® HPC Toolkit.

In [10]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none ! if you do not do this then special variable names have special types do not do that
    print *,"Hello World"
    write (*,*) "Hello World" ! first star is file (unit) number, second star is format
! end program
end subroutine main
In [11]:
main()
 Hello World
 Hello World

Variablen: Ganzzahlen¶

C++¶

In [12]:
%%cpp
int age=42;
cout <<"I am "<<age<<" old."<<endl;
I am 42 old.
In [13]:
%%cpp
int age=42;
int add=10;
age=age+add;
cout <<"I am "<<age<<" old."<<endl;
I am 52 old.
In [14]:
%%cpp
int age=42;
int add=10;
age+=add;   //this is a short form
cout <<"I am "<<age<<" old."<<endl;
I am 52 old.

C¶

In [15]:
%%cpp
int age=42;  //these variable definition need to be at the beginning of a scope in c!
printf("I am %d old.\n",age);
I am 42 old.
In [16]:
%%cpp
int age=42;
int add=10;
age=age+add;
printf("I am %d old.\n",age);
I am 52 old.
In [17]:
%%cpp
int age=42;
int add=10;
age+=add;
printf("I am %d old.\n",age);
I am 52 old.

Javascript¶

In [18]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let age=42 //type annotations are not available in javascript you need to use typescript
           //that is translated into javascript by a compiler
console.log("I am "+age+" old\n")
In [19]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let age=42
let add=10
age=age+add
console.log("I am "+age+" old\n")
In [20]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let age=42
let add=10
age+=add
console.log("I am "+age+" old\n")

Python¶

In [21]:
int: age=42                     # we can specify the type in front of the : but it is ignored
print("I am %d old.\n" % (age)) #c style parameters are possible but considered oldschool.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[21], line 1
----> 1 int: age=42                     # we can specify the type in front of the : but it is ignored
      2 print("I am %d old.\n" % (age)) #c style parameters are possible but considered oldschool.

NameError: name 'age' is not defined
In [22]:
age=42
print("I am",age,"old.\n")
I am 42 old.

In [23]:
age=42
add=10
age=age+add
print("I am",age,"old.\n")
I am 52 old.

In [24]:
age=42
add=10
age+=add
print("I am",age,"old.\n")
I am 52 old.

In [25]:
age=42
print(f"I am {age} old.\n")
I am 42 old.

In [26]:
age=42
print("I am {age1} and {age2} old.\n".format(age1=age,age2=age))
I am 42 and 42 old.

In [27]:
age=42
print("I am "+str(age)+" old.\n")
I am 42 old.

Fortran¶

In [28]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    integer :: i=42
    print *,"Hello World",i
! end program
end subroutine main
In [29]:
main()
 Hello World          42

Foatingpoint Numbers¶

C++¶

In [30]:
%%cpp
double age=42.5;
cout <<"I am "<<age<<" old."<<endl;
I am 42.5 old.
In [31]:
%%cpp
double age=42.5, add=10.2; //we can put declarations of same type in a single line
age+=add;
cout <<"I am "<<age<<" old."<<endl;
I am 52.7 old.

C¶

In [32]:
%%cpp
float f=4.2; //low precission 8 digits
double age=42.5,add=10.2; //16 digits
long double ld=8.88; //also only 16 bits
age+=add;
printf("I am %d old.\n",age);// this is wrong you need to check the types !! this is problematic in c
printf("I am %f old.\n",age);
I am -180201408 old.
I am 52.700000 old.
input_line_68:6:25: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
printf("I am %d old.\n",age);// this is wrong you need to check the types !! this is problematic in c
             ~~         ^~~
             %f

Javascript¶

In [33]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let age=42.5,add=10.2
age+=add;
console.log("I am "+age+" old\n")

Python¶

In [34]:
age=42.5;add=10.2
age+=add
print("I am",age,"old.\n")
I am 52.7 old.

Fortran¶

https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html format specifier instead of the * in print image.png

In [35]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    real*4 :: f=42.2
    real(kind=8) :: d=42.22222
    real(kind=16) :: ld=42.22222
    print *,"Hello World",f,d,ld
! end program
end subroutine main
In [36]:
main()
 Hello World   42.2000008       42.222221374511719        42.2222213745117187500000000000000000      

Complex numbers¶

c/c++¶

The C programming language, as of C99, supports complex number math with the three built-in types double _Complex, float _Complex, and long double _Complex (see _Complex). When the header <complex.h> is included, the three complex number types are also accessible as double complex, float complex, long double complex. See https://en.cppreference.com/w/c/numeric/complex

In [37]:
%%cpp
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
 
//int main()
{
    using namespace std::complex_literals;
    std::cout << std::fixed << std::setprecision(1);
 
    std::complex<double> z1 = 1i * 1i; // imaginary unit squared
    std::cout << "i * i = " << z1 << '\n';
 
    std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
    std::cout << "pow(i, 2) = " << z2 << '\n';
 
    const double PI = std::acos(-1); // or std::numbers::pi in C++20
    std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
    std::cout << "exp(i * pi) = " << z3 << '\n';
 
    std::complex<double> z4 = 1.0 + 2i, z5 = 1.0 - 2i; // conjugates
    std::cout << "(1 + 2i) * (1 - 2i) = " << z4 * z5 << '\n';
}
i * i = (-1.0,0.0)
pow(i, 2) = (-1.0,0.0)
exp(i * pi) = (-1.0,0.0)
(1 + 2i) * (1 - 2i) = (5.0,0.0)
In [38]:
%%cpp
#include <complex.h>
#include <stdio.h>
#include <tgmath.h>
 
//int main(void)
{
    double complex z1 = I * I;     // imaginary unit squared
    printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));
 
    double complex z2 = pow(I, 2); // imaginary unit squared
    printf("pow(I, 2) = %.1f%+.1fi\n", creal(z2), cimag(z2));
 
    double PI = acos(-1);
    double complex z3 = exp(I * PI); // Euler's formula
    printf("exp(I*PI) = %.1f%+.1fi\n", creal(z3), cimag(z3));
 
    double complex z4 = 1 + 2 * I, z5 = 1 - 2 * I; // conjugates
    printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4 * z5), cimag(z4 * z5));
}
input_line_70:8:19: error: expected ';' at end of declaration
    double complex z1 = I * I;     // imaginary unit squared
                  ^
                  ;
input_line_70:9:42: error: use of undeclared identifier 'z1'
    printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));
                                         ^
input_line_70:9:53: error: use of undeclared identifier 'z1'
    printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));
                                                    ^
input_line_70:11:12: error: redefinition of 'complex'
    double complex z2 = pow(I, 2); // imaginary unit squared
           ^
input_line_70:8:12: note: previous definition is here
    double complex z1 = I * I;     // imaginary unit squared
           ^
input_line_70:11:19: error: expected ';' at end of declaration
    double complex z2 = pow(I, 2); // imaginary unit squared
                  ^
                  ;
input_line_70:12:46: error: use of undeclared identifier 'z2'
    printf("pow(I, 2) = %.1f%+.1fi\n", creal(z2), cimag(z2));
                                             ^
input_line_70:12:57: error: use of undeclared identifier 'z2'
    printf("pow(I, 2) = %.1f%+.1fi\n", creal(z2), cimag(z2));
                                                        ^
input_line_70:15:12: error: redefinition of 'complex'
    double complex z3 = exp(I * PI); // Euler's formula
           ^
input_line_70:8:12: note: previous definition is here
    double complex z1 = I * I;     // imaginary unit squared
           ^
input_line_70:15:19: error: expected ';' at end of declaration
    double complex z3 = exp(I * PI); // Euler's formula
                  ^
                  ;
input_line_70:16:46: error: use of undeclared identifier 'z3'
    printf("exp(I*PI) = %.1f%+.1fi\n", creal(z3), cimag(z3));
                                             ^
input_line_70:16:57: error: use of undeclared identifier 'z3'
    printf("exp(I*PI) = %.1f%+.1fi\n", creal(z3), cimag(z3));
                                                        ^
input_line_70:18:12: error: redefinition of 'complex'
    double complex z4 = 1 + 2 * I, z5 = 1 - 2 * I; // conjugates
           ^
input_line_70:8:12: note: previous definition is here
    double complex z1 = I * I;     // imaginary unit squared
           ^
input_line_70:18:19: error: expected ';' at end of declaration
    double complex z4 = 1 + 2 * I, z5 = 1 - 2 * I; // conjugates
                  ^
                  ;
input_line_70:19:50: error: use of undeclared identifier 'z4'
    printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4 * z5), cimag(z4 * z5));
                                                 ^
input_line_70:19:55: error: use of undeclared identifier 'z5'
    printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4 * z5), cimag(z4 * z5));
                                                      ^
input_line_70:19:66: error: use of undeclared identifier 'z4'
    printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4 * z5), cimag(z4 * z5));
                                                                 ^
input_line_70:19:71: error: use of undeclared identifier 'z5'
    printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4 * z5), cimag(z4 * z5));
                                                                      ^

Python¶

https://www.geeksforgeeks.org/complex-numbers-in-python-set-1-introduction/

Fortran¶

In [39]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    complex (kind=16) :: c=(0,1) ! this is equal to sqrt(-1) this is a 32 bytes complex number
    print *,"Hello World",c
! end program
end subroutine main
In [40]:
main()
 Hello World                 (0.00000000000000000000000000000000000,1.00000000000000000000000000000000000)