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

📄 ocn_g03.h

📁 图像处理的压缩算法
💻 H
📖 第 1 页 / 共 3 页
字号:
/* <OCN_g03.h>
 *
 * Copyright 1996 Numerical Algorithms Group
 *
 * Include file for NAG C Library g03 Chapter
 *
 * Mark 5, 1997.
 * Mark 6 revised. IER-3042 (July 2000).
 */

#ifndef NAGG03
#define NAGG03

//#importdll "ONAG" // NAG DLL prepared by OriginLab
#pragma dll(ONAG)	
#include <NAG\nag_types.h>


/* begin proto */

/** g03aac
		performs a principal component analysis on a data matrix; both the
		principal component loadings and the principal component scores are returned.

Example 1:
	Assume we have four Matrix, "Matrix1"'s size is 10X3 and stores 30 number; "Matrix2"'s
	size is 3X6; "Matrix3"'s size is 3X3, and "Matrix4"'s size is 12X3.  We will store the 
	value to "Matrix2", "Matrix3" and "Matrix4".
	
	matrix p;
	p.SetSize(3,3);
	double s[3];
	matrix e;
	e.SetSize(3,6);
	matrix v;
	v.SetSize(12,3);
	double wt[12];
	double *wtptr;
	wtptr = NULL;
	int isx[3] = {1, 1, 1};
	int nvar = 3;
	int tdx = 3; 
	int tde = 6;
	int tdp = 3;
	int tdv = 3;
	int i, j, m = 3, n = 10;
	Nag_PrinCompMat pcmatrix;
	Nag_PrinCompScores scores;
	Matrix xx("Matrix1");
	matrix x = xx;
	char weight, mat, std;
	mat = 'V';
	std = 'E';
	weight = 'U';

	if (mat == 'C')
		pcmatrix = Nag_MatCorrelation;
	else if (mat == 'S')
		pcmatrix = Nag_MatStandardised;
	else if (mat == 'U')
		pcmatrix = Nag_MatSumSq;
	else
		pcmatrix = Nag_MatVarCovar;
	if (std == 'S')
		scores = Nag_ScoresStand;
	else if (std == 'U')
		scores = Nag_ScoresNotStand;
	else if (std == 'Z')
		scores = Nag_ScoresUnitVar;
	else
		scores = Nag_ScoresEigenval;
	
	nag_mv_prin_comp(pcmatrix, scores, n, m, x, tdx, isx, s, wtptr, nvar,
				e, tde, p, tdp, v, tdv);
	//put the result to the matrix.
	//first column is Eigenvalues, second column is Percentage variation, 
	//third column is Cumulative variation, fourth column is Chisp, 
	//fifth column is DF and sixth column is Sig.	
	Matrix ee("Matrix2");
	ee = e;
	Matrix pp("Matrix3");		//Eigenvalues
	pp = p;
	Matrix vv("Matrix4");		//Principal component scores
	vv = v;		
Example 2:
	A data set is taken from Cooley and Lohnes (1971), it consists of ten observations on 
	three variables.  The unweighted principal components based on the variance-covariance 
	matrix are computed and unstandardised principal component scores requested.

void test_nag_mv_prin_comp()
{
	matrix p;
	p.SetSize(3,3);
	double s[3];
	matrix e;
	e.SetSize(3,6);
	matrix v;
	v.SetSize(12,3);
	matrix x = {{7.0, 4.0, 3.0}, {4.0, 1.0, 8.0}, {6.0, 3.0, 5.0},
	{8.0, 6.0, 1.0}, {8.0, 5.0, 7.0}, {7.0, 2.0, 9.0}, {5.0, 3.0, 3.0}, 
	{9.0, 5.0, 8.0}, {7.0, 4.0, 5.0}, {8.0, 2.0, 2.0}};
	double wt[12];
	double *wtptr;
	int isx[3];
	int nvar = 3;
	int tdx = 3; 
	int tde = 6;
	int tdp = 3;
	int tdv = 3;
	int i, j, m = 3, n = 10;
	Nag_PrinCompMat pcmatrix;
	Nag_PrinCompScores scores;
	char weight, mat, std;
	
 	for(i = 0; i < 3; i++)
 		isx[i] = 1;
 
	mat = 'V';
	std = 'E';
	weight = 'U';

	if (mat == 'C')
		pcmatrix = Nag_MatCorrelation;
	else if (mat == 'S')
		pcmatrix = Nag_MatStandardised;
	else if (mat == 'U')
		pcmatrix = Nag_MatSumSq;
	else
		pcmatrix = Nag_MatVarCovar;
	if (std == 'S')
		scores = Nag_ScoresStand;
	else if (std == 'U')
		scores = Nag_ScoresNotStand;
	else if (std == 'Z')
		scores = Nag_ScoresUnitVar;
	else
		scores = Nag_ScoresEigenval;
	
	wtptr = NULL;
	
	nag_mv_prin_comp(pcmatrix, scores, n, m, x, tdx, isx, s, wtptr, nvar,
					e, tde, p, tdp, v, tdv);
					
	printf("Eigenvalues  Percentage   Cumulative   Chisq   DF   Sig\n");
	printf("              variation   variation\n\n");

	for (i = 0; i < nvar; ++i)
	{
		for (j = 0; j < 6; ++j)
			printf("%11.4f",e[i][j]);
		printf("\n");
	}
	printf("\nEigenvalues\n\n");
	for (i = 0; i < nvar; ++i)
	{
		for (j = 0; j < nvar; ++j)
			printf("%9.4f",p[i][j]);
		printf("\n");
	}
	printf("\nPrincipal component scores \n\n");
	for (i = 0; i < n; ++i)
	{
		printf("%2ld", i+1);
		for (j = 0; j < nvar; ++j)
			printf("%9.3f", v[i][j]);
		printf("\n");	
	}
}


	The output is following:
	
	Eigenvalues		Percentage 		Cumulative 		Chisq 		DF 			Sig
					variation 		variation
	8.2739 			0.6515 			0.6515 			8.6127 		5.0000 		0.1255
	3.6761 			0.2895 			0.9410 			4.1183 		2.0000 		0.1276
	0.7499 			0.0590 			1.0000 			0.0000 		0.0000 		0.0000
	
	Eigenvalues
	
	 0.1376 	0.6990   0.7017
	 0.2505 	0.6609 	-0.7075
	-0.9583 	0.2731 	-0.0842
	
	Principal component scores
	
	1 	 2.151 	 -0.173 	-0.107
	2 	-3.804   -2.887 	-0.510
	3 	-0.153   -0.987 	-0.269
	4 	 4.707 	  1.302 	-0.652
	5 	-1.294 	  2.279 	-0.449
	6 	-4.099 	  0.144 	 0.803
	7 	 1.626 	 -2.232 	-0.803
	8 	-2.114 	  3.251 	 0.168
	9 	 0.235 	  0.373 	-0.275
	10 	 2.746 	 -1.069 	 2.094
Return:
	The function returns NAG error code or 0 if no error.
	
	70:  On entry, parameter pcmatrix had an illegal value.  On entry, parameter scores had an illegal value.
	11:  On entry, m must not be less than 1: m = _value_.  On entry, n must not be less than 2: n = _value_.  On entry, nvar must not be less than 1: nvar = _value_.  On entry, tde must not be less than 6: tde = _value_.
	17:  On entry, tdx = _value_ while m = _value_. These parameters must satisfy tdx = m.  On entry, tdv = _value_ while nvar = _value_. These parameters must satisfy tdv = nvar.  On entry, tdp = _value_ while nvar = _value_. These parameters must satisfy tdp = nvar.  
	19:  On entry, nvar = _value_ while m = _value_. These parameters must satisfy nvar = m.
	20:  On entry, nvar = _value_ while n = _value_. These parameters must satisfy nvar < n.
	500: On entry, wt[_value_] = _value_.  Constraint: when referenced, all elements of wt must be non-negative.
	501: The number of variables, nvar in the analysis = _value_, while the number of variables included in the analysis via array isx = _value_.  Constraint: these two numbers must be the same.
	504: On entry, the standardisation element s[_value_] = _value_, while the variable to be included isx[_value_] = _value_.  Constraint: when a variable is to included, the standardisation element must be positive.  
	503: With weighted data, the e.ective number of observations given by the sum of weights = _value_, while the number of variables included in the analysis, nvar = _value_.  Constraint: effective number of observations > nvar + 1.
	427: The singular value decomposition has failed to converge.  This is an unlikely error exit.
	505: All eigenvalues/singular values are zero.  This will be caused by all the variables being constant.
	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_prin_comp function.
*/
int nag_mv_prin_comp(
    Nag_PrinCompMat pcmatrix,
    Nag_PrinCompScores scores,
    int n, // the number of observations
    int m, // the number of variables in the data matrix
    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.
    double s[], // the standardizations to be used, if any.
    double w[], // contain the weights to be used in the principal component analysis. 
    int nvar, 	// the number of variables in the principal component analysis.
    double e[],	// the statistics of the principal component analysis.
    int tde,	// the last dimension of the array e.
    double p[], // the first nvar columns of p contain the principal component loadings. The jth column of p contains the nvar coefficients for the jth principal component.
    int tdp,	// the last dimension of the array p 
    double v[],	// the first nvar columns of v contain the principal component scores. The jth column of v contains the n scores for the jth principal component.
    int tdv		// the last dimension of the array v.
); // Eigenvalues, Percentage Variation, Cumulative Variation, Principal Component Scores......

/** g03acc
		performs a canonical variate (canonical descrimination) analysis.

Example:
	A sample of nine observations, each consisting of three variables plus group indicator, is read in.
	There are three groups. An unweighted canonical variate analysis is performed.
	
void test_nag_mv_canon_var()
{
	
	matrix e;
	e.SetSize(3,6);
	matrix x = {{13.3, 10.6, 21.2}, {13.6, 10.2, 21.0}, {14.2, 10.7, 21.1},
	{13.4, 9.4, 21.0}, {13.2, 9.6, 20.1}, {13.9, 10.4, 19.8}, {12.9, 10.0, 20.5}, 
	{12.2, 9.9, 20.7}, {13.9, 11.0, 19.1}};	
	double wt[9];
	matrix cvm;
	cvm.SetSize(3,3);
	double tol;
	matrix cvx;
	cvx.SetSize(3,3);
	int i, j, m = 3, n = 9;
	int ng = 3;
	int nx = 3;
	int nig[3], ncv;
	int ing[9] = {1, 2, 3, 1, 2, 3, 1, 2, 3};
	int irx, isx[6];
	int tdx = 3;
	int tdc = 3;
	int tde = 6;
	char wtchar = 'U';
	Nag_Weightstype weight;

	weight = Nag_NoWeights; 
	
	for (j = 0; j < m; ++j)
		isx[j] = 1;
	tol = 1e-6;
	
	nag_mv_canon_var(weight, n, m, x, tdx, isx, nx, ing, ng, wt, nig,
					cvm, tdc, e, tde, &ncv, cvx, tdc, tol, &irx);
			
	printf("%s%2ld\n\n","Rank of x = ",irx);
	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 < nx; ++i)
	{
		for (j = 0; j < ncv; ++j)
			printf("%9.4f",cvx[i][j]);
		printf("\n");
	}
	printf("\nCanonical variate means\n");
	for (i = 0; i < ng; ++i)
	{
		for (j = 0; j < ncv; ++j)
			printf("%9.4f",cvm[i][j]);
		printf("\n");
	}
}	
	
	
	The output is following:
		
	Rank of x = 3
	Canonical 		Eigenvalues 	Percentage 		CHISQ 		DF 			SIG
	Correlations 					Variation	
	0.8826 			3.5238 			0.9795 			7.9032 		6.0000 		0.2453
	0.2623 			0.0739 			0.0205 			0.3564 		2.0000 		0.8368
	
	Canonical Coefficients for X
	-1.7070 	0.7277
	-1.3481 	0.3138
	 0.9327 	1.2199
	 
	Canonical variate means
	 0.9841 	0.2797
	 1.1805 	-0.2632
	-2.1646 	-0.0164

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

	70: On entry, parameter weight had an illegal value.
	11: On entry, nx must not be less than 1: nx = _value_.  On entry, ng must not be less than 2: ng = _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, m = _value_ while nx = _value_.  These parameters must satisfy m = nx.  On entry, tdx = _value_ while m = _value_.  These parameters must satisfy tdx = m.  On entry, tdcvx = _value_ while ng = _value_.  These parameters must satisfy tdcvx = ng-1.  On entry, tdcvm = _value_ while nx = _value_.  These parameters must satisfy tdcvm = nx.  
	29: On entry, n = _value_, nx = _value_ and ng = _value_.  These parameters must satisfy n = nx + ng.
	112: On entry, ing[_value_] = _value_, ng = _value_.  Constraint: 1 = ing[i - 1] = ng, i = 1, 2, . . ., n.
	510: The wt array argument must not be NULL when the weight argument indicates weights.
	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 number of variables included in the analysis via array isx = _value_.  Constraint: these two numbers must be the same.  
	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 variables provide an exact indication as to which group every observation is allocated.
	507: Either the e.ective number of groups is less than two or the e.ective number of groups plus the number of variables, nx is greater than the the e.ective number of observations.  
	508: The rank of the variables is zero.  This will happen if all the 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_var function.	
*/
int nag_mv_canon_var(
    Nag_Weightstype weights, //indicates the type of weights to be used in the analysis.
    int n, // the number of observations.

⌨️ 快捷键说明

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