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
-
#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.
-
#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]
61 62 63 |
# File 'lib/extendmatrix.rb', line 61 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)
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/extendmatrix.rb', line 200 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
143 144 145 |
# File 'lib/extendmatrix.rb', line 143 def / (c) map {|e| e.quo(c)} end |
#[](i) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/extendmatrix.rb', line 22 def [](i) case i when Range Vector[*to_a.slice(i)] else index(i) end end |
#[]=(i, v) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/extendmatrix.rb', line 43 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 |
#collect! ⇒ Object
Changes the elements of vector and returns a Vector
69 70 71 72 |
# File 'lib/extendmatrix.rb', line 69 def collect! els = @elements.collect! {|v| yield(v)} Vector.elements(els, false) end |
#each ⇒ Object
Iterates the elements of a vector
77 78 79 80 |
# File 'lib/extendmatrix.rb', line 77 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)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/extendmatrix.rb', line 159 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
14 |
# File 'lib/extendmatrix.rb', line 14 alias :index :[] |
#magnitude ⇒ Object
Magnitude or length of the vector Equal to sqrt(sum(x_i^2))
33 34 35 |
# File 'lib/extendmatrix.rb', line 33 def magnitude Math::sqrt(to_a.inject(0) {|ac, v| ac+(v**2)}) end |
#max ⇒ Object
Returns the maximum element of a vector
85 86 87 |
# File 'lib/extendmatrix.rb', line 85 def max to_a.max end |
#min ⇒ Object
Returns the minimum element of a vector
92 93 94 |
# File 'lib/extendmatrix.rb', line 92 def min to_a.min end |
#norm(p = 2) ⇒ Object
Returns the p-norm of a vector
104 105 106 |
# File 'lib/extendmatrix.rb', line 104 def norm(p = 2) Norm.sqnorm(self, p) ** (1.quo(p)) end |
#norm_inf ⇒ Object
Returns the infinite-norm
111 112 113 |
# File 'lib/extendmatrix.rb', line 111 def norm_inf [min.abs, max.abs].max end |
#normalize ⇒ Object
Return the vector normalized
192 193 194 |
# File 'lib/extendmatrix.rb', line 192 def normalize self.quo(self.norm) end |
#proj(v) ⇒ Object
Projection operator (en.wikipedia.org/wiki/Gram-Schmidt_process#The_Gram.E2.80.93Schmidt_process)
183 184 185 186 187 |
# File 'lib/extendmatrix.rb', line 183 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
118 119 120 |
# File 'lib/extendmatrix.rb', line 118 def slice(*args) Vector[*to_a.slice(*args)] end |
#slice=(args) ⇒ Object
Sets a slice of vector
131 132 133 134 135 136 137 138 |
# File 'lib/extendmatrix.rb', line 131 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
122 123 124 125 126 |
# File 'lib/extendmatrix.rb', line 122 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
98 99 100 |
# File 'lib/extendmatrix.rb', line 98 def sum to_a.inject(&:+) end |
#transpose ⇒ Object Also known as: t
Return the matrix column coresponding to the vector transpose
150 151 152 |
# File 'lib/extendmatrix.rb', line 150 def transpose Matrix[self.to_a] end |