-- |
-- Module      : Data.VectorSpace.Free
-- Copyright   : (c) Justus Sagemüller 2016
-- License     : GPL v3
-- 
-- Maintainer  : (@) jsag $ hvl.no
-- Stability   : experimental
-- Portability : portable
-- 
{-# LANGUAGE TypeFamilies            #-}
{-# LANGUAGE TypeOperators           #-}
{-# LANGUAGE FlexibleInstances       #-}
{-# LANGUAGE FlexibleContexts        #-}
{-# LANGUAGE CPP                     #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE DefaultSignatures       #-}
{-# LANGUAGE ScopedTypeVariables     #-}

module Data.VectorSpace.Free (
                             -- * Supported types
                             -- ** Fixed low dimension
                             -- | These come from the <http://hackage.haskell.org/package/linear/ linear> package.
                               V0
                             , V1
                             , V2
                             , V3
                             , V4
                             -- ** Arbitrary dimension
                             , Sequence, FinSuppSeq
                             -- * The vector-space type classes
                             -- ** General
                             -- | These come from the <http://hackage.haskell.org/package/vector-space/ vector-space> package.
                             , AffineSpace(..), AdditiveGroup(..)
                             , VectorSpace(..), InnerSpace(..), HasBasis(..)
                             -- ** Small
                             , OneDimensional(..)
                             -- ** Free
                             , FreeVectorSpace(..)
                             , FiniteFreeSpace(..)
                             ) where

import Data.AffineSpace
import Data.VectorSpace
import Data.Cross
import Data.VectorSpace.Free.Class
import Data.VectorSpace.Free.FiniteSupportedSequence (FinSuppSeq)
import Data.VectorSpace.Free.Sequence (Sequence)
import Data.Basis

import Data.MemoTrie
import Data.Void

import qualified Linear as L
import Linear.V0
import Linear.V1
import Linear.V2
import Linear.V3
import Linear.V4
import qualified Linear.Affine as LA

import Control.Lens ((^.), FoldableWithIndex, ifoldr, Lens')

import qualified Data.Foldable as Foldable

import qualified Data.Vector.Unboxed as UArr
import qualified Data.Vector.Generic.Mutable as MArr

import Data.Ratio
import Foreign.C.Types (CFloat, CDouble)

import GHC.Exts (IsList(..))
import qualified GHC.Generics as Gnrx
import GHC.Generics (Generic, (:*:)(..))

import qualified Text.Show.Pragmatic as SP

vDecomp :: FoldableWithIndex (L.E v) v => v s -> [(L.E v, s)]
vDecomp :: v s -> [(E v, s)]
vDecomp = (E v -> s -> [(E v, s)] -> [(E v, s)])
-> [(E v, s)] -> v s -> [(E v, s)]
forall i (f :: * -> *) a b.
FoldableWithIndex i f =>
(i -> a -> b -> b) -> b -> f a -> b
ifoldr (\E v
b s
s [(E v, s)]
l -> (E v
b,s
s)(E v, s) -> [(E v, s)] -> [(E v, s)]
forall a. a -> [a] -> [a]
:[(E v, s)]
l) []

#define portFinDV(v)                              \
instance Num s => AffineSpace (v s) where {        \
  type Diff (v s) = v s;                            \
  (.-.) = (L.^-^);                                   \
  (.+^) = (L.^+^) };                                  \
instance Num s => AdditiveGroup (v s) where {          \
  zeroV = L.zero;                                       \
  (^+^) = (L.^+^);                                       \
  negateV = L.negated };                                  \
instance Num s => VectorSpace (v s) where {                \
  type Scalar (v s) = s;                                    \
  (*^) = (L.*^) };                                           \
instance (Num s, AdditiveGroup s) => InnerSpace (v s) where { \
  (<.>) = L.dot };                                             \
instance (Num s, AdditiveGroup s) => HasBasis (v s) where {     \
  type Basis (v s) = L.E v;                                      \
  decompose = vDecomp;                                            \
  basisValue x = L.unit (L.el x :: Lens' (v s) s);                 \
  decompose' w (L.E le) = w^.le }

portFinDV(V0)
portFinDV(V1)
portFinDV(V2)
portFinDV(V3)
portFinDV(V4)

#define portFinDP(v)                                 \
instance Num s => AffineSpace (LA.Point v s) where {  \
  type Diff (LA.Point v s) = v s;                      \
  (.-.) = (LA..-.);                                     \
  (.+^) = (LA..+^) }

portFinDP(V0)
portFinDP(V1)
portFinDP(V2)
instance Num s => HasCross2 (V2 s) where
  cross2 :: V2 s -> V2 s
cross2 (V2 s
x s
y) = s -> s -> V2 s
forall a. a -> a -> V2 a
V2 (-s
y) s
x

portFinDP(V3)
instance Num s => HasCross3 (V3 s) where
  V3 s
ax s
ay s
az cross3 :: V3 s -> V3 s -> V3 s
`cross3` V3 s
bx s
by s
bz = s -> s -> s -> V3 s
forall a. a -> a -> a -> V3 a
V3 (s
ay s -> s -> s
forall a. Num a => a -> a -> a
* s
bz s -> s -> s
forall a. Num a => a -> a -> a
- s
az s -> s -> s
forall a. Num a => a -> a -> a
* s
by)
                                        (s
az s -> s -> s
forall a. Num a => a -> a -> a
* s
bx s -> s -> s
forall a. Num a => a -> a -> a
- s
ax s -> s -> s
forall a. Num a => a -> a -> a
* s
bz)
                                        (s
ax s -> s -> s
forall a. Num a => a -> a -> a
* s
by s -> s -> s
forall a. Num a => a -> a -> a
- s
ay s -> s -> s
forall a. Num a => a -> a -> a
* s
bx)

portFinDP(V4)


instance HasTrie (L.E V0) where
  newtype L.E V0 :->: a = V0T (V0 a)
  trie :: (E V0 -> b) -> E V0 :->: b
trie E V0 -> b
f = V0 b -> E V0 :->: b
forall a. V0 a -> E V0 :->: a
V0T V0 b
forall a. V0 a
V0
  untrie :: (E V0 :->: b) -> E V0 -> b
untrie (V0T v) (L.E forall x (f :: * -> *). Functor f => (x -> f x) -> V0 x -> f (V0 x)
i) = V0 b
vV0 b -> Getting b (V0 b) b -> b
forall s a. s -> Getting a s a -> a
^.Getting b (V0 b) b
forall x (f :: * -> *). Functor f => (x -> f x) -> V0 x -> f (V0 x)
i
  enumerate :: (E V0 :->: b) -> [(E V0, b)]
enumerate (V0T V0) = []
instance HasTrie (L.E V1) where
  newtype L.E V1 :->: a = V1T (V1 a)
  trie :: (E V1 -> b) -> E V1 :->: b
trie E V1 -> b
f = V1 b -> E V1 :->: b
forall a. V1 a -> E V1 :->: a
V1T (V1 b -> E V1 :->: b) -> V1 b -> E V1 :->: b
forall a b. (a -> b) -> a -> b
$ b -> V1 b
forall a. a -> V1 a
V1 (E V1 -> b
f E V1
forall (t :: * -> *). R1 t => E t
L.ex)
  untrie :: (E V1 :->: b) -> E V1 -> b
untrie (V1T v) (L.E forall x (f :: * -> *). Functor f => (x -> f x) -> V1 x -> f (V1 x)
i) = V1 b
vV1 b -> Getting b (V1 b) b -> b
forall s a. s -> Getting a s a -> a
^.Getting b (V1 b) b
forall x (f :: * -> *). Functor f => (x -> f x) -> V1 x -> f (V1 x)
i
  enumerate :: (E V1 :->: b) -> [(E V1, b)]
enumerate (V1T (V1 x)) = [(E V1
forall (t :: * -> *). R1 t => E t
L.ex, b
x)]
instance HasTrie (L.E V2) where
  newtype L.E V2 :->: a = V2T (V2 a)
  trie :: (E V2 -> b) -> E V2 :->: b
trie E V2 -> b
f = V2 b -> E V2 :->: b
forall a. V2 a -> E V2 :->: a
V2T (V2 b -> E V2 :->: b) -> V2 b -> E V2 :->: b
forall a b. (a -> b) -> a -> b
$ b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (E V2 -> b
f E V2
forall (t :: * -> *). R1 t => E t
L.ex) (E V2 -> b
f E V2
forall (t :: * -> *). R2 t => E t
L.ey)
  untrie :: (E V2 :->: b) -> E V2 -> b
untrie (V2T v) (L.E forall x (f :: * -> *). Functor f => (x -> f x) -> V2 x -> f (V2 x)
i) = V2 b
vV2 b -> Getting b (V2 b) b -> b
forall s a. s -> Getting a s a -> a
^.Getting b (V2 b) b
forall x (f :: * -> *). Functor f => (x -> f x) -> V2 x -> f (V2 x)
i
  enumerate :: (E V2 :->: b) -> [(E V2, b)]
enumerate (V2T (V2 x y)) = [(E V2
forall (t :: * -> *). R1 t => E t
L.ex, b
x), (E V2
forall (t :: * -> *). R2 t => E t
L.ey, b
y)]
instance HasTrie (L.E V3) where
  newtype L.E V3 :->: a = V3T (V3 a)
  trie :: (E V3 -> b) -> E V3 :->: b
trie E V3 -> b
f = V3 b -> E V3 :->: b
forall a. V3 a -> E V3 :->: a
V3T (V3 b -> E V3 :->: b) -> V3 b -> E V3 :->: b
forall a b. (a -> b) -> a -> b
$ b -> b -> b -> V3 b
forall a. a -> a -> a -> V3 a
V3 (E V3 -> b
f E V3
forall (t :: * -> *). R1 t => E t
L.ex) (E V3 -> b
f E V3
forall (t :: * -> *). R2 t => E t
L.ey) (E V3 -> b
f E V3
forall (t :: * -> *). R3 t => E t
L.ez)
  untrie :: (E V3 :->: b) -> E V3 -> b
untrie (V3T v) (L.E forall x (f :: * -> *). Functor f => (x -> f x) -> V3 x -> f (V3 x)
i) = V3 b
vV3 b -> Getting b (V3 b) b -> b
forall s a. s -> Getting a s a -> a
^.Getting b (V3 b) b
forall x (f :: * -> *). Functor f => (x -> f x) -> V3 x -> f (V3 x)
i
  enumerate :: (E V3 :->: b) -> [(E V3, b)]
enumerate (V3T (V3 x y z)) = [(E V3
forall (t :: * -> *). R1 t => E t
L.ex, b
x), (E V3
forall (t :: * -> *). R2 t => E t
L.ey, b
y), (E V3
forall (t :: * -> *). R3 t => E t
L.ez, b
z)]
instance HasTrie (L.E V4) where
  newtype L.E V4 :->: a = V4T (V4 a)
  trie :: (E V4 -> b) -> E V4 :->: b
trie E V4 -> b
f = V4 b -> E V4 :->: b
forall a. V4 a -> E V4 :->: a
V4T (V4 b -> E V4 :->: b) -> V4 b -> E V4 :->: b
forall a b. (a -> b) -> a -> b
$ b -> b -> b -> b -> V4 b
forall a. a -> a -> a -> a -> V4 a
V4 (E V4 -> b
f E V4
forall (t :: * -> *). R1 t => E t
L.ex) (E V4 -> b
f E V4
forall (t :: * -> *). R2 t => E t
L.ey) (E V4 -> b
f E V4
forall (t :: * -> *). R3 t => E t
L.ez) (E V4 -> b
f E V4
forall (t :: * -> *). R4 t => E t
L.ew)
  untrie :: (E V4 :->: b) -> E V4 -> b
untrie (V4T v) (L.E forall x (f :: * -> *). Functor f => (x -> f x) -> V4 x -> f (V4 x)
i) = V4 b
vV4 b -> Getting b (V4 b) b -> b
forall s a. s -> Getting a s a -> a
^.Getting b (V4 b) b
forall x (f :: * -> *). Functor f => (x -> f x) -> V4 x -> f (V4 x)
i
  enumerate :: (E V4 :->: b) -> [(E V4, b)]
enumerate (V4T (V4 x y z w)) = [(E V4
forall (t :: * -> *). R1 t => E t
L.ex, b
x), (E V4
forall (t :: * -> *). R2 t => E t
L.ey, b
y), (E V4
forall (t :: * -> *). R3 t => E t
L.ez, b
z), (E V4
forall (t :: * -> *). R4 t => E t
L.ew, b
w)]

instance SP.Show (V0 Int) where showsPrec :: Int -> V0 Int -> ShowS
showsPrec = Int -> V0 Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V1 Int) where showsPrec :: Int -> V1 Int -> ShowS
showsPrec = Int -> V1 Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V2 Int) where showsPrec :: Int -> V2 Int -> ShowS
showsPrec = Int -> V2 Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V3 Int) where showsPrec :: Int -> V3 Int -> ShowS
showsPrec = Int -> V3 Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V4 Int) where showsPrec :: Int -> V4 Int -> ShowS
showsPrec = Int -> V4 Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec

instance SP.Show (V0 Integer) where showsPrec :: Int -> V0 Integer -> ShowS
showsPrec = Int -> V0 Integer -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V1 Integer) where showsPrec :: Int -> V1 Integer -> ShowS
showsPrec = Int -> V1 Integer -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V2 Integer) where showsPrec :: Int -> V2 Integer -> ShowS
showsPrec = Int -> V2 Integer -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V3 Integer) where showsPrec :: Int -> V3 Integer -> ShowS
showsPrec = Int -> V3 Integer -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec
instance SP.Show (V4 Integer) where showsPrec :: Int -> V4 Integer -> ShowS
showsPrec = Int -> V4 Integer -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec

instance SP.Show (V0 Float) where
  show :: V0 Float -> String
show V0 Float
V0 = String
"V0"
instance SP.Show (V1 Float) where
  showsPrec :: Int -> V1 Float -> ShowS
showsPrec Int
p (V1 Float
x) = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V1 "String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Float -> ShowS
forall a. Show a => Int -> a -> ShowS
SP.showsPrec Int
10 Float
x
instance SP.Show (V2 Float) where
  showsPrec :: Int -> V2 Float -> ShowS
showsPrec Int
p V2 Float
v = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V2 "String -> ShowS
forall a. [a] -> [a] -> [a]
++)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
xsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ys
   where V2 ShowS
xs ShowS
ys = (Float -> Float) -> Int -> Int -> V2 Float -> V2 ShowS
forall n sn (list :: * -> *).
(ShowMagnitudeRangeLimited n, RealFloat sn, Traversable list) =>
(n -> sn) -> Int -> Int -> list n -> list ShowS
SP.showsPrecWithSharedPrecision Float -> Float
forall a. Num a => a -> a
abs Int
7 Int
10 V2 Float
v
instance SP.Show (V3 Float) where
  showsPrec :: Int -> V3 Float -> ShowS
showsPrec Int
p V3 Float
v = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V3 "String -> ShowS
forall a. [a] -> [a] -> [a]
++)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
xsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ysShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
zs
   where V3 ShowS
xs ShowS
ys ShowS
zs = (Float -> Float) -> Int -> Int -> V3 Float -> V3 ShowS
forall n sn (list :: * -> *).
(ShowMagnitudeRangeLimited n, RealFloat sn, Traversable list) =>
(n -> sn) -> Int -> Int -> list n -> list ShowS
SP.showsPrecWithSharedPrecision Float -> Float
forall a. Num a => a -> a
abs Int
7 Int
10 V3 Float
v
instance SP.Show (V4 Float) where
  showsPrec :: Int -> V4 Float -> ShowS
showsPrec Int
p V4 Float
v = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V4 "String -> ShowS
forall a. [a] -> [a] -> [a]
++)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
xsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ysShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
zsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ws
   where V4 ShowS
xs ShowS
ys ShowS
zs ShowS
ws = (Float -> Float) -> Int -> Int -> V4 Float -> V4 ShowS
forall n sn (list :: * -> *).
(ShowMagnitudeRangeLimited n, RealFloat sn, Traversable list) =>
(n -> sn) -> Int -> Int -> list n -> list ShowS
SP.showsPrecWithSharedPrecision Float -> Float
forall a. Num a => a -> a
abs Int
7 Int
10 V4 Float
v

instance SP.Show (V0 Double) where
  show :: V0 Double -> String
show V0 Double
V0 = String
"V0"
instance SP.Show (V1 Double) where
  showsPrec :: Int -> V1 Double -> ShowS
showsPrec Int
p (V1 Double
x) = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V1 "String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double -> ShowS
forall a. Show a => Int -> a -> ShowS
SP.showsPrec Int
10 Double
x
instance SP.Show (V2 Double) where
  showsPrec :: Int -> V2 Double -> ShowS
showsPrec Int
p V2 Double
v = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V2 "String -> ShowS
forall a. [a] -> [a] -> [a]
++)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
xsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ys
   where V2 ShowS
xs ShowS
ys = (Double -> Double) -> Int -> Int -> V2 Double -> V2 ShowS
forall n sn (list :: * -> *).
(ShowMagnitudeRangeLimited n, RealFloat sn, Traversable list) =>
(n -> sn) -> Int -> Int -> list n -> list ShowS
SP.showsPrecWithSharedPrecision Double -> Double
forall a. Num a => a -> a
abs Int
10 Int
10 V2 Double
v
instance SP.Show (V3 Double) where
  showsPrec :: Int -> V3 Double -> ShowS
showsPrec Int
p V3 Double
v = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V3 "String -> ShowS
forall a. [a] -> [a] -> [a]
++)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
xsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ysShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
zs
   where V3 ShowS
xs ShowS
ys ShowS
zs = (Double -> Double) -> Int -> Int -> V3 Double -> V3 ShowS
forall n sn (list :: * -> *).
(ShowMagnitudeRangeLimited n, RealFloat sn, Traversable list) =>
(n -> sn) -> Int -> Int -> list n -> list ShowS
SP.showsPrecWithSharedPrecision Double -> Double
forall a. Num a => a -> a
abs Int
10 Int
10 V3 Double
v
instance SP.Show (V4 Double) where
  showsPrec :: Int -> V4 Double -> ShowS
showsPrec Int
p V4 Double
v = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
9) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (String
"V4 "String -> ShowS
forall a. [a] -> [a] -> [a]
++)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
xsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ysShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
zsShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ShowS
ws
   where V4 ShowS
xs ShowS
ys ShowS
zs ShowS
ws = (Double -> Double) -> Int -> Int -> V4 Double -> V4 ShowS
forall n sn (list :: * -> *).
(ShowMagnitudeRangeLimited n, RealFloat sn, Traversable list) =>
(n -> sn) -> Int -> Int -> list n -> list ShowS
SP.showsPrecWithSharedPrecision Double -> Double
forall a. Num a => a -> a
abs Int
10 Int
10 V4 Double
v


infixr 7 ^/^, ^/!

class (VectorSpace v, Fractional (Scalar v)) => OneDimensional v where
  -- | Compare the (directed) length of two vectors.
  (^/^) :: v -> v -> Maybe (Scalar v)
  default (^/^) :: ( Generic v, OneDimensional (VRep v)
                   , Scalar (VRep v) ~ Scalar v )
                     => v -> v -> Maybe (Scalar v)
  v
v ^/^ v
w = (v -> Rep v Void
forall a x. Generic a => a -> Rep a x
Gnrx.from v
v :: VRep v) Rep v Void -> Rep v Void -> Maybe (Scalar (Rep v Void))
forall v. OneDimensional v => v -> v -> Maybe (Scalar v)
^/^ v -> Rep v Void
forall a x. Generic a => a -> Rep a x
Gnrx.from v
w
  -- | Unsafe version of '^/^'.
  (^/!) :: v -> v -> Scalar v
  v
v^/!v
w = case v
vv -> v -> Maybe (Scalar v)
forall v. OneDimensional v => v -> v -> Maybe (Scalar v)
^/^v
w of
       Just Scalar v
μ  -> Scalar v
μ
       Maybe (Scalar v)
Nothing -> Scalar v
1Scalar v -> Scalar v -> Scalar v
forall a. Fractional a => a -> a -> a
/Scalar v
0

instance OneDimensional Float where
  Float
_^/^ :: Float -> Float -> Maybe (Scalar Float)
^/^Float
0 = Maybe (Scalar Float)
forall a. Maybe a
Nothing
  Float
x^/^Float
y = Float -> Maybe Float
forall a. a -> Maybe a
Just (Float -> Maybe Float) -> Float -> Maybe Float
forall a b. (a -> b) -> a -> b
$ Float
xFloat -> Float -> Float
forall a. Fractional a => a -> a -> a
/Float
y
  Float
x^/! :: Float -> Float -> Scalar Float
^/!Float
y = Float
xFloat -> Float -> Float
forall a. Fractional a => a -> a -> a
/Float
y
instance OneDimensional Double where
  Double
_^/^ :: Double -> Double -> Maybe (Scalar Double)
^/^Double
0 = Maybe (Scalar Double)
forall a. Maybe a
Nothing
  Double
x^/^Double
y = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double) -> Double -> Maybe Double
forall a b. (a -> b) -> a -> b
$ Double
xDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
y
  Double
x^/! :: Double -> Double -> Scalar Double
^/!Double
y = Double
xDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
y
instance OneDimensional CFloat where
  CFloat
_^/^ :: CFloat -> CFloat -> Maybe (Scalar CFloat)
^/^CFloat
0 = Maybe (Scalar CFloat)
forall a. Maybe a
Nothing
  CFloat
x^/^CFloat
y = CFloat -> Maybe CFloat
forall a. a -> Maybe a
Just (CFloat -> Maybe CFloat) -> CFloat -> Maybe CFloat
forall a b. (a -> b) -> a -> b
$ CFloat
xCFloat -> CFloat -> CFloat
forall a. Fractional a => a -> a -> a
/CFloat
y
  CFloat
x^/! :: CFloat -> CFloat -> Scalar CFloat
^/!CFloat
y = CFloat
xCFloat -> CFloat -> CFloat
forall a. Fractional a => a -> a -> a
/CFloat
y
instance OneDimensional CDouble where
  CDouble
_^/^ :: CDouble -> CDouble -> Maybe (Scalar CDouble)
^/^CDouble
0 = Maybe (Scalar CDouble)
forall a. Maybe a
Nothing
  CDouble
x^/^CDouble
y = CDouble -> Maybe CDouble
forall a. a -> Maybe a
Just (CDouble -> Maybe CDouble) -> CDouble -> Maybe CDouble
forall a b. (a -> b) -> a -> b
$ CDouble
xCDouble -> CDouble -> CDouble
forall a. Fractional a => a -> a -> a
/CDouble
y
  CDouble
x^/! :: CDouble -> CDouble -> Scalar CDouble
^/!CDouble
y = CDouble
xCDouble -> CDouble -> CDouble
forall a. Fractional a => a -> a -> a
/CDouble
y
instance Integral i => OneDimensional (Ratio i) where
  Ratio i
_^/^ :: Ratio i -> Ratio i -> Maybe (Scalar (Ratio i))
^/^Ratio i
0 = Maybe (Scalar (Ratio i))
forall a. Maybe a
Nothing
  Ratio i
x^/^Ratio i
y = Ratio i -> Maybe (Ratio i)
forall a. a -> Maybe a
Just (Ratio i -> Maybe (Ratio i)) -> Ratio i -> Maybe (Ratio i)
forall a b. (a -> b) -> a -> b
$ Ratio i
xRatio i -> Ratio i -> Ratio i
forall a. Fractional a => a -> a -> a
/Ratio i
y
  Ratio i
x^/! :: Ratio i -> Ratio i -> Scalar (Ratio i)
^/!Ratio i
y = Ratio i
xRatio i -> Ratio i -> Ratio i
forall a. Fractional a => a -> a -> a
/Ratio i
y
instance (Eq r, Fractional r) => OneDimensional (V1 r) where
  V1 r
_^/^ :: V1 r -> V1 r -> Maybe (Scalar (V1 r))
^/^V1 r
0 = Maybe (Scalar (V1 r))
forall a. Maybe a
Nothing
  V1 r
x^/^V1 r
y = r -> Maybe r
forall a. a -> Maybe a
Just (r -> Maybe r) -> r -> Maybe r
forall a b. (a -> b) -> a -> b
$ r
xr -> r -> r
forall a. Fractional a => a -> a -> a
/r
y
  V1 r
x^/! :: V1 r -> V1 r -> Scalar (V1 r)
^/!V1 r
y = r
xr -> r -> r
forall a. Fractional a => a -> a -> a
/r
y



class (VectorSpace v, Num (Scalar v)) => FiniteFreeSpace v where
  freeDimension :: Functor p => p v -> Int
  default freeDimension :: (Generic v, FiniteFreeSpace (VRep v))
                        => p v -> Int
  freeDimension p v
_ = [Rep v Void] -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension ([]::[VRep v])
  toFullUnboxVect :: UArr.Unbox (Scalar v) => v -> UArr.Vector (Scalar v)
  default toFullUnboxVect
        :: ( Generic v, FiniteFreeSpace (VRep v)
           , UArr.Unbox (Scalar v)
           , Scalar (VRep v) ~ Scalar v )
                           => v -> UArr.Vector (Scalar v)
  toFullUnboxVect v
v = Rep v Void -> Vector (Scalar (Rep v Void))
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
v -> Vector (Scalar v)
toFullUnboxVect (v -> Rep v Void
forall a x. Generic a => a -> Rep a x
Gnrx.from v
v :: VRep v)
  unsafeFromFullUnboxVect :: UArr.Unbox (Scalar v) => UArr.Vector (Scalar v) -> v
  default unsafeFromFullUnboxVect
        :: ( Generic v, FiniteFreeSpace (VRep v)
           , UArr.Unbox (Scalar v)
           , Scalar (VRep v) ~ Scalar v )
                           => UArr.Vector (Scalar v) -> v
  unsafeFromFullUnboxVect Vector (Scalar v)
v = Rep v Void -> v
forall a x. Generic a => Rep a x -> a
Gnrx.to (Vector (Scalar (Rep v Void)) -> Rep v Void
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect Vector (Scalar v)
Vector (Scalar (Rep v Void))
v :: VRep v)
  fromUnboxVect :: UArr.Unbox (Scalar v) => UArr.Vector (Scalar v) -> v
  fromUnboxVect Vector (Scalar v)
v = v
result
   where result :: v
result = case Vector (Scalar v) -> Int
forall a. Unbox a => Vector a -> Int
UArr.length Vector (Scalar v)
v of
           Int
0        -> v
forall v. AdditiveGroup v => v
zeroV
           Int
n | Int
nInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
d  -> Vector (Scalar v) -> v
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect (Vector (Scalar v) -> v) -> Vector (Scalar v) -> v
forall a b. (a -> b) -> a -> b
$ Vector (Scalar v)
v Vector (Scalar v) -> Vector (Scalar v) -> Vector (Scalar v)
forall a. Unbox a => Vector a -> Vector a -> Vector a
UArr.++ Int -> Scalar v -> Vector (Scalar v)
forall a. Unbox a => Int -> a -> Vector a
UArr.replicate (Int
dInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
n) Scalar v
0
         d :: Int
d = [v] -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension [v
result]


instance FiniteFreeSpace Int where
  freeDimension :: p Int -> Int
freeDimension p Int
_ = Int
1
  toFullUnboxVect :: Int -> Vector (Scalar Int)
toFullUnboxVect = Int -> Vector (Scalar Int)
forall a. Unbox a => a -> Vector a
UArr.singleton
  unsafeFromFullUnboxVect :: Vector (Scalar Int) -> Int
unsafeFromFullUnboxVect Vector (Scalar Int)
v = Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector Int
Vector (Scalar Int)
v Int
0
instance FiniteFreeSpace Float where
  freeDimension :: p Float -> Int
freeDimension p Float
_ = Int
1
  toFullUnboxVect :: Float -> Vector (Scalar Float)
toFullUnboxVect = Float -> Vector (Scalar Float)
forall a. Unbox a => a -> Vector a
UArr.singleton
  unsafeFromFullUnboxVect :: Vector (Scalar Float) -> Float
unsafeFromFullUnboxVect Vector (Scalar Float)
v = Vector Float -> Int -> Float
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector Float
Vector (Scalar Float)
v Int
0
instance FiniteFreeSpace Double where
  freeDimension :: p Double -> Int
freeDimension p Double
_ = Int
1
  toFullUnboxVect :: Double -> Vector (Scalar Double)
toFullUnboxVect = Double -> Vector (Scalar Double)
forall a. Unbox a => a -> Vector a
UArr.singleton
  unsafeFromFullUnboxVect :: Vector (Scalar Double) -> Double
unsafeFromFullUnboxVect Vector (Scalar Double)
v = Vector Double -> Int -> Double
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector Double
Vector (Scalar Double)
v Int
0

instance (FiniteFreeSpace u, FiniteFreeSpace v, Scalar u ~ Scalar v)
      => FiniteFreeSpace (u,v) where
  freeDimension :: p (u, v) -> Int
freeDimension p (u, v)
puv = p u -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension (((u, v) -> u) -> p (u, v) -> p u
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (u, v) -> u
forall a b. (a, b) -> a
fst p (u, v)
puv) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ p v -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension (((u, v) -> v) -> p (u, v) -> p v
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (u, v) -> v
forall a b. (a, b) -> b
snd p (u, v)
puv)
  toFullUnboxVect :: (u, v) -> Vector (Scalar (u, v))
toFullUnboxVect (u
u,v
v) = u -> Vector (Scalar u)
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
v -> Vector (Scalar v)
toFullUnboxVect u
u Vector (Scalar v) -> Vector (Scalar v) -> Vector (Scalar v)
forall a. Unbox a => Vector a -> Vector a -> Vector a
UArr.++ v -> Vector (Scalar v)
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
v -> Vector (Scalar v)
toFullUnboxVect v
v
  unsafeFromFullUnboxVect :: Vector (Scalar (u, v)) -> (u, v)
unsafeFromFullUnboxVect Vector (Scalar (u, v))
uv = (u
u,v
v)
   where u :: u
u = Vector (Scalar u) -> u
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect Vector (Scalar u)
Vector (Scalar (u, v))
uv
         v :: v
v = Vector (Scalar v) -> v
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect (Vector (Scalar v) -> v) -> Vector (Scalar v) -> v
forall a b. (a -> b) -> a -> b
$ Int -> Vector (Scalar v) -> Vector (Scalar v)
forall a. Unbox a => Int -> Vector a -> Vector a
UArr.drop Int
du Vector (Scalar v)
Vector (Scalar (u, v))
uv
         du :: Int
du = [u] -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension [u
u]

instance Num s => FiniteFreeSpace (V0 s) where
  freeDimension :: p (V0 s) -> Int
freeDimension p (V0 s)
_ = Int
0
  toFullUnboxVect :: V0 s -> Vector (Scalar (V0 s))
toFullUnboxVect V0 s
_ = Vector (Scalar (V0 s))
forall a. Unbox a => Vector a
UArr.empty
  unsafeFromFullUnboxVect :: Vector (Scalar (V0 s)) -> V0 s
unsafeFromFullUnboxVect Vector (Scalar (V0 s))
_ = V0 s
forall v. AdditiveGroup v => v
zeroV
instance Num s => FiniteFreeSpace (V1 s) where
  freeDimension :: p (V1 s) -> Int
freeDimension p (V1 s)
_ = Int
1
  toFullUnboxVect :: V1 s -> Vector (Scalar (V1 s))
toFullUnboxVect (V1 s
n) = s -> Vector s
forall a. Unbox a => a -> Vector a
UArr.singleton s
n
  unsafeFromFullUnboxVect :: Vector (Scalar (V1 s)) -> V1 s
unsafeFromFullUnboxVect Vector (Scalar (V1 s))
v = s -> V1 s
forall a. a -> V1 a
V1 (s -> V1 s) -> s -> V1 s
forall a b. (a -> b) -> a -> b
$ Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V1 s))
v Int
0
instance Num s => FiniteFreeSpace (V2 s) where
  freeDimension :: p (V2 s) -> Int
freeDimension p (V2 s)
_ = Int
2
  toFullUnboxVect :: V2 s -> Vector (Scalar (V2 s))
toFullUnboxVect (V2 s
x s
y) = Int -> [s] -> Vector s
forall a. Unbox a => Int -> [a] -> Vector a
UArr.fromListN Int
2 [s
x,s
y]
  unsafeFromFullUnboxVect :: Vector (Scalar (V2 s)) -> V2 s
unsafeFromFullUnboxVect Vector (Scalar (V2 s))
v = s -> s -> V2 s
forall a. a -> a -> V2 a
V2 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V2 s))
v Int
0)
                                 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V2 s))
v Int
1)
instance Num s => FiniteFreeSpace (V3 s) where
  freeDimension :: p (V3 s) -> Int
freeDimension p (V3 s)
_ = Int
3
  toFullUnboxVect :: V3 s -> Vector (Scalar (V3 s))
toFullUnboxVect (V3 s
x s
y s
z) = Int -> [s] -> Vector s
forall a. Unbox a => Int -> [a] -> Vector a
UArr.fromListN Int
3 [s
x,s
y,s
z]
  unsafeFromFullUnboxVect :: Vector (Scalar (V3 s)) -> V3 s
unsafeFromFullUnboxVect Vector (Scalar (V3 s))
v = s -> s -> s -> V3 s
forall a. a -> a -> a -> V3 a
V3 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V3 s))
v Int
0)
                                 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V3 s))
v Int
1)
                                 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V3 s))
v Int
2)
instance Num s => FiniteFreeSpace (V4 s) where
  freeDimension :: p (V4 s) -> Int
freeDimension p (V4 s)
_ = Int
4
  toFullUnboxVect :: V4 s -> Vector (Scalar (V4 s))
toFullUnboxVect (V4 s
x s
y s
z s
w) = Int -> [s] -> Vector s
forall a. Unbox a => Int -> [a] -> Vector a
UArr.fromListN Int
3 [s
x,s
y,s
z,s
w]
  unsafeFromFullUnboxVect :: Vector (Scalar (V4 s)) -> V4 s
unsafeFromFullUnboxVect Vector (Scalar (V4 s))
v = s -> s -> s -> s -> V4 s
forall a. a -> a -> a -> a -> V4 a
V4 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V4 s))
v Int
0)
                                 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V4 s))
v Int
1)
                                 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V4 s))
v Int
2)
                                 (Vector s -> Int -> s
forall a. Unbox a => Vector a -> Int -> a
UArr.unsafeIndex Vector s
Vector (Scalar (V4 s))
v Int
3)





instance FiniteFreeSpace a => FiniteFreeSpace (Gnrx.Rec0 a s) where
  freeDimension :: p (Rec0 a s) -> Int
freeDimension = p a -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension (p a -> Int) -> (p (Rec0 a s) -> p a) -> p (Rec0 a s) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rec0 a s -> a) -> p (Rec0 a s) -> p a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rec0 a s -> a
forall i c k (p :: k). K1 i c p -> c
Gnrx.unK1
  toFullUnboxVect :: Rec0 a s -> Vector (Scalar (Rec0 a s))
toFullUnboxVect = a -> Vector (Scalar a)
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
v -> Vector (Scalar v)
toFullUnboxVect (a -> Vector (Scalar a))
-> (Rec0 a s -> a) -> Rec0 a s -> Vector (Scalar a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec0 a s -> a
forall i c k (p :: k). K1 i c p -> c
Gnrx.unK1
  unsafeFromFullUnboxVect :: Vector (Scalar (Rec0 a s)) -> Rec0 a s
unsafeFromFullUnboxVect = a -> Rec0 a s
forall k i c (p :: k). c -> K1 i c p
Gnrx.K1 (a -> Rec0 a s)
-> (Vector (Scalar a) -> a) -> Vector (Scalar a) -> Rec0 a s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Scalar a) -> a
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect
  fromUnboxVect :: Vector (Scalar (Rec0 a s)) -> Rec0 a s
fromUnboxVect = a -> Rec0 a s
forall k i c (p :: k). c -> K1 i c p
Gnrx.K1 (a -> Rec0 a s)
-> (Vector (Scalar a) -> a) -> Vector (Scalar a) -> Rec0 a s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Scalar a) -> a
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
fromUnboxVect
instance FiniteFreeSpace (f p) => FiniteFreeSpace (Gnrx.M1 i c f p) where
  freeDimension :: p (M1 i c f p) -> Int
freeDimension = p (f p) -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension (p (f p) -> Int)
-> (p (M1 i c f p) -> p (f p)) -> p (M1 i c f p) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (M1 i c f p -> f p) -> p (M1 i c f p) -> p (f p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap M1 i c f p -> f p
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
Gnrx.unM1
  toFullUnboxVect :: M1 i c f p -> Vector (Scalar (M1 i c f p))
toFullUnboxVect = f p -> Vector (Scalar (f p))
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
v -> Vector (Scalar v)
toFullUnboxVect (f p -> Vector (Scalar (f p)))
-> (M1 i c f p -> f p) -> M1 i c f p -> Vector (Scalar (f p))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c f p -> f p
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
Gnrx.unM1
  unsafeFromFullUnboxVect :: Vector (Scalar (M1 i c f p)) -> M1 i c f p
unsafeFromFullUnboxVect = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
Gnrx.M1 (f p -> M1 i c f p)
-> (Vector (Scalar (f p)) -> f p)
-> Vector (Scalar (f p))
-> M1 i c f p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Scalar (f p)) -> f p
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect
  fromUnboxVect :: Vector (Scalar (M1 i c f p)) -> M1 i c f p
fromUnboxVect = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
Gnrx.M1 (f p -> M1 i c f p)
-> (Vector (Scalar (f p)) -> f p)
-> Vector (Scalar (f p))
-> M1 i c f p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Scalar (f p)) -> f p
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
fromUnboxVect
instance (FiniteFreeSpace (f p), FiniteFreeSpace (g p), Scalar (f p) ~ Scalar (g p))
              => FiniteFreeSpace ((f :*: g) p) where
  freeDimension :: p ((:*:) f g p) -> Int
freeDimension p ((:*:) f g p)
p = p (f p) -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension (((:*:) f g p -> f p) -> p ((:*:) f g p) -> p (f p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(f p
x:*:g p
_)->f p
x) p ((:*:) f g p)
p)
                   Int -> Int -> Int
forall a. Num a => a -> a -> a
+ p (g p) -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension (((:*:) f g p -> g p) -> p ((:*:) f g p) -> p (g p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(f p
_:*:g p
y)->g p
y) p ((:*:) f g p)
p)
  toFullUnboxVect :: (:*:) f g p -> Vector (Scalar ((:*:) f g p))
toFullUnboxVect (f p
u:*:g p
v) = f p -> Vector (Scalar (f p))
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
v -> Vector (Scalar v)
toFullUnboxVect f p
u Vector (Scalar (g p))
-> Vector (Scalar (g p)) -> Vector (Scalar (g p))
forall a. Unbox a => Vector a -> Vector a -> Vector a
UArr.++ g p -> Vector (Scalar (g p))
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
v -> Vector (Scalar v)
toFullUnboxVect g p
v
  unsafeFromFullUnboxVect :: Vector (Scalar ((:*:) f g p)) -> (:*:) f g p
unsafeFromFullUnboxVect Vector (Scalar ((:*:) f g p))
uv = f p
uf p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*:g p
v
   where u :: f p
u = Vector (Scalar (f p)) -> f p
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect Vector (Scalar (f p))
Vector (Scalar ((:*:) f g p))
uv
         v :: g p
v = Vector (Scalar (g p)) -> g p
forall v.
(FiniteFreeSpace v, Unbox (Scalar v)) =>
Vector (Scalar v) -> v
unsafeFromFullUnboxVect (Vector (Scalar (g p)) -> g p) -> Vector (Scalar (g p)) -> g p
forall a b. (a -> b) -> a -> b
$ Int -> Vector (Scalar (g p)) -> Vector (Scalar (g p))
forall a. Unbox a => Int -> Vector a -> Vector a
UArr.drop Int
du Vector (Scalar (g p))
Vector (Scalar ((:*:) f g p))
uv
         du :: Int
du = [f p] -> Int
forall v (p :: * -> *).
(FiniteFreeSpace v, Functor p) =>
p v -> Int
freeDimension [f p
u]




instance OneDimensional a => OneDimensional (Gnrx.Rec0 a s) where
  Gnrx.K1 a
v ^/^ :: Rec0 a s -> Rec0 a s -> Maybe (Scalar (Rec0 a s))
^/^ Gnrx.K1 a
w = a
v a -> a -> Maybe (Scalar a)
forall v. OneDimensional v => v -> v -> Maybe (Scalar v)
^/^ a
w
  Gnrx.K1 a
v ^/! :: Rec0 a s -> Rec0 a s -> Scalar (Rec0 a s)
^/! Gnrx.K1 a
w = a
v a -> a -> Scalar a
forall v. OneDimensional v => v -> v -> Scalar v
^/! a
w
instance OneDimensional (f p) => OneDimensional (Gnrx.M1 i c f p) where
  Gnrx.M1 f p
v ^/^ :: M1 i c f p -> M1 i c f p -> Maybe (Scalar (M1 i c f p))
^/^ Gnrx.M1 f p
w = f p
v f p -> f p -> Maybe (Scalar (f p))
forall v. OneDimensional v => v -> v -> Maybe (Scalar v)
^/^ f p
w
  Gnrx.M1 f p
v ^/! :: M1 i c f p -> M1 i c f p -> Scalar (M1 i c f p)
^/! Gnrx.M1 f p
w = f p
v f p -> f p -> Scalar (f p)
forall v. OneDimensional v => v -> v -> Scalar v
^/! f p
w


type VRep v = Gnrx.Rep v Void