public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* Scheme+ first example in Kawa
@ 2023-10-18  6:32 Damien Mattei
  0 siblings, 0 replies; only message in thread
From: Damien Mattei @ 2023-10-18  6:32 UTC (permalink / raw)
  To: kawa mailing list

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.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-10-18  6:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-18  6:32 Scheme+ first example in Kawa Damien Mattei

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