我有一个Python列表
a = [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我想获得一个索引范围,例如,如果我选择指数0经过N,我得到(对N=10)重复
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
Run Code Online (Sandbox Code Playgroud)
我当然可以(int(float(N) / len(a) - 0.5) + 1) * a先重复列表并选择其中的范围[0:10],但这感觉相当笨拙.
任何提示?
我有一个索引元组数组,我想用它从多维numpy数组中挑选出值,
import numpy
a = numpy.random.rand(10, 10, 10)
idx = [[1, 1, 2], [0, 3, 7], [9, 8, 4], [9, 9, 9]]
Run Code Online (Sandbox Code Playgroud)
理解[a[i, j, k] for i, j, k in idx]只有在a.ndim知道的情况下才有效.
任何提示?
以下问题涉及x**k * y**l * z**m在许多点评估许多单项式().
我想计算两个numpy数组的"内在能力",即
import numpy
a = numpy.random.rand(10, 3)
b = numpy.random.rand(3, 5)
out = numpy.ones((10, 5))
for i in range(10):
for j in range(5):
for k in range(3):
out[i, j] *= a[i, k]**b[k, j]
print(out.shape)
Run Code Online (Sandbox Code Playgroud)
如果相反,该行将读取
out[i, j] += a[i, k]*b[j, k]
Run Code Online (Sandbox Code Playgroud)
这将是一些内部产品,可用简单的dot或可计算的einsum.
是否有可能只在一条numpy线上执行上述循环?
从开发存储库安装Python包时,我通常会导航到setup.py找到和执行的任何位置
pip install .
Run Code Online (Sandbox Code Playgroud)
这将安装包$HOME/.local/.尼斯.
如何卸载以这种方式安装的软件包?
采取以下Mathematica代码:
f[x_] := Exp[-x];
c = 0.9;
g[x_] := c*x^(c - 1)*Exp[-x^c];
SetPrecision[Integrate[f[x]*Log[f[x]/g[x]], {x, 0.001, \[Infinity]}],20]
Run Code Online (Sandbox Code Playgroud)
Mathematica可以毫无问题地进行计算并给出答案0.010089328699390866240。我希望能够执行类似的积分,但是没有Mathematica的副本。例如,仅凭天真地在scipy中实现它,就很难使用标准正交库,因为f(x)和g(x)任意接近0。这是使用标准正交的Python示例,由于需要无限的精度而失败::
from scipy.integrate import quad
import numpy as np
def f(x):
return sum([ps[idx]*lambdas[idx]*np.exp(- lambdas[idx] * x) for idx in range(len(ps))])
def g(x):
return scipy.stats.weibull_min.pdf(x, c=c)
c = 0.9
ps = [1]
lambdas = [1]
eps = 0.001 # weibull_min is only defined for x > 0
print(quad(lambda x: f(x) * np.log(f(x) / g(x)), eps, np.inf)) # Output
Run Code Online (Sandbox Code Playgroud)
应该大于0
如何在代码中像Mathematica一样执行这种不正确的积分?我不介意使用哪种免费语言/图书馆。
我正在从通过转换函数运行的输入列表组成一个 Python 列表。我只想在输出列表中包含那些结果不是的项目None。这有效:
def transform(n):
# expensive irl, so don't execute twice
return None if n == 2 else n**2
a = [1, 2, 3]
lst = []
for n in a:
t = transform(n)
if t is not None:
lst.append(t)
print(lst)
Run Code Online (Sandbox Code Playgroud)
[1, 9]
Run Code Online (Sandbox Code Playgroud)
我有预感,这可以通过理解来简化。然而,直接的解决方案
[1, 9]
Run Code Online (Sandbox Code Playgroud)
不好,因为transform()对每个条目应用两次。有什么办法解决这个问题吗?
我有一些时间,我读过VBO,这就是我得到的:
http://img64.imageshack.us/img64/5733/fps8.jpg
好吧,它比以前好多了.它是在Release上编译的.我使用VBO(可能,如果everthing的确定)和glDrawArrays绘制.
这是绘图代码.请给我建议如何优化它.我想要地形......呃几千FPS,这是真的吗?
void DrawVBO (void)
{
int i;
CVert* m_pVertices;
CTexCoord* m_pTexCoords;
unsigned int m_nTextureId;
m_pVertices = NULL;
m_pTexCoords = NULL;
m_nVertexCount = 0;
m_nVBOVertices = m_nVBOTexCoords = m_nTextureId = 0;
if( IsExtensionSupported( "GL_ARB_vertex_buffer_object" ) )
{
// Pobierz wska?niki na funkcje OpenGL
glGenBuffersARB = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress("glGenBuffersARB");
glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress("glBindBufferARB");
glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress("glBufferDataARB");
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)
wglGetProcAddress("glDeleteBuffersARB");
}
todrawquads=0;
nIndex=0;
// my function counting how many quads I will draw
for (i=0;i<MAX_CHUNKS_LOADED;i++)
{
if (chunks_loaded[i].created==1)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个在整数类型上模板化的C++类,例如,
template<typename int_type>
Run Code Online (Sandbox Code Playgroud)
比如那个类中的某个地方,我想用来sscanf从文件中读取一些值,例如,
int_type num_rows;
fgets( buffer, BUFSIZE, in_file );
sscanf( buffer, "%d", &num_rows);
Run Code Online (Sandbox Code Playgroud)
格式说明符仅int_type在内在函数时才能正常工作int.
是否有更好的方法来处理格式说明符int_type?
我在B.h头文件中定义了一个B类.B有一个静态数据成员.我在头文件中的B类中定义了这个静态数据成员.但是当我构建它时,会发生错误.
main.obj:错误LNK2005:"public:static class std :: basic_string,class std :: allocator> B :: b"(?b @ B @@ 2V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ A)已在B.obj中定义
致命错误LNK1169:找到一个或多个多重定义的符号
B.h:
#ifndef _B_H
#define _B_H
#include <string>
class B
{
public:
B();
~B();
static void showfunc();
static std::string b;
};
std::string B::b = "BBB";
#endif
Run Code Online (Sandbox Code Playgroud)
B.cpp:
#include <iostream>
#include <string>
#include "B.h"
using namespace std;
B::B()
{
}
B::~B()
{ …Run Code Online (Sandbox Code Playgroud) 从matplotlib散点图中,我正在尝试恢复点数据.考虑
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure()
x = np.linspace(0.0, 1.0, 5)
y = np.linspace(0.0, 1.0, 5)
plt.scatter(x, y)
ax = fig.get_children()[1]
pc = ax.get_children()[2]
for path in pc.get_paths():
print
print('path:')
print(path)
print
print('segments:')
for vert, code in path.iter_segments():
print(code, vert)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这产生了
path:
Path(array([[ 0. , -0.5 ],
[ 0.13260155, -0.5 ],
[ 0.25978994, -0.44731685],
[ 0.35355339, -0.35355339],
[ 0.44731685, -0.25978994],
[ 0.5 , -0.13260155],
[ 0.5 , 0. ],
[ 0.5 …Run Code Online (Sandbox Code Playgroud)