有这个标准功能吗?
Mathematica 中可能有内置函数或更好更快的方法来执行此操作
func[l_, g_, f_] := g @@ f @@@ Transpose[{Most[l], Rest[l]}]
,可用于执行此类操作
l = {a, b, c, d}
func[l, Plus, (#1 - #2)^2 &]
我不知道此类函数的正确名称。折叠拉链风格的东西。
更新 很多解决方案。谢谢大家。
使用
Partition[l, 2, 1]
代替
Transpose[{Most[l], Rest[l]}]
肯定会让它更清楚。
我尝试对函数运行计时,但得到了奇怪的结果:
func1[l_, g_, f_] := g @@ f @@@ Transpose[{Most[l], Rest[l]}]
func2[l_, g_, f_] := g @@ f @@@ Partition[l, 2, 1]
func3[l_, g_, f_] := g @@ ListConvolve[{1, 1}, l, {-1, 1}, {}, Times, f]
func4[l_, g_, f_] := g @@ Thread[f[Most@l, Rest@l]]
func5[l_, g_, f_] := g @@ f /@ Partition[l, 2, 1]
func6[l_, g_, f_] := g @@ Thread[f[Most[l], Rest[l]]]
func7[l_, f_, g_] := Inner[f, Sequence @@ Partition[l, Length[l] - 1, 1], g]
func8[l_, g_, f_] := g @@ MapThread[f, Partition[l, Length[l] - 1, 1]]
functions = {func1, func2, func3, func4, func5, func6, func7, func8}
input = Table[ToExpression["x" <> ToString[i]], {i, 1, 1000000}];
inputs = Table[Take[input, i*100000], {i, 1, 10}];
Table[
If[i == j == 0, "",
If[j == 0, functions[[i]],
If[i == 0, Length[inputs[[j]]],
Timing[functions[[i]][inputs[[j]]]][[1]]]]],
{i, 0, Length[functions]}, {j, 0, Length[inputs]}] // Transpose // TableForm
There is probably built-in function or a better and faster way to do this in Mathematica
func[l_, g_, f_] := g @@ f @@@ Transpose[{Most[l], Rest[l]}]
which can be used to do things like this
l = {a, b, c, d}
func[l, Plus, (#1 - #2)^2 &]
I don't know the proper name for this kind of function. Something in a fold-zip genre.
UPDATE
Lot's of solutions. Thanks to everyone.
Using
Partition[l, 2, 1]
instead of
Transpose[{Most[l], Rest[l]}]
definitely makes it clearer.
I've tried to run timings on the functions, but I get strange results:
func1[l_, g_, f_] := g @@ f @@@ Transpose[{Most[l], Rest[l]}]
func2[l_, g_, f_] := g @@ f @@@ Partition[l, 2, 1]
func3[l_, g_, f_] := g @@ ListConvolve[{1, 1}, l, {-1, 1}, {}, Times, f]
func4[l_, g_, f_] := g @@ Thread[f[Most@l, Rest@l]]
func5[l_, g_, f_] := g @@ f /@ Partition[l, 2, 1]
func6[l_, g_, f_] := g @@ Thread[f[Most[l], Rest[l]]]
func7[l_, f_, g_] := Inner[f, Sequence @@ Partition[l, Length[l] - 1, 1], g]
func8[l_, g_, f_] := g @@ MapThread[f, Partition[l, Length[l] - 1, 1]]
functions = {func1, func2, func3, func4, func5, func6, func7, func8}
input = Table[ToExpression["x" <> ToString[i]], {i, 1, 1000000}];
inputs = Table[Take[input, i*100000], {i, 1, 10}];
Table[
If[i == j == 0, "",
If[j == 0, functions[[i]],
If[i == 0, Length[inputs[[j]]],
Timing[functions[[i]][inputs[[j]]]][[1]]]]],
{i, 0, Length[functions]}, {j, 0, Length[inputs]}] // Transpose // TableForm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想要完全复制
func
功能的东西,我能想到的唯一漂亮的方法是将Transpose[Most[l],Rest[l]]
替换为 < code>Partition:如果你真的想要一些“内置”的东西,你可以破解一些
ListConvolve
来获得乐趣检查所有这些工作:
最后,如果这是你正在寻找的答案,我建议通过
Total[Differences[l]^2] 来计算它
If you want something that exactly duplicates the functionality of your
func
, the only prettyfication I can think of is replacingTranspose[Most[l],Rest[l]]
withPartition
:If you really want something "built in", you could hack on some
ListConvolve
for kicksChecking that all these work:
Finally, if this is the answer you are looking for, I would suggest computing it by
Total[Differences[l]^2]
每当您看到类似
f@@@Transpose[{args}]
之类的内容时,您应该想到Thread[]
。 Mathematica Thread 函数的语义对此进行了讨论。所以我能做的最好的事情就是
,但是
Most[l], Rest[l]
构造仍然看起来丑陋且低效。可能有一种更有效的方法来做到这一点 - 但也许这是 Mathematica 中所能达到的最紧凑的方法。Whenever you see something like
f@@@Transpose[{args}]
you should think ofThread[]
. This was discussed in The semantics of Mathematica's Thread function.So the best I could do was
but the
Most[l], Rest[l]
construction still seems ugly and inefficient. There is probably a more efficient way to do it - but maybe this is as compact as it can get in Mathematica.相当于(不是说更好):
或者几乎
但是这最后一个需要
哪个明显较差
Equivalent to (not saying it's better):
Or almost
But this last one needs
Which is clearly inferior
这不是一个答案,而是一个更好的计时例程的建议。这
将很快计算平均运行时间,例如,
尝试
l
的不同长度给出以下结果其中 N 是
l
中的元素数量。Thread[]
是明显的赢家。This is not an answer but a suggestion for a better timing routine. This
will calculate an average run time quite quickly, e.g.,
Trying out different lengths for
l
gives the following resultwhere N is the number of elements in
l
.Thread[]
is the clear winner.我认为这只是一个广义内积(广义点积),以
Transpose
/Most
/Rest
位为模,所以你也可以使用Inner
:还有
MapThread
,我为了完整性而提到:如果你总是做出差异正如其他人提到的,在列表中,
Differences
或ListConvolve
可能会更快。与 Perl 一样,TMTOWTDI 在
Mathematica 也是如此,正如您问题的所有答案所示!
I think that's just an generalized inner product (generalized dot product), modulo the
Transpose
/Most
/Rest
bit, so you could also just useInner
:There's also
MapThread
, which I mention for completeness:If you're always making differences out of the list,
Differences
orListConvolve
could be faster, as others have mentioned.Like Perl, TMTOWTDI in
Mathematica too, as all the answers to your question show!