我最近开始自学游戏编程.有人建议我从Python开始,我得到了"用Python和Pygame开始游戏开发:从新手到专业"的书.我得到了一个他们教导Vector和创建Vector2类的部分.一切顺利,直到我试图超过除法运算符.我的代码是这样的:
class Vector2(object):
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def __str__(self):
return "(%s, %s)"%(self.x, self.y)
@classmethod
def from_points(cls, P1, P2):
return cls(P2[0] - P1[0], P2[1] - P1[1])
def __add__(self,rhs):
return Vector2(self.x + rhs.x, self.y + rhs.y)
def __sub__(self,rhs):
return Vector2(self.x - rhs.x, self.y - rhs.y)
def __mul__(self, scalar):
return Vector2( self.x*scalar, self.y*scalar)
def __div__(self, scalar):
return Vector2( self.x/scalar, self.y/scalar)
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试调用"/"运算符时,会显示:
AB = Vector2(10.0,25.0)
print(AB) # <<<<(10.0, 25.0)
v1 = AB + Vector2(20.,10.)
print(v1) # <<<<(30.0, 35.0) …Run Code Online (Sandbox Code Playgroud) 在Matlab 2014b,当我CLEAR ALL在脚本的开头使用时,我得到以下警告,
For improved performance, consider not using CLEAR ALL within a script
Run Code Online (Sandbox Code Playgroud)
这在以前的版本中没有给出(我记得).
我发现的唯一原因是,当你从外面或者从其他脚本调用脚本你不想清除在工作区中的变量,并一次又一次地重新生成它们每次.
还有其他原因让我失踪吗?
CLEAR ALL使用单个脚本时,删除如何提高性能?
我想知道的区别(S)PCL :: PCLPointCloud2和PCL ::点云中的点云库(PCL).有一个函数可以从一个转换为另一个,但文档很差.我想知道哪一个更新?
ps(我不是在谈论ROS)
附上图像(test.tif).np.nan值是最白的区域.如何使用一些使用邻居值的间隙填充算法填充那些最白的区域?
import scipy.ndimage
data = ndimage.imread('test.tif')
Run Code Online (Sandbox Code Playgroud) 考虑一个多项式,例如:
p = [1 -9 27 -27];
Run Code Online (Sandbox Code Playgroud)
显然真正的根是3:
polyval(p,3)
0
Run Code Online (Sandbox Code Playgroud)
使用该roots功能时
q = roots([1 -9 27 -27]);
Run Code Online (Sandbox Code Playgroud)
用format short:
q =
3.0000 + 0.0000i
3.0000 + 0.0000i
3.0000 - 0.0000i
Run Code Online (Sandbox Code Playgroud)
并检查根是否真实:
bsxfun(@eq,ones(size(q)),isreal(q))
0
0
0
Run Code Online (Sandbox Code Playgroud)
format long我得到的更糟糕的是:
roots([1 -9 27 -27])
ans =
3.000019414068325 + 0.000000000000000i
2.999990292965843 + 0.000016813349886i
2.999990292965843 - 0.000016813349886i
Run Code Online (Sandbox Code Playgroud)
如何正确计算多项式的根?
我决定使用memmapfile,因为我的数据(通常为30Gb到60Gb)太大而无法放入计算机的内存中.
我的数据文件包含两列数据,对应两个传感器的输出,我有.bin和.txt两种格式.
m=memmapfile('G:\E-Stress Research\Data\2013-12-18\LD101_3\EPS/LD101_3.bin','format','int32')
m.data(1)
Run Code Online (Sandbox Code Playgroud)
我使用上面的代码将我的数据存储到变量"m",但我不知道使用什么数据格式(int8','int16','int32','int64','uint8','uint16', 'uint32','uint64','single'和'double').事实上,我尝试了MATLAB支持的所有数据格式,但是当我使用m.data(索引号)时,我从来没有得到一对数字(2列数据),这是我的预期,也就是数字根据我使用的格式不同.
如果有人有memmapfile的经验,请帮助我.
以下是我的数据文件的一些较小版本,以便人们可以理解我的数据结构:
詹姆斯欢呼
我在MATLAB中用两种不同的方式编写了一些代码.首先,我使用了两个for循环,乍一看似乎很愚蠢:
Initial = [zeros(10,1) ones(10,1)];
for xpop=1:10
for nvar=1:10
Parent(xpop,nvar) = Initial(nvar,1)+(Initial(nvar,2)-Initial(nvar,1))*rand();
end
end
Run Code Online (Sandbox Code Playgroud)
在第二个方案中,我尝试进行矢量化计算(我假设它可以更快):
Parent = repmat(Initial(:,1),1,10) + rand(10,10).*(repmat(Initial(:,2),1,10)-repmat(Initial(:,1),1,10));
Run Code Online (Sandbox Code Playgroud)
可以在以下三个不同的代码运行中看到经过的时间:
Elapsed time is 0.000456 seconds.
Elapsed time is 0.006342 seconds.
Elapsed time is 0.000457 seconds.
Elapsed time is 0.006147 seconds.
Elapsed time is 0.000471 seconds.
Elapsed time is 0.006433 seconds.
Run Code Online (Sandbox Code Playgroud)
为什么第一个方案比第二个方案更快?它真的在'.*'命令中为循环做了两个愚蠢的事吗?
我在Python中编写了以下代码,以便估算其值Pi.它被称为蒙特卡罗方法.显然,通过增加样本数量,代码变得更慢,我认为代码中最慢的部分是在采样部分.我怎样才能让它更快?
from __future__ import division
import numpy as np
a = 1
n = 1000000
s1 = np.random.uniform(0,a,n)
s2 = np.random.uniform(0,a,n)
ii=0
jj=0
for item in range(n):
if ((s1[item])**2 + (s2[item])**2) < 1:
ii = ii + 1
print float(ii*4/(n))
Run Code Online (Sandbox Code Playgroud)
你有其他(大概更快的)代码吗?
我有以下问题,我将尝试使用以下示例进行说明.
class Brick():
def __init__(self):
self.weight = 1
class House():
def __init__(self, number_bricks):
self.bricks = [Brick() for i in range(number_bricks)]
def get_weight(self):
return reduce(lambda x,y: x+y, [brick.weight for brick in self.bricks])
Run Code Online (Sandbox Code Playgroud)
但现在假设我创建了一种新的Brick,StrongBrick,以便我创建一个房子,一个子类StrongHouse,StrongBrick在StrongHouse扮演与Brick在House中扮演的角色完全相同的角色.我怎样才能以一种很好的方式做到这一点(不只是重新输入所有的类定义)?
所以基本的想法是,如何将一些由一些对象组成的类更改为同一个类但是由原始成员对象的子类组成?
非常感谢你能给我的任何帮助.
我正在寻找一种更有效的方法来制作以下矩阵L:
|2 -2 0 0 0 |
|-1 2 -1 0 0 0 |
|0 -1 2 -1 0 |
(1/2).|0 0 -1 2 -1 |
| . . . |
| 0 0 -1 2 -1|
| 0 0 -2 2|
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有这个:
L = diag(ones(n,1)*2)- ...
diag(ones(n-1,1),1) - ...
diag(ones(n-1,1),-1);
L(1,2) = -2;
L(end,end-1) = -2;
L = L/2;
Run Code Online (Sandbox Code Playgroud)
例如,因为n=5它产生:
L =
1.0000 -1.0000 0 0 0
-0.5000 1.0000 -0.5000 0 0
0 -0.5000 1.0000 …Run Code Online (Sandbox Code Playgroud) matlab ×5
python ×4
performance ×3
bigdata ×1
matrix ×1
numpy ×1
oop ×1
pi ×1
polynomials ×1
python-3.x ×1
scipy ×1