Julia 的安装,不管是使用编译好的程序,还是自己从源代码编译,都很简单。按照 这儿 的说明下载并安装即可。
使用交互式会话(也记为 repl),是学习 Julia 最简单的方法:
$ julia |
_ |
_ _ _(_)_ | A fresh approach to technical computing |
(_) | (_) (_) | Documentation: http://docs.julialang.org |
_ _ _| |_ __ _ | Type "help()" to list help topics |
| | | | | | |/ _` | | |
| | |_| | | | (_| | | Version 0.3.0-prerelease+3690 (2014-06-16 05:11 UTC) |
_/ |\__'_|_|_|\__'_| | Commit 1b73f04* (0 days old master) |
|__/ | x86_64-apple-darwin13.1.0 |
julia> 1 + 2 |
3 |
julia> ans |
3 |
输入 ^D
— ctrl
键加 d
键,或者输入 quit()
,可以退出交互式会话。交互式模式下, julia
会显示一个横幅,并提示用户来输入。一旦用户输入了完整的表达式,例如 1 + 2
,然后按回车,交互式会话就对表达式求值并返回这个值。如果输入的表达式末尾有分号,就不会显示它的值了。变量 ans
的值就是上一次计算的表达式的值,无论上一次是否被显示。变量 ans
仅适用于交互式会话,不适用于以其它方式运行的 Julia 代码。
如果想运行写在源文件 file.jl 中的代码,可以输入命令 include("file.jl")
。
要在非交互式模式下运行代码,你可以把它当做 Julia 命令行的第一个参数:
$ 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 |
-p
或 --machinefile
选项来开启并行模式。 -p n
会发起额外的 n 个工作进程,而 --machinefile file
会为文件 file 的每一行发起一个工作进程。 file 定义的机器,必须要能经由无密码的 ssh 访问,且每个机器上的 Julia 安装的位置应完全相同,每个机器的定义为 [user@]host[:port] [bind_addr]
。 user
默认为当前的用户,port
默认为标准 ssh 端口。可选择的,万一是多网主机,bind_addr
可被用来精确指定接口。
如果你想让 Julia 在启动时运行一些代码,可以将代码放入 ~/.juliarc.jl
:
$ echo 'println("Greetings! 你好! 안녕하세요?")' > ~/.juliarc.jl |
$ julia |
Greetings! 你好! 안녕하세요? |
... |
julia [options] [program] [args...] |
-v, --version Display version information |
-h, --help Print this message |
-q, --quiet Quiet startup without banner |
-H, --home <dir> Set location of julia executable |
-e, --eval <expr> Evaluate <expr> |
-E, --print <expr> Evaluate and show <expr> |
-P, --post-boot <expr> Evaluate <expr> right after boot |
-L, --load <file> Load <file> right after boot on all processors |
-J, --sysimage <file> Start up with the given system image file |
-p <n> Run n local processes |
--machinefile <file> Run processes on hosts listed in <file> |
-i Force isinteractive() to be true |
--no-history-file Don't load or save history |
-f, --no-startup Don't load ~/.juliarc.jl |
-F Load ~/.juliarc.jl, then handle remaining inputs |
--color={yes|no} Enable or disable color text |
--code-coverage Count executions of source lines |
--check-bounds={yes|no} Emit bounds checks always or never (ignoring declarations) |
--int-literals={32|64} Select integer literal size independent of platform |