我想编译一个我创建的自定义函数,以加快计算时间,这已经很小了,但是我把这个函数称为“几次”。
一些虚拟数据:
DD = 20; Days = 4; Peak = 1;
Loading =
Table[Table[{RandomReal[{-1500, 1500}],
RandomReal[{-1, 1}/100]}, {i, 1, 150}], {j, 1, Days}];
coo = Loading[[All, All, 1]];
功能:
Clear[TimeSwapFunctionTEST];
TimeSwapFunctionTEST[lst_ /; (And @@ (NumericQ /@ lst))] :=
Norm[With[{z =
Sort[Partition[
Flatten[Riffle[Flatten[coo + lst],
Flatten[Loading[[1 ;; Days, All, Peak + 1]]]]],
2], #1[[1]] < #2[[1]] &]},
Mean[Select[
Partition[
Riffle[Mean[Transpose[Partition[z[[All, 1]], DD]]],
Abs[Mean[
Transpose[
Partition[z[[All, 2]], DD]]]/(StandardDeviation[
Transpose[Partition[z[[All, 2]], DD]]]/Sqrt[DD])]],
2], #1[[1]] > 0 &][[All, 2]]]]]
一千次射击的计算时间约为6“
AbsoluteTiming[Do[TimeSwapFunctionTEST[{0, 0, 0, 0}], {i, 1, 1000}]]
{6.0323450, Null}
我已经尝试了但是我对参数lst_ /; (And @@ (NumericQ /@ lst))
有一些问题所以我必须删除它(但我最后会需要它)。
所以如果我试过这个:
Clear[TimeSwapFunctionTESTCompiled];
TimeSwapFunctionTESTCompiled =
Compile[{{lst(*/;(And@@(NumericQ/@lst))*), _Real}},
With[{x =
Sort[Partition[
Flatten[Riffle[Flatten[coo + lst],
Flatten[Loading[[1 ;; Days, 1 ;; -1, Peak + 1]]]]],
2], #1[[1]] < #2[[1]] &]},
Mean[Select[
Partition[
Riffle[Mean[Transpose[Partition[x[[All, 1]], DD]]],
Abs[Mean[
Transpose[
Partition[x[[All, 2]], DD]]]/(StandardDeviation[
Transpose[Partition[x[[All, 2]], DD]]]/Sqrt[DD])]],
2], #1[[1]] > 0 &][[All, 2]]]]]
我收到了这些错误:
Compile::part: Part specification Compile`FunctionVariable$25889[[All,2]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>
Compile::cplist: Compile`$5 should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function. >>
Compile::cplist: Compile`$5 should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function. >>
Compile::cplist: Compile`$6 should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function. >>
General::stop: Further output of Compile::cplist will be suppressed during this calculation. >>
Compile::part: Part specification Compile`FunctionVariable$25889[[All,1]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>
Compile::part: Part specification Compile`FunctionVariable$25889[[All,1]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>
General::stop: Further output of Compile::part will be suppressed during this calculation. >>
你有什么想法来编译吗? 感谢