Respect Number laws

This commit is contained in:
Yann Esposito (Yogsototh) 2012-06-25 11:13:44 +02:00
parent 51ca9d211b
commit b170948aeb
3 changed files with 33 additions and 25 deletions

View file

@ -65,7 +65,7 @@ An extension of complex numbers with a third component:
> fromInteger n = C (fromIntegral n, 0, 0) > fromInteger n = C (fromIntegral n, 0, 0)
> C (x,y,z) + C (x',y',z') = C (x+x', y+y', z+z') > C (x,y,z) + C (x',y',z') = C (x+x', y+y', z+z')
> abs (C (x,y,z)) = C (sqrt (x*x + y*y + z*z), 0, 0) > abs (C (x,y,z)) = C (sqrt (x*x + y*y + z*z), 0, 0)
> signum (C (x,y,z)) = C (signum x, 0, 0) > signum (C (x,y,z)) = C (signum x, signum y, signum z)
The most important part is the new multiplication instance. The most important part is the new multiplication instance.
Modifying this formula will change radically the shape of the result. Modifying this formula will change radically the shape of the result.

View file

@ -2,32 +2,36 @@ module ExtComplex where
import Graphics.Rendering.OpenGL import Graphics.Rendering.OpenGL
data ExtComplex = C (GLfloat,GLfloat,GLfloat) -- This time I use unpacked strict data type
-- Far faster when compiled.
data ExtComplex = C {-# UNPACK #-} !GLfloat
{-# UNPACK #-} !GLfloat
{-# UNPACK #-} !GLfloat
deriving (Show,Eq) deriving (Show,Eq)
instance Num ExtComplex where instance Num ExtComplex where
-- The shape of the 3D mandelbrot -- The shape of the 3D mandelbrot
-- will depend on this formula -- will depend on this formula
C (x,y,z) * C (x',y',z') = C (x*x' - y*y' - z*z', (C x y z) * (C x' y' z') = C (x*x' - y*y' - z*z')
x*y' + y*x' + z*z', (x*y' + y*x' + z*z')
x*z' + z*x' ) (x*z' + z*x' )
-- The rest is straightforward -- The rest is straightforward
fromInteger n = C (fromIntegral n, 0, 0) fromInteger n = C (fromIntegral n) 0 0
C (x,y,z) + C (x',y',z') = C (x+x', y+y', z+z') (C x y z) + (C x' y' z') = C (x+x') (y+y') (z+z')
abs (C (x,y,z)) = C (sqrt (x*x + y*y + z*z), 0, 0) abs (C x y z) = C (sqrt (x*x + y*y + z*z)) 0 0
signum (C (x,y,z)) = C (signum x, 0, 0) signum (C x y z) = C (signum x) (signum y) (signum z)
extcomplex :: GLfloat -> GLfloat -> GLfloat -> ExtComplex extcomplex :: GLfloat -> GLfloat -> GLfloat -> ExtComplex
extcomplex x y z = C (x,y,z) extcomplex x y z = C x y z
real :: ExtComplex -> GLfloat real :: ExtComplex -> GLfloat
real (C (x,y,z)) = x real (C x _ _) = x
im :: ExtComplex -> GLfloat im :: ExtComplex -> GLfloat
im (C (x,y,z)) = y im (C _ y _) = y
strange :: ExtComplex -> GLfloat strange :: ExtComplex -> GLfloat
strange (C (x,y,z)) = z strange (C _ _ z) = z
magnitude :: ExtComplex -> GLfloat magnitude :: ExtComplex -> GLfloat
magnitude = real.abs magnitude = real.abs

View file

@ -2,32 +2,36 @@ module ExtComplex where
import Graphics.Rendering.OpenGL import Graphics.Rendering.OpenGL
data ExtComplex = C (GLfloat,GLfloat,GLfloat) -- This time I use unpacked strict data type
-- Far faster when compiled.
data ExtComplex = C {-# UNPACK #-} !GLfloat
{-# UNPACK #-} !GLfloat
{-# UNPACK #-} !GLfloat
deriving (Show,Eq) deriving (Show,Eq)
instance Num ExtComplex where instance Num ExtComplex where
-- The shape of the 3D mandelbrot -- The shape of the 3D mandelbrot
-- will depend on this formula -- will depend on this formula
C (x,y,z) * C (x',y',z') = C (x*x' - y*y' - z*z', (C x y z) * (C x' y' z') = C (x*x' - y*y' - z*z')
x*y' + y*x' + z*z', (x*y' + y*x' + z*z')
x*z' + z*x' ) (x*z' + z*x' )
-- The rest is straightforward -- The rest is straightforward
fromInteger n = C (fromIntegral n, 0, 0) fromInteger n = C (fromIntegral n) 0 0
C (x,y,z) + C (x',y',z') = C (x+x', y+y', z+z') (C x y z) + (C x' y' z') = C (x+x') (y+y') (z+z')
abs (C (x,y,z)) = C (sqrt (x*x + y*y + z*z), 0, 0) abs (C x y z) = C (sqrt (x*x + y*y + z*z)) 0 0
signum (C (x,y,z)) = C (signum x, 0, 0) signum (C x y z) = C (signum x) (signum y) (signum z)
extcomplex :: GLfloat -> GLfloat -> GLfloat -> ExtComplex extcomplex :: GLfloat -> GLfloat -> GLfloat -> ExtComplex
extcomplex x y z = C (x,y,z) extcomplex x y z = C x y z
real :: ExtComplex -> GLfloat real :: ExtComplex -> GLfloat
real (C (x,y,z)) = x real (C x _ _) = x
im :: ExtComplex -> GLfloat im :: ExtComplex -> GLfloat
im (C (x,y,z)) = y im (C _ y _) = y
strange :: ExtComplex -> GLfloat strange :: ExtComplex -> GLfloat
strange (C (x,y,z)) = z strange (C _ _ z) = z
magnitude :: ExtComplex -> GLfloat magnitude :: ExtComplex -> GLfloat
magnitude = real.abs magnitude = real.abs