关于"除了代码之外的所有内容并行运行"的问题,来自Node.js的新人.这是一个明显人为的例子,但是我想说我想创建一个包含函数的数学库,factorize()其行为如下:
var http = require('http');
http.createServer(function (req, res) {
myMath.factorize(some_big_number,function(factors) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(factors));
}
}).listen(8000);
Run Code Online (Sandbox Code Playgroud)
如何编写它以便"并行运行"?
我一直在查看这个库中的解析代码作为一个可能需要一些处理时间的例子.代码的主体被认为是"你的代码",还是"并行运行"?
如果不是:在编写时我需要做什么,factorize()以便它也是非阻塞/表现得像客户端?使用EventEmitter是否足够?
如果不清楚,请提前道歉.
我试图在我的一个程序中复制主要瓶颈.
我想同时获得几个非整数像素值的线性(或相当双线性)插值.这是不是每一个像素坐标以同样的方式被干扰的情况.下面是一个完整/最小的脚本以及演示该问题的注释.我怎样才能加快计算速度result?
import numpy as np
import time
im = np.random.rand(640,480,3) # my "image"
xx, yy = np.meshgrid(np.arange(im.shape[1]), np.arange(im.shape[0]))
print "Check these are the right indices:",np.sum(im - im[yy,xx,:])
# perturb the indices slightly
# I want to calculate the interpolated
# values of "im" at these locations
xx = xx + np.random.normal(size=im.shape[:2])
yy = yy + np.random.normal(size=im.shape[:2])
# integer value/pixel locations
x_0 = np.int_(np.modf(xx)[1])
y_0 = np.int_(np.modf(yy)[1])
x_1, y_1 = x_0 + 1, y_0 …Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法在Matlab中读取.npy文件?我知道我可以使用scipy.io.savematPython 将它们转换为Matlab风格的.mat文件; 但是我对Matlab中的.npy文件的本机或插件支持更感兴趣.
当我比较我的函数中的两个numpy数组时,我得到一个错误,说只有长度为1的数组可以转换为Python标量:
from numpy.random import rand
from numba import autojit
@autojit
def myFun():
a = rand(10,1)
b = rand(10,1)
idx = a > b
return idx
myFun()
Run Code Online (Sandbox Code Playgroud)
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-f7b68c0872a3> in <module>()
----> 1 myFun()
/Users/Guest/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numba/numbawrapper.so in numba.numbawrapper._NumbaSpecializingWrapper.__call__ (numba/numbawrapper.c:3764)()
TypeError: only length-1 arrays can be converted to Python scalars
Run Code Online (Sandbox Code Playgroud) 这必须是一个非常基本的问题:我正在尝试使用Matplotlib.这是文档中的基本示例.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
Run Code Online (Sandbox Code Playgroud)
我试过这个ipython,bpython默认的解释器(Ubuntu 10.10,64位)和我得到的是以下消息:
[<matplotlib.lines.Line2D object at 0x3f14a90>]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
如果我有一个无向图,我怎样才能获得所有周期的列表?
例如,从下图中,我想要循环:
(a,b,d,e,c)
(a,b,c)
(b,d,e)
Run Code Online (Sandbox Code Playgroud)

我无法在PyQt中获得一个基本的着色器程序.我认为这至少应该正确编译着色器代码(我在这里不是专家),但addShaderFromSourceFile()无论我尝试什么,总是返回false.着色器程序日志也始终为空.
我在Ubuntu 12.04上,我可以用C++编译和运行GLSL着色器程序.所以我不认为这是一个系统问题.
文件shader.vert
void main(void)
{
gl_Position = ftransform();
}
Run Code Online (Sandbox Code Playgroud)
文件shader.frag
void main(void)
{
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
Run Code Online (Sandbox Code Playgroud)
文件test_shaders.py
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4 import QtCore, QtGui
from PyQt4.QtOpenGL import *
class ExampleQGLWidget(QGLWidget):
def __init__(self, parent):
QGLWidget.__init__(self, parent)
self.shaderProgram = QGLShaderProgram()
print self.shaderProgram.addShaderFromSourceFile(QGLShader.Vertex, "shader.vert")
print self.shaderProgram.addShaderFromSourceFile(QGLShader.Fragment, "shader.frag")
print self.shaderProgram.log()
self.shaderProgram.link()
glViewport(0,0, 640, 480)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
self.shaderProgram.bind()
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity() …Run Code Online (Sandbox Code Playgroud) 今天早些时候,我在尝试挑选一个namedtuple实例时遇到了麻烦.作为一个完整性检查,我尝试运行一些在另一个答案中发布的代码.在这里,简化了一点:
from collections import namedtuple
import pickle
P = namedtuple("P", "one two three four")
def pickle_test():
abe = P("abraham", "lincoln", "vampire", "hunter")
f = open('abe.pickle', 'w')
pickle.dump(abe, f)
f.close()
pickle_test()
Run Code Online (Sandbox Code Playgroud)
然后我改变了两行来使用我的命名元组:
from collections import namedtuple
import pickle
P = namedtuple("my_typename", "A B C")
def pickle_test():
abe = P("ONE", "TWO", "THREE")
f = open('abe.pickle', 'w')
pickle.dump(abe, f)
f.close()
pickle_test()
Run Code Online (Sandbox Code Playgroud)
然而,这给了我错误
File "/path/to/anaconda/lib/python2.7/pickle.py", line 748, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <class '__main__.my_typename'>: it's not found …Run Code Online (Sandbox Code Playgroud) 如何对以下双循环进行矢量化?
我有一个N乘A矩阵和一个N乘B矩阵,其中A和B可能不同,N比A和B小得多.我想按如下方式生成A×B矩阵,但理想情况下没有循环:
import numpy as np
def foo(arr):
# can be anything - just an example so that the code runs
return np.sum(arr)
num_a = 12
num_b = 8
num_dimensions = 3
a = np.random.rand(num_dimensions, num_a)
b = np.random.rand(num_dimensions, num_b)
# this is the loop I want to eliminate:
output = np.zeros( (num_a, num_b) )
for i in xrange(num_a):
for j in xrange(num_b):
output[i,j] = foo(a[:,i] - b[:,j])
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
今天一直试图实施Rabin-Miller Strong Pseudoprime Test.
使用Wolfram Mathworld作为参考,第3-5行总结了我的代码.
然而,当我运行程序时,它(有时)说素数(甚至低,如5,7,11)不是素数.我已经查看了很长一段时间的代码,无法弄清楚出了什么问题.
为了帮助我看了这个网站以及许多其他网站,但大多数使用另一个定义(可能相同,但因为我是这种数学的新手,我看不到相同的明显连接).
我的代码:
import random
def RabinMiller(n, k):
# obviously not prime
if n < 2 or n % 2 == 0:
return False
# special case
if n == 2:
return True
s = 0
r = n - 1
# factor n - 1 as 2^(r)*s
while r % 2 == 0:
s = s + 1
r = r // 2 # floor
# k = accuracy
for i …Run Code Online (Sandbox Code Playgroud) python ×8
numpy ×5
scipy ×3
algorithm ×2
matlab ×2
events ×1
glsl ×1
graph-theory ×1
javascript ×1
math ×1
matplotlib ×1
namedtuple ×1
node.js ×1
numba ×1
opengl ×1
pickle ×1
pyqt ×1
python-2.7 ×1
qt ×1