| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
NumHask.Examples
Description
NumHask usage examples
Examples
Imports and Pragmas
NumHask.Prelude is a complete replacement for the standard prelude.
NoImplicitPrelude is explicitly required as a pragma, and ExtendedDefaultRules is needed to avoid having to explicitly type literal numbers.
>>>:set -XNoImplicitPrelude>>>:set -XExtendedDefaultRules>>>import NumHask.Prelude
Basic Arithmetic
Int, Integer, Double and Float are from base. NumHask takes these classes and redefines the basic arithmetic operators.
>>>1 + 12
>>>1 - 10
>>>1 * 11
>>>1 / 11.0
Note that the literal numbers in the divide above defaulted to Float rather than Int.
>>>1 / (1::Int)... ... No instance for (MultiplicativeGroup Int) ... arising from a use of ‘/’ ...
>>>1 / fromIntegral (1::Int)1.0
>>>1 `div` 20
>>>3 `mod` 21
Float and Double are Field instances.
>>>zero == 0.0True
>>>one == 1.0True
>>>1.0 + 1.02.0
>>>1.0 - 1.00.0
>>>1.0 * 1.01.0
>>>1.0 / 1.01.0
BoundedField lets divide by zero work for Floats and Doubles.
>>>one/zeroInfinity
>>>-one/zero-Infinity
>>>zero/zero+oneNaN
>>>logBase 2 42.0
>>>2 ** 24.0
>>>sqrt 42.0
>>>exp 27.38905609893065
>>>log 20.6931471805599453
Vectors
A Vector is a number by virtue of it being a Representable Functor where the representation is an Int.
>>>import NumHask.Vector>>>:set -XDataKinds>>>:set -XOverloadedLists>>>[] :: Vector 3 Int[0,0,0]
>>>let a = [1..] :: Vector 3 Int>>>a[1,2,3]
>>>let b = [3,2] :: Vector 3 Int>>>b[3,2,0]
>>>let c = [1.0,2.0] :: Vector 3 Float>>>let d = [3.0,2.0] :: Vector 3 Float
>>>a+zero==aTrue>>>zero+a==aTrue>>>a+b[4,4,3]
>>>a-a == zeroTrue
>>>a * b[3,4,0]
>>>let a' = unsafeToVector . someVector $ a :: Vector 2 Int>>>let b' = unsafeToVector . someVector $ b :: Vector 2 Int>>>a' `divMod` b'([0,1],[1,0])
>>>c / d[0.33333334,1.0,NaN]
>>>size c :: Float2.236068
>>>distance c d :: Float2.0
>>>c <.> d :: Float7.0
The type of an outer product of two vectors is a Vector m (Vector n), and is a perfectly formed Matrix representation.
>>>a >< b[[3,2,0],[6,4,0],[9,6,0]]
>>>(a >< b) >< (b >< a)[[[9,12,0],[6,8,0],[0,0,0]],[[18,24,0],[12,16,0],[0,0,0]],[[27,36,0],[18,24,0],[0,0,0]]]