Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,17 @@
// matkronprod.js
// Prime function:
// mkp arrow function: Return the Kronecker product of the a and b matrices.
// Note: both a and b must be matrices, i.e., 2D rectangular arrays.
mkp=(a,b)=>a.map(a=>b.map(b=>a.map(y=>b.map(x=>r.push(y*x)),t.push(r=[]))),t=[])&&t;
// Helper functions:
// Log title and matrix mat to console
function matl2cons(title,mat) {console.log(title); console.log(mat.join`\n`)}
// Print title to document
function pttl2doc(title) {document.write('<b>'+title+'</b><br />')}
// Print title and matrix mat to document
function matp2doc(title,mat) {
document.write('<b>'+title+'</b>:<br />');
for (var i = 0; i < mat.length; i++) {
document.write('&nbsp;&nbsp;'+mat[i].join(' ')+'<br />');
}
}

View file

@ -0,0 +1,18 @@
<!-- KronProdTest.html -->
<html><head>
<title>Kronecker product: Sample 1 (from Wikipedia) and Sample 2</title>
<script src="matkronprod.js"></script>
<script>
var mr,ttl='Kronecker product of A and B matrices';
[ {a:[[1,2],[3,4]],b:[[0,5],[6,7]] },
{a:[[0,1,0],[1,1,1],[0,1,0]],b:[[1,1,1,1],[1,0,0,1],[1,1,1,1]] }
].forEach(m=>{
console.log(ttl); pttl2doc(ttl);
matl2cons('A',m.a); matp2doc('A',m.a);
matl2cons('B',m.b); matp2doc('B',m.b);
mr=mkp(m.a,m.b);
matl2cons('A x B',mr); matp2doc('A x B',mr);
})
</script>
</head><body></body>
</html>

View file

@ -0,0 +1,30 @@
// matkronprod2.js
// Prime function:
// mkp2(): Return the Kronecker product of the a and b matrices
// Note: both a and b must be matrices, i.e., 2D rectangular arrays.
function mkp2(a,b) {
var m=a.length, n=a[0].length, p=b.length, q=b[0].length;
var rtn=m*p, ctn=n*q; var r=new Array(rtn);
for (var i=0; i<rtn; i++) {r[i]=new Array(ctn)
for (var j=0;j<ctn;j++) {r[i][j]=0}
}
for (var i=0; i<m; i++) {
for (var j=0; j<n; j++) {
for (var k=0; k<p; k++) {
for (var l=0; l<q; l++) {
r[p*i+k][q*j+l]=a[i][j]*b[k][l];
}}}}//all4forend
return(r);
}
// Helper functions:
// Log title and matrix mat to console
function matl2cons(title,mat) {console.log(title); console.log(mat.join`\n`)}
// Print title to document
function pttl2doc(title) {document.write('<b>'+title+'</b><br>')}
// Print title and matrix mat to document
function matp2doc(title,mat) {
document.write('<b>'+title+'</b>:<br>');
for (var i=0; i < mat.length; i++) {
document.write('&nbsp;&nbsp;'+mat[i].join(' ')+'<br>');
}
}

View file

@ -0,0 +1,18 @@
<!-- KronProdTest2.html -->
<html><head>
<title>Kronecker product v.2: Sample 1 (from Wikipedia) and Sample 2</title>
<script src="matkronprod2.js"></script>
<script>
var mr,ttl='Kronecker product of A and B matrices';
[ {a:[[1,2],[3,4]],b:[[0,5],[6,7]] },
{a:[[0,1,0],[1,1,1],[0,1,0]],b:[[1,1,1,1],[1,0,0,1],[1,1,1,1]] }
].forEach(m=>{
console.log(ttl); pttl2doc(ttl);
matl2cons('A',m.a); matp2doc('A',m.a);
matl2cons('B',m.b); matp2doc('B',m.b);
mr=mkp2(m.a,m.b);
matl2cons('A x B',mr); matp2doc('A x B',mr);
})
</script>
</head><body></body>
</html>

View file

@ -0,0 +1,87 @@
(() => {
'use strict';
// --------- KRONECKER PRODUCT OF TWO MATRICES ---------
// kprod :: [[Num]] -> [[Num]] -> [[Num]]
const kprod = xs =>
ys => concatMap(
compose(map(concat), transpose)
)(
map(map(
flip(compose(map, map, mul))(ys)
))(xs)
);
// ----------------------- TEST ------------------------
// main :: IO ()
const main = () =>
unlines(map(compose(unlines, map(show)))([
kprod([
[1, 2],
[3, 4]
])([
[0, 5],
[6, 7]
]), [], // One empty output line
kprod([
[0, 1, 0],
[1, 1, 1],
[0, 1, 0]
])([
[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 1, 1, 1]
])
]));
// ----------------- GENERIC FUNCTIONS -----------------
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
x => fs.reduceRight((a, f) => f(a), x);
// concat :: [[a]] -> [a]
const concat = xs => [].concat.apply([], xs);
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f =>
xs => xs.flatMap(f);
// flip :: (a -> b -> c) -> b -> a -> c
const flip = f =>
x => y => f(y)(x);
// map :: (a -> b) -> [a] -> [b]
const map = f =>
xs => xs.map(f);
// mul (*) :: Num a => a -> a -> a
const mul = a =>
b => a * b;
// show :: a -> String
const show = x =>
JSON.stringify(x); //, null, 2);
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
xs[0].map((_, col) => xs.map(row => row[col]));
// unlines :: [String] -> String
const unlines = xs =>
xs.join('\n');
// MAIN ---
console.log(main());
})();