{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# What's the purpose of Jupyt-nb ?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyt-nb is designed to study Mathematical and Physical Constants properties of ratios as detailed in http://vixra.org/pdf/1904.0218v4.pdf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "-\n", "It uses 2 Julia packages:\n", "\n", "* MathPhysicalConstants.SI\n", "\n", "* PhysicalConstant.CODATA2019\n", "\n", "it can be linked to numpy using PyCall\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reference for Continuous Improvement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[](https://travis-ci.org/LaGuer/Jupyt-Nb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reference for offline visualization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[](https://nbviewer.jupyter.org/github/LaGuer/Jupyt-Nb/blob/master/Julia%20Jupiter-Notebook%20Constants%20in%20Cosmology.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pkg.clone(\"https://github.com/laguer/MathPhysicalConstants.jl\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Julia Version 0.6.4\n", "Commit 9d11f62bcb (2018-07-09 19:09 UTC)\n", "Platform Info:\n", " OS: Linux (x86_64-pc-linux-gnu)\n", " CPU: Intel(R) Xeon(R) CPU @ 2.20GHz\n", " WORD_SIZE: 64\n", " BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=16)\n", " LAPACK: libopenblas64_\n", " LIBM: libopenlibm\n", " LLVM: libLLVM-3.9.1 (ORCJIT, broadwell)\n" ] } ], "source": [ "versioninfo()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mInitializing package repository /srv/julia/pkg/v0.6\n", "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mPackage directory /srv/julia/pkg/v0.6 is already initialized.\n", "\u001b[39m" ] } ], "source": [ "Pkg.init()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mPackage Unitful is already installed\n", "\u001b[39m" ] } ], "source": [ "Pkg.add(\"Unitful\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#import Pkg; \n", "Pkg.status()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Pkg.clone(\"https://github.com/laguer/PhysicalConstant.jl.git\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mPackage PhysicalConstant is already installed\n", "\u001b[39m" ] } ], "source": [ "Pkg.add(\"PhysicalConstant\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pkg.dir(\"PhysicalConstant\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "using Unitful\n", "using Measurements\n", "using MathPhysicalConstants.SI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's experiment MathPhysConstants by looking at the reduced electron Compton wavelength\n", "Basic formula is given by ƛ_e ≡ ħ/m_e.c\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.861592046068737688702740558503376825155847784672545991504355454736855198364958e-13" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "big(SI.PlanckConstantHbar)/(big(SI.MassElectron)*big(SI.SpeedOfLight))\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.370359991390000118371972348541021347045898437500000000000000000000000000000000e+02" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "big(SI.SanchezElectrConstant)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$1.05457162825e-34 \\pm 0.0$" ], "text/plain": [ "1.05457162825e-34 ± 0.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "measurement(SI.PlanckConstantHbar)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$1.054571628249999983241389640919049903556150033962677651299952084762545619284539e-34 \\pm 0.000000000000000000000000000000000000000000000000000000000000000000000000000000$" ], "text/plain": [ "1.054571628249999983241389640919049903556150033962677651299952084762545619284539e-34 ± 0.000000000000000000000000000000000000000000000000000000000000000000000000000000" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "big(measurement(SI.PlanckConstantHbar))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "using Unitful\n", "using Measurements\n", "using MathPhysicalConstants.SI" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.814279104760214220732450485229492187500000000000000000000000000000000000000000e+06" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1 = big(e^e^e) " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.764865179158049315959156489518625436695868996929735868921423813247391069467881e+17" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1^e" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "137.035999139" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = SI.SanchezElectrConstant" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# $ln137/ln(a/137) ≈ (2 + 135/d_e)^2$" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18726.168436946326" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Unitful\n", "using Measurements\n", "using Cosmology\n", "using PyCall\n", "@pyimport numpy as np\n", "\n", "np.log(137)/ np.log(a/137)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# $exp(2^{e^{2+1/2}})$ ≈ $exp(e^{2e} + e^2)$ = 8.820166056085355e102" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "721.178003510984" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Unitful\n", "using Measurements\n", "using Cosmology\n", "using PyCall\n", "@pyimport numpy as np\n", "@pyimport sympy as sy\n", "@pyimport decimal\n", "\n", "#np.exp(2^e^2.5)\n", "np.exp(2^e)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Inf" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp(2^(e^2.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# $exp(e^{2e} + e^2)$" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8.820166056085355e102" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Unitful\n", "using Measurements\n", "using Cosmology\n", "using PyCall\n", "@pyimport numpy as np\n", "@pyimport sympy as sy\n", "@pyimport decimal\n", "\n", "# decimal.Decimal(np.exp(2^e^2.5))\n", "decimal.Decimal(np.exp(e^(2*e)+e^2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The economic number : $e^{e^e}$" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.8142791047601975e6\n" ] } ], "source": [ "using Unitful\n", "using Measurements\n", "using Cosmology\n", "using PyCall\n", "@pyimport numpy as np\n", "@pyimport sympy as sy\n", "@pyimport scipy as sci\n", "@pyimport matplotlib.pyplot as plt\n", "@pyimport decimal\n", "println(np.e^np.e^np.e)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wien Reduced Constant\n", "\n", "* See https://en.wikipedia.org/wiki/Wien%27s_displacement_law" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\omega = hc/kTλ_{Wien} $" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NIST tells b=2.897 7729 x 10-3" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.9651126128756795" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(SI.PlanckConstantH*SI.SpeedOfLight)/(SI.Boltzmann * 1 * 2.8977729e-3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$f(26) ≈ 6(2π^2a^3)^5$, where $2π^2a^3$ is the area of a 4-sphere of radius a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793\n" ] } ], "source": [ "using Unitful\n", "using Measurements\n", "using Cosmology\n", "using PyCall\n", "@pyimport math\n", "println(math.pi)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is a preview demo of **IJulia**: a [Julia-language](https://julialang.org/) backend combined with the [IPython](https://ipython.org/) interactive environment. This combination allows you to interact with the Julia language using IPython's powerful [graphical notebook](http://ipython.org/notebook.html), which combines code, formatted text, math, and multimedia in a single document.\n", "\n", "* Note: this is a *preview*, because it relies on pre-release bleeding-edge versions of Julia, IPython, and several Julia packages, as explained on the [IJulia github page](https://github.com/JuliaLang/IJulia.jl), and functionality is evolving rapidly. We hope to have a more polished release soon.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Julia interaction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basic mathematical expressions work like you expect:" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5113.984999751927" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = e\n", "exp(i*pi)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "$$ \\exp(i \\pi)$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can define variables, write loops, and execute arbitrary multiline code blocks. Here is an example of an alternating harmonic series $\\sum_{n=1}^\\infty \\frac{(-1)^n}{n}$ from a [Julia tutorial by Homer Reid](http://homerreid.ath.cx/teaching/18.330/JuliaProgramming.shtml#SimplePrograms):" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6930971830599458" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sum=0.0\n", "for n = 1:2:10000\n", " Sum += 1/n - 1/(n+1)\n", "end\n", "Sum # an expression on the last line (if it doesn't end with \";\") is printed as \"Out\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Previous outputs can be referred to via `Out[`*n*`]`, following the IPython, for example `Out[2]` for the result above. You can also use the shorthand `_2`, or `_` for the previous result, as in IPython. Like in Matlab, `ans` can also be used to refer to the previous result, *even if it was not printed* (when the command ended with `;`).\n", "\n", "For example, the harmonic series above should be converging (slowly) to $\\ln 2$, and we can check this:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-4.9997499999454575e-5" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Out[26] - log(2)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.8877084059945687e55\n" ] } ], "source": [ "println(math.e^128-1)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using PhysConsts\n", "α.quantity\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "big((α.value)^-1)\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "big(log((α.value)^-1))\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "big(math.exp(699))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Pkg; Pkg.status()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "using PhysConsts.DATA" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Speed of light in vacuum (c)\n", "Value = 2.99792458e8 m s^-1\n", "Standard uncertainty = (exact)\n", "Relative standard uncertainty = (exact)\n", "Reference = CODATA 2019" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.quantity" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Bohr radius (a0)\n", "Value = 5.2917721067e-11 m\n", "Standard uncertainty = 1.2e-20 m\n", "Relative standard uncertainty = 2.3e-10\n", "Reference = CODATA 2019" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a0.value" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Newtonian constant of gravitation (Gg)\n", "Value = 6.67408e-11 m^3 kg^-1 s^-2\n", "Standard uncertainty = 3.1e-15 m^3 kg^-1 s^-2\n", "Relative standard uncertainty = 4.6e-5\n", "Reference = CODATA 2019" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G.value" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Proton mass (m_p)\n", "Value = 1.672621898e-27 kg\n", "Standard uncertainty = 2.1e-35 kg\n", "Relative standard uncertainty = 1.3e-8\n", "Reference = CODATA 2019" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g.quantity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like Matlab or Scipy + Numpy, Julia has lots of mathematical functions and linear algebra built in. For example, we can define a $500\\times500$ random matrix $R$ and form the positive-definite matrix $R^* R$:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50×50 Array{Float64,2}:\n", " 14.8401 11.0159 11.0398 12.1371 12.9914 … 10.0666 10.2719 11.5613\n", " 11.0159 14.5508 10.6115 12.8797 13.3489 9.09651 11.5845 12.8904\n", " 11.0398 10.6115 14.664 12.1591 12.6995 11.3192 11.2392 12.7697\n", " 12.1371 12.8797 12.1591 18.0848 15.3832 10.9588 12.952 12.7643\n", " 12.9914 13.3489 12.6995 15.3832 18.8978 11.3417 13.766 13.7376\n", " 10.3582 12.1922 10.8784 12.5355 14.0756 … 9.75837 11.2291 12.7569\n", " 11.403 11.4861 12.1963 12.413 13.4622 11.4271 12.3969 12.9923\n", " 11.9348 11.3753 11.5956 13.6172 13.1759 10.7935 11.2115 12.2721\n", " 12.2734 12.3439 11.7942 14.8864 14.2113 11.7685 12.4404 12.9881\n", " 12.611 12.6692 12.6645 14.3057 15.212 12.4616 12.4764 13.661 \n", " 12.46 10.9597 11.9727 12.0599 12.9428 … 11.0595 11.6294 13.2982\n", " 10.7607 12.462 11.4551 12.9311 14.3571 10.629 12.2371 14.6529\n", " 11.5589 12.314 12.3473 12.9978 13.8131 10.5067 12.1406 13.694 \n", " ⋮ ⋱ \n", " 11.3496 11.8048 11.2844 13.7931 13.7687 11.7626 12.937 12.6045\n", " 11.0356 12.8355 11.1324 13.4014 13.5355 9.83385 11.9688 12.6278\n", " 12.3802 14.0015 12.4311 13.7834 14.4353 … 12.0277 12.6047 14.6398\n", " 11.4193 12.8167 12.8603 12.5936 14.013 10.8855 12.3162 14.082 \n", " 11.9126 11.3802 10.747 12.2219 12.3567 10.434 10.8304 12.1167\n", " 11.8378 12.2769 11.7172 14.8255 14.3169 11.4519 13.129 13.469 \n", " 12.6271 12.8352 12.74 13.0265 15.1861 12.46 13.2996 15.0121\n", " 12.1822 13.1224 11.2227 13.8452 14.8181 … 9.94071 12.3738 13.4795\n", " 11.6848 10.9105 10.2393 11.041 12.3348 9.44363 9.89868 10.8772\n", " 10.0666 9.09651 11.3192 10.9588 11.3417 14.6154 10.5976 10.1157\n", " 10.2719 11.5845 11.2392 12.952 13.766 10.5976 16.6939 12.2948\n", " 11.5613 12.8904 12.7697 12.7643 13.7376 10.1157 12.2948 18.5953" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R = rand(50,50)\n", "R' * R" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Notice that, by default, only a portion of a large matrix is shown. You didn't really want to read $500^2 = 250,000$ numbers, did you?)\n", "\n", "Standard output from Julia is also captured and sent to the IJulia notebook as you'd expect:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Testing, some unicode output on stderr...\n", "\n" ] } ], "source": [ "println(\"Hello world!\\n\")\n", "println(STDERR, \"Testing, some unicode output on stderr...\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IJulia even captures output from external C libraries (and notice how easy it is using Julia's `ccall` intrinsic):" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello from C!!\n" ] } ], "source": [ "ccall(:printf, Cint, (Ptr{UInt8},), \"Hello from C!!\\n\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can define functions, of course, and use them in later input cells:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Unitful\n", "using Measurements\n", "using Cosmology\n", "using PyCall\n", "\n", "@pyimport math\n", "@pyimport numpy as np\n", "f(x) = np.power(np.log(2), (x / 4 ))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8325546111576977\n" ] }, { "data": { "text/plain": [ "6-element Array{Float64,1}:\n", " 0.904122\n", " 0.895875\n", " 0.832555\n", " 0.75966 \n", " 0.632458\n", " 0.480453" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Unitful\n", "using Measurements\n", "using Cosmology\n", "using PyCall\n", "@pyimport math\n", "@pyimport numpy as np\n", "println(f(2))\n", "f([1.1,1.2,2,3,5,8])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the input above both printed an scalar to `STDOUT` and also returned a vector, the latter using Julia's ability to write polymorphic functions and built-in array operations.\n", "\n", "On the other hand adding a string to a number is not defined (there is no `+` method defined for those types, although we could easily add one), and attempting to do so will throw an exception:" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# f(\"Hello?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive Plotting in IJulia\n", "\n", "Below we'll show off some plotting using the excellent Gadfly package. The plots are heavily inspired by [this blog post](http://int8.io/basic-visualization-in-julia-gadfly/).\n", "\n", "✨✨✨✨✨\n", "\n", "* **NOTE**: This will take a while to compile the first time you import packages. If you know a bit of Julia and have a good solution for how Binder can pre-compile packages *before* the user runs them, let us know in the repo2docker Julia issue here: https://github.com/jupyter/repo2docker/issues/23.\n", "\n", "✨✨✨✨✨" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using RDatasets\n", "sleep = dataset(\"lme4\",\"sleepstudy\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Gadfly\n", "plot(sleep, x = \"Days\", y = \"Reaction\", Geom.point)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot(sleep, x = \"Reaction\", Geom.density, color = \"Subject\", Scale.x_continuous(minvalue= 0, maxvalue= 500))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot(x = sleep[:Reaction], Geom.histogram(bincount = 30), Scale.x_continuous(minvalue = 200), color = sleep[:Days])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multimedia display in IJulia" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like most programming languages, Julia has a built-in `print(x)` function for outputting an object `x` as text, and you can override the resulting text representation of a user-defined type by overloading Julia's `show` function. The next version of Julia, however, will extend this to a more general mechanism to display **arbitrary multimedia representations** of objects, as defined by standard [MIME types](https://en.wikipedia.org/wiki/Internet_media_type). More specifically, the Julia [multimedia I/O API](http://docs.julialang.org/en/latest/stdlib/base/#multimedia-i-o) provides:\n", "\n", "* A `display(x)` function requests the richest available multimedia display of a Julia object x (with a `text/plain` fallback).\n", "* Overloading `writemime` allows one to indicate arbitrary multimedia representations (keyed by standard MIME types) of user-defined types.\n", "* Multimedia-capable display backends may be registered by subclassing a generic `Display` type. IJulia provides one such backend which, thanks to the IPython notebook, is capable of displaying HTML, LaTeX, SVG, PNG, and JPEG media formats.\n", "\n", "The last two points are critical, because they separate **multimedia export** (which is defined by functions associated with the originating Julia data) from **multimedia display** (defined by backends which know nothing about the source of the data).\n", "\n", "Precisely these mechanism were used to create the inline PyPlot plots above. To start with, the simplest thing is to provide the MIME type of the data when you call `display`, which allows you to pass \"raw\" data in the corresponding format:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "display(\"text/html\", \"\"\"Hello world in HTML!\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, it will be more common to attach this information to types, so that they display correctly automatically. For example, let's define a simple `HTML` type in Julia that contains a string and automatically displays as HTML (given an HTML-capable backend such as IJulia):" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "show (generic function with 428 methods)" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type HTML\n", " s::String\n", "end\n", "import Base.show\n", "show(io::IO, MIME, x::HTML) = print(io, x.s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, `show` is just a function that writes `x` in the corresponding format (`text/html`) to the I/O stream `io`. The `@MIME` is a bit of magic to allow Julia's [multiple dispatch](http://en.wikipedia.org/wiki/Multiple_dispatch) to automatically select the correct `writemime` function for a given MIME type (here `\"text/html\"`) and object type (here `HTML`). We also needed an `import` statement in order to add new methods to an existing function from another module.\n", "\n", "This `show` definition is all that we need to make any object of type `HTML` display automatically as HTML text in IJulia:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "