Julia编程语言的新版本之所以受到整个人工智能界的关注,最主要的原因正是其将 C 语言的速度、Ruby 的灵活、Python 的通用性前所未有地结合在一起,支持并行处理,易于学习和使用,尤其适合科学和工程计算。
官方文档链接:
Julia Documentation · The Julia Language
软件下载链接:
▋〓〓特色〓〓▋函数可以直接赋别名
特色内容详见下文。
基本示例
julia> function f(x)
x+1
end
f (generic function with 1 method)
求值▋〓〓特色〓〓▋函数可以直接赋别名
julia> f(5)
6
julia> g = f
f (generic function with 1 method)
julia> g(8)
9
可以在函数定义中增加return关键词
julia> function f(x)
return x+1
end
f (generic function with 1 method)
函数返回值类型定义
(说明:类似C语言)
julia> function h(x)::Int32
return x+3
end
h (generic function with 1 method)
julia> typeof(h(4))
Int32
返回nothing
julia> function h(x)
println("x=$x")
return nothing
end
h (generic function with 1 method)
julia> h(45)
x=45
部分操作符的变量名
Expression | Calls |
---|---|
[A B C ...] | hcat |
[A; B; C; ...] | vcat |
[A B; C D; ...] | hvcat |
A' | adjoint |
A[i] | getindex |
A[i] = x | setindex! |
A.n | getproperty |
A.n = x | setproperty! |
匿名函数
匿名函数如下所示
julia> x->x^2
#1 (generic function with 1 method)
julia> map(x->x^2,[1,2,3])
3-element Vector{Int64}:
1
4
9
函数的多重返回值(类似Python和Matlab)
julia> function f(x,y)
return x+y,x-y
end
f (generic function with 2 methods)
julia> f(1,4)
(5, -3)
julia> (x,y)=f(1,4)
(5, -3)
julia> x
5
julia> y
-3
不定参数函数
过于灵活,目前没有明显使用意图,后续补充。