之间有什么区别
function foo(a::Adjoint{Float64, Matrix{T}} where T)
return 0
end
Run Code Online (Sandbox Code Playgroud)
和
function foo(a::Adjoint{Float64, Matrix{T} where T})
return 1
end
Run Code Online (Sandbox Code Playgroud)
(注意花括号的位置。)
julia> methods(foo)
# 2 methods for generic function "foo":
[1] foo(a::Adjoint{Float64,Array{T,2}} where T) in Main at REPL[247]:2
[2] foo(a::Adjoint{Float64,Array{T,2} where T}) in Main at REPL[248]:2
Run Code Online (Sandbox Code Playgroud)
似乎在这两种情况下,函数都会接受 Matrix 类型的伴随T?我无法弄清楚这两个功能之间的区别是什么。
考虑下面的代码:
julia> function foo(x::Float64)::Float64
return 2x
end
foo (generic function with 1 method)
julia> typeof(foo)
typeof(foo)
Run Code Online (Sandbox Code Playgroud)
一定有一个原因为什么typeof(foo)不返回更有意义的东西,例如(Float64 -> Float64)。它是什么?
我在看Zygote代码时发现了这个。
对于 Julia 手动参数复合类型示例
struct Point{T}
x::T
y::T
end
Run Code Online (Sandbox Code Playgroud)
可以编写一个外部构造函数,例如
Point(x::T, y::T) where {T} = Point{T}(x, y)
Run Code Online (Sandbox Code Playgroud)
为什么where {T}需要零件,即为什么不需要
Point(x::T, y::T) = Point{T}(x, y)
Run Code Online (Sandbox Code Playgroud)
可能的?Julia 抱怨T没有定义,但它不能T从::语法中找出它是一种类型吗?我是 Julia 的新手,所以我可能会遗漏一些非常基本的东西。
using Plots
plot(1:1:5, 1:1:5, linewidth=1)
plot!(1:1:5, 1:2:10, linewidth=5)
Run Code Online (Sandbox Code Playgroud)
是否可以使图例线宽与图中的线宽相匹配?不幸的是,我在文档中找不到任何内容。
julia> Integer <: Real
true
Run Code Online (Sandbox Code Playgroud)
如果我们不知道答案 ( Real) 如何找到 的超类型Integer?
另外,如何找到 的所有子类型Integer?
以下两个函数有什么区别?
julia> one(s::String) = return 1
one (generic function with 1 method)
julia> one(::String) = return 1
one (generic function with 1 method)
Run Code Online (Sandbox Code Playgroud)
两者似乎都被允许,它们之间似乎没有区别。我想不包括v可能向编译器发出未使用参数值的信号,但是这又是编译器可以弄清楚的,对吧?(免责声明:我不知道编译器是如何工作的)
我有一个Tuple元素,其中一些是NamedTuples。我想像这样展平NamedTuples:
julia> nt = (a="a", b="b")
(a = "a", b = "b")
julia> t = (1, 2, 3, nt)
(1, 2, 3, (a = "a", b = "b"))
julia> res = tuple(1, 2, 3, nt...)
(1, 2, 3, "a", "b")
Run Code Online (Sandbox Code Playgroud)
如何以编程方式执行此操作?我尝试了以下方法:
julia> exprs = [x isa NamedTuple ? Meta.parse("$x...") : x for x in t]
4-element Array{Any,1}:
1
2
3
:((a = "a", b = "b")...)
julia> res = tuple(eval(ex) for ex in exprs) …Run Code Online (Sandbox Code Playgroud) 考虑阅读以下代码
using DataFrames
using CSV
df = DataFrame()
Run Code Online (Sandbox Code Playgroud)
我如何知道哪个模块DataFrame来自哪个模块?我试过了,?DataFrame但没有有用的信息出现。答案当然是显而易见的,但在大包装中则不然。