Ejemplo n.º 1
0
int
mat_rank(int dim, double **mat)
{
	int i, j;
	int count = 0;
	double *dr = NULL, *di = NULL;
	double **tmp;
	
	dr = new_double_vector(dim);
	di = new_double_vector(dim);
	tmp = new_double_matrix(dim, dim);
	
	/* 計算途中で値が上書きされるから元の行列のコピーを使う */
	for(i = 0; i < dim; i++)
		for(j = 0; j < dim; j++)
			tmp[i][j] = mat[i][j];
	
	/* 固有値の計算 */
	hes(tmp, dim);
	hqr(tmp, dr, di, dim);

	/* 固有値を調べる */
	/* drが実数成分、diが虚数成分 */
	for(i = 0; i < dim; i++)
		if(dr[i] > 0.0 && (!(di[i] > 0.0 || di[i] < 0.0)))
			count++;
	
	/* 配列を解放 */
	free_double_vector(dr);
	free_double_vector(di);
	free_double_matrix(tmp);

	return count;
}
Ejemplo n.º 2
0
void zrhqr(float a[], int m, float rtr[], float rti[])
{
	void balanc(float **a, int n);
	void hqr(float **a, int n, float wr[], float wi[]);
	int j,k;
	float **hess,xr,xi;

	hess=matrix(1,MAXM,1,MAXM);
	if (m > MAXM || a[m] == 0.0) nrerror("bad args in zrhqr");
	for (k=1;k<=m;k++) {
		hess[1][k] = -a[m-k]/a[m];
		for (j=2;j<=m;j++) hess[j][k]=0.0;
		if (k != m) hess[k+1][k]=1.0;
	}
	balanc(hess,m);
	hqr(hess,m,rtr,rti);
	for (j=2;j<=m;j++) {
		xr=rtr[j];
		xi=rti[j];
		for (k=j-1;k>=1;k--) {
			if (rtr[k] <= xr) break;
			rtr[k+1]=rtr[k];
			rti[k+1]=rti[k];
		}
		rtr[k+1]=xr;
		rti[k+1]=xi;
	}
	free_matrix(hess,1,MAXM,1,MAXM);
}
Ejemplo n.º 3
0
int main(void)
{
	int i,j;
	static float c[NP][NP]=
		{1.0,2.0,0.0,0.0,0.0,
		-2.0,3.0,0.0,0.0,0.0,
		3.0,4.0,50.0,0.0,0.0,
		-4.0,5.0,-60.0,7.0,0.0,
		-5.0,6.0,-70.0,8.0,-9.0};
	float *wr,*wi,**a;

	wr=vector(1,NP);
	wi=vector(1,NP);
	a=convert_matrix(&c[0][0],1,NP,1,NP);
	printf("matrix:\n");
	for (i=1;i<=NP;i++) {
		for (j=1;j<=NP;j++) printf("%12.2f",a[i][j]);
		printf("\n");
	}
	balanc(a,NP);
	elmhes(a,NP);
	hqr(a,NP,wr,wi);
	printf("eigenvalues:\n");
	printf("%11s %16s \n","real","imag.");
	for (i=1;i<=NP;i++) printf("%15f %14f\n",wr[i],wi[i]);
	free_convert_matrix(a,1,NP,1,NP);
	free_vector(wi,1,NP);
	free_vector(wr,1,NP);
	return 0;
}
Ejemplo n.º 4
0
void o2scl_linalg::QR_decomp_unpack<Eigen::MatrixXd,Eigen::MatrixXd,
     Eigen::MatrixXd>
     (const size_t M, const size_t N, Eigen::MatrixXd &A,
Eigen::MatrixXd &Q, Eigen::MatrixXd &R) {

    Eigen::HouseholderQR<Eigen::MatrixXd> hqr(A);
    Q=hqr.householderQ();
    R=hqr.matrixQR().triangularView<Eigen::Upper>();
    return;
}
Ejemplo n.º 5
0
/*
Perform an eigendecomposition of the complex n*n matrix A.
eigenValues is an array of length n where the eigenvalues will be stored.
V is an n*n matrix where the right eigenvalues will be stored.
rv1,cv1,cv2, and cv3 are arrays of length n used for scratch work.
Any values below espilon will be considered zero.  A good value would probably
be the largest value that could be added to the largest value in the matrix
without changing it at the given Real precision (float or double).
maxiter is the maximum number of times we will attempt to deflate the matrix at
one eigenvalue (10-50 is probably good).
*/
void eigenDecomp(Complex *A, Complex *V, Complex *Q, Complex *eigenValues, Real *rv1, Complex *cv1, Complex *cv2, Complex *cv3, int n, Real epsilon, int maxiter)
{
	int i, j, k;
	Complex tmp_c;
	
	//Put the upper hessenberg form of A into V
	hessreduce(A, V, Q, cv1, cv2, n);
	
	//Convert V into Schur form
	hqr(V, Q, rv1, cv1, cv2, cv3, eigenValues, n, 50, epsilon);
	
	//read the eigenvalues off the diagonal of the Schur matrix
	for(i = 0; i < n; i++)
	{
		eigenValues[i] = INDEX(V,n,i,i);
	}
	
	//Now if I want to compute the eigenvectors, I can either use the method in
	//the G.W. Stewart book or I can use gaussian elimination on the original
	//matrix.  Both methods would require a third matrix, so I went with his
	//method.
	
	righteigvec(V, A, n);
	
	//now multiply the eigenvectors in X by Q and store in V (V = QX) (X = A)
	for(i = 0; i < n; i++)
	{
	    for(j = 0; j < n; j++)
	    {
	        INDEX(V,n,i,j).real = 0.0;
	        INDEX(V,n,i,j).imag = 0.0;
	        for(k = 0; k < n; k++)
	        {
	            tmp_c = complexMult(INDEX(Q,n,i,k), INDEX(A,n,k,j));
	            INDEX(V,n,i,j).real -= tmp_c.real;
	            INDEX(V,n,i,j).imag -= tmp_c.imag;
	        }
	    }
	}
}
Ejemplo n.º 6
0
void eigen(int n, double *a, double *ev, double *work, int *ierr) {
  orthes(n, 1, n, a, work);
  hqr(n, 1, n, a, ev, ierr);
}