Class: SyMath::Matrix
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Value
#*, #**, #<, #<=, #<=>, #>, #>=, #^, #add, #base, compose_with_simplify, create, #deep_clone, #div, #dump, #evaluate, #exponent, #factors, #inspect, #inv, #is_divisor_factor?, #is_finite?, #is_nan?, #is_negative?, #is_negative_number?, #is_number?, #is_positive?, #is_prod_exp?, #is_sum_exp?, #is_unit_quaternion?, #is_zero?, #mul, #neg, #power, #reduce, #reduce_modulo_sign, #sign, #sub, #terms, #to_m, #wedge
#flat, #hodge, #sharp
#anti_derivative, #get_linear_constants, initialize, #int_constant, #int_failure, #int_function, #int_inv, #int_pattern, #int_power, #int_product, #int_sum, #integral_bounds
#_d_wedge, #d, #d_failure, #d_fraction, #d_function, #d_function_def, #d_power, #d_product, initialize
Methods included from Operation
#iterate, #recurse
#combfrac_add_term, #combfrac_sum, #combine_fractions, #expand, #expand_product, #expand_single_pass, #factorize, #factorize_integer_poly, #factorize_simple, #has_fractional_terms?
#combine_factors, #compare_factors_and_swap, #normalize, #normalize_matrix, #normalize_power, #normalize_product, #normalize_single_pass, #normalize_sum, #order_product, #product_on_fraction_form, #reduce_constant_factors, #replace_combined_factors, #swap_factors
#build_assoc_op, #match, #match_assoc, #match_replace
Constructor Details
#initialize(data) ⇒ Matrix
Returns a new instance of Matrix.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/symath/matrix.rb', line 7
def initialize(data)
raise 'Not an array: ' + data.to_s if !data.is_a?(Array)
raise 'Array is empty' if data.length == 0
if data[0].is_a?(Array) then
@nrows = data.length
raise 'Number of columns is zero' if data[0].length == 0
@ncols = data[0].length
data.each do |r|
raise 'Row is not array' if !r.is_a?(Array)
raise 'Row has invalid length' if r.length != @ncols
end
@elements = data.map { |r| r.map { |c| c.to_m } }
else
@nrows = 1
@ncols = data.length
@elements = [data.map { |c| c.to_m }]
end
end
|
Instance Attribute Details
#ncols ⇒ Object
Returns the value of attribute ncols.
5
6
7
|
# File 'lib/symath/matrix.rb', line 5
def ncols
@ncols
end
|
#nrows ⇒ Object
Returns the value of attribute nrows.
5
6
7
|
# File 'lib/symath/matrix.rb', line 5
def nrows
@nrows
end
|
Instance Method Details
#+(other) ⇒ Object
112
113
114
|
# File 'lib/symath/matrix.rb', line 112
def +(other)
return add(other)
end
|
#-(other) ⇒ Object
128
129
130
|
# File 'lib/symath/matrix.rb', line 128
def -(other)
return sub(other)
end
|
#-@ ⇒ Object
142
143
144
|
# File 'lib/symath/matrix.rb', line 142
def -@()
return neg
end
|
#/(other) ⇒ Object
92
93
94
|
# File 'lib/symath/matrix.rb', line 92
def /(other)
return div(other)
end
|
#==(other) ⇒ Object
Also known as:
eql?
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/symath/matrix.rb', line 219
def ==(other)
return false if !other.is_a?(SyMath::Matrix)
return false if nrows != other.nrows
return false if ncols != other.ncols
(0..@nrows - 1).each do |r|
(0..@ncols - 1).each do |c|
return false if self[r, c] != other[r, c]
end
end
return true
end
|
#[](i, j) ⇒ Object
52
53
54
|
# File 'lib/symath/matrix.rb', line 52
def [](i, j)
return @elements[i][j]
end
|
#adjugate ⇒ Object
The adjugate of a matrix is the transpose of the cofactor matrix
157
158
159
160
161
162
163
|
# File 'lib/symath/matrix.rb', line 157
def adjugate()
data = (0..@ncols - 1).map do |c|
(0..@nrows - 1).map { |r| cofactor(r, c) }
end
return SyMath::Matrix.new(data)
end
|
#cofactor(r, c) ⇒ Object
The cofactor of an element is the minor given by the rows and columns not including the element, multiplied by a sign factor which alternates for each row and column
206
207
208
209
210
211
|
# File 'lib/symath/matrix.rb', line 206
def cofactor(r, c)
sign = (-1)**(r + c)
rows = (0..@nrows - 1).to_a - [r]
cols = (0..@ncols - 1).to_a - [c]
return minor(rows, cols)*sign.to_m
end
|
#col(i) ⇒ Object
48
49
50
|
# File 'lib/symath/matrix.rb', line 48
def col(i)
return @elements.map { |r| r[i] }
end
|
#determinant ⇒ Object
165
166
167
168
169
|
# File 'lib/symath/matrix.rb', line 165
def determinant()
raise 'Matrix is not square' if !is_square?
return minor((0..@nrows - 1).to_a, (0..@ncols - 1).to_a)
end
|
#hash ⇒ Object
40
41
42
|
# File 'lib/symath/matrix.rb', line 40
def hash()
return [0, 0].hash
end
|
#inverse ⇒ Object
150
151
152
153
154
|
# File 'lib/symath/matrix.rb', line 150
def inverse()
raise 'Matrix is not square' if !is_square?
return adjugate.matrix_div(determinant)
end
|
#is_associative? ⇒ Boolean
35
36
37
|
# File 'lib/symath/matrix.rb', line 35
def is_associative?()
return true
end
|
#is_commutative? ⇒ Boolean
31
32
33
|
# File 'lib/symath/matrix.rb', line 31
def is_commutative?()
return false
end
|
#is_square? ⇒ Boolean
56
57
58
|
# File 'lib/symath/matrix.rb', line 56
def is_square?()
return @ncols == @nrows
end
|
#matrix_add(other) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/symath/matrix.rb', line 96
def matrix_add(other)
if other.is_a?(SyMath::Minus) and other.argument.is_a?(SyMath::Matrix)
return self.matrix_sub(other.argument)
end
raise 'Invalid dimensions' if @ncols != other.ncols or @nrows != other.nrows
data = (0..@nrows - 1).map do |r|
(0..@ncols - 1).map do |c|
self[r, c] + other[r, c]
end
end
return SyMath::Matrix.new(data)
end
|
#matrix_div(other) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/symath/matrix.rb', line 82
def matrix_div(other)
raise 'Cannot divide matrix by matrix' if other.is_a?(SyMath::Matrix)
data = (0..@nrows - 1).map do |r|
(0..@ncols - 1).map { |c| self[r, c]/other }
end
return SyMath::Matrix.new(data)
end
|
#matrix_mul(other) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/symath/matrix.rb', line 60
def matrix_mul(other)
if !other.is_a?(SyMath::Matrix)
data = (0..@nrows - 1).map do |r|
(0..@ncols - 1).map { |c| self[r, c]*other }
end
return SyMath::Matrix.new(data)
end
raise 'Invalid dimensions' if @ncols != other.nrows
data = (0..@nrows - 1).map do |r|
(0..other.ncols - 1).map do |c|
(0..@ncols - 1).map do |c2|
self[r, c2]*other[c2, c]
end.inject(:+)
end
end
return SyMath::Matrix.new(data)
end
|
#matrix_neg ⇒ Object
132
133
134
135
136
137
138
139
140
|
# File 'lib/symath/matrix.rb', line 132
def matrix_neg()
data = @elements.map do |r|
r.map do |e|
- e
end
end
return SyMath::Matrix.new(data)
end
|
#matrix_sub(other) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/symath/matrix.rb', line 116
def matrix_sub(other)
raise 'Invalid dimensions' if @ncols != other.ncols or @nrows != other.nrows
data = (0..@nrows - 1).map do |r|
(0..@ncols - 1).map do |c|
self[r, c] - other[r, c]
end
end
return SyMath::Matrix.new(data)
end
|
#minor(rows, cols) ⇒ Object
The minor is the determinant of a submatrix. The submatrix is given by the rows and cols which are arrays of indexes to the rows and columns to be included
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/symath/matrix.rb', line 174
def minor(rows, cols)
raise 'Not square' if rows.length != cols.length
if rows.length == 1
return self[rows[0], cols[0]]
end
ret = 0.to_m
sign = 1
subrows = rows - [rows[0]]
cols.each do |c|
subcols = cols - [c]
if (sign > 0)
ret += self[rows[0], c]*minor(subrows, subcols)
else
ret -= self[rows[0], c]*minor(subrows, subcols)
end
sign *= -1
end
return ret
end
|
#row(i) ⇒ Object
44
45
46
|
# File 'lib/symath/matrix.rb', line 44
def row(i)
return @elements[i]
end
|
#to_s ⇒ Object
236
237
238
239
240
|
# File 'lib/symath/matrix.rb', line 236
def to_s()
return '[' + @elements.map { |r| r.map { |c| c.to_s }.join(', ') }.join('; ') + ']'
end
|
#trace ⇒ Object
213
214
215
216
217
|
# File 'lib/symath/matrix.rb', line 213
def trace()
raise 'Matrix is not square' if !is_square?
return (0..@nrows - 1).map { |i| self[i, i] }.inject(:+)
end
|
#transpose ⇒ Object
146
147
148
|
# File 'lib/symath/matrix.rb', line 146
def transpose()
return SyMath::Matrix.new(@elements.transpose)
end
|
#type ⇒ Object
242
243
244
|
# File 'lib/symath/matrix.rb', line 242
def type()
return SyMath::Type.new('matrix', dimn: ncols, dimm: nrows)
end
|