public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: kawa mailing list <kawa@sourceware.org>
Subject: Scheme+ first example in Kawa
Date: Wed, 18 Oct 2023 08:32:27 +0200	[thread overview]
Message-ID: <CADEOade8-i5yQTed2RcM-VYLxyOJvzdruhrn8xa1o5y54ouHLQ@mail.gmail.com> (raw)

matrix+.scm example in Scheme+ contains the matrix definitions, i will
just show the process on the multiplication matrix:

(define (multiply-matrix-matrix M1 M2)

  {(n1 p1) <+ (dim-matrix M1)}
  {(n2 p2) <+ (dim-matrix M2)}

  (when {p1 ≠ n2} (error "matrix.* : matrix product impossible,
incompatible dimensions"))

  {v1 <+ (matrix-v M1)}
  {v2 <+ (matrix-v M2)}

  (define (res i j)
    {sum <+ 0}
    (for ({k <+ 0} {k < p1} {k <- k + 1})
     {sum <- sum + v1[i][k] * v2[k][j]}))

  {v <+ (create-vector-2d res n1 p2)}

  (matrix v))

the Scheme+ code is converted in prefix Scheme:

kawa curly-infix2prefix.scm ../AI_Deep_Learning/kawa/matrix+.scm >
../AI_Deep_Learning/kawa/matrix.scm

matrix.scm contains all the functions,here is the one that interest
us, the converted function of the above one:

(define (multiply-matrix-matrix M1 M2) (<+ (n1 p1) (dim-matrix M1))
 (<+ (n2 p2) (dim-matrix M2))
 (when (|≠| p1 n2)
  (error "matrix.* : matrix product impossible, incompatible dimensions"))
 (<+ v1 (matrix-v M1)) (<+ v2 (matrix-v M2))
 (define (res i j) (<+ sum 0)
  (for ((<+ k 0) (< k p1) ($nfx$ k <- k + 1))
   ($nfx$ sum <- sum + (bracket-apply (bracket-apply v1 i) k) *
    (bracket-apply (bracket-apply v2 k) j))))
 (<+ v (create-vector-2d res n1 p2)) (matrix v))

note that bracket-apply and $nfx$ are part of Scheme+

then all is working for the matrix multiplication:

#|kawa:85|# (define M1 (create-matrix-by-function (lambda (i j) (+ i j)) 2 3))
#|kawa:86|# (define M2 (create-matrix-by-function (lambda (i j) (+ i j)) 3 2))
#|kawa:87|# (multiply-matrix-matrix M1 M2)
matrix@3fc1abf
#|kawa:88|# (define M1*M2 (multiply-matrix-matrix M1 M2))
#|kawa:89|# M1*M2
matrix@3bf5911d
#|kawa:90|# (matrix-v M1*M2)
#(#(5 8) #(8 14))
#|kawa:91|# (matrix-v M1)
#(#(0 1 2) #(1 2 3))
#|kawa:92|# (matrix-v M2)
#(#(0 1) #(1 2) #(2 3))

note that:
Scheme+ can do better, instead of Java and Kawa it can overload the *
operator , implemented but not yet tested in Kawa, here is the Racket
(Scheme+) example:

(define-overload-existing-operator *)
(overload-existing-operator * multiply-matrix-vector (matrix-vect? vector?))
above is matrix * vect overload ,but i'm sure matrix * matrix can works too...

can overload [ ] for matrix to used indexed notation like in Python:
;; overload [ ]
(overload-square-brackets matrix-ref matrix-set!  (matrix? number? number?))
(overload-square-brackets matrix-line-ref matrix-line-set! (matrix? number?))

note that:

the main matrix multiplication loop use:

{sum <- sum + v1[i][k] * v2[k][j]}

which translate in scheme:

($nfx$ sum <- sum + (bracket-apply (bracket-apply v1 i) k) *
    (bracket-apply (bracket-apply v2 k) j)))

the problem here is with speed,as $nfx$ the evaluator of expression
with operator precedence always recompute the same algorithm but the
expression operator precedence never change in the loop iteration,also
bracket-apply works for vector ,arrays,hash tables, strings, but here
the type matrix  implemented with vectors never change.

A future version of Scheme+ should optimize the code passed to the
scheme compiler by pre-computing symbolically the infix with
precedence expressions only one time (not at each iteration) and
making type inference before the kawa scheme compiler or racket or
guile compile/interpret the prefixed code.

                 reply	other threads:[~2023-10-18  6:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

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

  git send-email \
    --in-reply-to=CADEOade8-i5yQTed2RcM-VYLxyOJvzdruhrn8xa1o5y54ouHLQ@mail.gmail.com \
    --to=damien.mattei@gmail.com \
    --cc=kawa@sourceware.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).