⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ocn_g03.h

📁 图像处理的压缩算法
💻 H
📖 第 1 页 / 共 3 页
字号:
    int m, // the total number of variables.
    double x[], // contain the ith observation for the jth variable, for i = 1, 2, . . ., n; j = 1, 2, . . .,m.
    int tdx,	// the last dimension of the array x.
    int isx[],	// indicates whether or not the jth variable is to be included in the analysis.
    int nx,		// the number of variables in the analysis.
    int ing[],	// indicates which group the ith observation is in, for i = 1, 2, . . ., n. 
    int ng,		// The number of groups.
    double *wtptr, // if weight = Nag Weightsfreq or Nag Weightsvar then the elements of wt must contain the weights to be used in the analysis.
    int nig[],	// the number of observations in group j, for j = 1, 2, . . . , ng.
    double cvm[], // the mean of the jth canonical variate for the ith group, for i = 1, 2, . . . , ng; j = 1, 2, . . . , l; 
    int tdcvm,	// the last dimension of the array cvm.
    double e[], // the statistics of the canonical variate analysis.
    int tde,	// the last dimension of the array e.
    int *ncv,	// the number of canonical variates.
    double cvx[], // the canonical variate loadings.
    int tdcvx,	// the last dimension of the array cvx.
    double tol, // the value of tol is used to decide if the variables are of full rank.
    int *irankx // the rank of the dependent variables.
); // Canonical Coeffiecients, Canonical Correlation, Eigenvalues, Canonical Variate Means....
  
/** g03adc
		performs canonical correlation analysis upon input data matrices.
	
Example:
	A sample of nine observations with two variables in each set is read in. The second and third
	variables are x variables while the first and last are y variables. Canonical variate analysis is performed.

void test_nag_mv_canon_corr()
{
	
	matrix e, cvx, cvy;
	e.SetSize(2,6);
	cvx.SetSize(2,2);
	cvy.SetSize(2,2);
	matrix z = {{80.0, 58.4, 14.0, 21.0}, {75.0, 59.2, 15.0, 27.0}, {78.0, 60.3, 15.0, 27.0},
	{75.0, 57.4, 13.0, 22.0}, {79.0, 59.5, 14.0, 26.0}, {78.0, 58.1, 14.5, 26.0},
	{75.0, 58.0, 12.5, 23.0}, {64.0, 55.5, 11.0, 22.0}, {80.0, 59.2, 12.5, 22.0}};
	double wt[9];
	double tol;	
	double *wtptr;
	int i, j, m = 4, n = 9;
	int ix = 2, iy = 2;
	int nx, ny;
	int ncv;
	int isz[4] = {-1, 1, 1, -1};
	int tdz= 4;
	int tde=6;
	int tdc=2;
	char weight = 'U';

	wtptr = NULL;
	tol = 1e-6;
	nx = ix;
	ny= iy;
	
	nag_mv_canon_corr(n, m, z, tdz, isz, nx, ny, wtptr, e, tde, &ncv, cvx, tdc, cvy, tdc, tol);
	
	printf("\n%s%2ld%s%2ld\n\n","Rank of x = ",nx," Rank of y= ",ny);
	printf(" Canonical Eigenvalues Percentage Chisq DF Sig\n");
	printf(" correlations          variation\n");
	for (i = 0; i < ncv; ++i)
	{
		for (j = 0; j < 6; ++j)
			printf("%12.4f",e[i][j]);
		printf("\n");
	}
	printf("\nCanonical coefficients for x\n");
	for (i = 0; i < ix; ++i)
	{
		for (j = 0; j < ncv; ++j)
			printf("%9.4f",cvx[i][j]);
		printf("\n");
	}
	printf("\nCanonical coefficients for y\n");
	for (i = 0; i < iy ;++i)
	{
		for (j = 0; j < ncv; ++j)
			printf("%9.4f",cvy[i][j]);
		printf("\n");
	}
}

	The output is following:
	
	Rank of x = 2 Rank of y= 2
	Canonical 		Eigenvalues 	Percentage 	Chisq 		DF 			Sig
	correlations 					variation
	
	0.9570 			10.8916 		0.9863 		14.3914 	4.0000 		0.0061
	0.3624 		 	 0.1512 		0.0137 		 0.7744 	1.0000 		0.3789
	
	Canonical coefficients for x
	-0.4261 		 1.0337
	-0.3444 		-1.1136
	
	Canonical coefficients for y
	-0.1415 		 0.1504
	-0.2384 		-0.3424	

Return:
	The function returns NAG error code or 0 if no error. 

	11: On entry, nx must not be less than 1: nx = _value_.  On entry, ny must not be less than 1: ny = _value_.  On entry, tde must not be less than 6: tde = _value_.
	5: On entry, tol must not be less than 0.0: tol = _value_.
	17: On entry, tdz = _value_ while m = _value_.  These parameters must satisfy tdz = m.
	29: On entry, m = _value_, nx = _value_ and ny = _value_.  These parameters must satisfy m = nx + ny.  On entry, n = _value_, nx = _value_ and ny = _value_.  These parameters must satisfy n > nx + ny.  On entry, tdcvx = _value_, nx = _value_ and ny = _value_.  These parameters must satisfy tdcvx = min(nx,ny).  On entry, tdcvy = _value_, nx = _value_ and ny = _value_.  These parameters must satisfy tdcvy = min(nx,ny).  
	500: On entry, wt[_value_] = _value_.  Constraint: When referenced, all elements of wt must be non-negative.
	501: The number of variables, nx in the analysis = _value_, while the number of x variables included in the analysis via array isz = _value_.  Constraint: these two numbers must be the same.  The number of variables, ny in the analysis = _value_, while the number of y variables included in the analysis via array isz = _value_.  Constraint: these two numbers must be the same.  
	503: With weighted data, the e.ective number of observations given by the sum of weights = _value_, while number of variables included in the analysis, nx + ny = _value_.  Constraint: E.ective number of observations = nx + ny + 1.
	427: The singular value decomposition has failed to converge.  This is an unlikely error exit.  
	506: A canonical correlation is equal to one.  This will happen if the x and y variables are perfectly correlated.
	509: The rank of the x matrix or the rank of the y matrix is zero.  This will happen if all the x and y variables are constants.
	73: Memory allocation failed.
	74: An internal error has occurred in this function. Check the function call and array sizes.  If the function call is correct, please consult NAG for assistance.
		
	successfully call of the nag_mv_canon_corr function.		
*/
int nag_mv_canon_corr(
	int n, 		// the number of observations.
	int m, 		// the total number of variables.
	double z[], // contain the ith observation for the jth variable, for i = 1, 2, . . ., n; j = 1, 2, . . .,m.
	int tdz,	// the last dimension of the array z.
	int isz[], 	// indicates whether or not the jth variable is to be included in the analysis and to which set of variables it belongs.
	int nx, 	// the number of x variables in the analysis.
	int ny, 	// the number of y variables in the analysis.
	double wt[], // the weights to be used in the analysis.
	double e[],	// the statistics of the canonical variate analysis.
	int tde, 	// the last dimension of the array e.
	int *ncv, 	// the number of canonical correlations.
	double cvx[], // the canonical variate loadings for the x variables.
	int tdcvx,	// the last dimension of the array cvx.
	double cvy[], // the canonical variate loadings for the y variables.
	int tdcvy, 	// the last dimension of the array cvy.
	double tol 
); // Canonical coefficients, Eigenvalues, Percentage variation, Canonical coefficients...


/** g03bac
		computes orthogonal rotations for a matrix of loadings using a generalized orthomax criterion.
		
Example:
	The example is taken from page 75 of Lawley and Maxwell (1971). The results from a 
	factor analysis of ten variables using three factors are input and rotated using 
	varimax rotations without standardising rows.
	
	
void test_nag_mv_orthomax()
{

	double g = 1.0;
	matrix r, flr;
	r.SetSize(3,3);
	flr.SetSize(10,3);
	matrix fl = {{0.788, -0.152, -0.352}, {0.874, 0.381, 0.041}, {0.814, -0.043, -0.213},
	{0.798, -0.170, -0.204}, {0.641, 0.070, -0.042}, {0.755, -0.298, 0.067}, {0.782, -0.221, 0.028},
	{0.767, -0.091, 0.358}, {0.733, -0.384, 0.229}, {0.771, -0.101, 0.071}};
	double acc = 0.00001;	
	int iter, nvar;
	int i, j, k;
	int maxit = 20;
	int tdf = 3;
	int tdr = 3;
	char char_stand;
	Nag_RotationLoading stand;
	
	nvar = 10;
	k = 3;
	char_stand = 'U';
	
	if (char_stand == 'S')
		stand = Nag_RoLoadStand;
	else
		stand = Nag_RoLoadNotStand;

	nag_mv_orthomax(stand, g, nvar, k, fl, tdf, flr, r, tdr, acc, maxit, &iter);
	
	printf("\n Rotated factor loadings\n\n");
	for (i = 0; i < nvar; ++i)
	{
		for (j = 0; j < k ;++j)
			printf(" %8.3f",flr[i][j]);
		printf("\n");
	}
	printf("\n Rotation matrix\n\n");
	for (i = 0; i < k ;++i)
	{
		for (j = 0; j < k ;++j)
			printf(" %8.3f",r[i][j]);
		printf("\n");
	}	
}	
		
	The output is following:
		
	Rotated factor loadings
	
	0.329 	-0.289 	-0.759
	0.849 	-0.273 	-0.340
	0.450 	-0.327 	-0.633
	0.345 	-0.397 	-0.657
	0.453 	-0.276 	-0.370
	0.263 	-0.615 	-0.464
	0.332 	-0.561 	-0.485
	0.472 	-0.684 	-0.183
	0.209 	-0.754 	-0.354
	0.423 	-0.514 	-0.409
	
	Rotation matrix
	
	0.633 	-0.534 	-0.560
	0.758 	 0.573 	 0.311
	0.155 	-0.622 	 0.768

Return:
	The function returns NAG error code or 0 if no error. 
	
	70: On entry, parameter stand had an illegal value.
	11: On entry, k must not be less than 2: k = _value_.
	12: On entry, maxit must not be less than or equal to 0 : maxit = _value_.
	5:  On entry, g must not be less than 0.0: g = _value_.  On entry, acc must not be less than 0.0: acc = _value_.
	17: On entry, nvar = _value_ while k = _value_.  These parameters must satisfy nvar = k.  On entry, tdf = _value_ while k = _value_.  These parameters must satisfy tdf = k.  On entry, tdr = _value_ while k = _value_.  These parameters must satisfy tdr = k.
	427: The singular value decomposition has failed to converge.  This is an unlikely error exit.
	511: The algorithm to .nd R has failed to reach the required accuracy in the given number of iterations, _value_. Try increasing acc or increasing maxit. The returned solution should be a reasonable approximation.
	73: Memory allocation failed.  
	74: An internal error has occurred in this function. Check the function call and array sizes.  If the function call is correct, please consult NAG for assistance.  
		
	successfully call of the nag_mv_orthomax function.	
*/
int nag_mv_orthomax(
    Nag_RotationLoading stand,
    double g, // the criterion constant.
    int nvar, // The number of original variables.
    int k,	// The number of derived variates or factors.	  
    double fl[], // the matrix of loadings.
    int tdf, 	// the last dimension of the arrays fl and flr.
    double flr[], // the rotated matrix of loadings.
    double r[], // the matrix of rotations.
    int tdr,	// the last dimension of the array r.
    double acc, // indicates the accuracy required.
    int maxit,	// the maximum number of iterations.
    int *iter	// the number of iterations performed.
); // Rotation factor loading, Rotation matrix.

/** g03bcc
		computes Procrustes rotations in which an orthogonal rotation is found 
		so that a transformed matrix best matches a target matrix.
	
Example:
	Three points representing the vertices of a triangle in two dimensions are input. 
	The points are translated and rotated to match the triangle given by (0,0),(1,0),(0,2) 
	and scaling is applied after rotation. 

void test_nag_mv_procustes()
{
	matrix r, yhat;
	r.SetSize(2,2);
	yhat.SetSize(3,2);
	matrix x = {{0.63, 0.58}, {1.36, 0.39}, {1.01, 1.76}};
	matrix y = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 2.0}};
	double res[3];
	double alpha;
	double rss;
	int i, j, m = 2, n = 3;
	int tdx = 2;
	int tdr = 2;
	int tdy = 2;
	char char_scale;
	char char_stand;
	Nag_TransNorm stand;
	Nag_RotationScale scale;

	char_stand = 'C';
	char_scale = 'S';

	if (char_stand == 'N')
		stand = Nag_NoTransNorm;
	
	else if (char_stand == 'Z')
		stand = Nag_Orig;
	
	else if (char_stand == 'C')
		stand = Nag_OrigCentroid;
	
	else if (char_stand == 'U')
		stand = Nag_Norm;
	
	else if (char_stand == 'S')
		stand = Nag_OrigNorm;
	
	else if (char_stand == 'M')
		stand = Nag_OrigNormCentroid;
	
	if (char_scale == 'S')
		scale = Nag_LsqScale;
	
	else if (char_scale == 'U')
		scale = Nag_NotLsqScale;
	
	nag_mv_procustes(stand, scale, n, m, x, tdx, y, tdy, yhat, r, tdr, &alpha, &rss, res);
	
	printf("\n Rotation matrix\n\n");
	for (i = 0; i < m; ++i)
	{
		for (j = 0; j < m; ++j)
			printf(" %7.3f ",r[i][j]);
		printf("\n");
	}
	if (char_scale == 'S' || char_scale == 's')
		printf("\n%s%10.3f\n"," Scale factor = ",alpha);
	
	printf("\n Target matrix \n\n");
	for (i = 0; i < n; ++i)
	{
		for (j = 0; j < m; ++j)
			printf(" %7.3f ",y[i][j]);
		printf("\n");
	}
	printf("\n Fitted matrix\n\n");
	for (i = 0; i < n; ++i)
	{
		for (j = 0; j < m; ++j)
			printf(" %7.3f ",yhat[i][j]);
		printf("\n");
	}
	printf("\n%s%10.3f\n","RSS = ",rss);	
}	
	
	
	The output is following:
	
	Rotation matrix
	 0.967 		0.254
	-0.254 		0.967
	
	Scale factor = 1.556
	
	Target matrix
	0.000 	0.000
	1.000 	0.000
	0.000 	2.000
	
	Fitted matrix
	-0.093 		0.024
	 1.080 		0.026

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -