Julia: Difference between revisions
No edit summary |
No edit summary |
||
Line 33: | Line 33: | ||
<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> | |||
===Arrays=== | |||
{{main|https://en.wikibooks.org/wiki/Introducing_Julia/Arrays_and_tuples}} | |||
<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 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 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 17:47, 11 September 2019
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 selectionShift + 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("./");
Pkg.instantiate();
String Interpolation
"Variable x is $x, y is $y, and x+y is $(x+y)"
Arrays
// 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.
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 this.