Class: Vector
Defined Under Namespace
Modules: Norm
Class Method Summary collapse
-
.concat(*args) ⇒ Object
Returns a concatenated Vector.
-
.gram_schmidt(*vectors) ⇒ Object
Stabilized Gram-Schmidt process (en.wikipedia.org/wiki/Gram-Schmidt_process#Algorithm).
Instance Method Summary collapse
-
#/(c) ⇒ Object
(also: #quo)
Return the vector divided by a scalar.
- #[](i) ⇒ Object
- #[]=(i, v) ⇒ Object
- #coerce(other) ⇒ Object
-
#collect! ⇒ Object
Changes the elements of vector and returns a Vector.
-
#each ⇒ Object
Iterates the elements of a vector.
-
#house ⇒ Object
Computes the Householder vector (MC, Golub, p. 210, algorithm 5.1.1).
- #index ⇒ Object
-
#magnitude ⇒ Object
Magnitude or length of the vector Equal to sqrt(sum(x_i^2)).
-
#max ⇒ Object
Returns the maximum element of a vector.
-
#min ⇒ Object
Returns the minimum element of a vector.
-
#norm(p = 2) ⇒ Object
Returns the p-norm of a vector.
-
#norm_inf ⇒ Object
Returns the infinite-norm.
-
#normalize ⇒ Object
Return the vector normalized.
- #old_coerce ⇒ Object
-
#proj(v) ⇒ Object
Projection operator (en.wikipedia.org/wiki/Gram-Schmidt_process#The_Gram.E2.80.93Schmidt_process).
-
#slice(*args) ⇒ Object
Returns a slice of vector.
-
#slice=(args) ⇒ Object
Sets a slice of vector.
- #slice_set(v, b, e) ⇒ Object
-
#sum ⇒ Object
Returns the sum of values of the vector.
-
#transpose ⇒ Object
(also: #t)
Return the matrix column coresponding to the vector transpose.
Class Method Details
.concat(*args) ⇒ Object
Returns a concatenated Vector
Vector.concat(Vector[1,2,3], Vector[4,5,6])
=> Vector[1, 2, 3, 4, 5, 6]
72 73 74 |
# File 'lib/extendmatrix.rb', line 72 def concat(*args) Vector.elements(args.inject([]){|ac,v| ac+v.to_a}, false) end |
.gram_schmidt(*vectors) ⇒ Object
Stabilized Gram-Schmidt process (en.wikipedia.org/wiki/Gram-Schmidt_process#Algorithm)
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/extendmatrix.rb', line 211 def self.gram_schmidt(*vectors) v = vectors.clone for j in 0...v.size for i in 0..j-1 v[j] -= v[i] * v[j].inner_product(v[i]) end v[j] /= v[j].norm end v end |
Instance Method Details
#/(c) ⇒ Object Also known as: quo
Return the vector divided by a scalar
154 155 156 |
# File 'lib/extendmatrix.rb', line 154 def / (c) map {|e| e.quo(c)} end |
#[](i) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/extendmatrix.rb', line 33 def [](i) case i when Range Vector[*to_a.slice(i)] else index(i) end end |
#[]=(i, v) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/extendmatrix.rb', line 54 def []=(i, v) case i when Range (self.size..i.begin - 1).each{|e| self[e] = 0} # self.size must be in the first place because the size of self can be modified [v.size, i.entries.size].min.times {|e| self[e + i.begin] = v[e]} (v.size + i.begin .. i.end).each {|e| self[e] = 0} else @elements[i]=v end end |
#coerce(other) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/extendmatrix.rb', line 8 def coerce(other) case other when Numeric return Matrix::Scalar.new(other), self else raise TypeError, "#{self.class} can't be coerced into #{other.class}" end end |
#collect! ⇒ Object
Changes the elements of vector and returns a Vector
80 81 82 83 |
# File 'lib/extendmatrix.rb', line 80 def collect! els = @elements.collect! {|v| yield(v)} Vector.elements(els, false) end |
#each ⇒ Object
Iterates the elements of a vector
88 89 90 91 |
# File 'lib/extendmatrix.rb', line 88 def each (0...size).each {|i| yield(self[i])} nil end |
#house ⇒ Object
Computes the Householder vector (MC, Golub, p. 210, algorithm 5.1.1)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/extendmatrix.rb', line 170 def house s = self[1..length-1] sigma = s.inner_product(s) v = clone; v[0] = 1 if sigma == 0 beta = 0 else mu = Math.sqrt(self[0] ** 2 + sigma) if self[0] <= 0 v[0] = self[0] - mu else v[0] = - sigma.quo(self[0] + mu) end v2 = v[0] ** 2 beta = 2 * v2.quo(sigma + v2) v /= v[0] end return v, beta end |
#index ⇒ Object
25 |
# File 'lib/extendmatrix.rb', line 25 alias :index :[] |
#magnitude ⇒ Object
Magnitude or length of the vector Equal to sqrt(sum(x_i^2))
44 45 46 |
# File 'lib/extendmatrix.rb', line 44 def magnitude Math::sqrt(to_a.inject(0) {|ac, v| ac+(v**2)}) end |
#max ⇒ Object
Returns the maximum element of a vector
96 97 98 |
# File 'lib/extendmatrix.rb', line 96 def max to_a.max end |
#min ⇒ Object
Returns the minimum element of a vector
103 104 105 |
# File 'lib/extendmatrix.rb', line 103 def min to_a.min end |
#norm(p = 2) ⇒ Object
Returns the p-norm of a vector
115 116 117 |
# File 'lib/extendmatrix.rb', line 115 def norm(p = 2) Norm.sqnorm(self, p) ** (1.quo(p)) end |
#norm_inf ⇒ Object
Returns the infinite-norm
122 123 124 |
# File 'lib/extendmatrix.rb', line 122 def norm_inf [min.abs, max.abs].max end |
#normalize ⇒ Object
Return the vector normalized
203 204 205 |
# File 'lib/extendmatrix.rb', line 203 def normalize self.quo(self.norm) end |
#old_coerce ⇒ Object
7 |
# File 'lib/extendmatrix.rb', line 7 alias_method :old_coerce, :coerce |
#proj(v) ⇒ Object
Projection operator (en.wikipedia.org/wiki/Gram-Schmidt_process#The_Gram.E2.80.93Schmidt_process)
194 195 196 197 198 |
# File 'lib/extendmatrix.rb', line 194 def proj(v) vp = v.inner_product(self) vp = Float vp if vp.is_a?(Integer) self * (vp / inner_product(self)) end |
#slice(*args) ⇒ Object
Returns a slice of vector
129 130 131 |
# File 'lib/extendmatrix.rb', line 129 def slice(*args) Vector[*to_a.slice(*args)] end |
#slice=(args) ⇒ Object
Sets a slice of vector
142 143 144 145 146 147 148 149 |
# File 'lib/extendmatrix.rb', line 142 def slice=(args) case args[1] when Range slice_set(args[0], args[1].begin, args[1].last) else slice_set(args[0], args[1], args[2]) end end |
#slice_set(v, b, e) ⇒ Object
133 134 135 136 137 |
# File 'lib/extendmatrix.rb', line 133 def slice_set(v, b, e) for i in b..e self[i] = v[i-b] end end |
#sum ⇒ Object
Returns the sum of values of the vector
109 110 111 |
# File 'lib/extendmatrix.rb', line 109 def sum to_a.inject(&:+) end |
#transpose ⇒ Object Also known as: t
Return the matrix column coresponding to the vector transpose
161 162 163 |
# File 'lib/extendmatrix.rb', line 161 def transpose Matrix[self.to_a] end |