September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,26 +1,24 @@
|
|||
-- matrixMultiply :: [[n]] -> [[n]] -> [[n]]
|
||||
-- matrixMultiply :: Num a => [[a]] -> [[a]] -> [[a]]
|
||||
to matrixMultiply(a, b)
|
||||
script rows
|
||||
property xs : transpose(b)
|
||||
|
||||
on lambda(row)
|
||||
on |λ|(row)
|
||||
script columns
|
||||
on lambda(col)
|
||||
dotProduct(row, col)
|
||||
end lambda
|
||||
on |λ|(col)
|
||||
my dotProduct(row, col)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(columns, xs)
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(rows, a)
|
||||
end matrixMultiply
|
||||
|
||||
|
||||
|
||||
-- TEST
|
||||
|
||||
-- TEST -----------------------------------------------------------
|
||||
on run
|
||||
matrixMultiply({¬
|
||||
{-1, 1, 4}, ¬
|
||||
|
|
@ -37,64 +35,34 @@ on run
|
|||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ----------------------------------------------
|
||||
|
||||
-- dotProduct :: [n] -> [n] -> Maybe n
|
||||
on dotProduct(xs, ys)
|
||||
script product
|
||||
on lambda(a, b)
|
||||
script mult
|
||||
on |λ|(a, b)
|
||||
a * b
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
if length of xs is not length of ys then
|
||||
missing value
|
||||
else
|
||||
sum(zipWith(product, xs, ys))
|
||||
sum(zipWith(mult, xs, ys))
|
||||
end if
|
||||
end dotProduct
|
||||
|
||||
-- transpose :: [[a]] -> [[a]]
|
||||
on transpose(xss)
|
||||
script column
|
||||
on lambda(_, iCol)
|
||||
script row
|
||||
on lambda(xs)
|
||||
item iCol of xs
|
||||
end lambda
|
||||
end script
|
||||
|
||||
map(row, xss)
|
||||
end lambda
|
||||
end script
|
||||
|
||||
map(column, item 1 of xss)
|
||||
end transpose
|
||||
|
||||
-- sum :: [n] -> n
|
||||
on sum(xs)
|
||||
script add
|
||||
on lambda(a, b)
|
||||
a + b
|
||||
end lambda
|
||||
end script
|
||||
|
||||
foldl(add, 0, xs)
|
||||
end sum
|
||||
|
||||
|
||||
|
||||
-- GENERIC LIBRARY FUNCTIONS
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
-- foldr :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldr(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
repeat with i from lng to 1 by -1
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
end foldr
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
|
|
@ -102,35 +70,80 @@ on map(f, xs)
|
|||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set lng to length of xs
|
||||
if lng is not length of ys then
|
||||
missing value
|
||||
-- min :: Ord a => a -> a -> a
|
||||
on min(x, y)
|
||||
if y < x then
|
||||
y
|
||||
else
|
||||
tell mReturn(f)
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
x
|
||||
end if
|
||||
end zipWith
|
||||
end min
|
||||
|
||||
-- Script | Handler -> Script
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- product :: Num a => [a] -> a
|
||||
on product(xs)
|
||||
script mult
|
||||
on |λ|(a, b)
|
||||
a * b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldr(mult, 1, xs)
|
||||
end product
|
||||
|
||||
-- sum :: Num a => [a] -> a
|
||||
on sum(xs)
|
||||
script add
|
||||
on |λ|(a, b)
|
||||
a + b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldr(add, 0, xs)
|
||||
end sum
|
||||
|
||||
-- transpose :: [[a]] -> [[a]]
|
||||
on transpose(xss)
|
||||
script column
|
||||
on |λ|(_, iCol)
|
||||
script row
|
||||
on |λ|(xs)
|
||||
item iCol of xs
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(row, xss)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(column, item 1 of xss)
|
||||
end transpose
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set lng to min(length of xs, length of ys)
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end zipWith
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
include fsl-util.f
|
||||
S" fsl-util.fs" REQUIRED
|
||||
S" fsl/dynmem.seq" REQUIRED
|
||||
: F+! ( addr -- ) ( F: r -- ) DUP F@ F+ F! ;
|
||||
: FSQR ( F: r1 -- r2 ) FDUP F* ;
|
||||
S" fsl/gaussj.seq" REQUIRED
|
||||
|
||||
3 3 float matrix A{{
|
||||
A{{ 3 3 }}fread 1e 2e 3e 4e 5e 6e 7e 8e 9e
|
||||
3 3 float matrix B{{
|
||||
B{{ 3 3 }}fread 3e 3e 3e 2e 2e 2e 1e 1e 1e
|
||||
3 3 float matrix C{{ \ result
|
||||
3 3 float matrix A{{
|
||||
1e 2e 3e 4e 5e 6e 7e 8e 9e 3 3 A{{ }}fput
|
||||
3 3 float matrix B{{
|
||||
3e 3e 3e 2e 2e 2e 1e 1e 1e 3 3 B{{ }}fput
|
||||
float dmatrix C{{ \ result
|
||||
|
||||
A{{ B{{ C{{ mat*
|
||||
C{{ }}print
|
||||
A{{ 3 3 B{{ 3 3 & C{{ mat*
|
||||
3 3 C{{ }}fprint
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
[[51, -8, 26, -18], [-8, -38, -6, 34],
|
||||
[33, 42, 38, -14], [17, 74, 72, 44]]
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
((() => {
|
||||
'use strict';
|
||||
|
||||
// matrixMultiply :: Num a => [[a]] -> [[a]] -> [[a]]
|
||||
const matrixMultiply = (a, b) => {
|
||||
const bCols = transpose(b);
|
||||
return a.map(aRow => bCols.map(bCol => dotProduct(aRow, bCol)));
|
||||
}
|
||||
|
||||
// dotProduct :: Num a => [[a]] -> [[a]] -> [[a]]
|
||||
const dotProduct = (xs, ys) => sum(zipWith(product, xs, ys));
|
||||
|
||||
|
||||
// GENERIC
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = (f, xs, ys) =>
|
||||
xs.length === ys.length ? (
|
||||
xs.map((x, i) => f(x, ys[i]))
|
||||
) : undefined;
|
||||
|
||||
// transpose :: [[a]] -> [[a]]
|
||||
const transpose = xs =>
|
||||
xs[0].map((_, iCol) => xs.map(row => row[iCol]));
|
||||
|
||||
// sum :: (Num a) => [a] -> a
|
||||
const sum = xs =>
|
||||
xs.reduce((a, x) => a + x, 0);
|
||||
|
||||
// product :: Num a => a -> a -> a
|
||||
const product = (a, b) => a * b;
|
||||
|
||||
|
||||
// TEST
|
||||
return matrixMultiply(
|
||||
[
|
||||
[-1, 1, 4],
|
||||
[6, -4, 2],
|
||||
[-3, 5, 0],
|
||||
[3, 7, -2]
|
||||
],
|
||||
|
||||
[
|
||||
[-1, 1, 4, 8],
|
||||
[6, 9, 10, 2],
|
||||
[11, -4, 5, -3]
|
||||
]
|
||||
);
|
||||
|
||||
// --> [[51, -8, 26, -18], [-8, -38, -6, 34],
|
||||
// [33, 42, 38, -14], [17, 74, 72, 44]]
|
||||
}))();
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[[51, -8, 26, -18], [-8, -38, -6, 34],
|
||||
[33, 42, 38, -14], [17, 74, 72, 44]]
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
// version 1.1.3
|
||||
|
||||
typealias Vector = DoubleArray
|
||||
typealias Matrix = Array<Vector>
|
||||
|
||||
operator fun Matrix.times(other: Matrix): Matrix {
|
||||
val rows1 = this.size
|
||||
val cols1 = this[0].size
|
||||
val rows2 = other.size
|
||||
val cols2 = other[0].size
|
||||
require(cols1 == rows2)
|
||||
val result = Matrix(rows1) { Vector(cols2) }
|
||||
for (i in 0 until rows1) {
|
||||
for (j in 0 until cols2) {
|
||||
for (k in 0 until rows2) {
|
||||
result[i][j] += this[i][k] * other[k][j]
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun printMatrix(m: Matrix) {
|
||||
for (i in 0 until m.size) println(m[i].contentToString())
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val m1 = arrayOf(
|
||||
doubleArrayOf(-1.0, 1.0, 4.0),
|
||||
doubleArrayOf( 6.0, -4.0, 2.0),
|
||||
doubleArrayOf(-3.0, 5.0, 0.0),
|
||||
doubleArrayOf( 3.0, 7.0, -2.0)
|
||||
)
|
||||
val m2 = arrayOf(
|
||||
doubleArrayOf(-1.0, 1.0, 4.0, 8.0),
|
||||
doubleArrayOf( 6.0, 9.0, 10.0, 2.0),
|
||||
doubleArrayOf(11.0, -4.0, 5.0, -3.0)
|
||||
)
|
||||
printMatrix(m1 * m2)
|
||||
}
|
||||
58
Task/Matrix-multiplication/Rust/matrix-multiplication.rust
Normal file
58
Task/Matrix-multiplication/Rust/matrix-multiplication.rust
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
struct Matrix {
|
||||
dat: [[f32; 3]; 3]
|
||||
}
|
||||
|
||||
impl Matrix {
|
||||
pub fn mult_m(a: Matrix, b: Matrix) -> Matrix
|
||||
{
|
||||
let mut out = Matrix {
|
||||
dat: [[0., 0., 0.],
|
||||
[0., 0., 0.],
|
||||
[0., 0., 0.]
|
||||
]
|
||||
};
|
||||
|
||||
for i in 0..3{
|
||||
for j in 0..3 {
|
||||
for k in 0..3 {
|
||||
out.dat[i][j] += a.dat[i][k] * b.dat[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub fn print(self)
|
||||
{
|
||||
for i in 0..3 {
|
||||
for j in 0..3 {
|
||||
print!("{} ", self.dat[i][j]);
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
let a = Matrix {
|
||||
dat: [[1., 2., 3.],
|
||||
[4., 5., 6.],
|
||||
[7., 8., 9.]
|
||||
]
|
||||
};
|
||||
|
||||
let b = Matrix {
|
||||
dat: [[1., 0., 0.],
|
||||
[0., 1., 0.],
|
||||
[0., 0., 1.]]
|
||||
};
|
||||
|
||||
|
||||
|
||||
let c = Matrix::mult_m(a, b);
|
||||
|
||||
|
||||
c.print();
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
|
||||
A:=GSL.Matrix(4,2).set(1,2, 3,4, 5,6, 7,8);
|
||||
B:=GSL.Matrix(2,3).set(1,2,3, 4,5,6);
|
||||
(A*B).format().println(); // creates a new matrix
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
fcn matMult(a,b){
|
||||
n,m,p:=a[0].len(),a.len(),b[0].len();
|
||||
ans:=(0).pump(m,List().write, (0).pump(p,List,0).copy); // matrix of zeros
|
||||
foreach i,j,k in (m,p,n){ ans[i][j]+=a[i][k]*b[k][j]; }
|
||||
ans
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
a:=L( L(1,2,), L(3,4,), L(5,6,), L(7,8) );
|
||||
b:=L( L(1,2,3,), L(4,5,6) );
|
||||
printM(matMult(a,b));
|
||||
|
||||
fcn printM(m){ m.pump(Console.println,rowFmt) }
|
||||
fcn rowFmt(row){ ("%4d "*row.len()).fmt(row.xplode()) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue