{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Demo script for Julia language\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Interaction & Plotting"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.024"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# In Julia, calculations can be written very comfortable\n",
"n = 10\n",
"(2^n - 10^(0.3*n)) / 10^(0.3*n)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using IJulia\n",
"using Gadfly\n",
"using RDatasets"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# This is how additional packages can be imported\n",
"# import Pkg\n",
"# Pkg.add(\"RDatasets\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"iris = dataset(\"datasets\", \"iris\");"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Warning: `getindex(df::DataFrame, col_ind::ColumnIndex)` is deprecated, use `df[!, col_ind]` instead.\n",
"│ caller = evalmapping(::DataFrame, ::Symbol) at dataframes.jl:96\n",
"└ @ Gadfly /opt/julia/packages/Gadfly/1wgcD/src/dataframes.jl:96\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(iris, x=:SepalLength, y=:PetalWidth, color=:Species, Geom.point)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python $\\leftrightarrow$ Julia interoperability\n",
"\n",
"## Julia Kernel in Python notebook\n",
"\n",
"To use the `%%julia` magic in a notebook with another kernel, add `julia` in the defaults in the file `/opt/conda/lib/python3.7/site-packages/IPython/core/magics/script.py` (using nano in a terminal).\n",
"\n",
"Like here:\n",
"```python\n",
"defaults = [\n",
" 'sh',\n",
" 'bash',\n",
" 'perl',\n",
" 'ruby',\n",
" 'python',\n",
" 'python2',\n",
" 'python3',\n",
" 'pypy',\n",
" 'julia', # add the julia interpreter\n",
"]\n",
"```\n",
"\n",
"Then restart the kernal and use the julia kernel in a non-julia notebook with:\n",
"\n",
"```python\n",
"%%julia\n",
"rfib(n) = n < 2 ? n : rfib(n-1) + rfib(n-2)\n",
"rfib(12)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python Modules in Julia\n",
"\n",
"**Important: This funcionality is still in progress, see https://github.com/JuliaPy**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `/opt/julia/registries/General`\n",
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n",
"\u001b[?25l\u001b[2K\u001b[?25h\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `/opt/julia/environments/v1.1/Project.toml`\n",
"\u001b[90m [no changes]\u001b[39m\n",
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `/opt/julia/environments/v1.1/Manifest.toml`\n",
"\u001b[90m [no changes]\u001b[39m\n"
]
}
],
"source": [
"# Install if not already done\n",
"import Pkg; Pkg.add(\"PyCall\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"using PyCall"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PyObject "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np = pyimport(\"numpy\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Did it work?\n",
" -> true\n"
]
}
],
"source": [
"println(\"Did it work?\\n -> \", np.sin(np.pi/2) == 1.0)"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.1.0",
"language": "julia",
"name": "julia-1.1"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}