mathematica 发表于 2011-11-26 16:52:37

整理mathematica方程组求解结果的vim脚本

在使用mathematica求解得到方程组的求解结果时,
mathematica的求解结果粘贴出来的时候,往往很不
整齐,因此本人整理了一个vim脚本,专门用来整理
mathematica的求解结果。

比如用mathematica求解方程组
NSolve[{x + y + z + w == 20,
               x^2 - y^2 + z^2 - w^2 == 0,
               x^3 + y^3 + z^3 + w^3 == 7,
               x^4 + y^4 + z^4 - w^4 == 0},
                {x, y, z, w}]

如果直接从mathematica中粘贴出来的话,求解结果
如下:
{{x -> 11.5073 + 5.56279 I, y -> -11.4673, z -> 11.5073 - 5.56279 I,
w -> 8.45269}, {x -> 11.5073 - 5.56279 I, y -> -11.4673,
z -> 11.5073 + 5.56279 I,
w -> 8.45269}, {x -> -0.633291 + 6.87381 I,
y -> 4.66238 - 8.28313 I, z -> 7.79684 - 1.42422 I,
w -> 8.17408 + 2.83354 I}, {x -> -0.633291 - 6.87381 I,
y -> 4.66238 + 8.28313 I, z -> 7.79684 + 1.42422 I,
w -> 8.17408 - 2.83354 I}, {x -> 7.79684 - 1.42422 I,
y -> 4.66238 - 8.28313 I, z -> -0.633291 + 6.87381 I,
w -> 8.17408 + 2.83354 I}, {x -> 7.79684 + 1.42422 I,
y -> 4.66238 + 8.28313 I, z -> -0.633291 - 6.87381 I,
w -> 8.17408 - 2.83354 I}, {x -> 6.10028 + 2.52048 I,
y -> 6.07128 - 2.39536 I, z -> 3.99203 - 3.88309 I,
w -> 3.83641 + 3.75797 I}, {x -> 6.10028 - 2.52048 I,
y -> 6.07128 + 2.39536 I, z -> 3.99203 + 3.88309 I,
w -> 3.83641 - 3.75797 I}, {x -> 3.99203 + 3.88309 I,
y -> 6.07128 + 2.39536 I, z -> 6.10028 - 2.52048 I,
w -> 3.83641 - 3.75797 I}, {x -> 3.99203 - 3.88309 I,
y -> 6.07128 - 2.39536 I, z -> 6.10028 + 2.52048 I,
w -> 3.83641 + 3.75797 I}}

mathematica 发表于 2011-11-26 16:56:21

上面的求解结果很乱,所以本人写了一个vim的脚本,处理结果如下:
{x=11.5073+5.56279I,y=-11.4673,z=11.5073-5.56279I,w=8.45269},
{x=11.5073-5.56279I,y=-11.4673,z=11.5073+5.56279I,w=8.45269},
{x=-0.633291+6.87381I,y=4.66238-8.28313I,z=7.79684-1.42422I,w=8.17408+2.83354I},
{x=-0.633291-6.87381I,y=4.66238+8.28313I,z=7.79684+1.42422I,w=8.17408-2.83354I},
{x=7.79684-1.42422I,y=4.66238-8.28313I,z=-0.633291+6.87381I,w=8.17408+2.83354I},
{x=7.79684+1.42422I,y=4.66238+8.28313I,z=-0.633291-6.87381I,w=8.17408-2.83354I},
{x=6.10028+2.52048I,y=6.07128-2.39536I,z=3.99203-3.88309I,w=3.83641+3.75797I},
{x=6.10028-2.52048I,y=6.07128+2.39536I,z=3.99203+3.88309I,w=3.83641-3.75797I},
{x=3.99203+3.88309I,y=6.07128+2.39536I,z=6.10028-2.52048I,w=3.83641-3.75797I},
{x=3.99203-3.88309I,y=6.07128-2.39536I,z=6.10028+2.52048I,w=3.83641+3.75797I}
这个整理的结果应该比上面的好了不少。
对应的脚本见附件!
如果有建议的话,可以给我提出!
有注释是我写代码的一贯风格!

mathematica 发表于 2011-11-26 16:58:13

{x=11.5073+5.56279I   , y=-11.4673         , z=11.5073-5.56279I   , w=8.45269}          ,
{x=11.5073-5.56279I   , y=-11.4673         , z=11.5073+5.56279I   , w=8.45269}          ,
{x=-0.633291+6.87381I , y=4.66238-8.28313I , z=7.79684-1.42422I   , w=8.17408+2.83354I} ,
{x=-0.633291-6.87381I , y=4.66238+8.28313I , z=7.79684+1.42422I   , w=8.17408-2.83354I} ,
{x=7.79684-1.42422I   , y=4.66238-8.28313I , z=-0.633291+6.87381I , w=8.17408+2.83354I} ,
{x=7.79684+1.42422I   , y=4.66238+8.28313I , z=-0.633291-6.87381I , w=8.17408-2.83354I} ,
{x=6.10028+2.52048I   , y=6.07128-2.39536I , z=3.99203-3.88309I   , w=3.83641+3.75797I} ,
{x=6.10028-2.52048I   , y=6.07128+2.39536I , z=3.99203+3.88309I   , w=3.83641-3.75797I} ,
{x=3.99203+3.88309I   , y=6.07128+2.39536I , z=6.10028-2.52048I   , w=3.83641-3.75797I} ,
{x=3.99203-3.88309I   , y=6.07128-2.39536I , z=6.10028+2.52048I   , w=3.83641+3.75797I}
可以用align插件得到如上面的结果!

wayne 发表于 2011-11-26 22:28:48

3# mathematica
Mathematica其实非常擅长排版
输出结果的格式化有很多的丰富的函数 ,如 Grid,TableForm


如果想文本格式化的话,可以用Export

hujunhua 发表于 2011-12-1 12:00:28

Mathematica的Solve输出的是替换规则(Rules),应该是比较方便应用的。想排得好看的话可以很容易转成矩阵

hujunhua 发表于 2011-12-1 12:07:48

hujunhua 发表于 2011-12-1 12:13:54

mathematica 发表于 2012-8-3 18:35:04

3505
hujunhua 发表于 2011-12-1 12:07 http://bbs.emath.ac.cn/images/common/back.gif
为什么不像我一样既把代码贴出来,又把图片贴出来???????
页: [1]
查看完整版本: 整理mathematica方程组求解结果的vim脚本