Julia

From David's Wiki
Revision as of 20:38, 17 September 2019 by David (talk | contribs) (Change activate("./") to activate(@__DIR__))
\( \newcommand{\P}[]{\unicode{xB6}} \newcommand{\AA}[]{\unicode{x212B}} \newcommand{\empty}[]{\emptyset} \newcommand{\O}[]{\emptyset} \newcommand{\Alpha}[]{Α} \newcommand{\Beta}[]{Β} \newcommand{\Epsilon}[]{Ε} \newcommand{\Iota}[]{Ι} \newcommand{\Kappa}[]{Κ} \newcommand{\Rho}[]{Ρ} \newcommand{\Tau}[]{Τ} \newcommand{\Zeta}[]{Ζ} \newcommand{\Mu}[]{\unicode{x039C}} \newcommand{\Chi}[]{Χ} \newcommand{\Eta}[]{\unicode{x0397}} \newcommand{\Nu}[]{\unicode{x039D}} \newcommand{\Omicron}[]{\unicode{x039F}} \DeclareMathOperator{\sgn}{sgn} \def\oiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x222F}\,}{\unicode{x222F}}{\unicode{x222F}}{\unicode{x222F}}}\,}\nolimits} \def\oiiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x2230}\,}{\unicode{x2230}}{\unicode{x2230}}{\unicode{x2230}}}\,}\nolimits} \)
Julia
ParadigmMulti-paradigm: multiple dispatch (core), procedural, functional, meta, multistaged[1]
Designed byJeff Bezanson, Alan Edelman, Stefan Karpinski, Viral B. Shah
DeveloperJeff Bezanson, Stefan Karpinski, Viral B. Shah, and other contributors[2][3]
First appeared2012; 12 years ago (2012)[4]
Stable release
1.2.0[5] / 20 August 2019; 5 years ago (2019-08-20)
Preview release
1.3.0-rc2[6] / 12 September 2019; 5 years ago (2019-09-12) / 1.4.0-DEV with daily updates
Typing disciplineDynamic, nominative, parametric, optional
Implementation languageJulia, C, C++, Scheme, LLVM[7]
PlatformTier 1: x86-64, IA-32, CUDA
Tier 2: ARM (both 32- and 64-bit) and Tier 3: PowerPC
Also has web browser support (for JavaScript and WebAssembly)[8]
OSLinux, macOS, Windows and FreeBSD
LicenseMIT (core),[2] GPL v2;[7][9] a makefile option omits GPL libraries[10]
Filename extensions.jl
WebsiteJuliaLang.org
Influenced by

Julia is a numerical computation and general purpose high-level programming language. It's standout feature is its performance which allows libraries to be written in Julia directly. In contrast, many libraries in R and Python are written in C or C++ for performance purposes and accessed using Rcpp or Cython. If necessary, you can still interface with other languages. See Introducing Julia for a comprehensive guide on how to program in Julia.


Installation

Juno

Juno is an IDE for Julia. It consists of a set of packages added to Atom. Most items can be accessed from the Atom control palette Ctrl + Shift + P
Shortcuts:

  • Ctrl + Enter Evaluate current selection
  • Shift + Enter Evaluate current section and jump to next.

Basic Usage

Package Management

Guide

Initializing a new project

cd project_folder
julia
] activate ./
# Add your packages

Loading an existing project

cd project_folder
julia
using Pkg;
Pkg.activate(@__DIR__);
Pkg.instantiate();

Updating an existing project

If the dependencies in Project.toml have been updated, you will need to run Pkg.resolve() to install new packages and update your local Manifest.toml

using Pkg;
Pkg.activate(@__DIR__);
Pkg.resolve();

To update the your dependencies, run Pkg.update()

using Pkg;
Pkg.activate(@__DIR__);
Pkg.update();

String Interpolation

"Variable x is $x, y is $y, and x+y is $(x+y)"

Other Pkg Commands

You can see all the commands by typing ? in the package manager.

  • update [packagename] Update a specific package or all packages
  • remove [packagename] Remove a package
  • status Show the current Pkg directory and installed packages

Arrays

[Full details]

// Make an 1d array of Float64.
// Equivalent to [0.0, 0.0, 0.0]
myArr = zeros(4);

// Make a 1d array of Float64 left to uninitialized values
myArr = Array{Float64,1}(undef, 3)

// Convert an iterable to an array with collect
myArr = collect(1:3)

// Reference copy
a = [1, 2, 3];
b = [4, 5, 6];
b = a

// Shallow copy
a = [1, 2, 3];
b = [4, 5, 6];
// elementwise equals operator
b .= a
// or b[:] = a
// or b = copy(a) (this will discard the original b)

// Basic functionality
// Call functions elementwise by adding . after the function name
a = [1, 5, 99];
a = clamp.(a, 1, 11);
// a is now [1, 5, 11]
// Equivalent to clamp!(a, 1, 11)

// Julia also has map, foldl, foldr, reduce

Higher order functions

Julia supports high-order functions.

ts = ((a, b) -> (c, d) -> a + b + c + d)(1,2);

The call with arguments (1,2) returns a function.
Then ts is equivalent to

ts = (c, d) -> 1 + 2 + c + d;

You can also use the full function(a,b) syntax.

Animation Loop

You can use Timer(callback, delay, interval). This is similar to SetInterval in JavaScript.
End the loop with close(animate).

animate = Timer(function(t)
    println("Animating")
end, 0; interval=1/60)


Graphics

You can use MeshCat.jl to create visualizations to view in a web browser.
These visualizations are powered by WebGL using three.js.
Note that MeshCat.jl only exposes a small subset of three.js's features so I would not recommend it for creating highly intricate or interactive experiences at the moment.
MeshCat.jl is built using WebIO.jl.

Machine Learning


Object Oriented Programming

Julia is a Multiple Dispatch language.
You can use a struct in place of classes and define functions which have your struct as a parameter in place of methods.
For example:

using Parameters
# @with_kw allows default values
# By default, structs are immutable. Add the mutable keyword.
@with_kw mutable struct Person
  name
  age
end

# Outer constructor
function Person(name::String, age::Int)
  # Do anything here
  # Return a new Person
  Person(name = name, age = age)
end

Useful functions:

# Returns the type
typeof("str")
# Check type
"str" isa String
# Get the super type
supertype(Int64)
# Check if a is a subtype of b with <:
# This is the same symbol for creating a subtype of an abstract type
# E.g. mutable struct myNum <: Number
Int64 <: Number

Useful Packages

MeshCat.jl

Link
A wrapper for graphics visualizations around three.js. This opens in a web browser but can be used for making helium apps. See Graphics in Julia for more details on how to use MeshCat.jl.

Flux

Link
Flux is the largest machine learning library for Julia. It includes convenience features including NN layers, activation functions, and an automatic differentiation system. See Machine Learning in Julia for more details on how to use Flux.


Latexify.jl

Link
Prints out a latex string for any Julia expression or value.
Very useful for printing out matrices to insert into Latex documents.


References

<templatestyles src="Reflist/styles.css" />

  1. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"Smoothing data with Julia's @generated functions". 5 November 2015. Retrieved 9 December 2015. Julia's generated functions are closely related to the multistaged programming (MSP) paradigm popularized by Taha and Sheard, which generalizes the compile time/run time stages of program execution by allowing for multiple stages of delayed code execution.
  2. 2.0 2.1 <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"LICENSE.md". GitHub.
  3. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"Contributors to JuliaLang/julia". GitHub.
  4. 4.0 4.1 4.2 4.3 4.4 4.5 4.6 Cite error: Invalid <ref> tag; no text was provided for refs named announcement
  5. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"v1.2.0". Github.com. 2019-08-20. Retrieved 2019-08-20.
  6. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"v1.3.0-rc2". GitHub. Retrieved 2019-09-13.
  7. 7.0 7.1 <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"Julia". Julia. NumFocus project. Retrieved 9 December 2016. Julia's Base library, largely written in Julia itself, also integrates mature, best-of-breed open source C and Fortran libraries for ...
  8. Cite error: Invalid <ref> tag; no text was provided for refs named WebAssembly
  9. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"Non-GPL Julia?". Groups.google.com. Retrieved 2017-05-31.
  10. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"Introduce USE_GPL_LIBS Makefile flag to build Julia without GPL libraries". Note that this commit does not remove GPL utilities such as git and busybox that are included in the Julia binary installers on Mac and Windows. It allows building from source with no GPL library dependencies.
  11. 11.0 11.1 11.2 11.3 <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"Home · The Julia Language". docs.julialang.org. Retrieved 2018-08-15.
  12. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"Programming Language Network". GitHub. Retrieved 6 December 2016.
  13. <templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>"JuliaCon 2016". JuliaCon. Retrieved 6 December 2016. He has co-designed the programming language Scheme, which has greatly influenced the design of Julia