我试图理解在 Julia 中的输入并遇到以下问题Array。我写了一个函数bloch_vector_2d(Array{Complex,2});详细的实施是无关紧要的。打电话时,这里是投诉:
julia> bloch_vector_2d(rhoA)
ERROR: MethodError: no method matching bloch_vector_2d(::Array{Complex{Float64},2})
Closest candidates are:
bloch_vector_2d(::Array{Complex,2}) at REPL[56]:2
bloch_vector_2d(::StateAB) at REPL[54]:1
Stacktrace:
[1] top-level scope at REPL[64]:1
Run Code Online (Sandbox Code Playgroud)
问题是父类型的数组不会自动成为子类型数组的父级。
julia> Complex{Float64} <: Complex
true
julia> Array{Complex{Float64},2} <: Array{Complex,2}
false
Run Code Online (Sandbox Code Playgroud)
我认为将这一点强加给 julia 是有道理的Array{Complex{Float64},2} <: Array{Complex,2}。或者在 Julia 中实现这一点的正确方法是什么?任何帮助或意见表示赞赏!
我必须计算朱莉娅泊松分布的概率.我只知道如何获得泊松分布.但我必须计算概率.我也有20到100的lambda.
使用分布
泊松()
我有一个一维Array输出(如下所示),需要转换为DataFrame.
x = rand(4)
4-element Array{Float64,1}:
0.951252
0.936421
0.773268
0.207913
p = convert(DataFrame, x) // Why this doesn't work ?
Run Code Online (Sandbox Code Playgroud)
这导致:
MethodError:无法
convert将 Array{Float64,1} 类型的对象转换为 DataFrames.DataFrame 类型的对象这可能是由于调用构造函数 DataFrames.DataFrame(...) 引起的,因为类型构造函数回退到转换方法。
为什么这不起作用?
假设我们在 Julia 中有一组笛卡尔索引
julia> typeof(indx)
Array{CartesianIndex{2},1}
Run Code Online (Sandbox Code Playgroud)
现在我们想使用 PyPlot 将它们绘制为散点图。所以我们应该在转换INDX -阵列笛卡尔到一个二维矩阵,所以我们可以绘制它是这样的:
PyPlot.scatter(indx[:, 1], indx[:, 2])
Run Code Online (Sandbox Code Playgroud)
如何将Array{CartesianIndex{2},1}类型的Array转换为Array{Int,2}类型的 2D-Matrix
顺便说一下,这里是如何生成笛卡尔索引的虚拟数组的代码片段:
A = rand(1:10, 5, 5)
indx = findall(a -> a .> 5, A)
typeof(indx) # this is an Array{CartesianIndex{2},1}
Run Code Online (Sandbox Code Playgroud)
谢谢
我有这个代码:
Using ForwardDiff:gradient
derivative(f,x) = gradient(x->f(x[1]),[x])
Run Code Online (Sandbox Code Playgroud)
我知道 ForwardDiff 中的梯度采用向量作为参数,但这是什么意思:(x->f(x[1])),我对 感到困惑x[1]。
我目前正在通过命令行会话编程Julia.
我知道Julia中的预定义函数(例如sqrt)可以采用变量值,但在特定会话中我尝试使用该函数sqrt(25),它给了我5.0但是在我编写的同一个会话中sqrt=9,然后它说
" 错误:无法从模块Main分配变量Base.sqrt "
如果我必须要做到这一点,我必须重新打开一个新的会话,并为sqrt分配一个变量值sqrt=9,当我这样做时再说它
错误:MethodError:Int64类型的对象不可调用
当我尝试使用sqrt作为函数时.
同样的事情发生在pi.
我正在尝试查看可以使用 Julia 精确表示的最大正整数的位串。维基百科说 2^1024 * (1-2^(-53)) 是整数。但是,当我尝试使用 Julia 时,位串全为零。
julia> bitstring(Float64( 2^1024 - 2^971 ))
"0000000000000000000000000000000000000000000000000000000000000000"
Run Code Online (Sandbox Code Playgroud)
我期待的位串是
0 11111111110 1111111111111111111111111111111111111111111111111111
Run Code Online (Sandbox Code Playgroud)
你能帮我澄清为什么会发生这种差异吗?
我创建了这个模块
module MyModule
export my_square, my_abs, my_minus
my_square(x::Int64) = x * x
my_abs(x) = (x>=0) ? x : -x
my_add(x,y) = x + y
my_minus(x,y) = x - y
my_multiply(x,y) = x * y
end
Run Code Online (Sandbox Code Playgroud)
但是当导入它使用它时它会引发错误,有什么解决方案吗?
这是我在 Julia 中寻找的伪代码实现:
struct Example
field1::Float64
field2::Float64
end # End struct
example = Example(1., 2.)
function modifystruct(mystruct, fieldname)
mystruct.fieldname +=10
return mystruct
end
modifystruct(example, field1)
# In this instance I would want to have example.field1 = 11.
Run Code Online (Sandbox Code Playgroud)
我实际上该怎么做?我想提供类似字符串的字段名,并让我的 struct."whateverfieldname" 得到这样的修改。我应该补充一点,我不想编写这样的代码:
function modifystruct(mystruct, fieldname)
if fieldname = "fieldname1"
mystruct.field1 +=10
end
if fieldname = "fieldname2"
mystruct.field2 +=10
end
return mystruct
end
Run Code Online (Sandbox Code Playgroud)
很大程度上是因为我希望这段代码具有多用途性。我的程序可能使用不同类型的结构,因此我可以通过字段名称直接访问的结构越接近越好。有没有任何方法或实现可以为我做到这一点?
假设我在 Julia 中有以下内容:
mutable struct emptys
begin_time::Dict{Float64,Float64}; finish_time::Dict{Float64,Float64}; Revenue::Float64
end
population = [emptys(Dict(),Dict(),-Inf) for i in 1:n_pop] #n_pop is a large positive integer value.
for ind in 1:n_pop
r = rand()
append!(population[ind].Revenue, r)
append!(population[ind].begin_time, Dict(r=>cld(r^2,rand())))
append!(population[ind].finish_time, Dict(r=>r^3/rand()))
end
Run Code Online (Sandbox Code Playgroud)
现在我想根据收入值对总体进行排序。Julia 有什么办法可以实现这一目标吗?如果我用 Python 来做的话,会是这样的:
sorted(population, key = lambda x: x.Revenue) # The population in Python can be prepared using https://pypi.org/project/ypstruct/ library.
Run Code Online (Sandbox Code Playgroud)
请帮忙。
我正在尝试实现一种宏,以包含使用RCall@Rinclude Julia 包在 Julia 中的文件中编写的 R 代码,但我无法使字符串宏识别我已阅读的字符串中编码的代码:R"..."
RCode = """
sumMyArgs <- function(i, j, z) i+j+z
"""
open(f->write(f,RCode),"RScript.R","w")
macro Rinclude(fname)
quote
rCodeString = read($fname,String)
R"$rCodeString"
nothing
end
end
@Rinclude("RScript.R")
a = rcopy(R"sumMyArgs"(3,4,5)) # Error as it can't find sumMyArgs
Run Code Online (Sandbox Code Playgroud)
问题是 R"..." 字符串宏不适用于$rCodeString. 返回的对象是 aRObject{StrSxp}而不是 a RObject{ClosSxp}:
julia> a = R"""$(rCodeString)"""
RObject{StrSxp}
[1] "sumMyArgs <- function(i, j, z) i+j+z\n"
julia> R"""sumMyArgs3 <- function(i, j, z) i+j+z"""
RObject{ClosSxp}
function (i, j, z)
i + …Run Code Online (Sandbox Code Playgroud) 我有Julia的这段代码,可以将秒转换为小时,分钟和秒,但是当我运行它时,我得到(0, 0, 0)的只是输出。有人可以告诉我这怎么了吗?
function convert_from_seconds(sec::Int64)
hours = 0
minutes = 0
seconds = 0
time = (hours, minutes, seconds)
if sec < 60
seconds = sec
elseif sec < 3600
minutes = floor(sec / 60)
seconds = sec % 60
elseif sec < 216000
hours = floor(sec / 3600)
minutes = floor(hours % 3600)
seconds = minutes % 60
end
return time
end
Run Code Online (Sandbox Code Playgroud) Julia> type Circle
ERROR: syntax: extra token "Circle" after end of expression
Stack trace:
[1] top-level scope at none:0
Run Code Online (Sandbox Code Playgroud)
我已经尝试过 struct 方法,但它没有按要求工作。