[转] Julia 高性能动态编程语言入门

本文共 851字,阅读大约需要 3分钟 !
概 述
Julia 是一个 “全新”的高性能动态编程语言,前两天迎来了其 1.0 正式版的重大更新。Julia集 Python、C、R、Ruby 之所长,感觉就像一种脚本语言,并且对交互式使用有很好的支持。而且其天生的高性能、通用性与专业性使得其非常适用于科学数值计算、机器学习项目等前沿场景。我看完这个消息以后也迫不及待想尝试一下。
注: 本文原载于 My Personal Blog:, CodeSheep · 程序羊 !
本文内容脑图如下:

Julia的特性
- 高性能:Julia 通过 LLVM 为多个平台编译高效本地代码,性能很高
- 动态性:编程范式灵活,代码信噪比极高
- 通用性:易于表达OOP和函数式编程范式,并且其标准库提供异步I / O,进程控制,日志记录,概要分析,包管理器等。
- 专业性:擅长数值计算,支持许多数值数据类型,并且提供开箱即用的并行性。
- 可组合性:Julia 的包之间可以很好地协同工作。
正是由于这些特性,才使其应用场景宽泛,而且都是当下前沿热门应用场景:

编程环境支持
Julia通过提供了一系列插件支持,从而可以在大多数常见的编辑器中进行编程,具体包括
- Atom
- VS Code
- Jupyter
- Vim
- Emacs
- SublimeText
Julia安装和部署
Julia 提供了各种平台和环境的安装包,具体可以去其官网进行下载:

安装非常简单,像 Windows平台,基本上只需要点按下一步即可安装到位,而 MacOS平台使用 brew包管理器也仅需 一行命令 即可完成安装。
下面我以 Linux CentOS 7.4 平台为例,介绍一些其安装过程:
CentOS7 上 Julia 安装也无需复杂的过程,只需要下载对应的可执行版本,并置于系统的命令路径中即可愉快的使用:
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
此时执行 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("#")
- 类型转换和提升
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)
- 函数
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
- 对象构造
外部构造方式:
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)
内部构造方式:
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
基本的语言特性就体验到这,剩余的还有一些高级特性,包括:
- 模块
- 元编程
- 并行计算
- 网络和流
- 交互
- ......
不在此文一一赘述,详细了解就去参考官方文档吧。
本文由博客一文多发平台 OpenWrite 发布!
[转] Julia 高性能动态编程语言入门的更多相关文章
- Apple Swift编程语言入门教程
Apple Swift编程语言入门教程 作者: 日期: 布衣君子 2015.09.22 目录 1 简介 2 Swift入门 3 简单值 4 控制流 5 函数与闭包 6 对象与类 ...
- Dart编程语言入门
Dart基础入门语法介绍,详细说明可以查看相关视频<Dart编程语言入门>. 变量与常量 变量 1.使用 var 声明变量,默认值为 null var a;//null a = 10; 2 ...
- [转]Swift 编程语言入门教程
今天在网上看到一篇非常好的教程,分享给大家 原文地址:http://gashero.iteye.com/blog/2075324 目录 1 简介 2 Swift入门 3 简单值 4 控 ...
- Apple Swift编程语言入门
1 简单介绍 今天凌晨Apple刚刚公布了Swift编程语言,本文从其公布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS&a ...
- Swift 编程语言入门教程
1 简介 今天凌晨Apple刚刚发布了Swift编程语言,本文从其发布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS& ...
- 【腾讯Bugly干货分享】Android动态布局入门及NinePatchChunk解密
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57c7ff5d53bbcffd68c64411 作者:黄进——QQ音乐团队 摆脱 ...
- Nginx+Lua+MySQL/Redis实现高性能动态网页展现
Nginx结合Lua脚本,直接绕过Tomcat应用服务器,连接MySQL/Redis直接获取数据,再结合Lua中Template组件,直接写入动态数据,渲染成页面,响应前端,一次请求响应过程结束.最终 ...
- 动态代理入门(jdk)
动态代理就是aop的核心,动态代理简单的就是通过创建一个代理对象,然后把原来的方法增强.很抽象,例子是王道.jdk中提供了动态代理的实现,但是它是针对接口,如果要实现动态代理,需要被代理对象的接口.这 ...
- LCT动态树入门
LCT,link-cut-tree,一种基于splay的高级数据结构,常用于维护动态森林问题,但ta只能维护子树信息,无法修改子树信息. 首先,如果你不会splay,来这里看看吧. 接下来步入正题. ...
随机推荐
- codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))
题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...
- git bash下添加忽略文件列表
转载自:https://blog.csdn.net/weixin_42808389/article/details/81232119 在用KEIL 5(MDK ARM)开发项目时需要用到GIT管理代码 ...
- 在React中使用Bootstrap
这几天想在react中用一下bootstrap,尽管有一个适配react的很好的库叫react-bootstrap,但我还是想直接使用bootstrap 可以在react项目中执行以下命令安装boot ...
- 【Offer】[57-2] 【和为S的连续正数序列】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入一个正数s,打印出所有和为s的连续正数序列(至少含有两个数).例如,输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以 ...
- Django2.* + Mysql5.7开发环境整合
环境: MAC_OS 10.12 python 3.6 mysql 5.7.25 django 2.2.3 前提:python django mysql都已经安装成功可单独运行 一.settings中 ...
- struts2表单提单细节处理
1. 上传文件 大部分项目避免不了要上传文件. struts2提供了封闭的上传文件的入口, 网络上也存在大量的插件用于网页表单中上传文件. 由于自己习惯用SSH框架, 所以介绍一下struts2中文件 ...
- 大数据平台搭建 - cdh5.11.1 - hadoop集群安装
一.前言 由于线下测试的需要,需要在公司线下(测试)环境搭建大数据集群. 那么CDH是什么? hadoop是一个开源项目,所以很多公司再这个基础上进行商业化,不收费的hadoop版本主要有三个,分别是 ...
- Elastic Stack 笔记(三)Kibana5.6 安装
博客地址:http://www.moonxy.com 一.前言 Kibana 是 Elastic Stack 公司推出的一个针对 Elasticsearch 的开源分析及可视化平台,可以搜索.查看存放 ...
- C# HTTP网络常用方法封装
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Regi ...
- 一个vue练手的小项目
编程路上的菜鸟一枚 : 最近接触了vue 然后写了一个练手的项目 使用vue-cli脚手架来搭建了的项目 技术: vue2 + vue-router + ES6 + axios 框架有 mint- ...