{-# LANGUAGE RebindableSyntax #-}{- |
Copyright : (c) Henning Thielemann 2003-2006
License : GPL
Maintainer : numericprelude@henning-thielemann.de
Stability : provisional
Portability : portable
Abstract Physical Units
-}moduleNumber.Physical.UnitwhereimportMathObj.DiscreteMap(strip)importqualifiedData.MapasMapimportData.Map(Map)importData.Maybe(fromJust,fromMaybe)importqualifiedNumber.RatioasRatioimportData.Maybe.HT(toMaybe)importNumericPrelude.BaseimportNumericPrelude.Numeric{- | A Unit.T is a sparse vector with integer entries
Each map n->m means that the unit of the n-th dimension
is given m times.
Example: Let the quantity of length (meter, m) be the zeroth dimension
and let the quantity of time (second, s) be the first dimension,
then the composed unit "m_s²" corresponds to the Map
[(0,1),(1,-2)]
In future I want to have more abstraction here,
e.g. a type class from the Edison project
that abstracts from the underlying implementation.
Then one can easily switch between
Arrays, Binary trees (like Map) and what know I.
-}typeTi=MapiInt-- | The neutral Unit.Tscalar::Tiscalar=Map.empty-- | Test for the neutral Unit.TisScalar::Ti->BoolisScalar=Map.null-- | Convert a List to sparse Map representation-- Example: [-1,0,-2] -> [(0,-1),(2,-2)]fromVector::(Enumi,Ordi)=>[Int]->TifromVectorx=strip(Map.fromList(zip[toEnum0..toEnum((lengthx)-1)]x))-- | Convert Map to a ListtoVector::(Enumi,Ordi)=>Ti->[Int]toVectorx=map(flip(Map.findWithDefault0)x)[(toEnum0)..(maximum(Map.keysx))]ratScale::Ratio.TInt->Ti->TiratScaleexpo=fmap(fromMaybe(error"Physics.Quantity.Unit.ratScale: fractional result")).ratScaleMaybe2exporatScaleMaybe::Ratio.TInt->Ti->Maybe(Ti)ratScaleMaybeexpou=letfmMaybe=ratScaleMaybe2expouintoMaybe(not(Nothing`elem`Map.elemsfmMaybe))(fmapfromJustfmMaybe)-- helper function for ratScale and ratScaleMayberatScaleMaybe2::Ratio.TInt->Ti->Mapi(MaybeInt)ratScaleMaybe2expo=fmap(\c->lety=Ratio.scalecexpointoMaybe(denominatory==1)(numeratory)){- impossible because Unit.T is a type synonyme but not a data type
instance Show (Unit.T i) where
show = show.toVector
-}