Getting Started
The latest version of Julia can be downloaded and installed by following the instructions on the main GitHub page. The easiest way to learn and experiment with Julia is by starting an interactive session (also known as a read-eval-print loop or “repl”):$ julia
_
_ _ _(_)_ |
(_) | (_) (_) | A fresh approach to technical computing.
_ _ _| |_ __ _ |
| | | | | | |/ _` | | Version 0 (pre-release)
| | |_| | | | (_| | | Commit 61847c5aa7 (2011-08-20 06:11:31)*
_/ |\__'_|_|_|\__'_| |
|__/ |
julia> 1 + 2
3
julia> ans
3
julia> load("file.jl")
To run code in a file non-interactively, you can give it as the first argument to the julia command:
$ julia script.jl arg1 arg2...
$ julia -e 'for x in ARGS; println(x); end' foo bar
foo
bar
$ echo 'for x in ARGS; println(x); end' > script.jl
$ julia script.jl foo bar
foo
bar
julia [options] [program] [args...]
-q --quiet Quiet startup without banner
-H --home= Load files relative to
-T --tab= Set REPL tab width to
-e --eval= Evaluate and don't print
-E --print= Evaluate and print
-P --post-boot= Evaluate right after boot
-L --load=file Load right after boot
-J --sysimage=file Start up with the given system image file
-p n Run n local processes
-h --help Print this message
Example Code
At this point it is useful to take a look at some man-example-programs.Major Differences From MATLAB®
Julia’s syntax is intended to be familiar to users of MATLAB®. However, Julia is in no way a MATLAB® clone: there are major syntactic and functional differences. The following are the most significant differences that may trip up Julia users accustomed to MATLAB®:- Arrays are indexed with square brackets, A[i,j].
- Multiple values are returned and assigned with parentheses, return (a, b) and (a, b) = f(x).
- Values are passed and assigned by reference. If a function modifies an array, the changes will be visible in the caller.
- Use n for nx1: The number of arguments to an array constructor equals the number of dimensions of the result. In particular, rand(n) makes a 1-dimensional array.
- Concatenating scalars and arrays with the syntax [x,y,z] concatenates in the first dimension (“vertically”). For the second dimension (“horizontally”), use spaces as in [x y z]. To construct block matrices (concatenating in the first two dimensions), the syntax [a b; c d] is used to avoid confusion.
- Colons a:b and a:b:c construct Range objects. To construct a full vector, use linspace, or “concatenate” the range by enclosing it in brackets, [a:b].
- Functions return values using the return keyword, instead of by listing their names in the function definition (see The “return” Keyword for details).
- A file may contain any number of functions, and all definitions will be externally visible when the file is loaded.
- Reductions such as sum, prod, and max are performed over every element of an array when called with a single argument as in sum(A).
- Functions such as sort that operate column-wise by default (sort(A) is equivalent to sort(A,1)) do not have special behavior for 1xN arrays; the argument is returned unmodified since it still performs sort(A,1). To sort a 1xN matrix like a vector, use sort(A,2).
- Parentheses must be used to call a function with zero arguments, as in tic() and toc().
- Do not use semicolons to end statements. The results of statements are not automatically printed (except at the interactive prompt), and lines of code do not need to end with semicolons. The function println can be used to print a value followed by a newline.
- If A and B are arrays, A == B doesn’t return an array of booleans. Use A .== B instead. Likewise for the other boolean operators, <, >, !=, etc.