-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixFactory.rb
More file actions
36 lines (35 loc) · 835 Bytes
/
MatrixFactory.rb
File metadata and controls
36 lines (35 loc) · 835 Bytes
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
require_relative 'SparseMatrix'
require_relative 'NDimensionalMatrix'
require 'test/unit'
class MatrixFactory
include Test::Unit::Assertions
def initialize
end
def create_matrix(arg)
assert_respond_to(arg, :to_a)
assert_respond_to(arg, :flatten)
arg=arg.to_a
dim=[arg.length,arg[0].length]
if(arg.flatten.length/2<arg.flatten.count(0))
return SparseMatrix.new(*dim).init_array(arg)
else
return NDimensionalMatrix.new(arg)
end
end
end
# b = SparseMatrix.new([[1,0],[0,0],[1,0]])
# myhash=Hash.new
# myhash[[0,0]]=1
#
# c =SparseMatrix.new(2,2)
# c.insert_at([1,0],20)
# d=SparseMatrix.new(2,2,myhash)
# puts b
# puts d
# puts c
# factory=MatrixFactory.new
# a=factory.create_matrix([[1,0],[0,0]])
# b=factory.create_matrix([[1,0],[1,1],[1,0]])
# puts a.class
# puts b.class
# puts a*2