{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction au langage julia #\n", "## Types, id et références ##\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\n", "fonction donne l'identifiant de l'objet (en gros sa place en mémoire)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Int64\n", "17186855983609068021\n" ] } ], "source": [ "a = 3\n", "println(typeof(a))\n", "println(objectid(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Int, float, complexes, rationnels et booléens ##\n", "### Les entiers et réels flottants\n", "- Les types entiers signés : Int8, Int16, Int32, Int64, Int128;\n", "- Les types entiers non signés : UInt8, UInt16, UInt32, UInt64, UInt128;\n", "- Les réels flottants : Float16, Float32, Float64.\n", "Par défaut, le type d'une variable est définie par son affectation, mais on peut préciser son type." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =12\n", "type de x : Int64\n", "y = 12\n", "type de y : UInt64\n", "type de z : Float64\n" ] } ], "source": [ "x = 12\n", "println(\"x =\",x)\n", "println(\"type de x : \",typeof(x))\n", "y = UInt64(12)\n", "println(\"y = \",y)\n", "println(\"type de y : \",typeof(y))\n", "z = 1.0\n", "println(\"type de z : \",typeof(z))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Les complexes" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = 1 + 2im\n", "type de a : Complex{Int64}\n" ] } ], "source": [ "a = 1+2im\n", "println(\"a = \",a)\n", "println(\"type de a : \",typeof(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Les rationnels" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "q = 2//3\n", "type de qRational{Int64}\n", "q = 13//15\n" ] } ], "source": [ "q = 2//3\n", "println(\"q = \",q)\n", "println(\"type de q\",typeof(q))\n", "q = q + 1//5\n", "println(\"q = \",q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Booléens" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = true\n", "type de a : Bool\n" ] } ], "source": [ "a = true\n", "println(\"a = \",a)\n", "println(\"type de a : \",typeof(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Itérateur\n", "1:5 est un **itérateur**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = 1:2:7\n", "type de a : StepRange{Int64,Int64}\n", "b = [1, 3, 5, 7]\n", "type de b : Array{Int64,1}\n" ] } ], "source": [ "a = 1:2:7\n", "println(\"a = \",a)\n", "println(\"type de a : \", typeof(a))\n", "b = collect(a) # renvoie le vecteur des valeurs\n", "println(\"b = \",b)\n", "println(\"type de b : \", typeof(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## type array" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = [1 2 3], b = [1, 2, 3], c = [1.0 2.0 3.0; 1.0 2.0 3.0]\n", "type de a = Array{Int64,2}, type de b = Array{Int64,1}, type de c = Array{Float64,2}\n" ] } ], "source": [ "a = [1 2 3]\n", "b = [1,2,3]\n", "c = [1.0 2 3 ; 1 2 3]\n", "println(\"a = $a, b = $b, c = $c\")\n", "b_type = typeof(b)\n", "a_type = typeof(a)\n", "c_type = typeof(c)\n", "println(\"type de a = $a_type, type de b = $b_type, type de c = $c_type\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- a est une matrice d'entier à 1 ligne, 3 colonnes\n", "- b est un vecteur d'entier\n", "- c est une matrice de flottants 2 lignes, 3 colonnes\n", "Que se passe-t-il si on calcul : `a*b, b*a, c*b, c*a`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Array{Int64,1}:\n", " 14" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a*b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 1 2 3\n", " 2 4 6\n", " 3 6 9" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b*a" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "DimensionMismatch", "evalue": "DimensionMismatch(\"matrix A has dimensions (2,3), matrix B has dimensions (1,3)\")", "output_type": "error", "traceback": [ "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" ] } ], "source": [ "c*a" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 14.0\n", " 14.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c*b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b est donc un vecteur colonne" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Opérations terme à terme\n", "On a bien sur les opétations classique : + * et /, l'étoile \n", "\\* est la multiplication matricielle pais on a aussi les opérations terme à terme" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 2.0 4.0 6.0\n", " 2.0 4.0 6.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = 2*ones(2,3)\n", "c .* d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Attention au .** Étant donné que le point . permet à la fois de définir un float et d'effectuer\n", "les opérations élément par élément, il faut mettre des espaces entre les points afin de pouvoir distinguer\n", "la signification de chaque point." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 6.0\n", " 7.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5. .+ [1,2]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "syntax: invalid syntax \"5.+\"; add space(s) to clarify", "output_type": "error", "traceback": [ "syntax: invalid syntax \"5.+\"; add space(s) to clarify", "" ] } ], "source": [ "5.+[1,2]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "E = 1:4\n", "type de E : UnitRange{Int64}\n" ] } ], "source": [ "E = 1:4\n", "println(\"E = \",E)\n", "println(\"type de E : \",typeof(E))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On peut extraire des sous matrice facilement, mais attention " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 1 2\n", " 9 10" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "E = [1 2 3 4 ; 5 6 7 8 ; 9 10 11 12]\n", "E[[1,3],1:2]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "ArgumentError", "evalue": "ArgumentError: invalid index: UnitRange{Int64}[1:2] of type Array{UnitRange{Int64},1}", "output_type": "error", "traceback": [ "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" ] } ], "source": [ "E[[1,3],[1:2]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les tuples\n", "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", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(12, 13, \"quatorze\")" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = 12, 13, \"quatorze\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"quatorze\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[3]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Tuple{Int64,Int64,String}" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(t)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching setindex!(::Tuple{Int64,Int64,String}, ::Int64, ::Int64)", "output_type": "error", "traceback": [ "MethodError: no method matching setindex!(::Tuple{Int64,Int64,String}, ::Int64, ::Int64)", "", "Stacktrace:", " [1] top-level scope at In[22]:1" ] } ], "source": [ "t[2]=1 # erreur car non modifiable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Autres types de base\n", "* les chaînes de caractères;\n", "* les dictionnaires;\n", "* les ensembles.\n", "\n", "On peut aussi créer des types : structures, ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fonctions\n", "**Les paramètres de type scalaire ou tuple sont en entrée**" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Avant fct2\n", "objectid(a) = 9949443806018717880, objectid(b) = 11967854120867199718\n", "a, b = 0, 1\n", "Dans fct2\n", "objectid(a) = 9949443806018717880, objectid(b) = 11967854120867199718\n", "objectid(a) = 9949443806018717880, objectid(b) = 9949443806018717880\n", "Après fct2\n", "objectid(a) = 9949443806018717880, objectid(b) = 11967854120867199718\n", "a, b, c = 0,1,0\n" ] } ], "source": [ "function fct2(a,b)\n", " a_id = objectid(a)\n", " b_id = objectid(b)\n", " println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", " b = a\n", " a_id = objectid(a)\n", " b_id = objectid(b)\n", " println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", " return b\n", "end\n", "a = 0\n", "b = 1\n", "a_id = objectid(a)\n", "b_id = objectid(b)\n", "println(\"Avant fct2\")\n", "println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", "println(\"a, b = $a, $b\")\n", "println(\"Dans fct2\")\n", "c = fct2(a,b)\n", "println(\"Après fct2\")\n", "a_id = objectid(a)\n", "b_id = objectid(b)\n", "println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", "println(\"a, b, c = $a,$b,$c\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type Array\n", "\n", "**Les paramètres sont passés par référence**" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Avant fct3\n", "objectid(a) = 15031927814252516001, objectid(b) = 13102773869306322552\n", "a, b = [0, 0], [1, 1]\n", "Dans fct3\n", "objectid(a) = 15031927814252516001, objectid(b) = 13102773869306322552\n", "objectid(a) = 15031927814252516001, objectid(b) = 13102773869306322552\n", "a, b = [0, 0], [0, 0]\n", "Après fct3\n", "objectid(a) = 15031927814252516001, objectid(b) = 13102773869306322552, objectid(c) = 13102773869306322552\n", "a, b, c = [0, 0], [0, 0], [0, 0]\n", "b = [10, 0], c = [10, 0]\n" ] } ], "source": [ "function fct3(a,b)\n", " a_id = objectid(a)\n", " b_id = objectid(b)\n", " println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", " b[1] = a[1]\n", " b[2] = a[2]\n", " a_id = objectid(a)\n", " b_id = objectid(b)\n", " println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", " println(\"a, b = $a, $b\")\n", " return b \n", "end\n", "a = [0,0]\n", "b = [1,1]\n", "a_id = objectid(a)\n", "b_id = objectid(b)\n", "println(\"Avant fct3\")\n", "println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", "println(\"a, b = $a, $b\")\n", "println(\"Dans fct3\")\n", "c = fct3(a,b)\n", "println(\"Après fct3\")\n", "a_id = objectid(a)\n", "b_id = objectid(b)\n", "c_id = objectid(c)\n", "println(\"objectid(a) = $a_id, objectid(b) = $b_id, objectid(c) = $c_id\")\n", "println(\"a, b, c = $a, $b, $c\")\n", "c[1]=10\n", "println(\"b = $b, c = $c\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`b`et `c`sont les mêmes objects!**\n", "\n", "**`a` et `b` sont les mêmes objets**" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Avant fct3_1\n", "objectid(a) = 6846767276606612758, objectid(b) = 16616932192875253576\n", "a, b = [0, 0], [1, 1]\n", "Dans fct3_1\n", "objectid(a) = 6846767276606612758, objectid(b) = 16616932192875253576\n", "objectid(a) = 6846767276606612758, objectid(b) = 16616932192875253576\n", "a, b = [0, 0], [0, 0]\n", "Après fct3_1\n", "objectid(a) = 6846767276606612758, objectid(b) = 16616932192875253576, objectid(c) = 208917430185405003\n", "a, b, c = [0, 0], [0, 0], [0, 0]\n", "b = [0, 0], c = [10, 0]\n" ] } ], "source": [ "function fct3_1(a,b)\n", " a_id = objectid(a)\n", " b_id = objectid(b)\n", " println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", " b[1] = a[1]\n", " b[2] = a[2]\n", " a_id = objectid(a)\n", " b_id = objectid(b)\n", " println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", " println(\"a, b = $a, $b\")\n", " return b[:] # on renvoie un nouvel objet \n", "end\n", "a = [0,0]\n", "b = [1,1]\n", "a_id = objectid(a)\n", "b_id = objectid(b)\n", "println(\"Avant fct3_1\")\n", "println(\"objectid(a) = $a_id, objectid(b) = $b_id\")\n", "println(\"a, b = $a, $b\")\n", "println(\"Dans fct3_1\")\n", "c = fct3_1(a,b)\n", "println(\"Après fct3_1\")\n", "a_id = objectid(a)\n", "b_id = objectid(b)\n", "c_id = objectid(c)\n", "println(\"objectid(a) = $a_id, objectid(b) = $b_id, objectid(c) = $c_id\")\n", "println(\"a, b, c = $a, $b, $c\")\n", "c[1]=10\n", "println(\"b = $b, c = $c\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`b`et `c`sont différents**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- le contenu du vecteur b a été modifié;\n", "- le pointeur sur le vecteur b est le même." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`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", "metadata": {}, "source": [ "## Arguments\n", "- arguments optionnels;\n", "- vectorisation : `sin.([0,\\pi/2,\\pi])`;\n", "- `Maps`et `Filters`(programmation fonctionnelle);\n", "- récursivité." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number\n", "\tComplex\n", "\tReal\n", "\t\tAbstractFloat\n", "\t\t\tBigFloat\n", "\t\t\tFloat16\n", "\t\t\tFloat32\n", "\t\t\tFloat64\n", "\t\tAbstractIrrational\n", "\t\t\tIrrational\n", "\t\tInteger\n", "\t\t\tBool\n", "\t\t\tSigned\n", "\t\t\t\tBigInt\n", "\t\t\t\tInt128\n", "\t\t\t\tInt16\n", "\t\t\t\tInt32\n", "\t\t\t\tInt64\n", "\t\t\t\tInt8\n", "\t\t\tUnsigned\n", "\t\t\t\tUInt128\n", "\t\t\t\tUInt16\n", "\t\t\t\tUInt32\n", "\t\t\t\tUInt64\n", "\t\t\t\tUInt8\n", "\t\tRational\n" ] } ], "source": [ " function showtypetree(T, level=0)\n", " println(\"\\t\" ^ level, T)\n", " for t in subtypes(T)\n", " if t != Any\n", " showtypetree(t, level+1)\n", " end\n", " end\n", " end\n", " \n", " showtypetree(Number)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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": [ "# vecteur\n", "A = Array{Real}(undef,3)\n", "B = Vector{Real}(undef,4)\n", "A_type = typeof(A)\n", "B_type = typeof(B)\n", "println(\"type de A = $A_type, type de B = $B_type\")\n", "\n", "println(A)\n", "A_type == B_type # renvoie true\n", "#\n", "# Matrices\n", "A = Array{Real}(undef,2,4)\n", "B = Matrix{Real}(undef,3,4)\n", "A_type = typeof(A)\n", "B_type = typeof(B)\n", "println(\"type de A = $A_type, type de B = $B_type\")\n", "A_type == B_type # renvoie true " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Int<:Real" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Int<:AbstractFloat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Types dans les fonctions\n", "### Paramètres en entrée\n", "\n", "Si les paramètres ne sont pas du bon type alors le programme plante" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "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": [ "function fct5(a::Int, b::Int)\n", " return a+b\n", "end\n", "f1 = fct5(2.,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Paramètre en sortie" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "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": [ "function fct6!(a::Real ,x::Vector)\n", "#function fct(a::Real ,x::Vector, y::Vector)\n", " a = \"a\" # a is a new variable\n", " x = [2,2] # x is new variable\n", " return a, x\n", "end\n", "\n", "function fct7!(a::Real ,x::Vector)::Tuple{Int,Vector}\n", " a = \"a\" # a is a new variable\n", " x = [2,2] # x is new variable\n", " return a, x\n", "end\n", "\n", "println(\"Main Program, test of fct6!\")\n", "println(\"--------------------------\")\n", "a = 1\n", "x = [0, 0]\n", "f_a, f_x = fct6!(a, x)\n", "println(\"a, x = $a, $x\")\n", "println(\"f_a, f_x = $f_a, $f_x\")\n", "\n", "println(\"Main Program, test of fct7!\")\n", "println(\"--------------------------\")\n", "a = 1\n", "x = [0, 0]\n", "f_a, f_x = fct7!(a, x)\n", "println(\"a, x, = $a, $x\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dispatch multiple" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f1 = 4, f2 = 0.0\n" ] } ], "source": [ "function fct5(a::Real, b::Int)\n", " return a-b\n", "end\n", "f1 = fct5(2,2)\n", "f2 = fct5(2.0,2)\n", "println(\"f1 = $f1, f2 = $f2\")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 2 methods for generic function fct5: