Class: Unitwise::Unit

Inherits:
Object
  • Object
show all
Includes:
Compatible
Defined in:
lib/unitwise/unit.rb

Overview

A Unit is essentially a collection of Unitwise::Term. Terms can be combined through multiplication or division to create a unit. A unit does not have a magnitude, but it does have a scale.

Instance Method Summary collapse

Methods included from Compatible

#<=>, #compatible_with?, #composition, #composition_string, #dim, included

Constructor Details

#initialize(input) ⇒ Unit

Create a new unit. You can send an expression or a collection of terms collection of tems.

Parameters:

  • input (String, Unit, [Term])

    A string expression, a unit, or a



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/unitwise/unit.rb', line 12

def initialize(input)
  case input
  when Compatible
    @expression = input.expression
  when String, Symbol
    @expression = input.to_s
  else
    @terms = input
    @expression = Expression.compose(input)
  end
  freeze
end

Instance Method Details

#*(other) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/unitwise/unit.rb', line 61

def *(other)
  if other.respond_to?(:terms)
    self.class.new(terms + other.terms)
  elsif other.respond_to?(:atom)
    self.class.new(terms << other)
  elsif other.is_a?(Numeric)
    self.class.new(terms.map { |t| t * other })
  else
    fail TypeError, "Can't multiply #{self} by #{other}."
  end
end

#**(other) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/unitwise/unit.rb', line 83

def **(other)
  if other.is_a?(Numeric)
    self.class.new(terms.map { |t| t ** other })
  else
    fail TypeError, "Can't raise #{self} to #{other}."
  end
end

#/(other) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/unitwise/unit.rb', line 73

def /(other)
  if other.respond_to?(:terms)
    self.class.new(terms + other.terms.map { |t| t ** -1 })
  elsif other.respond_to?(:atom)
    self.class.new(terms << other ** -1)
  else
    fail TypeError, "Can't divide #{self} by #{other}."
  end
end

#atomsObject



29
30
31
# File 'lib/unitwise/unit.rb', line 29

def atoms
  terms.map(&:atom)
end

#depthObject



39
40
41
# File 'lib/unitwise/unit.rb', line 39

def depth
  terms.map(&:depth).max + 1
end

#magnitude(scalar = scalar) ⇒ Object



55
56
57
58
59
# File 'lib/unitwise/unit.rb', line 55

def magnitude(scalar = scalar)
  terms.reduce(1.0) do |prod, term|
    prod * term.magnitude(scalar)
  end
end

#root_termsObject



44
45
46
# File 'lib/unitwise/unit.rb', line 44

def root_terms
  terms.map(&:root_terms).flatten
end

#scalar(magnitude = 1.0) ⇒ Object



49
50
51
52
53
# File 'lib/unitwise/unit.rb', line 49

def scalar(magnitude = 1.0)
  terms.reduce(1.0) do |prod, term|
    prod * term.scalar(magnitude)
  end
end

#special?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/unitwise/unit.rb', line 34

def special?
  terms.count == 1 && terms.all?(&:special?)
end

#termsObject



25
26
27
# File 'lib/unitwise/unit.rb', line 25

def terms
  @terms || Expression.decompose(expression)
end

#to_sObject



91
92
93
# File 'lib/unitwise/unit.rb', line 91

def to_s
  expression
end