C 当传递到pgplot时,是否有O(1)方法来分离复数数组的实部和虚部?

C 当传递到pgplot时,是否有O(1)方法来分离复数数组的实部和虚部?,c,arrays,time-complexity,complex-numbers,C,Arrays,Time Complexity,Complex Numbers,我的代码中有一个复杂的浮点数组,是库为我生成的。认为是这样的: float _Complex data[N]; float real[N]; float imag[N]; for (int pt=0;pt<N;pt++) { real[pt] = creal(data[pt]); imag[pt] = cimag(data[pt])); } 为了将其作为具有实部和虚部的独立数组,我对数组进行迭代,并获取如下值: float _Complex data[N]; floa

我的代码中有一个复杂的浮点数组,是库为我生成的。认为是这样的:

float _Complex data[N];
float real[N];
float imag[N];
for (int pt=0;pt<N;pt++) {
  real[pt] = creal(data[pt]);
  imag[pt] = cimag(data[pt]));    
}
为了将其作为具有实部和虚部的独立数组,我对数组进行迭代,并获取如下值:

float _Complex data[N];
float real[N];
float imag[N];
for (int pt=0;pt<N;pt++) {
  real[pt] = creal(data[pt]);
  imag[pt] = cimag(data[pt]));    
}
float real[N];
浮动图像[N];

对于(int pt=0;pt如果您有一个长度为N的输入数组,则实际上没有一个O(1)算法来遍历所有输入。

如果您有一个长度为N的输入数组,则实际上没有一个O(1)算法来遍历所有输入。

给定:

float _Complex data[N];
我们知道:

float *ptr = (float *) data;
ptr[2 * n + 0] <- real part.
ptr[2 * n + 1] <- imaginary part.
float*ptr=(float*)数据;
ptr[2*n+0]给定:

我们知道:

float *ptr = (float *) data;
ptr[2 * n + 0] <- real part.
ptr[2 * n + 1] <- imaginary part.
float*ptr=(float*)数据;

ptr[2*n+0]该
pgplot
库提供了一个无障碍的界面来绘制标记数组:

void cpgpt(int n, const float *xpts, const float *ypts, int symbol);
但这只是对
cpgpt1
的单个调用的一个简单包装。因此,添加一个跨接接口非常容易:

void cpgpts(int n, int stride, const float *xpts, const float *ypts, int symbol) {
  for (int i = 0; i < n; ++i) {
    cpgpt1(*xpts, *ypts, symbol);
    xpts += stride;
    ypts += stride;
  }
}
例如:

// Plot the data as symbols on the Re-Im plane
cpgptsc(count, data, symbol);
// Plot the data as lines on the Re-Im plane
cpglinesc(count, data);
您可以类似地重新实现
cpgline

void cpglines(int n, int stride, const float *xpts, const float *ypts, int symbol) {
  cpgmove(*xpts, *ypts);
  for (int i = 1; i < n; ++ i) {
    xpts += stride;
    ypts += stride;
    cpgdraw(*xpts, *ypts);
  }
}

void cpglinesc(int n, const float _Complex *pts, int symbol) {
  cpglines(n, 2, (const float*)pts, ((const float*)pts)+1, symbol);
}
如果只绘制单个组件(实组件或虚组件),则为其创建合理的包装器也同样简单:

void cpglinesx(int n, int stride, float dx, float x0, const float *ypts) {
  cpgmove(x0, *ypts);
  for (int i = 1; i < n; ++ i) {
    x0 += dx;
    ypts += stride;
    cpgdraw(x0, *ypts);
  }
}

void cpglinesxre(int n, float dx, float x0, const float _Complex *pts) {
  cpglinesx(n, 2, dx, x0, (const float*)pts);
}

void cpglinesxim(int n, float dx, float x0, const float _Complex *pts) {
  cpglinesx(n, 2, dx, x0, ((const float*)pts)+1);
}

pgplot
库提供了一个无障碍的界面来绘制标记数组:

void cpgpt(int n, const float *xpts, const float *ypts, int symbol);
但这只是对
cpgpt1
的单个调用的一个简单包装。因此,添加一个跨接接口非常容易:

void cpgpts(int n, int stride, const float *xpts, const float *ypts, int symbol) {
  for (int i = 0; i < n; ++i) {
    cpgpt1(*xpts, *ypts, symbol);
    xpts += stride;
    ypts += stride;
  }
}
例如:

// Plot the data as symbols on the Re-Im plane
cpgptsc(count, data, symbol);
// Plot the data as lines on the Re-Im plane
cpglinesc(count, data);
您可以类似地重新实现
cpgline

void cpglines(int n, int stride, const float *xpts, const float *ypts, int symbol) {
  cpgmove(*xpts, *ypts);
  for (int i = 1; i < n; ++ i) {
    xpts += stride;
    ypts += stride;
    cpgdraw(*xpts, *ypts);
  }
}

void cpglinesc(int n, const float _Complex *pts, int symbol) {
  cpglines(n, 2, (const float*)pts, ((const float*)pts)+1, symbol);
}
如果只绘制单个组件(实组件或虚组件),则为其创建合理的包装器也同样简单:

void cpglinesx(int n, int stride, float dx, float x0, const float *ypts) {
  cpgmove(x0, *ypts);
  for (int i = 1; i < n; ++ i) {
    x0 += dx;
    ypts += stride;
    cpgdraw(x0, *ypts);
  }
}

void cpglinesxre(int n, float dx, float x0, const float _Complex *pts) {
  cpglinesx(n, 2, dx, x0, (const float*)pts);
}

void cpglinesxim(int n, float dx, float x0, const float _Complex *pts) {
  cpglinesx(n, 2, dx, x0, ((const float*)pts)+1);
}


不,如果你想要O(1),你需要和他们一起工作首先,这真的是一个C问题,还是C++问题?你在说什么绘图库?第二,你确定没有办法把数组的步幅传递给绘图库吗?也许你的绘图库可以被修改来接受一个步幅,那么你就不需要合作了。我想下一个问题的教训是:抽象出细节只在某一点上有用。在这里,你的问题是你假设了一个问题的解决方案,但没有告诉我们问题是什么。这个问题确实与PGPLOT库有很大关系,与复杂性理论没有多大关系:)Kubabor C++没有复杂的,你需要在它们的位置上工作,如果你想要O(1)。我不能使用它们。首先,这是一个C问题还是一个C++问题?你在说什么样的绘图库?其次,您确定无法将阵列跨距传递到绘图库吗?也许可以修改绘图库以接受跨步,这样就不必复制数据了。我想下一个问题的教训是:提取细节只在一定程度上有用。在这里,你的问题是你预先假定了一个问题的解决方案,而没有告诉我们问题是什么。这个问题确实与PGPrPoT库非常相关,与复杂性理论没有多大关系:)Kubaber-C++没有复杂的结构,我希望如果复合库将它存储为两个数组,可能有一种方法可以获取指向数组实部和虚部的第一个元素的指针,并从此访问它。@VivekVK将复数作为独立的实部和虚部存储在数组中,这是有效地将CPU缓存大小减半的可靠方法。任何理智的实现都不会做这样的事情。你的问题假设了一些根本不存在的东西。@VivekVK:没有办法获得O(1)C中的连续数组,C明确要求实部和虚部相邻存储,表示形式与相应的实浮点类型匹配。实现可以忽略此要求的唯一时间是编译器可以看到对象地址的所有用途,并且可以证明程序无法观察到替代存储格式。我所期望的是,如果复杂库将其存储为两个数组,可能有一种方法可以获取指向数组实部和虚部的第一个元素的指针,并从此访问它。@VivekVK将复数作为独立的实部和虚部存储在数组中,这是有效地将CPU缓存大小减半的可靠方法。任何理智的实现都不会做这样的事情。你的问题假设了一些根本不存在的东西。@VivekVK:没有办法获得O(1)C中的连续数组,C明确要求实部和虚部相邻存储,表示形式与相应的实浮点类型匹配。实现可以忽略此要求的唯一时间是编译器可以看到对象地址的所有用途,并且可以证明程序无法观察到替代存储格式。是的,这是有道理的。我必须处理延迟问题。无论如何,谢谢你的回复。我不能100%确定这是否有定义良好的行为,但这是最接近解决方案的事情…@KubaOber我正在使用PGPLOT来绘制这些,因为这是项目使用的标准库。我不认为它提供了一种跨越的方式,因为它只是一个古老的fortran绘图库的包装。我必须处理延迟问题。无论如何,谢谢你的回复。我不能100%确定这是否有定义良好的行为,但这是最接近解决方案的事情…@KubaOber我正在使用PGPLOT来绘制这些,因为这是项目使用的标准库。我不认为它提供了一种跨越的方式,因为它只是一个古老的fortran绘图库的包装。我不能接受你的答案,因为这有点偏离了问题的标题。但这真的救了我今天的命,多亏了