24小时热门版块排行榜    

CyRhmU.jpeg
查看: 426  |  回复: 1

tang76

铜虫 (小有名气)

[求助] 请教个mathematica的问题

mathematica输出表达式后,能否定义规则让它化简?

如以下代码:
sol = Solve[{x^2 + y^2 + z^2 == 1, x + y + z == a , x*y*z == b}, {x,
    y, z}];
xx = x /. sol
yy = y /. sol
zz = z /. sol
Print["Writing Fortran Code . . . : / "];
SetDirectory["F:\\tang\\mathtest"];
strm = OpenWrite["test.f90", FormatType -> FotranForm,
   PageWidth -> 70];
(* write subroutine of invisopar*)
WriteString[strm, "subroutine test(x,y,z,a,b)\n"];
WriteString[strm, "implicit none\n"];
WriteString[strm, "real*8::x,y,z,a,b\n"];
nroot = Length[xx];
For[ii = 1, ii <= nroot,
  WriteString[strm,
   "x = " <> ToString[FortranForm[xx[[ii]]]] <> "\n"]; ii++];
For[ii = 1, ii <= nroot,
  WriteString[strm,
   "y = " <> ToString[FortranForm[yy[[ii]]]] <> "\n"]; ii++];
For[ii = 1, ii <= nroot,
  WriteString[strm,
   "z = " <> ToString[FortranForm[zz[[ii]]]] <> "\n"]; ii++];
WriteString[strm, "end subroutine\n"];
Close[strm];
Print["Finished Writing Fortran Code . . . : / "];



输出到test.f90后,第一个解为:
x = a/3. + (-6 + 2*a**2)/(3.*2**0.6666666666666666*(-36*a + 20*a**3 + Sqrt(4*(-6 + 2*a**2)**3 + (-36*a + 20*a**3 - 216*b)**2) - 216*b)**0.3333333333333333) - (-36*a + 20*a**3 + Sqrt(4*(-6 + 2*a**2)**3 + (-36*a + 20*a**3 - 216*b)**2) - 216*b)**0.3333333333333333/(6.*2**0.3333333333333333)


希望有以下输出:
tmp0 = 216*b
tmp1 = -36*a + 20*a**3
tmp2 = (tmp1 - tmp0 )**2
tmp3 = -6 + 2*a**2
tmp4 = 4*tmp3**3
tmp5 = Sqrt(tmp4 + tmp2 )
tmp6 = tmp1 + tmp5 - tmp0
tmp7 = tmp6**0.3333333333333333
tmp8 = 2**0.6666666666666666

x = a/3. + tmp3 /(3.*tmp8 *tmp7 ) - tmp7/(6.*tmp8)


请问如何实现?多谢


P.S:
maple有关键字可以实现上面类似的功能,如以下代码:

with(codegen, fortran)
A := array(1 .. 2, 1 .. 2, symmetric); A[1, 1] := log(x); A[1, 2] := 1-log(x); A[2, 2] := 2-log(x);
print(A);
fortran(A, optimized, mode = double)
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zzjt

铜虫 (小有名气)

FortranForm[expr] 作为Fortran语言版本的 expr输出。
FortranForm 是一个包装,对输出产生影响,但不计算。
别跟自己过不去=.=
2楼2013-04-17 10:26:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 tang76 的主题更新
信息提示
请填处理意见