guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: matrix library?


From: tomas
Subject: Re: matrix library?
Date: Wed, 4 Oct 2023 20:28:58 +0200

On Wed, Oct 04, 2023 at 05:42:10PM +0200, Damien Mattei wrote:
> thank you. i will try to find a simple example in.
> 
> On Wed, Oct 4, 2023 at 1:36 PM Nala Ginrut <nalaginrut@gmail.com> wrote:
> >
> > And I'd mention AIScm which bound tensorflow APIs. It needs more love.
> >
> > https://wedesoft.github.io/aiscm/
> >
> >
> > On Wed, Oct 4, 2023, 17:12 <tomas@tuxteam.de> wrote:
> >>
> >> On Wed, Oct 04, 2023 at 08:46:02AM +0200, Damien Mattei wrote:
> >> > hello,
> >> > does anyone know if it exists a basic matrix library for Guile?
> >> > just need to multiply a matrix and a vector.
> >>
> >> Perhaps this thread from guile-user [1] has something for you
> >>
> >> Cheers
> >>
> >> [1] 
> >> https://lists.gnu.org/archive/html/guile-user/2018-12/threads.html#00117

That all said, if your case doesn't get much hairier,
it is fairly easy to navigate with Guile's arrays:

(define (dot v1 v2)
  "The dot product of v1 and v2"
  (let ((sum 0))
    (array-for-each
      (lambda (x1 x2)
        (set! sum (+ sum (* x1 x2))))
      v1 v2)
    sum))

(define (row m i)
  "The i-th row of m, as a vector"
  (make-shared-array m (lambda (j) (list i j)) (car (array-dimensions m))))

(define (mat* m v)
  "Left multiply matrix m and vector v"
  (let* ((height (cadr (array-dimensions m)))
         (res (make-typed-array 'f64 0 height)))
    (do ((i 0 (1+ i)))
        ((>= i height))
        (array-set! res (dot (row m i) v) i))
    res))


(define vec #1f64(1 2 1))
(define mat #2f64((0.5 0.5 0) (0 0.5 0.5) (-0.5 0 0.5)))

(mat* mat vec)

 => (1.5 1.5 0)

CAVEAT: only tested with this one example. Input value checking
left as an...

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]