SVD分解C实现
int svd(int m,int n,int withu,int withv,double eps,double tol,
double *a, double *q, double *u, double *v, double *vt)
{
int i,j,k,l,l1,iter,retval;
double c,f,g,h,s,x,y,z;
double *e;
e = (double *)calloc(n,sizeof(double));
retval = 0;
/* Copy 'a' to 'u' */
for (i=0;i<m;i++) {
for (j=0;j<n;j++)
u[i*n + j] = a[i*n + j];
}
/* Householder's reduction to bidiagonal form. */
g = x = 0.0;
for (i=0;i<n;i++) {
e[i] = g;
s = 0.0;
l = i+1;
for (j=i;j<m;j++)
s += (u[j*n+i]*u[j*n+i]);
if (s < tol)
g = 0.0;
else {
f = u[i*n+i];
g = (f < 0) ? sqrt(s) : -sqrt(s);
h = f * g - s;
u[i*n+i] = f - g;
for (j=l;j<n;j++) {
s = 0.0;
for (k=i;k<m;k++)
s += (u[k*n+i] * u[k*n+j]);
f = s / h;
for (k=i;k<m;k++)
u[k*n+j] += (f * u[k*n+i]);
}
/* end j */
}
/* end s */
q[i] = g;
s = 0.0;
for (j=l;j<n;j++)
s += (u[i*n+j] * u[i*n+j]);
if (s < tol)
g = 0.0;
else {
f = u[i*n+i+1];
g = (f < 0) ? sqrt(s) : -sqrt(s);
h = f * g - s;
u[i*n+i+1] = f - g;
for (j=l;j<n;j++)
e[j] = u[i*n+j]/h;
for (j=l;j<m;j++) {
s = 0.0;
相关文档:
#include <stdio.h>
#include <string.h>
int
main(void)
{
char str[] =
"3BVPSq4xF.K?=u#,"
"G'K<MrDnRr7gH%#,"
"XKf<f%G`w^=?C<#,"
"HgU_AnNR?*PDQU#,"
......
联系1-9编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。
#include "stdio.h"
main(){
int c;
int flag;
flag=0;//是否空格标志
while ((c=getchar())!=EOF){
if (c!=32) {
putchar(c);
flag=0;
}else if(flag==0){
flag=1;
putchar(c);
}
/* ......
网上搜索了一大堆去掉/*和*/之间注释的代码,就像<The C Programming Language>练习1-23里有人说的一样
大部分都会被以下的程序broken,这个功能看起来简单,实际上很有难度.网上实现的代码,除了我找到的一个用文件指针实现的没有问题外,其余的都存在各种bug,不信的话就用以下的程序测试一下:-),当然这个程序也不够完善 ......