Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/share/ruby
File: matrix.rb
Vector.Raise ErrDimensionMismatch unless v.size == size
[2000] Fix | Delete
end
[2001] Fix | Delete
case size
[2002] Fix | Delete
when 2
[2003] Fix | Delete
Vector[-@elements[1], @elements[0]]
[2004] Fix | Delete
when 3
[2005] Fix | Delete
v = vs[0]
[2006] Fix | Delete
Vector[ v[2]*@elements[1] - v[1]*@elements[2],
[2007] Fix | Delete
v[0]*@elements[2] - v[2]*@elements[0],
[2008] Fix | Delete
v[1]*@elements[0] - v[0]*@elements[1] ]
[2009] Fix | Delete
else
[2010] Fix | Delete
rows = self, *vs, Array.new(size) {|i| Vector.basis(size: size, index: i) }
[2011] Fix | Delete
Matrix.rows(rows).laplace_expansion(row: size - 1)
[2012] Fix | Delete
end
[2013] Fix | Delete
end
[2014] Fix | Delete
alias_method :cross, :cross_product
[2015] Fix | Delete
[2016] Fix | Delete
#
[2017] Fix | Delete
# Like Array#collect.
[2018] Fix | Delete
#
[2019] Fix | Delete
def collect(&block) # :yield: e
[2020] Fix | Delete
return to_enum(:collect) unless block_given?
[2021] Fix | Delete
els = @elements.collect(&block)
[2022] Fix | Delete
self.class.elements(els, false)
[2023] Fix | Delete
end
[2024] Fix | Delete
alias map collect
[2025] Fix | Delete
[2026] Fix | Delete
#
[2027] Fix | Delete
# Returns the modulus (Pythagorean distance) of the vector.
[2028] Fix | Delete
# Vector[5,8,2].r => 9.643650761
[2029] Fix | Delete
#
[2030] Fix | Delete
def magnitude
[2031] Fix | Delete
Math.sqrt(@elements.inject(0) {|v, e| v + e.abs2})
[2032] Fix | Delete
end
[2033] Fix | Delete
alias r magnitude
[2034] Fix | Delete
alias norm magnitude
[2035] Fix | Delete
[2036] Fix | Delete
#
[2037] Fix | Delete
# Like Vector#collect2, but returns a Vector instead of an Array.
[2038] Fix | Delete
#
[2039] Fix | Delete
def map2(v, &block) # :yield: e1, e2
[2040] Fix | Delete
return to_enum(:map2, v) unless block_given?
[2041] Fix | Delete
els = collect2(v, &block)
[2042] Fix | Delete
self.class.elements(els, false)
[2043] Fix | Delete
end
[2044] Fix | Delete
[2045] Fix | Delete
class ZeroVectorError < StandardError
[2046] Fix | Delete
end
[2047] Fix | Delete
#
[2048] Fix | Delete
# Returns a new vector with the same direction but with norm 1.
[2049] Fix | Delete
# v = Vector[5,8,2].normalize
[2050] Fix | Delete
# # => Vector[0.5184758473652127, 0.8295613557843402, 0.20739033894608505]
[2051] Fix | Delete
# v.norm => 1.0
[2052] Fix | Delete
#
[2053] Fix | Delete
def normalize
[2054] Fix | Delete
n = magnitude
[2055] Fix | Delete
raise ZeroVectorError, "Zero vectors can not be normalized" if n == 0
[2056] Fix | Delete
self / n
[2057] Fix | Delete
end
[2058] Fix | Delete
[2059] Fix | Delete
#
[2060] Fix | Delete
# Returns an angle with another vector. Result is within the [0...Math::PI].
[2061] Fix | Delete
# Vector[1,0].angle_with(Vector[0,1])
[2062] Fix | Delete
# # => Math::PI / 2
[2063] Fix | Delete
#
[2064] Fix | Delete
def angle_with(v)
[2065] Fix | Delete
raise TypeError, "Expected a Vector, got a #{v.class}" unless v.is_a?(Vector)
[2066] Fix | Delete
Vector.Raise ErrDimensionMismatch if size != v.size
[2067] Fix | Delete
prod = magnitude * v.magnitude
[2068] Fix | Delete
raise ZeroVectorError, "Can't get angle of zero vector" if prod == 0
[2069] Fix | Delete
[2070] Fix | Delete
Math.acos( inner_product(v) / prod )
[2071] Fix | Delete
end
[2072] Fix | Delete
[2073] Fix | Delete
#--
[2074] Fix | Delete
# CONVERTING
[2075] Fix | Delete
#++
[2076] Fix | Delete
[2077] Fix | Delete
#
[2078] Fix | Delete
# Creates a single-row matrix from this vector.
[2079] Fix | Delete
#
[2080] Fix | Delete
def covector
[2081] Fix | Delete
Matrix.row_vector(self)
[2082] Fix | Delete
end
[2083] Fix | Delete
[2084] Fix | Delete
#
[2085] Fix | Delete
# Returns the elements of the vector in an array.
[2086] Fix | Delete
#
[2087] Fix | Delete
def to_a
[2088] Fix | Delete
@elements.dup
[2089] Fix | Delete
end
[2090] Fix | Delete
[2091] Fix | Delete
#
[2092] Fix | Delete
# Return a single-column matrix from this vector
[2093] Fix | Delete
#
[2094] Fix | Delete
def to_matrix
[2095] Fix | Delete
Matrix.column_vector(self)
[2096] Fix | Delete
end
[2097] Fix | Delete
[2098] Fix | Delete
def elements_to_f
[2099] Fix | Delete
warn "Vector#elements_to_f is deprecated", uplevel: 1
[2100] Fix | Delete
map(&:to_f)
[2101] Fix | Delete
end
[2102] Fix | Delete
[2103] Fix | Delete
def elements_to_i
[2104] Fix | Delete
warn "Vector#elements_to_i is deprecated", uplevel: 1
[2105] Fix | Delete
map(&:to_i)
[2106] Fix | Delete
end
[2107] Fix | Delete
[2108] Fix | Delete
def elements_to_r
[2109] Fix | Delete
warn "Vector#elements_to_r is deprecated", uplevel: 1
[2110] Fix | Delete
map(&:to_r)
[2111] Fix | Delete
end
[2112] Fix | Delete
[2113] Fix | Delete
#
[2114] Fix | Delete
# The coerce method provides support for Ruby type coercion.
[2115] Fix | Delete
# This coercion mechanism is used by Ruby to handle mixed-type
[2116] Fix | Delete
# numeric operations: it is intended to find a compatible common
[2117] Fix | Delete
# type between the two operands of the operator.
[2118] Fix | Delete
# See also Numeric#coerce.
[2119] Fix | Delete
#
[2120] Fix | Delete
def coerce(other)
[2121] Fix | Delete
case other
[2122] Fix | Delete
when Numeric
[2123] Fix | Delete
return Matrix::Scalar.new(other), self
[2124] Fix | Delete
else
[2125] Fix | Delete
raise TypeError, "#{self.class} can't be coerced into #{other.class}"
[2126] Fix | Delete
end
[2127] Fix | Delete
end
[2128] Fix | Delete
[2129] Fix | Delete
#--
[2130] Fix | Delete
# PRINTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[2131] Fix | Delete
#++
[2132] Fix | Delete
[2133] Fix | Delete
#
[2134] Fix | Delete
# Overrides Object#to_s
[2135] Fix | Delete
#
[2136] Fix | Delete
def to_s
[2137] Fix | Delete
"Vector[" + @elements.join(", ") + "]"
[2138] Fix | Delete
end
[2139] Fix | Delete
[2140] Fix | Delete
#
[2141] Fix | Delete
# Overrides Object#inspect
[2142] Fix | Delete
#
[2143] Fix | Delete
def inspect
[2144] Fix | Delete
"Vector" + @elements.inspect
[2145] Fix | Delete
end
[2146] Fix | Delete
end
[2147] Fix | Delete
[2148] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function