Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active July 4, 2020 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genkuroki/3ccd6f6e294d35d0d319f5df5a641851 to your computer and use it in GitHub Desktop.
Save genkuroki/3ccd6f6e294d35d0d319f5df5a641851 to your computer and use it in GitHub Desktop.
OffserArrays and evalpoly
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using OffsetArrays\n\nA = OffsetArray([0.1, 2.3, 4.5, 6.7, 8.9], -2:2)\n\n@show A[-2]\n@show A[-1]\n@show A[0]\n@show A[1]\n@show A[2];",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "A[-2] = 0.1\nA[-1] = 2.3\nA[0] = 4.5\nA[1] = 6.7\nA[2] = 8.9\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show A[begin]\n@show A[end];",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "A[begin] = 0.1\nA[end] = 8.9\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "binom(x, k) = iszero(k) ? one(x) : prod((x-j+1)/j for j in k:-1:1)\n@show a = 0.5\n@show L = 1000\nc = OffsetArray(binom.(a, 0:L), 0:L) # binomial coefficients for the exponent a\n@show c[0]\n@show c[1]\n@show c[2]\n@show c[3]\n@show c[4]\n@show c[5]\n@show c[L];",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "a = 0.5 = 0.5\nL = 1000 = 1000\nc[0] = 1.0\nc[1] = 0.5\nc[2] = -0.125\nc[3] = 0.0625\nc[4] = -0.0390625\nc[5] = 0.02734375\nc[L] = -8.923967556705516e-6\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(x; c=c) = sum(c[k]*(x-1)^k for k in eachindex(c)) # Taylor expansion of √x at x = 1\nf(1.9), √1.9",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "(1.3784048752090234, 1.378404875209022)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "evalpoly(1.9 - 1, parent(c))",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "1.378404875209022"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@benchmark evalpoly(1.9 - 1, $(parent(c)))",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 1.493 μs (0.00% GC)\n median time: 1.540 μs (0.00% GC)\n mean time: 1.561 μs (0.00% GC)\n maximum time: 15.256 μs (0.00% GC)\n --------------\n samples: 10000\n evals/sample: 10"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@benchmark f(1.9; c=$c)",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 48 bytes\n allocs estimate: 2\n --------------\n minimum time: 89.107 μs (0.00% GC)\n median time: 89.574 μs (0.00% GC)\n mean time: 92.975 μs (0.00% GC)\n maximum time: 247.729 μs (0.00% GC)\n --------------\n samples: 10000\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.4",
"display_name": "Julia 1.4.2",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.4.2"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "3ccd6f6e294d35d0d319f5df5a641851",
"data": {
"description": "OffserArrays and evalpoly",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/3ccd6f6e294d35d0d319f5df5a641851"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment