Base class for creating objects to manage quantities as Numeric.
Base Quantities are the basic, directly measurable, fundamental quantities: Mass, Length, Time, etc.
A unit of measure used for Base (SI) Quantities
A marker trait identifying SI Base Units
Represents a quantity of some thing for which there is no dimension.
Base trait for units of squants.Dimensionless
Double (Real Number) Vector
Strategy #3 Prototype for implementations that support dimensional conversions
A base trait for measurable quantities found within physical systems
A base trait for measurable quantities
Represents a Range starting at one Quantity value and going up to another
Quantity Vector
Defines an interface and partial implementation for types that represent a ratio between any two quantities
Provides implicit conversions that allow Doubles to lead in * operations
A Unit of Measure that require a simple multiplier for converting to and from the underlying value's unit
A Unit of Measure is used to define the scale of a quantity measurement
Identifies the Unit of Measure used for storing the quantity's underlying value
Root trait for representing Vectors
Singleton defining Metric System multipliers
Factory singleton for squants.Dimensionless
Represents a unit of dozen (12)
Represents a unit of singles
Represents a unit of gross (144)
Singleton defining Metric System multipliers
Strategy #2 Prototype for implementations that support dimensional conversions
Represents a unit of scores (20)
Squants Market API
Squants
The Scala API for Quantities, Units of Measure and Dimensional Analysis
Overview
Squants is a framework of data types and a domain specific language (DSL) for representing Quantities, their Units of Measure, and their Dimensional relationships. The API supports typesafe dimensional analysis, improved domain models and more. All types are immutable and thread-safe.
The motivating driver for the framework was to ensure developers of VPower, Viridity Energy's flagship energy management product, would have a type safe way to define energy related expressions, operations, domain models and API's.
For example, when using a Double to describe Energy (kWh) and Power/Load (kW), it is possible to compile a program that adds these two values together. This is not appropriate as kW and kWh measure two different quantities. The unit kWh is used to measure an amount of Energy used or produced. The unit kW is used to measure Power/Load, the rate at which Energy is being used or produced, that is, Power/Load is the first time derivative of Energy, Energy / Time.
Consider the following code
which not only adds different quantity types (load vs energy), it also fails to convert the scales (Mega vs Kilo). Because this code compiles, detection of these errors is pushed further into the development cycle.
The Squants Type Library and DSL helps prevent errors like these by type checking operations at compile time and automatically applying scale and type conversions (see below) at run-time. For example,
is a valid assertion because Kilowatts and Megawatts are both measures of load. Only the scale is different and the framework applies an appropriate conversion. Also, notice that keeping track of the scale within the value name is no longer needed. If necessary, the value can be converted to a specific scale using the methods provided by the quantity class.
The following code highlights the type safety features.
The invalid expression prevents the code from compiling, catching the error made when using Double much earlier.
Domain smart type conversions are supported. Most conversions are implemented by defining relationships between Quantity types using infix operations.
This code demonstrates use of the Power.* method, defined as an infix operator that takes a Time value and returns an Energy value, conversely
demonstrates use of the Energy./ method that takes a Time and returns a Power
Other conversions are done using specialized versions of a Quantity's factory methods
Natural Language Features
Implicit conversions give the DSL some features that allows client code to express quantities in a more natural way.
Many Quantity classes also provide a factory method that takes a quantity expression
Useful for automatically interpreting strings from user input, json marshaller and other sources
Type Hierarchy
The type hierarchy includes two root base traits: squants.Quantity and squants.UnitOfMeasure
Quantity
Quantity measures the magnitude or multitude of some thing. Classes extending Quantity represent the various types of quantities that can be measured. These are our alternative to just using Double. Common 'Base' Quantities include Mass, Temperature, Length, Time, Energy, etc.
Derived Quantities are based on one or more other quantities. Typical examples include Time Derivatives.
Speed is the 1st Time Derivative of Length (Distance), Acceleration is the 2nd Time Derivative.
Power is the 1st Time Derivative of Energy, PowerRamp is the 2nd
Unit of Measure
UnitOfMeasure is the scale or multiplier in which the Quantity is being measured.
For each Quantity a series of UOM objects implement a base UOM trait typed to that Quantity. The UOM objects define the unit symbols and conversion settings. Factory methods in each UOM object create instances of the corresponding Quantity.
For example UOM objects extending squants.space.LengthUnit can be used to create Length quantities
Units of Measure for Time include Milliseconds, Seconds, Minutes, Hours, and Days
Units of Measure for Temperature include Celsius, Kelvin, and Fahrenheit
Units of Measure for Mass include Grams, Kilograms, etc.
Domain Modeling
An excellent use case for Squants is stronger types for fields in your domain model.
Another use case is performing domain specific computations.
0.1
0.1