From 049c352ea84e9ea6e1df2236d15a1b63e12d9e84 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Thu, 22 May 2025 11:49:24 +0200 Subject: [PATCH] provide tensor function, just creating arrays --- decons.lisp | 20 ++++++++++++++++++-- test-decons.lisp | 12 ++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/decons.lisp b/decons.lisp index 58c2409..f6eb901 100644 --- a/decons.lisp +++ b/decons.lisp @@ -3,9 +3,9 @@ (defpackage :decons (:use :common-lisp) (:local-nicknames (:util :scopes/util)) - (:export #:*pi* - #:area #:circle + (:export #:*pi* #:area #:circle #:absv #:double #:remainder + #:scalar-p #:tensor #:at #:line )) @@ -34,6 +34,22 @@ v (remainder (- v d) d))) +;;;; library stuff + +(defgeneric scalar-p (x) + (:method (x) t) + (:method ((x list)) nil) + (:method ((x array)) nil)) + +(defun tensor (s v) + (make-array s :initial-contents v)) + +(defun at (a &rest subs) + (apply #'aref a subs)) + +(defun (setf at) (v a &rest subs) + (setf (apply #'aref a subs) v)) + ;;;; parameterized functions (defun line (x) diff --git a/test-decons.lisp b/test-decons.lisp index 89cfaee..e2099f8 100644 --- a/test-decons.lisp +++ b/test-decons.lisp @@ -11,6 +11,7 @@ (defun run () (let ((t:*test-suite* (t:test-suite "decons"))) (test-basic) + (test-lib) (test-line) (t:show-result))) @@ -24,6 +25,17 @@ (== (decons:remainder 7 4) 3) ) +(deftest test-lib () + (== (decons:scalar-p 7) t) + (== (decons:scalar-p '(a b)) nil) + (let ((t1 (decons:tensor '(4) '(0 1 2 3)))) + (== (decons:at t1 0) 0) + (setf (decons:at t1 0) 5) + (== (decons:at t1 0) 5) + (== (decons:scalar-p t1) nil) + ) +) + (deftest test-line () (let ((p1 (decons:line 0.0)) (p2 (decons:line 1.0))