From ca2b943a6e2b4437b61a376527fea45a73204d76 Mon Sep 17 00:00:00 2001 From: Peter Arndt Date: Thu, 14 Jul 2016 10:36:16 +0200 Subject: [PATCH] new version --- Introduction.html | 417 ++++++++++++------------------------------- Introduction.ipynb | 430 ++++++++++++++------------------------------- 2 files changed, 246 insertions(+), 601 deletions(-) diff --git a/Introduction.html b/Introduction.html index cbef4d1..e35390a 100644 --- a/Introduction.html +++ b/Introduction.html @@ -12143,10 +12143,10 @@

Varialbles and Types
-
In [19]:
+
In [1]:
-
typeof(ans)
+
12  42
 
@@ -12157,11 +12157,11 @@

Varialbles and Types -
Out[19]:
+
Out[1]:
-
Rational{Int64}
+
true
@@ -12172,10 +12172,10 @@

Varialbles and Types
-
In [20]:
+
In [3]:
-
"Like in perl you may write x=$x"
+
2  [6,3,5]
 
@@ -12186,11 +12186,11 @@

Varialbles and Types -
Out[20]:
+
Out[3]:
-
"Like in perl you may write x=7"
+
false
@@ -12201,10 +12201,10 @@

Varialbles and Types
-
In [21]:
+
In [19]:
- -
-
-
-
-
-

The type hierarchy for numbers looks like: -type hierarchy

-
-
-
-
-
-
-

Functions + Multiple Dispatch

One can define functions simply like that:

-
-
-
In [22]:
+
In [20]:
-
f(x) = x ^ 3
+
"Like in perl you may write x=$x"
 
@@ -12274,39 +12244,11 @@

Functions + Multiple Dispatch -
Out[22]:
+
Out[20]:
-
f (generic function with 1 method)
-
- -
- -
-

- -
-
-
-
In [23]:
-
-
-
methods(f)
-
- -
-
-
- -
-
- - -
Out[23]:
- -
-1 method for generic function f:
  • f(x) at In[22]:1
+
"Like in perl you may write x=7"
@@ -12317,10 +12259,10 @@

Functions + Multiple Dispatch
-
In [24]:
+
In [21]:
-
f(3)
+
"... but also $(exp(x))"
 
@@ -12331,11 +12273,11 @@

Functions + Multiple Dispatch -
Out[24]:
+
Out[21]:
-
27
+
"... but also 1096.6331584284585"
@@ -12344,54 +12286,42 @@

Functions + Multiple Dispatch -
-
In [25]:
-
-
-
f("ha ")
-
- +
+
+
- -
-
- - -
Out[25]:
- - -
-
"ha ha ha "
- +
+
+
+
+

The type hierarchy for numbers looks like: +type hierarchy

-
-

An equivalent syntax used for more complicated function is:

+

Functions + Multiple Dispatch

One can define functions simply like that:

-
In [26]:
+
In [6]:
-
function g(y)
-    println("in function g")
-    y+3
-end
+
f(x) = x ^ 3
 
@@ -12402,11 +12332,11 @@

Functions + Multiple Dispatch -
Out[26]:
+
Out[6]:
-
g (generic function with 1 method)
+
f (generic function with 2 methods)
@@ -12417,10 +12347,10 @@

Functions + Multiple Dispatch
-
In [27]:
+
In [7]:
-
g(3)
+
f(3)
 
@@ -12431,18 +12361,11 @@

Functions + Multiple Dispatch -
-
-
in function g
-
-
-
- -
-
-
-
In [29]:
-
-
-
function g(y::Int)
-    println("in function g(y::Int)")
-    y+1
-end
-
-function g(y::AbstractString, z=15)
-    println("in function g(y::AbstractString)")
-    y * "+$z"
-end
-
- -
-
-
- -
-
- - -
Out[29]:
+
Out[8]:
-
g (generic function with 4 methods)
+
"Hello"
@@ -12532,12 +12412,10 @@

Functions + Multiple Dispatch
-
In [31]:
+
In [28]:
-
x="bla"
-println(typeof(x))
-g(x)
+
function f(x::AbstractString) 
+    println("\nnow in f(x::AbstractString) with x=$x of type $(typeof(x))")
+    "Hello $x"
+end
+
+function f(x::AbstractFloat) 
+    println("\nnow in f(x::AbstractFloat) with x=$x of type $(typeof(x))")
+    x+1.0
+end
 
@@ -12587,19 +12463,11 @@

Functions + Multiple Dispatch -
-
-
ASCIIString
-in function g(y::AbstractString)
-
-
-
- -
Out[31]:
+
Out[28]:
-
"bla+15"
+
f (generic function with 3 methods)
@@ -12610,10 +12478,10 @@

Functions + Multiple Dispatch
-
In [32]:
+
In [29]:
-
g(x,1000)
+
methods(f)
 
@@ -12624,18 +12492,10 @@

Functions + Multiple Dispatch -
-
-
in function g(y::AbstractString)
-
-
-
- -
-
-
In [37]:
+
In [38]:
-
methods(g)
+
methods(print)
 
@@ -12806,10 +12611,10 @@

Multiple Dispatch -
Out[37]:
+
Out[38]:
-4 methods for generic function g:
  • g(y::Int64) at In[29]:2
  • g(y::AbstractString) at In[29]:7
  • g(y::AbstractString, z) at In[29]:7
  • g(y) at In[26]:2
+18 methods for generic function print:
@@ -12820,10 +12625,10 @@

Multiple Dispatch
-
In [38]:
+
In [36]:
-
methods(print)
+
methods(println)
 
@@ -12834,10 +12639,10 @@

Multiple Dispatch -
Out[38]:
+
Out[36]:
-18 methods for generic function print: +2 methods for generic function println:
@@ -13221,7 +13026,7 @@

Arrays

-
In [51]:
+
In [37]:
A=[1,2,9]
@@ -13235,7 +13040,7 @@ 

Arrays

-
Out[51]:
+
Out[37]:
@@ -13253,10 +13058,10 @@

Arrays

-
In [52]:
+
In [38]:
-
A+2
+
A+2.1
 
@@ -13267,14 +13072,14 @@

Arrays

-
Out[52]:
+
Out[38]:
-
3-element Array{Int64,1}:
-  3
-  4
- 11
+
3-element Array{Float64,1}:
+  3.1
+  4.1
+ 11.1
@@ -13285,7 +13090,7 @@

Arrays

-
In [53]:
+
In [41]:
A .== 2
@@ -13299,7 +13104,7 @@ 

Arrays

-
Out[53]:
+
Out[41]:
@@ -13317,7 +13122,7 @@

Arrays

-
In [54]:
+
In [42]:
B=[1 2 3; 6 7 4; 6 2 1]
@@ -13331,7 +13136,7 @@ 

Arrays

-
Out[54]:
+
Out[42]:
@@ -13349,7 +13154,7 @@

Arrays

-
In [55]:
+
In [43]:
C=[ i*j for i in 1:3, j in 1:4 ]
@@ -13363,7 +13168,7 @@ 

Arrays

-
Out[55]:
+
Out[43]:
diff --git a/Introduction.ipynb b/Introduction.ipynb index ff65320..2710d3b 100644 --- a/Introduction.ipynb +++ b/Introduction.ipynb @@ -302,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -310,21 +310,21 @@ { "data": { "text/plain": [ - "Rational{Int64}" + "true" ] }, - "execution_count": 19, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "typeof(ans)" + "12 ≠ 42" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -332,21 +332,21 @@ { "data": { "text/plain": [ - "\"Like in perl you may write x=7\"" + "false" ] }, - "execution_count": 20, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"Like in perl you may write x=$x\"" + "2 ∈ [6,3,5]" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -354,44 +354,21 @@ { "data": { "text/plain": [ - "\"... but also 1096.6331584284585\"" + "Rational{Int64}" ] }, - "execution_count": 21, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"... but also $(exp(x))\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Type Hierarchy " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The type hierarchy for numbers looks like:\n", - "![type hierarchy](type-hierarchy-for-julia-numbers.png)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Functions + Multiple Dispatch\n", - "One can define functions simply like that:" + "typeof(ans)" ] }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -399,98 +376,66 @@ { "data": { "text/plain": [ - "f (generic function with 1 method)" + "\"Like in perl you may write x=7\"" ] }, - "execution_count": 22, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "f(x) = x ^ 3" + "\"Like in perl you may write x=$x\"" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { - "text/html": [ - "1 method for generic function f:
  • f(x) at In[22]:1
" - ], "text/plain": [ - "# 1 method for generic function \"f\":\n", - "f(x) at In[22]:1" + "\"... but also 1096.6331584284585\"" ] }, - "execution_count": 23, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "methods(f)" + "\"... but also $(exp(x))\"" ] }, { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "27" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], + "cell_type": "markdown", + "metadata": {}, "source": [ - "f(3)" + "# Type Hierarchy " ] }, { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "\"ha ha ha \"" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], + "cell_type": "markdown", + "metadata": {}, "source": [ - "f(\"ha \")" + "The type hierarchy for numbers looks like:\n", + "![type hierarchy](type-hierarchy-for-julia-numbers.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "An equivalent syntax used for more complicated function is:" + "# Functions + Multiple Dispatch\n", + "One can define functions simply like that:" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -498,53 +443,43 @@ { "data": { "text/plain": [ - "g (generic function with 1 method)" + "f (generic function with 2 methods)" ] }, - "execution_count": 26, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "function g(y)\n", - " println(\"in function g\")\n", - " y+3\n", - "end" + "f(x) = x ^ 3" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "in function g\n" - ] - }, { "data": { "text/plain": [ - "6" + "27" ] }, - "execution_count": 27, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "g(3)" + "f(3)" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -553,150 +488,106 @@ "name": "stdout", "output_type": "stream", "text": [ - "in function g\n" + "now in f(x::AbstractString)\n" ] }, - { - "ename": "LoadError", - "evalue": "LoadError: MethodError: `+` has no method matching +(::ASCIIString, ::Int64)\nClosest candidates are:\n +(::Any, ::Any, !Matched::Any, !Matched::Any...)\n +(!Matched::Int64, ::Int64)\n +(!Matched::Complex{Bool}, ::Real)\n ...\nwhile loading In[28], in expression starting on line 1", - "output_type": "error", - "traceback": [ - "LoadError: MethodError: `+` has no method matching +(::ASCIIString, ::Int64)\nClosest candidates are:\n +(::Any, ::Any, !Matched::Any, !Matched::Any...)\n +(!Matched::Int64, ::Int64)\n +(!Matched::Complex{Bool}, ::Real)\n ...\nwhile loading In[28], in expression starting on line 1", - "", - " in g at ./In[26]:3" - ] - } - ], - "source": [ - "g(\"bla \")" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ { "data": { "text/plain": [ - "g (generic function with 4 methods)" + "\"Hello\"" ] }, - "execution_count": 29, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "function g(y::Int)\n", - " println(\"in function g(y::Int)\")\n", - " y+1\n", - "end\n", - "\n", - "function g(y::AbstractString, z=15)\n", - " println(\"in function g(y::AbstractString)\")\n", - " y * \"+$z\"\n", - "end" + "f(\"ha \")" ] }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "typeof(x) = Int64\n", - "in function g(y::Int)\n" + "ename": "LoadError", + "evalue": "LoadError: UndefVarError: Float not defined\nwhile loading In[15], in expression starting on line 1", + "output_type": "error", + "traceback": [ + "LoadError: UndefVarError: Float not defined\nwhile loading In[15], in expression starting on line 1", + "" ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "x=2\n", - "@show typeof(x)\n", - "g(x)" + "super(Float)" ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ASCIIString\n", - "in function g(y::AbstractString)\n" - ] - }, { "data": { "text/plain": [ - "\"bla+15\"" + "f (generic function with 3 methods)" ] }, - "execution_count": 31, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x=\"bla\"\n", - "println(typeof(x))\n", - "g(x)" + "function f(x::AbstractString) \n", + " println(\"\\nnow in f(x::AbstractString) with x=$x of type $(typeof(x))\")\n", + " \"Hello $x\"\n", + "end\n", + "\n", + "function f(x::AbstractFloat) \n", + " println(\"\\nnow in f(x::AbstractFloat) with x=$x of type $(typeof(x))\")\n", + " x+1.0\n", + "end" ] }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "in function g(y::AbstractString)\n" - ] - }, { "data": { + "text/html": [ + "3 methods for generic function f:
  • f(x::AbstractString) at In[28]:2
  • f(x::AbstractFloat) at In[28]:7
  • f(x) at In[6]:1
" + ], "text/plain": [ - "\"bla+1000\"" + "# 3 methods for generic function \"f\":\n", + "f(x::AbstractString) at In[28]:2\n", + "f(x::AbstractFloat) at In[28]:7\n", + "f(x) at In[6]:1" ] }, - "execution_count": 32, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "g(x,1000)" + "methods(f)" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 32, "metadata": { "collapsed": false }, @@ -705,26 +596,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "UTF8String\n", - "in function g(y::AbstractString)\n" + "\n", + "now in f(x::AbstractFloat) with x=2.5 of type Float64\n", + "f(2.5) = 3.5\n" ] - }, - { - "data": { - "text/plain": [ - "\"λ+15\"" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "x=\"λ\"\n", - "α = 7\n", - "println(typeof(x))\n", - "g(x)" + "@show f(2.5);" ] }, { @@ -735,18 +614,17 @@ }, "outputs": [ { - "data": { - "text/plain": [ - "π = 3.1415926535897..." - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "now in f(x::AbstractFloat) with x=2.5 of type Float16\n", + "f(Float16(2.5)) = 3.5\n" + ] } ], "source": [ - "π" + "@show f(Float16(2.5));" ] }, { @@ -755,88 +633,23 @@ "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "AbstractString" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "super(typeof(x))" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Float64\n", - "in function g\n" + "\n", + "now in f(x::AbstractString) with x=abc of type ASCIIString\n", + "f(\"abc\") = \"Hello abc\"\n", + "\n", + "now in f(x::AbstractString) with x=αβγ of type UTF8String\n", + "f(\"αβγ\") = \"Hello αβγ\"\n" ] - }, - { - "data": { - "text/plain": [ - "5.7" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "x=2.7\n", - "println(typeof(x))\n", - "g(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Multiple Dispatch" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/html": [ - "4 methods for generic function g:
  • g(y::Int64) at In[29]:2
  • g(y::AbstractString) at In[29]:7
  • g(y::AbstractString, z) at In[29]:7
  • g(y) at In[26]:2
" - ], - "text/plain": [ - "# 4 methods for generic function \"g\":\n", - "g(y::Int64) at In[29]:2\n", - "g(y::AbstractString) at In[29]:7\n", - "g(y::AbstractString, z) at In[29]:7\n", - "g(y) at In[26]:2" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "methods(g)" + "@show f(\"abc\")\n", + "@show f(\"αβγ\");" ] }, { @@ -882,6 +695,33 @@ "methods(print)" ] }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "2 methods for generic function println:" + ], + "text/plain": [ + "# 2 methods for generic function \"println\":\n", + "println(io::IO, xs...) at strings/io.jl:25\n", + "println(xs...) at strings/io.jl:28" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "methods(println)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1191,7 +1031,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 37, "metadata": { "collapsed": false }, @@ -1205,7 +1045,7 @@ " 9" ] }, - "execution_count": 51, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -1216,7 +1056,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 38, "metadata": { "collapsed": false }, @@ -1224,24 +1064,24 @@ { "data": { "text/plain": [ - "3-element Array{Int64,1}:\n", - " 3\n", - " 4\n", - " 11" + "3-element Array{Float64,1}:\n", + " 3.1\n", + " 4.1\n", + " 11.1" ] }, - "execution_count": 52, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "A+2" + "A+2.1" ] }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 41, "metadata": { "collapsed": false }, @@ -1255,7 +1095,7 @@ " false" ] }, - "execution_count": 53, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -1266,7 +1106,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 42, "metadata": { "collapsed": false }, @@ -1280,7 +1120,7 @@ " 6 2 1" ] }, - "execution_count": 54, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -1291,7 +1131,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 43, "metadata": { "collapsed": false }, @@ -1305,7 +1145,7 @@ " 3 6 9 12" ] }, - "execution_count": 55, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" }