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: Sat, 23 Jan 2016 13:42:27 +0100	[thread overview]
Message-ID: <877fj0lb64.fsf@T420.taylan> (raw)
In-Reply-To: <87io2kpqdq.fsf@web.de> (Arne Babenhauserheide's message of "Sat,  23 Jan 2016 11:00:17 +0100")

Arne Babenhauserheide <arne_bab@web.de> writes:

> Hi,
>
> I just recreated a fluid advection exercise in Guile Scheme and I’m not
> quite happy with its readability. Can you help me improve it?
>
> My main gripe is that the math does not look instantly accessible.
>
> 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}]}}}

which is less readable than the Python version because there's no
first-hand support for operator precedence so we {} group all binary
operations, plus we need to use {} within [], plus we must separate
operators with whitespace, but maybe it's acceptable.

You can put "#!curly-infix" at the start of your Scheme file to enable
SRFI-105 syntax.

Note that all those 'psi[x]' expressions will expand to

    ($bracket-apply$ psi x)

and you can implement a macro of that name to turn that into whatever is
suitable at compile-time.  (If performance is not an issue, SRFI-123
provides a run-time polymorphic procedure for $bracket-access$.)

With a smart definition of $bracket-apply$, you could also cut down
those psi[{i + 1}] to psi[i + 1].  The latter will expand to

    ($bracket-apply$ psi i + 1)

which could be accommodated for in a special-purpose definition of
$bracket-apply$ such as:

    (syntax-rules ()
      ((_ obj index)
       (obj index))
      ((_ obj x operator y)
       (obj (operator x y))))

That would *not* support e.g. psi[i + j - 1], which would instead need
to be written e.g. psi[{i + j} - 1], i.e. we only save one pair of {},
but maybe that's good enough.

By the way, e.g. {x - y + z} will turn into ($nfx$ x - y + z), and maybe
there's a definition for $nfx$ out in the wild (or you can create one)
that does operator precedence.  (That could then also be used in
$bracket-apply$.)  So optimally, the whole thing could become:

    {psi[i] - c1 * {psi[i + 1] - psi[i - 1]} + c2 * {psi[i + 1] - 2 * psi[i] + psi[i - 1]}}

Taylan



  parent reply	other threads:[~2016-01-23 12:42 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 [this message]
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=877fj0lb64.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).