Module: NumRu::Misc

Defined in:
lib/numru/misc/misc.rb,
lib/numru/misc/emath.rb,
lib/numru/misc/keywordopt.rb,
lib/numru/misc/md_iterators.rb

Defined Under Namespace

Modules: EMath, MD_Iterators Classes: HelpMessagingException, KeywordOpt, KeywordOptAutoHelp

Class Method Summary collapse

Class Method Details

.check_shape_consistency(cshapes, *args) ⇒ Object



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
# File 'lib/numru/misc/misc.rb', line 92

def check_shape_consistency(cshapes, *args)
	 ranks = Array.new
	 elm2idx = Hash.new
	 spl = cshapes.split(' +')
	 if spl.length >= 2 && /^\.\.\.?$/ =~ spl[-1]  # '..' or '...'
      ((spl.length-1)...args.length).each{|i|
  spl[i]=spl[i-1]
	    }	       
	 end
	 if spl.length != args.length
	    raise ArgumentError,"# of the argument (#{args.length}) is inconsistent with the 1st arg '#{cshapes}'"
	 end
   spl.each_with_index{|csh,i| 
	    sh = csh.split(',')
	    ranks.push( sh.length )
	    sh.each_with_index{|tag,j|
  elm2idx[tag] = Array.new if !elm2idx[tag]
  elm2idx[tag].push([i,j])
	    }
	 }
	 ranks.each_with_index{|len,i|
	    if args[i].rank != len
  raise "(#{i+1}th arg) unexepected rank #{args[i].rank} for #{len}"
	    end
	 }
	 elm2idx.each{|tag,ary|
	    if tag.to_i > 0   # numeric (positive integer)
  size = tag.to_i
  start = 0
	    else              # alphabet
  size = args[ary[0][0]].shape[ary[0][1]]
  start = 1
	    end
	    (start...ary.length).each{|i|
  if args[ary[i][0]].shape[ary[i][1]] != size
		  if start == 0
 raise "length of dim #{ary[i][1]} of #{ary[i][0]+1}th "+
                     "arg is unexpected " +
                     "(#{args[ary[i][0]].shape[ary[i][1]]} for #{size})"
		  else
 raise "Dimension lengths inconsistent between "+
			"dim #{ary[0][1]} of #{ary[0][0]+1}th arg and " +
    "dim #{ary[i][1]} of #{ary[i][0]+1}th arg"
		  end
  end
	    }
	 }
	 nil
end