unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: taylanbayirli@gmail.com (Taylan Ulrich Bayırlı/Kammer)
To: Arne Babenhauserheide <arne_bab@web.de>
Cc: guile-devel@gnu.org
Subject: Re: I wrote fluid advection code: How to make this more elegant?
Date: Sun, 24 Jan 2016 18:46:43 +0100	[thread overview]
Message-ID: <878u3ekgzg.fsf@T420.taylan> (raw)
In-Reply-To: <8760yiopq1.fsf@web.de> (Arne Babenhauserheide's message of "Sun,  24 Jan 2016 18:24:22 +0100")

Arne Babenhauserheide <arne_bab@web.de> writes:

> 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))

Good call, I had forgotten that's included in SRFI 105.  (Thought it was
just for x[y].)

> (though this did not work for me in the REPL right now — did I miss something?)

Any SRFI 105 syntax must appear within {}, so that would have to be
e.g. {p{i + 1}}, although if it appears within a larger {} block then it
won't have that annoyance.

> 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]}))

I agree mixing in some prefix notation actually makes things more
readable here.

Taylan



      reply	other threads:[~2016-01-24 17:46 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
2016-01-24 17:46     ` Taylan Ulrich Bayırlı/Kammer [this message]

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=878u3ekgzg.fsf@T420.taylan \
    --to=taylanbayirli@gmail.com \
    --cc=arne_bab@web.de \
    --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).