我在网上找到了一段代码,其中有一行我的编译器无法运行。我也不明白怎么才能正常写。
#define _sum_k(a, b, c, s) { s = 0; foreach(k, a, b) s+= c; }
foreach(k, a, b)
意味着 for (int k = a; k < b; k++)
我该如何像普通的 for 循环一样编写它?
代码用于 LU 分解,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define foreach(a, b, c) for (int a = b; a < c; a++)
#define for_i foreach(i, 0, n)
#define for_j foreach(j, 0, n)
#define for_k foreach(k, 0, n)
#define for_ij for_i for_j
#define for_ijk for_ij for_k
#define _dim int n
#define _swap(x, y) { typeof(x) tmp = x; x = y; y = tmp; }
#define _sum_k(a, b, c, s) { s = 0; foreach(k, a, b) s+= c; }
typedef double **mat;
#define _zero(a) mat_zero(a, n)
void mat_zero(mat x, int n) {
for_ij
x[i][j] = 0;
}
#define _new(a) a = mat_new(n)
mat mat_new(_dim) {
mat x = malloc(sizeof(double*) * n);
x[0] = malloc(sizeof(double) * n * n);
for_i
x[i] = x[0] + n * i;
_zero(x);
return x;
}
#define _copy(a) mat_copy(a, n)
mat mat_copy(void *s, _dim) {
mat x = mat_new(n);
for_ij
x[i][j] = ((double(*)[n]) s)[i][j];
return x;
}
#define _del(x) mat_del(x)
void mat_del(mat x) {
free(x[0]);
free(x);
}
#define _QUOT(x) #x
#define QUOTE(x) _QUOT(x)
#define _show(a) printf(QUOTE(a)" =");mat_show(a, 0, n)
void mat_show(mat x, char *fmt, _dim) {
if (!fmt)
fmt = "%8.4g";
for_i {
printf(i ? " " : " [ ");
for_j {
printf(fmt, x[i][j]);
printf(j < n - 1 ? " " : i == n - 1 ? " ]\n" : "\n");
}
}
}
#define _mul(a, b) mat_mul(a, b, n)
mat mat_mul(mat a, mat b, _dim) {
mat c = _new(c);
for_ijk
c[i][j] += a[i][k] * b[k][j];
return c;
}
#define _pivot(a, b) mat_pivot(a, b, n)
void mat_pivot(mat a, mat p, _dim) {
for_ij
{
p[i][j] = (i == j);
}
for_i {
int max_j = i;
foreach(j, i, n)
if (fabs(a[j][i]) > fabs(a[max_j][i]))
max_j = j;
if (max_j != i)
for_k {
_swap(p[i][k], p[max_j][k]);
}
}
}
#define _LU(a, l, u, p) mat_LU(a, l, u, p, n)
void mat_LU(mat A, mat L, mat U, mat P, _dim) {
_zero(L);
_zero(U);
_pivot(A, P);
mat Aprime = _mul(P, A);
for_i {
L[i][i] = 1;
}
for_ij
{
double s;
if (j <= i) {
_sum_k(0, j, L[j][k] * U[k][i], s)
U[j][i] = Aprime[j][i] - s;
}
if (j >= i) {
_sum_k(0, i, L[j][k] * U[k][i], s);
L[j][i] = (Aprime[j][i] - s) / U[i][i];
}
}
_del(Aprime);
}
double A3[][3] = { { 1, 3, 5 }, { 2, 4, 7 }, { 1, 1, 0 } };
double A4[][4] = { { 11, 9, 24, 2 }, { 1, 5, 2, 6 }, { 3, 17, 18, 1 }, { 2, 5,
7, 1 } };
int main() {
int n = 3;
mat A, L, P, U;
_new(L);
_new(P);
_new(U);
A = _copy(A3);
_LU(A, L, U, P);
_show(A);
_show(L);
_show(U);
_show(P);
_del(A);
_del(L);
_del(U);
_del(P);
printf("\n");
n = 4;
_new(L);
_new(P);
_new(U);
A = _copy(A4);
_LU(A, L, U, P);
_show(A);
_show(L);
_show(U);
_show(P);
_del(A);
_del(L);
_del(U);
_del(P);
return 0;
}
编辑:我替换了每个宏,包括有问题的宏(使用 s = c * (b-a) * ( b > a);
),但它崩溃了,所以也许这不是它应该的样子。
最佳答案
预处理器只是执行“搜索和替换”。
#define foreach(a, b, c) for (int a = b; a < c; a++)
字面意思是foreach(x, y, z)
将替换为 for (int x = y; x < z; x++)
。等等。
它对每个 #define
都这样做是字面意思,直到没有更多的替代品为止。
您可以简单地通过预处理器运行您的程序并查看剩下的内容:
cpp myfile.c > myfile_preprocessed.c
// #include's omitted
typedef double **mat;
void mat_zero(mat x, int n) {
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
x[i][j] = 0;
}
mat mat_new(int n) {
mat x = malloc(sizeof(double*) * n);
x[0] = malloc(sizeof(double) * n * n);
for (int i = 0; i < n; i++)
x[i] = x[0] + n * i;
mat_zero(x, n);
return x;
}
mat mat_copy(void *s, int n) {
mat x = mat_new(n);
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
x[i][j] = ((double(*)[n]) s)[i][j];
return x;
}
void mat_del(mat x) {
free(x[0]);
free(x);
}
void mat_show(mat x, char *fmt, int n) {
if (!fmt)
fmt = "%8.4g";
for (int i = 0; i < n; i++) {
printf(i ? " " : " [ ");
for (int j = 0; j < n; j++) {
printf(fmt, x[i][j]);
printf(j < n - 1 ? " " : i == n - 1 ? " ]\n" : "\n");
}
}
}
mat mat_mul(mat a, mat b, int n) {
mat c = c = mat_new(n);
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
return c;
}
void mat_pivot(mat a, mat p, int n) {
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
{
p[i][j] = (i == j);
}
for (int i = 0; i < n; i++) {
int max_j = i;
for (int j = i; j < n; j++)
if (fabs(a[j][i]) > fabs(a[max_j][i]))
max_j = j;
if (max_j != i)
for (int k = 0; k < n; k++) {
{ typeof(p[i][k]) tmp = p[i][k]; p[i][k] = p[max_j][k]; p[max_j][k] = tmp; };
}
}
}
void mat_LU(mat A, mat L, mat U, mat P, int n) {
mat_zero(L, n);
mat_zero(U, n);
mat_pivot(A, P, n);
mat Aprime = mat_mul(P, A, n);
for (int i = 0; i < n; i++) {
L[i][i] = 1;
}
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
{
double s;
if (j <= i) {
{ s = 0; for (int k = 0; k < j; k++) s+= L[j][k] * U[k][i]; }
U[j][i] = Aprime[j][i] - s;
}
if (j >= i) {
{ s = 0; for (int k = 0; k < i; k++) s+= L[j][k] * U[k][i]; };
L[j][i] = (Aprime[j][i] - s) / U[i][i];
}
}
mat_del(Aprime);
}
double A3[][3] = { { 1, 3, 5 }, { 2, 4, 7 }, { 1, 1, 0 } };
double A4[][4] = { { 11, 9, 24, 2 }, { 1, 5, 2, 6 }, { 3, 17, 18, 1 }, { 2, 5,
7, 1 } };
int main() {
int n = 3;
mat A, L, P, U;
L = mat_new(n);
P = mat_new(n);
U = mat_new(n);
A = mat_copy(A3, n);
mat_LU(A, L, U, P, n);
printf("A"" =");mat_show(A, 0, n);
printf("L"" =");mat_show(L, 0, n);
printf("U"" =");mat_show(U, 0, n);
printf("P"" =");mat_show(P, 0, n);
mat_del(A);
mat_del(L);
mat_del(U);
mat_del(P);
printf("\n");
n = 4;
L = mat_new(n);
P = mat_new(n);
U = mat_new(n);
A = mat_copy(A4, n);
mat_LU(A, L, U, P, n);
printf("A"" =");mat_show(A, 0, n);
printf("L"" =");mat_show(L, 0, n);
printf("U"" =");mat_show(U, 0, n);
printf("P"" =");mat_show(P, 0, n);
mat_del(A);
mat_del(L);
mat_del(U);
mat_del(P);
return 0;
}
请注意,不要用函数调用替换 for 循环的主体,生成的代码可能会变慢,这取决于优化器的质量和设置。
关于c - 缩短的 for/foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50356801/