Julia 的数据类型(笔记)

                     

贡献者: addis

  • 本文处于草稿阶段。

1. 变量类型

   参考 Julia 文档

   常见类型 Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64(即 Int), UInt64, Int128, UInt128, Float16, Float32, Float64, ComplexF16, ComplexF32ComplexF64

   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(变量或类型) 来查看类型的字节数

逻辑类型

整数

浮点数

2. 无理数

3. 字符串类型

4. 抽象类型

   声明抽象类型

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 的子类。

图
图 1:Julia 自带类型结构

   以及

Complex{T<:Real} <: Number
Rational{T<:Integer} <: Real
Irrational{sym} <: AbstractIrrational

5. 原始类型

   原始类型(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

6. 复合类型

   复合类型(composite) 类似于 C++ 中的 struct,但是不能有成员函数。

struct Foo
    bar # 抽象类型是 Any
    baz::Int
    qux::Float64
end
创建该类型的对象
foo = Foo("Hello, world.", 23, 1.5) # 叫做 constructor

7. Type Unions

IntOrString = Union{Int, AbstractString}
1 :: IntOrString
"Hello!" :: IntOrString
1.0 :: IntOrString # 异常
但是,这和使用 <: 有什么区别?一个类只可能有一个非 Union 的母类,如果一个函数的参数想要支持两个母类不同的类怎么办?那就用 Union 即可。

   另外一个应用的例子是 Union{T, Nothing} 作为函数参数,这样这个参数就可以忽略了。

8. 含参类型

   复数类型 ComplexF16, ComplexF32ComplexF64Complex{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)

9. 含参抽象类型

   含参抽象类型(Parametric Abstract Types) 例如

abstract type Pointy{T} end
struct Point{T} <: Pointy{T}
    x::T
    y::T
end

10. Pair

11. Tuple

   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

12. 类似 C++ 的数据结构

Set

Dict

13. Vararg Tuple Types

14. Named Tuple Types

15. Parametric Primitive Types

16. UnionAll Types

17. Singleton types

18. Type{T} type selectors

19. Type Aliases

   使用等号。例如

# 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

20. Operations on Types

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


致读者: 小时百科一直以来坚持所有内容免费,这导致我们处于严重的亏损状态。 长此以往很可能会最终导致我们不得不选择大量广告以及内容付费等。 因此,我们请求广大读者热心打赏 ,使网站得以健康发展。 如果看到这条信息的每位读者能慷慨打赏 10 元,我们一个星期内就能脱离亏损, 并保证在接下来的一整年里向所有读者继续免费提供优质内容。 但遗憾的是只有不到 1% 的读者愿意捐款, 他们的付出帮助了 99% 的读者免费获取知识, 我们在此表示感谢。

                     

友情链接: 超理论坛 | ©小时科技 保留一切权利