5 void ke_core_strq(int n
, double *q
, double *b
, double *c
)
9 for (i
= n
- 1; i
>= 1; i
--) {
12 for (k
= 0; k
< i
; k
++) {
19 c
[i
] = q
[i
* n
+ i
- 1];
29 for (j
= 0; j
< i
; j
++) {
30 q
[j
* n
+ i
] = q
[i
* n
+ j
] / h
;
32 for (k
= 0; k
<= j
; k
++)
33 g
= g
+ q
[j
* n
+ k
] * q
[i
* n
+ k
];
35 for (k
= j
+ 1; k
<= i
- 1; k
++)
36 g
= g
+ q
[k
* n
+ j
] * q
[i
* n
+ k
];
38 f
= f
+ g
* q
[j
* n
+ i
];
41 for (j
= 0; j
< i
; j
++) {
45 for (k
= 0; k
<= j
; k
++) {
47 q
[u
] = q
[u
] - f
* c
[k
] - g
* q
[i
* n
+ k
];
53 for (i
= 0; i
< n
- 1; i
++)
57 for (i
= 0; i
< n
; i
++) {
58 if (b
[i
] != 0.0 && i
- 1 >= 0)
59 for (j
= 0; j
< i
; j
++) {
61 for (k
= 0; k
< i
; k
++)
62 g
= g
+ q
[i
* n
+ k
] * q
[k
* n
+ j
];
63 for (k
= 0; k
< i
; k
++) {
65 q
[u
] = q
[u
] - g
* q
[k
* n
+ i
];
72 for (j
= 0; j
< i
; j
++) {
79 int ke_core_sstq(int n
, double *b
, double *c
, double *q
, int cal_ev
, double eps
, int l
)
81 int i
, j
, k
, m
, it
, u
, v
;
82 double d
, f
, h
, g
, p
, r
, e
, s
;
86 for (j
= 0; j
< n
; j
++) {
88 h
= eps
* (fabs(b
[j
]) + fabs(c
[j
]));
92 while (m
< n
&& fabs(c
[m
]) > d
)
96 if (it
== l
) return KE_EXCESS_ITER
;
99 p
= (b
[j
+ 1] - g
) / (2.0 * c
[j
]);
100 r
= sqrt(p
* p
+ 1.0);
102 b
[j
] = c
[j
] / (p
+ r
);
104 b
[j
] = c
[j
] / (p
- r
);
106 for (i
= j
+ 1; i
< n
; i
++)
112 for (i
= m
- 1; i
>= j
; i
--) {
115 if (fabs(p
) >= fabs(c
[i
])) {
117 r
= sqrt(e
* e
+ 1.0);
118 c
[i
+ 1] = s
* p
* r
;
123 r
= sqrt(e
* e
+ 1.0);
124 c
[i
+ 1] = s
* c
[i
] * r
;
128 p
= e
* b
[i
] - s
* g
;
129 b
[i
+ 1] = h
+ s
* (e
* g
+ s
* b
[i
]);
131 for (k
= 0; k
< n
; k
++) {
135 q
[u
] = s
* q
[v
] + e
* h
;
136 q
[v
] = e
* q
[v
] - s
* h
;
143 while (fabs(c
[j
]) > d
);
147 for (i
= 0; i
< n
; i
++) {
152 while (j
< n
&& b
[j
] <= p
) {
161 for (j
= 0; j
< n
; j
++) {
173 #define MALLOC(type, size) ((type*)malloc(size * sizeof(type)))
175 int ke_eigen_sd(int n
, double *a
, double *v
, int cal_ev
, double eps
, int max_iter
)
179 if (1.0 + eps
<= 1.0) eps
= 1e-7;
180 if (max_iter
<= 0) max_iter
= 50;
181 c
= MALLOC(double, n
);
182 ke_core_strq(n
, a
, v
, c
);
183 r
= ke_core_sstq(n
, v
, c
, a
, cal_ev
, eps
, max_iter
);