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
# 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
  end
end

Instance Method Details

#*(other) ⇒ Object



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

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



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

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



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

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

#atoms ⇒ Object



31
32
33
# File 'lib/unitwise/unit.rb', line 31

def atoms
  terms.map(&:atom)
end

#depth ⇒ Object



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

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

#expression ⇒ Object



23
24
25
# File 'lib/unitwise/unit.rb', line 23

def expression
  @expression ||= (Expression.compose(@terms) if @terms)
end

#magnitude(scalar = scalar) ⇒ Object



53
54
55
56
57
# File 'lib/unitwise/unit.rb', line 53

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

#root_terms ⇒ Object



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

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

#scalar(magnitude = 1) ⇒ Object



47
48
49
50
51
# File 'lib/unitwise/unit.rb', line 47

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

#special? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#terms ⇒ Object



27
28
29
# File 'lib/unitwise/unit.rb', line 27

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

#to_s ⇒ Object



89
90
91
# File 'lib/unitwise/unit.rb', line 89

def to_s
  expression
end