TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Functional Fluid Dynamics in Clojure

66 点作者 khingebjerg大约 15 年前

4 条评论

eric_t大约 15 年前
I have a PhD in computational fluid dynamics (CFD), and have written several CFD codes in Fortran, C, Matlab and Python. I'm also a programming language geek, and have been interested in functional programming languages for quite some time. When I see something like this, however, I loose some of my faith. It just seems like too much effort to get decent speed, and even when not considering speed it doesn't seem to give much benefit. For instance, this is how the diffusion function would look in Fortran (skipping some details):<p><pre><code> pure function diffusion(x0) result(x) real, intent(in), dimension(:,:) :: x0 real, intent(out),dimension(:,:) :: x x = x0(2:n+1,1:n ) &#38; + x0(0:n-1,1:n ) &#38; + x0(1:n ,2:n+1) &#38; + x0(1:n ,0:n-1) &#38; -a*x0(1:n ,1:n ) end function </code></pre> Some things to note:<p>- The "pure" keyword guarantees that this function has no side effects.<p>- No do loops are needed! Fortran array slicing is very handy.<p>- The compiler will convert this to use SIMD instructions<p>- Adding some OpenMP hints to make it run on all cores is also very easy.<p>So this type of code in Fortran is short, very easy to understand and you are guaranteed extreme performance. Maybe functional programming has some benefits when you're dealing with more complex datastructures (for instance I'm working on a code right now which uses parallel octrees, kind off a pain in Fortran), but for simple things like this, I fail to see the point.<p>I want to believe, so perhaps someone here can enlighten me?
评论 #1227701 未加载
评论 #1227670 未加载
评论 #1227718 未加载
评论 #1228437 未加载
ewjordan大约 15 年前
I dunno, seems like exactly the type of situation where it would be much more appropriate to just write the damn thing in Java...isn't the ability to do just that with performance sensitive code one of the best things Clojure offers compared to "standard" Lisps? You'd get better performance, clearer (and less) code, and less development time, as well as the ability to practically cut and paste the C code from the linked paper.<p>I feel (as I do so often when projects written in certain languages that may or may not start with "Haskell" show up here) that the main take away is "Look, we can write working apps, too, and they're Better-Because-They're-Functional!" And yes, there are a lot of things that <i>are</i> better when you code them functionally, but simple numerical algorithms like this one play specifically to the strengths of imperative languages, and I see no clear benefit.<p>It's the same code smell that I get when people start putting together utility libraries, macro tools, and bytecode processors just so they can pretend to do functional programming in Java: if you find yourself struggling to do things non-idiomatically in one language that would be trivially natural in another, why not just switch languages? <i>Especially</i> if they all live on the same JVM and play fairly well together...
obeattie大约 15 年前
"The archaic C language". Alrighty.
评论 #1228118 未加载
a-priori大约 15 年前
Do most fluid dynamics simulations use Euler integration like this?
评论 #1227643 未加载
评论 #1227960 未加载