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.
原文地址:https://www.cnblogs.com/qizhien/p/11619986.html
时间: 2024-10-27 00:21:38