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
# File 'lib/unitwise/unit.rb', line 12

def initialize(input)
  if input.respond_to?(:expression)
    @expression = input.expression
  elsif input.respond_to?(:each)
    @terms = input
  else
    @expression = input.to_s
  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



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

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



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

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

#inverse_scalar(x = 1) ⇒ Object



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

def inverse_scalar(x = 1)
  terms.reduce(1) do |prod, term|
    prod * term.inverse_scalar(x)
  end
end

#root_terms ⇒ Object



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

def root_terms
  terms.flat_map(&:root_terms)
end

#scalar(x = 1) ⇒ Object



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

def scalar(x = 1)
  terms.reduce(1) do |prod, term|
    prod * term.scalar(x)
  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

#terms ⇒ Object



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

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