Julia初学备忘
println("hello!")
println("hello!")
print("hello!")
print("hello!")
hello!
hello!
hello!hello!
两个函数换行区别。
using Pkg
abstract type Real <: Number end
Real 是Number的子类
(1+2)::Int
:: 类型声明符号,如同Python3中: ,如 def uniquePaths(self, m: int, n: int) -> int:
::运算符可以用来在程序中给表达式和变量附加类型注释。
例子,让参数为指定类型,
#指定参数 M 为Matrix类型,这里T是参数模板比如整数Int,Float64等,T <: Number表示参数得是Number子类型
function restructure_matrix(M::Matrix{T}) where {T <: Number}
#Matrix其实是二维数组
Matrix
Array{T,2} where T
#Vector是维数为1的数组
Vector
Array{T,1} where T
例子,让返回值为指定类型,
function sinc(x)::Float64
if x == 0
return 1
end
return sin(pi*x)/(pi*x)
end
随便乱试,
字符串,符号以及字典
sym = Symbol("haha") #:haha
str = String("haha") #"haha"
dic = Dict([("C",7),("D",8),("A", 3), ("B", 2),("E",1)])
tem =keys(dic)
typeof(tem) #Base.KeySet
collect(tem)
typeof(collect(tem)) #Array{String,1}
collect(dic)
sort(collect(dic)) #按键排序
dic["B"]
dic[collect(keys(dic))[1]] #取字典第一个元素,字典键是无序(不按你加入顺序)的且“B”为第一个
sort(collect(keys(dic))) #这时A才在前
#collect后是Pair的向量
dic
Dict{String,Int64} with 5 entries:
"B" => 2
"A" => 3
"C" => 7
"D" => 8
"E" => 1
collect(dic)
5-element Array{Pair{String,Int64},1}:
"B" => 2
"A" => 3
"C" => 7
"D" => 8
"E" => 1
例子,声明复合类型成员类型,
struct Foo
bar #默认Any类型
baz::Int
qux::Float64
end
#摸索一下
typeof(Foo)
Foo.bar #type DataType has no field bar
a = Foo(1,2,3)
#atom ctrl+/注释 atom 选中提示 设为 enter
typeof(a) #Foo
a.bar
a.baz
数组挺重要的, ; 另起一行,增加行数;空格另起一列,增加列数;分别相当于numpy中 np.r_[] 与np.c_[] (有时间好好总结,易忘记,官方说法是增加第一维与第二维,这不清晰,谁知道你往哪边数。。)
Xtest = [0 4 1;
2 2 0;
1 1 1]
3×3 Array{Int64,2}:
0 4 1
2 2 0
1 1 1
更多例子,没时间解释了,跑跑,想想就知道,(浪费很长时间,用了再看,记此备案)
[1 3 2 4]
[[1 3] [2 4]]
1×4 Array{Int64,2}:
1 3 2 4
[1 ;3; 2; 4]
[1 ,3 ,2, 4]
[[1, 3] ;[2, 4]]
4-element Array{Int64,1}:
1
3
2
4
[1 3; 2 4]
[[1 3] ;[2 4]]
2×2 Array{Int64,2}:
1 3
2 4
[[1 ,3] [2 ,4]] #[[1, 3] ;[2, 4]] add row
2×2 Array{Int64,2}:
1 2
3 4
附上python例子,
np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
[[1 2 3 0 0 4 5 6]]
np.c_[np.array([1,2,3]), np.array([4,5,6])]
[[1 4]
[2 5]
[3 6]]
np.r_[np.array([1,2,3]), np.array([4,5,6])]
[0 1 2 3 4 5]
np.r_[np.array([1,2,3]), 0, 0, np.array([4,5,6])]
[1 2 3 0 0 4 5 6]
上面可能有点迷,在二维数组,这个规律更清晰,
x= np.c_[np.array([11,12]), np.array([14,15])]
y = np.arange(4).reshape(2,2)
y
array([[0, 1],
[2, 3]])
x
array([[11, 14],
[12, 15]])
np.c_[x,y] #列数增加
array([[11, 14, 0, 1],
[12, 15, 2, 3]])
np.r_[x,y] #行数增加
array([[11, 14],
[12, 15],
[ 0, 1],
[ 2, 3]])
复合类型
#复合类型 Composite Types
julia>
julia> foo = Foo("Hello, world.", 23, 1.5)
Foo("Hello, world.", 23, 1.5)
julia> typeof(foo)
Foo
#类型联合
julia> IntOrString = Union{Int,AbstractString}
Union{Int64, AbstractString}
julia> 1 :: IntOrString
1
julia> "Hello!" :: IntOrString
"Hello!"
julia> 1.0 :: IntOrString
ERROR: TypeError: in typeassert, expected Union{Int64, AbstractString}, got Float64
#有参数复合类型 Parametric Composite Types
julia> struct Point{T}
x::T
y::T
end
#NTuple{N,T} is a convenient alias for Tuple{Vararg{T,N}}, i.e. a tuple type containing exactly N elements of type T.

Julia初学备忘的更多相关文章
- Unity3D Object.DontDestroyOnLoad 备忘
初学Untiy3D,记录备忘. public static void DontDestroyOnLoad(Object target); Makes the object target not be ...
- Oracle使用备忘
初学Oracle,很多语句记不住,写在这里备忘. 1.查看某表空间的数据文件 select file_name 文件名, tablespace_name 表空间名, bytes 已使用大小M, max ...
- GIS部分理论知识备忘随笔
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
- Vi命令备忘
备忘 Ctrl+u:向文件首翻半屏: Ctrl+d:向文件尾翻半屏: Ctrl+f:向文件尾翻一屏: Ctrl+b:向文件首翻一屏: Esc:从编辑模式切换到命令模式: ZZ:命令模式下保存当前文件所 ...
- ExtJs4常用配置方法备忘
viewport布局常用属性 new Ext.Viewport({ layout: "border", renderTo: Ext.getBody(), defaults: { b ...
- [备忘] Automatically reset Windows Update components
这两天遇到Windows 10的更新问题,官方有一个小工具,可以用来修复Windows Update的问题,备忘如下 https://support.microsoft.com/en-us/kb/97 ...
- ECMAScript 5(ES5)中bind方法简介备忘
一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...
- MFC通过txt查找文件并进行复制-备忘
MFC基于对话框的Demo txt中每行一个23位的卡号. 文件夹中包含以卡号命名的图像文件.(fpt或者bmp文件) 要求遍历文件夹,找到txt中卡号所对应的图像文件,并复制出来. VC6.0写的. ...
随机推荐
- 根据图中的盲点坐标,弹出div层
<div class="map_r" id="mapinfo" style="position: absolute; top: 20px; le ...
- px和dp(内含大量的像素单位详解)
1.前言: 读完本文你会学到什么: dp(device pixels) px(css pixels) pt(point) ppi(pixels per inch) dpi(dots per inch) ...
- 小米 OJ 编程比赛 02 月常规赛 3 Logic Gatekeeper CDQ分治
link:https://code.mi.com/problem/list/view?id=139 题意: 有一个1e6 * 1e6 大的格子,现在有两种操作:1,给一个子矩阵中的每个格子加上k.2, ...
- P2766 最长不下降子序列问题 网络流
link:https://www.luogu.org/problemnew/show/P2766 题意 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的 ...
- ZOJ-3964 Yet Another Game of Stones
Yet Another Game of Stones 题意: Alice 和 Bob 在进行取石子游戏, 现在一共有n堆石子, 每堆石头有ai个, 然后每堆石头有一个bi属性, 如果bi == 0, ...
- hdu 1028 Ignatius and the Princess III 母函数
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 如何将idea工程打包成jar文件
如何将idea工程打包成jar文件 近日在工作中遇到了一个问题,需要把本地的java文件打成jar包,传到云服务器上运行.于是学习了一下如何在intellij idea中将java工程打成jar包. ...
- 从SpringBoot构建十万博文聊聊Tomcat集群监控
前言 在十万博文终极架构中,我们使用了Tomcat集群,但这并不能保证系统不会出问题,为了保证系统的稳定运行,我们还需要对 Tomcat 进行有效的运维监控手段,不至于问题出现或者许久一段时间才知道. ...
- 几个Python爬虫工具介绍
Request Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用 上面一句话 出自Requests官方文档的第一句,从而奠定了其逗逼的文档风格.类似的还有: 警告: ...
- LeetCode探索初级算法 - 动态规划
LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...