Class: Mageo::Axes

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

Overview

n 次元空間における座標系を表現する n 本のベクトルを扱うクラス。

Defined Under Namespace

Classes: InitializeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vectors) ⇒ Axes

要素数が nxn でなければ例外。0x0 も例外。 角各要素は to_f メソッドを持たなければならない。

Raises:



13
14
15
16
17
18
19
20
21
# File 'lib/mageo/axes.rb', line 13

def initialize(vectors)
  raise InitializeError if vectors.size == 0
  @axes = vectors.map{|vector| Vector[*vector]}

  #raise InitializeError unless @axes.regular? # この判定はうまくいかない。バグ?
  @axes.each do |vector|
    raise InitializeError unless @axes.size == vector.size
  end
end

Instance Attribute Details

#axesObject (readonly)

Returns the value of attribute axes.



6
7
8
# File 'lib/mageo/axes.rb', line 6

def axes
  @axes
end

Class Method Details

.dependent?(axes) ⇒ Boolean

Return true is the vector is not independent.

Returns:

  • (Boolean)


28
29
30
# File 'lib/mageo/axes.rb', line 28

def self.dependent?(axes)
  return ! (self.independent?(axes))
end

.independent?(axes) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/mageo/axes.rb', line 23

def self.independent?(axes)
  return Matrix[* axes.map{|i| i.to_a}].regular?
end

Instance Method Details

#==(other) ⇒ Object

Check equal. Usually, this method should not be used, except for tests.



40
41
42
43
44
45
46
47
48
49
# File 'lib/mageo/axes.rb', line 40

def ==(other)
  result = true
  size.times do |i|
    size.times do |j|
      result = false if @axes[i][j] != other.axes[i][j]
    end
  end

  return result
end

#[](index) ⇒ Object

Item access for three axes in cartesian coordinates. Note: []= method is not provided.



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

def [](index)
  @axes[ index ]
end

#dependent?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/mageo/axes.rb', line 51

def dependent?
  self.class.dependent?(@axes)
end

#eachObject

Iterate each vector of axes.



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

def each
  @axes.each { |i| yield(i) }
end

#independent?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/mageo/axes.rb', line 55

def independent?
  self.class.independent?(@axes)
end

#sizeObject

Return a number of vectors in axes, always three. Mimic for Array.



34
35
36
# File 'lib/mageo/axes.rb', line 34

def size
  return @axes.size
end

#to_aObject

Convert to Array. Non-destructive.



80
81
82
83
84
85
86
87
# File 'lib/mageo/axes.rb', line 80

def to_a
  result = [
    @axes[0].to_a,
    @axes[1].to_a,
    @axes[2].to_a,
  ]
  return result
end