physics: dimensions, quantities and constants

[ bsd3, library, science ] [ Propose Tags ] [ Report a vulnerability ]

Library to work with typed SI dimensions and common physical functions and constants.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.2.1
Change log ChangeLog.md
Dependencies base (>=4.8 && <5) [details]
Tested with ghc ==7.10.3, ghc ==8.0.2, ghc ==8.2.2, ghc ==8.4.3, ghc ==8.6.1
License BSD-3-Clause
Copyright 2018, Florian Knupfer
Author Florian Knupfer
Maintainer fknupfer@gmail.com
Category Science
Source repo head: git clone https://github.com/knupfer/physics
Uploaded by knupfer at 2018-10-15T16:19:58Z
Distributions NixOS:0.1.2.1
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 760 total (6 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-10-15 [all 1 reports]

Readme for physics-0.1.2.1

[back to package description]

Physics

physics is a library for working with quantities in a performant and type safe manner. It provides some common constants and functions to work on quantities.

Typesafety

Quantities do not have an instance of Num to prevent multiplication without changing the units. Special functions are provided which substitute common operations like >+<, >-<, >/<, >*<.

The naming convention is that operators get on quantity facing sides a greater than or an less than sign. If you want to multiply a quantity with a number you can use *<.

Planck units

physics supports planck units, albeit for most cases they are not recommended. The standard used are SI units.

import Physics.Units
import qualified Physics.Units.Planck as P

main = do
  print lightspeed
  -- 299792458 m s⁻¹
  print $ fromSI lightspeed
  -- 1.0 mₚ sₚ⁻¹
  print . fromPlanck $ P.metre >/< P.second
  -- 2.99792458e8 m s⁻¹

Constants

Constants are divided into measured constants (which are sorted by increasing precision), exact constants (lightspeed for example) and derived constants. All derived constants are defined in terms of measured and exact constants and not simply as a number.

Because of this, updating constants is quite easy, only a small number of measured constants need to be updated when more precise measurements are available.

Performance

physics strives to be as performant as possible. Quantities are newtypes and operators are defined in terms of num operators.

Example usage

{-# LANGUAGE TypeOperators #-}

module Main where

import Physics.Units

main :: IO ()
main = do
  print $ fmap round momentum
  -- 992 m kg s⁻¹
  print $ fmap round kineticEnergy
  -- 5176 m² kg s⁻²

mass :: Kilogram Double
mass = 95 *< kilogram

time :: Second Double
time = 9.58 *< second

distance :: Metre Double
distance = 100 *< metre

speed :: (Metre >/< Second) Double
speed = distance >/< time

momentum :: (Newton >*< Second) Double
momentum = mass >*< speed

kineticEnergy :: Joule Double
kineticEnergy = 1/2 *< mass >*< square speed

-- This would be a type error:
-- kineticEnergy = 1/2 *< mass >*< speed

Compatibility

physics works with GHC 7.10.3 or newer. Some features like showing an Unit or nicer type errors are only available on newer ghcs.