Skip to content

Commit 01e297c

Browse files
authored
Merge pull request #3 from DataHaskell/hmatrix-add
hmatrix add
2 parents ff2b8d7 + d983816 commit 01e297c

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

Diff for: .travis.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@ matrix:
3333
# https://github.com/hvr/multi-ghc-travis
3434
- env: BUILD=cabal GHCVER=7.10.3 CABALVER=1.22 HAPPYVER=1.19.5 ALEXVER=3.1.7
3535
compiler: ": #GHC 7.10.3"
36-
addons: {apt: {packages: [cabal-install-1.22,ghc-7.10.3,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
36+
addons: {apt: {packages: [cabal-install-1.22,ghc-7.10.3,happy-1.19.5,alex-3.1.7,libblas-dev, liblapack-dev, g++-4.8], sources: [hvr-ghc, ubuntu-toolchain-r-test]}}
3737
- env: BUILD=cabal GHCVER=8.0.1 CABALVER=1.24 HAPPYVER=1.19.5 ALEXVER=3.1.7
3838
compiler: ": #GHC 8.0.1"
39-
addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
39+
addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1,happy-1.19.5,alex-3.1.7,libblas-dev, liblapack-dev, g++-4.8], sources: [hvr-ghc, ubuntu-toolchain-r-test]}}
4040

4141
# Build with the newest GHC and cabal-install. This is an accepted failure,
4242
# see below.
4343
- env: BUILD=cabal GHCVER=head CABALVER=head HAPPYVER=1.19.5 ALEXVER=3.1.7
4444
compiler: ": #GHC HEAD"
45-
addons: {apt: {packages: [cabal-install-head,ghc-head,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
45+
addons: {apt: {packages: [cabal-install-head,ghc-head,happy-1.19.5,alex-3.1.7,libblas-dev, liblapack-dev, g++-4.8], sources: [hvr-ghc, ubuntu-toolchain-r-test]}}
4646

4747
# The Stack builds. We can pass in arbitrary Stack arguments via the ARGS
4848
# variable, such as using --stack-yaml to point to a different file.
4949
- env: BUILD=stack ARGS="--resolver lts-6"
5050
compiler: ": #stack 7.10.3"
51-
addons: {apt: {packages: [libgmp-dev]}}
51+
addons: {apt: {packages: [libgmp-dev, libblas-dev, liblapack-dev, g++-4.8], sources: [ubuntu-toolchain-r-test]}}
5252

5353
- env: BUILD=stack ARGS="--resolver lts-7"
5454
compiler: ": #stack 8.0.1"
55-
addons: {apt: {packages: [libgmp-dev]}}
55+
addons: {apt: {packages: [libgmp-dev, libblas-dev, liblapack-dev, g++-4.8], sources: [ubuntu-toolchain-r-test]}}
5656

5757
# Nightly builds are allowed to fail
5858
- env: BUILD=stack ARGS="--resolver nightly"
5959
compiler: ": #stack nightly"
60-
addons: {apt: {packages: [libgmp-dev]}}
60+
addons: {apt: {packages: [libgmp-dev, libblas-dev, liblapack-dev, g++-4.8], sources: [ubuntu-toolchain-r-test]}}
6161

6262
# Build on macOS in addition to Linux
6363
- env: BUILD=stack ARGS="--resolver lts-7"
@@ -146,4 +146,4 @@ script:
146146
done
147147
;;
148148
esac
149-
set +ex
149+
set +ex

Diff for: data-haskell-examples.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ executable data-haskell-examples
8787
base >= 4.7 && < 5,
8888
protolude,
8989
data-haskell-examples,
90-
vector
90+
vector,
91+
hmatrix
9192
default-extensions:
9293
NoImplicitPrelude,
9394
UnicodeSyntax,

Diff for: examples/examples.hs

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Protolude
22
import Data.Vector as V
3+
import Numeric.LinearAlgebra as H
34

45
main :: IO ()
56
main = do
@@ -10,7 +11,7 @@ main = do
1011
print $ V.null x -- False
1112

1213
-- indexing
13-
print $ x ! 1 -- 1
14+
print $ x V.! 1 -- 1
1415
print $ V.head x -- 0
1516
print $ last x -- 5
1617

@@ -29,3 +30,35 @@ main = do
2930
-- Update
3031
print $ x // [(0, 1), (2, 6)] -- [1, 1, 6, 3, 4, 5]
3132
print $ V.map (+2) x -- [2, 3, 4, 5, 6, 7]
33+
34+
-- hmatrix examples
35+
-- Creating matrices
36+
let x = (2><2) [0..3] :: Matrix Double
37+
38+
-- [ 0.0, 1.0
39+
-- , 2.0, 3.0 ]
40+
let y = fromLists [[0, 1], [2, 3]] :: Matrix Double
41+
42+
-- Random matrices
43+
r <- randn 2 3
44+
-- [ 0.7764496757867578, 1.246311658930589, -0.684233085372455
45+
-- , -2.540045307941425, -0.20975584071908912, -9.039537343065803e-3 ]
46+
47+
-- Matrix multiplication
48+
print $ x H.<> y -- [ 2.0, 3.0
49+
-- , 6.0, 11.0 ]
50+
51+
-- Transpose
52+
print $ tr x -- [ 0.0, 2.0
53+
-- , 1.0, 3.0 ]
54+
55+
-- Matrix slicing
56+
print $ r ?? (H.All, Take 2) -- [ 0.7764496757867578, 1.246311658930589
57+
-- , -2.540045307941425, -0.20975584071908912 ]
58+
59+
-- Mapping over matrices
60+
print $ cmap ((+ 2) . (*2)) x -- [ 2.0, 4.0
61+
-- , 6.0, 8.0 ]
62+
63+
-- Flatten
64+
print $ flatten x -- [0.0, 1.0, 2.0, 3.0]

Diff for: readme.md

+2-33
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,9 @@ libraries
2121

2222
[vector](http://www.datahaskell.org/docs/library/vector.html) is an efficient implementation of Int-indexed arrays (both mutable and immutable), with a powerful loop optimisation framework.
2323

24+
[hmatrix](http://www.datahaskell.org/docs/library/hmatrix.html) is a linear algebra and matrix library, using the Data.Vector.Storable instance in the vector package under the hood.
2425

2526
examples
2627
---
2728

28-
``` {.sourceCode .literate .haskell}
29-
import Protolude
30-
import Data.Vector as V
31-
32-
main :: IO ()
33-
main = do
34-
-- vector examples
35-
let x = V.fromList [0..5]
36-
37-
print $ V.length x -- 6
38-
print $ V.null x -- False
39-
40-
-- indexing
41-
print $ x ! 1 -- 1
42-
print $ V.head x -- 0
43-
print $ last x -- 5
44-
45-
-- Slicing
46-
print $ slice 2 3 x -- [2, 3, 4]
47-
print $ V.splitAt 2 x -- ([0, 1], [2, 3, 4, 5])
48-
49-
-- Prepending and Appending
50-
print $ cons (-1) x -- [-1, 0, 1, 2, 3, 4, 5]
51-
print $ snoc x 6 -- [0, 1, 2, 3, 4, 5, 6]
52-
53-
-- Concatenation
54-
print $ x V.++ x -- [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
55-
print $ V.concat [x, x] -- [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
56-
57-
-- Update
58-
print $ x // [(0, 1), (2, 6)] -- [1, 1, 6, 3, 4, 5]
59-
print $ V.map (+2) x -- [2, 3, 4, 5, 6, 7]
60-
```
29+
See [examples.hs](examples/examples.hs)

0 commit comments

Comments
 (0)