Beispiel #1
0
void IndexEncoding::PrecomputeA(
	int idx_dic_a, int idx_dic_b, 
	const Vector<double> &vec_x_map, 
	const SMatrix<double> &matPrecomputed, 
	int num_dic_each_partition, 
	short* prepresentation, 
	Vector<double> &vec_pre_a, int sub_dic_size) const
{
	vec_pre_a.AllocateSpace(sub_dic_size);
	int off_set_a = idx_dic_a * sub_dic_size;
	int off_set_b = idx_dic_b * sub_dic_size;

	for (int i = 0; i < sub_dic_size; i++)
	{
		int idx_word = off_set_a + i;
		double s = matPrecomputed[idx_word][idx_word];
		for (int j = 0; j < num_dic_each_partition; j++)
		{
			if (j == idx_dic_a || j == idx_dic_b)
			{
				continue;
			}
			int curr_idx = prepresentation[j];
			if (curr_idx != -1)
			{
				s += matPrecomputed[idx_word][curr_idx];
			}
		}
		s -= vec_x_map[idx_word];
		vec_pre_a[i] = s;
	}
}
Beispiel #2
0
void MultiIndexMapper::Map(long long s, Vector<int> &multi_index) const
{
	int length = m_vecProductCumulate.Size();
	multi_index.AllocateSpace(length);
	for (int i = length - 1; i >= 0; i--)
	{
		int idx = s / m_vecProductCumulate[i];
		multi_index[i] = idx;
		s -= idx * m_vecProductCumulate[i];
	}
}
Beispiel #3
0
void IndexEncoding::BestNextWords(const double* p_residual, 
								  const SMatrix<double>& matDictionary, 
								  int idx_start,
								  int idx_end,
								  SMatrix<double> &mat_diff, 
								  Vector<short>& best_idx) const
{
	int m_nDimension = matDictionary.Cols();

	SMatrix<double> mat_each_diff(idx_end - idx_start, m_nDimension);

	Heap<PAIR<double> > pq;
	pq.Reserve(m_nNumBestCan);

	for (int i = idx_start; i < idx_end; i++)
	{
		const double* p_dic = matDictionary[i];

		double e = 0;
		for (int j = 0; j < m_nDimension; j++)
		{
			double d = p_residual[j] - p_dic[j];
			mat_each_diff[i - idx_start][j] = d;
			e += d * d;
		}

		if (pq.size() >= m_nNumBestCan)
		{
			const PAIR<double> &p = pq.Top();
			if (p.distance > e)
			{
				pq.popMin();

				pq.insert(PAIR<double>(i - idx_start, e));
			}
		}
		else
		{
			pq.insert(PAIR<double>(i - idx_start, e));
		}

	}

	mat_diff.AllocateSpace(m_nNumBestCan, m_nDimension);
	best_idx.AllocateSpace(m_nNumBestCan);
	for (int i = m_nNumBestCan - 1; i >= 0; i--)
	{
		PAIR<double> p;
		pq.popMin(p);
		best_idx[i] = p.index;
		memcpy(mat_diff[i], mat_each_diff[p.index], sizeof(double) * m_nDimension);
	}
}
Beispiel #4
0
void IndexEncoding::MultiDictionaryPreprocess(
	const Vector<SMatrix<double> > &vecmatDictionary, 
	int num_dic_each_partition, 
	Vector<SMatrix<double> > &vecmatPrecomputed)  const
{
	int num_partitions = vecmatDictionary.size();

	vecmatPrecomputed.AllocateSpace(num_partitions);

	for (int i = 0; i < num_partitions; i++)
	{
		const SMatrix<double> &matDictionary = vecmatDictionary[i];
		SMatrix<double> &matPrecomputed = vecmatPrecomputed[i];

		MultiDictionaryPreprocess(matDictionary, num_dic_each_partition, matPrecomputed);
	}
}