From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id qFLnDjIW9WUvuwAAe85BDQ:P1 (envelope-from ) for ; Sat, 16 Mar 2024 04:46:58 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id qFLnDjIW9WUvuwAAe85BDQ (envelope-from ) for ; Sat, 16 Mar 2024 04:46:58 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gF+qK2T3; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1710560818; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=fF9QsuCxMaIhwptkJIfiwKAawb2ID4juegNEE70dX9Y=; b=rLM9O0Ycj0wXEfA+V+xqaUrNGAqSPXYGCPqqpOvNFj8X/Tz/1/Qq74Q3CIji4Z8Y/7nMUB CWqBHKnx13K/TmCcG3dBdG1OAOdaylvl2ifiN0lEP3gXals4wGHHPIWhrZ+0NLO0j9c5P6 2RCba6IxGAhNUL8R0mEdtCa4/uVBgBTxqaHImyGhHP2LVWS0aK8TzFJbo10BLIEXL4ycZp nWdbSticg9PvJrlpQxz8raYJPfZHD4ZVmmC+HN7cuFg33x+JcPN3HZzyOkMlYimnCd9VZP TaglDOFniOm+CIXGzPCO2ekLV9dHpTVLdGiXWPbcN4ufl/uw57MQu0sPVassYQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gF+qK2T3; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=key1; d=yhetil.org; t=1710560818; a=rsa-sha256; cv=none; b=ElfwKlompK/CcOcCvF/0UxUKCdviIIH28QWaQpy8bCTKNURWaEL0JWhK9yHcbYApti8iFZ M6OZAbzn4f+LmhjYAtmnyjd8vw2ru+03gRj1n75ASheQJ8vvDbk7l1SDLLAK6sF/ahq/fe hDx/Gm22rlbxBWTSfx9AgzdWirMV2cG6aXcEaup/vND8i843mdb9mf8W/1jTB5kuI2CyD5 9jNbEoLFGonZ7ChysH4743dTzMED6HLfXEtqn8xsaTqJ6ABTP0q5MuVGmC2sKNE7fvd9CK eNwGTkfGs1wViG9iFBhao0dkVbJtAeC2PDo5CS1Yq0yoNy82VD8DHqHVQ3krIA== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id EB77354C65 for ; Sat, 16 Mar 2024 04:46:57 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rlKzk-0002xU-06; Fri, 15 Mar 2024 23:46:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rlKze-0002x3-UR for emacs-orgmode@gnu.org; Fri, 15 Mar 2024 23:46:01 -0400 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rlKzc-0008TB-93 for emacs-orgmode@gnu.org; Fri, 15 Mar 2024 23:45:58 -0400 Received: by mail-pf1-x442.google.com with SMTP id d2e1a72fcca58-6e6f6825102so1436816b3a.1 for ; Fri, 15 Mar 2024 20:45:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710560751; x=1711165551; darn=gnu.org; h=mime-version:user-agent:message-id:date:subject:to:from:from:to:cc :subject:date:message-id:reply-to; bh=fF9QsuCxMaIhwptkJIfiwKAawb2ID4juegNEE70dX9Y=; b=gF+qK2T38ZAgpIdfk2dsB52kJUdiZlYoYPIUxbnMXm3p4GFNeofJB89ph2hoPIZ5W7 nPdpnOlln4mRG3xwfnW5qzLTYkic7ehsmuJmhcPtSc8mggH5RdKooBAuCejf0WmaBp6W KMaepg+NE4vUhIT2IBX33lSskA6yBbdP8IM9Fv1laFSP++zgl/wixnXtX4TERh+rN9y/ 3y90uF/qz5psdnMSorMQe8Vrj6WO5PQ4XHuAxBiFfo9Xvl87rtPO99v14FjZw7EQwSm3 cH8F64G3h0NCs8b6twimfrxG6C4RE7jzMT7n+SPtuPpHAMsFIXQ//qc0cyGZ2viUnb1L B0Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710560751; x=1711165551; h=mime-version:user-agent:message-id:date:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fF9QsuCxMaIhwptkJIfiwKAawb2ID4juegNEE70dX9Y=; b=Qkk1EE99IzQ4KEkt/C2kimYnnbWiGZhDGK1rJg1d+8NhjPmIhj6zqRPrq6U8iOi9m/ jXa8j/ToSBP/ZV67mSI2hpH6k2UPgfWFUAjuy+6yTv5NnwDwIOFooilXf0kE+kDqIPwE n85TNmqoLRvwJCPxt38SYb91NS+norRWw5QNEMokAnva3eLXRQ9QyoQtSQc7f/BN6RuM TlMlqP+g0ik/SO5F3sz++cu9QJJzmCLwkjx3LVnR1Pki69lIZ5DWqyWaeql9GMzyfxZc y8sP6FqZALKNQko6AFWbfoXtbNvjUYHl7pk2qXMMItJyfKMTz390ieE2sq1l4GTrxoyA cjaQ== X-Gm-Message-State: AOJu0Yzx9dzyo5ExM/tyHdxN8//qh74STFeOj9/+Z9F67TcZuUoU1Ev0 hSFDWJ92BLmrthxdVvE5WIINRnir9uPaWrsNFbh+PmcydGtrI2vcw3mnj3bEmn0= X-Google-Smtp-Source: AGHT+IE6jSKLJXbj6hw37uNdohOMiw2dvP8QY2tOc0uW0la/Buhbjt+WOAh2dgYhns265+Ih39Njfg== X-Received: by 2002:a05:6a20:38a0:b0:1a2:ba3f:e530 with SMTP id n32-20020a056a2038a000b001a2ba3fe530mr5889259pzf.50.1710560751243; Fri, 15 Mar 2024 20:45:51 -0700 (PDT) Received: from localhost ([1.7.159.70]) by smtp.gmail.com with ESMTPSA id fm22-20020a056a002f9600b006e5a3db5875sm4252248pfb.13.2024.03.15.20.45.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 20:45:50 -0700 (PDT) From: Visuwesh To: emacs-orgmode@gnu.org Subject: [PATCH] Add support for tables in Calc src block :var Date: Sat, 16 Mar 2024 09:15:46 +0530 Message-ID: <877ci2261h.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::442; envelope-from=visuweshm@gmail.com; helo=mail-pf1-x442.google.com X-Spam_score_int: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -6.78 X-Spam-Score: -6.78 X-Migadu-Queue-Id: EB77354C65 X-Migadu-Scanner: mx13.migadu.com X-TUID: 8+tFgvomWeBZ --=-=-= Content-Type: text/plain Attached patch adds support for passing tables as matrices (or vector) to a Calc source block through the :var parameter. There might be a better way to do it than manually construct the data structure expected by Calc for a matrix but given that it rarely sees changes in this area, it shouldn't be a bother. I also added tests for this feature, and other simple tests to ensure everything works. I checked that all the tests passed on my side. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-ob-calc-Support-passing-tables-as-matrices-with-var.patch >From 5dd444a3dd688b366e1151739f6a8e8088bb0623 Mon Sep 17 00:00:00 2001 From: Visuwesh Date: Sat, 16 Mar 2024 09:10:53 +0530 Subject: [PATCH] ob-calc: Support passing tables as matrices with :var A table with MxN dimensions is converted to a MxN matrix when given in :var to a Calc source block. A table with a single row is converted to a vector (i.e., row vector). * lisp/ob-calc.el (org-babel-execute-src-block:calc): Construct the right data structure to pass tables as matrices to Calc. * testing/examples/ob-calc-test.org: * testing/lisp/test-ob-calc.el: Add tests for ob-calc, and this new feature. --- lisp/ob-calc.el | 10 +++- testing/examples/ob-calc-test.org | 57 ++++++++++++++++++++++ testing/lisp/test-ob-calc.el | 80 +++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 testing/examples/ob-calc-test.org create mode 100644 testing/lisp/test-ob-calc.el diff --git a/lisp/ob-calc.el b/lisp/ob-calc.el index f36df77ff..c8bbcd16b 100644 --- a/lisp/ob-calc.el +++ b/lisp/ob-calc.el @@ -64,7 +64,15 @@ (var-names (mapcar #'symbol-name org--var-syms))) (mapc (lambda (pair) - (calc-push-list (list (cdr pair))) + (let ((val (cdr pair))) + (calc-push-list + (list (if (listp val) + (cons 'vec + (if (null (cdr val)) + (car val) + (mapcar (lambda (x) (if (listp x) (cons 'vec x) x)) + val))) + val)))) (calc-store-into (car pair))) vars) (mapc diff --git a/testing/examples/ob-calc-test.org b/testing/examples/ob-calc-test.org new file mode 100644 index 000000000..6df44c6a4 --- /dev/null +++ b/testing/examples/ob-calc-test.org @@ -0,0 +1,57 @@ +#+TITLE: Tests for ob-calc +#+OPTIONS: ^:nil + +* Simple +:PROPERTIES: +:ID: 40e4cd26-fe15-45c0-938b-111e021a5a99 +:END: + +#+BEGIN_SRC calc :results silent + 1 * 2 +#+END_SRC + +#+BEGIN_SRC calc :results silent + 12 + 16 - 1 +#+END_SRC + +#+BEGIN_SRC calc :results silent + inv(a) +#+END_SRC + +* Tables +:PROPERTIES: +:ID: 138938e1-f0ba-4c2b-babe-5d20f2b83557 +:END: + +#+NAME: ob-calc-table-1 +| 1 | 2 | 3 | +| 5 | 6 | 7 | +| 9 | 14 | 11 | + +#+NAME: ob-calc-table-2 +| 1 | 2 | 3 | 4 | 5 | + +#+NAME: ob-calc-table-3 +| 1 | +| 2 | +| 3 | + +#+BEGIN_SRC calc :results silent :var a=ob-calc-table-1 + inv(a) +#+END_SRC + +#+BEGIN_SRC calc :results silent :var a=ob-calc-table-2 + a*2 - 2 +#+END_SRC + +#+BEGIN_SRC calc :results silent :var a=ob-calc-table-2 + vmean(a) +#+END_SRC + +#+BEGIN_SRC calc :results silent :var a=ob-calc-table-3 + a +#+END_SRC + +#+BEGIN_SRC calc :results silent :var a=ob-calc-table-2 + a +#+END_SRC diff --git a/testing/lisp/test-ob-calc.el b/testing/lisp/test-ob-calc.el new file mode 100644 index 000000000..443ccd64b --- /dev/null +++ b/testing/lisp/test-ob-calc.el @@ -0,0 +1,80 @@ +;;; test-ob-calc.el --- tests for ob-calc.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 Visuwesh + +;; Author: Visuwesh + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Code: +(require 'ob-calc) + +(unless (featurep 'ob-calc) + (signal 'missing-test-dependency "Support for Calc code blocks")) + +(ert-deftest ob-calc/simple-program-mult () + "Test of simple multiplication." + (org-test-at-id "40e4cd26-fe15-45c0-938b-111e021a5a99" + (org-babel-next-src-block) + (should (equal "2" (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/simple-program-arith () + "Test of simple arithmetic." + (org-test-at-id "40e4cd26-fe15-45c0-938b-111e021a5a99" + (org-babel-next-src-block 2) + (should (equal "27" (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/simple-program-symbolic () + "Test of simple symbolic algebra." + (org-test-at-id "40e4cd26-fe15-45c0-938b-111e021a5a99" + (org-babel-next-src-block 3) + (should (equal "1 / a" (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/matrix-inversion () + "Test of a matrix inversion." + (org-test-at-id "138938e1-f0ba-4c2b-babe-5d20f2b83557" + (org-babel-next-src-block) + (should (equal "[[-1, 0.625, -0.125], [0.25, -0.5, 0.25], [0.5, 0.125, -0.125]]" + (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/matrix-algebra () + "Test of simple matrix algebra." + (org-test-at-id "138938e1-f0ba-4c2b-babe-5d20f2b83557" + (org-babel-next-src-block 2) + (should (equal "[0, 2, 4, 6, 8]" + (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/matrix-mean () + "Test of simple mean of a vector." + (org-test-at-id "138938e1-f0ba-4c2b-babe-5d20f2b83557" + (org-babel-next-src-block 3) + (should (equal "3" + (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/matrix-correct-conv-column () + "Test of conversion of column table to Calc format." + (org-test-at-id "138938e1-f0ba-4c2b-babe-5d20f2b83557" + (org-babel-next-src-block 4) + (should (equal "[[1], [2], [3]]" + (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/matrix-correct-conv-row () + "Test of conversion of row table to Calc format." + (org-test-at-id "138938e1-f0ba-4c2b-babe-5d20f2b83557" + (org-babel-next-src-block 5) + (should (equal "[1, 2, 3, 4, 5]" + (org-babel-execute-src-block))))) + +(provide 'test-ob-calc) +;;; test-ob-calc.el ends here -- 2.43.0 --=-=-= Content-Type: text/plain P.S. Please keep me in the CCs as I am not subscribed to the list. --=-=-=--