我对Julia - Windows上的1.0.0版本非常陌生。声明了以下内容
julia> Inf / Inf
NaN
但是当我执行以下操作时,我得到了不同的结果
julia> 1/0
Inf
julia> 1/0 / 1/0 # this should be NaN right, tried (1/0)/(1/0) as well
Inf
julia> 1/0
Inf
julia> ans/ans
NaN
为什么1/0 / 1/0不是NaN,而ans/ans是呢?
我在几个地方看到了解决方案
a = [1 2 3; 4 5 Inf]
a[isinf(a)] = NaN
但是这给了我一个关于Julia 1.0.1的错误:
ERROR: MethodError: no method matching isinf(::Array{Float64,2})
Closest candidates are:
isinf(::BigFloat) at mpfr.jl:851
isinf(::Missing) at missing.jl:79
isinf(::ForwardDiff.Dual) at <path on my local ma
如何使用Julia的点表示法进行元素级操作,并确保结果保存在已经存在的数组中?
function myfun(x, y)
return x + y
end
a = myfun(1, 2) # Results in a == 3
a = myfun.([1 2], [3; 4]) # Results in a == [4 5; 5 6]
function myfun!(x, y, out)
out .= x + y
end
a = zeros(2, 2)
myfun!.([1 2], [3; 4], a) # Results in a DimensionMismat
当我运行下面的代码时,fminbox或Optim.autodiff函数似乎创建了一个类型为数组{Float64{matching...Array},1}的向量,因为我得到了错误消息"fbellmanind没有方法matching...Array{Dual{Float64},1}“。我已经指定了函数fbellmanind来接受Array{Any,1},但没有成功。有什么想法吗?
function fbargsolve(x::Vector)
fbellmanind(probc,EV,V,Ind,x,V0,VUnemp0,Vnp,Vp,q,obj,assets,EmpState,i)
f
在Julia 1.0.0 REPL中,我得到了以下结果:
# Line 1: This make sense. I did not expect a Float64 to equal a BigFloat.
julia> 26.1 == big"26.1"
false
# Line 2: This surprised me when Line 1 is considered. Again, like Line 1, I
# did not expect a Float64 to equal an equivalent BigFloat.
julia> 26.
这是我在Julia中的代码。我想找出数组b的元素在数组a中的位置。 a = [2,4,1,3]
b = [1,4]
c=[]
for i in 1:length(b)
push!(c, findfirst(isequal(b[i]), a));
end
println(c) 结果是3,2。这是正确的。但我认为应该有更有效的朱利安( Julian )方法。我试过了 julia> findall(x -> x == b, a)
0-element Array{Int64,1} 这是错误的。然后 julia> findall(x -> x == .b, a)
ERR
我们在count_heads.jl中有一个函数:
function count_hands(n)
c::Int=0
for i=1:n
c+=rand(Bool)
end
c
end
我们以./julia -p 2的形式运行朱莉娅,我们想在不同的过程中计算a和b,我们有:
julia> @everywhere include("count_hands.jl")
julia> a=@spawn count_hands(1000000000)
julia> b=@spawn count_hands(100000000
我的问题大致如下。给定一个数值矩阵X,其中每一行都是一个项。我想在所有行中根据L2距离找到每一行的最近邻居,除了它本身。我试着阅读了官方文档,但对于如何实现这一点仍然有些困惑。有人能给我点提示吗?
我的代码如下
function l2_dist(v1, v2)
return sqrt(sum((v1 - v2) .^ 2))
end
function main(Mat, dist_fun)
n = size(Mat, 1)
Dist = SharedArray{Float64}(n) #[Inf for i in 1:n]
Id = SharedArray{
我在Python和Julia中实现了Mandelbrot函数;但是,在第6次迭代之后,代码产生了不同的结果。主要原因是什么?
以下是Python的代码:
def mandelbrot(a):
z = 0
for i in range(50):
z = z**2 + a
return z
下面是Julia中的相同代码:
function mandelbrot(a)
z = 0
for i=1:50
z = z^2 + a
end
return z
end
在Julia中,我希望找到每行中最大值的矩阵的列索引,结果是一个Vector{Int}。下面是我目前的做法(Samples有7列和10,000行):
mxindices = [ i[2] for i in findmax(Samples, dims = 2)[2]][:,1]
这是可行的,但感觉相当笨拙和冗长。不知道有没有更好的办法。
我正在尝试编写一个接受参数的函数,该参数可以是元组或数组。例如,这是可行的:
julia> temp(x::Union{Vector{Int64},NTuple{4,Int64}}) = sum(x)
temp (generic function with 1 method)
julia> temp((3,1,5,4))
13
julia> temp([3,1,5,4])
13
另一方面,当我尝试使用未指定长度的元组时,它对数组失败:
julia> temp(x::Union{Vector{Int64},NTuple{N,Int64}}) where N = sum
有一个名为orders_data的CSV文件存储在我的系统中,但是当我试图使用朱庇特笔记本中的readdlm命令(在我的浏览器中运行)加载这个文件时,它说“找不到这样的文件目录”。
我不知道为什么会这样?是否有特定的位置需要存储文件才能使用Julia命令访问?我是否需要先安装一些软件包才能使用浏览器版本的jupyter加载文件呢?
//Error information
SystemError:打开文件orders_data.csv:没有这样的文件或目录
对不起,我肯定漏掉了一些东西,但是为什么呢? 在基本循环中为每个元素报告一个内存分配,如下所示: julia> using BenchmarkTools
julia> v=[1:15;]
15-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
julia> @btime for vi in v end
1.420 μs (15 allocations: 480 bytes)
julia> @btime for i in eachindex(v
我有以下MATLAB代码片段:
>> R = randn(3000,6000); % build a random 3000 by 6000 matrix
>> tic; norm(R, 1); toc;
Elapsed time is 0.005586 seconds.
>> tic; norm(R, 2); toc;
Elapsed time is 3.019667 seconds.
>> tic; norm(R, inf); toc;
Elapsed time is 0.005393 seconds.
>>
我的问题是,为