本文共 851字,閱讀大約需要 3分鐘 液样!
概 述
Julia 是一個 “全新”的高性能動態(tài)編程語言键兜,前兩天迎來了其 1.0 正式版的重大更新。Julia集 Python映挂、C泽篮、R、Ruby 之所長袖肥,感覺就像一種腳本語言咪辱,并且對交互式使用有很好的支持。而且其天生的高性能椎组、通用性與專業(yè)性使得其非常適用于科學(xué)數(shù)值計算油狂、機器學(xué)習(xí)項目等前沿場景。我看完這個消息以后也迫不及待想嘗試一下。
注: 本文原載于 My Personal Blog:专筷, CodeSheep · 程序羊 弱贼!
本文內(nèi)容腦圖如下:
Julia的特性
- 高性能:Julia 通過 LLVM 為多個平臺編譯高效本地代碼,性能很高
- 動態(tài)性:編程范式靈活磷蛹,代碼信噪比極高
- 通用性:易于表達OOP和函數(shù)式編程范式吮旅,并且其標準庫提供異步I / O,進程控制味咳,日志記錄庇勃,概要分析,包管理器等槽驶。
- 專業(yè)性:擅長數(shù)值計算责嚷,支持許多數(shù)值數(shù)據(jù)類型,并且提供開箱即用的并行性掂铐。
- 可組合性:Julia 的包之間可以很好地協(xié)同工作罕拂。
正是由于這些特性,才使其應(yīng)用場景寬泛全陨,而且都是當下前沿熱門應(yīng)用場景:
編程環(huán)境支持
Julia通過提供了一系列插件支持爆班,從而可以在大多數(shù)常見的編輯器中進行編程,具體包括
- Atom
- VS Code
- Jupyter
- Vim
- Emacs
- SublimeText
Julia安裝和部署
Julia 提供了各種平臺和環(huán)境的安裝包辱姨,具體可以去其官網(wǎng)進行下載:
安裝非常簡單柿菩,像 Windows平臺,基本上只需要點按下一步即可安裝到位雨涛,而 MacOS平臺使用 brew包管理器也僅需 一行命令 即可完成安裝碗旅。
下面我以 Linux CentOS 7.4 平臺為例,介紹一些其安裝過程:
CentOS7 上 Julia 安裝也無需復(fù)雜的過程镜悉,只需要下載對應(yīng)的可執(zhí)行版本祟辟,并置于系統(tǒng)的命令路徑中即可愉快的使用:
wget https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.0.0-linux-x86_64.tar.gz
tar zxvf julia-1.0.0-linux-x86_64.tar.gz
cd /usr/bin
ln -s /root/julia-1.0.0/bin/julia
此時執(zhí)行 julia
命令即可啟動 Julia控制臺,順便來向世界問一下好吧:
下面做一些上手實驗侣肄,大致來感受一下該語言精煉旧困、靈活的風格。即使不使用任何文字說明稼锅,也能很容易地理解各個命令的含義吼具,這也說明該語言很好上手。
Julia上手體驗
- 變量操作
julia> x=10
10
julia> x+1
11
julia> x^2
100
julia> pi
π = 3.1415926535897...
julia> sqrt(100)
10.0
julia> ~123
-124
julia> 123 & 234
106
julia> ~UInt32(123)
0xffffff84
julia> [1,2,3] .^ 3
3-element Array{Int64,1}:
1
8
27
julia> 1 == 1
true
julia> NaN == NaN
false
julia> NaN != NaN
true
julia>
julia> 'x'
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)
julia> Int('x')
120
julia> str = "Hello, world.\n"
"Hello, world.\n"
julia> str[1]
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)
julia> str[end]
'\n': ASCII/Unicode U+000a (category Cc: Other, control)
julia> str[4:9]
"lo, wo"
julia> greet = "Hello"
"Hello"
julia> whom = "world"
"world"
julia> "$greet, $whom.\n"
"Hello, world.\n"
julia> findfirst(isequal('x'), "xylophone")
1
julia> findnext(isequal('o'), "xylophone", 1)
4
julia> findnext(isequal('o'), "xylophone", 5)
7
julia> findnext(isequal('o'), "xylophone", 8)
julia> occursin("world", "Hello, world.")
true
julia> repeat(".:Z:.", 10)
".:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:."
julia> occursin(r"^\s*(?:#|$)", "not a comment")
false
julia> occursin(r"^\s*(?:#|$)", "# a comment")
true
julia> match(r"^\s*(?:#|$)", "# a comment")
RegexMatch("#")
- 類型轉(zhuǎn)換和提升
julia> x = 12
12
julia> typeof(x)
Int64
julia> convert(UInt8, x)
0x0c
julia> convert(AbstractFloat, x)
12.0
julia> a = Any[1 2 3; 4 5 6]
2×3 Array{Any,2}:
1 2 3
4 5 6
julia> convert(Array{Float64}, a)
2×3 Array{Float64,2}:
1.0 2.0 3.0
4.0 5.0 6.0
julia> promote(1, 2.5)
(1.0, 2.5)
julia> promote(2, 3//4)
(2//1, 3//4)
julia> promote(1.5, im)
(1.5 + 0.0im, 0.0 + 1.0im)
julia> promote(1 + 2im, 3//4)
(1//1 + 2//1*im, 3//4 + 0//1*im)
- 函數(shù)
julia> f(x,y) = x + y
f (generic function with 1 method)
julia> f(2,3)
5
julia> g = f
f (generic function with 1 method)
julia> g(2,3)
5
julia> ∑(x,y) = x + y
∑ (generic function with 1 method)
julia> ∑(2, 3)
5
julia> +(1,2,3)
6
julia> x -> x^2 + 2x - 1
#3 (generic function with 1 method)
julia> map(x -> x^2 + 2x - 1, [1,3,-1])
3-element Array{Int64,1}:
2
14
-2
julia> function foo(a,b)
a+b, a*b
end;
julia> foo(2,3)
(5, 6)
julia> x, y = foo(2,3);
julia> x
5
julia> y
6
- 控制流
julia> z = begin
x = 1
y = 2
x + y
end
3
julia> function test(x, y)
if x < y
println("x is less than y")
elseif x > y
println("x is greater than y")
else
println("x is equal to y")
end
end
test (generic function with 1 method)
julia> test(1, 2)
x is less than y
julia> test(2, 1)
x is greater than y
julia> test(1, 1)
x is equal to y
julia> println(x < y ? "less than" : "not less than")
less than
julia> while i <= 5
println(i)
i += 1
end
1
2
3
4
5
julia> for i = 1:5
println(i)
end
1
2
3
4
5
- 對象構(gòu)造
外部構(gòu)造方式:
julia> struct Foo
bar
baz
end
julia>
julia> fun=Foo(1,2)
Foo(1, 2)
julia> fun.bar
1
julia> fun.baz
2
julia> Foo(x) = Foo(x,x)
Foo
julia> Foo(1)
Foo(1, 1)
julia> Foo() = Foo(0)
Foo
julia> Foo()
Foo(0, 0)
內(nèi)部構(gòu)造方式:
julia> struct OrderedPair
x::Real
y::Real
OrderedPair(x,y) = x > y ? error("out of order") : new(x,y)
end
julia>
julia> OrderedPair(1, 2)
OrderedPair(1, 2)
julia> OrderedPair(2,1)
ERROR: out of order
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] OrderedPair(::Int64, ::Int64) at ./REPL[45]:4
[3] top-level scope at none:0
- 迭代與索引
迭代操作:
julia> struct Squares
count::Int
end
julia> Base.iterate(S::Squares, state=1) = state > S.count ? nothing : (state*state, state+1)
julia> for i in Squares(7)
println(i)
end
1
4
9
16
25
36
49
julia> 25 in Squares(10)
true
julia> using Statistics
julia> mean(Squares(100))
3383.5
julia> std(Squares(100))
3024.355854282583
julia> Base.eltype(::Type{Squares}) = Int
julia> Base.length(S::Squares) = S.count
julia> collect(Squares(4))
4-element Array{Int64,1}:
1
4
9
16
索引操作:
julia> function Base.getindex(S::Squares, i::Int)
1 <= i <= S.count || throw(BoundsError(S, i))
return i*i
end
julia> Squares(100)[23]
529
julia> Base.firstindex(S::Squares) = 1
julia> Base.lastindex(S::Squares) = length(S)
julia> Squares(23)[end]
529
julia> Base.getindex(S::Squares, i::Number) = S[convert(Int, i)]
julia> Base.getindex(S::Squares, I) = [S[i] for i in I]
julia> Squares(10)[[3,4.,5]]
3-element Array{Int64,1}:
9
16
25
基本的語言特性就體驗到這矩距,剩余的還有一些高級特性拗盒,包括:
- 模塊
- 元編程
- 并行計算
- 網(wǎng)絡(luò)和流
- 交互
- ......
不在此文一一贅述,詳細了解就去參考官方文檔吧锥债。
后 記
由于能力有限陡蝇,若有錯誤或者不當之處痊臭,還請大家批評指正,一起學(xué)習(xí)交流登夫!