我是Julia的新手,主要在Mathematica工作,所以我可能会有一些基本的错误.我试图计算Julia计算随机矩阵的特征系统的时间,并发现它比Mathematica慢5-6倍.
在朱莉娅:
D=1000*(rand(1000,1000)-0.5);
@time (E,F)=eig(D);
Out: elapsed time: 7.47950706 seconds (79638920 bytes allocated*)
Run Code Online (Sandbox Code Playgroud)
在Mathematica中:
First@Timing@Eigensystem[RandomReal[{-500, 500}, {1000, 1000}]]
Out: 1.310408
Run Code Online (Sandbox Code Playgroud)
对于2000 x 2000阵列来说它是相似的,虽然Julia结果的速度比Mathematica调用的速度略慢,但它仍然较慢; Julia需要22秒,而Mathematica会在8秒内计算出来.
至于我在线性代数的Julia标准库中读到的,通过调用LAPACK来实现分解,我认为这应该是非常好的,所以我很困惑为什么Julia代码运行得那么慢.有谁知道为什么会这样?它是在进行Mathematica不做的某种平衡或阵列对称检测吗?还是它实际上更慢?
此外,这是一个语法问题,可能是一个愚蠢的错误,但你如何改变朱莉娅的平衡?我试过了
@time (E,F)=eig(D[, balance=:nobalance]);
Run Code Online (Sandbox Code Playgroud)
完全像从Julia手册中复制和粘贴一样,但它只是给出了语法错误,所以出了点问题.
我正在使用Windows 7 64位,Julia版本0.2.0 64位,使用Steven Johnson网站上的说明安装,首先安装Anaconda以处理先决条件.我使用的是Mathematica学生版9.0.1.
编辑1:
执行versioninfo()屈服
Julia Version 0.2.0
Commit 05c6461 (2013-11-16 23:44 UTC)
Platform Info:
System: Windows (x86_64-w64-mingw32)
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY)
LAPACK: libopenblas
LIBM: libopenlibm
Run Code Online (Sandbox Code Playgroud)
所以看起来我正在使用openBLAS用于LAPACK和BLAS.一旦我获得了Mathematica实现信息,我也会添加它.
编辑2:
似乎Windows Mathematica 可能使用英特尔MKL BLAS.
我目前有一个带有数组属性的类型
immutable foo
a::Int64
b::Int64
x::Array{Float64,1} # One dimension array of Float 64, but no length info
end
Run Code Online (Sandbox Code Playgroud)
我知道该数组将始终包含100个Float64元素.有没有办法在类型注释中传递此信息?也许类似于声明实例化数组大小的方式类似x = Array(Float64, 100)?
问题是关于朱莉娅的"最佳实践".我读过这个和这个.我有一个功能
function discount_rate(n, fv, pmt, pv; pmt_type = 0)
...
end
Run Code Online (Sandbox Code Playgroud)
现在的问题是我必须像这样调用方法
discount_rate( 10, 10, 10, -10 )
Run Code Online (Sandbox Code Playgroud)
目前尚不清楚这些论点意味着什么 - 即使我忘了.我喜欢做的就是写作
discount_rate( n = 10, fv = 10, pmt = 10, pv = -10 )
Run Code Online (Sandbox Code Playgroud)
这更清楚:更容易阅读和理解.但我无法通过创建这些参数keywords参数或optional参数来定义我的方法,因为它们没有自然默认值.从设计的角度来看,有推荐的解决方法吗?
在Graphs.jl包中检查边是否存在边的首选方法是什么?
假设我们有一个GenericGraph G,我们想要检查a-> b是否在图中.我想有类似的东西,has_edge(G, a, b)但似乎不存在.
我目前正在使用in(a, in_neighbors(b, G))检查,但这可能效率很低.
我有两个函数用于在Julia中以数字方式确定pi.第二个函数(我认为是矢量化的)比第一个函数慢.为什么矢量化速度较慢?是否有规则何时进行矢量化以及何时不进行?
function determine_pi(n)
area = zeros(Float64, n);
sum = 0;
for i=1:n
if ((rand()^2+rand()^2) <=1)
sum = sum + 1;
end
area[i] = sum*1.0/i;
end
return area
end
Run Code Online (Sandbox Code Playgroud)
和另一个功能
function determine_pi_vec(n)
res = cumsum(map(x -> x<=1?1:0, rand(n).^2+rand(n).^2))./[1:n]
return res
end
Run Code Online (Sandbox Code Playgroud)
当运行n = 10 ^ 7时,以下是执行时间(运行几次后)
n=10^7
@time returnArray = determine_pi(n)
#output elapsed time: 0.183211324 seconds (80000128 bytes allocated)
@time returnArray2 = determine_pi_vec(n);
#elapsed time: 2.436501454 seconds (880001336 bytes allocated, 30.71% gc time)
Run Code Online (Sandbox Code Playgroud) 我知道该whos()函数将给出内存中所有对象的大小.这可能会很慢执行,并且有时会在某些对象上失败,从而使整个函数挂起.有没有办法获得特定对象的内存大小,类似于sys.getsizeof()Python中的函数?
在Python 3中,我愿意
print_me = "Look at this significant figure formatted number: {:.2f}!".format(floating_point_number)
print(print_me)
Run Code Online (Sandbox Code Playgroud)
要么
print_me = f"Look at this significant figure formatted number: {floating_point_number:.2f}!"
print(print_me)
Run Code Online (Sandbox Code Playgroud)
在朱莉娅
print_me = "Look at this significant figure formatted number: $floating_point_number"
print(print_me)
Run Code Online (Sandbox Code Playgroud)
但这会产生说法
Look at this significant figure formatted number: 61.61616161616161
Run Code Online (Sandbox Code Playgroud)
如何让Julia限制它显示的小数位数?请注意,据我所知,要打印的字符串的必要存储使用@printf宏来排除.
这有效,但在风格上似乎不正确.
floating_point_number = round(floating_point_number,2)
print_me = "Look at this significant figure formatted number: $floating_point_number"
print(print_me)
Run Code Online (Sandbox Code Playgroud) 我试图以一种非常无效的方式打破嵌套循环:
BreakingPoint = false
a=["R1","R2","R3"]
b=["R2","R3","R4"]
for i in a
for j in b
if i == j
BreakingPoint = true
println("i = $i, j = $j.")
end
if BreakingPoint == true; break; end
end
if BreakingPoint == true; break; end
end
Run Code Online (Sandbox Code Playgroud)
有更简单的方法吗?在我的实际问题,我没有什么是数组的想法a和b,除了他们是ASCIIString秒.数组名称(a以及b示例代码)也是通过元编程方法自动生成的.
给定模块层次结构
module A
module B; function foo end; end
module C
"""
bar(x)
Like [`foo`](@ref), but more `bar`.
"""
function bar end
end
end
Run Code Online (Sandbox Code Playgroud)
我怎么能从foo文档串交叉引用bar?我曾尝试A.B.foo,B.foo以及..B.foo没有成功.
我正在寻找Julia中的(希望内置)函数来计算组合的数量
我显然可以使用阶乘法来实现自己,但我几乎可以肯定有人已经对此感到担忧.