
| Current Path : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/Notebooks/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/Notebooks/Function Wizardy.ipynb |
{
"metadata": {
"language": "Julia",
"name": "",
"signature": "sha256:73b6f1897ee9392d29656c9ce8b1ce2b6c7a28958df580ca24c749ea831ab9e9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"function bitcount(a::Integer)\n",
" sum = 0\n",
" for i = 1:(sizeof(a)*8) - leading_zeros(a)\n",
" sum += (abs(a)>>i)&0x1\n",
" end\n",
" return sum\n",
"end"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 55,
"text": [
"bitcount (generic function with 1 method)"
]
}
],
"prompt_number": 55
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitcount(-1234)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 56,
"text": [
"5"
]
}
],
"prompt_number": 56
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# We're going to create a function, sumf(func, x), that computes\n",
"# sum(func(x)) with func applied to each element of the array x.\n",
"# The cool part is that we won't create a temporary array y = func(x),\n",
"# as we would if we relied on func being vectorized.\n",
" \n",
"# Here's a function we'll use for our testing\n",
"sinc_plus_x(x) = sin(x)/x + x\n",
" \n",
"# For comparison, here's the hand-written version of sumf(sinc_plus_x, x)\n",
"function sum_sinc_plus_x(x::AbstractArray)\n",
" s = 0.0\n",
" for xs in x\n",
" s += sinc_plus_x(xs)\n",
" end\n",
" s\n",
"end\n",
" \n",
"# Here's the general version. It generates a separate function\n",
"# for each supplied user function, and stores each generated function\n",
"# for future use.\n",
"global sumf\n",
"let _sumf_method_cache = Dict() # keep this private\n",
"function sumf(func::Function, x::AbstractArray)\n",
" if !haskey(_sumf_method_cache, func)\n",
" # If we haven't generated the function yet, do so now and store the result\n",
" F = Expr(:quote, func)\n",
" f = eval(quote\n",
" local _sumf_ # to keep this from being defined externally\n",
" function _sumf_(x::AbstractArray)\n",
" s = 0.0\n",
" for xs in x\n",
" s += ($F)(xs)\n",
" end\n",
" s\n",
" end\n",
" end)\n",
" _sumf_method_cache[func] = f\n",
" else\n",
" # Use the previously-defined version\n",
" f = _sumf_method_cache[func]\n",
" end\n",
" f(x) # Evaluate the function on our arguments\n",
"end\n",
"end # end of let\n",
" \n",
"x = rand(10^7)\n",
"println(\"Testing the hand-written version\")\n",
"@time sum_sinc_plus_x(x)\n",
"gc() # so garbage-collection doesn't influence the results\n",
"@time vmanual = sum_sinc_plus_x(x)\n",
" \n",
"println(\"Testing the generated version\")\n",
"@time sumf(sinc_plus_x, x)\n",
"gc()\n",
"@time vgen = sumf(sinc_plus_x, x)\n",
"@assert vmanual == vgen"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Testing the hand-written version\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"elapsed time: "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0.273156132 seconds (156172 bytes allocated)\n",
"elapsed time: "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0.254177949 seconds (112 bytes allocated)\n",
"Testing the generated version\n",
"elapsed time: "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0.363040016 seconds (1939628 bytes allocated)\n",
"elapsed time: "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0.256776295 seconds (1560 bytes allocated)\n"
]
}
],
"prompt_number": 58
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = rand(5)\n",
"b = zeros(5,5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 59,
"text": [
"5x5 Array{Float64,2}:\n",
" 0.0 0.0 0.0 0.0 0.0\n",
" 0.0 0.0 0.0 0.0 0.0\n",
" 0.0 0.0 0.0 0.0 0.0\n",
" 0.0 0.0 0.0 0.0 0.0\n",
" 0.0 0.0 0.0 0.0 0.0"
]
}
],
"prompt_number": 59
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"b += Diagonal(a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 60,
"text": [
"5x5 Array{Float64,2}:\n",
" 0.527211 0.0 0.0 0.0 0.0 \n",
" 0.0 0.421915 0.0 0.0 0.0 \n",
" 0.0 0.0 0.392379 0.0 0.0 \n",
" 0.0 0.0 0.0 0.301467 0.0 \n",
" 0.0 0.0 0.0 0.0 0.919195"
]
}
],
"prompt_number": 60
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}