title 'IMLMAT2: Matrix arithmetic: +, -, #';
options nodate;
proc iml;
reset print log;
*--(1) matrix addition and subtraction;
a = {1 2 3, 4 5 6};
b = {5 1 0, 3 -2 1};
c = {2 2 2, 1 1 1};
*-- matrices are added elementwise (must be same order);
d = a + b;
d = a + {1 3, 3 10};
*-- matrices are subtracted elementwise;
d = a - b;
*-- negation is also elementwise;
d = -a;
*--(2) properties of matrix addition, subtraction;
* a. commutative: A + B = B + A;
print ( A + B ) ( B + A );
equal = all( (A + B) = (B + A) );
* b. associative: A + (B+C) = (A+B) + C;
print ( (A+B) + C ) ( A + (B+C) );
* c. - A is additive inverse of A;
d = A - A;
*--(3) Product of a matrix and a scalar: elementwise;
d = 3 # A;
* this product is also commutative:
d = A # 3;
* ...and distributive over addition:
print ( 3 # (A + B) ) ( (3#A) + (3#B) );
* multiplication by 1 and 0;
d = 1 # A;
d = 0 # A;
*--(4) Elementwise product and division;
d = A # B;
*-- division is NOT actually part of matrix algebra,
but elementwise division is a natural extension;
d = A / 2;
quit;