Julia: Difference between revisions

No edit summary
No edit summary
 
(33 intermediate revisions by the same user not shown)
Line 1: Line 1:
__FORCETOC__


==Basic Usage==
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 (programming language)]] or [[C++]] for performance purposes and accessed using Rcpp or Cython.
If necessary, you can still interface with other languages.
See [https://en.wikibooks.org/wiki/Introducing_Julia Introducing Julia] for a comprehensive guide on how to program in Julia.
 
 
==Installation==
 
===Juno===
[https://junolab.org 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 <code>Ctrl + Shift + P</code><br>
Shortcuts:
* <code>Ctrl + Enter</code> Evaluate current selection/section.
* <code>Shift + Enter</code> Evaluate current selection/section and jump to next.
 
==Usage==
===Package Management===
===Package Management===
[https://www.simonwenkel.com/2018/10/06/a-brief-introduction-to-package-management-with-julia.html Guide]
[https://www.simonwenkel.com/2018/10/06/a-brief-introduction-to-package-management-with-julia.html Guide]
Initializing a new project
====Initializing a new project====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
cd project_folder
cd project_folder
Line 12: Line 27:
</syntaxhighlight>
</syntaxhighlight>


Loading an existing project
====Loading an existing project====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
cd project_folder
cd project_folder
julia
julia
using Pkg;
using Pkg;
Pkg.activate("./");
Pkg.activate(@__DIR__);
Pkg.instantiate();
Pkg.instantiate();
</syntaxhighlight>
</syntaxhighlight>
===String Interpolation===
 
====Updating an existing project====
If the dependencies in <code>Project.toml</code> have been updated, you will need to run
<code>Pkg.resolve()</code> to install new packages and update your local <code>Manifest.toml</code>
<syntaxhighlight lang="bash">
using Pkg;
Pkg.activate(@__DIR__);
Pkg.resolve();
</syntaxhighlight>
 
To update the your dependencies, run <code>Pkg.update()</code>
<syntaxhighlight lang="bash">
using Pkg;
Pkg.activate(@__DIR__);
Pkg.update();
</syntaxhighlight>
 
====Other Pkg Commands====
You can see all the commands by typing <code>?</code> in the package manager.
* <code>add [packagename]</code> Add a package
* <code>update [packagename]</code> Update a specific package or all packages
* <code>remove [packagename]</code> Remove a package
* <code>status</code> Show the current Pkg directory and installed packages
 
===Basics===
====Functions====
You can chain functions using the pipe operator <code>|></code>. This is similar to <code>%>%</code> in [[R]] or <code>|></code> in [[F_sharp| F#]]
<syntaxhighlight lang="julia>
a = [1,2,3];
// Equivalent to sum(a)
b = a |> sum
 
</syntaxhighlight>
====Strings====
=====String Interpolation=====
<syntaxhighlight lang="julia">
<syntaxhighlight lang="julia">
"Variable x is $x, y is $y, and x+y is $(x+y)"
"Variable x is $x, y is $y, and x+y is $(x+y)"
</syntaxhighlight>
====String Concatenation====
Julia uses <code>*</code> to concatenate strings.
You will get an error if you use <code>+</code>.
<syntaxhighlight lang="julia">
"a"*"b"
</syntaxhighlight>
====Arrays====
[[https://en.wikibooks.org/wiki/Introducing_Julia/Arrays_and_tuples Full details]]
<syntaxhighlight lang="julia">
// 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 a collection to an array with collect
myArr = collect(1:0.5:3)
// myArr == [1.0,1.5,2.0,2.5,3.0]
// 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
</syntaxhighlight>
</syntaxhighlight>


Line 38: Line 132:


===Animation Loop===
===Animation Loop===
You can use <code>Timer(callback, delay, interval)</code>. This is similar to SetInterval in JavaScript.
You can use [https://docs.julialang.org/en/v1/base/base/#Base.Timer-Tuple{Function,Real} <code>Timer(callback, delay, interval)</code>]. This is similar to SetInterval in JavaScript.<br>
End the loop with [https://docs.julialang.org/en/v1/base/io-network/#Base.close <code>close(animate)</code>].
<syntaxhighlight lang="julia">
<syntaxhighlight lang="julia">
animate = Timer(function(t)
animate = Timer(function(t)
Line 46: Line 141:




==Graphics==
{{Main|Graphics in Julia}}
You can use MeshCat.jl to create visualizations to view in a web browser.<br>
These visualizations are powered by WebGL using three.js.<br>
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.<br>
MeshCat.jl is built using [https://github.com/JuliaGizmos/WebIO.jl WebIO.jl].<br>
==Machine Learning==
{{Main|Machine Learning in Julia}}
See [[Flux]] and [[Knet.jl]]
==Object Oriented Programming==
Julia is a [https://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch] language.<br>
You can use a <code>struct</code> in place of classes and define methods which use your struct as a parameter.<br>
You can find all definitions of a function using the <code>methods</code> function.
For example:
<syntaxhighlight lang="julia">
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
</syntaxhighlight>
Useful functions:
<pre>
# 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
</pre>


==Useful Packages==
==Useful Packages==
===MeshCat.jl===
===MeshCat.jl===
[https://github.com/rdeits/MeshCat.jl Link]<br>
[https://github.com/rdeits/MeshCat.jl Link]<br>
A wrapper for graphics visualizations around three.js. This opens in a web browser but can be used for making helium apps.
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===
[https://fluxml.ai Link]<br>
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===
[https://github.com/korsbo/Latexify.jl Link]<br>
Prints out a latex string for any Julia expression or value.<br>
Very useful for printing out matrices to insert into Latex documents.
 
==References==
{{Reflist}}