我最近刚转到Julia1.1 Version 1.1.0 (2019-01-21),但我认为这也是报道的1.0版中的一个问题。为了总结一个虚拟变量,会出现一个错误:
julia> a = 0.0
0.0
for i in 1:10
a += 1
end
ERROR: UndefVarError: a not defined
现在,强制使用变量的范围:
julia> local a
julia> for i in 1:10
a += 1
end
ERROR: UndefVarError: a not defined
无论执行global
我有一个简单的while循环,它使用i = 1作为索引。
global i = 1
n = rows
while i <= n
if prod(isa.(collect((y)[i,:]),Number))==0
delete!(y,i)
x_axis = x_axis[1:end .!= i]
n -= 1
end
i += 1
end
但我发现了一个错误:
UndefVarError: i not defined
top-lev
SLURM (特别是s批处理)是否应该自动将用户脚本(而不是作业配置脚本)复制到集群的计算节点以供执行?从我的登录节点执行sbatch文件后,输出文件将在我的一个计算节点上创建,但包含以下内容:
ERROR: could not open file /home/pi/slurm.jl
Stacktrace:
[1] include at ./boot.jl:328 [inlined]
[2] include_relative(::Module, ::String) at ./loading.jl:1105
[3] include(::Module, ::String) at ./Base
在C编程语言中,#define指令允许在源代码中定义宏。这些宏定义允许声明常量值,以便在整个代码中使用。
宏定义不是变量,不能像变量一样被程序代码更改。在创建表示数字、字符串或表达式的常量时,我们通常使用此语法。像这样
#include <stdio.h>
#define NAME "Jack"
#define AGE 10
int main()
{
printf("%s is over %d years old.\n", NAME, AGE);
return 0;
}
好处是,如果代码中有多个函数,我就不需要将常量变量输入到每个函数
julia> debug("hello")
ERROR: UndefVarError: debug not defined
Stacktrace:
[1] top-level scope at REPL[6]:1
即使尝试过docker,也找不到合适的替代品。
help?> debug
search: @debug
Couldn't find debug
Perhaps you meant @debug, @enum, big or detach
No documentation found.
Binding debug does not ex
最近,我对朱莉娅-朗很感兴趣,因为它声称它是一种动态语言,具有接近C性能的特性。然而,到目前为止,我在这方面的经验并不好(至少在性能方面)。我正在编写的应用程序需要随机访问特定数组索引,然后将它们的值与其他特定数组索引进行比较(多次迭代)。下面的代码模拟了我在程序中的需要:我的Julia代码在大约8秒内完成了执行,而java脚本代码在chrome环境下需要的时间不到1秒!我在朱莉娅密码上做错什么了吗?提前谢谢。
朱莉娅代码:
n=5000;
x=rand(n)
y=rand(n)
mn=ones(n)*1000;
tic();
for i in 1:n;
for j in 1:n;
我有一个脚本main.jl,它打印一个简单的"Hello“字符串:
println("Hello world!")
但是,当试图像这样在终端上运行脚本时:
julia> main.jl
我知道错误:
ERROR: type #main has no field jl
我可以在网上找到的所有信息都建议像我运行脚本那样调用脚本。我已经保证我在正确的目录中--我做错了什么?
我知道for循环现在在Julia中是本地的。但有一件事我不明白。请考虑以下两个示例。
示例1
a = 0
for i = 1:3
a += 1
end
示例2
a = [0]
for i = 1:3
a[1] += 1
end
示例1抛出一条错误消息,这是我所理解的。但是,示例2的运行情况与预期一致。我该如何理解这一点?数组不是变量吗?有人能给我解释一下吗?
如何为julia创建文档的本地版本?我试过了:
/Applications/Julia-0.3.1.app/Contents/Resources/julia/share/julia/doc make html
fatal: Not a git repository (or any of the parent directories): .git
make: [juliadoc-pkg] Error 128 (ignored)
PYTHONPATH=:juliadoc sphinx-build -b html -d _build/doctrees . _build/html
Running
在Julia中,将变量定义为const与将变量定义为const global有什么区别?考虑到下面的示例,如果我将const global更改为const,有什么区别?
#set number of cores
number_cores=7;
addprocs(number_cores)
#include necessary functions
@everywhere include("$(pwd())\\lib.jl");
const global n = 2; # number of observables
const global k = nVAR + 2*n-1;
以下代码失败。
global Θ=1.0
function f(a)
c=sin(a+θ)
return c
end
f(1)
UndefVarError: θ not defined
Stacktrace:
[1] f(a::Int64)
@ Main ./In[1]:3
[2] top-level scope
@ In[1]:6
[3] eval
@ ./boot.jl:373 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::Stri
在之后,我决定用GNU对朱莉娅进行基准测试,结果与中所示的提速不一致。
我用CXXFLAGS='-std=c++11 -O3'编译了朱莉娅和got,结果如下:
a=0.9999;
tic;y=a.^(1:10000);toc
Elapsed time is 0.000159025 seconds.
tic;y=a.^(1:10000);toc
Elapsed time is 0.000162125 seconds.
tic;y=a.^(1:10000);toc
Elapsed time is 0.000159979 seconds.
--
tic;y=cumprod(o
例如:
函数main()
function doit()
A = ones(Int, 2)
return A
end
doit()
display(A)
A = [1, 2, 3]
display(A)
doit()
display(A)
另外,如果嵌套函数有两个或三个层,我们如何将局部变量值返回到main或将它们保存到main中?
对不起,我肯定漏掉了一些东西,但是为什么呢? 在基本循环中为每个元素报告一个内存分配,如下所示: 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
我试图分发一个将向量输出到数组中的函数。
我跟踪文章时使用了如下代码:
a = distribute([Float64[] for _ in 1:nrow(df)])
@sync @distributed for i in 1:nrow(df)
append!(localpart(a)[i], foo(df[i]))
end
但我得到了以下错误:
BoundsError:尝试访问索引147处的145个元素向量{Vector{Float64}}
由于我需要在共享数组中存储向量,所以我只使用过SharedArrays并行处理,这不是一个选项。任何建议都能挽救生命。