Example #1
0
	def frame0_to_1(self, in_):
		"""
		Allocating version of :meth`AbstractMapping.frame1_to_0`.

		If input array is a matrix, mapping operates on rows. If array
		is a vector, mapping operates on entries.

		Arguments:
			in_: Input array with :attr:`AbstractMapping.n_frame0` rows
				and ``k`` >= ``1`` columns.

		Returns:
			:class:`numpy.ndarray`: Array with
			:attr:`AbstractMapping.n_frame0` rows and same number of
			columns as input. Input entries are mapped one-to-one or
			many-to-one *into* output.
		"""
		if is_vector(in_):
			out_ = zeros(self.__n_frame0)
		elif sparse_or_dense(in_):
			dim1 = self.__n_frame0
			dim2 = in_.shape[1]
			out_ = zeros((dim1, dim2))

		return self.frame1_to_0_inplace(in_, out_)
Example #2
0
	def frame0_to_1(self, in_):
		"""
		Map elements of array ``in_`` to a new array.

		If input array is a matrix, mapping operates on rows. If array
		is a vector, mapping operates on entries.

		A new empty vector or matrix

		Arguments:
			in_: Input array with :attr:`AbstractMapping.n_frame0` rows
				and ``k`` >= ``1`` columns.

		Returns:
			:class:`numpy.ndarray`: Array with
			:attr:`AbstractMapping.n_frame1` rows and ``k`` columns.
			Input entries are mapped one-to-one or one-to-many *into*
			output.
		"""
		if is_vector(in_):
			out_ = zeros(self.__n_frame1)
		elif sparse_or_dense(in_):
			dim1 = self.__n_frame1
			dim2 = in_.shape[1]
			out_ = zeros((dim1, dim2))

		return self.frame0_to_1_inplace(in_, out_)
Example #3
0
	def frame1_to_0(self, in_):
		"""
		Allocating version of :meth`DiscreteMapping.frame1_to_0`.

		If input array is a matrix, mapping operates on rows. If array
		is a vector, mapping operates on entries.

		Arguments:
			in_: Input array with :attr:`DiscreteMapping.n_frame0` rows
				and ``k`` >= ``1`` columns.

		Returns:
			:class:`numpy.ndarray`: Array with
			:attr:`DiscreteMapping.n_frame0` rows and same number of
			columns as input. Input entries are mapped one-to-one or
			many-to-one *into* output.
		"""
		if is_vector(in_):
			out_ = np.zeros(self.__n_frame0)
		elif sparse_or_dense(in_):
			dim1 = self.__n_frame0
			dim2 = in_.shape[1]
			out_ = np.zeros((dim1, dim2))

		return self.frame1_to_0_inplace(in_, out_)
Example #4
0
	def frame0_to_1(self, in_):
		"""
		Map elements of array ``in_`` to a new array.

		If input array is a matrix, mapping operates on rows. If array
		is a vector, mapping operates on entries.

		A new empty vector or matrix

		Arguments:
			in_: Input array with :attr:`DiscreteMapping.n_frame0` rows
				and ``k`` >= ``1`` columns.

		Returns:
			:class:`numpy.ndarray`: Array with
			:attr:`DiscreteMapping.n_frame1` rows and ``k`` columns.
			Input entries are mapped one-to-one or one-to-many *into*
			output.
		"""
		if is_vector(in_):
			out_ = np.zeros(self.__n_frame1)
		elif sparse_or_dense(in_):
			dim1 = self.__n_frame1
			dim2 = in_.shape[1]
			out_ = np.zeros((dim1, dim2))

		return self.frame0_to_1_inplace(in_, out_)
Example #5
0
	def frame0_to_1_inplace(self, in_, out_, clear_output=False):
		"""
		Map elements of array ``in_`` to elements of array ``out_``.

		Procedure::
			# let forward_map be the map: SET_0 --> SET_1.
			# for each INDEX_0, INDEX_1 in forward_map, do
			# 	out_[INDEX_1] += in_[INDEX_0]
			# end for

		If arrays are matrices, mapping operates on rows. If arrays are
		vectors, mapping operates on entries.

		Arguments:
			in_: Input array with :attr:`AbstractMapping.n_frame0` rows
				and ``k`` >= ``1`` columns.
			out_: Output array with :attr:`AbstractMapping.n_frame1`
				rows and ``k`` >= 1 columns. Modified in-place.
			clear_output (:obj:`bool`, optional): If ``True``, set
				output array to ``0`` before adding input values.

		Returns:
			Vector `out_`, after in-place modification.

		Raises:
			TypeError: If input and output arrays are not (jointly)
				vectors or matrices.
			ValueError: If input and output array dimensions are not
				compatible with each other or consistent with the
				dimensions of the mapping.
		"""
		vector_processing = is_vector(in_) and is_vector(out_)
		matrix_processing = sparse_or_dense(in_) and sparse_or_dense(out_)

		if not vector_processing or matrix_processing:
			raise TypeError('arguments "in_" and "out_" be numpy or '
							'scipy vectors or matrices')

		dim_in1 = in_.shape[0]
		dim_out1 = out_.shape[0]
		if vector_processing:
			dim_in2 = 1
			dim_out2 = 1
		else:
			dim_in2 = in_.shape[1]
			dim_out2 = out_.shape[1]

		if bool(
				dim_in_1 != self.n_frame0 or
				dim_out1 != self.n_frame1 or
				dim_in2 != dim_out2):
			raise ValueError('arguments "in_" and "out_" be vectors or '
							 'matrices of dimensions M x N, and K x N, '
							 'respectively, with:\nM = {}\nK={}\n'
							 'Provided:\n input: {}x{}\noutput: {}x{}'
							 ''.format(self.n_frame0, self.n_frame1,
							 dim_in1, dim_in2, dim_out1, dim_out2))
		if clear_output:
			out_ *= 0

		if vector_processing:
			for idx_0, idx_1 in enumerate(self.vec):
				out_[idx_1] += in_[idx_0]

		if matrix_processing:
			for idx_0, idx_1 in enumerate(self.vec):
				out_[idx_1, :] += in_[idx_0, :]

		return out_
Example #6
0
	def frame0_to_1_inplace(self, in_, out_, clear_output=False):
		"""
		Map elements of array ``in_`` to elements of array ``out_``.

		Procedure::
			# let forward_map be the map: SET_0 --> SET_1.
			# for each INDEX_0, INDEX_1 in forward_map, do
			# 	out_[INDEX_1] += in_[INDEX_0]
			# end for

		If arrays are matrices, mapping operates on rows. If arrays are
		vectors, mapping operates on entries.

		Arguments:
			in_: Input array with :attr:`DiscreteMapping.n_frame0` rows
				and ``k`` >= ``1`` columns.
			out_: Output array with :attr:`DiscreteMapping.n_frame1`
				rows and ``k`` >= 1 columns. Modified in-place.
			clear_output (:obj:`bool`, optional): If ``True``, set
				output array to ``0`` before adding input values.

		Returns:
			Vector `out_`, after in-place modification.

		Raises:
			TypeError: If input and output arrays are not (jointly)
				vectors or matrices.
			ValueError: If input and output array dimensions are not
				compatible with each other or consistent with the
				dimensions of the mapping.
		"""
		vector_processing = is_vector(in_) and is_vector(out_)
		matrix_processing = sparse_or_dense(in_) and sparse_or_dense(out_)
		if not (vector_processing or matrix_processing):
			raise TypeError('arguments "in_" and "out_" be numpy or '
							'scipy vectors or matrices')

		dim_in1 = in_.shape[0]
		dim_out1 = out_.shape[0]
		if vector_processing:
			dim_in2 = 1
			dim_out2 = 1
		else:
			dim_in2 = in_.shape[1]
			dim_out2 = out_.shape[1]

		if bool(
				dim_in1 != self.n_frame0 or
				dim_out1 != self.n_frame1 or
				dim_in2 != dim_out2):
			raise ValueError('arguments "in_" and "out_" be vectors or '
							 'matrices of dimensions M x N, and K x N, '
							 'respectively, with:\nM = {}\nK={}\n'
							 'Provided:\n input: {}x{}\noutput: {}x{}'
							 ''.format(self.n_frame0, self.n_frame1,
							 dim_in1, dim_in2, dim_out1, dim_out2))
		if clear_output:
			out_ *= 0

		if vector_processing:
			for idx_0, idx_1 in enumerate(self.vec):
				out_[idx_1] += in_[idx_0]
		else:
			for idx_0, idx_1 in enumerate(self.vec):
				out_[idx_1, :] += in_[idx_0, :]
		# TODO: use efficient slicing when input is sparse. warn when output sparse??

		return out_