我已将数据从 mathematica 导出到 csv 文件。文件结构如下:
"x","y","Ex","Ey"
0.,0.,0.+0.*I,-3.0434726787506006*^-12+3.4234894344189825*^-12*I
0.,0.,0.+0.*I,-5.0434726787506006*^-12+10.4234894344189825*^-13*I
...
Run Code Online (Sandbox Code Playgroud)
我正在用 pandas 读取数据,但出现错误
import csv
import pandas as pd
import numpy as np
df=pd.read_csv('filename.csv')
df.columns=['x', 'y', 'Ex','Ey']
df['Ey'] = df['Ey'].str.replace('*^','E')
df['Ey'] = df['Ey'].str.replace('I','1j').apply(lambda x: np.complex(x))
Run Code Online (Sandbox Code Playgroud)
编辑:我在代码的倒数第二行中收到以下错误:
Traceback (most recent call last):
File "plot.py", line 6, in <module>
df['Ey'] = df['Ey'].str.replace('*^','E')
File "/home/.../.local/lib/python2.7/site-packages/pandas/core/strings.py", line 1579, in replace
flags=flags)
File "/home/.../.local/lib/python2.7/site-packages/pandas/core/strings.py", line 424, in str_replace
regex = re.compile(pat, flags=flags)
File "/usr/lib/python2.7/re.py", line 194, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 251, in …Run Code Online (Sandbox Code Playgroud) 以下代码应生成 griddata。但如果我选择插值类型“三次”或“线性”,我会在 z 网格中得到 nan。文,我选择“最近”,一切正常。这是一个示例代码:
import numpy as np
from scipy.interpolate import griddata
x = np.array([0.03,0.05,0033])
y = np.array([0.004,0.01,0.02])
z = np.array([1,2,3])
xy = np.zeros((2,np.size(x)))
xy[0] = x
xy[1] = y
xy = xy.T
grid_x, grid_y = np.mgrid[0.0:0.09:250*1j, 0.0:0.03:250*1j] #generating the grid
i_type= 'cubic' #nearest, linear, cubic
grid_z = griddata(xy, z, (grid_x, grid_y), method=i_type)
#check if there is a nan in the z grid:
print np.isnan(grid_z).any()
Run Code Online (Sandbox Code Playgroud)
我不知道为什么这不起作用..
我有一个数组,并希望不提取特定范围内的所有条目
x = np.array([1,2,3,4])
condition = x<=4 and x>1
x_sel = np.extract(condition,x)
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我越来越
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
如果我在没有并且仅检查一个条件的情况下也这样做
x = np.array([1,2,3,4])
condition = x<=4
x_sel = np.extract(condition,x)
Run Code Online (Sandbox Code Playgroud)
一切正常......对于courese,我可以在一个条件下应用该程序两次,但是在一行中没有解决方案吗?
提前谢谢了
以下代码将std :: array推回到std :: vector N次.这样做有更优雅,更短的方式吗?
#include <iostream>
#include <vector>
#include <array>
#include <iomanip>
#include <complex>
#include <cmath>
int main () {
int N=10;
std::vector< std::array<std::complex<double>,3> > v;
v.reserve(N);
for(int i=0;i<N;i++){
std::array<std::complex<double>,3> el { {0.0,3.0,0.0} };
v.push_back(el);
}
}
Run Code Online (Sandbox Code Playgroud) 我想要一个具有成员数组的类.初始化对象时应该给出数组的大小.我刚刚找到了一种方法来指导这样做.我认为它工作正常,但是你可以告诉我这是否是最好的方法,或者有什么东西不起作用我还没有认识到呢?
#include <iostream>
using namespace std;
class Surface {
private:
float dx;
int N;
float* mesh_points;
public:
Surface(float, int);
~Surface();
void set_dx (float);
float get_dx();
};
Surface::Surface(float dx,int N){
this->dx = dx;
this ->N = N;
mesh_points = new float[N];
}
void Surface::set_dx (float dx) {
this->dx = dx;
}
float Surface::get_dx (void) {
return dx;
}
Surface::~Surface(){
delete[] mesh_points;
}
int main () {
Surface s(1.2,10);
s.set_dx (3.3);
cout << "dx: "<< s.get_dx() <<endl;
float mesh_points[3];
return 0;
}
Run Code Online (Sandbox Code Playgroud)