为什么会有产生-1这个bug
mathematica吧
全部回复
仅看楼主
level 6
sgylvqº 楼主
a[0] := 0;
a[n_] := Piecewise[{{1,
a[n - 1] == 0}, {a[n - 1] +
2 (RandomVariate[BernoulliDistribution[(6 - a[n - 1])/6]] - 1/2),
a[n - 1] != 0 && a[n - 1] != 6}, {5, a[n - 1] == 6}}]
2021年04月10日 18点04分 1
level 6
sgylvqº 楼主
初始时刻t=0,位置x=0,每当时间+1进行1次状态转移,x=0必然转到x=1,x=1可能转到x=0和x=2,按理说位置x只能是正数或0,代码的写法也保证了x非负,但是a3却有可能产生-1,为什么???
2021年04月10日 18点04分 2
a[3]还会出现0,也是有问题的,奇数次转移根本不可能回到原点
2021年04月10日 18点04分
level 6
sgylvqº 楼主
a[#] & /@ Array[3 &, 100]
2021年04月10日 18点04分 3
level 6
sgylvqº 楼主
第一次对mma感到绝望,根本找不出bug在哪
2021年04月10日 18点04分 4
level 6
sgylvqº 楼主
地推有毒:
z[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{"1",
RowBox[{"x", "==", "0"}]},
{
RowBox[{"x", "+",
RowBox[{"2",
RowBox[{"(",
RowBox[{
RowBox[{"RandomVariate", "[",
RowBox[{"BernoulliDistribution", "[",
FractionBox[
RowBox[{"M", "+", "1", "-", "x"}],
RowBox[{"M", "+", "1"}]], "]"}], "]"}], "-",
FractionBox["1", "2"]}], ")"}]}]}],
RowBox[{
RowBox[{"x", "!=", "0"}], "&&",
RowBox[{"x", "!=",
RowBox[{"M", "+", "1"}]}]}]},
{"M",
RowBox[{"x", "==",
RowBox[{"M", "+", "1"}]}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{
"Columns" -> {{Left}}, "ColumnsIndexed" -> {},
"Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{
"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {},
"Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]},
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}, "RowsIndexed" -> {}},
Selectable->True]}
},
GridBoxAlignment->{
"Columns" -> {{Left}}, "ColumnsIndexed" -> {},
"Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{
"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {},
"Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]},
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}, "RowsIndexed" -> {}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\); M = 9;
Apply[Composition, Array[z &, 16]][#] & /@ Array[0 &, 100]
2021年04月11日 01点04分 5
复合就不会报错
2021年04月11日 01点04分
吧务
level 10
With[{prev=a[n-1]},Piecewise[]
]
2021年04月11日 04点04分 6
你要注意到,你定义中的多个a[n-1],在计算中会随机化为不同的值。使用With来仅计算一次且命名为prev,之后不再写a[n-1]而是用计算好的prev。
2021年04月11日 04点04分
2021年04月11日 04点04分
很有道理,十分感谢
2021年04月12日 01点04分
@asdasd1dsadsa 其实机制上还是有点区别的……不过这个可以用With解决是真的。
2021年05月01日 04点05分
1