相关疑难解决方法(0)

左对右链表,替换速度

在Mathematica中有两种明显的方法来构建链表,"左":

{1, {2, {3, {4, {5, {6, {7, {}}}}}}}}
Run Code Online (Sandbox Code Playgroud)

并且"正确":

{{{{{{{{}, 7}, 6}, 5}, 4}, 3}, 2}, 1}
Run Code Online (Sandbox Code Playgroud)

这些可以用:

toLeftLL = Fold[{#2, #} &, {}, Reverse@#] & ;

toRightLL = Fold[List, {}, Reverse@#] & ;
Run Code Online (Sandbox Code Playgroud)

如果我使用这些,并简单ReplaceRepeated地浏览链表,我会得到截然不同的Timing结果:

r = Range[15000];
left = toLeftLL@r;
right = toRightLL@r;

Timing[i = 0; left //. {head_, tail_} :> (i++; tail); i]
Timing[i = 0; right //. {tail_, head_} :> (i++; tail); i]

(* Out[6]= {0.016, 15000} *)

(* Out[7]= {5.437, 15000} …
Run Code Online (Sandbox Code Playgroud)

optimization wolfram-mathematica linked-list

11
推荐指数
1
解决办法
386
查看次数

斐波那契的表现

f[0] = 0;
f[1] = 1;
f[x_] := f[x-1] + f[x-2]
Run Code Online (Sandbox Code Playgroud)

这个功能在Mathematica中运行缓慢,我需要提高速度.我必须使用函数式编程和递归.我不确定为什么这么慢,甚至最轻微的想法如何改善这将是有帮助的.

performance wolfram-mathematica fibonacci

6
推荐指数
1
解决办法
1607
查看次数

在Mathematica中查找类似但不相同的元素

我有一个数字列表.我想从列表中提取出一些属于某些波段并具有一些最小长度的数字.例如,假设我想在此列表上操作:

thisList = {-1.2, -1.8, 1.5, -0.6, -0.8, -0.1, 1.4, -0.3, -0.1, -0.7}
Run Code Online (Sandbox Code Playgroud)

band=1runLength=3.我想拥有

{{-0.6, -0.8, -0.1}, {-0.3, -0.1, -0.7}}
Run Code Online (Sandbox Code Playgroud)

作为结果.现在我正在使用

Cases[
 Partition[thisList,runLength,1],
 x_ /; Abs[Max[x] - Min[x]] < band
]
Run Code Online (Sandbox Code Playgroud)

主要问题是运行重叠的地方,我得到了许多运行副本.例如,使用

thisList = {-1.2, -1.8, 1.5, -0.6, -0.8, -0.1, -0.5, -0.3, -0.1, -0.7}
Run Code Online (Sandbox Code Playgroud)

给我

{{-0.6, -0.8, -0.1}, {-0.8, -0.1, -0.5}, {-0.1, -0.5, -0.3}, {-0.5, -0.3, -0.1}, {-0.3, -0.1, -0.7}}
Run Code Online (Sandbox Code Playgroud)

我宁愿拥有的地方

{-0.6, -0.8, -0.1, -0.5, -0.3, -0.1, -0.7}
Run Code Online (Sandbox Code Playgroud)

没有做一些重复结果的减少.什么是正确的方法?如果它不涉及使用爆炸数据,那就太好了Partition.

wolfram-mathematica

5
推荐指数
1
解决办法
349
查看次数

在处理递归函数时如何提高(mathematica)性能?

背景.我想打印一张31 ^(1/2)的会聚表.我对表进行了以下递归定义.(交换31 ^(1/2)黄金比率,下表将包含斐波那契系列).

 cf := ContinuedFraction
 tf := TableForm
 p[-1] = 0; p[0] = 1; q[-1] = 1; q[0] = 0;
 a[k_] := cf[Sqrt[31], k][[k]]
 p[k_] := a[k]*p[k - 1] + p[k - 2]
 q[k_] := a[k]*q[k - 1] + q[k - 2]
 s[n_] := Timing[Table[{k, a[k], p[k], q[k]}, {k, 8, 8 n, 8}]] // tf
Run Code Online (Sandbox Code Playgroud)

时间以指数方式快速增长.我不得不alt +.(中止)s [4].

问题:如何在处理递归函数时提高(mathematica)性能?

algorithm wolfram-mathematica

3
推荐指数
2
解决办法
849
查看次数