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
import ROOT
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.
%%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!
%%cpp
//multiline code works with backlash
cout<<"Hello Wor\
ld!"\
<<" Konnichiwa!"<<endl;
Hello World! Konnichiwa!
C¶
%%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!
%%cpp
printf("Hello \
World!"); //here we print "Hello World!"
Hello World!
Javascript¶
%%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
%%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¶
#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.
%%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
main()
Hello World Hello World
Variablen: Ganzzahlen¶
C++¶
%%cpp
int age=42;
cout <<"I am "<<age<<" old."<<endl;
I am 42 old.
%%cpp
int age=42;
int add=10;
age=age+add;
cout <<"I am "<<age<<" old."<<endl;
I am 52 old.
%%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¶
%%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.
%%cpp
int age=42;
int add=10;
age=age+add;
printf("I am %d old.\n",age);
I am 52 old.
%%cpp
int age=42;
int add=10;
age+=add;
printf("I am %d old.\n",age);
I am 52 old.
Javascript¶
%%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")
%%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")
%%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¶
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
age=42
print("I am",age,"old.\n")
I am 42 old.
age=42
add=10
age=age+add
print("I am",age,"old.\n")
I am 52 old.
age=42
add=10
age+=add
print("I am",age,"old.\n")
I am 52 old.
age=42
print(f"I am {age} old.\n")
I am 42 old.
age=42
print("I am {age1} and {age2} old.\n".format(age1=age,age2=age))
I am 42 and 42 old.
age=42
print("I am "+str(age)+" old.\n")
I am 42 old.
Fortran¶
%%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
main()
Hello World 42
Foatingpoint Numbers¶
C++¶
%%cpp
double age=42.5;
cout <<"I am "<<age<<" old."<<endl;
I am 42.5 old.
%%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¶
%%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¶
%%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¶
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
%%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
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
%%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)
%%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));
^
Fortran¶
%%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
main()
Hello World (0.00000000000000000000000000000000000,1.00000000000000000000000000000000000)