Ejemplo n.º 1
0
void eig(const Matrix& A, Matrix& E) {
	Array2D< double > AA(A.row_num(), A.col_num(), const_cast< double* >(A.GetBuffer()));
	Eigenvalue< double > eig_core(AA);
	Array1D< double > res_eigs;
	eig_core.getRealEigenvalues(res_eigs);
	assign_mat_ar1d(E, res_eigs);
}
Ejemplo n.º 2
0
void svd(const Matrix& A, Matrix& U, Matrix& E, Matrix& V)
{
	Array2D<double> AA(A.row_num(), A.col_num(), const_cast< double* >(A.GetBuffer()));
	SVD<double> kernel(AA);
	Array2D<double> AU, AE, AV;
	kernel.getU(AU); kernel.getV(AV); kernel.getS(AE);
	assign_mat_ar2d(U, AU);
	assign_mat_ar2d(V, AV);
	assign_mat_ar2d(E, AE);
}
Ejemplo n.º 3
0
//template< int cdm_method >
norm_tools::dist_stat calc_dist_matrix_impl(const Matrix& data, Matrix& dist, norm_tools::vm_norm_fn_t norm2_fn)
{
	//some constant values used
	const ulong points_num = data.row_num();
	const double mult = 2.0/(points_num * (points_num - 1));
	const double mult1 = 1.0/data.row_num();

	//resize distance matrix
	dist(points_num, points_num);
	//statistics
	norm_tools::dist_stat stat;
	//make a copy of input data
	Matrix data_cpy(data.row_num(), data.col_num(), data.GetBuffer());
	//locally used matrices
	Matrix dist_row(1, data.row_num()), dv, norm;
	//mean distance^2
	double meand2 = 0, meand2_nn = 0;
	//current min distance (between nearest neighbours)
	double cur_mind = 0;

	for(ulong i = 0; i < points_num - 1; ++i) {
		dv <<= data_cpy.GetRows(0);
		data_cpy.DelRows(0);
		//calc norms^2
		norm <<= norm2_fn(dv, data_cpy);
		//update mean distance^2
		meand2 += mult * norm.Sum();
		//update mean nn distance^2
		meand2_nn += mult1 * cur_mind * cur_mind;

		//calc norm
		transform(norm, ptr_fun< double, double >(std::sqrt));

		dist_row = 0;
		dist_row.SetColumns(norm, i + 1, norm.size());
		dist.SetRows(dist_row, i);
		//calc mean
		stat.mean_ += mult * norm.Sum();
		//calc distance to nearest neighbour
		cur_mind = norm.Min();
		if(i == 0 || cur_mind < stat.min_) stat.min_ = cur_mind;
		//update mean distance to nearest neighbour
		stat.mean_nn_ += cur_mind * mult1;
	}
	//fill lower triangle of distance matrix
	dist <<= dist + !dist;

	//calc mse
	stat.mse_ = sqrt(stat.mean_*(meand2/stat.mean_ - stat.mean_));
	stat.mse_nn_ = sqrt(stat.mean_nn_*(meand2_nn/stat.mean_nn_ - stat.mean_nn_));
	return stat;
}