Jump to content

Matrix multiplication: Difference between revisions

Add S-Lang
(Add S-Lang)
Line 4,088:
 
</lang>
 
=={{header|S-lang}}==
<lang C>% Matrix multiplication is a built-in with the S-Lang octothorpe operator.
variable A = [1,2,3,4,5,6];
reshape(A, [2,3]); % reshape 1d array to 2 rows, 3 columns
printf("A is %S\n", A); print(A);
 
variable B = [1:6]; % index range, 1 to 6 same as above in A
reshape(B, [3,2]); % reshape to 3 rows, 2 columns
printf("\nB is %S\n", B); print(B);
 
printf("\nA # B is %S\n", A#B);
print(A#B);
 
% Multiply binary operator is different, dimensions need to be equal
reshape(B, [2,3]);
printf("\nA * B is %S (with reshaped B to match A)\n", A*B);
print(A*B);</lang>
 
{{out}}
<pre>prompt$ slsh matrix_mul.sl
A is Integer_Type[2,3]
1 2 3
4 5 6
 
B is Integer_Type[3,2]
1 2
3 4
5 6
 
A # B is Float_Type[2,2]
22.0 28.0
49.0 64.0
 
A * B is Integer_Type[2,3] (with B reshaped to match A)
1 4 9
16 25 36</pre>
 
=={{header|Scala}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.