"type de A = Array{Real,1}, type de B = Array{Real,1}\n",
"Real[#undef, #undef, #undef]\n",
"type de A = Array{Real,2}, type de B = Array{Real,2}\n"
]
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"# vecteur\n",
"# vecteur\n",
"A = Array{Real}(undef,3)\n",
"A = Array{Real}(undef,3)\n",
...
@@ -922,40 +561,18 @@
...
@@ -922,40 +561,18 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"Int<:Real"
"Int<:Real"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"Int<:AbstractFloat"
"Int<:AbstractFloat"
]
]
...
@@ -972,21 +589,9 @@
...
@@ -972,21 +589,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"ename": "MethodError",
"evalue": "MethodError: no method matching fct5(::Float64, ::Int64)\nClosest candidates are:\n fct5(!Matched::Int64, ::Int64) at In[30]:2",
"output_type": "error",
"traceback": [
"MethodError: no method matching fct5(::Float64, ::Int64)\nClosest candidates are:\n fct5(!Matched::Int64, ::Int64) at In[30]:2",
"",
"Stacktrace:",
" [1] top-level scope at In[30]:4"
]
}
],
"source": [
"source": [
"function fct5(a::Int, b::Int)\n",
"function fct5(a::Int, b::Int)\n",
" return a+b\n",
" return a+b\n",
...
@@ -1003,35 +608,9 @@
...
@@ -1003,35 +608,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Main Program, test of fct6!\n",
"--------------------------\n",
"a, x = 1, [0, 0]\n",
"f_a, f_x = a, [2, 2]\n",
"Main Program, test of fct7!\n",
"--------------------------\n"
]
},
{
"ename": "MethodError",
"evalue": "MethodError: Cannot `convert` an object of type String to an object of type Int64\nClosest candidates are:\n convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6\n convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7\n convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23\n ...",
"output_type": "error",
"traceback": [
"MethodError: Cannot `convert` an object of type String to an object of type Int64\nClosest candidates are:\n convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6\n convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7\n convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23\n ...",
"",
"Stacktrace:",
" [1] convert(::Type{Tuple{Int64,Array{T,1} where T}}, ::Tuple{String,Array{Int64,1}}) at ./essentials.jl:310",
" [2] fct7!(::Int64, ::Array{Int64,1}) at ./In[31]:11",
" [3] top-level scope at In[31]:26"
]
}
],
"source": [
"source": [
"function fct6!(a::Real ,x::Vector)\n",
"function fct6!(a::Real ,x::Vector)\n",
"#function fct(a::Real ,x::Vector, y::Vector)\n",
"#function fct(a::Real ,x::Vector, y::Vector)\n",
...
@@ -1071,17 +650,9 @@
...
@@ -1071,17 +650,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 32,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"f1 = 4, f2 = 0.0\n"
]
}
],
"source": [
"source": [
"function fct5(a::Real, b::Int)\n",
"function fct5(a::Real, b::Int)\n",
" return a-b\n",
" return a-b\n",
...
@@ -1093,25 +664,9 @@
...
@@ -1093,25 +664,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 33,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/html": [
"# 2 methods for generic function <b>fct5</b>:<ul><li> fct5(a::<b>Int64</b>, b::<b>Int64</b>) in Main at In[30]:2</li> <li> fct5(a::<b>Real</b>, b::<b>Int64</b>) in Main at In[32]:2</li> </ul>"
],
"text/plain": [
"# 2 methods for generic function \"fct5\":\n",
"[1] fct5(a::Int64, b::Int64) in Main at In[30]:2\n",
"[2] fct5(a::Real, b::Int64) in Main at In[32]:2"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"methods(fct5)"
"methods(fct5)"
]
]
...
@@ -1131,20 +686,9 @@
...
@@ -1131,20 +686,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 34,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/plain": [
"niveau_un (generic function with 1 method)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"function niveau_un() \n",
"function niveau_un() \n",
" function niveau_deux()\n",
" function niveau_deux()\n",
...
@@ -1170,44 +714,18 @@
...
@@ -1170,44 +714,18 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 35,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"x=1\n",
"x=4\n",
"x=3\n",
"x=3\n",
"x=5\n",
"x=3\n"
]
}
],
"source": [
"source": [
"niveau_un()"
"niveau_un()"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"x = 1\n",
"après le for x = 4\n",
"x = 3\n",
"x = 4\n",
"x = 5\n",
"x = 4\n"
]
}
],
"source": [
"source": [
"function niveau_deux()\n",
"function niveau_deux()\n",
" x=3;\n",
" x=3;\n",
...
@@ -1245,20 +763,9 @@
...
@@ -1245,20 +763,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 37,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.012944 seconds (3.65 k allocations: 80.641 KiB)\n",
"Permormance1 = 503.0964853516044\n",
" 0.000240 seconds (3.49 k allocations: 70.156 KiB)\n",
"Permormance2 = 503.0964853516044\n"
]
}
],
"source": [
"source": [
"x = rand(1000);\n",
"x = rand(1000);\n",
"\n",
"\n",
...
@@ -1286,20 +793,9 @@
...
@@ -1286,20 +793,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 38,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.009822 seconds (5.25 k allocations: 241.867 KiB)\n",
"Permormance1 = 503.8521806777714\n",
" 0.000004 seconds (1 allocation: 16 bytes)\n",
"Permormance2 = 503.8521806777714\n"
]
}
],
"source": [
"source": [
"x = rand(1000);\n",
"x = rand(1000);\n",
"\n",
"\n",
...
@@ -1327,20 +823,9 @@
...
@@ -1327,20 +823,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 39,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/plain": [
"sumofsins2 (generic function with 1 method)"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"function sumofsins1(n::Integer) \n",
"function sumofsins1(n::Integer) \n",
" r = 0 \n",
" r = 0 \n",
...
@@ -1361,18 +846,9 @@
...
@@ -1361,18 +846,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 40,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.085448 seconds (125.55 k allocations: 6.676 MiB)\n",
" 0.079326 seconds (118.57 k allocations: 6.369 MiB)\n"
`Julia` est un langage typé. La fonction `typeof` permet d'obtenir le type d'un objet ou d'une variable. Un seconde fonction importante pour comprendre comment sont stockés les objets est `objectid`. Cette
`Julia` est un langage typé. La fonction `typeof` permet d'obtenir le type d'un objet ou d'une variable. Un seconde fonction importante pour comprendre comment sont stockés les objets est `objectid`. Cette
fonction donne l'identifiant de l'objet (en gros sa place en mémoire).
fonction donne l'identifiant de l'objet (en gros sa place en mémoire).
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
a=3
a=3
println(typeof(a))
println(typeof(a))
println(objectid(a))
println(objectid(a))
```
```
%% Output
Int64
17186855983609068021
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Int, float, complexes, rationnels et booléens ##
## Int, float, complexes, rationnels et booléens ##
- Les types entiers non signés : UInt8, UInt16, UInt32, UInt64, UInt128;
- Les types entiers non signés : UInt8, UInt16, UInt32, UInt64, UInt128;
- Les réels flottants : Float16, Float32, Float64.
- Les réels flottants : Float16, Float32, Float64.
Par défaut, le type d'une variable est définie par son affectation, mais on peut préciser son type.
Par défaut, le type d'une variable est définie par son affectation, mais on peut préciser son type.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
x=12
x=12
println("x =",x)
println("x =",x)
println("type de x : ",typeof(x))
println("type de x : ",typeof(x))
y=UInt64(12)
y=UInt64(12)
println("y = ",y)
println("y = ",y)
println("type de y : ",typeof(y))
println("type de y : ",typeof(y))
z=1.0
z=1.0
println("type de z : ",typeof(z))
println("type de z : ",typeof(z))
```
```
%% Output
x =12
type de x : Int64
y = 12
type de y : UInt64
type de z : Float64
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
### Les complexes
### Les complexes
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
a=1+2im
a=1+2im
println("a = ",a)
println("a = ",a)
println("type de a : ",typeof(a))
println("type de a : ",typeof(a))
```
```
%% Output
a = 1 + 2im
type de a : Complex{Int64}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
### Les rationnels
### Les rationnels
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
q=2//3
q=2//3
println("q = ",q)
println("q = ",q)
println("type de q",typeof(q))
println("type de q",typeof(q))
q=q+1//5
q=q+1//5
println("q = ",q)
println("q = ",q)
```
```
%% Output
q = 2//3
type de qRational{Int64}
q = 13//15
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
### Booléens
### Booléens
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
a=true
a=true
println("a = ",a)
println("a = ",a)
println("type de a : ",typeof(a))
println("type de a : ",typeof(a))
```
```
%% Output
a = true
type de a : Bool
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Itérateur
## Itérateur
1:5 est un **itérateur**
1:5 est un **itérateur**
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
a=1:2:7
a=1:2:7
println("a = ",a)
println("a = ",a)
println("type de a : ",typeof(a))
println("type de a : ",typeof(a))
b=collect(a)# renvoie le vecteur des valeurs
b=collect(a)# renvoie le vecteur des valeurs
println("b = ",b)
println("b = ",b)
println("type de b : ",typeof(b))
println("type de b : ",typeof(b))
```
```
%% Output
a = 1:2:7
type de a : StepRange{Int64,Int64}
b = [1, 3, 5, 7]
type de b : Array{Int64,1}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## type array
## type array
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
a=[123]
a=[123]
b=[1,2,3]
b=[1,2,3]
c=[1.023;123]
c=[1.023;123]
println("a = $a, b = $b, c = $c")
println("a = $a, b = $b, c = $c")
b_type=typeof(b)
b_type=typeof(b)
a_type=typeof(a)
a_type=typeof(a)
c_type=typeof(c)
c_type=typeof(c)
println("type de a = $a_type, type de b = $b_type, type de c = $c_type")
println("type de a = $a_type, type de b = $b_type, type de c = $c_type")
```
```
%% Output
a = [1 2 3], b = [1, 2, 3], c = [1.0 2.0 3.0; 1.0 2.0 3.0]
type de a = Array{Int64,2}, type de b = Array{Int64,1}, type de c = Array{Float64,2}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
- a est une matrice d'entier à 1 ligne, 3 colonnes
- a est une matrice d'entier à 1 ligne, 3 colonnes
- b est un vecteur d'entier
- b est un vecteur d'entier
- c est une matrice de flottants 2 lignes, 3 colonnes
- c est une matrice de flottants 2 lignes, 3 colonnes
Que se passe-t-il si on calcul : `a*b, b*a, c*b, c*a`
Que se passe-t-il si on calcul : `a*b, b*a, c*b, c*a`
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
a*b
a*b
```
```
%% Output
1-element Array{Int64,1}:
14
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
b*a
b*a
```
```
%% Output
3×3 Array{Int64,2}:
1 2 3
2 4 6
3 6 9
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
c*a
c*a
```
```
%% Output
DimensionMismatch("matrix A has dimensions (2,3), matrix B has dimensions (1,3)")
Stacktrace:
[1] _generic_matmatmul!(::Array{Float64,2}, ::Char, ::Char, ::Array{Float64,2}, ::Array{Int64,2}, ::LinearAlgebra.MulAddMul{true,true,Bool,Bool}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:736
[2] generic_matmatmul!(::Array{Float64,2}, ::Char, ::Char, ::Array{Float64,2}, ::Array{Int64,2}, ::LinearAlgebra.MulAddMul{true,true,Bool,Bool}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:724
[3] mul! at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:235 [inlined]
[4] mul! at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:208 [inlined]
[5] *(::Array{Float64,2}, ::Array{Int64,2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:153
[6] top-level scope at In[10]:1
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
c*b
c*b
```
```
%% Output
2-element Array{Float64,1}:
14.0
14.0
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
b est donc un vecteur colonne
b est donc un vecteur colonne
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Opérations terme à terme
## Opérations terme à terme
On a bien sur les opétations classique : + * et /, l'étoile
On a bien sur les opétations classique : + * et /, l'étoile
\* est la multiplication matricielle pais on a aussi les opérations terme à terme
\* est la multiplication matricielle pais on a aussi les opérations terme à terme
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
d=2*ones(2,3)
d=2*ones(2,3)
c.*d
c.*d
```
```
%% Output
2×3 Array{Float64,2}:
2.0 4.0 6.0
2.0 4.0 6.0
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
**Attention au .** Étant donné que le point . permet à la fois de définir un float et d'effectuer
**Attention au .** Étant donné que le point . permet à la fois de définir un float et d'effectuer
les opérations élément par élément, il faut mettre des espaces entre les points afin de pouvoir distinguer
les opérations élément par élément, il faut mettre des espaces entre les points afin de pouvoir distinguer
la signification de chaque point.
la signification de chaque point.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
5..+[1,2]
5..+[1,2]
```
```
%% Output
2-element Array{Float64,1}:
6.0
7.0
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
5.+[1,2]
5.+[1,2]
```
```
%% Output
syntax: invalid syntax "5.+"; add space(s) to clarify
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
E=1:4
E=1:4
println("E = ",E)
println("E = ",E)
println("type de E : ",typeof(E))
println("type de E : ",typeof(E))
```
```
%% Output
E = 1:4
type de E : UnitRange{Int64}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
On peut extraire des sous matrice facilement, mais attention
On peut extraire des sous matrice facilement, mais attention
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
E=[1234;5678;9101112]
E=[1234;5678;9101112]
E[[1,3],1:2]
E[[1,3],1:2]
```
```
%% Output
2×2 Array{Int64,2}:
1 2
9 10
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
E[[1,3],[1:2]]
E[[1,3],[1:2]]
```
```
%% Output
ArgumentError: invalid index: UnitRange{Int64}[1:2] of type Array{UnitRange{Int64},1}
Stacktrace:
[1] to_index(::Array{UnitRange{Int64},1}) at ./indices.jl:294
[2] to_index(::Array{Int64,2}, ::Array{UnitRange{Int64},1}) at ./indices.jl:274
[3] to_indices at ./indices.jl:325 [inlined] (repeats 2 times)
[4] to_indices at ./indices.jl:321 [inlined]
[5] getindex(::Array{Int64,2}, ::Array{Int64,1}, ::Array{UnitRange{Int64},1}) at ./abstractarray.jl:980
[6] top-level scope at In[17]:1
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Les tuples
## Les tuples
Ce sont des sortes de tableaux à 1 dimension dont les objets peuvent avoir des types différents. Ce sont des objet non modifiables
Ce sont des sortes de tableaux à 1 dimension dont les objets peuvent avoir des types différents. Ce sont des objet non modifiables
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
t=12,13,"quatorze"
t=12,13,"quatorze"
```
```
%% Output
(12, 13, "quatorze")
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
t[1]
t[1]
```
```
%% Output
12
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
t[3]
t[3]
```
```
%% Output
"quatorze"
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
typeof(t)
typeof(t)
```
```
%% Output
Tuple{Int64,Int64,String}
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
t[2]=1# erreur car non modifiable
t[2]=1# erreur car non modifiable
```
```
%% Output
MethodError: no method matching setindex!(::Tuple{Int64,Int64,String}, ::Int64, ::Int64)
Stacktrace:
[1] top-level scope at In[22]:1
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
### Autres types de base
### Autres types de base
* les chaînes de caractères;
* les chaînes de caractères;
* les dictionnaires;
* les dictionnaires;
* les ensembles.
* les ensembles.
On peut aussi créer des types : structures, ...
On peut aussi créer des types : structures, ...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Fonctions
## Fonctions
**Les paramètres de type scalaire ou tuple sont en entrée**
**Les paramètres de type scalaire ou tuple sont en entrée**
**`fct`et `fct!` donnent les mêmes résultats**. Par convention on aura un ! à la fin du nom de la fonction si au moins un des paramètres en entrée est modifié (en général le premier).
**`fct`et `fct!` donnent les mêmes résultats**. Par convention on aura un ! à la fin du nom de la fonction si au moins un des paramètres en entrée est modifié (en général le premier).
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Arguments
## Arguments
- arguments optionnels;
- arguments optionnels;
- vectorisation : `sin.([0,\pi/2,\pi])`;
- vectorisation : `sin.([0,\pi/2,\pi])`;
-`Maps`et `Filters`(programmation fonctionnelle);
-`Maps`et `Filters`(programmation fonctionnelle);
- récursivité.
- récursivité.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
function showtypetree(T,level=0)
function showtypetree(T,level=0)
println("\t"^level,T)
println("\t"^level,T)
fortinsubtypes(T)
fortinsubtypes(T)
ift!=Any
ift!=Any
showtypetree(t,level+1)
showtypetree(t,level+1)
end
end
end
end
end
end
showtypetree(Number)
showtypetree(Number)
```
```
%% Output
Number
Complex
Real
AbstractFloat
BigFloat
Float16
Float32
Float64
AbstractIrrational
Irrational
Integer
Bool
Signed
BigInt
Int128
Int16
Int32
Int64
Int8
Unsigned
UInt128
UInt16
UInt32
UInt64
UInt8
Rational
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
# vecteur
# vecteur
A=Array{Real}(undef,3)
A=Array{Real}(undef,3)
B=Vector{Real}(undef,4)
B=Vector{Real}(undef,4)
A_type=typeof(A)
A_type=typeof(A)
B_type=typeof(B)
B_type=typeof(B)
println("type de A = $A_type, type de B = $B_type")
println("type de A = $A_type, type de B = $B_type")
println(A)
println(A)
A_type==B_type# renvoie true
A_type==B_type# renvoie true
#
#
# Matrices
# Matrices
A=Array{Real}(undef,2,4)
A=Array{Real}(undef,2,4)
B=Matrix{Real}(undef,3,4)
B=Matrix{Real}(undef,3,4)
A_type=typeof(A)
A_type=typeof(A)
B_type=typeof(B)
B_type=typeof(B)
println("type de A = $A_type, type de B = $B_type")
println("type de A = $A_type, type de B = $B_type")
A_type==B_type# renvoie true
A_type==B_type# renvoie true
```
```
%% Output
type de A = Array{Real,1}, type de B = Array{Real,1}
Real[#undef, #undef, #undef]
type de A = Array{Real,2}, type de B = Array{Real,2}
true
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
Int<:Real
Int<:Real
```
```
%% Output
true
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
Int<:AbstractFloat
Int<:AbstractFloat
```
```
%% Output
false
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Types dans les fonctions
## Types dans les fonctions
### Paramètres en entrée
### Paramètres en entrée
Si les paramètres ne sont pas du bon type alors le programme plante
Si les paramètres ne sont pas du bon type alors le programme plante
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
function fct5(a::Int,b::Int)
function fct5(a::Int,b::Int)
returna+b
returna+b
end
end
f1=fct5(2.,2)
f1=fct5(2.,2)
```
```
%% Output
MethodError: no method matching fct5(::Float64, ::Int64)
Closest candidates are:
fct5(!Matched::Int64, ::Int64) at In[30]:2
Stacktrace:
[1] top-level scope at In[30]:4
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
### Paramètre en sortie
### Paramètre en sortie
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
function fct6!(a::Real,x::Vector)
function fct6!(a::Real,x::Vector)
#function fct(a::Real ,x::Vector, y::Vector)
#function fct(a::Real ,x::Vector, y::Vector)
a="a"# a is a new variable
a="a"# a is a new variable
x=[2,2]# x is new variable
x=[2,2]# x is new variable
returna,x
returna,x
end
end
function fct7!(a::Real,x::Vector)::Tuple{Int,Vector}
function fct7!(a::Real,x::Vector)::Tuple{Int,Vector}
a="a"# a is a new variable
a="a"# a is a new variable
x=[2,2]# x is new variable
x=[2,2]# x is new variable
returna,x
returna,x
end
end
println("Main Program, test of fct6!")
println("Main Program, test of fct6!")
println("--------------------------")
println("--------------------------")
a=1
a=1
x=[0,0]
x=[0,0]
f_a,f_x=fct6!(a,x)
f_a,f_x=fct6!(a,x)
println("a, x = $a, $x")
println("a, x = $a, $x")
println("f_a, f_x = $f_a, $f_x")
println("f_a, f_x = $f_a, $f_x")
println("Main Program, test of fct7!")
println("Main Program, test of fct7!")
println("--------------------------")
println("--------------------------")
a=1
a=1
x=[0,0]
x=[0,0]
f_a,f_x=fct7!(a,x)
f_a,f_x=fct7!(a,x)
println("a, x, = $a, $x")
println("a, x, = $a, $x")
```
```
%% Output
Main Program, test of fct6!
--------------------------
a, x = 1, [0, 0]
f_a, f_x = a, [2, 2]
Main Program, test of fct7!
--------------------------
MethodError: Cannot `convert` an object of type String to an object of type Int64
Closest candidates are:
convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6
convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7
convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23
...
Stacktrace:
[1] convert(::Type{Tuple{Int64,Array{T,1} where T}}, ::Tuple{String,Array{Int64,1}}) at ./essentials.jl:310
[2] fct7!(::Int64, ::Array{Int64,1}) at ./In[31]:11
[3] top-level scope at In[31]:26
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Dispatch multiple
## Dispatch multiple
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
function fct5(a::Real,b::Int)
function fct5(a::Real,b::Int)
returna-b
returna-b
end
end
f1=fct5(2,2)
f1=fct5(2,2)
f2=fct5(2.0,2)
f2=fct5(2.0,2)
println("f1 = $f1, f2 = $f2")
println("f1 = $f1, f2 = $f2")
```
```
%% Output
f1 = 4, f2 = 0.0
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
methods(fct5)
methods(fct5)
```
```
%% Output
# 2 methods for generic function "fct5":
[1] fct5(a::Int64, b::Int64) in Main at In[30]:2
[2] fct5(a::Real, b::Int64) in Main at In[32]:2
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Portée des variables
# Portée des variables
De nombreux langages de programmation font la différence entre les variables globales (communes à tout le programme) et les variables locales, qui correspondent aux variables introduites dans le code d'une fonction
De nombreux langages de programmation font la différence entre les variables globales (communes à tout le programme) et les variables locales, qui correspondent aux variables introduites dans le code d'une fonction
Ici, la situation est un peu plus complexe, car la philosophie générale est d'aller vers une localisation plus forte des variables de manière à éviter des conflits de noms pouvant induire des comportements non voulus.
Ici, la situation est un peu plus complexe, car la philosophie générale est d'aller vers une localisation plus forte des variables de manière à éviter des conflits de noms pouvant induire des comportements non voulus.
Ce renforcement du cloisonnement se fait de deux manières :
Ce renforcement du cloisonnement se fait de deux manières :
* Une variable déclarée dans le programme principal (ou en ligne de commande REPL) n'est pas immédiatement accessible dans les blocs for. . . end, while. . . end,try. . . end du programme principal, non plus que dans les fonctions appelées par le programme. Leur appel doit être précédé du mot global.
* Une variable déclarée dans le programme principal (ou en ligne de commande REPL) n'est pas immédiatement accessible dans les blocs for. . . end, while. . . end,try. . . end du programme principal, non plus que dans les fonctions appelées par le programme. Leur appel doit être précédé du mot global.
* Une variable déclarée dans une fonction est visible dans toute fonction interne à cette fonction, ainsi que dans les blocs for. . . end, while. . . end,try. . . end de cette fonction.
* Une variable déclarée dans une fonction est visible dans toute fonction interne à cette fonction, ainsi que dans les blocs for. . . end, while. . . end,try. . . end de cette fonction.
* Si dans un bloc de code la déclaration d'une variable est précédée du mot clef local, c'est une nouvelle variable locale qui est créée. Elle sera détruite à la fin du bloc; si une variable précédente existait, c'est à nouveau elle qui a la main.
* Si dans un bloc de code la déclaration d'une variable est précédée du mot clef local, c'est une nouvelle variable locale qui est créée. Elle sera détruite à la fin du bloc; si une variable précédente existait, c'est à nouveau elle qui a la main.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
function niveau_un()
function niveau_un()
function niveau_deux()
function niveau_deux()
x=3;
x=3;
println("x=",x)
println("x=",x)
end
end
function niveau_deux_deux()
function niveau_deux_deux()
localx=5;
localx=5;
println("x=",x)
println("x=",x)
end
end
x=1;
x=1;
println("x=",x)
println("x=",x)
fori=1:1
fori=1:1
x=4;
x=4;
end
end
println("x=",x)
println("x=",x)
niveau_deux();
niveau_deux();
println("x=",x)
println("x=",x)
niveau_deux_deux();
niveau_deux_deux();
println("x=",x)
println("x=",x)
end
end
```
```
%% Output
niveau_un (generic function with 1 method)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
niveau_un()
niveau_un()
```
```
%% Output
x=1
x=4
x=3
x=3
x=5
x=3
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
function niveau_deux()
function niveau_deux()
x=3;
x=3;
println("x = ",x)
println("x = ",x)
end
end
function niveau_deux_deux()
function niveau_deux_deux()
localx=5;
localx=5;
println("x = ",x)
println("x = ",x)
end
end
x=1;
x=1;
println("x = ",x)
println("x = ",x)
fori=1:1
fori=1:1
x=4;
x=4;
end
end
println("après le for x = ",x)
println("après le for x = ",x)
niveau_deux();
niveau_deux();
println("x = ",x)
println("x = ",x)
niveau_deux_deux()
niveau_deux_deux()
println("x = ",x)
println("x = ",x)
```
```
%% Output
x = 1
après le for x = 4
x = 3
x = 4
x = 5
x = 4
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
**mais sous julia REPL on obtient pour `x`après le for 1 !**
**mais sous julia REPL on obtient pour `x`après le for 1 !**
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Performance
# Performance
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
x=rand(1000);
x=rand(1000);
function sum_global()
function sum_global()
s=0.0
s=0.0
foriinx
foriinx
s+=i
s+=i
end
end
returns
returns
end;
end;
p1=@timesum_global()
p1=@timesum_global()
println("Permormance1 = $p1")
println("Permormance1 = $p1")
p2=@timesum_global()
p2=@timesum_global()
println("Permormance2 = $p2")
println("Permormance2 = $p2")
```
```
%% Output
0.012944 seconds (3.65 k allocations: 80.641 KiB)
Permormance1 = 503.0964853516044
0.000240 seconds (3.49 k allocations: 70.156 KiB)
Permormance2 = 503.0964853516044
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
On the first call (@time sum_global()) the function gets compiled. (If you've not yet used @time in this session, it will also compile functions needed for timing.) You should not take the results of this run seriously. For the second run, note that in addition to reporting the time, it also indicated that a significant amount of memory was allocated. We are here just computing a sum over all elements in a vector of 64-bit floats so there should be no need to allocate memory (at least not on the heap which is what @time reports).
On the first call (@time sum_global()) the function gets compiled. (If you've not yet used @time in this session, it will also compile functions needed for timing.) You should not take the results of this run seriously. For the second run, note that in addition to reporting the time, it also indicated that a significant amount of memory was allocated. We are here just computing a sum over all elements in a vector of 64-bit floats so there should be no need to allocate memory (at least not on the heap which is what @time reports).
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
x=rand(1000);
x=rand(1000);
function sum_arg(x)
function sum_arg(x)
s=0.0
s=0.0
foriinx
foriinx
s+=i
s+=i
end
end
returns
returns
end;
end;
p1=@timesum_arg(x)
p1=@timesum_arg(x)
println("Permormance1 = $p1")
println("Permormance1 = $p1")
p2=@timesum_arg(x)
p2=@timesum_arg(x)
println("Permormance2 = $p2")
println("Permormance2 = $p2")
```
```
%% Output
0.009822 seconds (5.25 k allocations: 241.867 KiB)
Permormance1 = 503.8521806777714
0.000004 seconds (1 allocation: 16 bytes)
Permormance2 = 503.8521806777714
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Conclusion : attention aux variables globales!
Conclusion : attention aux variables globales!
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
function sumofsins1(n::Integer)
function sumofsins1(n::Integer)
r=0
r=0
foriin1:n
foriin1:n
r+=sin(3.4)
r+=sin(3.4)
end
end
returnr
returnr
end
end
function sumofsins2(n::Integer)
function sumofsins2(n::Integer)
r=0.0
r=0.0
foriin1:n
foriin1:n
r+=sin(3.4)
r+=sin(3.4)
end
end
returnr
returnr
end
end
```
```
%% Output
sumofsins2 (generic function with 1 method)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
sumofsins1(100_000)
sumofsins1(100_000)
sumofsins2(100_000)
sumofsins2(100_000)
@time[sumofsins1(100_000)foriin1:100];
@time[sumofsins1(100_000)foriin1:100];
@time[sumofsins2(100_000)foriin1:100];
@time[sumofsins2(100_000)foriin1:100];
```
```
%% Output
0.085448 seconds (125.55 k allocations: 6.676 MiB)
0.079326 seconds (118.57 k allocations: 6.369 MiB)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
@time[sumofsins1(100_000)foriin1:100];
@time[sumofsins1(100_000)foriin1:100];
@time[sumofsins2(100_000)foriin1:100];
@time[sumofsins2(100_000)foriin1:100];
```
```
%% Output
0.116044 seconds (125.54 k allocations: 6.647 MiB, 15.46% gc time)
0.098289 seconds (118.57 k allocations: 6.369 MiB)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Autres points
## Autres points
### Modules
### Modules
### Interface programme en C, en fortran, ...
### Interface programme en C, en fortran, ...
### Calcul parallèle
### Calcul parallèle
### ...
### ...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Remarques
## Remarques
### Caractères UTF-8
### Caractères UTF-8
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` julia
``` julia
β
β
α=10.# on tape \alp puis TAB, on obtient \alpha et on retape TAB et on a le caractère grec
α=10.# on tape \alp puis TAB, on obtient \alpha et on retape TAB et on a le caractère grec