| 查看: 3768 | 回复: 27 | |||
[求助]
Mathematica里的compile怎么使用已有1人参与
|
|||
| 请教一下各位:Mathematica里的compile怎么使用?我现在的程序里需要一个大的循环数组,计算的时间太长,想提高一下计算速度。 |
» 收录本帖的淘帖专辑推荐
软件 |
» 猜你喜欢
孩子确诊有中度注意力缺陷
已经有12人回复
2025冷门绝学什么时候出结果
已经有3人回复
天津工业大学郑柳春团队欢迎化学化工、高分子化学或有机合成方向的博士生和硕士生加入
已经有4人回复
康复大学泰山学者周祺惠团队招收博士研究生
已经有6人回复
AI论文写作工具:是科研加速器还是学术作弊器?
已经有3人回复
2026博士申请-功能高分子,水凝胶方向
已经有6人回复
论文投稿,期刊推荐
已经有4人回复
硕士和导师闹得不愉快
已经有13人回复
请问2026国家基金面上项目会启动申2停1吗
已经有5人回复
同一篇文章,用不同账号投稿对编辑决定是否送审有没有影响?
已经有3人回复
» 本主题相关价值贴推荐,对您同样有帮助:
QE安装出错,求助大神
已经有6人回复
mathematica 10.0 百度云分享链接
已经有17人回复
请教mathematica中求解问题
已经有8人回复
一个很不错的mathematica教程
已经有507人回复
Mathematica问题求助
已经有7人回复
有没有origin使用高手?
已经有3人回复
请教LSDA+U计算的问题
已经有24人回复
谁来帮我一下好不好?mathematica 方程组输入一直错误,不知道怎么办
已经有3人回复
Mathematica迭代运算的终止条件怎么加进去
已经有7人回复
【原创】在WORD中使用Mathematica画的图形的体会
已经有13人回复
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
mshwangg
至尊木虫 (正式写手)
- 程序强帖: 5
- 应助: 206 (大学生)
- 金币: 10702.8
- 散金: 100
- 红花: 19
- 帖子: 597
- 在线: 195.4小时
- 虫号: 576702
- 注册: 2008-06-21
- 专业: 物理学I
【答案】应助回帖
★
感谢参与,应助指数 +1
zdzqy: 金币+1, ★★★很有帮助 2013-08-07 10:40:49
感谢参与,应助指数 +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
walk1997
金虫 (著名写手)
- 应助: 1 (幼儿园)
- 金币: 4676.2
- 红花: 22
- 帖子: 1066
- 在线: 798.1小时
- 虫号: 416039
- 注册: 2007-06-29
- 性别: GG
- 专业: 粒子物理学和场论
|
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
walk1997
金虫 (著名写手)
- 应助: 1 (幼儿园)
- 金币: 4676.2
- 红花: 22
- 帖子: 1066
- 在线: 798.1小时
- 虫号: 416039
- 注册: 2007-06-29
- 性别: GG
- 专业: 粒子物理学和场论
4楼2013-06-06 08:24:22
5楼2013-06-07 19:47:38
6楼2013-06-07 19:49:43
8楼2013-08-04 20:31:05
|
想请问一下,下面的这个小程序怎么使用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
walk1997
金虫 (著名写手)
- 应助: 1 (幼儿园)
- 金币: 4676.2
- 红花: 22
- 帖子: 1066
- 在线: 798.1小时
- 虫号: 416039
- 注册: 2007-06-29
- 性别: GG
- 专业: 粒子物理学和场论
10楼2013-08-06 23:58:27













回复此楼