{ "metadata": { "kernelspec": { "codemirror_mode": "julia", "display_name": "IJulia", "language": "julia", "name": "julia" }, "language": "Julia", "name": "", "signature": "sha256:309e1b3ef2f60d19e040236f4a169ce79ae4add9f10ecad7c3788681b4f7c93f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "An IJulia Preview" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is a preview demo of **IJulia**: a [Julia-language](http://julialang.org/) backend combined with the [IPython](http://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." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basic Julia interaction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basic mathematical expressions work like you expect:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "1 + sin(3)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "s = 0.0\n", "for n = 1:2:10000\n", " s += 1/n - 1/(n+1)\n", "end\n", "s # an expression on the last line (if it doesn't end with \";\") is printed as \"Out\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "Out[2] - log(2)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "R = rand(500,500)\n", "R' * R" ], "language": "python", "metadata": {}, "outputs": [] }, { "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?)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tab completion and introspection work" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ran" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Julia\u2013Python interoperability: SciPy and Matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia can easily and transparently call external Python code using a package called [PyCall](https://github.com/stevengj/PyCall.jl), and to illustrate that capability we will show some examples calling [SciPy](http://www.scipy.org/) and [Matplotlib](http://matplotlib.org/) from Julia.\n", "\n", "For example, we can use the [Newton solver in scipy.optimize](http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html) to solve a transcendental equation $\\cos(x) - x = 0$ given a *Julia* function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "using PyCall\n", "@pyimport scipy.optimize as so\n", "so.newton(x -> cos(x) - x, 1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the same `@pyimport` syntax to import Matplotlib (specifically, the `matplotlib.pyplot` module), but to integrate Matplotlib's graphics with the IJulia display requires a little more work. To simplify this, we've created a [PyPlot module](https://github.com/stevengj/PyPlot.jl) for Julia:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "using PyPlot\n", "x = linspace(0,2*pi,1000)\n", "y = sin(3*x + 4*cos(2*x))\n", "plot(x, y, color=\"red\", linewidth=2.0, linestyle=\"--\")\n", "ylabel(\"the y axis\")\n", "xlabel(\"the x axis\")\n", "title(\"a sinusoidally-modulated sinusoid\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that, by default, the plots are displayed inline (just as for the `%pylab inline` \"magic\" in IPython). This kind of multimedia display can be enabled for *any* Julia object, as explained in the next section." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Multimedia display in IJulia" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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", "collapsed": false, "input": [ "display(\"text/html\", \"\"\"Hello world in HTML!\"\"\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "type HTML\n", " s::String\n", "end\n", "import Base.writemime\n", "writemime(io::IO, ::MIME\"text/html\", x::HTML) = print(io, x.s)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, `writemime` 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 `writemime` definition is all that we need to make any object of type `HTML` display automatically as HTML text in IJulia:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = HTML(\"\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "display(x)\n", "println(x)" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }