如何在 Julia 中显示字段值
How to show field values in Julia
我想知道是否可以在 Julia 中显示字段值。
例如,这个Python程序,从消费者class:
获取对象变量wealth
class Consumer:
def __init__(self, w):
"Initialize consumer with w dollars of wealth"
self.wealth = w
def earn(self, y):
"The consumer earns y dollars"
self.wealth += y
def spend(self, x):
"The consumer spends x dollars if feasible"
new_wealth = self.wealth - x
if new_wealth < 0:
print("Insufficent funds")
else:
self.wealth = new_wealth
c1.wealthc1 = Consumer(10) # Create instance with initial wealth 10
c1.spend(5)
c1.wealth
财富变量为5
。我想知道如何将这段代码翻译成 Julia。
Julia 不支持 classes(就 OOP 而言)。
但是,有composite types可以代表你class的变量:
type Consumer
wealth::Float64
end
现在,由于 Julia 不支持 classes,所有方法都必须存在于此类型之外,这允许 Julia 的关键功能之一,多重分派 , 也可以使用 user-defined 类型。 (https://docs.julialang.org/en/stable/manual/methods/, https://www.juliabloggers.com/julia-in-ecology-why-multiple-dispatch-is-good/)
因此,您必须添加这样的方法:
function earn!(consumer::Consumer, y::Float64)
println("The consumer earns y dollars")
consumer.wealth = consumer.wealth + y
end
(同样可以实现spend
功能)
最简单的方法很像 Python:
mutable struct Consumer
wealth
end
function earn(c::Consumer, y)
c.wealth += y
end
function spend(c::Consumer, y)
c.wealth -= y
end
现在您可以像这样使用它:
julia> c1 = Consumer(10)
Consumer(10)
julia> spend(c1, 5)
5
julia> c1.wealth
5
您可以阅读更多相关信息 here。
但在 Julia 中你可能会这样写:
mutable struct ConsumerTyped{T<:Real}
wealth::T
end
function earn(c::ConsumerTyped, y)
c.wealth += y
end
function spend(c::ConsumerTyped, y)
c.wealth -= y
end
这在表面上几乎是一样的。不同之处在于 T
指定了 wealth
的类型。有两个好处:您可以在代码中进行类型控制,函数将 运行 更快。
给出这样的定义,您唯一需要知道的是可以用两种方式调用构造函数:
c2 = ConsumerTyped{Float64}(10) # explicitly specifies T
c3 = ConsumerTyped(10) # T implicitly derived from the argument
现在让我们比较一下这两种类型的性能:
julia> using BenchmarkTools
julia> c1 = Consumer(10)
Consumer(10)
julia> c2 = ConsumerTyped(10)
ConsumerTyped{Int64}(10)
julia> @benchmark spend(c1, 1)
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 56.434 ns (0.00% GC)
median time: 57.376 ns (0.00% GC)
mean time: 60.126 ns (0.84% GC)
maximum time: 847.942 ns (87.69% GC)
--------------
samples: 10000
evals/sample: 992
julia> @benchmark spend(c2, 1)
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 29.858 ns (0.00% GC)
median time: 30.791 ns (0.00% GC)
mean time: 32.835 ns (1.63% GC)
maximum time: 966.188 ns (90.20% GC)
--------------
samples: 10000
evals/sample: 1000
你会发现你获得了大约 2 倍的加速。
我想知道是否可以在 Julia 中显示字段值。
例如,这个Python程序,从消费者class:
获取对象变量wealth
class Consumer:
def __init__(self, w):
"Initialize consumer with w dollars of wealth"
self.wealth = w
def earn(self, y):
"The consumer earns y dollars"
self.wealth += y
def spend(self, x):
"The consumer spends x dollars if feasible"
new_wealth = self.wealth - x
if new_wealth < 0:
print("Insufficent funds")
else:
self.wealth = new_wealth
c1.wealthc1 = Consumer(10) # Create instance with initial wealth 10
c1.spend(5)
c1.wealth
财富变量为5
。我想知道如何将这段代码翻译成 Julia。
Julia 不支持 classes(就 OOP 而言)。
但是,有composite types可以代表你class的变量:
type Consumer
wealth::Float64
end
现在,由于 Julia 不支持 classes,所有方法都必须存在于此类型之外,这允许 Julia 的关键功能之一,多重分派 , 也可以使用 user-defined 类型。 (https://docs.julialang.org/en/stable/manual/methods/, https://www.juliabloggers.com/julia-in-ecology-why-multiple-dispatch-is-good/) 因此,您必须添加这样的方法:
function earn!(consumer::Consumer, y::Float64)
println("The consumer earns y dollars")
consumer.wealth = consumer.wealth + y
end
(同样可以实现spend
功能)
最简单的方法很像 Python:
mutable struct Consumer
wealth
end
function earn(c::Consumer, y)
c.wealth += y
end
function spend(c::Consumer, y)
c.wealth -= y
end
现在您可以像这样使用它:
julia> c1 = Consumer(10)
Consumer(10)
julia> spend(c1, 5)
5
julia> c1.wealth
5
您可以阅读更多相关信息 here。
但在 Julia 中你可能会这样写:
mutable struct ConsumerTyped{T<:Real}
wealth::T
end
function earn(c::ConsumerTyped, y)
c.wealth += y
end
function spend(c::ConsumerTyped, y)
c.wealth -= y
end
这在表面上几乎是一样的。不同之处在于 T
指定了 wealth
的类型。有两个好处:您可以在代码中进行类型控制,函数将 运行 更快。
给出这样的定义,您唯一需要知道的是可以用两种方式调用构造函数:
c2 = ConsumerTyped{Float64}(10) # explicitly specifies T
c3 = ConsumerTyped(10) # T implicitly derived from the argument
现在让我们比较一下这两种类型的性能:
julia> using BenchmarkTools
julia> c1 = Consumer(10)
Consumer(10)
julia> c2 = ConsumerTyped(10)
ConsumerTyped{Int64}(10)
julia> @benchmark spend(c1, 1)
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 56.434 ns (0.00% GC)
median time: 57.376 ns (0.00% GC)
mean time: 60.126 ns (0.84% GC)
maximum time: 847.942 ns (87.69% GC)
--------------
samples: 10000
evals/sample: 992
julia> @benchmark spend(c2, 1)
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 29.858 ns (0.00% GC)
median time: 30.791 ns (0.00% GC)
mean time: 32.835 ns (1.63% GC)
maximum time: 966.188 ns (90.20% GC)
--------------
samples: 10000
evals/sample: 1000
你会发现你获得了大约 2 倍的加速。