# Getting started with Julia ```julia; echo=false; results="hidden" using CalculusWithJulia using CalculusWithJulia.WeaveSupport using Plots nothing ``` Julia is a freely available, open-source programming language aimed at technical computing. As it is open source, indeed with a liberal MIT license, it can be installed for free on many types of computers (though not phones or tablets). [lauch binder](https://mybinder.org/v2/gh/CalculusWithJulia/CwJScratchPad.git/master) ### Installing Julia locally There is the service [Binder](https://mybinder.org), which provides a web-based interface to `Julia` built around `Jupyter`, a wildly succesful platform for interacting with different open-source software programs. Clicking the launch button above will open a web page which provides a blank notebook, save for a package used by these notes. However, `Binder` is nowhere near as reliable as a local installation. Installing `Julia` locally is not more difficult than installing other softward. Binaries of `Julia` are provided at [julialang.org](http://julialang.org/downloads/). Julia has an official released version and a developmental version. Unless there is a compelling reason, the latest released version should be downloaded and installed for use. For Windows users, there is a `juliaup` program for managing the installation of Julia. The base `Julia` provides a *command-line interface*, or REPL (read-evaluate-parse). ### Basic interactive usage Once installed, `Julia` can be started by clicking on an icon or typing `julia` at the command line. Either will open a *command line interface* for a user to interact with a `Julia` process. The basic workflow is easy: commands are typed then sent to a `Julia` process when the "return" key is pressed for a complete expression. Then the output is displayed. A command is typed following the *prompt*. An example might be `2 + 2`. To send the command to the `Julia` interpreter the "return" key is pressed. A complete expression or expressions will then be parsed and evaluated (executed). If the expression is not complete, `julia`'s prompt will still accept input to complete the expression. Type `2 +` to see. (The expression `2 +` is not complete, as the infix operator `+` expects two arguments, one on its left and one on its right.) ```julia; eval=false _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.7.0 (2021-11-30) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia> 2 + 2 4 ``` Above, `julia>` is the prompt. These notes will not include the prompt, so that copying-and-pasting can be more easily used. Input and output cells display similarly, though with differences in coloring. For example: ```julia; 2 + 2 ``` Other interfaces to `Julia` are described briefly in [Julia interfaces](./julia_interfaces.html). The notebook interface provided through `IJulia` most closely matches the style of the notes. ## Add-on packages `Julia` has well over a 1000 external, add-on packages that enhance the offerings of base `Julia`. We refer to one, `CalculusWithJulia`, that is designed to accompany these notes. This package installs several other packages that provide the needed functionality. The package (and its dependencies) can be installed through: ```julia; eval=false using Pkg Pkg.add("CalculusWithJulia") ``` (Or the one liner `] add CalculusWithJulia`. Some additional details on packages is provided [here](./calculus_with_julia.html).) Installation only needs to be done once, but to use a package it must be loaded into each new session. This can be done with this command: ```julia; using CalculusWithJulia ``` ## The basics of working with Pluto The `Pluto` notebook interface is a Julia-specific interface that is started by: ```julia; eval=false using Pluto Pluto.run() ``` These commands will cause a tab in a web browser to open to `Pluto`'s landing page. From here a new or existing notebook can be opened. Notebooks can even be opened from urls or from Pluto-generated HTML pages, such as some of the sections of these notes. Pluto cells are reactive, so modifications of a variable in the top-level scope will propagate to all references of that variable. This is very useful for explorations, but does require unique variable names in the top-level scope. (Names can be reused in functions and `let` blocks.) A Pluto cell holds a single command, which may be a block holding multiple statements. Cells are executed, typically, by typing "shift-enter". The output of the cell appears *above* the cell. ## The basics of working with IJulia The **very** basics of the Jupyter notebook interface provided by `IJulia` are covered here. An `IJulia` notebook is made up of cells. Within a cell a collection of commands may be typed (one or more). When a cell is executed (by the triangle icon or under the Cell menu) the contents of the cell are evaluated by the `Julia` kernel and any output is displayed below the cell. Typically this is just the output of the last command. ```julia; 2 + 2 3 + 3 ``` If the last commands are separated by commas, then a "tuple" will be formed and each output will be displayed, separated by commas. ```julia; 2 + 2, 3 + 3 ``` Comments can be made in a cell. Anything after a `#` will be ignored. ```julia; 2 + 2 # this is a comment, you can ignore me... ``` Graphics are provided by external packages. There is no built-in graphing. We use the `Plots` package and its default backend. The `Plots` package provides a common interface to several different backends, so this choice is easily changed. The `pyplot` backend is used in these notes, though for interactive use both the `GR` and `Plotly` backends have advantages. ```julia; using CalculusWithJulia using Plots ``` ```julia; echo=false pyplot() ``` With that in hand, to make a graph of a function over a range, we follow this pattern: ```julia; plot(sin, 0, 2pi) ``` A few things: * Cells are numbered in the order they were evaluated. * This order need not be from top to bottom of the notebook. * The evaluation of a cell happens within the state of the workspace, which depends on what was evaluated earlier. * The workspace can be cleared by the "Restart" menu item under "Kernel". After restarting the "Run All" menu item under "Cell" can be used to re-run all the commands in the notebook - from top to bottom. "Run all" will also reload any packages.