AI教程网 - 未来以来,拥抱AI;新手入门,从AI教程网开始......

Julia 开始

Julia教程 AI君 56℃

开始

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

输入 ^Dctrl 键加 d 键,或者输入 quit() ,可以退出交互式会话。交互式模式下, julia 会显示一个横幅,并提示用户来输入。一旦用户输入了完整的表达式,例如 1 + 2 ,然后按回车,交互式会话就对表达式求值并返回这个值。如果输入的表达式末尾有分号,就不会显示它的值了。变量 ans 的值就是上一次计算的表达式的值,无论上一次是否被显示。变量 ans 仅适用于交互式会话,不适用于以其它方式运行的 Julia 代码。

如果想运行写在源文件 file.jl 中的代码,可以输入命令 include("file.jl")

要在非交互式模式下运行代码,你可以把它当做 Julia 命令行的第一个参数:

$ julia script.jl arg1 arg2...

如这个例子所示,julia 后面跟着的命令行参数,被认为是程序 script.jl 的命令行参数。这些参数使用全局变量 ARGS 来传递。使用 -e 选项,也可以在命令行设置 ARGS 参数。可如下操作,来打印传递的参数:

$ 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 可以用 -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 有各种可选项:

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

资源

除了本手册,还有一些其它的资源:

转载请注明:www.ainoob.cn » Julia 开始

喜欢 (0)or分享 (0)