Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -1,26 +1,10 @@
foldlZipWith::(a -> b -> c) -> (d -> c -> d) -> d -> [a] -> [b] -> d
foldlZipWith _ _ u [] _ = u
foldlZipWith _ _ u _ [] = u
foldlZipWith f g u (x:xs) (y:ys) = foldlZipWith f g (g u (f x y)) xs ys
multiply :: Num a => [[a]] -> [[a]] -> [[a]]
multiply us vs = map (mult [] vs) us
where
mult xs [] _ = xs
mult xs _ [] = xs
mult [] (zs:zss) (y:ys) = mult (map (y *) zs) zss ys
mult xs (zs:zss) (y:ys) = mult (zipWith (\u v -> u + v * y) xs zs) zss ys
foldl1ZipWith::(a -> b -> c) -> (c -> c -> c) -> [a] -> [b] -> c
foldl1ZipWith _ _ [] _ = error "First list is empty"
foldl1ZipWith _ _ _ [] = error "Second list is empty"
foldl1ZipWith f g (x:xs) (y:ys) = foldlZipWith f g (f x y) xs ys
multAdd::(a -> b -> c) -> (c -> c -> c) -> [[a]] -> [[b]] -> [[c]]
multAdd f g xs ys = map (\us -> foldl1ZipWith (\u vs -> map (f u) vs) (zipWith g) us ys) xs
mult:: Num a => [[a]] -> [[a]] -> [[a]]
mult xs ys = multAdd (*) (+) xs ys
test a b = do
let c = mult a b
putStrLn "a ="
mapM_ print a
putStrLn "b ="
mapM_ print b
putStrLn "c = a * b = mult a b ="
mapM_ print c
main = test [[1, 2],[3, 4]] [[-3, -8, 3],[-2, 1, 4]]
main :: IO ()
main = mapM_ print $ multiply [[1, 2], [3, 4]] [[-3, -8, 3], [-2, 1, 4]

View file

@ -0,0 +1,12 @@
mult :: Num a => [[a]] -> [[a]] -> [[a]]
mult uss vss =
map
((\xs ->
if null xs
then []
else foldl1 (zipWith (+)) xs) .
zipWith (flip (map . (*))) vss)
uss
main :: IO ()
main = mapM_ print $ mult [[1, 2], [3, 4]] [[-3, -8, 3], [-2, 1, 4]]

View file

@ -0,0 +1,23 @@
{def mat*mat
{lambda {:a :b}
{#.new {map {{lambda {:a :b :i} // loop on row
{#.new {map {{lambda {:a :b :i :j} // loop on col
{+ {map {{lambda {:a :b :i :j :k} // sum of prod
{* {#.get {#.get :a :i} :k} // of a[i,k]
{#.get {#.get :b :k} :j}} // and b[k,j]
} :a :b :i :j} {serie 0 {- {#.length :b} 1}} }}
} :a :b :i} {serie 0 {- {#.length {#.get :b 0}} 1}} }}
} :a :b} {serie 0 {- {#.length :a} 1}} }
}}}
{def A {#.new {#.new 1 2}
{#.new 3 4}
{#.new 5 6}
{#.new 7 8}}} = [[1,2],[3,4],[5,6],[7,8]]
{def B {#.new {#.new 1 2 3}
{#.new 4 5 6}}} = [[1,2,3],[4,5,6]]
{mat*mat {A} {B}}
-> [[ 9,12,15],
[19,26,33],
[29,40,51],
[39,54,69]]

View file

@ -0,0 +1,59 @@
@inlinable
public func matrixMult<T: Numeric>(_ m1: [[T]], _ m2: [[T]]) -> [[T]] {
let n = m1[0].count
let m = m1.count
let p = m2[0].count
guard m != 0 else {
return []
}
precondition(n == m2.count)
var ret = Array(repeating: Array(repeating: T.zero, count: p), count: m)
for i in 0..<m {
for j in 0..<p {
for k in 0..<n {
ret[i][j] += m1[i][k] * m2[k][j]
}
}
}
return ret
}
@inlinable
public func printMatrix<T>(_ matrix: [[T]]) {
guard !matrix.isEmpty else {
print()
return
}
let rows = matrix.count
let cols = matrix[0].count
for i in 0..<rows {
for j in 0..<cols {
print(matrix[i][j], terminator: " ")
}
print()
}
}
let m1 = [
[6.5, 2, 3],
[4.5, 1, 5]
]
let m2 = [
[10.0, 16, 23, 50],
[12, -8, 16, -4],
[70, 60, -1, -2]
]
let m3 = matrixMult(m1, m2)
printMatrix(m3)