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写的. ...
随机推荐
- SqlServer关于“无法删除数据库 "XXXX",因为该数据库当前正在使用”问题的解决方案
引言 在项目中,通过使用SQL语句“DROP DATABASE [数据库名]”删除数据时,一直出现“无法删除数据库 "XXXX",因为该数据库当前正在使用”的错误信息,经测试在Sq ...
- [翻译] .NET Core 3.0 Preview 9 发布
原文: Announcing .NET Core 3.0 Preview 9 今天,我们宣布推出 .NET Core 3.0 Preview 9.就像 Preview 8 一样,我们专注于打磨 .NE ...
- SCRUM的三个工件
转自:http://www.scrumcn.com/agile/scrum-knowledge-library/scrum.html#tab-id-6 Scrum 的工件以不同的方式展现工作和价值,可 ...
- P3317 [SDOI2014]重建 变元矩阵树定理 高斯消元
传送门:https://www.luogu.org/problemnew/show/P3317 这道题的推导公式还是比较好理解的,但是由于这个矩阵是小数的,要注意高斯消元方法的使用: #include ...
- HDU - 3966 树链刨分
题目传送门 操作就是询问某个点的值, 然后就是对一条路径上的值全部修改. 最基本的树刨题目了. 树刨的思想: 1. 对于每个点找到他的重儿子. void dfs1(int o, int u){ sz[ ...
- 51nod 1218 最长递增子序列 V2(dp + 思维)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 题解:先要确定这些点是不是属于最长递增序列然后再确定这 ...
- win10 无法安装/启用 .net framework 3.5
有些程序依赖.net framework 3.5 win10可以在控制面板->程序和功能->启用或关闭windows功能 启用 但有时会报错 比如 0x800f0950 官方论坛的解决办法 ...
- mybatis foreach批量处理
---恢复内容开始--- http://blog.csdn.net/jiesa/article/details/52185617 foreach属性 属性 描述 item 循环体中的具体对象.支持属性 ...
- 【Offer】[5] 【替换空格】
题目描述 思路分析 Java代码 代码链接 题目描述 请实现一个函数,把字符串中的每个空格替换成"%20". 例如输入"We are happy.",则输出&q ...
- 在Word指定位置插入富文本域值(html文本)
遇到此问题,首先想到的就是各种百度.结果度娘了一会并没有发现有用的有效的解决方法,哎,看来还得靠自己啊. 首先整理了下手头上的资源,一是HtmlAgilityPack,专门解析Html文本用的:二是我 ...