-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDimensionalMatrix.rb
More file actions
174 lines (141 loc) · 3.29 KB
/
NDimensionalMatrix.rb
File metadata and controls
174 lines (141 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'matrix'
require 'pp'
require 'test/unit'
require_relative 'SparseMatrixPrePost'
class NDimensionalMatrix
attr_accessor :dimension, :values_hash
include Test::Unit::Assertions
def initialize(*args)
begin
*rest_of_args,input_hash=*args
init_Hash(*rest_of_args,input_hash)
rescue
begin
init_dim_val *args
rescue
begin
init_sparse_matrix *args
rescue
init_matrix *args
end
end
end
end
def init_matrix *args
pre_init_matrix(*args)
m=args[0]
@arr=m.to_a
@dimension=[m.to_a.length,m.to_a[0].length]
end
def init_sparse_matrix(*args)
pre_init_sparse_matrix(*args)
sm=args[0]
init_Hash(*sm.getDimension,sm.get_sparse_matrix_hash)
end
def init_dim_val(*args)
*rest, value= args
@arr=recursive_nest_array(*rest,value)
@dimension=*rest
end
def get_array
return @arr
end
def init_Hash(*rest_of_args,input_hash)
pre_init_hash(*rest_of_args,input_hash)
init_dim_val(*rest_of_args,0)
input_hash.each do |key, value|
@arr[key[0]][key[1]]=value
end
@values_hash=input_hash
@dimension=*rest_of_args
end
def recursive_nest_array(*args,value)
arg1,*rest = args
Array.new(arg1){rest.empty? ? value: recursive_nest_array(*rest,value)}
end
# Insert values into matrix
def insert_at(position,value)
pre_insert_at(position,value)
@values_hash[position] = value
post_insert_at(position,value)
end
def to_s
@values_hash.each do |key, array|
puts "#{key}---#{array}"
end
end
def invariants()
assert(size >= 1,"not a valid matrix dimension")
assert (self.respond_to? (:checkSum)), "It is not a Dense matrix whoops"
end
# we also cannot insert zeros as they are implied to be there.
def pre_insert_at(position,value)
invariants
assert_equal position.length, @dimension.length,"Invalid position."
for i in 0..@dimension.length-1 do
assert(@dimension[i]>position[i], "Invalid position in matrix")
end
end
# ensures that the value is inserted to the matrix
def post_insert_at(position,value)
invariants
assert_equal value,@values_hash[position],"Value is not inserted. FAILED."
end
#returns the size
def size()
return @dimension.inject(:*)
end
def getDimension
return self.dimension
end
def getValues
return self.values_hash
end
def checkSum
preCheckSum(@arr)
result = @arr.flatten.inject(:+)
postCheckSum(result)
return result
end
def preCheckSum(matrix)
assert(matrix.respond_to? (:flatten), 'not checking sum of array')
invariants
end
def postCheckSum(sum)
assert sum.respond_to? (:round), 'not returning a value as check sum'
invariants
end
def to_s
# @arr.inspect
get_2d_matrix.to_a.map(&:inspect).inspect
end
def get_2d_matrix
Matrix[*@arr]
end
def printMatrix
puts get_2d_matrix.to_a.map(&:inspect)
end
def * (m)
puts m.class
assert(self.respond_to?(:get_2d_matrix), 'self not NDimensionalMatrix')
assert(m.respond_to?(:get_2d_matrix), 'other not NDimensionalMatrix')
NDimensionalMatrix.new(self.get_2d_matrix*m.get_2d_matrix)
end
def **(m)
NDimensionalMatrix.new(self.get_2d_matrix**m)
end
def +(m)
begin
self.get_2d_matrix+m.get_2d_matrix
rescue
self.get_2d_matrix+m
end
end
def det
result = self.get_2d_matrix.determinant
return result
end
def inv
NDimensionalMatrix.new(self.get_2d_matrix.inverse)
end
end