Skip to content

Commit 2557087

Browse files
author
Oskar Lundström
committed
Merge branch 'master' into oskar
2 parents 3bc502a + 1d0ed96 commit 2557087

File tree

23 files changed

+950
-1007
lines changed

23 files changed

+950
-1007
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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 Dimensions.QuantityExtended where
12+
13+
import qualified Dimensions.ValueLevel as V
14+
import Dimensions.TypeLevel as T
15+
import Prelude as P hiding (length)
16+
17+
----------------------------------------
18+
-- Än så länge inget nytt
19+
----------------------------------------
20+
21+
data Quantity (d :: T.Dim) (v :: *) where
22+
ValQuantity :: V.Dim -> v -> Quantity d v
23+
24+
showQuantity :: (Show v) => Quantity d v -> String
25+
showQuantity (ValQuantity d v) = show v ++ " " ++ show d
26+
27+
instance (Show v) => Show (Quantity d v) where
28+
show = showQuantity
29+
30+
instance (Eq v) => Eq (Quantity d v) where
31+
(ValQuantity _ v1) == (ValQuantity _ v2) = v1 == v2
32+
33+
instance (Ord v) => Ord (Quantity d v) where
34+
(ValQuantity _ v1) `compare` (ValQuantity _ v2) = v1 `compare` v2
35+
36+
instance Functor (Quantity d) where
37+
fmap f (ValQuantity d v) = ValQuantity d (f v)
38+
39+
----------------------------------------
40+
-- Socker
41+
----------------------------------------
42+
43+
infixl 3 ##
44+
(##) :: v -> Quantity d w -> Quantity d v
45+
v ## (ValQuantity d _) = ValQuantity d v
46+
47+
-- Dummy-värden med matchande värde/typ-nivå dimensioner
48+
-- med en dummy-typ.
49+
50+
length :: Quantity Length Double
51+
length = ValQuantity V.length 1.0
52+
mass :: Quantity Mass Double
53+
mass = ValQuantity V.mass 1.0
54+
time :: Quantity Time Double
55+
time = ValQuantity V.time 1.0
56+
57+
-- Med `##` kan en Quantity med vilken värdetyp som helst skapas
58+
-- med valfri dimension av ovanstående.
59+
60+
-- Om värdetypen ej stöder multiplikation och division kan
61+
-- dessa dummy-värden ändå göras så på, och därför kan man
62+
-- alltid få valfri dimension.
63+
64+
----------------------------------------
65+
-- Aritmetik
66+
----------------------------------------
67+
68+
-- En `Quantity` innehåller något av någon typ. Om och hur addition
69+
-- o.s.v. ser ut för den kan variera, så typen själv ska sköta det.
70+
-- Dessutom kan det var multiplikation mellan olika typer.
71+
72+
class Addable a b c where
73+
doAdd :: a -> b -> c
74+
75+
(+#) :: (Addable a b c) => Quantity d a ->
76+
Quantity d b ->
77+
Quantity d c
78+
(ValQuantity d a) +# (ValQuantity _ b) = ValQuantity d $ doAdd a b
79+
80+
-- Nedan går ej! Blir problem med Vector då. Vet ej varför.
81+
82+
-- Allt "numeriskt" är adderbart
83+
--instance (Num v) => Addable v v v where
84+
-- doAdd = (+)
85+
86+
----------
87+
88+
class Subable a b c where
89+
doSub :: a -> b -> c
90+
91+
(-#) :: (Subable a b c) => Quantity d a ->
92+
Quantity d b ->
93+
Quantity d c
94+
(ValQuantity d a) -# (ValQuantity _ b) = ValQuantity d $ doSub a b
95+
96+
--instance (Num v) => Subable v v v where
97+
-- doSub = (-)
98+
99+
----------
100+
101+
class Multiplicable a b c where
102+
doMul :: a -> b -> c
103+
104+
(*#) :: (Multiplicable a b c) => Quantity d1 a ->
105+
Quantity d2 b ->
106+
Quantity (d1 `Mul` d2) c
107+
(ValQuantity d1 a) *# (ValQuantity d2 b) = ValQuantity (d1 `V.mul` d2) $ doMul a b
108+
109+
--instance (Num v) => Multiplicable v v v where
110+
-- doMul = (*)
111+
112+
----------
113+
114+
class Divisionable a b c where
115+
doDiv :: a -> b -> c
116+
117+
(/#) :: (Divisionable a b c) => Quantity d1 a ->
118+
Quantity d2 b ->
119+
Quantity (d1 `Div` d2) c
120+
(ValQuantity d1 a) /# (ValQuantity d2 b) = ValQuantity (d1 `V.div` d2) $ doDiv a b
121+
122+
--instance (Fractional v) => Divisionable v v v where
123+
-- doMul = (*)
124+
125+
----------------------------------------
126+
-- Derivering och integrering
127+
----------------------------------------
128+
129+
-- Är själva grejen som finns i en Quantity deriverbar och
130+
-- integrerbar ska Quantityn med den i också vara det.
131+
132+
class Differentiable a b where
133+
doDif :: a -> b
134+
135+
diff :: (Differentiable a b) => Quantity d a -> Quantity (d `Div` time) b
136+
diff = fmap doDif
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
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+
Lines changed: 15 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Improvmenet:
2+
notation
3+
formulas
4+
tests
5+
6+
7+
18

29
Box on an incline
310
=================
@@ -10,6 +17,7 @@ All vectors are in newton.
1017

1118
Notation:
1219
fg = gravitational accelleration
20+
1321
m = mass of box
1422

1523
> fg = V2 0 (-10)
@@ -28,61 +36,24 @@ The normal against the incline:
2836
> fn :: Vector2 Double -> Angle -> Vector2 Double
2937
> fn fa a = negate (f_l_ fa a)
3038

31-
Frictionfree incline:
39+
Friction free incline:
3240

33-
Force resultant:
41+
Resulting force:
3442

3543
> fr :: Vector2 Double -> Angle -> Vector2 Double
3644
> fr fa a = (fn fa a) + fa
3745

46+
With friction:
3847

39-
** Tests:**
40-
------------
41-
42-
*Main> fr (V2 0 10) 0
43-
44-
(0.0 x, 0.0 y)
45-
46-
Good: No inclination - stands still.
47-
48-
49-
*Main> fr (V2 0 (-10)) 0
50-
51-
(0.0 x, -20.0 y)
52-
53-
Odd: Motsatt gravitation ger något underligt? Vi säger fortfarande att normalen är magnituden
54-
55-
56-
*Main> fr (V2 0 10) (pi/2)
57-
58-
(-6.123233995736766e-16 x, 10.0 y)
59-
60-
Good: 90* lutning - faller med G.
61-
62-
*Main> fr (V2 0 10) (pi/3)
63-
64-
(-4.330127018922194 x, 7.499999999999999 y)
65-
66-
*Main> fr (V2 0 10) (pi/4)
67-
68-
(-5.0 x, 4.999999999999999 y)
69-
70-
*Main> fr (V2 0 10) (pi/6)
71-
72-
(-4.330127018922193 x, 2.499999999999999 y)
73-
74-
75-
**Frictionconstant - in motion:**
76-
77-
\begin{align}
78-
F_{friction} = \mu * F_{normal} \iff \mu = \frac{F_{friction}}{F_{normal}}
79-
\end{align}
48+
$$ F_{friction} = \mu * F_{normal} \iff \mu = \frac{F_{friction}}{F_{normal}} $$
8049

8150
> us = 0.5
8251
> uk = 0.4
8352

8453
Add image how friction depends if there is movement.
8554

55+
![Friction](friction.png){.float-img-left}
56+
8657
> type FricConst = Double
8758

8859
Friction:
@@ -94,7 +65,7 @@ Friction:
9465

9566
We have the normal force and only needs the constants.
9667

97-
The current speed does not affect the friction. However F*M = Nm = work = J -> racer cars burn tires.
68+
The current speed does not affect the friction.
9869

9970
> motscalar :: FricConst -> Vector2 Double -> Scalar
10071
> motscalar u f = u * (magnitude f)
@@ -122,81 +93,3 @@ Now we just need to sum the force vectors:
12293
> fru' fa a u = (motkraftv u (fn fa a) (fr fa a))
12394

12495

125-
126-
127-
Hmm om jag försöker skala riktningsvektorer till sin enhetsvektor så blir det blub med nollvektorn.
128-
129-
enh_vekt v = scale (1 / (magnitude v)) v
130-
$ enh_vekt (V2 0 0)
131-
(NaN x, NaN y)
132-
133-
Jag skulle anta att enh_vekt bara gäller då (magnitude v) =/= 0.
134-
135-
136-
Fixed nollvektorn.
137-
138-
139-
*Main> fru fg (pi/4) 0
140-
(-5.0 x, 4.999999999999999 y)
141-
*Main> fru fg (pi/4) 1
142-
(1.7763568394002505e-15 x, -1.7763568394002505e-15 y)
143-
*Main> fru fg (pi/4) 0.5
144-
(-2.499999999999999 x, 2.4999999999999987 y)
145-
Ugh? Den är linjär? 1 i friktionskoeff = full stop. alltid?
146-
147-
När är isf motkraften = fallkraften?
148-
*Main> fru fg 0 5
149-
(0.0 x, 0.0 y)
150-
*Main> fru fg 0 1
151-
(0.0 x, 0.0 y)
152-
*Main> fru fg (pi/2) 10
153-
(-6.123233995736762e-16 x, 9.999999999999995 y)
154-
155-
Hmm?
156-
157-
*Main> fru fg (pi/6) 1
158-
(3.169872981077808 x, -1.8301270189221936 y)
159-
*Main> fru fg (pi/6) 0
160-
(-4.330127018922193 x, 2.499999999999999 y)
161-
*Main> fru fg (pi/6) 100000
162-
(749995.6698729811 x, -433010.2018922193 y)
163-
164-
Den statiska friktionen är konstig. Den borde stå still vid låg vinkel o hög friktion.
165-
166-
Jag summerar ju visserligen krafterna, så det är nog något lurt med friktionshanteringen.
167-
168-
Tests:
169-
170-
fr
171-
*Main> fr fg (pi/3)
172-
(-4.330127018922194 x, 7.499999999999999 y)
173-
*Main> fr fg (pi/6)
174-
(-4.330127018922193 x, 2.499999999999999 y)
175-
176-
177-
fru
178-
*Main> fru fg (pi/3) 1
179-
(-1.8301270189221928 x, 3.1698729810778055 y)
180-
*Main> fru fg (pi/6) 1
181-
(3.169872981077808 x, -1.8301270189221936 y)
182-
183-
184-
185-
fru'
186-
*Main> fru' fg (pi/3) 1
187-
(2.500000000000001 x, -4.330127018922194 y)
188-
*Main> fru' fg (pi/6) 1
189-
(7.500000000000001 x, -4.330127018922193 y
190-
191-
192-
193-
wtf händer? Hur kan fr ha samma x-vektor för två olika vinklar inom samma kvadrant?
194-
195-
fn
196-
197-
*Main> fn fg (pi/3)
198-
(-4.330127018922194 x, -2.500000000000001 y)
199-
*Main> fn fg (pi/6)
200-
(-4.330127018922193 x, -7.500000000000001 y)
201-
202-

0 commit comments

Comments
 (0)