相关疑难解决方法(0)

从递归到迭代的方法

在我多年的编程中,我已经使用递归来解决简单的问题,但我完全清楚,有时你需要迭代,因为内存/速度问题.

所以,在很久以前的某个时候,我去尝试找出是否存在任何"模式"或文本书的方式将常见的递归方法转换为迭代而没有发现任何东西.或者至少我记不住任何事都会有所帮助.

  • 有一般规则吗?
  • 有"模式"吗?

theory iteration recursion computer-science

323
推荐指数
11
解决办法
12万
查看次数

使用Python的样条线(使用控制结和端点)

我正在尝试执行以下操作(从维基百科中提取的图像)

仿样

#!/usr/bin/env python
from scipy import interpolate
import numpy as np
import matplotlib.pyplot as plt

# sampling
x = np.linspace(0, 10, 10)
y = np.sin(x)

# spline trough all the sampled points
tck = interpolate.splrep(x, y)
x2 = np.linspace(0, 10, 200)
y2 = interpolate.splev(x2, tck)

# spline with all the middle points as knots (not working yet)
# knots = x[1:-1]  # it should be something like this
knots = np.array([x[1]])  # not working with above line and just seeing …
Run Code Online (Sandbox Code Playgroud)

python math numpy spline cubic-spline

14
推荐指数
3
解决办法
9258
查看次数

使用Python进行B样条插值

我试图用Python重现B样条的Mathematica示例.

mathematica示例的代码读取

pts = {{0, 0}, {0, 2}, {2, 3}, {4, 0}, {6, 3}, {8, 2}, {8, 0}};
Graphics[{BSplineCurve[pts, SplineKnots -> {0, 0, 0, 0, 2, 3, 4, 6, 6, 6, 6}], Green, Line[pts], Red, Point[pts]}]
Run Code Online (Sandbox Code Playgroud)

并产生我所期望的.现在我尝试用Python/scipy做同样的事情:

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as si

points = np.array([[0, 0], [0, 2], [2, 3], [4, 0], [6, 3], [8, 2], [8, 0]])
x = points[:,0]
y = points[:,1]

t = range(len(x))
knots = [2, 3, 4]
ipl_t …
Run Code Online (Sandbox Code Playgroud)

python bspline

12
推荐指数
2
解决办法
2万
查看次数

为什么python中的函数/方法调用很昂贵?

这篇文章中,Guido van Rossum说功能调用可能很昂贵,但我不明白为什么也不贵.

多少延迟会为您的代码添加一个简单的函数调用,为什么?

python profiling function-calls python-internals

8
推荐指数
3
解决办法
5591
查看次数