From 9f46483462cc602d61dd4067420b9ef4f0e8ba76 Mon Sep 17 00:00:00 2001 From: digikar99 Date: Fri, 7 Apr 2023 19:06:16 +0530 Subject: [PATCH] Add TARGET argument for SCALE (https://github.com/quil-lang/magicl/issues/175) --- src/high-level/abstract-tensor.lisp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/high-level/abstract-tensor.lisp b/src/high-level/abstract-tensor.lisp index 6bb91fd..11f283e 100644 --- a/src/high-level/abstract-tensor.lisp +++ b/src/high-level/abstract-tensor.lisp @@ -132,12 +132,17 @@ If LAYOUT is specified then traverse TENSOR in the specified order (column major (lambda (tensor factor) (map! (lambda (x) (* x factor)) tensor))) -(define-backend-function scale (tensor factor) - "Scale TENSOR by FACTOR, returning a new tensor of the same type as TENSOR") +(define-backend-function scale (tensor factor &optional target) + "Scale TENSOR by FACTOR. +If TARGET is specified then the result is stored in TARGET, +otherwise a new tensor of the same type as TENSOR is used for the result.") (define-backend-implementation scale :lisp - (lambda (tensor factor) - (scale! (deep-copy-tensor tensor) factor))) + (lambda (tensor factor &optional (target nil targetp)) + (scale! (if targetp + target + (deep-copy-tensor tensor)) + factor))) (defgeneric slice (tensor from to) (:documentation "Slice a tensor from FROM to TO, returning a new tensor with the contained elements")