软件编程版 #135201 层

谁能帮忙看看代码转化问题错在哪

楼主 · 1 楼
Matlab代码
function [R, indef, E] = cholmod(A)
n = length(A);
diagA = diag(A);
gamma = max(abs(diagA)); % max diagonal entry
xi = max(max(abs(A - diag(diagA)))); % max offidagonal entry
delta = eps*(max([gamma+xi, 1]));
beta = sqrt(max([gamma, xi/n, eps]));
indef = 0;

% initialize d and L

d = zeros(n,1);
L = eye(n);


% there are no inner for loops, everything implemented with
% vector operations for a reasonable level of efficiency

for j = 1:n
K = 1:j-1; % column index: all columns to left of diagonal
% d(K) doesn't work in case K is empty
djtemp = A(j,j) - L(j,K)*(d(K,1).*L(j,K)'); % C(j,j) in book
if j < n
I = j+1:n; % row index: all rows below diagonal
Ccol = A(I,j) - L(I,K)*(d(K,1).*L(j,K)'); % C(I,j) in book
theta = max(abs(Ccol));
% guarantees d(j) not too small and L(I,j) not too big
% in sufficiently positive definite case, d(j) = djtemp
d(j) = max([abs(djtemp), (theta/beta)^2, delta]);
L(I,j) = Ccol/d(j);
else
d(j) = max([abs(djtemp), delta]);
end
end

C代码
void modified_choldcmp(double *G, double *d, int n)
{
double *L, *temp, *Ccol;
double gamma, delta, theta, beta, omega, temp_element, t1, t2, djtemp, xi;
const double eps=1e-5;
int i, j, r, I, K;
double t;
L=new double[n*n];
temp=new double[n];
Ccol=new double[n];

unit_matrix(L, n);

gamma=0;
for(i=0; i<n; i++)
{
d[i]=0;
t=fabs(G[i*n+i]);
gamma=(t>gamma)?t:gamma;
}


for(i=0; i<n; i++)
for(j=0; j<i-1; j++)
{
t=fabs(G[i*n+j]);
xi=(t>xi)?t:xi;
}

delta=gamma+xi>1?gamma+xi:1;
delta*=eps;

t=xi/n;
beta=(gamma>t)?gamma:t;
beta=beta>eps?beta:eps;
beta=sqrt(beta);

for(j=0; j<n; j++)
{
for(K=0; K<j-1; K++)
temp[K]=d[K]*L[K*n+j];
temp_element=0;
for(K=0; K<j-1; K++)
temp_element+=L[j*n+K]*temp[K];
djtemp=G[j*n+j]-temp_element;

if(j<n-1)
{
for(K=0; K<j-1; K++)
temp[K]=d[K]*L[K*n+j];
for(I=0; I<n; I++)
Ccol[I]=0;
for(I=j+1; I<n; I++)
{
temp_element=0;
for(K=0; K<j-1; K++)
temp_element+=L[I*n+K]*temp[K];
Ccol[I]=G[I*n+j]-temp_element;
}

theta=0;
for(I=0; I<n; I++)
theta=theta>fabs(Ccol[I])?theta:fabs(Ccol[I]);

t1=fabs(djtemp);
t2=(theta/beta)*(theta/beta);
t1=t1>t2?t1:t2;
t1=t1>delta?t1:delta;
d[j]=t1;
for(I=j+1; I<n; I++)
L[I*n+j]=Ccol[I]/d[j];
}
else
{
d[j]=(temp_element=fabs(djtemp))>delta?temp_element:delta;
}
}

}