贡献者: addis
参考 Julia 文档.
float, double 等不是对象,只有 struct, class 定义的才是对象.Julia 中的对象没有成员函数.
::变量类型 用于确认它具有该类型,如果类型不符会产生异常.如果 变量类型 是抽象的,那么表达式只能是它的一个子类.
常见类型
Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64(即 Int), UInt64, Int128, UInt128, Float16, Float32, Float64, ComplexF16, ComplexF32 和 ComplexF64
BigInt 是任意大小的整数,BigFloat 是任意精度浮点数.
julia> BigFloat(2.1) # 2.1 here is a Float64
2.100000000000000088817...
julia> BigFloat("2.1") # the closest BigFloat to 2.1
2.099999999...99999986
julia> BigFloat("2.1", RoundUp)
2.100000000...0000021
julia> BigFloat("2.1", precision=128)
2.0999999...995
julia> BigFloat("2.1", RoundUp, precision=128)
2.100000000...00000000000007
使用 typeof(var) 来询问变量类型,用 isa(var, type) 来确定类型.sizeof(变量或类型) 来查看类型的字节数
setprecision(二进制精度); 可以设置运算默认的精度
BigFloat(pi) 会默认为上面设置的精度,BigFloat(pi, precision=...) 可以临时改变精度,但是 BigFloat(pi, precision=...)*2 仍然是上面设置的精度,因为乘法使用上面设置的精度.
pi 的加减乘除都得到 Float64 类型.
Bool 类型的值可以是 true 或者 false
any([true, false, ...]) 和 all([trua, false, ...])
typeof(3) 是 Int64;typeof(12345678902234567890) 是 Int128,typeof(1234567890223456789032345678904234567890) 是 BigInt.typemin(类型) 和 typemax(类型) 可以查看最大最小值.
a = Int8(1),a += 1 后 a 会变为 Int64.因为 1 相当于 Int64(1).
Float64.
eps(Float64) 返回 2.220446049250313e-16
NaN 是 not a number,可以用 isnan(x) 来判断
pi 的类型是 Irrational{:π} 其中 :π 的类型是 Symbol(又例如 :abc,:a)注意 :pi 和 :π 是两个不同的 Symbol.
BigFloat(pi, precision=500) 可以获得 500 位二进制的 pi.
Char 类型.sizeof(Char) = 4,格式应该是 UTF32
s = "abcde" 的类型是 String,注意 String 并不是 Array{Char, 1}! s[i] 可以获取单个 Char,但是不能赋值,因为 String 是不可改变的(immutable).
s1 = replace(s, "bc" => "BC").s1 也可以是 s 本身.
s = s1*s2 拼接字符串(等效地:s = string(s1, s2)),s *= s1 append.
s = s1^3 重复字符串 3 次,s ^= 3 同理.相当于 repeat(s1, 3)
findfirst("abc", s) 搜索字符串,返回一个 UnitRange{Int64},如 3:5.
findnext("abc", s, ind) 从 ind 开始搜索下一个
collect(s) 把字符串变为 Vector{Char}
s = b"abcABC123" 的类型是 Base.CodeUnits{UInt8, String},s[i] 的类型是 UInt8
声明抽象类型
abstract type 子类名 <: 母类名 end
声明后,可以用 类1 <: 类2 来判断是否符合该关系,返回一个 Bool.
用 supertype(类) 可以查看某个类的母类,subtypes(类) 可以查看所有子类.
例子
abstract type Number end
abstract type Real <: Number end
abstract type AbstractFloat <: Real end
abstract type Integer <: Real end
abstract type Signed <: Integer end
abstract type Unsigned <: Integer end
最高级的抽象类是 Any,这里的 Number 就是 Any 的子类.
以及
Complex{T<:Real} <: Number
Rational{T<:Integer} <: Real
Irrational{sym} <: AbstractIrrational
原始类型(primitive type)是由 bit 组成的具体类型.Julia 可以自定义原始类型.定义原始类型的格式为
primitive type 类名 比特数 end
primitive type 类名 <: 父类名 比特数 end
例如
primitive type Float16 <: AbstractFloat 16 end
primitive type Float32 <: AbstractFloat 32 end
primitive type Float64 <: AbstractFloat 64 end
primitive type Bool <: Integer 8 end
primitive type Char <: AbstractChar 32 end
primitive type Int8 <: Signed 8 end
primitive type UInt8 <: Unsigned 8 end
primitive type Int16 <: Signed 16 end
primitive type UInt16 <: Unsigned 16 end
primitive type Int32 <: Signed 32 end
primitive type UInt32 <: Unsigned 32 end
primitive type Int64 <: Signed 64 end
primitive type UInt64 <: Unsigned 64 end
primitive type Int128 <: Signed 128 end
primitive type UInt128 <: Unsigned 128 end
复合类型(composite) 类似于 C++ 中的 struct
struct Foo
bar # 抽象类型是 Any
baz::Int
qux::Float64
end
创建该类型的对象
foo = Foo("Hello, world.", 23, 1.5) # 叫做 constructor
查看成员名称用 fieldnames(Foo)
要获取成员的值,用 foo.bar, foo.baz, foo.qux 等.用 struct 声明的复合类型值在生成后就不能被改变.如果 struct 里面有矩阵等,那么矩阵元仍然是 mutable 的(没有 lower level const).
如果要支持成员改变,那么用 mutable struct 来声明即可.mutable struct 通常存在 heap 中而不是在 stack 中.
IntOrString = Union{Int, AbstractString}
1 :: IntOrString
"Hello!" :: IntOrString
1.0 :: IntOrString # 异常
但是,这和使用 <: 有什么区别?一个类只可能有一个非 Union 的母类,如果一个函数的参数想要支持两个母类不同的类怎么办?那就用 Union 即可.
另外一个应用的例子是 Union{T, Nothing} 作为函数参数,这样这个参数就可以忽略了.
复数类型 ComplexF16, ComplexF32 和 ComplexF64 是 Complex{Float16}, Complex{Float32} 和 Complex{Float64} 的别名.Complex 就是一个含参类型(parametric type)
例如
struct Point{T}
x::T
y::T
end
那么 Point{Float64} 等都是合法的具体类型.
Point 本身是一个抽象类,是其具体类的母类
julia> Point{Float64} <: Point
true
julia> Point{AbstractString} <: Point
true
<: 相当于一个二元算符,输出 Bool.
例子:
function norm(p::Point{Real})
sqrt(p.x^2 + p.y^2)
end
constructor
p = Point{Float64}(1.0, 2.0)
含参抽象类型(Parametric Abstract Types) 例如
abstract type Pointy{T} end
struct Point{T} <: Pointy{T}
x::T
y::T
end
"abc" => 1.23 的类型是 Pair{String, Float64},令 p = "abc" => 1.23,那么 p.first 和 p.second 分别可以获取两个变量.
Pair 同样是 immutable 的.
Tuple 是函数参数列表的抽象.例如两个元素的 tuple type 类似于如下的含参 struct 类型
struct Tuple2{A,B}
a::A
b::B
end
例子
julia> typeof((1,"foo",2.5))
Tuple{Int64, String, Float64}
julia> Tuple{Int,AbstractString} <: Tuple{Real,Any}
true
julia> Tuple{Int,AbstractString} <: Tuple{Real,Real}
false
t = (值1, 值2, 值3, ...) 就是 Tuple
Tuple 的元素类型可以不同
t = (1, "3.1", [1 2; 3 4]) 的类型是 Tuple{Int64, String, Matrix{Int64}}
length(),isempty(),reverse() 同样适用于 Tuple.
t[i] 获取某个元素,但不能对其赋值! Tuple 的元素永远不能改变! Tuple 类型是 immutable 的.
x = (a=2, b="abc"),类型为 NamedTuple{(:a, :b), Tuple{Int64, String}}.然后可以使用 x.a 和 x.b 来获取元素.
(1,),空 tuple 用 () 表示,类型为 Tuple{}.
(x,y) = (1,"ab") 或者 x,y = (1,"ab") 会分别把元素赋值给 x, y.这可以用于函数返回多个值.
s = Set{Int64}() 生成某种类型的空 set.
s = Set([1,2,3]) 把数组变为 Set(自动推导出类型 Set{Int64})
push!(s, 123, 23, 532) 添加若干元素.
empty!(s) 清空集合.
in(123, s) 查看某个元素是否存在
union!(s1,s2) 计算并集,赋值给 s1,s = union(s1,s2) 计算并集,赋值给 s.
d = Dict{key类型,Val类型}() 创建一个字典(哈希表),也可以直接用 Dict("ab"=>12, "bc"=>23)(会自动推导类型 Dict{String, Int64})
push!(d, key1=>val1, key2=>val2) 添加若干 entry,如果 key 已经存在,就覆盖 val.
a[key] = val.
a[key] 获取对应的 val,如果 key 不存在则会出错.
delete!(d, key)
keys(d) 表示所有存在的 key,类型是 Base.KeySet{key类型, Dict{key类型, val类型}},不会复制值.
vals(d) 同理,类型是 Base.ValueIterator{Dict{String, Int64}}
for (k, v) in d
...
end
使用等号.例如
# 32-bit system:
julia> UInt
UInt32
# 64-bit system:
julia> UInt
UInt64
实现方法
This is accomplished via the following code in base/boot.jl:
if Int === Int64
const UInt = UInt64
else
const UInt = UInt32
end
julia> isa(1, Int)
true
julia> typeof(Rational{Int})
DataType
julia> typeof(Union{Real,String})
Union
julia> typeof(DataType)
DataType
julia> typeof(Union)
DataType
julia> supertype(Float64)
AbstractFloat
julia> supertype(Number)
Any
julia> supertype(AbstractString)
Any
julia> supertype(Any)
Any
 
 
 
 
 
 
 
 
 
 
 
友情链接: 超理论坛 | ©小时科技 保留一切权利