Skip to content

Commit b445434

Browse files
author
Oskar Lundström
committed
Merge branch 'oskar'
2 parents 1d0ed96 + 2557087 commit b445434

File tree

6 files changed

+1037
-6
lines changed

6 files changed

+1037
-6
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
2+
{-# LANGUAGE TypeOperators #-}
3+
{-# LANGUAGE MultiParamTypeClasses #-}
4+
{-# LANGUAGE FlexibleInstances #-}
5+
6+
module Combination.Combination
7+
(
8+
) where
9+
10+
import Vector.Vector2
11+
--import Calculus.FunExpr
12+
--import Calculus.DifferentialCalc
13+
--import Calculus.IntegralCalc
14+
import Dimensions.Quantity2
15+
import Dimensions.TypeLevel
16+
import qualified Dimensions.ValueLevel as V
17+
import Prelude hiding (length)
18+
19+
-- Calculus i Dimensions
20+
------------------------
21+
22+
{-
23+
24+
instance Fractional FunExpr where
25+
fromRational = Const . fromRational
26+
(/) = (:/)
27+
28+
instance Floating FunExpr where
29+
pi = Const pi
30+
exp = (Exp :.)
31+
log = (Log :.)
32+
sin = (Sin :.)
33+
cos = (Cos :.)
34+
asin = (Asin :.)
35+
acos = (Acos :.)
36+
atan = undefined
37+
sinh = undefined
38+
cosh = undefined
39+
asinh = undefined
40+
acosh = undefined
41+
atanh = undefined
42+
43+
-- Ett flygplan med position 5+2t
44+
t1 :: Quantity Length FunExpr
45+
t1 = (Const 5 :+ Const 2 :* Id) # length
46+
47+
-- eller kortare
48+
type QC d = Quantity d FunExpr
49+
50+
t2 :: QC Length
51+
t2 = t1
52+
53+
-- Ett flygplan i turbulens
54+
t3 :: QC Length
55+
t3 = 5 + sin Id # length
56+
57+
-- Ett flygplan som står stilla
58+
t4 :: QC Length
59+
t4 = 8 # length
60+
61+
type Area = Length `Mul` Length
62+
63+
-- Någon slags multiplikation
64+
t5 :: QC Area
65+
t5 = t1 *# t3
66+
67+
t6 :: QC Area
68+
t6 = fmap simplify t5
69+
70+
-- Någon slags division
71+
t7 :: QC Length
72+
t7 = t6 /# t4
73+
74+
-- Går ej, vilket är bra
75+
-- t8 :: QC Area
76+
-- t8 = t6 /# t4
77+
78+
-- Stöd för derivering. Hela QC och dessa
79+
-- bör vara dola för utomstående användare.
80+
81+
-- fmap på Quantity, men inte QC!
82+
83+
differentiateWRTtime :: QC d -> QC (d `Div` Time)
84+
differentiateWRTtime qc = fmap simplify $ fmap derive qc /# time
85+
86+
integrateWRTtime :: QC d -> QC (d `Mul` Time)
87+
integrateWRTtime qc = fmap simplify $ fmap integrate qc *# time
88+
89+
-}
90+
91+
92+
--------------------
93+
-- Calc i Vec i Quan
94+
--------------------
95+
96+
-----------------------
97+
-- Instansiering
98+
99+
-- En vektor kan adderas
100+
101+
-- (Kan alla göras samtidigt?)
102+
103+
instance (Addable v v v) => Addable (Vector2 v) (Vector2 v) (Vector2 v) where
104+
doAdd = vzipWith (doAdd)
105+
instance (Addable v v v) => Addable (Vector3 v) (Vector3 v) (Vector3 v) where
106+
doAdd = vzipWith (doAdd)
107+
108+
-- En vektor kan multipliceras på flera sätt
109+
110+
-- Skalning (från vänster)
111+
instance (Num v) => Multiplicable v (Vector2 v) (Vector2 v) where
112+
doMult = scale
113+
instance (Num v) => Multiplicable v (Vector3 v) (Vector3 v) where
114+
doMult = scale
115+
116+
-- Kryssprdoukt
117+
instance (Num v) => Multiplicable (Vector3 v) (Vector3 v) (Vector3 v) where
118+
doMult = crossProd
119+
120+
-- Skalärprodukt
121+
instance (Num v) => Multiplicable (Vector2 v) (Vector2 v) v where
122+
doMult = dotProd
123+
instance (Num v) => Multiplicable (Vector3 v) (Vector3 v) v where
124+
doMult = dotProd
125+
126+
-- En vektor kan "skapas"
127+
128+
instance (Creatable v) => Creatable (Vector2 v) where
129+
anyVal = V2 anyVal anyVal
130+
instance (Creatable v) => Creatable (Vector3 v) where
131+
anyVal = V3 anyVal anyVal anyVal
132+
133+
------------------------------------
134+
-- Användning
135+
136+
-- Ej dimensionssäkra
137+
v1 :: Vector3 Double
138+
v1 = V3 3 2 3
139+
v2 :: Vector3 Double
140+
v2 = V3 1 2 5
141+
v3 :: Vector3 Double
142+
v3 = V3 7 8 2
143+
144+
-- Dimensionsäkra
145+
v1d :: Quantity Length (Vector3 Double)
146+
v1d = v1 ## length
147+
v2d :: Quantity Mass (Vector3 Double)
148+
v2d = v2 ## mass
149+
v3d :: Quantity Time (Vector3 Double)
150+
v3d = v3 ## time
151+
152+
-- t1 kräver typsignatur, antagligen för den här MultiParam...
153+
-- så att ska veta vilken instans
154+
155+
-- Addition
156+
t1 :: Quantity Length (Vector3 Double)
157+
t1 = v1d +# v1d
158+
159+
-- Kryssprodukt
160+
t2 :: Quantity (Length `Mul` Mass) (Vector3 Double)
161+
t2 = v1d *# v2d
162+
163+
-- Skalning
164+
t3 :: Quantity (Length `Mul` Mass) (Vector3 Double)
165+
t3 = s *# v2d
166+
where
167+
s :: Quantity Length Double
168+
s = 3.0 ## length
169+
170+
-- Skalärprodukt
171+
t4 :: Quantity (Time `Mul` Length) Double
172+
t4 = v3d *# v1d
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+
210+
211+
212+
213+
214+
215+
216+
217+
218+
219+
220+
221+
222+
223+
224+
225+
226+
227+

Physics/src/Combination/MWE.hs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
{-# LANGUAGE UndecidableInstances #-}
3+
{-# LANGUAGE FlexibleInstances #-}
4+
{-# LANGUAGE GADTs #-}
5+
{-# LANGUAGE DataKinds #-}
6+
{-# LANGUAGE TypeOperators #-}
7+
{-# LANGUAGE KindSignatures #-}
8+
9+
{-# LANGUAGE MultiParamTypeClasses #-}
10+
11+
module Combination.MWE where
12+
13+
class Addable a b c where
14+
doAdd :: a -> b -> c
15+
16+
addera :: (Addable a b c) => a -> b -> c
17+
addera = doAdd
18+
19+
--instance (Addable Double Double Double) where
20+
-- doAdd = (+)
21+
22+
instance (Num v) => Addable v v v where
23+
doAdd = (+)
24+
25+
data Container (d :: *) (v :: *) where
26+
ValContainer :: v -> Container d v
27+
28+
addCon :: (Addable v v v) => Container d v -> Container d v -> Container d v
29+
addCon (ValContainer x) (ValContainer y) = ValContainer $ doAdd x y
30+
31+
v1 :: Container Bool Double
32+
v1 = ValContainer 2.0
33+
v2 :: Container Bool Double
34+
v2 = ValContainer 3.0
35+
36+
containerAdd :: (Addable a b c) => Container d a ->
37+
Container d b ->
38+
Container d c
39+
containerAdd (ValContainer a) (ValContainer b) = ValContainer (doAdd a b)
40+
41+
v3 :: Container Bool Double
42+
v3 = containerAdd v1 v2
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+

Physics/src/Dimensions/Quantity/Test.lhs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ Testing of Quantity
1616
}
1717

1818
> module Dimensions.Quantity.Test
19-
> ( runTests
20-
> )
21-
> where
19+
> ( runTests
20+
> ) where
2221

2322

2423
< {-# LANGUAGE DataKinds #-}

0 commit comments

Comments
 (0)