1 - Introduction

Simple Calculations and Variables

Julia can be used as a "pocket calculator". All the usual arithmetic operators and standard mathematical functions are available. Exponentiation is denoted by ^. Mathematical constants such as e or pi are known. The imaginary unit \(\sqrt{-1}\) is denoted by im.
Boolean operators &&, ||, and ! work as usual with true and false as logical constants. Boolean comparisons through ==, <, <=, etc.
Comparisons chaining is supported. The end-of-line comment sign is #; inline comments with #= ... =#.

In [1]:
(1 + 2*3 + 3^4 + 4^5) / 10  # no integer division
Out[1]:
111.2
In [2]:
sqrt(-1)
DomainError
while loading In[2], in expression starting on line 1

 in sqrt at math.jl:131
In [3]:
22 < pi^e < e^pi < 24       # chained comparison
Out[3]:
true

Variable names consist of lower and upper case letters, digits, and underscore or (but start with a letter). UTF8 characters are allowed. Julia is case sensitive. A semicolon at the end suppresses printing on the console. When multiplying a variable with a number, the * sign can be left out.

In [4]:
γ = 0.57721_56649_01532_86;  # Euler-Masceroni constant
2γ
Out[4]:
1.1544313298030657

Strings are written with ", character literals with '. The string function will change an object into its string representation. Strings are concatenated with * and duplicated with ^. String interpolation is done with the

In [5]:
"The result of e*pi is " * string(e*pi)
Out[5]:
"The result of e*pi is 8.539734222673566"
In [6]:
println("The result of pi*e is $(pi*e).")
The result of pi*e is 8.539734222673566.

Rational numbers are represented as m//n and printed in their normalized, i.e. cancelled, form.
They are a subtype of floating point numbers and float(m//n) will convert them to floats.

In [7]:
r = 1//3+ 1//6 + 1//12 + 1//15
Out[7]:
13//20
In [8]:
1//3 - 1/3
Out[8]:
0.0

BigFloat and BigInt examples

In [9]:
factorial(20)
Out[9]:
2432902008176640000

Simple, one-line functions can be defined in a very intuitive way, e.g., f(x) = sin(x) + cos(x).

In [10]:
p(x) = 1 + 2x^2 + 3x^3;  # a polynomial

p(1), p(1.0), p(1im)     # returns a tupel
Out[10]:
(6,6.0,-1 - 3im)

The type hierarchy of numerical types looks like this:

Number
    Real
        FloatingPoint
            BigFloat
            Float64 Float32 Float16
        Integer
            BigInt
            Signed
                Int128  Int64  Int32  Int16  Int8
                          --Int--
            Unsigned
                Uint128 Uint64 Uint32 Uint16 Uint8
            Bool
            Char
        Rational
    Complex

Vectors and matrices

Vectors and matrices are defined in Julia in a very MATLAB-like way.
Array{Float64,1}

In [11]:
v = [1, 2, 3, 4]
Out[11]:
4-element Array{Int64,1}:
 1
 2
 3
 4
In [12]:
v[1], v[2:end]
Out[12]:
(1,[2,3,4])
In [13]:
A = [1.0 2 3 4; 5 6 7 8; 9 10 11 12]
Out[13]:
3x4 Array{Float64,2}:
 1.0   2.0   3.0   4.0
 5.0   6.0   7.0   8.0
 9.0  10.0  11.0  12.0
In [14]:
H12 = [1/(i+j-1) for i in 1:12, j=1:12]
# det(H12)
Out[14]:
12x12 Array{Float64,2}:
 1.0        0.5        0.333333   …  0.1        0.0909091  0.0833333
 0.5        0.333333   0.25          0.0909091  0.0833333  0.0769231
 0.333333   0.25       0.2           0.0833333  0.0769231  0.0714286
 0.25       0.2        0.166667      0.0769231  0.0714286  0.0666667
 0.2        0.166667   0.142857      0.0714286  0.0666667  0.0625   
 0.166667   0.142857   0.125      …  0.0666667  0.0625     0.0588235
 0.142857   0.125      0.111111      0.0625     0.0588235  0.0555556
 0.125      0.111111   0.1           0.0588235  0.0555556  0.0526316
 0.111111   0.1        0.0909091     0.0555556  0.0526316  0.05     
 0.1        0.0909091  0.0833333     0.0526316  0.05       0.047619 
 0.0909091  0.0833333  0.0769231  …  0.05       0.047619   0.0454545
 0.0833333  0.0769231  0.0714286     0.047619   0.0454545  0.0434783
In [15]:
w = A * v

A \ w
Out[15]:
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0