sci

package
v0.0.0-...-b7787a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

README

A go library for scientific unit calculations, powering a scientific spreadsheet application. This library is inspired by ruby-unit and allows for code like:


  // creating values
  x := &sci.Value{M: "10.001", U: si.Millimeter}
  y := &sci.Value{M: "10.001", U: si.Millimeter}
  z := &sci.Value{}

  // math
  err := z.Add(x, y)

  // units are composite values, algebraic values

  // acceleration (m/s^2)
  Acceleration = sci.DivUnit{
    N: si.Meter,
    D: sci.MulUnit{ units.Second, units.Second },
  }
   

  // Defining constants
  var (
    EarthGravity = si.MustParseValue("9.807 m/s^2")
  )

This spreadsheet application could allow for "input cells" and "output cells" which exposes the sheet as an API to others allowing for collaboration.

Documentation

Overview

Package sci implements a system for performing calculations on physical quantities. The math API is inspired by the big package of the go standard library: The receiver of a .

The core

Index

Constants

View Source
const (
	// MaxExp represents the largest absolute value that an exponent is allowed to
	// have within this library.  It's a conservative cap for now, as I (scott) am
	// not very sure what sort of issues will arise if left higher or unbounded.
	MaxExp = 4
)

Variables

View Source
var (
	// ErrIncompatibleTypes is returned when attempted to perform an operation
	// (such as addition) on two incompatible types.
	ErrIncompatibleTypes = errors.New("incompatible types")
)

Functions

This section is empty.

Types

type BaseUnit

type BaseUnit struct {
	Name    string
	Measure Measure
	// contains filtered or unexported fields
}

BaseUnit represents the a base unit of a given measure against which other units can be defined. For example, a meter is a base unit of length.

func (*BaseUnit) PopulateNormalizedUnit

func (bu *BaseUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inverted bool)

PopulateNormalizedUnit implements Unit

func (*BaseUnit) String

func (bu *BaseUnit) String() string

String implements fmt.Stringer

func (*BaseUnit) System

func (bu *BaseUnit) System() *System

System implements Unit

type BaseUnitAlreadyDefinedError

type BaseUnitAlreadyDefinedError struct {
	Existing *BaseUnit
}

BaseUnitAlreadyDefinedError is an error that occurs when attempting to redefine the base unit used for some measure in a system of units. A system of units may only have one base unit per measure to ensure that we can define any value belonging to a given measure in relation to a single base unit.

func (*BaseUnitAlreadyDefinedError) Error

func (berr *BaseUnitAlreadyDefinedError) Error() string

Error implements the error interface

type Converter

type Converter interface {
	Convert(in *Value) (*Value, error)
}

Converter represents a type that can convert a value of one unit into another.

type DerivedUnit

type DerivedUnit struct {
	Value *Value
}

DerivedUnit represents a unit expressed in relation to some base unit.

func (*DerivedUnit) PopulateNormalizedUnit

func (u *DerivedUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)

PopulateNormalizedUnit implements Unit

func (*DerivedUnit) String

func (u *DerivedUnit) String() string

String implements fmt.Stringer

func (*DerivedUnit) System

func (u *DerivedUnit) System() *System

System implements Unit

type DivUnit

type DivUnit struct {
	N Unit
	D Unit
}

DivUnit represents a compound unit such as "feet / hour"

func (*DivUnit) PopulateNormalizedUnit

func (u *DivUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)

PopulateNormalizedUnit implements Unit

func (*DivUnit) String

func (u *DivUnit) String() string

String implements fmt.Stringer

func (*DivUnit) System

func (u *DivUnit) System() *System

System implements Unit

type ExpToBigError

type ExpToBigError struct {
	Exp int64
}

ExpToBigError is the error that is returned when a string that is being parsed into a unit has an exponent that is too large.

func (*ExpToBigError) Error

func (experr *ExpToBigError) Error() string

Error implements the error interface

type MagnitudeError

type MagnitudeError struct {
	M string
}

MagnitudeError represents the error produces when trying to operate on a value whose magnitude (the M field) is invalid.

func (*MagnitudeError) Error

func (merr *MagnitudeError) Error() string

Error implements the error interface

type Measure

type Measure string

Measure represents a domain of measurement, such as length, time, or mass.

const (
	// Length represents the length measure
	Length Measure = "Length"

	// Time represents the time measure
	Time Measure = "Time"
)

type MulUnit

type MulUnit []Unit

MulUnit represents a compound unit such as "foot*pound"

func (*MulUnit) PopulateNormalizedUnit

func (u *MulUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)

PopulateNormalizedUnit implements Unit

func (*MulUnit) String

func (u *MulUnit) String() string

String implements fmt.Stringer

func (*MulUnit) System

func (u *MulUnit) System() *System

System implements Unit

type NilUnit

type NilUnit struct {
	// contains filtered or unexported fields
}

NilUnit represents "no unit". Values of it represents "a mignitude without a unit". NilUnit is also used to represent inverse units, such as hz (1 / s) when combind using DivUnit.

func (*NilUnit) PopulateNormalizedUnit

func (u *NilUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)

PopulateNormalizedUnit implements Unit

func (*NilUnit) String

func (u *NilUnit) String() string

String implements fmt.Stringer

func (*NilUnit) System

func (u *NilUnit) System() *System

System implements Unit

type NormalizedUnit

type NormalizedUnit struct {
	Components map[*BaseUnit]int
	// contains filtered or unexported fields
}

NormalizedUnit represents the non-aliased form of a unit, expressed completely in terms of base units. derived units are expanded into base units and then contribute themselves to either the numerator by increasing the count by one, or to the denominator by decreasing the count by one. This will be a recursive process.

func (*NormalizedUnit) Add

func (nu *NormalizedUnit) Add(bu *BaseUnit, exp int)

Add contributes the provided base unit at the provided exponent (negative numbers representing to the negative power) to the normalized unit.

func (*NormalizedUnit) Invert

func (nu *NormalizedUnit) Invert() *NormalizedUnit

Invert returns a copy of the normalized unit, inverted.

type Prefix

type Prefix struct {
	Name    string
	Scalar  string
	Aliases []string
}

Prefix represents a unit prefix.

type System

type System struct {
	// Name is an optional name for a system
	Name string

	// BaseUnits represents the collection of defined base units
	BaseUnits map[Measure]*BaseUnit
	// contains filtered or unexported fields
}

System represents a system of measurement.

func NewSystem

func NewSystem(name string) *System

NewSystem creates a new unit system with the given name

func (*System) DefineBaseUnit

func (sys *System) DefineBaseUnit(name string, m Measure) (*BaseUnit, error)

DefineBaseUnit defines a base unit of measure in the unit system. Only one base unit per measure is allowed, and every other unit in a given system must be defined in terms of the base units of the system.

func (*System) DefineUnit

func (sys *System) DefineUnit(name string, valstr string) (Unit, error)

DefineUnit adds a new DerivedUnit to the system using val as the definition. val must be expressed in terms of units previously defined within the system.

func (*System) LookupUnit

func (sys *System) LookupUnit(name string) (Unit, error)

LookupUnit finds a unit by name or alias in the system of units. An error of type *UnitNotFoundError will be returned if a unit with the given name has not been previously defined.

func (*System) MustDefineBaseUnit

func (sys *System) MustDefineBaseUnit(name string, m Measure) *BaseUnit

MustDefineBaseUnit is the panicking version of define base unit

func (*System) MustDefineUnit

func (sys *System) MustDefineUnit(name string, val string) Unit

MustDefineUnit is the panicking version of DefineUnit

func (*System) MustParse

func (sys *System) MustParse(val string) *Value

MustParse is the panicking version of Parse

func (*System) Nil

func (sys *System) Nil() *NilUnit

func (*System) Parse

func (sys *System) Parse(val string) (*Value, error)

Parse parses a single value using the units defined (i.e. previously added to the system using Add()) in sys.

func (*System) ParseUnit

func (sys *System) ParseUnit(unitstr string) (Unit, error)

ParseUnit converts the provided string into a Unit value, looking up defined units as necessary and forming new algebraic units as specified.

type Unit

type Unit interface {
	fmt.Stringer
	System() *System
	PopulateNormalizedUnit(nu *NormalizedUnit, inverted bool)
}

Unit represents any unit of measure

type UnitAlreadyDefinedError

type UnitAlreadyDefinedError struct {
	Existing Unit
	Name     string
}

UnitAlreadyDefinedError is an error that occurs when attempting to redefine the a named unit. A given system may not have multiple units that have the same name dfined within it.

func (*UnitAlreadyDefinedError) Error

func (uerr *UnitAlreadyDefinedError) Error() string

Error implements the error interface

type UnitNotDefinedError

type UnitNotDefinedError struct {
	// Name is the name for the unit attempted to be found
	Name string
}

UnitNotDefinedError is an error that occurs when attempting to lookup a unit in the system.

func (*UnitNotDefinedError) Error

func (uerr *UnitNotDefinedError) Error() string

Error implements the error interface

type Value

type Value struct {
	// M is the magnitude or the multitude of the value (depending upon whether or
	// not the unit is collective or non-collective), expressed as a string
	// (parsing rules TBD)
	M string

	// U is the unit of the value.
	U Unit
}

Value represents a value. Examples include "3 mm" or "10 m/s"

func (*Value) Add

func (v *Value) Add(l, r *Value) error

Add adds `l` and `r` together and stores the result in `v`, providing that `l` and `r` can be added together.

func (*Value) Div

func (v *Value) Div(l, r *Value) error

Div divides `l` and `r` together and stores the result in `v`.

func (*Value) Eq

func (v *Value) Eq(other *Value) bool

Eq checks v and other for equality

func (*Value) Mul

func (v *Value) Mul(l, r *Value) error

Mul multiplies `l` and `r` together and stores the result in `v`.

Notes

Bugs

  • we don't have unit conversions yet, so the following adoption of l.U for the return value is broken.

Directories

Path Synopsis
systems
si
uscs
Package uscs provides the United states customary unit system.
Package uscs provides the United states customary unit system.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL