Quantities

This section describes the Quantities submodule of Alicorn. The module is concerned with defining and manipulating physical quantities.

Unless stated otherwise, all types, functions, and constants defined in the submodule are exported by Alicorn.

Contents

Abstract Supertypes

Interface of AbstractQuantity

The following functions are considered part of the interface of AbstractQuantity and need to be extended for all concrete subtypes of AbstractQuantity:

Base.:==Method
Base.:==(quantity1::AbstractQuantity, quantity2::AbstractQuantity)

Compare two physical quantities.

The behavior of the comparison depends on the concrete subtype of AbstractUnit.

source
Alicorn.Quantities.inUnitsOfMethod
inUnitsOf(quantity::AbstractQuantity, targetUnit::AbstractUnit)::SimpleQuantity

Express quantity as an object of type SimpleQuantity in terms of the unit specified by targetUnit.

source
Base.:*Method
Base.:*(quantity::AbstractQuantity, unit::AbstractUnit)

Modify the unit of quantity by multiplying it with unit.

source
Base.:/Method
Base.:/(quantity::AbstractQuantity, unit::AbstractUnit)

Modify the unit of quantity by dividing it by unit.

source
Base.:+Method
Base.:+(quantity1::AbstractQuantity, quantity2::AbstractQuantity)

Add two physical quantities.

source
Base.:-Method
Base.:-(quantity1::AbstractQuantity, quantity2::AbstractQuantity)

Subtract quantity2 from quantity1.

source
Base.:*Method
Base.:*(quantity1::AbstractQuantity, quantity2::AbstractQuantity)

Multiply two physical quantities.

source
Base.:/Method
Base.:/(quantity1::AbstractQuantity, quantity2::AbstractQuantity)

Divide quantity1 by quantity2.

source
Base.invMethod
Base.inv(quantity::AbstractQuantity)

Determine the multiplicative inverse of quantity.

source
Base.:^Method
Base.:^(quantity::AbstractQuantity, exponent::Real)

Raise quantity to the power of exponent.

source
Base.sqrtMethod
Base.sqrt(quantity::AbstractQuantity)

Take the square root of quantity.

source

SimpleQuantity

Alicorn.Quantities.SimpleQuantityType
SimpleQuantity{T} <: AbstractQuantity

A physical quantity consisting of a value and a physical unit.

SimpleQuantity is a parametric type, where T is the type of the quantity's value. While the value can be of any type, SimpleQuantity implements the AbstractQuantity interface and hence assumes that the type T supports arithmetic operations.

Fields

  • value::T: value of the quantity
  • unit::Unit: unit of the quantity

Constructor

SimpleQuantity(value::T, abstractUnit::AbstractUnit) where T

Examples

  1. The quantity $7\,\mathrm{nm}$ (seven nanometers) can be constructed using the constructor method as follows:

    julia> ucat = UnitCatalogue() ;
    
    julia> nanometer = ucat.nano * ucat.meter
    UnitFactor nm
    
    julia> quantity = SimpleQuantity(7, nanometer)
    7 nm
  2. Alternatively, $7\,\mathrm{nm}$ can be constructed arithmetically:

    julia> ucat = UnitCatalogue() ;
    
    julia> nanometer = ucat.nano * ucat.meter
    UnitFactor nm
    
    julia> quantity = 7 * nanometer
    7 nm
  3. The value can be of any type. Any mathematical operation included in the interface of AbstractQuantity is applied to the value field, and the unit is modified accordingly.

    julia> ucat = UnitCatalogue() ;
    
    julia> nanometer = ucat.nano * ucat.meter
    UnitFactor nm
    
    julia> quantity1 = [4 5] * nanometer
    SimpleQuantity{Array{Int64,2}} of unit nm
    
    julia> quantity1sqrd = quantity1 * transpose(quantity1)
    SimpleQuantity{Array{Int64,2}} of unit nm^2
    
    julia> quantity1sqrd.value
    1×1 Array{Int64,2}:
     41

    The responsibility to check that the resulting quantity is meaningful and supports arithemtic operations lies with the user. For example, Alicorn allows to assign a unit to a string. String concatenation with * or ^ results in a corresponding change of the unit:

    julia> quantity2 = "this is nonsense" * nanometer
    SimpleQuantity{String} of unit nm
    
    julia> quantity2sqrd = quantity2^2
    SimpleQuantity{String} of unit nm^2
    
    julia> quantity2sqrd.value
    "this is nonsensethis is nonsense"

    On the other hand, multiplication with a number raises an exception since there is no corresponding method for strings.

    julia> 2 * quantity2
    MethodError: no method matching *(::Int64, ::SimpleQuantity{String})
    [...]
source
Base.:==Method
Base.:(==)(simpleQuantity1::SimpleQuantity, simpleQuantity2::SimpleQuantity)

Compare two SimpleQuantity objects.

The two quantities are equal if both their values and their units are equal. Note that the units are not converted during the comparison.

Examples

julia> ucat = UnitCatalogue() ;

julia> q1 = 7 * ucat.meter
7 m

julia> q2 = 700 * (ucat.centi * ucat.meter)
700 cm

julia> q1 == q1
true

julia> q1 == q2
false
source
Base.:*Method
Base.:*(value::Any, abstractUnit::AbstractUnit)::SimpleQuantity

Combine value and abstractUnit to form a physical quantity of type SimpleQuantity.

Example

julia> ucat = UnitCatalogue() ;

julia> 3.5 * ucat.tesla
3.5 T
source
Base.:/Method
Base.:/(value::Any, abstractUnit::AbstractUnit)::SimpleQuantity

Combine value and abstractUnit to form a physical quantity of type SimpleQuantity.

Example

julia> ucat = UnitCatalogue() ;

julia> 3.5 / ucat.second
3.5 s^-1
source