- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下问题。
我正在开发一个随机模拟器,该模拟器可以随机采样系统的配置,并存储在特定时间实例中每种配置被访问了多少次的统计信息。大致来说,代码是这样的
f[_Integer][{_Integer..}] :=0
...
someplace later in the code, e.g.,
index = get index;
c = get random configuration (i.e. a tuple of integers, say a pair {n1, n2});
f[index][c] = f[index][c] + 1;
which tags that configuration c has occurred once more in the simulation at time instance index.
?f
f[1][{1, 2}] = 112
f[1][{3, 4}] = 114
f[2][{1, 6}] = 216
f[2][{2, 7}] = 227
...
f[index][someconfiguration] = some value
...
f[_Integer][{_Integer..}] :=0
result = ExtractConfigurationsAndOccurences[f, 2]
result = {list1, list2}
list1 = {{1, 6}, {2, 7}} (* the list of configurations that occurred during the simulation*)
list2 = {216, 227} (* how many times each of them occurred *)
Cases
语句对其进行过滤。我意识到应该不惜一切代价避免这种程序,因为将有成倍地测试许多配置(定义),这大大降低了代码的速度。
最佳答案
这是我先前答案的完整重写。事实证明,在我以前的尝试中,我忽略了基于压缩数组和稀疏数组的组合的简单得多的方法,该方法比所有以前的方法都更快,内存更多-效率更高(至少在样本大小范围内,进行了测试),而仅以最小的方式更改了基于SubValues
的原始方法。由于询问了有关最有效方法的问题,因此我将从答案中删除其他方法(因为它们更加复杂并且占用了大量空间。希望看到它们的人可以查看此方法的以前的修订版)回答)。
原始的基于SubValues
的方法
我们首先介绍一个函数来为我们生成配置的测试样本。这里是:
Clear[generateConfigurations];
generateConfigurations[maxIndex_Integer, maxConfX_Integer, maxConfY_Integer,
nconfs_Integer] :=
Transpose[{
RandomInteger[{1, maxIndex}, nconfs],
Transpose[{
RandomInteger[{1, maxConfX}, nconfs],
RandomInteger[{1, maxConfY}, nconfs]
}]}];
我们可以生成一个小样本来说明:
In[3]:= sample = generateConfigurations[2,2,2,10]
Out[3]= {{2,{2,1}},{2,{1,1}},{1,{2,1}},{1,{1,2}},{1,{1,2}},
{1,{2,1}},{2,{1,2}},{2,{2,2}},{1,{2,2}},{1,{2,1}}}
在这里,我们只有2个索引,以及“x”和“y”数字都从1变为2的配置-10个此类配置。
SubValues
的计数器:
Clear[testAccumulate];
testAccumulate[ff_Symbol, data_] :=
Module[{},
ClearAll[ff];
ff[_][_] = 0;
Do[
doSomeStuff;
ff[#1][#2]++ & @@ elem;
doSomeMoreStaff;
, {elem, data}]];
此处的
doSomeStuff
和
doSomeMoreStaff
符号表示一些可能在计数代码之前或之后的代码。
data
参数应该是
generateConfigurations
产生的形式的列表。例如:
In[6]:=
testAccumulate[ff,sample];
SubValues[ff]
Out[7]= {HoldPattern[ff[1][{1,2}]]:>2,HoldPattern[ff[1][{2,1}]]:>3,
HoldPattern[ff[1][{2,2}]]:>1,HoldPattern[ff[2][{1,1}]]:>1,
HoldPattern[ff[2][{1,2}]]:>1,HoldPattern[ff[2][{2,1}]]:>1,
HoldPattern[ff[2][{2,2}]]:>1,HoldPattern[ff[_][_]]:>0}
以下函数将从
SubValues
列表中提取结果数据(索引,配置及其频率):
Clear[getResultingData];
getResultingData[f_Symbol] :=
Transpose[{#[[All, 1, 1, 0, 1]], #[[All, 1, 1, 1]], #[[All, 2]]}] &@
Most@SubValues[f, Sort -> False];
例如:
In[10]:= result = getResultingData[ff]
Out[10]= {{2,{2,1},1},{2,{1,1},1},{1,{2,1},3},{1,{1,2},2},{2,{1,2},1},
{2,{2,2},1},{1,{2,2},1}}
为了完成数据处理周期,以下是一个简单的函数,它基于
Select
为固定索引提取数据:
Clear[getResultsForFixedIndex];
getResultsForFixedIndex[data_, index_] :=
If[# === {}, {}, Transpose[#]] &[
Select[data, First@# == index &][[All, {2, 3}]]];
对于我们的测试示例,
In[13]:= getResultsForFixedIndex[result,1]
Out[13]= {{{2,1},{1,2},{2,2}},{3,2,1}}
据推测,这与@zorank在代码中尝试过的内容很接近。
In[14]:=
largeSample = generateConfigurations[20,500,500,5000000];
testAccumulate[ff,largeSample];//Timing
Out[15]= {31.89,Null}
现在,我们将从
SubValues
的
ff
中提取完整数据:
In[16]:= (largeres = getResultingData[ff]); // Timing
Out[16]= {10.844, Null}
这需要一些时间,但是一个操作只能执行一次。但是,当我们开始为固定索引提取数据时,我们发现它非常慢:
In[24]:= getResultsForFixedIndex[largeres,10]//Short//Timing
Out[24]= {2.687,{{{196,26},{53,36},{360,43},{104,144},<<157674>>,{31,305},{240,291},
{256,38},{352,469}},{<<1>>}}}
我们将在此处使用的主要思想是加快速度,将单个列表打包在
largeres
中,这些列表用于索引,组合和频率。虽然无法打包完整列表,但这些部分可以单独:
In[18]:= Timing[
subIndicesPacked = Developer`ToPackedArray[largeres[[All,1]]];
subCombsPacked = Developer`ToPackedArray[largeres[[All,2]]];
subFreqsPacked = Developer`ToPackedArray[largeres[[All,3]]];
]
Out[18]= {1.672,Null}
这也需要一些时间,但它又是一次性操作。
Clear[extractPositionFromSparseArray];
extractPositionFromSparseArray[HoldPattern[SparseArray[u___]]] := {u}[[4, 2, 2]]
Clear[getCombinationsAndFrequenciesForIndex];
getCombinationsAndFrequenciesForIndex[packedIndices_, packedCombs_,
packedFreqs_, index_Integer] :=
With[{positions =
extractPositionFromSparseArray[
SparseArray[1 - Unitize[packedIndices - index]]]},
{Extract[packedCombs, positions],Extract[packedFreqs, positions]}];
现在,我们有:
In[25]:=
getCombinationsAndFrequenciesForIndex[subIndicesPacked,subCombsPacked,subFreqsPacked,10]
//Short//Timing
Out[25]= {0.094,{{{196,26},{53,36},{360,43},{104,144},<<157674>>,{31,305},{240,291},
{256,38},{352,469}},{<<1>>}}}
我们的速度提高了30倍天真的
Select
方法。
Select
的复杂性相同,即基于所有索引的唯一组合的总列表长度是线性的。因此,从理论上讲,先前讨论的基于嵌套哈希表等的解决方案可能在渐近性上更好。问题是,实际上我们可能会在此之前很久就达到内存限制。对于1000万个配置示例,上述代码仍比我之前发布的最快解决方案快2-3倍。
Clear[getCombinationsAndFrequenciesForIndex];
getCombinationsAndFrequenciesForIndex[packedIndices_, packedCombs_,
packedFreqs_, index_Integer] :=
With[{positions =
extractPositionFromSparseArray[
SparseArray[Unitize[packedIndices - index], Automatic, 1]]},
{Extract[packedCombs, positions], Extract[packedFreqs, positions]}];
使代码仍然快两倍。而且,对于更稀疏的索引(例如,使用诸如
generateConfigurations[2000, 500, 500, 5000000]
之类的参数调用样本生成函数),相对于基于
Select
的函数而言,其提速约为100倍。
关于wolfram-mathematica - 从稀疏定义列表中选择无模式下调值的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7256563/
我记得 WRI 的某个人在 official newsgroup 中说出于兼容性目的,Mathematica 7 仍然有一个来自 Mathematica 5 的老式 Mathematica 帮助浏览器
除非巫师先生在度假,否则很难击败这种似乎具有无所不在和无所不知的现象。我们如何使用 Mathematica 和 StackOverflow API 超越他? 最佳答案 这是 Sjoerd 解决方案的一
您好,我无法理解以下函数的输出。 ListPointPlot3D[Table[Sin[x^2 + y], {x, 0, 3, 0.15}, {y, 0, 3
我在图形中显示了一组点: alt text http://img69.imageshack.us/img69/874/plc1k1lrqynuyshgrdegvfy.jpg 我想知道是否有任何命令可以
让 S是具有唯一元素的向量,并且 s它的一个子集,也有独特的元素;例如,S={1,2,3,4,5,6}和 s={1,3,4,6} .现在给出另一个向量 c={7,8,9,7} ,如何创建矢量 C=[7
我想知道如果给定多个替换规则,mma 如何确定在发生碰撞时首先应用哪个规则。一个例子是: x^3 + x^2*s + x^3*s^2 + s x /. {x -> 0, x^_?OddQ -> 2}
在mathematica 中(我使用的是 mma 5.0(猜猜很老)),如果我将以下内容作为一行输入: Needs["Graphics`Master`"]; Animate[Plot[Sin[n x]
如果我有一个字母表,假设 sigma = {1,2,3,4,a,b,c,d,e,f,g} 并且想要生成所有长度为 n 的单词,有没有办法做到这一点? 我可以做 Flatten[Outer[Functi
生成参数列表的素数列表的最简单函数是什么?想出这样的函数并不难,比如: foo[n_] := Block[{A = {}, p = 2}, While[p All] 关于wol
在此示例中,将函数定义为“functionB”时,这是一个奇怪的结果。有人可以解释吗?我想绘制functionB[x]和functionB[Sqrt[x]],它们必须不同,但是此代码显示functio
如何在 Mathematica 中对列表进行数值微分而不先将其拟合为数学表达式(即使用 FindFit )? 具体来说,我想在列表中找到最大斜率的点。 我已经考虑使用 Differences并找到最大
我正在寻找 Mathematica 中原子对象的完整列表(其中 AtomQ 产生 True)。 我知道 Symbol String Integer Real Rational Complex Spar
最近发现了一些old post在 MathGroup 上使用未记录的命令行选项 -batchinput和 -batchoutput显示: If test.mma contains the follo
Mathematica 似乎缺少此功能,或者无论如何我都找不到它。 Series function 可以对多个变量进行连续扩展,但它似乎无法进行完整的多元扩展。 有谁知道如何做到这一点? 谢谢 最佳答
我有兴趣运行相同的函数,该函数以并行方式在多核上使用不同的参数值进行一些蒙特卡罗评估。我还想确保整个函数在同一个内核上运行,而函数内的计算不会跨内核分布。例如,假设我有一个函数(故意简化) f[a_,
我怎样才能从 Range[96] 中不重复地获得 48 个随机对的两个不同元素?即 96 个元素只使用一次。 当我尝试将元组/子集与 Select 结合使用时,我觉得必须有一种更直接的方法来实现这一点
我正在评估 Wolfram Mathematica 8 的大学类(class)项目。 我很难定义目标编程模型。 首先是因为我在数学方面真的很匮乏 :) 其次,因为我在 official documen
我开发了一些代码来从 LogNormalDistribution 和 StableDistribution 的乘积生成随机变量: LNStableRV[{\[Alpha]_, \[Beta]_, \[
我有一个想要优化的字符串验证函数。该字符串的长度为 2n,由 0 和 1 组成,例如 str="100001"。我想测试一下: 1) 字符串中奇数索引位置的 1 的数量(必须不少于 1)是否等于偶数索
请考虑: Function[subID, pointSO[subID] = RandomInteger[{1, 4}, {5, 2}]] /@ {"subA", "subB"}; M
我是一名优秀的程序员,十分优秀!