Switch to unified view

a b/combinedDeepLearningActiveContour/minFunc/mcholC.c
1
#include <math.h>
2
#include "mex.h"
3
4
double mymax(double x, double y)
5
{
6
    if (x > y)
7
        return x;
8
    else
9
        return y;
10
}
11
12
double absolute(double x)
13
{
14
    if (x >= -x)
15
        return x;
16
    else
17
        return -x;
18
}
19
20
void permuteInt(int *x, int p, int q)
21
{
22
    int temp;
23
    temp = x[p];
24
    x[p] = x[q];
25
    x[q] = temp;
26
}
27
28
void permute(double *x, int p, int q)
29
{
30
    double temp;
31
    temp = x[p];
32
    x[p] = x[q];
33
    x[q] = temp;
34
}
35
36
void permuteRows(double *x, int p, int q,int n)
37
{
38
    int i;
39
    double temp;
40
    for(i = 0; i < n; i++)
41
    {
42
        temp = x[p+i*n];
43
        x[p+i*n] = x[q+i*n];
44
        x[q+i*n] = temp;
45
    }
46
}
47
48
void permuteCols(double *x, int p, int q,int n)
49
{
50
    int i;
51
    double temp;
52
    for(i = 0; i < n; i++)
53
    {
54
        temp = x[i+p*n];
55
        x[i+p*n] = x[i+q*n];
56
        x[i+q*n] = temp;
57
    }
58
}
59
60
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
61
{
62
    int n,sizL[2],sizD[2],i,j,q,s,
63
    *P;
64
    
65
    double mu,gamma,xi,delta,beta,maxVal,theta,
66
    *c,    *H, *L, *D, *A;
67
    
68
    /* Input */
69
    H = mxGetPr(prhs[0]);
70
    if (nrhs == 1)
71
    {
72
        mu = 1e-12;
73
    }
74
    else
75
    {
76
        mu = mxGetScalar(prhs[1]);
77
    }
78
    
79
    /* Compute Sizes */
80
    n = mxGetDimensions(prhs[0])[0];
81
    
82
    /* Form Output */
83
    sizL[0] = n;
84
    sizL[1] = n;
85
    plhs[0] = mxCreateNumericArray(2,sizL,mxDOUBLE_CLASS,mxREAL);
86
    L = mxGetPr(plhs[0]);
87
    sizD[0] = n;
88
    sizD[1] = 1;
89
    plhs[1] = mxCreateNumericArray(2,sizD,mxDOUBLE_CLASS,mxREAL);
90
    D = mxGetPr(plhs[1]);
91
    plhs[2] = mxCreateNumericArray(2,sizD,mxINT32_CLASS,mxREAL);
92
    P = (int*)mxGetData(plhs[2]);
93
    
94
    /* Initialize */
95
    c = mxCalloc(n*n,sizeof(double));
96
    A = mxCalloc(n*n,sizeof(double));
97
    
98
    for (i = 0; i < n; i++)
99
    {
100
        P[i] = i;
101
        for (j = 0;j < n; j++)
102
        {
103
            A[i+n*j] = H[i+n*j];
104
        }
105
    }
106
    
107
    gamma = 0;
108
    for (i = 0; i < n; i++)
109
    {
110
        L[i+n*i] = 1;
111
        c[i+n*i] = A[i+n*i];
112
    }
113
    
114
    /* Compute modification parameters */
115
    gamma = -1;
116
    xi = -1;
117
    for (i = 0; i < n; i++)
118
    {
119
        gamma = mymax(gamma,absolute(A[i+n*i]));
120
        for (j = 0;j < n; j++)
121
        {
122
            //printf("A(%d,%d) = %f, %f\n",i,j,A[i+n*j],absolute(A[i+n*j]));
123
            if (i != j)
124
                xi = mymax(xi,absolute(A[i+n*j]));
125
        }
126
    }
127
    delta = mu*mymax(gamma+xi,1);
128
    
129
    if (n > 1)
130
    {
131
        beta = sqrt(mymax(gamma,mymax(mu,xi/sqrt(n*n-1))));
132
    }
133
    else
134
    {
135
        beta = sqrt(mymax(gamma,mu));
136
    }
137
    
138
    for (j = 0; j < n; j++)
139
    {
140
        
141
    /* Find q that results in Best Permutation with j */
142
        maxVal = -1;
143
        q = 0;
144
        for(i = j; i < n; i++)
145
        {
146
            if (absolute(c[i+n*i]) > maxVal)
147
            {
148
                maxVal = mymax(maxVal,absolute(c[i+n*i]));
149
                q = i;
150
            }
151
        }
152
        
153
        /* Permute D,c,L,A,P */
154
        permute(D,j,q);
155
        permuteInt(P,j,q);
156
        permuteRows(c,j,q,n);
157
        permuteCols(c,j,q,n);
158
        permuteRows(L,j,q,n);
159
        permuteCols(L,j,q,n);
160
        permuteRows(A,j,q,n);
161
        permuteCols(A,j,q,n);
162
        
163
        for(s = 0; s <= j-1; s++)
164
            L[j+n*s] = c[j+n*s]/D[s];
165
        
166
        for(i = j+1; i < n; i++)
167
        {
168
            c[i+j*n] = A[i+j*n];
169
            for(s = 0; s <= j-1; s++)
170
            {
171
                c[i+j*n] -= L[j+n*s]*c[i+n*s];
172
            }
173
        }
174
        
175
        theta = 0;
176
        if (j < n-1)
177
        {
178
            for(i = j+1;i < n; i++)
179
                theta = mymax(theta,absolute(c[i+n*j]));
180
        }
181
        
182
        D[j] = mymax(absolute(c[j+n*j]),mymax(delta,theta*theta/(beta*beta)));
183
        
184
        if (j < n-1)
185
        {
186
            for(i = j+1; i < n; i++)
187
            {
188
                c[i+n*i] = c[i+n*i] - c[i+n*j]*c[i+n*j]/D[j];
189
            }
190
        }
191
        
192
    }
193
    
194
    for(i = 0; i < n; i++)
195
        P[i]++;
196
    
197
    mxFree(c);
198
    mxFree(A);
199
}