怎么把一个式子中加减分开
mathematica吧
全部回复
仅看楼主
level 8
比如a - b + c - d + e,
我想要
a+c+e-(b+d)
2013年02月18日 08点02分 1
level 8
要处理的式子比较长,这个简化了的情况
2013年02月18日 09点02分 2
level 10
提取出正负项比较简单,构成这样的:a+c+e-(b+d)格式比较麻烦。。。你的式子大概是怎么样的?
2013年02月18日 09点02分 5
level 10
Total@Flatten[ GatherBy[List @@ (a - b + c - d + e), Head@# == Times &]]
2013年02月18日 09点02分 6
Total@一下岂不是就打回原形了
2013年02月18日 10点02分
回复 0577216 :[拍砖]要先Unprotect把Orderless属性去掉。。。
2013年02月18日 11点02分
回复 mm_酱 :我以为Total是没有Orderless属性的,原来Total用于符号的时候就变成Plus了
2013年02月18日 11点02分
level 9
我晕,绕了一大圈才弄成列表[拍砖]后面咋整
input = a - b + c - d + e;Total /@ GatherBy[ToExpression[StringReplace[\!\(\*TagBox[StyleBox[RowBox[{"\"\<\>\"", "<>", RowBox[{"ToString", "[", RowBox[{"FullForm", "@", "input"}], "]"}], "<>", "\"\<\>\""}],ShowSpecialCharacters->False,ShowStringCharacters->True,NumberMarks->True],FullForm]\), "Plus" -> "List"]], Head@# == Times &]
2013年02月18日 09点02分 7
input = a - b + c - d + e; Total /@ GatherBy[ToExpression[StringReplace[ToString[FullForm@input],"Plus" -> "List"]], Head@# == Times &]为啥显示这么多。。。。
2013年02月18日 09点02分
level 9
写了个罗嗦的partMinusPlus
input = a - b + 3 c - 2 d + e;
partMinusPlus[expr_] := Module[{minuspart},minuspart = Total@Cases[expr, Times[n_ /; n < 0, _]]; {expr - minuspart, minuspart}];
partMinusPlus[input]
结果为:
{a + 3 c + e, -b - 2 d}
2013年02月18日 10点02分 8
level 9
假如楼长只要形式的话,这个输出为String的partMinusPlusString或许可用一用:
input = a - b + 3 c - 2 d + e;
partMinusPlusString[expr_] := Module[{minuspart}, minuspart = Total@Cases[expr, Times[n_ /; n < 0, _]]; ((expr - minuspart) // ToString) <> "-" <> "(" <> ((-minuspart) // ToString) <> ")"];
partMinusPlusString[input]
输出为
"a + 3 c + e-(b + 2 d)"
2013年02月18日 11点02分 9
level 9
终于写出了一个拙劣但能work的版本,试一试这个partMinusPlusNew:
input = a - b + 3 c - 2 d + e;
partMinusPlusNew[expr_] := Module[{minuspart}, minuspart = Total@Cases[expr, Times[n_ /; n < 0, _]]; ("HoldForm[" <> ((expr - minuspart) // ToString) <> "-" <> "(" <> ((-minuspart) // ToString) <> ")]") // ToExpression];
partMinusPlusNew[input]
2013年02月18日 11点02分 10
【注意:】使用任何含有ToString或ToExpression命令的程序,一切后果自负
2013年02月18日 11点02分
level 10
Unprotect[Plus]; ClearAttributes[Plus, Orderless]; Plus @@ Flatten[GatherBy[List @@ (a - b + c - d + e), Head@# == Times &]]
2013年02月18日 11点02分 12
level 10
没用Unprotect和ToString的版本
2013年02月18日 12点02分 13
原来还有ToBoxes这种家伙,学习了~~~
2013年02月19日 03点02分
不过让我鸡蛋里挑一挑骨头:试一试input=a + b - 3 c/t + Sin[\[Omega] t - k x]; 就在Sin里扔一负号(坏笑)
2013年02月19日 03点02分
吧务
level 9
发现本吧好多人都是“相对论”吧的人啊
2013年02月18日 12点02分 14
水吧。
2013年02月18日 13点02分
回复 bobdaft :怪怪的感觉。
2013年02月18日 13点02分
level 10
cond = Times[_,_?(# < 0 &)]; Plus @@ HoldForm /@ (Plus @@@ (Cases[k - 4 b + 3 d - 4 c + 6 e, #] & /@ {Except[cond], cond}))
2013年02月19日 03点02分 15
level 10
cond = Times[_, _?(# < 0 &)]; Subtract @@ HoldForm /@ ({Plus @@ #[[1]], -Plus @@ #[[2]]} &@(Cases[k - 4 b + 3 d - 4 c + 6 e, #] & /@ {Except[cond], cond}))
2013年02月19日 03点02分 16
这个算是目前最好的版本了[顶]
2013年02月19日 04点02分
level 9
仿mm酱的partMinusPlusmmjiang函数(百度居然吞代码!是不是这货对RowBox之类也敏感啊[瀑布汗~]):
输出为:a+(3 c)/t -b-2 d-Sin[k x-t \[Omega]]
2013年02月19日 04点02分 17
1