Sha256: be2720e431e11190d90182188d253f7c969d2688125f550f9f8f20f0c312ea7c

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

class Mat
	def sum(arg=:all)
		case arg
		when Mat
			_sum(arg)
		when :all, :a
			_sum(Mat.new(1, 1, self.vtype))
		when :col, :c
			_sum(Mat.new(1, col_size, self.vtype))
		when :row, :r
			_sum(Mat.new(row_size, 1, self.vtype))
		when :none, :n
			self.dup
		end
	end
	
	def mean(arg=:all)
		foo = sum(arg)
		if foo.kind_of?(Mat)
			if foo.row_size != self.row_size
				foo.e_div(self.row_size)
			else
				foo.e_div(self.col_size)
			end
		elsif arg.kind_of?(Mat)
			arg.e_div(self.length)
			arg[0, 0]
		else
			foo.quo(self.length)
		end
	end
	
	private def _mean(arg, ddof)
		foo = sum(arg)
		if foo.kind_of?(Mat)
			if foo.row_size != self.row_size
				foo.e_div(self.row_size-ddof)
			else
				foo.e_div(self.col_size-ddof)
			end
		elsif arg.kind_of?(Mat)
			arg.e_div(self.length-ddof)
			arg[0, 0]
		else
			foo.quo(self.length)
		end
	end
	def var(arg=:all, ddof: 0)
		foo = mean(arg)
		if foo.kind_of?(Mat)
			bar = foo.repmat(self.row_size.div(foo.col_size), self.col_size.div(foo.col_size))
			bar.sub!(self).e_mul!(bar)
		else
			unless arg.kind_of?(Mat)
				arg = Mat.new(1, 1, self.vtype) do
					foo
				end
			end
			bar = Mat.new(self.row_size, self.col_size, self.vtype)
			bar.fill(foo)
		end
		bar.sub!(self)
		bar.e_mul!(bar)
		bar.__send__(:_mean, arg, ddof)
		if foo.kind_of?(Mat)
			bar
		else
			bar[0, 0]
		end
	end
	alias :variance :var
	
	def std(arg=:all, ddof: 0)
		foo = var(arg, ddof: ddof)
		if foo.kind_of?(Mat)
			foo.sqrt!
		elsif arg.kind_of?(Mat)
			arg.sqrt!
			arg[0, 0]
		else
			Math.sqrt(arg)
		end
	end
	alias :stddev :std
	alias :standard_deviation :std
	
	def geo_mean(arg=:all)
		foo = self.log.mean(arg)
		if foo.kind_of?(Mat)
			foo.exp!
		elsif arg.kind_of?(Mat)
			arg.exp!
			arg[0, 0]
		else
			Math.exp(foo)
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kmat-0.1.0 lib/kmat/statistics.rb
kmat-0.0.3 lib/kmat/statistics.rb