From: Arne Babenhauserheide <arne_bab@web.de>
To: "Taylan Ulrich Bayırlı/Kammer" <taylanbayirli@gmail.com>
Cc: Arne Babenhauserheide <arne_bab@web.de>,
David A Wheeler <dwheeler@dwheeler.com>,
guile-devel@gnu.org
Subject: Re: I wrote fluid advection code: How to make this more elegant?
Date: Sun, 24 Jan 2016 18:24:22 +0100 [thread overview]
Message-ID: <8760yiopq1.fsf@web.de> (raw)
In-Reply-To: <877fj0lb64.fsf@T420.taylan>
[-- Attachment #1: Type: text/plain, Size: 2347 bytes --]
Taylan Ulrich Bayırlı/Kammer writes:
> Arne Babenhauserheide <arne_bab@web.de> writes:
>> The original version was in Python:
>>
>> psi[i] - c1*(psi[i+1] - psi[i-1]) + c2*(psi[i+1] - 2.0*psi[i] + psi[i-1])
>>
>> My port to Scheme looks like this:
>>
>> (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))
>
> Guile supports SRFI-105, so that could be:
>
> {{psi[i] - {c1 * {psi[{i + 1}] - psi[{i - 1}]}}} + {c2 * {{psi[{i + 1}] - {2 * psi[i]}} + psi[{i - 1}]}}}
That’s already pretty close — I wonder why I didn’t think of the psi[i]
form.
I think a + around the equation would actually help here:
(+ psi[i]
(* -1 c1 {psi[{i + 1}] - psi[{i - 1}]})
(* c2 {{psi[{i + 1}] - {2 * psi[i]}} + psi[{i - 1}]}))
Though neoteric expressions combined with curly infix make this even
easier: p{i + 1} → (p (+ i 1))
(though this did not work for me in the REPL right now — did I miss something?)
So the function psir could be used to have elegant access to elements:
(+ psi[i]
(* -1 c1 {psi[{i + 1}] - psi[{i - 1}]})
(* c2 {{psi[{i + 1}] - {2 * psi[i]}} + psi[{i - 1}]}))
> {psi[i] - c1 * {psi[i + 1] - psi[i - 1]} + c2 * {psi[i + 1] - 2 * psi[i] + psi[i - 1]}}
That looks roughly as readable as the Python version. With the + around
I think it becomes better:
(+ psi[i]
(* -1 c1 {psi[i + 1] - psi[i - 1]})
(* c2 {{psi[i + 1] - {2 * psi[i]}} + psi[i - 1]}))
And I don’t think it’s worse than the Python version, because in Python
I also need to first import numpy which changes some semantics (the []
operator changes and arrays have some special properties which can
sometimes bite).
Besides: when working on the Python, I was tempted to bracket the
expression to make it easier to split the lines and make the boundary
between sub-expressions clearer:
(psi[i]
- c1*(psi[i+1] - psi[i-1])
+ c2*(psi[i+1] - 2.0*psi[i] + psi[i-1]))
Best wishes,
Arne
PS: CCed to David A. Wheeler who wrote SRFI-105.
--
Unpolitisch sein
heißt politisch sein
ohne es zu merken
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 298 bytes --]
next prev parent reply other threads:[~2016-01-24 17:24 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
2016-01-23 12:42 ` Taylan Ulrich Bayırlı/Kammer
2016-01-24 17:21 ` Nala Ginrut
2016-01-24 17:24 ` Arne Babenhauserheide [this message]
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=8760yiopq1.fsf@web.de \
--to=arne_bab@web.de \
--cc=dwheeler@dwheeler.com \
--cc=guile-devel@gnu.org \
--cc=taylanbayirli@gmail.com \
/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).