From 07edc0f7a17ac77ccae9cbd4506dfc51b58800d5 Mon Sep 17 00:00:00 2001 From: qt Date: Sun, 5 Nov 2023 06:27:14 +0800 Subject: [PATCH] In function row and column, we expected result is a vector, but what is actually returned is a matrix --- magicl.asd | 2 +- src/high-level/matrix.lisp | 28 +++++++++++++++++++++------- src/high-level/vector.lisp | 18 +++++++++++------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/magicl.asd b/magicl.asd index 9a4a0e7..b35580f 100755 --- a/magicl.asd +++ b/magicl.asd @@ -47,8 +47,8 @@ (:file "abstract-tensor") (:file "specialize-tensor") (:file "tensor") + (:file "vector") (:file "matrix") - (:file "vector") (:file "types/single-float") (:file "types/double-float") (:file "types/complex-single-float") diff --git a/src/high-level/matrix.lisp b/src/high-level/matrix.lisp index 25840da..4b54f82 100644 --- a/src/high-level/matrix.lisp +++ b/src/high-level/matrix.lisp @@ -254,22 +254,36 @@ ELEMENT-TYPE, CAST, COPY-TENSOR, DEEP-COPY-TENSOR, TREF, SETF TREF)" (setf (matrix-nrows m) (first new-value) (matrix-ncols m) (second new-value)))) -;; Specific constructors +(defgeneric row-matrix->vector (matrix) + (:documentation + "Convert a MATRIX (a row vector) into a vector. + The output will share memory with the input.") + (:method (matrix) + (reshape matrix (list (size matrix))))) + +(defgeneric column-matrix->vector (matrix) + (:documentation + "Convert a MATRIX (a column vector) into a vector. + The output will share memory with the input.") + (:method (matrix) + (reshape matrix (list (size matrix))))) + +;; Specific constructors (defgeneric row (matrix index) (:documentation "Get row vector from a matrix") (:method ((m matrix) index) (check-type index alexandria:non-negative-fixnum) - (slice m - (list index 0) - (list (1+ index) (ncols m))))) + (let* ((row-matrix (slice m (list index 0) + (list (1+ index) (ncols m))))) + (row-matrix->vector row-matrix)))) (defgeneric column (matrix index) (:documentation "Get column vector from a matrix") (:method ((m matrix) index) - (slice m - (list 0 index) - (list (nrows m) (1+ index))))) + (let* ((column-matrix (slice m (list 0 index) + (list (nrows m) (1+ index))))) + (column-matrix->vector column-matrix)))) (define-extensible-function (mult mult-lisp) (a b &key target alpha beta transa transb) (:documentation diff --git a/src/high-level/vector.lisp b/src/high-level/vector.lisp index c9f686a..c283089 100644 --- a/src/high-level/vector.lisp +++ b/src/high-level/vector.lisp @@ -196,15 +196,19 @@ Special values of P are: :INFINITY :INF :POSITIVE-INFINITY" "Normalize the vector VECTOR in-place." (scale! vector (/ (norm vector p)))) -;;; These are implemented later in matrix-functions/row-column-matrices.lisp (defgeneric vector->row-matrix (vector) - (:documentation "Convert a VECTOR to a row vector as a matrix. The output will share memory with the input.")) + (:documentation + "Convert a VECTOR to a row vector as a matrix. + The output will share memory with the input.") + (:method (vector) + (reshape vector (list 1 (size vector))))) + (defgeneric vector->column-matrix (vector) - (:documentation "Convert a VECTOR to a column vector as a matrix. The output will share memory with the input.")) -(defgeneric row-matrix->vector (matrix) - (:documentation "Convert a MATRIX (a row vector) into a vector. The output will share memory with the input.")) -(defgeneric column-matrix->vector (matrix) - (:documentation "Convert a MATRIX (a column vector) into a vector. The output will share memory with the input.")) + (:documentation + "Convert a VECTOR to a column vector as a matrix. + The output will share memory with the input.") + (:method (vector) + (reshape vector (list (size vector) 1))))