我巧合地发现我无法在python代码中更改实际目录.我的测试程序如下:
from os import system
def sh(script):
system("bash -c '%s'" % script)
sh("cd /home")
sh("pwd")
Run Code Online (Sandbox Code Playgroud)
输出pwd不是/home,而是上面代码所在的目录.
有人可以解释为什么会这样吗?
这是我的一部分 CMakeLists.txt
set (VTK_DIR "/usr/include/vtk-5.8")
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
IF(VTK_FOUND)
message("found VTK. Version:" ${VTK_VERSION}. VTK_DIR: ${VTK_DIR})
ELSE(VTK_FOUND)
MESSAGE(FATAL_ERROR
"Cannot build the executable without VTK. Please set the
VTK_DIR")
ENDIF(VTK_FOUND)
Run Code Online (Sandbox Code Playgroud)
cmake . 告诉我:
发现VTK。版本:6.0.0.VTK_DIR:/usr/local/lib/cmake/vtk-6.0
在命令行中提供 VTK_DIR 也无济于事:
cmake -DVTK_DIR:PATH=/usr/include/vtk-5.8 .
Run Code Online (Sandbox Code Playgroud)
仍然 cmake 寻找/usr/local/lib/cmake/vtk-6.0VTK。
这里有什么问题?
我有一些粒子在走廊中移动并具有封闭边界条件的数据.绘制轨迹会产生锯齿形轨迹.

我想知道如何阻止plot()连接点,粒子回到起点.有些东西比如图片的上半部分,但没有"."
我的第一个想法是在numpy数组a[:-1]-a[1:]获得positiv 时找到索引.然后从0到该索引的绘图.但是如何获得第一次出现正元素的指数a[:-1]-a[1:]?也许还有其他一些想法.
我有一个包含一些几何数据的WKT文件。
这是一个样本(折线):
s = "ST_GeomFromText( 'LINESTRING( 11.6614 48.0189, 11.6671 48.011, 11.6712 48.0051, 11.6747 48.0001, 11.6777 47.9956, 11.6795 47.9927)',4326)"
Run Code Online (Sandbox Code Playgroud)
我想要的是点的坐标。所以我做了以下事情:
s2 = s.split("'")[1]
s3 = s2.split("(")[1]
s4 = s3.strip(' )')
s5 = s4.split(',')
print s5
['11.6614 48.0189',
' 11.6671 48.011',
' 11.6712 48.0051',
' 11.6747 48.0001',
' 11.6777 47.9956',
' 11.6795 47.9927']
Run Code Online (Sandbox Code Playgroud)
这些s2, s3, s4 and s5只是虚拟变量,以表明该解决方案超出了善恶。
有没有更简洁的解决方案?
我想为我的小孩写一个包含一些算术问题的文件.我想要一些着色,这样他就可以很容易地在加号和减号之间做出区别.这对我很有用.不幸的是,只在一个终端.
import random as rd
from termcolor import colored
N = 10
MAX = 100
f = open("math.txt", "w")
def get_random_str():
a = rd.randint(1, MAX)
b = rd.randint(1, MAX)
if a < MAX*0.4:
string = "%3d "%a + str(colored('+', 'blue')) + " %d = \n"%(b)
else:
if a>b:
string = "%3d "%a + str(colored('-', 'red')) + " %d = \n"%(b)
else:
string = "%3d "%a + str(colored('-', 'red')) + " %d = \n"%(b)
return string
#-------------------------------------------------------------------------
for i in range(1,N): …Run Code Online (Sandbox Code Playgroud) 从视频记录中,可以提取行人穿过瓶颈的头部轨迹。因为,行人在移动轨迹时摇摆包含我想消除的令人讨厌的振荡。(平滑算法与此问题无关)
结果如图所示。
所以,我的目标是使用蓝点(称为零点)获得平滑的轨迹。为此,我想到了样条曲线。不幸的是,scipy 的函数splrep有一个限制,即数据应该相对于 x 轴进行排序(为什么?)。
但是在我的情况下,对数据点进行排序并不是一个好主意,它会使真实轨迹变形。样条的结果,排序后看起来像。
(图的最后一部分是第二部分的放大图,以展示振荡。)
从数学的角度来看,splrep的结果可能是正确的,但是它们没有物理相关性,这是因为我没有必要操纵轨迹。
任何想法如何用python(scipy)正确解决这个问题?
如果有人想玩数据,这是我的代码。
from matplotlib.pyplot import *
from numpy import *
from scipy import interpolate
# trajectory
path = array([
[ 3.83911987e+02, -3.63506010e+01],
[ 3.80407013e+02, -3.70444980e+01],
[ 3.77910004e+02, -3.73224980e+01],
[ 3.75592010e+02, -3.72258990e+01],
[ 3.72606995e+02, -3.68973010e+01],
[ 3.68860992e+02, -3.64117010e+01],
[ 3.64709991e+02, -3.54816020e+01],
[ 3.60441986e+02, -3.46052020e+01],
[ 3.56470001e+02, -3.33995020e+01],
[ 3.53148010e+02, -3.17936990e+01],
[ 3.50332001e+02, -2.99004990e+01],
[ 3.48214996e+02, -2.76462990e+01],
[ 3.45072998e+02, -2.56490000e+01],
[ …Run Code Online (Sandbox Code Playgroud) 这是行人在果酱中移动的轨迹

正如你所看到的,他/她的头部表现出类似振荡的运动.所以不是一个完美的正弦曲线,但不是一条线.
是否有可能为这条不规则曲线定义"幅度"和"频率"?
更新:
到目前为止我尝试了两种不同的方法
结果如下:

我在RDP中看到的问题是free参数dist.低值(意味着更多细节)意味着产生的轨迹中的振荡.所以我不得不小心这个参数.此外,splined曲线更加平滑.
你怎么看?
给出以下示例
d = array([[1, 2, 3],
[1, 2, 3],
[1, 3, 3],
[4, 4, 4],
[5, 5, 5]
])
Run Code Online (Sandbox Code Playgroud)
要获取包含1在第一列中的子数组:
d[ d[:,0] == 1 ]
array([[1, 2, 3],
[1, 2, 3],
[1, 3, 3]])
Run Code Online (Sandbox Code Playgroud)
如何得到(没有循环)包含1和的子数组5?不应该是这样的
d[ d[:,0] == [1,5] ] # ---> array([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
这不起作用?
我想随机产生array的n一和m零.
我想到了这个解决方案:
np.ones)np.zeros)np.hstack)np.random.shuffle)似乎不是一个自然的解决方案.一些pythonic的想法?
为了解决线性矩阵方程,可以使用numpy.linalg.solve
LAPACK例程* gesv。
根据文档
DGESV computes the solution to a real system of linear equations
A * X = B,
where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
The LU decomposition with partial pivoting and row interchanges is
used to factor A as
A = P * L * U,
where P is a permutation matrix, L is unit lower triangular, and U is
upper triangular. The factored form of A is …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个将文本转换为猪拉丁语的函数:简单的文本转换,修改每个单词,将第一个字符移动到末尾并将“ay”附加到末尾。但我得到的只是一个空列表。有小费吗?
def pig_latin(text):
say = ""
words = text.split()
for word in words:
endString = str(word[1]).upper()+str(word[2:])
them = endString, str(word[0:1]).lower(), 'ay'
word = ''.join(them)
return word
print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
Run Code Online (Sandbox Code Playgroud) 我想用一个简单的规则来实现一维元胞自动机:
例如:
0 1 1 0 1
更新后:
1 1 0 1 0
我的解决方案是
def update(cells):
neighbors = np.roll(cells,-1)
dim = len(cells)
tmp_cells = np.zeros(dim)
for i,j in enumerate(cells):
if j and not neighbors[i]:
tmp_cells[i], tmp_cells[(i+1)%dim] = 0, 1
elif j:
tmp_cells[i] = 1
return tmp_cells
Run Code Online (Sandbox Code Playgroud)
这工作正常,但解决方案没有利用所有的可能性,np.arrays并简化为一个简单的list算法.
我以为我可以在cells和之间找到一个简洁的逻辑neighbors,但显然我现在必须睡觉了.
一些想法?
我有以下vector对值:
first 3 second 2
first 1 second 2
first 1 second 1
first 2 second 2
Run Code Online (Sandbox Code Playgroud)
我想对我的矢量进行排序,结果就是这样
==========================
first 1 second 2
first 1 second 1
first 2 second 2
first 3 second 2
Run Code Online (Sandbox Code Playgroud)
这意味着:
我的代码看起来像:
#include <utility> // std::pair
#include <iostream> // std::cout
#include <vector>
typedef std::pair<double, double> my_pair;
struct sort_pred
{
bool operator () (const my_pair& left, const my_pair& right)
{
return (left.first < right.first) && (left.second > right.second);
}
};
int main …Run Code Online (Sandbox Code Playgroud)