From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 5872A3858291 for ; Wed, 18 Oct 2023 06:32:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5872A3858291 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5872A3858291 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697610763; cv=none; b=K7BoNf5lZTOcybeuqqUpxn11FOcaXsGCYig1QodmtDrcv6w8W/CRo9Riq3JhSqTu8oyWqe7EMGVlabcuEO9K3Xx+ozFqR29YoNdJzLtyoNgaWxk9vzaxei+HjVuCl+DBWO3DFi3qrse0HovrFmf5/+R6T5reTBnQXznOJAxPIAA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697610763; c=relaxed/simple; bh=JZLrvEWqhmxS7gIXEumBUzXL8+BqEPvC1Q3Ha9apqP8=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=hDpfAwtKDoXvSa+efpLX8DnfmOvQVurzqiBxYgVA4+ioHQy/E6+FNuzy4sMOb+QRJr5L/BqQl/vUrXgZsJB4W+cKeGuA0PykcZ6nPyK8eYt82Ys7+LzLZp9fT0394GBHvkx0moqF4/yDJVVH0nCQkte+hT1c47wZbrDS96uhxDU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-9adca291f99so1008685166b.2 for ; Tue, 17 Oct 2023 23:32:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697610760; x=1698215560; darn=sourceware.org; h=content-transfer-encoding:to:subject:message-id:date:from :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=KKcCCJtdp258VckPYrHDqBFRmbhNjxeBK8yruJbNxxc=; b=YQb4Y8Lv3FXywxd4i1Qrto1TCgUbXfWhl+glGp5c8YYvkj7wlJSRXFZdT3yAcb5u7A lcKJ47xO6KWayAj0VXK+Xna2JwfSM3QmuSXjmY5beoeag8HPXHvpNH1barDqLDL72DyT 0b18iyQ+CRQvqYesPX88bSz2rSIXYiOq1//W+kSbKiiEdu7Jv1gyZWlJdnG+DtcFPPB9 UrOdvqwaV877/opioQSF+rHVO/a2SBnJUWBzwW9nr+uH6AtG5UZqJJcwtJZhrP2SOT2r C04nBzmyNNigC9V8SamK0xw1eo9Q4inrR5XhLL04kWUv8csrrXQBDULNtbWlqVb3B+Vf qF/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697610760; x=1698215560; h=content-transfer-encoding:to:subject:message-id:date:from :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KKcCCJtdp258VckPYrHDqBFRmbhNjxeBK8yruJbNxxc=; b=CondmydvOMtsC2nm4ulNXDdchMqUWOEED+pJv7VOIZpsvgeCAhD92DVGM7JAV4bIVd 5TTaHdTb4EU50r/3RtkRHRzclFvDm/ghknqUHJCh25zm7DV/JEeuFv1r1FYqvAQjsESq 0hUqwSEUKz7KzlkiFeUVi7JT3ziFTeDP4YhxnBa0ir4ce/Atv4/xTdREu0F+aTNWaPko vanw5jUBWG1hPzV5vFzhQqkhBN8hDX6Askz3DbeubQ0+aUItd/Z1HDkwm8yWq4y237M8 7V3rC1JSaXKBZTOMhGvfl1VVQJkHoUsrisNZRwswqwpq0JoyXXt1n70kC/ItILPlRwU3 CORg== X-Gm-Message-State: AOJu0YwbXV11QqJb2ZkM4cWTqDlsBEAU9nQjc84Tq7/zqlsSa4d2v4Yi rVZUDs7JqB7b0h/kEsyr4aQXuUROD5jZq016cahOMBCAK4w= X-Google-Smtp-Source: AGHT+IFahmPtvDXwHL34Hro4TYg3yKgYPceaI0KlmZq5wKA5jXmUYB2+jL6GfnYNXI7alpyADL/awUJ6ohissMyVQWk= X-Received: by 2002:a17:907:a0c9:b0:9b2:c583:cd71 with SMTP id hw9-20020a170907a0c900b009b2c583cd71mr3271301ejc.50.1697610759416; Tue, 17 Oct 2023 23:32:39 -0700 (PDT) MIME-Version: 1.0 From: Damien Mattei Date: Wed, 18 Oct 2023 08:32:27 +0200 Message-ID: Subject: Scheme+ first example in Kawa To: kawa mailing list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 =E2=89=A0 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 (|=E2=89=A0| 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.