Skip to content

Commit

Permalink
In function row and column, we expected result is a vector, but what …
Browse files Browse the repository at this point in the history
…is actually returned is a matrix
  • Loading branch information
anranyicheng authored and stylewarning committed Nov 4, 2023
1 parent 7421c1a commit 07edc0f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion magicl.asd
Expand Up @@ -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")
Expand Down
28 changes: 21 additions & 7 deletions src/high-level/matrix.lisp
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions src/high-level/vector.lisp
Expand Up @@ -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))))



Expand Down

0 comments on commit 07edc0f

Please sign in to comment.