Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
31
Task/Matrix-multiplication/Ada/matrix-multiplication-1.adb
Normal file
31
Task/Matrix-multiplication/Ada/matrix-multiplication-1.adb
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
|
||||
|
||||
procedure Matrix_Product is
|
||||
|
||||
procedure Put (X : Real_Matrix) is
|
||||
type Fixed is delta 0.01 range -100.0..100.0;
|
||||
begin
|
||||
for I in X'Range (1) loop
|
||||
for J in X'Range (2) loop
|
||||
Put (Fixed'Image (Fixed (X (I, J))));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Put;
|
||||
|
||||
A : constant Real_Matrix :=
|
||||
( ( 1.0, 1.0, 1.0, 1.0),
|
||||
( 2.0, 4.0, 8.0, 16.0),
|
||||
( 3.0, 9.0, 27.0, 81.0),
|
||||
( 4.0, 16.0, 64.0, 256.0)
|
||||
);
|
||||
B : constant Real_Matrix :=
|
||||
( ( 4.0, -3.0, 4.0/3.0, -1.0/4.0 ),
|
||||
(-13.0/3.0, 19.0/4.0, -7.0/3.0, 11.0/24.0),
|
||||
( 3.0/2.0, -2.0, 7.0/6.0, -1.0/4.0 ),
|
||||
( -1.0/6.0, 1.0/4.0, -1.0/6.0, 1.0/24.0)
|
||||
);
|
||||
begin
|
||||
Put (A * B);
|
||||
end Matrix_Product;
|
||||
26
Task/Matrix-multiplication/Ada/matrix-multiplication-2.adb
Normal file
26
Task/Matrix-multiplication/Ada/matrix-multiplication-2.adb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package Matrix_Ops is
|
||||
type Matrix is array (Natural range <>, Natural range <>) of Float;
|
||||
function "*" (Left, Right : Matrix) return Matrix;
|
||||
end Matrix_Ops;
|
||||
|
||||
package body Matrix_Ops is
|
||||
---------
|
||||
-- "*" --
|
||||
---------
|
||||
function "*" (Left, Right : Matrix) return Matrix is
|
||||
Temp : Matrix(Left'Range(1), Right'Range(2)) := (others =>(others => 0.0));
|
||||
begin
|
||||
if Left'Length(2) /= Right'Length(1) then
|
||||
raise Constraint_Error;
|
||||
end if;
|
||||
|
||||
for I in Left'range(1) loop
|
||||
for J in Right'range(2) loop
|
||||
for K in Left'range(2) loop
|
||||
Temp(I,J) := Temp(I,J) + Left(I, K)*Right(K, J);
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
return Temp;
|
||||
end "*";
|
||||
end Matrix_Ops;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
proc *(a:[], b:[]) {
|
||||
|
||||
if (a.eltType != b.eltType) then
|
||||
writeln("type mismatch: ", a.eltType, " ", b.eltType);
|
||||
|
||||
var ad = a.domain.dims();
|
||||
var bd = b.domain.dims();
|
||||
var (arows, acols) = ad;
|
||||
var (brows, bcols) = bd;
|
||||
if (arows != bcols) then
|
||||
writeln("dimension mismatch: ", ad, " ", bd);
|
||||
|
||||
var c:[{arows, bcols}] a.eltType = 0;
|
||||
|
||||
for i in arows do
|
||||
for j in bcols do
|
||||
for k in acols do
|
||||
c(i,j) += a(i,k) * b(k,j);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
var m1:[{1..2, 1..2}] int;
|
||||
m1(1,1) = 1; m1(1,2) = 2;
|
||||
m1(2,1) = 3; m1(2,2) = 4;
|
||||
writeln(m1);
|
||||
|
||||
var m2:[{1..2, 1..2}] int;
|
||||
m2(1,1) = 2; m2(1,2) = 3;
|
||||
m2(2,1) = 4; m2(2,2) = 5;
|
||||
writeln(m2);
|
||||
|
||||
var m3 = m1 * m2;
|
||||
writeln(m3);
|
||||
|
||||
var m4:[{1..2, 1..3}] int;
|
||||
m4(1, 1) = 1; m4(1, 2) = 2; m4(1, 3) = 3;
|
||||
m4(2, 1) = 4; m4(2, 2) = 5; m4(2, 3) = 6;
|
||||
writeln(m4);
|
||||
|
||||
var m5:[{1..3, 1..2}] int;
|
||||
m5(1, 1) = 6; m5(1, 2) = -1;
|
||||
m5(2, 1) = 3; m5(2, 2) = 2;
|
||||
m5(3, 1) = 0; m5(3, 2) = -3;
|
||||
writeln(m5);
|
||||
|
||||
writeln(m4 * m5);
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(defvar M1 '((2 1 4)
|
||||
(0 1 1)))
|
||||
|
||||
(defvar M2 '(( 6 3 -1 0)
|
||||
( 1 1 0 4)
|
||||
(-2 5 0 2)))
|
||||
|
||||
(seq-map (lambda (a1)
|
||||
(seq-map (lambda (a2) (apply #'+ (seq-mapn #'* a1 a2)))
|
||||
(apply #'seq-mapn #'list M2)))
|
||||
M1)
|
||||
16
Task/Matrix-multiplication/Euphoria/matrix-multiplication.eu
Normal file
16
Task/Matrix-multiplication/Euphoria/matrix-multiplication.eu
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
function matrix_mul(sequence a, sequence b)
|
||||
sequence c
|
||||
if length(a[1]) != length(b) then
|
||||
return 0
|
||||
else
|
||||
c = repeat(repeat(0,length(b[1])),length(a))
|
||||
for i = 1 to length(a) do
|
||||
for j = 1 to length(b[1]) do
|
||||
for k = 1 to length(a[1]) do
|
||||
c[i][j] += a[i][k]*b[k][j]
|
||||
end for
|
||||
end for
|
||||
end for
|
||||
return c
|
||||
end if
|
||||
end function
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
> (set ma '((1 2)
|
||||
(3 4)
|
||||
(5 6)
|
||||
(7 8)))
|
||||
((1 2) (3 4) (5 6) (7 8))
|
||||
> (set mb (transpose ma))
|
||||
((1 3 5 7) (2 4 6 8))
|
||||
> (matrix*-lc ma mb)
|
||||
((5 11 17 23) (11 25 39 53) (17 39 61 83) (23 53 83 113))
|
||||
40
Task/Matrix-multiplication/Pluto/matrix-multiplication.pluto
Normal file
40
Task/Matrix-multiplication/Pluto/matrix-multiplication.pluto
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
require "matrix"
|
||||
|
||||
local a = matrix.from({
|
||||
{1, 2},
|
||||
{3, 4},
|
||||
{5, 6},
|
||||
{7, 8}
|
||||
})
|
||||
|
||||
local b = matrix.from({
|
||||
{1, 2, 3},
|
||||
{4, 5, 6}
|
||||
})
|
||||
|
||||
local d = matrix.from({
|
||||
{2, 1, 4},
|
||||
{0, 1, 1}
|
||||
})
|
||||
|
||||
local e = matrix.from({
|
||||
{ 6, 3, -1, 0},
|
||||
{ 1, 1, 0, 4},
|
||||
{-2, 5, 0, 2}
|
||||
})
|
||||
|
||||
print("Matrix A:\n")
|
||||
print(a)
|
||||
print("\nMatrix B:\n")
|
||||
print(b)
|
||||
print("\nMatrix C = A x B:\n")
|
||||
local c = a:matmul(b)
|
||||
print(c)
|
||||
print()
|
||||
print("Matrix D:\n")
|
||||
print(d)
|
||||
print("\nMatrix E:\n")
|
||||
print(e)
|
||||
print("\nMatrix F = D x E:\n")
|
||||
local f = d:matmul(e)
|
||||
print(f)
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
function multarrays($a, $b) {
|
||||
$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) { $a | foreach{"$_"}}
|
||||
|
||||
$a = @(@(1,2),@(3,4))
|
||||
$b = @(@(5,6),@(7,8))
|
||||
$c = @(5,6)
|
||||
"`$a ="
|
||||
show $a
|
||||
""
|
||||
"`$b ="
|
||||
show $b
|
||||
""
|
||||
"`$c ="
|
||||
$c
|
||||
""
|
||||
"`$a * `$b ="
|
||||
show (multarrays $a $b)
|
||||
" "
|
||||
"`$a * `$c ="
|
||||
show (multarrays $a $c)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(define (matrix-multiply matrix1 matrix2)
|
||||
(map
|
||||
(lambda (row)
|
||||
(apply map
|
||||
(lambda column
|
||||
(apply + (map * row column)))
|
||||
matrix2))
|
||||
matrix1))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
(import (srfi 231))
|
||||
|
||||
(define (matrix-multiply A B)
|
||||
(array-inner-product A + * B))
|
||||
|
||||
(pretty-print
|
||||
(array->list*
|
||||
(matrix-multiply
|
||||
(list*->array 2 '((1 2)
|
||||
(3 4)))
|
||||
(list*->array 2 '((-3 8 3)
|
||||
(-2 1 4))))))
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
Dim matrix1(2,2)
|
||||
matrix1(0,0) = 3 : matrix1(0,1) = 7 : matrix1(0,2) = 4
|
||||
matrix1(1,0) = 5 : matrix1(1,1) = -2 : matrix1(1,2) = 9
|
||||
matrix1(2,0) = 8 : matrix1(2,1) = -6 : matrix1(2,2) = -5
|
||||
Dim matrix2(2,2)
|
||||
matrix2(0,0) = 9 : matrix2(0,1) = 2 : matrix2(0,2) = 1
|
||||
matrix2(1,0) = -7 : matrix2(1,1) = 3 : matrix2(1,2) = -10
|
||||
matrix2(2,0) = 4 : matrix2(2,1) = 5 : matrix2(2,2) = -6
|
||||
|
||||
function getMatrixProduct(firstMatrix,secondMatrix)
|
||||
if ubound(firstMatrix,2) <> ubound(secondMatrix,1) then exit function
|
||||
redim resultMatrix(ubound(firstMatrix,1),ubound(secondMatrix,2))
|
||||
for i = 0 to ubound(firstMatrix,1) : for j = 0 to ubound(secondMatrix,2) : for k = 0 to ubound(firstMatrix,2)
|
||||
resultMatrix(i,j) = resultMatrix(i,j) + (firstMatrix(i,k) * secondMatrix(k,j))
|
||||
next : next : next
|
||||
getMatrixProduct = resultMatrix
|
||||
End function
|
||||
|
||||
dim resultMatrix : resultMatrix = getMatrixProduct(matrix1,matrix2)
|
||||
dim outputString
|
||||
for a = 0 to ubound(resultMatrix,1)
|
||||
for b = 0 to ubound(resultMatrix,2)
|
||||
outputString = outputString & resultMatrix(a,b) & vbTab
|
||||
next
|
||||
outputString = outputString & vbCrlf
|
||||
next
|
||||
msgbox outputString
|
||||
Loading…
Add table
Add a link
Reference in a new issue