Skip to content

Commit

Permalink
polished dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyday567 committed Jun 22, 2024
1 parent 85132f0 commit 8598550
Show file tree
Hide file tree
Showing 6 changed files with 829 additions and 170 deletions.
261 changes: 247 additions & 14 deletions readme.org
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,121 @@ import Prettyprinter
#+end_src

#+RESULTS:
: [3 of 5] Compiling NumHask.Array.Dynamic ( src/NumHask/Array/Dynamic.hs, interpreted ) [Source file changed]
: [4 of 5] Compiling NumHask.Array.Fixed ( src/NumHask/Array/Fixed.hs, interpreted ) [Source file changed]
: [5 of 5] Compiling NumHask.Array ( src/NumHask/Array.hs, interpreted ) [NumHask.Array.Fixed changed]
: Ok, five modules loaded.
#+begin_example
Build profile: -w ghc-9.8.2 -O1
In order, the following will be built (use -v for more details):
- numhask-array-0.12 (lib) (ephemeral targets)
Preprocessing library for numhask-array-0.12..
GHCi, version 9.8.2: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /Users/tonyday567/haskell/numhask-array/.ghci
[1 of 5] Compiling NumHask.Array.Shape ( src/NumHask/Array/Shape.hs, interpreted )
[2 of 5] Compiling NumHask.Array.Sort ( src/NumHask/Array/Sort.hs, interpreted )
[3 of 5] Compiling NumHask.Array.Dynamic ( src/NumHask/Array/Dynamic.hs, interpreted )
[4 of 5] Compiling NumHask.Array.Fixed ( src/NumHask/Array/Fixed.hs, interpreted )
[5 of 5] Compiling NumHask.Array ( src/NumHask/Array.hs, interpreted )
Ok, five modules loaded.
Ok, five modules loaded.
#+end_example

** backpermute


| function | f | g |
|---------------+--------------------------------------------+-------------------------------------------------------------|
| takeD d t | modifyIndex d . min t | id |
| dropD d t | \s -> modifyIndex d (const ((s !! d) - t)) | (+t) |
| reshape s | id | shapen (shape a) .flatten |
| rotates rs | id | S.rotateIndex rs |
| diag | minRank | replicate (rank a) . head |
| undiag | replicate r . head | bool (const zero) id . S.isDiag |
| selectD d x | S.deleteIndex d | S.addIndex d x |
| extracts ds | S.takeIndexes ds | S.addIndexes ds |
| joins ds | S.addIndexes ds (index a 0) | index (index a (S.takeIndexes s ds)) (S.deleteIndexes s ds) |
| expand f | (<>) | (List.take (rank a), List.drop (rank a)) |
| slice pss | (S.ranks pss) | List.zipWith (!!) pss |
| reorder ds | S.reorder ds | S.addIndexes [] ds |
| reverses ds | id | S.reverseIndex ds |
| inflates ds n | S.addIndexes d n | S.deleteIndexes d |
| transpose | List.reverse | List.reverse |
| | | |



#+begin_src haskell-ng :results output
import qualified Data.List as List
x = iota [2,3]
x
D.backpermute (List.drop 1 :: [Int] -> [Int]) x
#+end_src

#+RESULTS:
: UnsafeArray [2,3] [0,1,2,3,4,5]
: UnsafeArray [3] [0,0,0]

** iota

#+begin_src haskell-ng :results output
D.range (D.toScalar 3)
D.join $ D.tabulateA (D.toScalar 3) id
D.join $ D.tabulate (D.toScalar 3) id
D.tabulate (D.toScalar 3) id
D.tabulateA (D.toScalar 3) id
#+end_src

#+RESULTS:
: UnsafeArray [3] [0,1,2]
: UnsafeArray [3] [0,1,2]
: UnsafeArray [3,1] [0,1,2]
: UnsafeArray [3] [UnsafeArray [1] [0],UnsafeArray [1] [1],UnsafeArray [1] [2]]
: UnsafeArray [3] [UnsafeArray [] [0],UnsafeArray [] [1],UnsafeArray [] [2]]

** zipWith

#+begin_src haskell-ng :results output
m = D.array [3,4] [0..11]
m
D.zipWith (-) m m
D.zipWithE (-) m m
#+end_src

#+RESULTS:
: UnsafeArray [3,4] [0,1,2,3,4,5,6,7,8,9,10,11]
: UnsafeArray [3,4] [0,0,0,0,0,0,0,0,0,0,0,0]
: UnsafeArray [3,4] [0,0,0,0,0,0,0,0,0,0,0,0]

** cons

#+begin_src haskell-ng :results output
(x:|xs) = array [4] [0..3]
x
xs
(x:|xs)
#+end_src

#+RESULTS:
: UnsafeArray [] [0]
: UnsafeArray [3] [1,2,3]
: UnsafeArray [4] [0,1,2,3]


** transmit

Apply a binary fnuction to sub-components of an array matching the size of the second array, and an array.

#+begin_src haskell-ng :results output
import qualified Data.List as List
a = D.array [2,3] [0..5]
b = D.array [3] [0..2]
D.extracts [1] a
D.extracts [0] b
f = D.concatenate 0
D.transmits [(1,0)] f a b
#+end_src

#+RESULTS:
: UnsafeArray [3] [UnsafeArray [2] [0,3],UnsafeArray [2] [1,4],UnsafeArray [2] [2,5]]
: UnsafeArray [3] [UnsafeArray [] [0],UnsafeArray [] [1],UnsafeArray [] [2]]
: UnsafeArray [3,3] [0,3,0,1,4,1,2,5,2]

** 3-Dim example

Expand All @@ -128,7 +239,7 @@ pretty $ (D.shape @[Int]) <$> D.extracts [0] a
#+RESULTS:
: [[3,4],[3,4]]

** scalars
*** scalars

#+begin_src haskell-ng :results output
s = D.tabulate [] (const 0) :: D.Array Int
Expand Down Expand Up @@ -164,29 +275,33 @@ s3''
D.indices []
D.indices [3]
D.indices (Scalar 3)
D.indices (D.array [] [3])
D.indices [2,3]
#+end_src

#+RESULTS:
: Array {toShape = [], toVector = [[]]}
: Array {toShape = [3], toVector = [[0],[1],[2]]}
: Array {toShape = [3], toVector = [Scalar 0,Scalar 1,Scalar 2]}
: Array {toShape = [2,3], toVector = [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]]}
: UnsafeArray [] [[]]
: UnsafeArray [3] [[0],[1],[2]]
: UnsafeArray [] [Scalar 0,Scalar 1,Scalar 2]
: UnsafeArray [3] [UnsafeArray [1] [0],UnsafeArray [1] [1],UnsafeArray [1] [2]]
: UnsafeArray [2,3] [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]]

*** range

#+begin_src haskell-ng :results output
joins' $ fmap D.asArray $ D.indices []
joins' $ fmap D.asArray $ D.indices [3]
joins' $ fmap D.asArray $ D.indices (Scalar 3)
joins' $ fmap D.asArray $ D.indices (D.array [] [3])
joins' $ fmap D.asArray $ D.indices [2,3]
#+end_src

#+RESULTS:
: Array {toShape = [0], toVector = []}
: Array {toShape = [3,1], toVector = [0,1,2]}
: Array {toShape = [3], toVector = [0,1,2]}
: Array {toShape = [2,3,2], toVector = [0,0,0,1,0,2,1,0,1,1,1,2]}
: UnsafeArray [0] []
: UnsafeArray [3,1] [0,1,2]
: UnsafeArray [] [0]
: UnsafeArray [3,1] [0,1,2]
: UnsafeArray [2,3,2] [0,0,0,1,0,2,1,0,1,1,1,2]

*** diag

Expand Down Expand Up @@ -275,7 +390,6 @@ pretty $ D.folds [0,1] (sum . fmap (const one)) a
pretty $ D.folds [0, 1] (sum . fmap (const one)) a
#+end_example


** extracts

#+begin_src haskell-ng :results output
Expand Down Expand Up @@ -378,6 +492,125 @@ pretty $ with (D.array @[Int] [2,3,4] [1..24]) (NumHask.Array.Fixed.selects (Pro
: :: F.Array '[4] Int
: [17,18,19,20]

** cons

#+begin_src haskell-ng :results output
let a = D.array [2,3,4] [0..23] :: D.Array Int
a
#+end_src

#+RESULTS:
: UnsafeArray [2,3,4] [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]

#+begin_src haskell-ng :results output
m = D.array [3,2] [0..5]
(x:|xs) = D.array [3,2] [0..5]
pretty x
pretty xs
#+end_src

#+RESULTS:
#+begin_example
[0,1]
[0,2,4]
<interactive>:402:1: error: [GHC-87543]
Ambiguous occurrence ‘selects’.
It could refer to
either ‘F.selects’,
imported from ‘NumHask.Array.Fixed’
(and originally defined at src/NumHask/Array/Fixed.hs:486:1-7),
or ‘D.selects’,
imported from ‘NumHask.Array.Dynamic’
(and originally defined at src/NumHask/Array/Dynamic.hs:489:1-7).
#+end_example

#+begin_src haskell-ng :results output
D.selects' [(0,0)] m
D.drops [1,0] m
#+end_src

#+RESULTS:
: UnsafeArray [2] [0,1]
: UnsafeArray [2,2] [2,3,4,5]
#+begin_src haskell-ng :results output
:t foldl'
#+end_src

#+RESULTS:
: foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b

* scalar applications

#+begin_src haskell-ng :results output
S.shapenL [] 20
S.flattenL [] []
S.checkIndex 0 0
S.deleteIndex [] 2
S.replaceIndex 0 1 []
S.modifyIndex 0 (+1) []
S.replaceIndex 1 3 []
S.reverseIndex [0] [] []
S.rotateIndex [(0,1)] [] [1]
#+end_src

#+RESULTS:
: []
: 0
: True
: []
: []
: []
: []
: []
: [1]

#+begin_src haskell-ng :results output
D.stretch 0 (D.toScalar 1)
#+end_src

#+RESULTS:
: UnsafeArray [1] [1]

#+begin_src haskell-ng :results output
x = D.toScalar 1 :: D.Array Int
y = D.toScalar 2 :: D.Array Int
D.length x
fmap (+1) x
-- > toScalar x == D.tabulate [] (const x)
D.index x []
D.indices []
D.selects [] x
D.selects [(0,0)] x
D.selects [(2,2)] x
D.takes [] x
D.takes [(0,1)] x
D.takes [(1,1)] x

-- D.drops [(0,0)] x
-- D.row 0 (D.toScalar 2)
-- D.concatenate 0 (D.toScalar 2) (D.toScalar 3)
#+end_src

#+RESULTS:
#+begin_example
1
UnsafeArray [] [2]
1
UnsafeArray [] [[]]
UnsafeArray [] [1]
UnsafeArray [] [1]
UnsafeArray [] [1]
UnsafeArray [] [1]
UnsafeArray [1] [1]
UnsafeArray [1] [1]
#+end_example

#+begin_src haskell-ng :results output
A.divide (D.toScalar 1) (D.toScalar 2)
#+end_src

#+RESULTS:
: Right (UnsafeArray [] [0.5])

* fixed

Expand Down
2 changes: 1 addition & 1 deletion src/NumHask/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module NumHask.Array
where

import NumHask.Array.Fixed
import NumHask.Array.Shape hiding (size, rank)
import NumHask.Array.Shape hiding (size, rank, concatenate, reorder, squeeze)

-- $imports
--
Expand Down
Loading

0 comments on commit 8598550

Please sign in to comment.