September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,54 @@
# Usage: GAWK -f MATRIX_MULTIPLICATION.AWK filename
# Separate matrices a and b by a blank line
BEGIN {
ranka1 = 0; ranka2 = 0
rankb1 = 0; rankb2 = 0
matrix = 1 # Indicate first (1) or second (2) matrix
i = 0
}
NF == 0 {
if (++matrix > 2) {
printf("Warning: Ignoring data below line %d.\n", NR)
}
i = 0
next
}
{
# Store first matrix in a, second matrix in b
if (matrix == 1) {
ranka1 = ++i
ranka2 = max(ranka2, NF)
for (j = 1; j <= NF; j++)
a[i,j] = $j
}
if (matrix == 2) {
rankb1 = ++i
rankb2 = max(rankb2, NF)
for (j = 1; j <= NF; j++)
b[i,j] = $j
}
}
END {
# Check ranks of a and b
if ((ranka1 < 1) || (ranka2 < 1) || (rankb1 < 1) || (rankb2 < 1) ||
(ranka2 != rankb1)) {
printf("Error: Incompatible ranks (%dx%d)*(%dx%d).\n", ranka1, ranka2, rankb1, rankb2)
exit
}
# Multiplication c = a * b
for (i = 1; i <= ranka1; i++) {
for (j = 1; j <= rankb2; j++) {
c[i,j] = 0
for (k = 1; k <= ranka2; k++)
c[i,j] += a[i,k] * b[k,j]
}
}
# Print matrix c
for (i = 1; i <= ranka1; i++) {
for (j = 1; j <= rankb2; j++)
printf("%g%s", c[i,j], j < rankb2 ? " " : "\n")
}
}
function max(m, n) {
return m > n ? m : n
}

View file

@ -0,0 +1,14 @@
DIM matrix1(4,2),matrix2(2,3)
MAT READ matrix1
DATA 1,2
DATA 3,4
DATA 5,6
DATA 7,8
MAT READ matrix2
DATA 1,2,3
DATA 4,5,6
MAT product=matrix1*matrix2
MAT PRINT product

View file

@ -0,0 +1,65 @@
alias Matrix => Integer[][];
void printMatrix(Matrix m) {
value strings = m.collect((row) => row.collect(Integer.string));
value maxLength = max(expand(strings).map(String.size)) else 0;
value padded = strings.collect((row) => row.collect((s) => s.padLeading(maxLength)));
for (row in padded) {
print("[``", ".join(row)``]");
}
}
Matrix? multiplyMatrices(Matrix a, Matrix b) {
function rectangular(Matrix m) =>
if (exists firstRow = m.first)
then m.every((row) => row.size == firstRow.size)
else false;
function rowCount(Matrix m) => m.size;
function columnCount(Matrix m) => m[0]?.size else 0;
if (!rectangular(a) || !rectangular(b) || columnCount(a) != rowCount(b)) {
return null;
}
function getNumber(Matrix m, Integer x, Integer y) {
assert (exists number = m[y]?.get(x));
return number;
}
function getRow(Matrix m, Integer rowIndex) {
assert (exists row = m[rowIndex]);
return row;
}
function getColumn(Matrix m, Integer columnIndex) => {
for (y in 0:rowCount(m))
getNumber(m, columnIndex, y)
};
return [
for (y in 0:rowCount(a)) [
for (x in 0:columnCount(b))
sum { 0, for ([a1, b1] in zipPairs(getRow(a, y), getColumn(b, x))) a1 * b1 }
]
];
}
shared void run() {
value m = [[1, 2, 3], [4, 5, 6]];
printMatrix(m);
print("---------");
print("multiplied by");
value m2 = [[7, 8], [9, 10], [11, 12]];
printMatrix(m2);
print("---------");
print("equals:");
value result = multiplyMatrices(m, m2);
if (exists result) {
printMatrix(result);
}
else {
print("something went wrong!");
}
}

View file

@ -16,3 +16,20 @@ sub mmult
}
return [@p];
}
sub display { join("\n" => map join(" " => map(sprintf("%4d", $_), @$_)), @{+shift})."\n" }
@a =
(
[1, 2],
[3, 4]
);
@b =
(
[-3, -8, 3],
[-2, 1, 4]
);
$c = mmult(\@a,\@b);
display($c)

View file

@ -1,24 +1,19 @@
function multarrays($a, $b) {
$c = @()
if($a -and $b) {
$n = $a.count - 1
$m = $b[0].count - 1
$c = @(0)*($n+1)
foreach ($i in 0..$n) {
$c[$i] = foreach ($j in 0..$m) {
$sum = 0
foreach ($k in 0..$n){$sum += $a[$i][$k]*$b[$k][$j]}
$sum
}
$n,$m,$p = ($a.Count - 1), ($b.Count - 1), ($b[0].Count - 1)
if ($a[0].Count -ne $b.Count) {throw "Multiplication impossible"}
$c = @(0)*($a[0].Count)
foreach ($i in 0..$n) {
$c[$i] = foreach ($j in 0..$p) {
$sum = 0
foreach ($k in 0..$m){$sum += $a[$i][$k]*$b[$k][$j]}
$sum
}
}
$c
}
function show($a) {
if($a) {
0..($a.count - 1) | foreach{ if($a[$_]){"$($a[$_])"}else{""} }
}
}
}
function show($a) { $a | foreach{"$_"}}
$a = @(@(1,2),@(3,4))
$b = @(@(5,6),@(7,8))
$c = @(5,6)

View file

@ -0,0 +1,19 @@
structure Matrix = struct
local
open Array2
fun dot(x,y) = Vector.foldli (fn (i,xi,agg) => agg+xi*Vector.sub(y,i)) 0 x
in
val fromList = fromList
fun x*y = tabulate ColMajor (nRows x, nCols y, fn (i,j) => dot(row(x,i),column(y,j)))
(* for display *)
fun toList a =
List.tabulate(nRows a, fn i => List.tabulate(nCols a, fn j => sub(a,i,j)))
end
end;
(* example *)
let open Matrix
val m1 = fromList [[1,2],[3,4]]
val m2 = fromList [[~3,~8,3],[~2,1,4]]
in
toList (m1*m2)
end;

View file

@ -0,0 +1 @@
val it = [[~7,~6,11],[~17,~20,25]] : int list list

View file

@ -0,0 +1,3 @@
Function matrix_multiplication(a As Variant, b As Variant) As Variant
matrix_multiplication = WorksheetFunction.MMult(a, b)
End Function