unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Arne Babenhauserheide <arne_bab@web.de>
To: Panicz Maciej Godek <godek.maciek@gmail.com>
Cc: Arne Babenhauserheide <arne_bab@web.de>,
	guile-devel <guile-devel@gnu.org>
Subject: Re: I wrote fluid advection code: How to make this more elegant?
Date: Sun, 24 Jan 2016 16:04:40 +0100	[thread overview]
Message-ID: <877fiznhmf.fsf@web.de> (raw)
In-Reply-To: <CAMFYt2Y33Ka9kxp7jZidH55i98Yyg6xghK-B9CvYhC17VQDypg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2363 bytes --]


Panicz Maciej Godek writes:
> although I cannot be of immediate help with the topic, because I don't know
> anything about advection, I think that the main problem with your code is
> that it is imperative. While Python's stylistics handles the imperative
> code quite nicely, it looks rather strange in Scheme.
>
> From my experience, the proper Scheme solution should resemble the
> mathematical formulation of a problem (except that it should be more
> descriptive), rather than a list of steps for solving it.

That’s a good point — I should have given the mathematical formulation
along with the code. The advection equation this implements is:

(φ_j,n+1 - φ_j,n)/Δt
  + c(φ_j+1,n - φ_j-1,n)/2Δx
  - (c² Δt/Δx) * (φ_j+1,n - 2φ_j,n + φ_j-1,n) / 2Δx
  = 0

[sadly this isn’t easy to read, either, but it’s the math which needs implementation]

> Also, it should be more readable to use pattern matching instead of list
> indexing, so most likely your expression
>
> (let ((newvalue (+ (- (psir i)
>                           (* c1 (- (psir (+ i 1)) (psir (- i 1)))))
>                        (* c2 (+ (- (psir (+ i 1)) (* 2 (psir i)))
>                                 (psir (- i 1)))))))
>       (array-set! psinew newvalue i))
>
> should look like this
>
> (map (lambda (prev this next)
>           (- this
>              (* c1 (- next prev))
>              (* (- c2) (+ next (* -2 this) prev))))
>        `(0 ,@(drop psir 1))
>        psir
>        `(,@(drop-right psir 1) 0))
>
> While this may also look slightly difficult to read (and write),

I think it already helps a lot, that this is shorter (prev this next
clearly wins against (+/- i 1)).

> this isn't solely because of the expression's structure, but because
> the factors of the expression have no name, and therefore the source
> code doesn't explain their role (this is the problem of the Python
> code either, but Python doesn't prompt to fix that)

that part is already in the math, but using variables with better names
sounds like an improvement.

Thank you!

> PS I think that this subject fits better to guile-user

You’re right — sorry. I’m still spoiled by projects where exactly one
mailing list is active…

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 298 bytes --]

  reply	other threads:[~2016-01-24 15:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-23 10:00 I wrote fluid advection code: How to make this more elegant? Arne Babenhauserheide
2016-01-23 11:33 ` Panicz Maciej Godek
2016-01-24 15:04   ` Arne Babenhauserheide [this message]
2016-01-23 12:42 ` Taylan Ulrich Bayırlı/Kammer
2016-01-24 17:21   ` Nala Ginrut
2016-01-24 17:24   ` Arne Babenhauserheide
2016-01-24 17:46     ` Taylan Ulrich Bayırlı/Kammer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877fiznhmf.fsf@web.de \
    --to=arne_bab@web.de \
    --cc=godek.maciek@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).