24小时热门版块排行榜    

CyRhmU.jpeg
查看: 3768  |  回复: 27

zdzqy

新虫 (小有名气)

[求助] Mathematica里的compile怎么使用已有1人参与

请教一下各位:Mathematica里的compile怎么使用?我现在的程序里需要一个大的循环数组,计算的时间太长,想提高一下计算速度。
回复此楼

» 收录本帖的淘帖专辑推荐

软件

» 猜你喜欢

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

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
回帖置顶 ( 共有1个 )

chyanog

金虫 (小有名气)

zdzqy: 回帖置顶 2013-08-07 10:44:14
Compile一点也不鸡肋,并不是只能编译一些简单函数,一些数值计算程序可以提速30~100倍以上,但使用一些高级的函数时(NSolve,NIntegrate等)就没必要Compile了,不会有加速效果,
使用CompilationTarget -> "C",需要本机安装有C编译器,如VisualStudio,GCC都可以,没有的话即使加了这个选项,仍然是相当于CompilationTarget -> "WVM",同时会有一个警告
可以测试下面一段程序
ggs[m_Integer] := Block[{t},
   Do[t = Sqrt@N[i*i + j*j];
    If[FractionalPart[t] == 0, Sow@{i, j, Round@t}], {i, m}, {j,   i + 1, m}]];

cggs = Compile[{{m, _Integer}}, Block[{t},
    Do[t = Sqrt@N[i*i + j*j];
     If[FractionalPart[t] == 0, Sow@{i, j, Round@t}], {i, m}, {j,  i + 1, m}]]];

Reap[ggs@1000][[2, 1]] // Length // AbsoluteTiming
Reap[cggs@1000][[2, 1]] // Length // AbsoluteTiming
7楼2013-08-03 20:11:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通回帖

mshwangg

至尊木虫 (正式写手)

【答案】应助回帖


感谢参与,应助指数 +1
zdzqy: 金币+1, ★★★很有帮助 2013-08-07 10:40:49
Mathematica帮助文件里关于Compile有这样两段话:
If you make a definition like f[x_]:=x Sin[x], Mathematica will store the expression x Sin[x] in a form that can be evaluated for any x. Then when you give a particular value for x, Mathematica substitutes this value into x Sin[x], and evaluates the result. The internal code that Mathematica uses to perform this evaluation is set up to work equally well whether the value you give for x is a number, a list, an algebraic object, or any other kind of expression.
Having to take account of all these possibilities inevitably makes the evaluation process slower. However, if Mathematica could assume that x will be a machine number, then it could avoid many steps, and potentially evaluate an expression like x Sin[x] much more quickly.

看明白了吧?Mathematica处理一个变量的时候要考虑很多种可能的情况,比如说这个变量是实数、复数、数组还是代数符号等等,所以处理起来比较慢。而用Compile不过是设定使用的变量是具体的那种数据类型,计算起来当然要快一些。

实际上,个人认为Compile的功能有点鸡肋,它不生成可执行文件,也不可以脱离Mathematica环境运行程序。如果在程序中每个变量在使用之前赋值并且所有可能的函数都做数值运算,比如NSolve,NIntegrate,那么Compile与否其实差别不大,不会为你节省多少时间。

Complie的使用方法参考:在Mathematica里键入“Compile”,全选,按键盘上的“F1”!
虽然等于没说,但是比说了还详细。
2楼2013-06-04 09:39:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

walk1997

金虫 (著名写手)

1. 安装好C
2. 直接调用complile。
对于复杂函数的计算的 还是有比较明显速度的提高的
不过编译成C和VM的 看起来差别不是很大 也许非常复杂的表达式会有差别
另外 如果是表达式非常复杂的话 最好相优化下表达式
an example to show the difference
Clear["Globale`*"];
f[x_Real] := x^2 + 1;
c1 = Compile[ {{x, _Real}}, x^2 + 1, CompilationTarget -> "C"];
c2 = Compile[ {{x, _Real}}, x^2 + 1];
Do[c1, {i, 1.0, 10.0^6, 1.0}] // Timing
Do[c2, {i, 1.0, 10.0^6, 1.0}] // Timing
Do[f, {i, 1.0, 10.0^6, 1.0}] // Timing
Do[c1, {i, 1, 10^6, 1}] // Timing
Do[c2, {i, 1, 10^6, 1}] // Timing
Do[f, {i, 1, 10^6, 1}] // Timing
3楼2013-06-06 08:23:10
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

walk1997

金虫 (著名写手)

最下面几行 碰到 时候 总是出错 看来以后改成 [j]
4楼2013-06-06 08:24:22
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zdzqy

新虫 (小有名气)

引用回帖:
2楼: Originally posted by mshwangg at 2013-06-04 09:39:06
Mathematica帮助文件里关于Compile有这样两段话:
If you make a definition like f:=x Sin, Mathematica will store the expression x Sin in a form that can be evaluated for any x. Then when you give a par ...

谢谢,这两天出差,回复晚了
5楼2013-06-07 19:47:38
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zdzqy

新虫 (小有名气)

引用回帖:
4楼: Originally posted by walk1997 at 2013-06-06 08:24:22
最下面几行 碰到 时候 总是出错 看来以后改成

嗯,我看楼上的建议确实有道理,因为我的函数并不复杂,估计Compile并不能给程序提速多少,谢谢
6楼2013-06-07 19:49:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zdzqy

新虫 (小有名气)

引用回帖:
7楼: Originally posted by chyanog at 2013-08-03 20:11:32
Compile一点也不鸡肋,并不是只能编译一些简单函数,一些数值计算程序可以提速30~100倍以上,但使用一些高级的函数时(NSolve,NIntegrate等)就没必要Compile了,不会有加速效果,
使用CompilationTarget -> & ...

好的,我试试,我的程序里涉及到的是初等函数的加减乘除运算,按照你的思路,应该是可以加速不少
8楼2013-08-04 20:31:05
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zdzqy

新虫 (小有名气)

引用回帖:
7楼: Originally posted by chyanog at 2013-08-03 20:11:32
Compile一点也不鸡肋,并不是只能编译一些简单函数,一些数值计算程序可以提速30~100倍以上,但使用一些高级的函数时(NSolve,NIntegrate等)就没必要Compile了,不会有加速效果,
使用CompilationTarget -> & ...

想请问一下,下面的这个小程序怎么使用Compile命令。

\!\(\(M1 = \@\(\((x - x1)\)\^2 + \((y - y1)\)\^2\);\)\[IndentingNewLine]
  \(M2 = \@\(\((x - x2)\)\^2 + \((y - y2)\)\^2\);\)\[IndentingNewLine]
  \(M3 = \@\(\((x -
      x3)\)\^2 + \((y - y3)\)\^2\);\)\[IndentingNewLine]\[IndentingNewLine]
  \(M11 = M1*\((\((x1 - x2)\)\ y + x2\ y1 - x1\ y2 + x\ \((\(-
    y1\) + y2)\))\);\)\[IndentingNewLine]
  \(M12 = M2*\((\((x1 - x2)\)\ y + x2\ y1 - x1\ y2 +
    x\ \((\(-y1\) + y2)\))\);\)\[IndentingNewLine]
  \(M13 = M1*\((\((\(-x1\) + x3)\)\ y -
   x3\ y1 + x\ \((y1 - y3)\) + x1\ y3)\);\)\[IndentingNewLine]
  \(M14 = M3*\((\((\(-x1\) + x3)\)\ y -
            x3\ y1 + x\ \((y1 - y3)\) + x1\ y3)\);\)\[IndentingNewLine]
  \(M15 = M2*\((\((x2 - x3)\)\ y + x3\
  y2 - x2\ y3 + x\ \((\(-y2\) + y3)\))\);\)\[IndentingNewLine]
  \(M16 = M3*\((\((x2 - x3)\)\ y +
            x3\ y2 - x2\
                y3 + x\ \((\(-y2\) +
                   y3)\))\);\)\[IndentingNewLine]\[IndentingNewLine]\
\[IndentingNewLine]
  \(QQ = \(\((x -
       x1)\)\ \((x1 - x2)\) + \((y -
           y1)\)\ \((y1 - y2)\)\)\/M11 + \(\((
                  x - x2)\)\ \((\(-x1\) + x2)\) + \((y - y2)\)\ \((\(-
            y1\) + y2)\)\)\/M12 + \(\((x -
        x1)\)\ \((x1 - x3)\) + \((y - y1)\)\ \((y1 - y3)\)\)\/M13 + \(\((x - \
x3)\)\ \((\(-x1\) + x3)\) + \((y - y3)\)\ \((\(-y1\) + y3)\)\)\/M14 + \(\((x \
- x2)\)\ \((x2 - x3)\) + \((y - y2)\)\ \((y2 - y3)\)\)\/M15 + \(\((x - x3)\)\ \
\((\(-x2\) + x3)\) + \((y - y3)\)\ \((\(-y2\) + y3)\)\)\/M16;\)\n\
\[IndentingNewLine]\n
  \(zz1 = \(-b11\)*\((c13*dd11 - s1*\((c33*da11 - e33*db11)\))\)*QQ - \
b21*\((c13*dd21 - s2*\((c33*da21 - e33*db21)\))\)*QQ - b31*\((c13*dd31 - \
s3*\((c33*da31 - e33*db31)\))\)*QQ;\)\[IndentingNewLine]\n
  \(zz2 = \(-b12\)*\((c13*dd11 - s1*\((c33*da11 - e33*db11)\))\)*QQ - \
b22*\((c13*dd21 - s2*\((c33*da21 - e33*db21)\))\)*QQ - b32*\((c13*
  dd31 - s3*\((c33*da31 - e33*db31)\))\)*QQ;\)\n\n\n
  \(dz1 = b11*\((s1*\((e33*da11 + ep33*db11)\) - e31*dd11)\)*QQ + b21*\((
          s2*\((
            e33*da21 + ep33*db21)\) - e31*dd21)\)*QQ + b31*\((s3*\((e33*da31 +
                 ep33*db31)\) - e31*dd31)\)*QQ;\)\n\[IndentingNewLine]
  \(dz2 = b12*\((
            s1*\((e33*da11 + ep33*db11)\) - e31*dd11)\)*QQ + b22*\((s2*\((e33*
                da21 + ep33*db21)\) - e31*dd21)\)*QQ + b32*\((s3*\((
            e33*da31 + ep33*db31)\) - e31*dd31)\)*QQ;\)\n\[IndentingNewLine]
  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAA\[IndentingNewLine]
  \(AA = Array[A, {NN2, NN2}];\)\[IndentingNewLine]\n
  Do[A[\((i - 1)\)*2 + 1, \((j - 1)\)*2 + 1] = zz1 /. {x -> cn[\([i, 1]\)], y \
-> cn[\([i, 2]\)], x1 -> nxx[\([j, 1]\)], x2 -> nxx[\([j, 2]\)], x3 -> \
nxx[\([j, 3]\)], y1 ->
   nyy[\([j, 1]\)], y2 -> nyy[\([j, 2]\)], y3 -> nyy[\([j,
    3]\)]}; \[IndentingNewLine]A[\((i - 1)\)*2 + 1, \((j - 1)\)*2 +
              2] = zz2 /. {x -> cn[\([i,
          1]\)], y -> cn[\([i, 2]\)], x1 -> nxx[\([j, 1]\)],
      x2 -> nxx[\([j,
         2]\)], x3 -> nxx[\([j, 3]\)], y1 -> nyy[\([
          j, 1]\)], y2 -> nyy[\([j, 2]\)], y3 -> nyy[\([j,
             3]\)]}; A[\((i - 1)\)*2 + 2, \((j - 1)\)*2 + 1] =
            dz1 /. {x -> cn[\([i, 1]\)], y ->
          cn[\([i, 2]\)], x1 -> nxx[\([j, 1]\)], x2 -> nxx[\([
            j, 2]\)], x3 -> nxx[\([j, 3]\)], y1 -> nyy[\([
            j, 1]\)], y2 -> nyy[\([j, 2]\)], y3 -> nyy[\([
            j, 3]\)]}; A[\((i - 1)\)*2 + 2, \((j - 1)\)*2 + 2] = dz2 /. {x ->
       cn[\([i, 1]\)], y -> cn[\([i, 2]\)], x1 -> nxx[\([j, 1]\)], x2 ->
            nxx[\([j, 2]\)], x3 -> nxx[\([j,
          3]\)], y1 -> nyy[\([j, 1]\)], y2 -> nyy[\([j,
          2]\)], y3 -> nyy[\([j, 3]\)]}, \[IndentingNewLine]{i, 1,
            NN1}, {j, 1, NN1}]\[IndentingNewLine]
  \)
9楼2013-08-05 10:50:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

walk1997

金虫 (著名写手)

引用回帖:
7楼: Originally posted by chyanog at 2013-08-03 20:11:32
Compile一点也不鸡肋,并不是只能编译一些简单函数,一些数值计算程序可以提速30~100倍以上,但使用一些高级的函数时(NSolve,NIntegrate等)就没必要Compile了,不会有加速效果,
使用CompilationTarget -> & ...

学习了  非常好的例子
函数内部的运算量/次数很大的时候 加速比较明显
也和运算的种类有关 编译成C 和编译成 WMV 也差个几倍
10楼2013-08-06 23:58:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zdzqy 的主题更新
信息提示
请填处理意见