2014-12-17 04:11:18 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
|
|
|
|
|
|
|
|
module Test.Vector where
|
|
|
|
|
|
|
|
import Algebra.Vector
|
|
|
|
import Control.Applicative
|
2014-12-17 18:37:28 +00:00
|
|
|
import Control.Arrow
|
2014-12-17 04:11:18 +00:00
|
|
|
{- import Control.Monad -}
|
2014-12-17 23:47:51 +00:00
|
|
|
import Diagrams.Coordinates
|
|
|
|
import Diagrams.Points
|
2014-12-17 04:11:18 +00:00
|
|
|
import Diagrams.TwoD.Types
|
|
|
|
import Test.QuickCheck
|
|
|
|
|
|
|
|
|
2014-12-17 22:21:26 +00:00
|
|
|
newtype RoundDouble = RoundDouble { getRD :: Double }
|
|
|
|
deriving (Eq, Ord, Show, Read)
|
|
|
|
|
|
|
|
|
|
|
|
newtype PosRoundDouble = PosRoundDouble { getPRD :: Double }
|
|
|
|
deriving (Eq, Ord, Show, Read)
|
|
|
|
|
|
|
|
|
|
|
|
newtype RoundR2 = RoundR2 { getRR2 :: R2 }
|
|
|
|
deriving (Eq, Ord, Show, Read)
|
|
|
|
|
|
|
|
|
|
|
|
newtype PosRoundR2 = PosRoundR2 { getPRR2 :: R2 }
|
|
|
|
deriving (Eq, Ord, Show, Read)
|
|
|
|
|
|
|
|
|
2014-12-17 23:47:51 +00:00
|
|
|
newtype RoundP2 = RoundP2 { getRP2 :: P2 }
|
|
|
|
deriving (Eq, Ord, Show, Read)
|
|
|
|
|
|
|
|
|
|
|
|
newtype PosRoundP2 = PosRoundP2 { getPRP2 :: P2 }
|
|
|
|
deriving (Eq, Ord, Show, Read)
|
|
|
|
|
|
|
|
|
2014-12-17 22:21:26 +00:00
|
|
|
instance Arbitrary RoundDouble where
|
|
|
|
arbitrary = RoundDouble <$> fromIntegral <$> (arbitrary :: Gen Int)
|
|
|
|
|
|
|
|
|
|
|
|
instance Arbitrary PosRoundDouble where
|
|
|
|
arbitrary = PosRoundDouble
|
|
|
|
<$> fromIntegral
|
|
|
|
-- (maxBound :: Int) instead of 100000 generates doubles
|
|
|
|
<$> (choose (1, 10000) :: Gen Int)
|
|
|
|
|
|
|
|
|
|
|
|
instance Arbitrary RoundR2 where
|
|
|
|
arbitrary = curry (RoundR2 . r2 . (getRD *** getRD))
|
|
|
|
<$> (arbitrary :: Gen RoundDouble)
|
|
|
|
<*> (arbitrary :: Gen RoundDouble)
|
|
|
|
|
|
|
|
|
|
|
|
instance Arbitrary PosRoundR2 where
|
|
|
|
arbitrary = curry (PosRoundR2 . r2 . (getPRD *** getPRD))
|
|
|
|
<$> (arbitrary :: Gen PosRoundDouble)
|
|
|
|
<*> (arbitrary :: Gen PosRoundDouble)
|
|
|
|
|
|
|
|
|
2014-12-17 23:47:51 +00:00
|
|
|
instance Arbitrary RoundP2 where
|
|
|
|
arbitrary = curry (RoundP2 . p2 . (getRD *** getRD))
|
|
|
|
<$> (arbitrary :: Gen RoundDouble)
|
|
|
|
<*> (arbitrary :: Gen RoundDouble)
|
|
|
|
|
|
|
|
|
|
|
|
instance Arbitrary PosRoundP2 where
|
|
|
|
arbitrary = curry (PosRoundP2 . p2 . (getPRD *** getPRD))
|
|
|
|
<$> (arbitrary :: Gen PosRoundDouble)
|
|
|
|
<*> (arbitrary :: Gen PosRoundDouble)
|
|
|
|
|
|
|
|
|
2014-12-17 04:11:18 +00:00
|
|
|
instance Arbitrary R2 where
|
|
|
|
arbitrary = curry r2 <$> arbitrary <*> arbitrary
|
|
|
|
|
|
|
|
|
|
|
|
instance Arbitrary P2 where
|
|
|
|
arbitrary = curry p2 <$> arbitrary <*> arbitrary
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- the point describing the lower left corner of the square
|
|
|
|
-- must be part of the square
|
2014-12-17 04:11:18 +00:00
|
|
|
inRangeProp1 :: Square -> Bool
|
|
|
|
inRangeProp1 sq@((x1, y1), _) =
|
|
|
|
inRange sq (p2 (x1, y1))
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- the point describing the upper right corner of the square
|
|
|
|
-- must be part of the square
|
2014-12-17 04:11:18 +00:00
|
|
|
inRangeProp2 :: Square -> Bool
|
|
|
|
inRangeProp2 sq@(_, (x2, y2)) =
|
|
|
|
inRange sq (p2 (x2, y2))
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- the point describing the upper left corner of the square
|
|
|
|
-- must be part of the square
|
2014-12-17 04:11:18 +00:00
|
|
|
inRangeProp3 :: Square -> Bool
|
|
|
|
inRangeProp3 sq@((x1, _), (_, y2)) =
|
|
|
|
inRange sq (p2 (x1, y2))
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- the point describing the lower right corner of the square
|
|
|
|
-- must be part of the square
|
2014-12-17 04:11:18 +00:00
|
|
|
inRangeProp4 :: Square -> Bool
|
|
|
|
inRangeProp4 sq@((_, y1), (x2, _)) =
|
|
|
|
inRange sq (p2 (x2, y1))
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- generating random points within the square
|
2014-12-17 04:15:03 +00:00
|
|
|
inRangeProp5 :: Square -> Positive Double -> Positive Double -> Bool
|
|
|
|
inRangeProp5 sq@((x1, y1), (x2, y2)) (Positive a) (Positive b) =
|
|
|
|
inRange sq (p2 (x1 + ((x2 - x1) / (a + 1)), y1 + ((y2 - y1) / (b + 1))))
|
2014-12-17 04:11:18 +00:00
|
|
|
|
|
|
|
|
2014-12-18 00:06:16 +00:00
|
|
|
-- generating random points outside of the square
|
|
|
|
inRangeProp6 :: Square -> Positive Double -> Positive Double -> Bool
|
|
|
|
inRangeProp6 sq@((x1, y1), (x2, y2)) (Positive a) (Positive b) =
|
|
|
|
(not . inRange sq $ p2 (max x1 x2 + (a + 1), max y1 y2 + (b + 1)))
|
|
|
|
&& (not . inRange sq $ p2 (max x1 x2 + (a + 1), max y1 y2 - (b + 1)))
|
|
|
|
&& (not . inRange sq $ p2 (max x1 x2 - (a + 1), max y1 y2 + (b + 1)))
|
|
|
|
&& (not . inRange sq $ p2 (min x1 x2 - (a + 1), min y1 y2 - (b + 1)))
|
|
|
|
&& (not . inRange sq $ p2 (min x1 x2 + (a + 1), min y1 y2 - (b + 1)))
|
|
|
|
&& (not . inRange sq $ p2 (min x1 x2 - (a + 1), min y1 y2 + (b + 1)))
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- apply id function on the point
|
2014-12-17 04:11:18 +00:00
|
|
|
onPTProp1 :: PT -> Bool
|
|
|
|
onPTProp1 pt = onPT id pt == pt
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- add a random value to the point coordinates
|
2014-12-17 04:11:18 +00:00
|
|
|
onPTProp2 :: PT -> Positive R2 -> Bool
|
|
|
|
onPTProp2 pt (Positive (R2 rx ry))
|
|
|
|
= onPT (\(x, y) -> (x + rx, y + ry)) pt /= pt
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- angle between two vectors both on the x-axis must be 0
|
2014-12-17 04:11:18 +00:00
|
|
|
getAngleProp1 :: Positive Vec -> Positive Vec -> Bool
|
|
|
|
getAngleProp1 (Positive (R2 x1 _)) (Positive (R2 x2 _))
|
|
|
|
= getAngle (R2 x1 0) (R2 x2 0) == 0
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- angle between two vectors both on the y-axis must be 0
|
2014-12-17 04:11:18 +00:00
|
|
|
getAngleProp2 :: Positive Vec -> Positive Vec -> Bool
|
|
|
|
getAngleProp2 (Positive (R2 _ y1)) (Positive (R2 _ y2))
|
|
|
|
= getAngle (R2 0 y1) (R2 0 y2) == 0
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- angle between two vectors both on the x-axis but with opposite direction
|
|
|
|
-- must be pi
|
2014-12-17 04:11:18 +00:00
|
|
|
getAngleProp3 :: Positive Vec -> Positive Vec -> Bool
|
|
|
|
getAngleProp3 (Positive (R2 x1 _)) (Positive (R2 x2 _))
|
|
|
|
= getAngle (R2 (negate x1) 0) (R2 x2 0) == pi
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- angle between two vectors both on the y-axis but with opposite direction
|
|
|
|
-- must be pi
|
2014-12-17 04:11:18 +00:00
|
|
|
getAngleProp4 :: Positive Vec -> Positive Vec -> Bool
|
|
|
|
getAngleProp4 (Positive (R2 _ y1)) (Positive (R2 _ y2))
|
|
|
|
= getAngle (R2 0 (negate y1)) (R2 0 y2) == pi
|
|
|
|
|
|
|
|
|
2014-12-17 21:26:40 +00:00
|
|
|
-- angle between vector in x-axis direction and y-axis direction must be
|
|
|
|
-- p/2
|
2014-12-17 04:11:18 +00:00
|
|
|
getAngleProp5 :: Positive Vec -> Positive Vec -> Bool
|
|
|
|
getAngleProp5 (Positive (R2 x1 _)) (Positive (R2 _ y2))
|
|
|
|
= getAngle (R2 x1 0) (R2 0 y2) == pi / 2
|
|
|
|
|
|
|
|
|
2014-12-17 21:27:29 +00:00
|
|
|
-- commutative
|
2014-12-17 04:11:18 +00:00
|
|
|
getAngleProp6 :: Positive Vec -> Positive Vec -> Bool
|
2014-12-17 21:27:29 +00:00
|
|
|
getAngleProp6 (Positive v1) (Positive v2)
|
|
|
|
= getAngle v1 v2 == getAngle v2 v1
|
2014-12-17 18:37:28 +00:00
|
|
|
|
|
|
|
|
2014-12-17 22:21:48 +00:00
|
|
|
-- Angle between two identical vectors. We can't check against 0
|
|
|
|
-- because of sqrt in vecLength.
|
|
|
|
getAngleProp7 :: PosRoundR2 -> Bool
|
|
|
|
getAngleProp7 (PosRoundR2 v)
|
|
|
|
= getAngle v v < 0.0001 || isNaN (getAngle v v)
|
|
|
|
|
|
|
|
|
2014-12-17 18:37:28 +00:00
|
|
|
-- commutative
|
|
|
|
scalarProdProp1 :: Vec -> Vec -> Bool
|
|
|
|
scalarProdProp1 v1 v2 = v1 `scalarProd` v2 == v2 `scalarProd` v1
|
|
|
|
|
|
|
|
|
|
|
|
-- distributive, avoid doubles as we get messed up precision
|
2014-12-17 22:21:26 +00:00
|
|
|
scalarProdProp2 :: RoundR2 -> RoundR2 -> RoundR2 -> Bool
|
|
|
|
scalarProdProp2 (RoundR2 v1) (RoundR2 v2) (RoundR2 v3) =
|
|
|
|
v1 `scalarProd` (v2 + v3)
|
2014-12-17 18:37:28 +00:00
|
|
|
==
|
2014-12-17 22:21:26 +00:00
|
|
|
(v1 `scalarProd` v2) + (v1 `scalarProd` v3)
|
2014-12-17 18:37:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- bilinear, avoid doubles as we get messed up precision
|
2014-12-17 22:21:26 +00:00
|
|
|
scalarProdProp3 :: RoundDouble -> RoundR2 -> RoundR2 -> RoundR2 -> Bool
|
|
|
|
scalarProdProp3 (RoundDouble r) (RoundR2 v1) (RoundR2 v2) (RoundR2 v3) =
|
|
|
|
v1 `scalarProd` (scalarMul r v2 + v3)
|
2014-12-17 18:37:28 +00:00
|
|
|
==
|
2014-12-17 22:21:26 +00:00
|
|
|
r * (v1 `scalarProd` v2) + (v1 `scalarProd` v3)
|
2014-12-17 18:37:28 +00:00
|
|
|
|
2014-12-17 18:53:03 +00:00
|
|
|
|
|
|
|
-- scalar multiplication
|
2014-12-17 22:21:26 +00:00
|
|
|
scalarProdProp4 :: RoundDouble -> RoundDouble -> RoundR2 -> RoundR2 -> Bool
|
|
|
|
scalarProdProp4 (RoundDouble s1) (RoundDouble s2) (RoundR2 v1) (RoundR2 v2)
|
|
|
|
= scalarMul s1 v1 `scalarProd` scalarMul s2 v2
|
2014-12-17 18:53:03 +00:00
|
|
|
==
|
2014-12-17 22:21:26 +00:00
|
|
|
s1 * s2 * (v1 `scalarProd` v2)
|
2014-12-17 18:53:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- orthogonal
|
|
|
|
scalarProdProp5 :: Positive Vec -> Positive Vec -> Bool
|
|
|
|
scalarProdProp5 (Positive (R2 x1 _)) (Positive (R2 _ y2))
|
|
|
|
= scalarProd (R2 x1 0) (R2 0 y2) == 0
|
2014-12-17 23:47:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- this is almost the same as the function definition
|
|
|
|
dimToSquareProp1 :: (Double, Double) -> (Double, Double) -> Bool
|
|
|
|
dimToSquareProp1 (x1, x2) (y1, y2) =
|
|
|
|
((x1, y1), (x2, y2)) == dimToSquare (x1, x2) (y1, y2)
|
|
|
|
|
|
|
|
|
|
|
|
-- multiply scalar with result of vecLength or with the vector itself...
|
|
|
|
-- both results must be the same. We can't check against 0
|
|
|
|
-- because of sqrt in vecLength.
|
|
|
|
vecLengthProp1 :: PosRoundDouble -> Vec -> Bool
|
|
|
|
vecLengthProp1 (PosRoundDouble r) v
|
|
|
|
= abs (vecLength v * r - vecLength (scalarMul r v)) < 0.0001
|
|
|
|
|
|
|
|
|
|
|
|
-- convert to vector and back again
|
|
|
|
pt2VecProp1 :: PT -> Bool
|
|
|
|
pt2VecProp1 pt = (vec2Pt . pt2Vec $ pt) == pt
|
|
|
|
|
|
|
|
|
|
|
|
-- unbox coordinates and check if equal
|
|
|
|
pt2VecProp2 :: PT -> Bool
|
|
|
|
pt2VecProp2 pt = (unr2 . pt2Vec $ pt) == unp2 pt
|
|
|
|
|
|
|
|
|
|
|
|
-- convert to point and back again
|
|
|
|
vec2PtProp1 :: Vec -> Bool
|
|
|
|
vec2PtProp1 v = (pt2Vec . vec2Pt $ v) == v
|
|
|
|
|
|
|
|
|
|
|
|
-- unbox coordinates and check if equal
|
|
|
|
vec2PtProp2 :: Vec -> Bool
|
|
|
|
vec2PtProp2 v = (unp2 . vec2Pt $ v) == unr2 v
|
|
|
|
|
|
|
|
|
|
|
|
-- vector from a to b must not be the same as b to a
|
|
|
|
vp2Prop1 :: PT -> PT -> Bool
|
|
|
|
vp2Prop1 p1' p2'
|
|
|
|
| p1' == origin && p2' == origin = True
|
|
|
|
| otherwise = vp2 p1' p2' /= vp2 p2' p1'
|
|
|
|
|
|
|
|
|
|
|
|
-- negating vector from a to be must be the same as vector b to a
|
|
|
|
vp2Prop2 :: PT -> PT -> Bool
|
|
|
|
vp2Prop2 p1' p2'
|
|
|
|
| p1' == origin && p2' == origin = True
|
|
|
|
| otherwise = vp2 p1' p2' == (\(R2 x y) -> negate x ^& negate y)
|
|
|
|
(vp2 p2' p1')
|
|
|
|
&&
|
|
|
|
vp2 p2' p1' == (\(R2 x y) -> negate x ^& negate y)
|
|
|
|
(vp2 p1' p2')
|
|
|
|
|
|
|
|
|
|
|
|
-- determinant of the 3 same points is always 0
|
|
|
|
detProp1 :: PT -> Bool
|
|
|
|
detProp1 pt' = det pt' pt' pt' == 0
|