SVD (c语言代码)

/*
* svdcomp - SVD decomposition routine.
* Takes an mxn matrix a and decomposes it into udv, where u,v are
* left and right orthogonal transformation matrices, and d is a
* diagonal matrix of singular values.
*
* This routine is adapted from svdecomp.c in XLISP-STAT 2.1 which is
* code from Numerical Recipes adapted by Luke Tierney and David Betz.
*
* Input to dsvd is as follows:
*   a = mxn matrix to be decomposed, gets overwritten with u
*   m = row dimension of a
*   n = column dimension of a
*   w = returns the vector of singular values of a
*   v = returns the right orthogonal transformation matrix
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
float **Make2DArray(int row, int col){
	float **a;
	int i;
	a = (float **)malloc(row * sizeof(float *));
	for (i = 0; i<row; i++){
		a[i] = (float *)malloc(col * sizeof(float));
	}
	return a;
}
void SetZero(float** a, int m){
	int i, j;
	for (i = 0; i < m; i++){
		for (j = 0; j < m; j++){
			a[i][j] = 0;
		}
	}
}
void Free2DArray(float** a, int row){
	int i;
	for (i = 0; i<row; i++){
		free(a[i]);
	}
	free(a);
}

void p(float**a, int m, int n){
	for (int i = 0; i < m; i++){
		for (int j = 0; j < n; j++)
			printf("%f ", a[i][j]);
		printf("\n");
	}
}

static double PYTHAG(double a, double b)
{
	double at = fabs(a), bt = fabs(b), ct, result;

	if (at > bt)       { ct = bt / at; result = at * sqrt(1.0 + ct * ct); }
	else if (bt > 0.0) { ct = at / bt; result = bt * sqrt(1.0 + ct * ct); }
	else result = 0.0;
	return(result);
}

int dsvd(float **a, int m, int n, float *w, float **v)
{
	int flag, i, its, j, jj, k, l, nm;
	double c, f, h, s, x, y, z;
	double anorm = 0.0, g = 0.0, scale = 0.0;
	double *rv1;

	if (m < n)
	{
		fprintf(stderr, "#rows must be &g
  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
以下是一个使用Jacobi方法实现SVD分解的示例代码: ```c #include <stdio.h> #include <math.h> #define MAX_ITERATIONS 100 #define EPSILON 1e-10 void svd_jacobi(double A[], double U[], double S[], double V[], int m, int n) { // 初始化U、S、V为单位矩阵 for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { U[i*m + j] = (i == j) ? 1.0 : 0.0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { V[i*n + j] = (i == j) ? 1.0 : 0.0; } } // 迭代更新 int iter = 0; while (iter < MAX_ITERATIONS) { double max_offdiag = 0; int p = 0, q = 0; // 寻找最大非对角线元素 for (int i = 0; i < m-1; i++) { for (int j = i+1; j < m; j++) { double offdiag = fabs(A[i*n + j]); if (offdiag > max_offdiag) { max_offdiag = offdiag; p = i; q = j; } } } // 检查收敛条件 if (max_offdiag < EPSILON) { break; } // 计算旋转角度 double theta = 0.5 * atan2(2*A[p*n + q], A[p*n + p] - A[q*n + q]); double c = cos(theta); double s = sin(theta); // 更新A for (int i = 0; i < m; i++) { double api = A[p*n + i]; double aqi = A[q*n + i]; A[p*n + i] = c * api + s * aqi; A[q*n + i] = -s * api + c * aqi; } for (int i = 0; i < n; i++) { double api = A[i*n + p]; double aqi = A[i*n + q]; A[i*n + p] = c * api + s * aqi; A[i*n + q] = -s * api + c * aqi; } // 更新U和V for (int i = 0; i < m; i++) { double upi = U[i*m + p]; double uqi = U[i*m + q]; U[i*m + p] = c * upi + s * uqi; U[i*m + q] = -s * upi + c * uqi; } for (int i = 0; i < n; i++) { double vpi = V[i*n + p]; double vqi = V[i*n + q]; V[i*n + p] = c * vpi + s * vqi; V[i*n + q] = -s * vpi + c * vqi; } iter++; } // 提取奇异值并排序 for (int i = 0; i < n; i++) { S[i] = A[i*n + i]; } for (int i = 0; i < n-1; i++) { for (int j = i+1; j < n; j++) { if (S[i] < S[j]) { double temp = S[i]; S[i] = S[j]; S[j] = temp; for (int k = 0; k < m; k++) { double tempU = U[k*m + i]; U[k*m + i] = U[k*m + j]; U[k*m + j] = tempU; } for (int k = 0; k < n; k++) { double tempV = V[k*n + i]; V[k*n + i] = V[k*n + j]; V[k*n + j] = tempV; } } } } } int main() { double A[6] = {1, 2, 3, 4, 5, 6}; // 示例输入矩阵 double U[9] = {0}; // 存储左奇异向量 double S[3] = {0}; // 存储奇异值 double V[4] = {0}; // 存储右奇异向量 svd_jacobi(A, U, S, V, 2, 3); // 进行SVD分解 printf("U:\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { printf("%.2f ", U[i*2 + j]); } printf("\n"); } printf("S:\n"); for (int i = 0; i < 2; i++) { printf("%.2f ", S[i]); } printf("\n"); printf("V:\n"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { printf("%.2f ", V[i*2 + j]); } printf("\n"); } return 0; } ``` 这段代码演示了如何使用Jacobi方法进行SVD分解。你可以根据需要修改输入矩阵的大小和元素,然后运行代码以获取相应的左奇异向量、奇异值和右奇异向量。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值