500*_*500 5 layout wolfram-mathematica
我现在已经看到了在这个论坛上布局代码的不同优雅方式.
作为初学者,我想就如下布局代码的最佳布局方式提出建议.
这是丑陋的,不是最优的,甚至可能是愚蠢的,但专家程序员可能会原谅我,我将其用作"最坏情况".
我是新手的目的是清晰.
rejection[disp_, fixationNOtoConsiderForDuration_, durationLimit_,
minDistance_] :=
With[{fakedata = consider[t4dataLAEH10, 9, disp, {17, 18, 11}]},
With[{num =
Flatten[Position[
Take[fakedata[[All, 3]], fixationNOtoConsiderForDuration],
x_ /; (x > durationLimit)]]},
If[num =!= {},
With[{fakedata1 = Drop[fakedata[[All, {1, 2}]], Last@num]},
With[{num1 =
Flatten[Position[
Table[
Sqrt[((fakedata1[[fixation1, 1]] -
centerX)^2 + (fakedata1[[fixation1, 2]] -
centerY)^2)],
{fixation1, 1, Length@fakedata1}],
x_ /; (x < minDistance)]]},
If[num1 =!= {},
Delete[fakedata1[[All, {1, 2}]], List /@ num1],
fakedata[[All, {1, 2}]]]]],
With[{fakedata2 = fakedata[[All, {1, 2}]]},
With[{num2 =
Flatten[Position[
Table[
Sqrt[((fakedata2[[fixation2, 1]] -
centerX)^2 + (fakedata2[[fixation2, 2]] -
centerY)^2)],
{fixation2, 1, Length@fakedata2}],
x_ /; (x < minDistance)]]},
If[num2 =!= {},
Delete[fakedata2[[All, {1, 2}]], List /@ num2],
fakedata[[All, {1, 2}]]]]]]]]
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,我将其拆分Flatten[Position[...]]为一个单独的函数.
我还使用单个作用域构造而不是嵌套With.
喜欢:
Block[{fakedata, fakedata1, fakedata2, num, result},
fakedata = ...;
num = ...;
If[num =!= {},
fakedata1 = ...;
result = localPoints[fakedata1];
];
num1 = ...;
If[num1 =!= {},
fakedata2 = ...;
result = localPoints[fakedata2];
];
result
]
Run Code Online (Sandbox Code Playgroud)
我选择进行完全重写,因为原始代码与我自己的风格相差太远,无法像我自己的代码那样格式化它。
我没有尝试对此进行测试,因此完全有可能出现问题,但我相信其中大部分是正确的。
我使用文本单元格来描述函数的语法。我通常会嵌入(* comments *)来解释代码,但如果解释变得很长,我也会将它们移至文本单元格。
我添加了一条解释的评论func1。这不是一个很有帮助的评论,但它可以作为一个例子。
这是我的笔记本电脑放大 75% 的图像:

复制粘贴的单元格表达式:
Cell[CellGroupData[{Cell["Rewrite", "Subsection"],
Cell[TextData[{
StyleBox["distance", "Program"],
"[{",
StyleBox["x1",
FontSlant->"Italic"],
", ",
StyleBox["y1",
FontSlant->"Italic"],
"}] gives EuclideanDistance from {x1, y1} to global {centerX, \
centerY}\n",
StyleBox["distance", "Program"],
"[{",
StyleBox["x1",
FontSlant->"Italic"],
", ",
StyleBox["y1",
FontSlant->"Italic"],
"}, {",
StyleBox["x2",
FontSlant->"Italic"],
", ",
StyleBox["y2",
FontSlant->"Italic"],
"}] gives EuclideanDistance from {x1, y1} to {x2, y2}"
}], "Text"],
Cell[BoxData[
RowBox[{
RowBox[{"distance", "[",
RowBox[{"a_", ",",
RowBox[{"b_:",
RowBox[{"Hold", "@",
RowBox[{"{",
RowBox[{"centerX", ",", "centerY"}], "}"}]}]}]}], "]"}], ":=",
RowBox[{"EuclideanDistance", "[",
RowBox[{"a", ",",
RowBox[{"ReleaseHold", "@", "b"}]}], "]"}]}]], "Input"],
Cell[TextData[{
StyleBox["rejection", "Program"],
"[",
StyleBox["disp, fixation, durationLimit, minDistance",
FontSlant->"Italic"],
"]\n\nfilters data from ",
StyleBox["t4dataLAEH10", "Program"],
" according to:\n\t",
StyleBox["disp",
FontSlant->"Italic"],
" : (description of argument disp)\n\t",
StyleBox["fixation",
FontSlant->"Italic"],
" : (description of argument fixation)\n\t",
StyleBox["durationLimit",
FontSlant->"Italic"],
" : (description of durationLimit)\n\t",
StyleBox["minDistance",
FontSlant->"Italic"],
" : (description of minDistance)"
}], "Text"],
Cell[BoxData[
RowBox[{
RowBox[{"rejection", "[",
RowBox[{
"disp_", ",", "fixation_", ",", "durationLimit_", ",",
"minDistance_"}], "]"}], ":=", "\[IndentingNewLine]",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"fakedata", ",", "num", ",", "func1"}], "}"}], ",",
"\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{"description", " ", "of", " ", "fakedata"}], " ", "*)"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{"fakedata", "=",
RowBox[{"consider", "[",
RowBox[{"t4dataLAEH10", ",", "9", ",", "disp", ",",
RowBox[{"{",
RowBox[{"17", ",", "18", ",", "11"}], "}"}]}], "]"}]}], ";",
"\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{"description", " ", "of", " ", "num"}], " ", "*)"}],
"\[IndentingNewLine]",
RowBox[{"num", "=",
RowBox[{"Position", "[",
RowBox[{
RowBox[{
RowBox[{"fakedata", "\[LeftDoubleBracket]",
RowBox[{"All", ",", "3"}], "\[RightDoubleBracket]"}], "~",
"Take", "~", "fixation"}], ",",
RowBox[{"x_", "/;",
RowBox[{"x", ">", "durationLimit"}]}]}], "]"}]}], ";",
"\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{
RowBox[{"func1", ":", " ",
RowBox[{
"Take", " ", "the", " ", "first", " ", "two", " ", "columns",
" ", "of", " ", "fakedata"}]}], ",", " ",
RowBox[{
RowBox[{
RowBox[{
"and", " ", "drop", " ", "rows", " ", "specified", " ", "by",
" ",
RowBox[{
StyleBox["dropspec",
FontSlant->"Italic"], ".", "\[IndentingNewLine]",
"Delete"}], " ", "any", " ", "rows", " ", "for", " ",
"which", " ",
StyleBox["distance", "Program"]}], " ", "<", " ",
StyleBox["minDistance",
FontSlant->"Italic"]}], ";", " ",
RowBox[{
"if", " ", "no", " ", "rows", " ", "are", " ", "deleted"}]}],
",", "\[IndentingNewLine]", " ",
RowBox[{
"return", " ", "the", " ", "first", " ", "two", " ", "columns",
" ", "of", " ", "fakedata"}], ",", " ",
RowBox[{"ignoring", " ",
RowBox[{"dropspec", "."}]}]}],
StyleBox[" ",
FontSlant->"Italic"], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"func1", "[", "dropspec_", "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"part", ",", "fake"}], "}"}], ",",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"part", "=",
RowBox[{"fakedata", "\[LeftDoubleBracket]",
RowBox[{"All", ",",
RowBox[{"{",
RowBox[{"1", ",", "2"}], "}"}]}],
"\[RightDoubleBracket]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"fake", "=",
RowBox[{"part", "~", "Drop", "~", "dropspec"}]}], ";",
"\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{"#", "=!=",
RowBox[{"{", "}"}]}], ",",
RowBox[{"fake", "~", "Delete", "~", "#"}], ",", "part"}],
"]"}], "&"}], "@", "\[IndentingNewLine]",
RowBox[{"Position", "[",
RowBox[{
RowBox[{"distance", "/@", "fake"}], ",",
RowBox[{"x_", "/;",
RowBox[{"x", "<", "minDistance"}]}]}], "]"}]}]}]}],
"]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{"num", "=!=",
RowBox[{"{", "}"}]}], ",",
RowBox[{"func1", " ", "@", " ",
RowBox[{"num", "\[LeftDoubleBracket]",
RowBox[{
RowBox[{"-", "1"}], ",", "1"}], "\[RightDoubleBracket]"}]}],
",",
RowBox[{"func1", "@", "0"}]}], "]"}]}]}], " ",
"]"}]}]], "Input"]
}, Open ]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
529 次 |
| 最近记录: |