pyx和c++

This commit is contained in:
Irony 2018-05-06 01:34:55 +08:00
parent 258f459507
commit 69e5b66034
14 changed files with 11408 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,27 @@
#%%cython
import numpy as np
cimport numpy as np
np.import_array()
# 参考http://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html?highlight=cdef%20extern%20from
# 参考https://www.zhihu.com/question/23003213
cdef extern from "CalSpecSpeaLib.h":
void cal_spec_accel(double acc[], int len, double dt, double maxPeriod, double periodStep, double dampRatio, double *Period, double *Fre, double *MAcc, double *MVel, double *MDis, int numt)
def calspecaccel(np.ndarray[double, ndim=1, mode="c"] acc, int length, double dt, double maxPeriod, double periodStep, double dampRatio):
cdef int numt = int(maxPeriod / periodStep) + 1
# 初始化各存储数据
cdef np.ndarray[double, ndim=1] Fre = np.zeros(numt, float)
cdef np.ndarray[double, ndim=1] MDis = np.zeros(numt, float)
cdef np.ndarray[double, ndim=1] MVel = np.zeros(numt, float)
cdef np.ndarray[double, ndim=1] MAcc = np.zeros(numt, float)
#产生501个
cdef np.ndarray[double, ndim=1] Period = np.arange(0.0, maxPeriod + periodStep, periodStep) # 10.0 + 0.02, 0.02
Period[0] = 0.001
# 调用CalSpecSpeaLib.cpp定义的函数对数组进行处理
cal_spec_accel(<double*> np.PyArray_DATA(acc), length, dt, maxPeriod, periodStep, dampRatio,
<double*> np.PyArray_DATA(Period), <double*> np.PyArray_DATA(Fre),
<double*> np.PyArray_DATA(MAcc), <double*> np.PyArray_DATA(MVel),
<double*> np.PyArray_DATA(MDis), numt)
return Period, Fre, MAcc, MVel, MDis

View file

@ -0,0 +1,63 @@
#include "CalSpecSpeaLib.h"
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
double maxabs(double num[], int len)
{
//略
return abs(num[0]);
}
bool isALLvaluezero(double num[], int len)
{
//略
return false;
}
void cal_spec_accel(double acc[], int len, double dt, double maxPeriod, double periodStep, double dampRatio, double *Period, double *Fre, double *MAcc, double *MVel, double *MDis, int numt)
{
//略
Period[0] = 99.95;
Fre[0] = 99.96;
MAcc[0] = 99.97;
MVel[0] = 99.98;
MDis[0] = 99.99;
}
int main()
{
double acc[20] = { 0.0038,0.0049,0.0061,0.0075,0.0088,0.01,0.0112,0.0123,0.0133,0.0140,0.0146,0.0152,0.0157,0.0162,0.0167,0.0172,0.0175,0.0178,0.0179,0.0179 };
int len = 20;
double dt = 0.005;
double maxPeriod = 10.0;
double periodStep = 0.02;
double dampRatio = 0.05;
int numt = int(maxPeriod / periodStep) + 1;
double *Fre = new double[numt];
double *MDis = new double[numt];
double *MVel = new double[numt];
double *MAcc = new double[numt];
double *Period = new double[numt];
for (int i = 0; i<numt; i++) {
Period[i] = periodStep*i;
Fre[i] = 0;
MDis[i] = 0;
MVel[i] = 0;
MAcc[i] = 0;
}
Period[0] = 0.001;
//cal_spec_accel(double acc[], int len,double dt,double maxPeriod,double periodStep,double dampRatio,double *Period,double *Fre,double *MAcc,double *MVel,double *MDis)
cal_spec_accel(acc, len, dt, maxPeriod, periodStep, dampRatio, Period, Fre, MAcc, MVel, MDis, numt);
for (int i = 0; i<numt; i++)
{
cout << MAcc[i] << endl;
}
delete[] Fre;
return 0;
}

View file

@ -0,0 +1,3 @@
double maxabs(double num[], int len);
bool isALLvaluezero(double num[], int len);
void cal_spec_accel(double acc[], int len, double dt, double maxPeriod, double periodStep, double dampRatio, double *Period, double *Fre, double *MAcc, double *MVel, double *MDis, int numt);

View file

@ -0,0 +1,7 @@
cd %~dp0
python setup.py build
python test.py
pause

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,11 @@
from distutils.core import setup, Extension
from Cython.Distutils import build_ext
import numpy
setup(
cmdclass={'build_ext': build_ext},
ext_modules=[Extension("CalSpecSpea", sources=[
"CalSpecSpea.pyx", "CalSpecSpeaLib.cpp"], language="c++", include_dirs=[numpy.get_include()])]
)

View file

@ -0,0 +1,32 @@
import sys
import time
sys.path.append(
'./build/lib.{0}-{1}.{2}'.format(sys.platform, sys.version_info.major, sys.version_info.minor))
from CalSpecSpea import calspecaccel
import matplotlib.pyplot as plt
import numpy as np
t1 = time.time()
acc = np.loadtxt('hf_acc.txt')
dt = 0.005
maxPeriod = 10.0
periodStep = 0.02
dampRatio = 0.05
Period, Fre, MAcc, MVel, MDis = calspecaccel(
acc, acc.shape[0], dt, maxPeriod, periodStep, dampRatio)
print('Period[0]', Period[0])
print('Fre[0]', Fre[0])
print('MAcc[0]', MAcc[0])
print('MVel[0]', MVel[0])
print('MDis[0]', MDis[0])
t2 = time.time() - t1
print(t2)
plt.plot(Period, MAcc)
plt.show()

View file

@ -35,6 +35,8 @@
- [1.25 网络操作](网络操作/)
- [1.26 无边框自定义标题栏窗口](无边框自定义标题栏窗口/)
- [1.27 QRC资源文件使用](QRC资源文件使用/)
- [1.28 C和C++扩展](C和C++扩展/)
- [1.28.1 pyx和c++](C和C++扩展/pyx和c++/)
### [2.QGraphicsView练习](QGraphicsView练习/)