Class: AppMath::R2

Inherits:
Object
  • Object
show all
Defined in:
lib/kepler_2d.rb

Overview

Real vector space of two dimensions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ R2

Returns a new instance of R2.



41
42
43
44
# File 'lib/kepler_2d.rb', line 41

def initialize(x,y)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



38
39
40
# File 'lib/kepler_2d.rb', line 38

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#*(s) ⇒ Object



58
59
60
# File 'lib/kepler_2d.rb', line 58

def *(s)
  R2.new(x * s, y * s)
end

#+(p) ⇒ Object



50
51
52
# File 'lib/kepler_2d.rb', line 50

def +(p)
  R2.new(x + p.x, y + p.y)
end

#-(p) ⇒ Object



54
55
56
# File 'lib/kepler_2d.rb', line 54

def -(p)
  R2.new(x - p.x, y - p.y)
end

#-@Object



62
63
64
# File 'lib/kepler_2d.rb', line 62

def -@
  R2.new(-x, -y)
end

#absObject



70
71
72
# File 'lib/kepler_2d.rb', line 70

def abs
  x.hypot(y)
end

#abs2Object

abs squared



75
76
77
# File 'lib/kepler_2d.rb', line 75

def abs2
  x * x + y * y
end

#cloneObject



46
47
48
# File 'lib/kepler_2d.rb', line 46

def clone
  R.new(x,y)
end

#spr(p) ⇒ Object

scalar product



80
81
82
# File 'lib/kepler_2d.rb', line 80

def spr(p)
  x * p.x + y * p.y
end

#to_sObject



66
67
68
# File 'lib/kepler_2d.rb', line 66

def to_s
  res = "R2(" + x.to_s + ", " + y.to_s + ")"
end

#uvObject

unit vector



85
86
87
88
89
90
91
92
93
# File 'lib/kepler_2d.rb', line 85

def uv
  r = abs
  if r.zero?
    clone
  else 
    ri = r.inv
    self * ri
  end
end