代码之家  ›  专栏  ›  技术社区  ›  Max

这个有标准功能吗?

  •  2
  • Max  · 技术社区  · 14 年前

    在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
    
    5 回复  |  直到 14 年前
        1
  •  6
  •   Janus    14 年前

    func Transpose[Most[l],Rest[l]] Partition

    func2[l_,g_,f_]:=g@@f@@@Partition[l,2,1]
    

    ListConvolve

    func3[l_,g_,f_]:=g@@ListConvolve[{1,1},l,{-1,1},{},Times,f]
    

    Through[{func,func2,func3}[l,Plus,(#1-#2)^2&]]
    Out[19]= {(a-b)^2+(b-c)^2+(c-d)^2,(a-b)^2+(b-c)^2+(c-d)^2,(a-b)^2+(b-c)^2+(c-d)^2}
    

    Total[Differences[l]^2]

    Out[14]= (-a+b)^2+(-b+c)^2+(-c+d)^2 
    
        2
  •  4
  •   Community CDub    7 年前

    f@@@Transpose[{args}] Thread[] The semantics of Mathematica's Thread function

    func[l_, g_, f_] := g @@ Thread[f[Most[l], Rest[l]]]
    

    Most[l], Rest[l]

        3
  •  3
  •   Dr. belisarius    14 年前

          func[l_, g_, f_] := g @@ Thread[f[Most@l, Rest@l]]
    

          func[l_, g_, f_] := g @@ f /@ Partition[l, 2, 1]
    

          func[l, Plus, (#[[1]] + #[[2]])^2 &]
    

        4
  •  3
  •   Timo    14 年前
     timeAvg[func_] := Module[{
        x = 0, y = 0, timeLimit = 0.1, p, q, 
        iterTimes = Power[10, Range[0, 10]]},
       Catch[
         If[(x = First[Timing[(y++; Do[func, {#}]);]]) > timeLimit,
            Throw[{x, y}]
            ] & /@ iterTimes
         ] /. {p_, q_} :> p/iterTimes[[q]]
       ];
    Attributes[timeAvg] = {HoldAll};
    

    timeAvg@func1[l, Plus, (#1 - #2)^2 &]
    

    l Timing Thread[]

        5
  •  2
  •   Michael Pilat    14 年前

    Transpose Most Rest Inner

    func[lis_, f_, g_] := Inner[f, Sequence@@Partition[list, Length[lis]-1, 1], g]
    
    In[90]:= func[l,Plus,(#-#2)^2&]
    Out[90]= (a - b)^2 + (b - c)^2 + (c - d)^2
    

    MapThread

    func2[lis_, g_, f_] := g @@ MapThread[f, Partition[lis, Length[lis]-1, 1]]
    
    In[94]:= func2[l, Plus, (# - #2)^2 &]
    Out[94]= (a - b)^2 + (b - c)^2 + (c - d)^2
    

    Differences ListConvolve

    TMTOWTDI