println("hello!")
println("hello!")
print("hello!")
print("hello!")
hello!
hello!
hello!hello!
Wrap the difference between the two functions.
using Pkg
abstract type Real <: Number end
Real is a subclass of Number of
(1+2)::Int
::
Symbol type declaration, as in Python3 :
, such asdef uniquePaths(self, m: int, n: int) -> int:
::
Operator can be used to additional variables and expressions in the program type annotation.
Example, let's parameters for the specified type,
#指定参数 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
Example, let's return value is the specified type,
function sinc(x)::Float64
if x == 0
return 1
end
return sin(pi*x)/(pi*x)
end
Lightly trial,
Strings, symbols and dictionaries
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
Example, declare a complex type member type,
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
An array of very important, ;
a separate line, increase the number of lines ; spaces from one another, increasing the number of columns ; correspond in numpy np.r_[]
and np.c_[]
(there is a good time to sum up, easy to forget that the official statement is to increase the first dimension and a second dimension, which is not clear, who knows you where the number of edges ..)
Xtest = [0 4 1;
2 2 0;
1 1 1]
3×3 Array{Int64,2}:
0 4 1
2 2 0
1 1 1
For more examples, no time to explain, running, think about it, we know that (waste for a long time, with a look, remember this for the record)
[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
Attach python example,
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]
The above may be a bit lost, in a two-dimensional array, the law clearer,
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 type
#复合类型 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.