├── requirements.txt ├── Setup.hs ├── .gitignore ├── .gitmodules ├── app └── Main.hs ├── .gitattributes ├── src ├── Lib.hs └── quickstart │ └── hmatrix.hs ├── docs ├── quickstart.linear-algebra.rst ├── index.rst ├── quickstart.rst ├── Makefile ├── quickstart.tricks.rst ├── make.bat ├── quickstart.shape-manipulation.rst ├── quickstart.fancy-indexing.rst ├── comparetabs.py ├── quickstart.basics.rst └── conf.py ├── stack.yaml ├── haskell-numpy-docs.cabal ├── LICENSE └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | Sphinx 2 | sphinx-tabs 3 | -------------------------------------------------------------------------------- /Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .ghci 2 | .stack-work 3 | *.pyc 4 | docs/_build 5 | docs/doctrees 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "numpy"] 2 | path = numpy 3 | url = git@github.com:numpy/numpy.git 4 | -------------------------------------------------------------------------------- /app/Main.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import Lib 4 | 5 | main :: IO () 6 | main = someFunc 7 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | numpy-docs/* linguist-documentation 2 | docs/comparetabs.py linguist-documentation=false 3 | -------------------------------------------------------------------------------- /src/Lib.hs: -------------------------------------------------------------------------------- 1 | module Lib where 2 | 3 | someFunc :: IO () 4 | someFunc = print "The meat is in the docs!" 5 | -------------------------------------------------------------------------------- /docs/quickstart.linear-algebra.rst: -------------------------------------------------------------------------------- 1 | .. _quickstart.linear-algebra: 2 | 3 | Linear Algebra 4 | ============== 5 | 6 | Simple Array Operations 7 | ----------------------- 8 | 9 | .. compare-tabs:: 10 | 11 | python numpy ../numpy/doc/source/user/quickstart.rst 1344-1378 12 | haskell hmatrix ../src/quickstart/hmatrix.hs 588-628 13 | 14 | 15 | -------------------------------------------------------------------------------- /stack.yaml: -------------------------------------------------------------------------------- 1 | flags: 2 | hmatrix: 3 | openblas: true 4 | extra-package-dbs: [] 5 | packages: 6 | - '.' 7 | extra-deps: 8 | - hTensor-0.9.1 9 | - hmatrix-0.18.0.0 10 | resolver: lts-8.5 11 | extra-lib-dirs: 12 | - C:\Users\Yakov\Downloads\OpenBLAS-v0.2.19-Win64-int32\bin 13 | extra-include-dirs: 14 | - C:\Users\Yakov\Downloads\OpenBLAS-v0.2.19-Win64-int32\include 15 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. haskell-numpy-docs documentation master file, created by 2 | sphinx-quickstart on Tue Mar 14 10:23:52 2017. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to haskell-numpy-docs's documentation! 7 | ============================================== 8 | 9 | .. toctree:: 10 | :maxdepth: 3 11 | 12 | quickstart 13 | -------------------------------------------------------------------------------- /docs/quickstart.rst: -------------------------------------------------------------------------------- 1 | .. _quickstart: 2 | 3 | Quickstart 4 | ========== 5 | 6 | Refer to the `numpy documentation `_ 7 | for the associated context regarding the examples. 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | 12 | quickstart.basics 13 | quickstart.shape-manipulation 14 | quickstart.fancy-indexing 15 | quickstart.linear-algebra 16 | quickstart.tricks 17 | 18 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SPHINXPROJ = haskell-numpy-docs 8 | SOURCEDIR = . 9 | BUILDDIR = _build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -------------------------------------------------------------------------------- /docs/quickstart.tricks.rst: -------------------------------------------------------------------------------- 1 | .. _quickstart.tricks: 2 | 3 | Tricks and Tips 4 | =============== 5 | 6 | "Automatic" Reshaping 7 | --------------------- 8 | 9 | .. compare-tabs:: 10 | 11 | python numpy ../numpy/doc/source/user/quickstart.rst 1401-1415 12 | haskell hmatrix ../src/quickstart/hmatrix.hs 631-643 13 | 14 | 15 | Vector Stacking 16 | --------------- 17 | 18 | .. compare-tabs:: 19 | 20 | python numpy ../numpy/doc/source/user/quickstart.rst 1427-1431 21 | haskell hmatrix ../src/quickstart/hmatrix.hs 646-659 22 | 23 | 24 | Histograms 25 | ---------- 26 | 27 | .. compare-tabs:: 28 | 29 | python numpy ../numpy/doc/source/user/quickstart.rst 1452-1463 30 | haskell hmatrix ../src/quickstart/hmatrix.hs 662-683 31 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=. 11 | set BUILDDIR=_build 12 | set SPHINXPROJ=haskell-numpy-docs 13 | 14 | if "%1" == "" goto help 15 | 16 | %SPHINXBUILD% >NUL 2>NUL 17 | if errorlevel 9009 ( 18 | echo. 19 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 20 | echo.installed, then set the SPHINXBUILD environment variable to point 21 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 22 | echo.may add the Sphinx directory to PATH. 23 | echo. 24 | echo.If you don't have Sphinx installed, grab it from 25 | echo.http://sphinx-doc.org/ 26 | exit /b 1 27 | ) 28 | 29 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 30 | goto end 31 | 32 | :help 33 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 34 | 35 | :end 36 | popd 37 | -------------------------------------------------------------------------------- /haskell-numpy-docs.cabal: -------------------------------------------------------------------------------- 1 | name: haskell-numpy-docs 2 | version: 0.1.0.0 3 | -- synopsis: 4 | -- description: 5 | homepage: https://github.com/pechersky/haskell-numpy-docs#readme 6 | license: BSD3 7 | license-file: LICENSE 8 | author: Yakov Pechersky 9 | maintainer: yakov@pechersky.us 10 | copyright: 2017 Yakov Pechersky 11 | category: Web 12 | build-type: Simple 13 | cabal-version: >=1.10 14 | 15 | library 16 | hs-source-dirs: src 17 | exposed-modules: Lib 18 | build-depends: base >= 4.7 && < 5 19 | , doctest 20 | , hmatrix 21 | , hTensor 22 | , vector 23 | default-language: Haskell2010 24 | 25 | executable haskell-numpy-docs-exe 26 | hs-source-dirs: app 27 | main-is: Main.hs 28 | ghc-options: -threaded -rtsopts -with-rtsopts=-N 29 | build-depends: base 30 | , haskell-numpy-docs 31 | default-language: Haskell2010 32 | 33 | source-repository head 34 | type: git 35 | location: https://github.com/pechersky/haskell-numpy-docs 36 | -------------------------------------------------------------------------------- /docs/quickstart.shape-manipulation.rst: -------------------------------------------------------------------------------- 1 | .. _quickstart.shape-manipulation: 2 | 3 | Shape Manipulation 4 | ================== 5 | 6 | Changing the shape of any array 7 | ------------------------------- 8 | 9 | .. compare-tabs:: 10 | 11 | python numpy ../numpy/doc/source/user/quickstart.rst 621-627,633-650 12 | haskell hmatrix ../src/quickstart/hmatrix.hs 325-327,331-357 13 | 14 | .. compare-tabs:: 15 | 16 | python numpy ../numpy/doc/source/user/quickstart.rst 668-675,680-683 17 | haskell hmatrix ../src/quickstart/hmatrix.hs 361-375 18 | 19 | 20 | Stacking together different arrays 21 | ---------------------------------- 22 | 23 | .. compare-tabs:: 24 | 25 | python numpy ../numpy/doc/source/user/quickstart.rst 697-712 26 | haskell hmatrix ../src/quickstart/hmatrix.hs 378-380,385-388,381,390-403 27 | 28 | .. compare-tabs:: 29 | 30 | python numpy ../numpy/doc/source/user/quickstart.rst 718-737,755-756 31 | haskell hmatrix ../src/quickstart/hmatrix.hs 414-431 32 | 33 | 34 | Splitting one array into several smaller ones 35 | --------------------------------------------- 36 | 37 | .. compare-tabs:: 38 | 39 | python numpy ../numpy/doc/source/user/quickstart.rst 783-796 40 | haskell hmatrix ../src/quickstart/hmatrix.hs 434-436,440-459 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/quickstart.fancy-indexing.rst: -------------------------------------------------------------------------------- 1 | .. _quickstart.fancy-indexing: 2 | 3 | Fancy indexing and index tricks 4 | =============================== 5 | 6 | Indexing with Arrays of Indices 7 | ------------------------------- 8 | 9 | .. compare-tabs:: 10 | 11 | python numpy ../numpy/doc/source/user/quickstart.rst 1017-1025 12 | haskell hmatrix ../src/quickstart/hmatrix.hs 462-470 13 | 14 | .. compare-tabs:: 15 | 16 | python numpy ../numpy/doc/source/user/quickstart.rst 1034-1049 17 | haskell hmatrix ../src/quickstart/hmatrix.hs 473-484 18 | 19 | .. compare-tabs:: 20 | 21 | python numpy ../numpy/doc/source/user/quickstart.rst 1056-1080 22 | haskell hmatrix ../src/quickstart/hmatrix.hs 487-505 23 | 24 | .. compare-tabs:: 25 | 26 | python numpy ../numpy/doc/source/user/quickstart.rst 1111-1136 27 | haskell hmatrix ../src/quickstart/hmatrix.hs 508-529 28 | 29 | .. compare-tabs:: 30 | 31 | python numpy ../numpy/doc/source/user/quickstart.rst 1140-1145,1150-1153,1158-1161 32 | haskell hmatrix ../src/quickstart/hmatrix.hs 532-543 33 | 34 | 35 | Indexing with Boolean Arrays 36 | ---------------------------- 37 | 38 | .. compare-tabs:: 39 | 40 | python numpy ../numpy/doc/source/user/quickstart.rst 1178-1185,1189-1193 41 | haskell hmatrix ../src/quickstart/hmatrix.hs 546-557 42 | 43 | .. compare-tabs:: 44 | 45 | python numpy ../numpy/doc/source/user/quickstart.rst 1226-1244 46 | haskell hmatrix ../src/quickstart/hmatrix.hs 560-585 47 | 48 | 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Yakov Pechersky (c) 2017 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following 13 | disclaimer in the documentation and/or other materials provided 14 | with the distribution. 15 | 16 | * Neither the name of Yakov Pechersky nor the names of other 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | -------------------------------------------------------------------------------- /docs/comparetabs.py: -------------------------------------------------------------------------------- 1 | """ Code comparison tabs for Sphinx """ 2 | 3 | import os 4 | import json 5 | from docutils.parsers.rst import Directive 6 | from docutils import nodes 7 | 8 | class CompareTabsDirective(Directive): 9 | """Directive to compare code using tabs.""" 10 | 11 | has_content = True 12 | 13 | DEDENT = { 14 | 'python': 4, 15 | 'haskell': 3, 16 | } 17 | 18 | def run(self): 19 | """Parse a compare-tabs directive.""" 20 | 21 | self.assert_has_content() 22 | 23 | group_name = self.content[0] 24 | 25 | tabs = [] 26 | for idx, line in enumerate(self.content.data): 27 | tabs.append(line.split()) 28 | 29 | new_content = [ 30 | '.. tabs::' 31 | ] 32 | for tabsplit in tabs: 33 | language = tabsplit[0] 34 | name = tabsplit[1] 35 | filepath = tabsplit[2] 36 | lines_raw = tabsplit[3:] 37 | lines = str.join(',', str.join(' ', lines_raw).replace(',',' ').split()) 38 | new_content.extend([ 39 | ' .. tab:: {}'.format(name), 40 | ' ', 41 | ' .. literalinclude:: {}'.format(filepath), 42 | ' :dedent: {}'.format(self.DEDENT[language]), 43 | ' :language: {}'.format(language), 44 | ' :lines: {}'.format(lines), 45 | ' ', 46 | ]) 47 | 48 | for idx, line in enumerate(new_content): 49 | self.content.data.insert(idx, line) 50 | self.content.items.insert(idx, (None, idx)) 51 | 52 | node = nodes.container() 53 | self.state.nested_parse(self.content[:-1*len(tabs)], self.content_offset, node) 54 | return node.children 55 | 56 | 57 | def setup(app): 58 | app.add_directive('compare-tabs', CompareTabsDirective) -------------------------------------------------------------------------------- /docs/quickstart.basics.rst: -------------------------------------------------------------------------------- 1 | .. _quickstart.basics: 2 | 3 | The Basics 4 | ========== 5 | 6 | An example 7 | ---------- 8 | 9 | .. compare-tabs:: 10 | 11 | python numpy ../numpy/doc/source/user/quickstart.rst 78-100 12 | haskell hmatrix ../src/quickstart/hmatrix.hs 6-7,10-17,19,22-34 13 | 14 | 15 | Array creation 16 | -------------- 17 | 18 | .. compare-tabs:: 19 | 20 | python numpy ../numpy/doc/source/user/quickstart.rst 114-122 21 | haskell hmatrix ../src/quickstart/hmatrix.hs 6,37-44,46 22 | 23 | .. compare-tabs:: 24 | 25 | python numpy ../numpy/doc/source/user/quickstart.rst 139-142 26 | haskell hmatrix ../src/quickstart/hmatrix.hs 48-52 27 | 28 | .. compare-tabs:: 29 | 30 | python numpy ../numpy/doc/source/user/quickstart.rst 148-151 31 | haskell hmatrix ../src/quickstart/hmatrix.hs 55-59 32 | 33 | .. compare-tabs:: 34 | 35 | python numpy ../numpy/doc/source/user/quickstart.rst 166-179 36 | haskell hmatrix ../src/quickstart/hmatrix.hs 62-66,68,70-74,76-77 37 | 38 | .. compare-tabs:: 39 | 40 | python numpy ../numpy/doc/source/user/quickstart.rst 186-189 41 | haskell hmatrix ../src/quickstart/hmatrix.hs 80-83 42 | 43 | .. compare-tabs:: 44 | 45 | python numpy ../numpy/doc/source/user/quickstart.rst 197-201 46 | haskell hmatrix ../src/quickstart/hmatrix.hs 86-89 47 | 48 | 49 | Printing arrays 50 | --------------- 51 | 52 | .. compare-tabs:: 53 | 54 | python numpy ../numpy/doc/source/user/quickstart.rst 234-252 55 | haskell hmatrix ../src/quickstart/hmatrix.hs 92-104 56 | 57 | .. compare-tabs:: 58 | 59 | python numpy ../numpy/doc/source/user/quickstart.rst 260-270 60 | haskell hmatrix ../src/quickstart/hmatrix.hs 106-116 61 | 62 | 63 | Basic Operations 64 | ---------------- 65 | 66 | .. compare-tabs:: 67 | 68 | python numpy ../numpy/doc/source/user/quickstart.rst 288-300 69 | haskell hmatrix ../src/quickstart/hmatrix.hs 119-132 70 | 71 | .. compare-tabs:: 72 | 73 | python numpy ../numpy/doc/source/user/quickstart.rst 306-318 74 | haskell hmatrix ../src/quickstart/hmatrix.hs 135-149 75 | 76 | .. compare-tabs:: 77 | 78 | python numpy ../numpy/doc/source/user/quickstart.rst 325-338 79 | haskell hmatrix ../src/quickstart/hmatrix.hs 152-162,165-167 80 | 81 | .. compare-tabs:: 82 | 83 | python numpy ../numpy/doc/source/user/quickstart.rst 346-360 84 | haskell hmatrix ../src/quickstart/hmatrix.hs 170-184,186-187 85 | 86 | .. compare-tabs:: 87 | 88 | python numpy ../numpy/doc/source/user/quickstart.rst 367-376 89 | haskell hmatrix ../src/quickstart/hmatrix.hs 189,193-202 90 | 91 | .. compare-tabs:: 92 | 93 | python numpy ../numpy/doc/source/user/quickstart.rst 383-398 94 | haskell hmatrix ../src/quickstart/hmatrix.hs 205-219 95 | 96 | 97 | Universal Functions 98 | ------------------- 99 | 100 | .. compare-tabs:: 101 | 102 | python numpy ../numpy/doc/source/user/quickstart.rst 411-420 103 | haskell hmatrix ../src/quickstart/hmatrix.hs 222-231 104 | 105 | 106 | Indexing, Slicing, and Iterating 107 | -------------------------------- 108 | 109 | .. compare-tabs:: 110 | 111 | python numpy ../numpy/doc/source/user/quickstart.rst 477-501 112 | haskell hmatrix ../src/quickstart/hmatrix.hs 234-240,242-246,249-266 113 | 114 | .. compare-tabs:: 115 | 116 | python numpy ../numpy/doc/source/user/quickstart.rst 506-524,531-532,549-560 117 | haskell hmatrix ../src/quickstart/hmatrix.hs 269-289,291 118 | 119 | .. compare-tabs:: 120 | 121 | python numpy ../numpy/doc/source/user/quickstart.rst 565-572,579-601 122 | haskell hmatrix ../src/quickstart/hmatrix.hs 269-289,291 123 | 124 | 125 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # haskell-numpy-docs 2 | 3 | This repo provides the docs with which one can compare how use Haskell (and 4 | associated libraries) in the same way one would use the Python numerical library 5 | [numpy][numpy]. A contribution of the [DataHaskell][datahaskell] project. 6 | 7 | [numpy]: http://www.numpy.org 8 | [datahaskell]: http://www.datahaskell.org/ 9 | 10 | ## The actual docs 11 | 12 | https://pechersky.github.io/haskell-numpy-docs/ 13 | 14 | ## Structure 15 | 16 | The `numpy` docs are provided as a submodule, pointing to the [numpy repo][numpy git]. 17 | 18 | The examples are in the `src` directory, grouped by the docs page that they are 19 | associated with. Each docs page can have a different library provide examples. 20 | Each library should get its own `hs` file. For example, there is a 21 | [src/quickstart/hmatrix.hs](src/quickstart/hmatrix.hs) for the [HMatrix][hmatrix git] version of the numpy 22 | quickstart docs. 23 | 24 | The actual docs pages are built using [Sphinx][sphinx docs], a Python 25 | documentation generator. It uses an `rst` format. To write docs based on the 26 | examples you wrote, use the [sphinx-tabs][sphinx tabs git] and the specially 27 | written `comparetabs` extensions. The docs `rst`s only require pointing to the 28 | parent example documents, the lines that each example should pull in, and the 29 | language of the example. For a good starting point, check out 30 | [docs/quickstart.rst](docs/quickstart.rst). 31 | 32 | [numpy git]: https://github.com/numpy/numpy/ 33 | [hmatrix git]: http://github.com/albertoruiz/hmatrix 34 | [sphinx docs]: http://www.sphinx-doc.org/en/stable/ 35 | [sphinx tabs git]: https://github.com/djungelorm/sphinx-tabs 36 | 37 | ## Developing 38 | 39 | Select the library you want to test. Add it to the [haskell-numpy-docs.cabal](haskell-numpy-docs.cabal) 40 | file. Run `stack solver` to prepare the dependencies. Run `stack build` to 41 | provide install the libraries. 42 | 43 | There are some special things you might need to do to develop on Windows 44 | machines. Specifically, you need to provide a BLAS implementation for HMatrix to 45 | work. Check out `stack.yaml` for an example. 46 | 47 | ## Testing 48 | 49 | You can write the Haskell examples to be valid Haskell. This is verifiable using 50 | the [doctest][doctest git] library. To check an examples file, run a command 51 | like `stack exec doctest src/quickstart/hmatrix.hs`, for example. 52 | 53 | [doctest git]: https://github.com/sol/doctest 54 | 55 | ## Building documentation 56 | 57 | Make sure the proper Python dependencies are installed using `pip install -r 58 | requirements.txt`. To build the documentation, `cd` to the `docs` directory. Run 59 | `make html`. 60 | 61 | For directions about how to push the docs, take a look at [how to push 62 | docs](#pushing-docs). 63 | 64 | ## Contributing 65 | 66 | File a PR with a new library, or new examples, or more idiomatic Haskell code. 67 | File an issue for more in-depth discussion. Visit the [DataHaskell][datahaskell gitter] 68 | channel and talk! 69 | 70 | [datahaskell gitter]: https://gitter.im/dataHaskell/Lobby 71 | 72 | ## Advanced git stuff 73 | 74 | ### Pushing docs 75 | 76 | In a properly set up repo, following a `make html`, run: 77 | ``` 78 | cd _build/html 79 | git commit -a -m 'new examples here' 80 | git push origin gh-pages 81 | ``` 82 | 83 | This is based on a way to provide sphinx documentations to GitHub pages, given 84 | at https://gist.github.com/brantfaircloth/791759. 85 | 86 | The simple explanation of how to set this up for you is that in the `_build/html` 87 | directory, you need to 88 | ``` 89 | cd docs 90 | git clone git@github.com:pechersky/haskell-numpy-docs.git _build/html 91 | cd _build/html 92 | git checkout master 93 | rm .git/index 94 | git clean -fdx 95 | git checkout gh-pages 96 | ``` 97 | This is because the first part of the gist was already done. Be careful to not 98 | push the docs to the `master` branch! 99 | 100 | --- 101 | 102 | Yakov Pechersky, 2017 103 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # haskell-numpy-docs documentation build configuration file, created by 4 | # sphinx-quickstart on Tue Mar 14 10:23:52 2017. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | # If extensions (or modules to document with autodoc) are in another directory, 16 | # add these directories to sys.path here. If the directory is relative to the 17 | # documentation root, use os.path.abspath to make it absolute, like shown here. 18 | # 19 | import os 20 | import sys 21 | sys.path.insert(0, os.path.abspath('.')) 22 | 23 | 24 | # -- General configuration ------------------------------------------------ 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | # 28 | # needs_sphinx = '1.0' 29 | 30 | # Add any Sphinx extension module names here, as strings. They can be 31 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 32 | # ones. 33 | extensions = ['sphinx_tabs.tabs', 'comparetabs'] 34 | 35 | # Add any paths that contain templates here, relative to this directory. 36 | templates_path = ['_templates'] 37 | 38 | # The suffix(es) of source filenames. 39 | # You can specify multiple suffix as a list of string: 40 | # 41 | # source_suffix = ['.rst', '.md'] 42 | source_suffix = '.rst' 43 | 44 | # The master toctree document. 45 | master_doc = 'index' 46 | 47 | # General information about the project. 48 | project = u'haskell-numpy-docs' 49 | copyright = u'2017, Yakov Pechersky' 50 | author = u'Yakov Pechersky' 51 | 52 | # The version info for the project you're documenting, acts as replacement for 53 | # |version| and |release|, also used in various other places throughout the 54 | # built documents. 55 | # 56 | # The short X.Y version. 57 | version = u'' 58 | # The full version, including alpha/beta/rc tags. 59 | release = u'' 60 | 61 | # The language for content autogenerated by Sphinx. Refer to documentation 62 | # for a list of supported languages. 63 | # 64 | # This is also used if you do content translation via gettext catalogs. 65 | # Usually you set "language" from the command line for these cases. 66 | language = None 67 | 68 | # List of patterns, relative to source directory, that match files and 69 | # directories to ignore when looking for source files. 70 | # This patterns also effect to html_static_path and html_extra_path 71 | exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] 72 | 73 | # The name of the Pygments (syntax highlighting) style to use. 74 | pygments_style = 'sphinx' 75 | 76 | # If true, `todo` and `todoList` produce output, else they produce nothing. 77 | todo_include_todos = False 78 | 79 | 80 | # -- Options for HTML output ---------------------------------------------- 81 | 82 | # The theme to use for HTML and HTML Help pages. See the documentation for 83 | # a list of builtin themes. 84 | # 85 | html_theme = 'alabaster' 86 | 87 | # Theme options are theme-specific and customize the look and feel of a theme 88 | # further. For a list of options available for each theme, see the 89 | # documentation. 90 | # 91 | html_theme_options = { 92 | 'fixed_sidebar': True, 93 | } 94 | html_sidebars = { 95 | '**': [ 96 | 'about.html', 97 | 'navigation.html', 98 | 'relations.html', 99 | 'searchbox.html', 100 | 'donate.html', 101 | ] 102 | } 103 | 104 | # Add any paths that contain custom static files (such as style sheets) here, 105 | # relative to this directory. They are copied after the builtin static files, 106 | # so a file named "default.css" will overwrite the builtin "default.css". 107 | html_static_path = ['_static'] 108 | 109 | 110 | # -- Options for HTMLHelp output ------------------------------------------ 111 | 112 | # Output file base name for HTML help builder. 113 | htmlhelp_basename = 'haskell-numpy-docsdoc' 114 | 115 | 116 | # -- Options for LaTeX output --------------------------------------------- 117 | 118 | latex_elements = { 119 | # The paper size ('letterpaper' or 'a4paper'). 120 | # 121 | # 'papersize': 'letterpaper', 122 | 123 | # The font size ('10pt', '11pt' or '12pt'). 124 | # 125 | # 'pointsize': '10pt', 126 | 127 | # Additional stuff for the LaTeX preamble. 128 | # 129 | # 'preamble': '', 130 | 131 | # Latex figure (float) alignment 132 | # 133 | # 'figure_align': 'htbp', 134 | } 135 | 136 | # Grouping the document tree into LaTeX files. List of tuples 137 | # (source start file, target name, title, 138 | # author, documentclass [howto, manual, or own class]). 139 | latex_documents = [ 140 | (master_doc, 'haskell-numpy-docs.tex', u'haskell-numpy-docs Documentation', 141 | u'Yakov Pechersky', 'manual'), 142 | ] 143 | 144 | 145 | # -- Options for manual page output --------------------------------------- 146 | 147 | # One entry per manual page. List of tuples 148 | # (source start file, name, description, authors, manual section). 149 | man_pages = [ 150 | (master_doc, 'haskell-numpy-docs', u'haskell-numpy-docs Documentation', 151 | [author], 1) 152 | ] 153 | 154 | 155 | # -- Options for Texinfo output ------------------------------------------- 156 | 157 | # Grouping the document tree into Texinfo files. List of tuples 158 | # (source start file, target name, title, author, 159 | # dir menu entry, description, category) 160 | texinfo_documents = [ 161 | (master_doc, 'haskell-numpy-docs', u'haskell-numpy-docs Documentation', 162 | author, 'haskell-numpy-docs', 'One line description of project.', 163 | 'Miscellaneous'), 164 | ] 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /src/quickstart/hmatrix.hs: -------------------------------------------------------------------------------- 1 | module Hmatrix where 2 | 3 | import Numeric.LinearAlgebra 4 | 5 | -- $setup 6 | -- >>> import Numeric.LinearAlgebra 7 | -- >>> import Foreign.Storable (sizeOf) 8 | 9 | -- | 78-100 10 | -- >>> let a = (3><5) [0::I ..] -- or ((3><5) . toList . range) 15 11 | -- >>> a -- for vectors that are just indices, using @range 15@ is fine 12 | -- (3><5) 13 | -- [ 0, 1, 2, 3, 4 14 | -- , 5, 6, 7, 8, 9 15 | -- , 10, 11, 12, 13, 14 ] 16 | -- >>> size a 17 | -- (3,5) 18 | 19 | -- -- there is no @ndim@ for hmatrix, since there are only scalar, vectors, and (2D) matrices 20 | -- | 78-100 21 | -- >>> let a = ((3><5) . toList . range) 15 22 | -- >>> :t (a ! 0 ! 0) 23 | -- (a ! 0 ! 0) :: I 24 | -- >>> sizeOf (a ! 0 ! 0) 25 | -- 4 26 | -- >>> size (flatten a) 27 | -- 15 28 | -- >>> :t a 29 | -- a :: Matrix I 30 | -- >>> let b = fromList [6,7,8] 31 | -- >>> b 32 | -- [6.0,7.0,8.0] 33 | -- >>> :t b 34 | -- b :: (Foreign.Storable.Storable a, Num a) => Vector a 35 | 36 | -- | 114-122 37 | -- >>> let a = fromList [2,3,4::Z] 38 | -- >>> a 39 | -- [2,3,4] 40 | -- >>> :t a 41 | -- a :: Vector Z 42 | -- >>> let b = fromList [1.2, 3.5, 5.1::R] 43 | -- >>> :t b 44 | -- b :: Vector R 45 | 46 | -- -- If you do not cast the numerical literal, you don't get a fully-resolved-type vector 47 | -- | 139-142 48 | -- >>> let b = fromLists [[1.5,2,3], [4,5,6::R]] 49 | -- >>> b 50 | -- (2><3) 51 | -- [ 1.5, 2.0, 3.0 52 | -- , 4.0, 5.0, 6.0 ] 53 | 54 | -- | 148-151 55 | -- >>> let c = (fromLists [ [1,2], [3,4] ]) :: Matrix C 56 | -- >>> c 57 | -- (2><2) 58 | -- [ 1.0 :+ 0.0, 2.0 :+ 0.0 59 | -- , 3.0 :+ 0.0, 4.0 :+ 0.0 ] 60 | 61 | -- | 166-179 62 | -- >>> konst (0::R) (3::Int,4::Int) 63 | -- (3><4) 64 | -- [ 0.0, 0.0, 0.0, 0.0 65 | -- , 0.0, 0.0, 0.0, 0.0 66 | -- , 0.0, 0.0, 0.0, 0.0 ] 67 | 68 | -- -- or 69 | -- | 166-179 70 | -- >>> ((3><4) . repeat) 0 71 | -- (3><4) 72 | -- [ 0.0, 0.0, 0.0, 0.0 73 | -- , 0.0, 0.0, 0.0, 0.0 74 | -- , 0.0, 0.0, 0.0, 0.0 ] 75 | 76 | -- -- there are no 3D tensors in hmatrix 77 | -- -- there are no "empty" matrices in hmatrix 78 | 79 | -- | 186-189 80 | -- >>> vector (init [10, 15.. 30]) 81 | -- [10.0,15.0,20.0,25.0] 82 | -- >>> vector (init [0, 0.3.. 2]) 83 | -- [0.0,0.3,0.6,0.8999999999999999,1.1999999999999997,1.4999999999999996,1.7999999999999994] 84 | 85 | -- | 197-201 86 | -- >>> linspace 9 (0, 2::Double) 87 | -- [0.0,0.25,0.5,0.75,1.0,1.25,1.5,1.75,2.0] 88 | -- >>> let x = linspace 100 (0, 2*pi) :: Vector Double 89 | -- >>> let f = cmap sin x 90 | 91 | -- | 234-252 92 | -- >>> let a = 6 |> [0..] 93 | -- >>> disp 2 (asRow a) 94 | -- 1x6 95 | -- 0 1 2 3 4 5 96 | -- >>> let b = (4><3) [0..] 97 | -- >>> disp 2 b 98 | -- 4x3 99 | -- 0 1 2 100 | -- 3 4 5 101 | -- 6 7 8 102 | -- 9 10 11 103 | 104 | -- -- there are no 3D matrices in hmatrix 105 | -- | 260-270 106 | -- >>> dispShort 6 6 0 (asRow (10000 |> [0..])) 107 | -- 1x10000 108 | -- 0 1 2 .. 9998 9999 109 | -- >>> dispShort 6 6 0 ((100><100) [0..]) 110 | -- 100x100 111 | -- 0 1 2 .. 98 99 112 | -- 100 101 102 .. 198 199 113 | -- 200 201 202 .. 298 299 114 | -- :: 115 | -- 9800 9801 9802 .. 9898 9899 116 | -- 9900 9901 9902 .. 9998 9999 117 | 118 | -- | 288-300 119 | -- >>> let a = fromList [20,30,40,50::R] 120 | -- >>> let b = 4 |> [0::R ..] 121 | -- >>> b 122 | -- [0.0,1.0,2.0,3.0] 123 | -- >>> let c = a-b 124 | -- >>> c 125 | -- [20.0,29.0,38.0,47.0] 126 | -- >>> cmap (^ 2) b 127 | -- [0.0,1.0,4.0,9.0] 128 | -- >>> disp 8 (asRow (cmap ((*10) . sin) a)) 129 | -- 1x4 130 | -- 9.12945251 -9.88031624 7.45113160 -2.62374854 131 | -- >>> (fromList . (fmap (<35)) . toList) a -- hmatrix does not have an Element Bool definition 132 | -- [True,True,False,False] 133 | 134 | -- | 306-318 135 | -- >>> let a = fromLists [[1,1],[0,1]] :: Matrix R 136 | -- >>> let b = fromLists [[2,0],[3,4]] :: Matrix R 137 | -- >>> a * b -- capitals are reserved for data constructors 138 | -- (2><2) 139 | -- [ 2.0, 0.0 140 | -- , 0.0, 4.0 ] 141 | -- >>> a <> b 142 | -- (2><2) 143 | -- [ 5.0, 4.0 144 | -- , 3.0, 4.0 ] 145 | -- >>> import Numeric.LinearAlgebra.HMatrix (mul) 146 | -- >>> mul a b -- dot is reserved for vector <.> vector 147 | -- (2><2) 148 | -- [ 5.0, 4.0 149 | -- , 3.0, 4.0 ] 150 | 151 | -- | 325-338 152 | -- >>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), randomVector, rand) 153 | -- >>> let a' = ((2><3) . repeat) 1 -- inplace mutation not useful here 154 | -- >>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c)) 155 | -- >>> let b' = rand' 1 2 3 -- or unseeded: rand 2 3; rand' not actually deterministic (?) 156 | -- >>> let a = cmap (*3) a' :: Matrix Z -- haskell cannot reassign variables using itself 157 | -- >>> a 158 | -- (2><3) 159 | -- [ 3, 3, 3 160 | -- , 3, 3, 3 ] 161 | -- >>> let b = (+ (fromZ a)) b' -- need to cast away from integer (Z) 162 | -- >>> disp 7 b -- unseeded would need fmap due to being IO (...) 163 | -- ... 164 | 165 | -- 2x3 166 | -- 3.4583777 3.1595528 3.4802844 167 | -- 3.6986673 3.8688027 3.5846251 168 | 169 | -- | 346-360 170 | -- >>> :set -XFlexibleContexts 171 | -- >>> let a = konst 1 3 -- type not resolved here 172 | -- >>> let b = linspace 3 (0, pi) -- set FlexibleContexts to get type inference 173 | -- >>> :t b 174 | -- b :: (Container Vector e, Floating e) => Vector e 175 | -- >>> let c = a + b :: Vector R -- we can force resolving type of only c 176 | -- >>> disp 8 (asRow (c :: Vector R)) -- a and b will remain unresolved 177 | -- 1x3 178 | -- 1.00000000 2.57079633 4.14159265 179 | -- >>> :t c 180 | -- c :: Vector R 181 | -- >>> let d = cmap (exp . (*iC)) (complex c) 182 | -- >>> putStrLn (dispcf 8 (asRow d)) 183 | -- 1x3 184 | -- 0.54030231+0.84147098i -0.84147098+0.54030231i -0.54030231-0.84147098i 185 | -- 186 | -- >>> :t d 187 | -- d :: Vector C 188 | 189 | -- >>> let a = rand' 1 2 3 190 | -- | 367-376 191 | -- >>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c)) 192 | -- >>> let a = fromLists [[0.18626021, 0.34556073, 0.39676747], [0.53881673, 0.41919451, 0.6852195]] :: Matrix R 193 | -- >>> a 194 | -- (2><3) 195 | -- [ 0.18626021, 0.34556073, 0.39676747 196 | -- , 0.53881673, 0.41919451, 0.6852195 ] 197 | -- >>> sumElements a :: R 198 | -- 2.57181915 199 | -- >>> minElement a :: R 200 | -- 0.18626021 201 | -- >>> maxElement a :: R 202 | -- 0.6852195 203 | 204 | -- | 383-398 205 | -- >>> let b = (3><4) [0::I ..] 206 | -- >>> b 207 | -- (3><4) 208 | -- [ 0, 1, 2, 3 209 | -- , 4, 5, 6, 7 210 | -- , 8, 9, 10, 11 ] 211 | -- >>> (fromList . fmap sumElements . toColumns) b 212 | -- [12,15,18,21] 213 | -- >>> (fromList . fmap minElement . toRows) b 214 | -- [0,4,8] 215 | -- >>> (fromLists . (fmap (scanl1 (+))) . toLists) b 216 | -- (3><4) 217 | -- [ 0, 1, 3, 6 218 | -- , 4, 9, 15, 22 219 | -- , 8, 17, 27, 38 ] 220 | 221 | -- | 411-420 222 | -- >>> let b = 3 |> [0::R ..] -- if we used @I@, we'd need @fromInt b@ below 223 | -- >>> b 224 | -- [0.0,1.0,2.0] 225 | -- >>> cmap exp b 226 | -- [1.0,2.718281828459045,7.38905609893065] 227 | -- >>> cmap sqrt b 228 | -- [0.0,1.0,1.4142135623730951] 229 | -- >>> let c = vector [2, -1, 4] 230 | -- >>> b + c 231 | -- [2.0,0.0,6.0] 232 | 233 | -- | 477-501 234 | -- >>> let a = build 10 (^3) :: Vector I -- helper function with implicit counter 235 | -- >>> a 236 | -- [0,1,8,27,64,125,216,343,512,729] 237 | -- >>> a ! 2 -- also can do @a `atIndex` 2@ 238 | -- 8 239 | -- >>> subVector 2 (5-2) a 240 | -- [8,27,64] 241 | 242 | -- -- that uses @Data.Vector.slice@ underneath, without copying 243 | -- -- we could convert to a matrix for nice indexing 244 | -- -- using @(flatten . ((flip (??)) (All, Range 2 1 4)) . asRow) a@ 245 | -- -- we could also do @(flatten . remap (asColumn (scalar 0)) (asRow (fromList [2..4])) . asRow) b@ 246 | -- -- or even @fromList ((sequence . fmap (flip (!))) [2..4] b)@ 247 | -- | 477-501 248 | -- >>> let a = build 10 (^3) :: Vector I -- helper function with implicit counter 249 | -- >>> let a' = accum a const (zip [0,2..4] (repeat (negate 1000))) 250 | -- >>> a' 251 | -- [-1000,1,-1000,27,-1000,125,216,343,512,729] 252 | -- >>> import qualified Data.Vector.Storable as V 253 | -- >>> V.reverse a' -- unlike in Python, this is not a view, and is O(n) 254 | -- [729,512,343,216,125,-1000,27,-1000,1,-1000] 255 | -- >>> import Numeric.LinearAlgebra.Devel (mapVectorM_) 256 | -- >>> mapVectorM_ print (cmap (**(1/3.0)) (fromInt a') :: Vector R) 257 | -- NaN 258 | -- 1.0 259 | -- NaN 260 | -- 3.0 261 | -- NaN 262 | -- 4.999999999999999 263 | -- 6.0 264 | -- 6.999999999999998 265 | -- 8.0 266 | -- 8.999999999999998 267 | 268 | -- | 506-524,531-532 269 | -- >>> let f x y = 10*x+y 270 | -- >>> let b = build (5,4) f :: Matrix Z 271 | -- >>> b 272 | -- (5><4) 273 | -- [ 0, 1, 2, 3 274 | -- , 10, 11, 12, 13 275 | -- , 20, 21, 22, 23 276 | -- , 30, 31, 32, 33 277 | -- , 40, 41, 42, 43 ] 278 | -- >>> b ! 2 ! 3 279 | -- 23 280 | -- >>> flatten (subMatrix (0,1) (5,1) b) -- @subMatrix@ always return a Matrix 281 | -- [1,11,21,31,41] 282 | -- >>> flatten (b ?? (All, Pos (scalar 1))) 283 | -- [1,11,21,31,41] 284 | -- >>> b ?? (Range 1 1 2, All) 285 | -- (2><4) 286 | -- [ 10, 11, 12, 13 287 | -- , 20, 21, 22, 23 ] 288 | -- >>> flatten (b ? [rows b - 1]) 289 | -- [40,41,42,43] 290 | 291 | -- -- hmatrix does not have 3D matrices; there are no ellipsis equivalents 292 | -- | 565-572,579-601 293 | -- >>> let f x y = 10*x+y 294 | -- >>> let b = build (5,4) f :: Matrix Z 295 | -- >>> mapM_ print (toRows b) 296 | -- [0,1,2,3] 297 | -- [10,11,12,13] 298 | -- [20,21,22,23] 299 | -- [30,31,32,33] 300 | -- [40,41,42,43] 301 | -- >>> import Numeric.LinearAlgebra.Devel (mapMatrixWithIndexM_) 302 | -- >>> mapMatrixWithIndexM_ (const print) b 303 | -- 0 304 | -- 1 305 | -- 2 306 | -- 3 307 | -- 10 308 | -- 11 309 | -- 12 310 | -- 13 311 | -- 20 312 | -- 21 313 | -- 22 314 | -- 23 315 | -- 30 316 | -- 31 317 | -- 32 318 | -- 33 319 | -- 40 320 | -- 41 321 | -- 42 322 | -- 43 323 | 324 | -- | 621-627,633-650 325 | -- >>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), randomVector, rand) 326 | -- >>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c)) 327 | -- >>> let a = cmap (floor . (*10)) (rand' 1 3 4) :: Matrix Z 328 | 329 | -- | 621-627,633-650 330 | -- >>> let a = fromLists [[2,8,0,6],[4,5,1,1],[8,9,3,6]] :: Matrix Z 331 | -- >>> a 332 | -- (3><4) 333 | -- [ 2, 8, 0, 6 334 | -- , 4, 5, 1, 1 335 | -- , 8, 9, 3, 6 ] 336 | -- >>> size a 337 | -- (3,4) 338 | -- >>> flatten a 339 | -- [2,8,0,6,4,5,1,1,8,9,3,6] 340 | -- >>> ((reshape 2) . flatten) a 341 | -- (6><2) 342 | -- [ 2, 8 343 | -- , 0, 6 344 | -- , 4, 5 345 | -- , 1, 1 346 | -- , 8, 9 347 | -- , 3, 6 ] 348 | -- >>> tr a 349 | -- (4><3) 350 | -- [ 2, 4, 8 351 | -- , 8, 5, 9 352 | -- , 0, 1, 3 353 | -- , 6, 1, 6 ] 354 | -- >>> size (tr a) 355 | -- (4,3) 356 | -- >>> size a 357 | -- (3,4) 358 | 359 | -- | 668-675,680-683 360 | -- >>> let a = fromLists [[2,8,0,6],[4,5,1,1],[8,9,3,6]] :: Matrix Z 361 | -- >>> a 362 | -- (3><4) 363 | -- [ 2, 8, 0, 6 364 | -- , 4, 5, 1, 1 365 | -- , 8, 9, 3, 6 ] 366 | -- >>> let a' = ((reshape 6) . flatten) a 367 | -- >>> a' 368 | -- (2><6) 369 | -- [ 2, 8, 0, 6, 4, 5 370 | -- , 1, 1, 8, 9, 3, 6 ] 371 | -- >>> (tr . (reshape 3) . flatten . tr) a 372 | -- (3><4) 373 | -- [ 2, 8, 0, 6 374 | -- , 4, 5, 1, 1 375 | -- , 8, 9, 3, 6 ] 376 | 377 | -- | 697-712 378 | -- >>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), randomVector, rand) 379 | -- >>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c)) 380 | -- >>> let a = cmap (floor . (*10)) (rand' 1 2 2) :: Matrix Z 381 | -- >>> let b = cmap (floor . (*10)) (rand' 2 2 2) :: Matrix Z 382 | 383 | -- | 697-712 384 | -- >>> let a = fromLists [[8,8],[0,0]] :: Matrix Z 385 | -- >>> a 386 | -- (2><2) 387 | -- [ 8, 8 388 | -- , 0, 0 ] 389 | -- >>> let b = fromLists [[1,8],[0,4]] :: Matrix Z 390 | -- >>> b 391 | -- (2><2) 392 | -- [ 1, 8 393 | -- , 0, 4 ] 394 | -- >>> a === b 395 | -- (4><2) 396 | -- [ 8, 8 397 | -- , 0, 0 398 | -- , 1, 8 399 | -- , 0, 4 ] 400 | -- >>> a ||| b 401 | -- (2><4) 402 | -- [ 8, 8, 1, 8 403 | -- , 0, 0, 0, 4 ] 404 | 405 | -- | 718-737 406 | -- >>> let a = fromLists [[8,8],[0,0]] :: Matrix Z 407 | -- >>> let b = fromLists [[1,8],[0,4]] :: Matrix Z 408 | -- >>> a ||| b 409 | -- (2><4) 410 | -- [ 8, 8, 1, 8 411 | -- , 0, 0, 0, 4 ] 412 | 413 | -- | 718-737,755-756 414 | -- >>> let a = fromList [4,2] :: Vector Z 415 | -- >>> let b = fromList [3,8] :: Vector Z 416 | -- >>> fromColumns [a,b] 417 | -- (2><2) 418 | -- [ 4, 3 419 | -- , 2, 8 ] 420 | -- >>> vjoin [a,b] 421 | -- [4,2,3,8] 422 | -- >>> asColumn a 423 | -- (2><1) 424 | -- [ 4 425 | -- , 2 ] 426 | -- >>> fromBlocks [fmap asColumn [a,b]] 427 | -- (2><2) 428 | -- [ 4, 3 429 | -- , 2, 8 ] 430 | -- >>> fromList ([1..3] ++ [0] ++ [4]) :: Vector Z 431 | -- [1,2,3,0,4] 432 | 433 | -- | 783-796 434 | -- >>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), randomVector, rand) 435 | -- >>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c)) 436 | -- >>> let a = cmap (floor . (*10)) (rand' 1 2 12) :: Matrix Z 437 | 438 | -- | 783-796 439 | -- >>> let a = fromLists [[9,5,6,3,6,8,0,7,9,7,2,7],[1,4,9,2,2,1,0,6,2,2,4,0]] :: Matrix Z 440 | -- >>> a 441 | -- (2><12) 442 | -- [ 9, 5, 6, 3, 6, 8, 0, 7, 9, 7, 2, 7 443 | -- , 1, 4, 9, 2, 2, 1, 0, 6, 2, 2, 4, 0 ] 444 | -- >>> toBlocksEvery 2 4 a -- we could do @toBlocksEvery (rows a) ((floor . (/3) . fromIntegral . cols) a) a@ 445 | -- [[(2><4) 446 | -- [ 9, 5, 6, 3 447 | -- , 1, 4, 9, 2 ],(2><4) 448 | -- [ 6, 8, 0, 7 449 | -- , 2, 1, 0, 6 ],(2><4) 450 | -- [ 9, 7, 2, 7 451 | -- , 2, 2, 4, 0 ]]] 452 | -- >>> toBlocks [2] [3, 4-3, cols a - 4] a 453 | -- [[(2><3) 454 | -- [ 9, 5, 6 455 | -- , 1, 4, 9 ],(2><1) 456 | -- [ 3 457 | -- , 2 ],(2><8) 458 | -- [ 6, 8, 0, 7, 9, 7, 2, 7 459 | -- , 2, 1, 0, 6, 2, 2, 4, 0 ]]] 460 | 461 | -- | 1017-1025 462 | -- >>> let a = build 12 (^2) :: Vector Z 463 | -- >>> let i = fromList [1,1,3,8,5] :: Vector I 464 | -- >>> flatten ((asRow a) ?? (All, Pos i)) 465 | -- [1,1,9,64,25] 466 | -- >>> let j = fromLists [[3,4],[9,7]] :: Matrix I 467 | -- >>> remap (asRow (scalar 0)) (j) ((fromRows . replicate (rows j)) a) 468 | -- (2><2) 469 | -- [ 9, 16 470 | -- , 81, 49 ] 471 | 472 | -- | 1034-1049 473 | -- >>> let palette = fromLists [[0,0,0],[255,0,0],[0,255,0],[0,0,255],[255,255,255]] :: Matrix I 474 | -- >>> let image = fromLists [[0,1,2,0],[0,3,4,0]] :: Matrix I 475 | -- >>> fmap (\img -> palette ?? (Pos img, All)) (toRows image) -- almost 3D, [Matrix Z] instead 476 | -- [(4><3) 477 | -- [ 0, 0, 0 478 | -- , 255, 0, 0 479 | -- , 0, 255, 0 480 | -- , 0, 0, 0 ],(4><3) 481 | -- [ 0, 0, 0 482 | -- , 0, 0, 255 483 | -- , 255, 255, 255 484 | -- , 0, 0, 0 ]] 485 | 486 | -- | 1056-1080 487 | -- >>> let a = (3><4) [0::Z ..] 488 | -- >>> let i = fromLists [[0,1],[1,2]] :: Matrix I 489 | -- >>> let j = fromLists [[2,1],[3,3]] :: Matrix I 490 | -- >>> remap i j a 491 | -- (2><2) 492 | -- [ 2, 5 493 | -- , 7, 11 ] 494 | -- >>> remap i (asColumn (scalar 2)) a 495 | -- (2><2) 496 | -- [ 2, 6 497 | -- , 6, 10 ] 498 | -- >>> fmap (\r -> remap (asRow (scalar r)) j a) (toList ((rows a) |> [0..])) -- emulating 3D 499 | -- [(2><2) 500 | -- [ 2, 1 501 | -- , 3, 3 ],(2><2) 502 | -- [ 6, 5 503 | -- , 7, 7 ],(2><2) 504 | -- [ 10, 9 505 | -- , 11, 11 ]] 506 | 507 | -- | 1111-1136 508 | -- >>> let time = linspace 5 (20.0, 145.0) :: Vector R 509 | -- >>> let ydata = cmap sin ((5><4) [0..]) :: Matrix R -- data is a protected word 510 | -- >>> time 511 | -- [20.0,51.25,82.5,113.75,145.0] 512 | -- >>> ydata 513 | -- (5><4) 514 | -- [ 0.0, 0.8414709848078965, 0.9092974268256817, 0.1411200080598672 515 | -- , -0.7568024953079282, -0.9589242746631385, -0.27941549819892586, 0.6569865987187891 516 | -- , 0.9893582466233818, 0.4121184852417566, -0.5440211108893698, -0.9999902065507035 517 | -- , -0.5365729180004349, 0.4201670368266409, 0.9906073556948704, 0.6502878401571168 518 | -- , -0.2879033166650653, -0.9613974918795568, -0.750987246771676, 0.14987720966295234 ] 519 | -- >>> let ind = fmap maxIndex (toColumns ydata) 520 | -- >>> ind 521 | -- [2,0,3,1] 522 | -- >>> let time_max = flatten ((asRow time) ?? (All, Pos (idxs ind))) 523 | -- >>> let ydata_max = takeDiag (ydata ?? (Pos (idxs ind), All)) 524 | -- >>> time_max 525 | -- [82.5,20.0,113.75,51.25] 526 | -- >>> ydata_max 527 | -- [0.9893582466233818,0.8414709848078965,0.9906073556948704,0.6569865987187891] 528 | -- >>> (toList ydata_max) == (fmap maxElement (toColumns ydata)) 529 | -- True 530 | 531 | -- | 1140-1145,1150-1153,1158-1161 532 | -- >>> let a = 5 |> [0..] :: Vector Z 533 | -- >>> a 534 | -- [0,1,2,3,4] 535 | -- >>> let a' = accum a const (zip [1,3,4] (repeat 0)) 536 | -- >>> a' 537 | -- [0,0,2,0,0] 538 | -- >>> let a' = accum a const (zip [1,2,3] [0,0,2]) 539 | -- >>> a' 540 | -- [0,0,0,2,4] 541 | -- >>> let a' = accum a (+) (zip [0,0,2] (repeat 1)) 542 | -- >>> a' -- this DOES work, since we hit the 0 index twice mutably 543 | -- [2,1,3,3,4] 544 | 545 | -- | 1178-1185,1189-1193 546 | -- >>> let a = (3><4) [0..] :: Matrix Z 547 | -- >>> let b = (fmap (fmap (>4))) (toLists a) -- gives a [[Bool]], which is not that useful 548 | -- >>> let b = find (>4) a -- better than just Matrix Bool, which does not work in hmatrix 549 | -- >>> b -- a [IndexOf c] is more idiomatic 550 | -- [(1,1),(1,2),(1,3),(2,0),(2,1),(2,2),(2,3)] 551 | -- >>> fromList (fmap (atIndex a) b) 552 | -- [5,6,7,8,9,10,11] 553 | -- >>> accum a const (zip b (repeat 0)) 554 | -- (3><4) 555 | -- [ 0, 1, 2, 3 556 | -- , 4, 0, 0, 0 557 | -- , 0, 0, 0, 0 ] 558 | 559 | -- | 1226-1244 560 | -- >>> let a = (3><4) [0..] :: Matrix Z 561 | -- >>> let b1' = [False,True,True] 562 | -- >>> let b2' = [True,False,True,False] 563 | -- >>> import Data.List (elemIndices) 564 | -- >>> let helper = elemIndices True 565 | -- >>> let b1 = helper b1' 566 | -- >>> let b2 = helper b2' 567 | -- >>> a ?? (Pos (idxs b1), All) 568 | -- (2><4) 569 | -- [ 4, 5, 6, 7 570 | -- , 8, 9, 10, 11 ] 571 | -- >>> a ? b1 572 | -- (2><4) 573 | -- [ 4, 5, 6, 7 574 | -- , 8, 9, 10, 11 ] 575 | -- >>> a ?? (All, Pos (idxs b2)) 576 | -- (3><2) 577 | -- [ 0, 2 578 | -- , 4, 6 579 | -- , 8, 10 ] 580 | -- >>> a ?? (Pos (idxs b1), Pos (idxs b2)) -- unlike numpy, will return matrix that matches any combination from selections 581 | -- (2><2) 582 | -- [ 4, 6 583 | -- , 8, 10 ] 584 | -- >>> (fromList . fmap (atIndex a)) (zip b1 b2) -- this will give you the numpy result, the zipped indexing of a matrix to give a vector 585 | -- [4,10] 586 | 587 | -- | 1344-1378 588 | -- >>> let a = fromLists [[1.0, 2.0],[3.0,4.0]] :: Matrix R 589 | -- >>> disp 2 a 590 | -- 2x2 591 | -- 1 2 592 | -- 3 4 593 | -- 594 | -- >>> tr a 595 | -- (2><2) 596 | -- [ 1.0, 3.0 597 | -- , 2.0, 4.0 ] 598 | -- 599 | -- >>> disp 2 (inv a) 600 | -- 2x2 601 | -- -2.00 1.00 602 | -- 1.50 -0.50 603 | -- 604 | -- >>> let u = ident 2 :: Matrix R 605 | -- >>> u 606 | -- (2><2) 607 | -- [ 1.0, 0.0 608 | -- , 0.0, 1.0 ] 609 | -- >>> let j = fromLists [[0.0,-1.0],[1.0,0.0]] :: Matrix R 610 | -- 611 | -- >>> j <> j 612 | -- (2><2) 613 | -- [ -1.0, 0.0 614 | -- , 0.0, -1.0 ] 615 | -- 616 | -- >>> sumElements (takeDiag u) 617 | -- 2.0 618 | -- 619 | -- >>> let y = vector [5,7] 620 | -- >>> disp 2 (asColumn (a <\> y)) 621 | -- 2x1 622 | -- -3.00 623 | -- 4.00 624 | -- 625 | -- >>> eig j 626 | -- ([0.0 :+ 1.0,0.0 :+ (-1.0)],(2><2) 627 | -- [ 0.7071067811865475 :+ 0.0, 0.7071067811865475 :+ (-0.0) 628 | -- , 0.0 :+ (-0.7071067811865475), 0.0 :+ 0.7071067811865475 ]) 629 | 630 | -- | 1401-1415 631 | -- >>> let a = 30 |> [0..] :: Vector Z 632 | -- >>> toBlocksEvery 5 3 (reshape 3 a) -- emulate depth with lists 633 | -- [[(5><3) 634 | -- [ 0, 1, 2 635 | -- , 3, 4, 5 636 | -- , 6, 7, 8 637 | -- , 9, 10, 11 638 | -- , 12, 13, 14 ]],[(5><3) 639 | -- [ 15, 16, 17 640 | -- , 18, 19, 20 641 | -- , 21, 22, 23 642 | -- , 24, 25, 26 643 | -- , 27, 28, 29 ]]] 644 | 645 | -- | 1427-1431 646 | -- >>> let x = fromList [0,2..10-2] :: Vector Z 647 | -- >>> x 648 | -- [0,2,4,6,8] 649 | -- >>> let y = 5 |> [0..] :: Vector Z 650 | -- >>> y 651 | -- [0,1,2,3,4] 652 | -- >>> let m = fromRows [x,y] 653 | -- >>> m 654 | -- (2><5) 655 | -- [ 0, 2, 4, 6, 8 656 | -- , 0, 1, 2, 3, 4 ] 657 | -- >>> let xy = vjoin [x,y] 658 | -- >>> xy 659 | -- [0,2,4,6,8,0,1,2,3,4] 660 | 661 | -- | 1452-1463 662 | -- >>> let (mu, sigma) = (2, 0.5) 663 | -- >>> (mu, sigma) 664 | -- (2,0.5) 665 | -- >>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), gaussianSample, rand) 666 | -- >>> let rand' seed mu sigma c = mu + sigma * (randomVector seed Gaussian c) 667 | -- >>> let v = rand' 1 mu sigma 10000 668 | -- >>> :{ 669 | -- normedhist :: Vector R -> Int -> Vector R 670 | -- normedhist vec numbins = accum bins (+) (fmap go (toList vec)) 671 | -- where 672 | -- bins = konst 0 numbins 673 | -- (lo, hi) = (minElement vec, maxElement vec) 674 | -- delt = (hi-lo) / fromIntegral numbins 675 | -- valscale val = min (numbins-1) (floor ((val-lo) / delt)) 676 | -- go val = (valscale val, 1 / fromIntegral (size vec)) 677 | -- :} 678 | -- 679 | -- >>> let hist = normedhist v 50 680 | -- >>> disp 2 (scalar (sumElements hist)) 681 | -- 1x1 682 | -- 1.00 683 | -- >>> let bins = linspace 50 (minElement v, maxElement v) 684 | --------------------------------------------------------------------------------