Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,106 @@
* Matrix multiplication 06/08/2015
MATRIXRC CSECT Matrix multiplication
USING MATRIXRC,R13
SAVEARA B STM-SAVEARA(R15)
DC 17F'0'
STM STM R14,R12,12(R13)
ST R13,4(R15)
ST R15,8(R13)
LR R13,R15
LA R7,1 i=1
LOOPI1 CH R7,M do i=1 to m (R7)
BH ELOOPI1
LA R8,1 j=1
LOOPJ1 CH R8,P do j=1 to p (R8)
BH ELOOPJ1
LR R1,R7 i
BCTR R1,0
MH R1,P
LR R6,R8 j
BCTR R6,0
AR R1,R6
SLA R1,2
LA R6,0
ST R6,C(R1) c(i,j)=0
LA R9,1 k=1
LOOPK1 CH R9,N do k=1 to n (R9)
BH ELOOPK1
LR R1,R7 i
BCTR R1,0
MH R1,P
LR R6,R8 j
BCTR R6,0
AR R1,R6
SLA R1,2
L R2,C(R1) R2=c(i,j)
LR R10,R1 R10=offset(i,j)
LR R1,R7 i
BCTR R1,0
MH R1,N
LR R6,R9 k
BCTR R6,0
AR R1,R6
SLA R1,2
L R3,A(R1) R3=a(i,k)
LR R1,R9 k
BCTR R1,0
MH R1,P
LR R6,R8 j
BCTR R6,0
AR R1,R6
SLA R1,2
L R4,B(R1) R4=b(k,j)
LR R15,R3 a(i,k)
MR R14,R4 a(i,k)*b(k,j)
LR R3,R15
AR R2,R3 R2=R2+a(i,k)*b(k,j)
ST R2,C(R10) c(i,j)=c(i,j)+a(i,k)*b(k,j)
LA R9,1(R9) k=k+1
B LOOPK1
ELOOPK1 LA R8,1(R8) j=j+1
B LOOPJ1
ELOOPJ1 LA R7,1(R7) i=i+1
B LOOPI1
ELOOPI1 MVC Z,=CL80' ' clear buffer
LA R7,1
LOOPI2 CH R7,M do i=1 to m
BH ELOOPI2
LA R8,1
LOOPJ2 CH R8,P do j=1 to p
BH ELOOPJ2
LR R1,R7 i
BCTR R1,0
MH R1,P
LR R6,R8 j
BCTR R6,0
AR R1,R6
SLA R1,2
L R6,C(R1) c(i,j)
LA R3,Z
AH R3,IZ
XDECO R6,W
MVC 0(5,R3),W+7 output c(i,j)
LH R3,IZ
LA R3,5(R3)
STH R3,IZ
LA R8,1(R8) j=j+1
B LOOPJ2
ELOOPJ2 XPRNT Z,80 print buffer
MVC IZ,=H'0'
LA R7,1(R7) i=i+1
B LOOPI2
ELOOPI2 L R13,4(0,R13)
LM R14,R12,12(R13)
XR R15,R15
BR R14
A DC F'1',F'2',F'3',F'4',F'5',F'6',F'7',F'8' a(4,2)
B DC F'1',F'2',F'3',F'4',F'5',F'6' b(2,3)
C DS 12F c(4,3)
N DC H'2' dim(a,2)=dim(b,1)
M DC H'4' dim(a,1)
P DC H'3' dim(b,2)
Z DS CL80
IZ DC H'0'
W DS CL16
YREGS
END MATRIXRC

View file

@ -0,0 +1,24 @@
Assume the matrices to be multiplied are a and b
IF (LEN(a,2) = LEN(b)) 'if valid dims
n = LEN(a,2)
m = LEN(a)
p = LEN(b,2)
DIM ans(0 TO m - 1, 0 TO p - 1)
FOR i = 0 TO m - 1
FOR j = 0 TO p - 1
FOR k = 0 TO n - 1
ans(i, j) = ans(i, j) + (a(i, k) * b(k, j))
NEXT k, j, i
'print answer
FOR i = 0 TO m - 1
FOR j = 0 TO p - 1
PRINT ans(i, j);
NEXT j
PRINT
NEXT i
ELSE
PRINT "invalid dimensions"
END IF

View file

@ -0,0 +1,57 @@
%% Multiplies two matrices. Usage example:
%% $ matrix:multiply([[1,2,3],[4,5,6]], [[4,4],[0,0],[1,4]])
%% If the dimentions are incompatible, an error is thrown.
%%
%% The erl shell may encode the lists output as strings. In order to prevent such
%% behaviour, BEFORE running matrix:multiply, run shell:strings(false) to disable
%% auto-encoding. When finished, run shell:strings(true) to reset the defaults.
-module(matrix).
-export([multiply/2]).
transpose([[]|_]) ->
[];
transpose(B) ->
[lists:map(fun hd/1, B) | transpose(lists:map(fun tl/1, B))].
red(Pair, Sum) ->
X = element(1, Pair), %gets X
Y = element(2, Pair), %gets Y
X * Y + Sum.
%% Mathematical dot product. A x B = d
%% A, B = 1-dimension vector
%% d = scalar
dot_product(A, B) ->
lists:foldl(fun red/2, 0, lists:zip(A, B)).
%% Exposed function. Expected result is C = A x B.
multiply(A, B) ->
%% First transposes B, to facilitate the calculations (It's easier to fetch
%% row than column wise).
multiply_internal(A, transpose(B)).
%% This function does the actual multiplication, but expects the second matrix
%% to be transposed.
multiply_internal([Head | Rest], B) ->
% multiply each row by Y
Element = multiply_row_by_col(Head, B),
% concatenate the result of this multiplication with the next ones
[Element | multiply_internal(Rest, B)];
multiply_internal([], B) ->
% concatenating and empty list to the end of a list, changes nothing.
[].
multiply_row_by_col(Row, [Col_Head | Col_Rest]) ->
Scalar = dot_product(Row, Col_Head),
[Scalar | multiply_row_by_col(Row, Col_Rest)];
multiply_row_by_col(Row, []) ->
[].

View file

@ -1,54 +1,23 @@
package main
import "fmt"
import (
"fmt"
type Value float64
type Matrix [][]Value
func Multiply(m1, m2 Matrix) (m3 Matrix, ok bool) {
rows, cols, extra := len(m1), len(m2[0]), len(m2)
if len(m1[0]) != extra { return nil, false }
m3 = make(Matrix, rows)
for i := 0; i < rows; i++ {
m3[i] = make([]Value,cols)
for j := 0; j < cols; j++ {
for k := 0; k < extra; k++ {
m3[i][j] += m1[i][k] * m2[k][j]
}
}
}
return m3, true
}
func (m Matrix) String() string {
rows := len(m)
cols := len(m[0])
out := "["
for r := 0; r < rows; r++ {
if r > 0 { out += ",\n " }
out += "[ "
for c := 0; c < cols; c++ {
if c > 0 { out += ", " }
out += fmt.Sprintf("%7.3f", m[r][c])
}
out += " ]"
}
out += "]"
return out
}
"github.com/gonum/matrix/mat64"
)
func main() {
A := Matrix{[]Value{1, 1, 1, 1},
[]Value{2, 4, 8, 16},
[]Value{3, 9, 27, 81},
[]Value{4, 16, 64, 256}}
B := Matrix{[]Value{ 4.0 , -3.0 , 4.0/3, -1.0/4 },
[]Value{-13.0/3, 19.0/4, -7.0/3, 11.0/24},
[]Value{ 3.0/2, -2.0 , 7.0/6, -1.0/4 },
[]Value{ -1.0/6, 1.0/4, -1.0/6, 1.0/24}}
P,ok := Multiply(A,B)
if !ok { panic("Invalid dimensions") }
fmt.Printf("Matrix A:\n%s\n\n", A)
fmt.Printf("Matrix B:\n%s\n\n", B)
fmt.Printf("Product of A and B:\n%s\n\n", P)
a := mat64.NewDense(2, 4, []float64{
1, 2, 3, 4,
5, 6, 7, 8,
})
b := mat64.NewDense(4, 3, []float64{
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,
})
var m mat64.Dense
m.Mul(a, b)
fmt.Println(mat64.Formatted(&m))
}

View file

@ -1,89 +1,28 @@
package main
import "fmt"
import (
"fmt"
type matrix struct {
ele []float64
stride int
}
func matrixFromRows(rows [][]float64) *matrix {
if len(rows) == 0 {
return &matrix{nil, 0}
}
m := &matrix{make([]float64, len(rows)*len(rows[0])), len(rows[0])}
for rx, row := range rows {
copy(m.ele[rx*m.stride:(rx+1)*m.stride], row)
}
return m
}
func (m *matrix) print(heading string) {
if heading > "" {
fmt.Print("\n", heading, "\n")
}
for e := 0; e < len(m.ele); e += m.stride {
fmt.Printf("%6.3f ", m.ele[e:e+m.stride])
fmt.Println()
}
}
func (m1 *matrix) multiply(m2 *matrix) (m3 *matrix, ok bool) {
if m1.stride*m2.stride != len(m2.ele) {
return nil, false
}
m3 = &matrix{make([]float64, (len(m1.ele)/m1.stride)*m2.stride), m2.stride}
for m1c0, m3x := 0, 0; m1c0 < len(m1.ele); m1c0 += m1.stride {
for m2r0 := 0; m2r0 < m2.stride; m2r0++ {
for m1x, m2x := m1c0, m2r0; m2x < len(m2.ele); m2x += m2.stride {
m3.ele[m3x] += m1.ele[m1x] * m2.ele[m2x]
m1x++
}
m3x++
}
}
return m3, true
}
mat "github.com/skelterjohn/go.matrix"
)
func main() {
a := matrixFromRows([][]float64{
{1, 1, 1, 1},
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256},
a := mat.MakeDenseMatrixStacked([][]float64{
{1, 2, 3, 4},
{5, 6, 7, 8},
})
b := matrixFromRows([][]float64{
{
4,
-3,
4. / 3,
-1. / 4,
},
{
-13. / 3,
19. / 4,
-7. / 3,
11. / 24,
},
{
3. / 2,
-2,
7. / 6,
-1. / 4,
},
{
-1. / 6,
1. / 4,
-1. / 6,
1. / 24,
},
b := mat.MakeDenseMatrixStacked([][]float64{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12},
})
p, ok := a.multiply(b)
a.print("Matrix A:")
b.print("Matrix B:")
if !ok {
fmt.Println("not conformable for matrix multiplication")
fmt.Printf("Matrix A:\n%v\n", a)
fmt.Printf("Matrix B:\n%v\n", b)
p, err := a.TimesDense(b)
if err != nil {
fmt.Println(err)
return
}
p.print("Product of A and B:")
fmt.Printf("Product of A and B:\n%v\n", p)
}

View file

@ -1,50 +1,60 @@
package main
import (
"fmt"
import "fmt"
mat "github.com/skelterjohn/go.matrix"
)
type Value float64
type Matrix [][]Value
func Multiply(m1, m2 Matrix) (m3 Matrix, ok bool) {
rows, cols, extra := len(m1), len(m2[0]), len(m2)
if len(m1[0]) != extra {
return nil, false
}
m3 = make(Matrix, rows)
for i := 0; i < rows; i++ {
m3[i] = make([]Value, cols)
for j := 0; j < cols; j++ {
for k := 0; k < extra; k++ {
m3[i][j] += m1[i][k] * m2[k][j]
}
}
}
return m3, true
}
func (m Matrix) String() string {
rows := len(m)
cols := len(m[0])
out := "["
for r := 0; r < rows; r++ {
if r > 0 {
out += ",\n "
}
out += "[ "
for c := 0; c < cols; c++ {
if c > 0 {
out += ", "
}
out += fmt.Sprintf("%7.3f", m[r][c])
}
out += " ]"
}
out += "]"
return out
}
func main() {
a := mat.MakeDenseMatrixStacked([][]float64{
{1, 1, 1, 1},
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256},
})
b := mat.MakeDenseMatrixStacked([][]float64{
{
4,
-3,
4. / 3,
-1. / 4,
},
{
-13. / 3,
19. / 4,
-7. / 3,
11. / 24,
},
{
3. / 2,
-2,
7. / 6,
-1. / 4,
},
{
-1. / 6,
1. / 4,
-1. / 6,
1. / 24,
},
})
p, err := a.TimesDense(b)
fmt.Printf("Matrix A:\n%v\n", a)
fmt.Printf("Matrix B:\n%v\n", b)
if err != nil {
fmt.Println(err)
return
A := Matrix{[]Value{1, 2, 3, 4},
[]Value{5, 6, 7, 8}}
B := Matrix{[]Value{1, 2, 3},
[]Value{4, 5, 6},
[]Value{7, 8, 9},
[]Value{10, 11, 12}}
P, ok := Multiply(A, B)
if !ok {
panic("Invalid dimensions")
}
fmt.Printf("Product of A and B:\n%v\n", p)
fmt.Printf("Matrix A:\n%s\n\n", A)
fmt.Printf("Matrix B:\n%s\n\n", B)
fmt.Printf("Product of A and B:\n%s\n\n", P)
}

View file

@ -0,0 +1,56 @@
package main
import "fmt"
type matrix struct {
stride int
ele []float64
}
func (m *matrix) print(heading string) {
if heading > "" {
fmt.Print("\n", heading, "\n")
}
for e := 0; e < len(m.ele); e += m.stride {
fmt.Printf("%8.3f ", m.ele[e:e+m.stride])
fmt.Println()
}
}
func (m1 *matrix) multiply(m2 *matrix) (m3 *matrix, ok bool) {
if m1.stride*m2.stride != len(m2.ele) {
return nil, false
}
m3 = &matrix{m2.stride, make([]float64, (len(m1.ele)/m1.stride)*m2.stride)}
for m1c0, m3x := 0, 0; m1c0 < len(m1.ele); m1c0 += m1.stride {
for m2r0 := 0; m2r0 < m2.stride; m2r0++ {
for m1x, m2x := m1c0, m2r0; m2x < len(m2.ele); m2x += m2.stride {
m3.ele[m3x] += m1.ele[m1x] * m2.ele[m2x]
m1x++
}
m3x++
}
}
return m3, true
}
func main() {
a := matrix{4, []float64{
1, 2, 3, 4,
5, 6, 7, 8,
}}
b := matrix{3, []float64{
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,
}}
p, ok := a.multiply(&b)
a.print("Matrix A:")
b.print("Matrix B:")
if !ok {
fmt.Println("not conformable for matrix multiplication")
return
}
p.print("Product of A and B:")
}

View file

@ -1,6 +1,6 @@
sub mmult(@a,@b) {
my @p;
for ^@a X ^@b[0] -> $r, $c {
for ^@a X ^@b[0] -> ($r, $c) {
@p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b;
}
@p;

View file

@ -1,7 +1,11 @@
sub mmult(@a,@b) {
for ^@a -> $r {[
for ^@b[0] -> $c {
[+] (@a[$r][^@b] Z* @b[^@b]»[$c])
sub mmult(\a,\b) {
[
for ^a -> \r {
[
for ^b[0] -> \c {
[+] a[r;^b] Z* b[^b;c]
}
]
}
]}
]
}

View file

@ -0,0 +1,31 @@
function array-mult($A, $B) {
$C = @()
if($n -gt 0) {
$C = 0..($n-1)| foreach{@(0)}
0..($n-1)| foreach{
$i = $_
$C[$i] = 0..($n-1)| foreach{
$j = $_
$((0..($n-1) | foreach{
$k = $_
$A[$i][$k]*$B[$k][$j]
} | measure -Sum).Sum)
}
}
}
$C
}
function show($a) {
if($a.Count -gt 0) {
$n = $a.Count - 1
0..$n | foreach{ "$($a[$_][0..$n])" }
}
}
$A = @(@(1,2),@(3,4))
$B = @(@(5,6),@(7,8))
$I = @(@(1,0),@(0,1))
$C = array-mult $A $B
$D = array-mult $A $I
show $C
" "
show $D

View file

@ -1,39 +1,37 @@
/*REXX program multiplies 2 matrixes together, shows matrixes and result*/
x. = /*the beginnings of the A matrix.*/
x.1 = 1 2 /*╔═════════════════════════════╗*/
x.2 = 3 4 /*║As none of the values haven't║*/
x.3 = 5 6 /*║a sign, quotes aren't needed.║*/
x.4 = 7 8 /*╚═════════════════════════════╝*/
do r=1 while x.r\=='' /*build the "A" matric from X. #s*/
do c=1 while x.r\==''; parse var x.r a.r.c x.r; end
end /*r*/
Arows=r-1 /*adjust number of rows (DO loop)*/
Acols=c-1 /* " " " cols " " */
y. = /*the beginnings of the B matrix.*/
y.1 = 1 2 3
y.2 = 4 5 6
do r=1 while y.r\=='' /*build the "B" matric from Y. #s*/
do c=1 while y.r\==''; parse var y.r b.r.c y.r; end
end
Brows=r-1 /*adjust number of rows (DO loop)*/
Bcols=c-1 /* " " " cols " " */
c.=0; L=0 /*L is max width of an element.*/
do i =1 for Arows /*multiply matrix A & B ──► C */
do j =1 for Bcols
/*REXX program multiplies two matrices together, displays matrices and result.*/
x.=; x.1=1 2 /*╔═══════════════════════════════════╗*/
x.2=3 4 /*║ As none of the matrix values have ║*/
x.3=5 6 /*║ a sign, quotes aren't needed. ║*/
x.4=7 8 /*╚═══════════════════════════════════╝*/
do r=1 while x.r\=='' /*build the "A" matrix from X. numbers.*/
do c=1 while x.r\==''; parse var x.r a.r.c x.r; end
end /*r*/
Arows=r-1 /*adjust the number of rows (DO loop).*/
Acols=c-1 /* " " " " cols " " .*/
y.=; y.1=1 2 3
y.2=4 5 6
do r=1 while y.r\=='' /*build the "B" matrix from Y. numbers.*/
do c=1 while y.r\==''; parse var y.r b.r.c y.r; end
end /*r*/
Brows=r-1 /*adjust the number of rows (DO loop).*/
Bcols=c-1 /* " " " " cols " " */
c.=0; w=0 /*W is max width of an matrix element.*/
do i=1 for Arows /*multiply matrix A and B ───► C */
do j=1 for Bcols
do k=1 for Acols
c.i.j = c.i.j + a.i.k * b.k.j; L=max(L,length(c.i.j))
c.i.j = c.i.j + a.i.k * b.k.j; w=max(w, length(c.i.j))
end /*k*/
end /*j*/
end /*i*/
call showMatrix 'A', Arows, Acols /*display matrix A ───► terminal.*/
call showMatrix 'B', Brows, Bcols /* " " B ───► " */
call showMatrix 'C', Arows, Bcols /* " " C ───► " */
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SHOWMATRIX subroutine───────────────*/
showMatrix: parse arg mat,rows,cols; say
say center(mat 'matrix',cols*(L+1)+4,"")
do r =1 for rows; _=
do c=1 for cols; _=_ right(value(mat'.'r'.'c),L); end; say _
end /*r*/
call showMatrix 'A', Arows, Acols /*display matrix A ───► the terminal.*/
call showMatrix 'B', Brows, Bcols /* " " B ───► " " */
call showMatrix 'C', Arows, Bcols /* " " C ───► " " */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
showMatrix: parse arg mat,rows,cols; say
say center(mat 'matrix', cols*(w+1)+4, "")
do r =1 for rows; _=
do c=1 for cols; _=_ right(value(mat'.'r'.'c), w); end; say _
end /*r*/
return

View file

@ -0,0 +1,19 @@
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
Call multiply_matrix(matrix1,matrix2)
Sub multiply_matrix(arr1,arr2)
For i = 0 To UBound(arr1)
For j = 0 To 2
WScript.StdOut.Write (arr1(i,j) * arr2(i,j)) & vbTab
Next
WScript.StdOut.WriteLine
Next
End Sub