Element-wise operations: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added task, Common Lisp implementation.)
 
(+J)
Line 50: Line 50:
(defun .^ (A c) (element-wise-scalar #'expt A c))
(defun .^ (A c) (element-wise-scalar #'expt A c))
</lang>
</lang>


=={{header|J}}==

'''Solution''': J's arithmetical primitives act elementwise by default (in J parlance, such operations are known as "scalar" or "rank zero", which means they generalize to high-order arrays transparently, operating elementwise). Thus: <lang j> scalar =: 10
vector =: 2 3 5
matrix =: 3 3 $ 7 11 13 17 19 23 29 31 37

scalar * scalar
100
scalar * vector
20 30 50
scalar * matrix
70 110 130
170 190 230
290 310 370
vector * vector
4 9 25
vector * matrix
14 22 26
51 57 69
145 155 185
matrix * matrix
49 121 169
289 361 529
841 961 1369</lang> And similarly for <tt>+</tt>, <tt>-</tt>, <tt>%</tt> (division), and <tt>^</tt> .

Revision as of 15:40, 8 June 2011

Task
Element-wise operations
You are encouraged to solve this task according to the task description, using any language you may know.

Similar to Matrix multiplication and Matrix transposition, the task is to implement basic element-wise matrix-matrix and scalar-matrix operations, which can be referred to in other, higher-order tasks. Implement addition, subtraction, multiplication, division and exponentiation.

Extend the task if necessary to include additional basic operations, which should not require their own specialised task. A reference implementation in Common Lisp is included.

Common Lisp

Element-wise matrix-matrix operations. Matrices are represented as 2D-arrays. <lang lisp>(defun element-wise-matrix (fn A B)

 (let* ((len (array-total-size A))
        (m   (car (array-dimensions A)))
        (n   (cadr (array-dimensions A)))
        (C   (make-array `(,m ,n) :initial-element 0.0d0)))
   
   (loop for i from 0 to (1- len) do
        (setf (row-major-aref C i) 
              (funcall fn
                       (row-major-aref A i)
                       (row-major-aref B i))))
   C))
A.+B, A.-B, A.*B, A./B, A.^B.

(defun m+ (A B) (element-wise-matrix #'+ A B)) (defun m- (A B) (element-wise-matrix #'- A B)) (defun m* (A B) (element-wise-matrix #'* A B)) (defun m/ (A B) (element-wise-matrix #'/ A B)) (defun m^ (A B) (element-wise-matrix #'expt A B))</lang>

Elementwise scalar-matrix operations. <lang lisp> (defun element-wise-scalar (fn A c)

 (let* ((len (array-total-size A))
        (m   (car (array-dimensions A)))
        (n   (cadr (array-dimensions A)))
        (B   (make-array `(,m ,n) :initial-element 0.0d0)))
   
   (loop for i from 0 to (1- len) do
        (setf (row-major-aref B i) 
              (funcall fn
                       (row-major-aref A i)
                       c)))
   B))
c.+A, A.-c, c.*A, A./c, A.^c.

(defun .+ (c A) (element-wise-scalar #'+ A c)) (defun .- (A c) (element-wise-scalar #'- A c)) (defun .* (c A) (element-wise-scalar #'* A c)) (defun ./ (A c) (element-wise-scalar #'/ A c)) (defun .^ (A c) (element-wise-scalar #'expt A c)) </lang>


J

Solution: J's arithmetical primitives act elementwise by default (in J parlance, such operations are known as "scalar" or "rank zero", which means they generalize to high-order arrays transparently, operating elementwise). Thus: <lang j> scalar =: 10

  vector =: 2 3 5
  matrix =: 3 3 $    7 11 13  17 19 23  29 31 37
  scalar * scalar

100

  scalar * vector

20 30 50

  scalar * matrix
70 110 130

170 190 230 290 310 370

  vector * vector

4 9 25

  vector * matrix
14  22  26
51  57  69

145 155 185

  matrix * matrix
49 121  169

289 361 529 841 961 1369</lang> And similarly for +, -, % (division), and ^ .