我有一些代码使用 scipy.integration.cumtrapz 来计算采样信号的反导数。我想使用辛普森规则而不是梯形。然而 scipy.integration.simps 似乎没有累积的对应物......我错过了什么吗?有没有一种简单的方法可以与“scipy.integration.simps”进行累积集成?
程序员 A 编写了以下函数:
def compute_value(threshold = sys.float_info.max):
...
return value
Run Code Online (Sandbox Code Playgroud)
它具有可选参数阈值,它以自然的方式将最大浮点值作为默认值来表示“无阈值”。
程序员 B 也有一个阈值的表示,但是用 None 来表示没有阈值。不幸的是,如果阈值=无,compute_value 函数不会引发任何异常,但给出了不正确的答案。因此,当程序员 B 通过 None 作为阈值时,会出现错误。
我会说最好的解决方案是更改功能
def compute_value(threshold = None):
if threshold is None:
threshold = sys.float_info.max
...
return value
Run Code Online (Sandbox Code Playgroud)
因为这个函数比以前更通用,因为它以一种有意义的方式处理 None 值。
这就提出了一个问题:是否最好只使用 None 作为默认值?
具有不同于 None 的默认参数的函数给我带来麻烦已经不是第一次了。在其他情况下,我发现自己从 kwargs 字典中删除 None 值......
另一个相关(可能很愚蠢)的问题。其实程序员B修改了上面的函数如下:
def compute_value(threshold = sys.float_info.max):
if threshold is None:
threshold = sys.float_info.max
...
return value
Run Code Online (Sandbox Code Playgroud)
这是完全正确的,但对我来说似乎很糟糕。不好,因为 sys.float_info.max 重复了两次……但是:这是否违反了 DRY 原则?因为,严格来说,在第一个实现中 None 也重复了两次,并且 None 和 sys.float_info.max 都是常量。
我可以假设(int)(float)n == n任何int n?至少我需要这个非负31位值.
附录.怎么样(int)(double)n ==n?
我正在使用形状为 (N,)、(N,3) 和 (N,3,3) 的 numpy 数组,它们表示 3D 空间中的标量、向量和矩阵序列。我已经实现了逐点点积、矩阵乘法和矩阵/向量乘法,如下所示:
def dot_product(v, w):
return np.einsum('ij, ij -> i', v, w)
def matrix_vector_product(M, v):
return np.einsum('ijk, ik -> ij', M, v)
def matrix_matrix_product(A, B):
return np.einsum('ijk, ikl -> ijl', A, B)
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用 einsum 是因为缺乏更好的解决方案。令我惊讶的是,我无法使用 np.dot ......这似乎不适合这种需要。有没有更numpythonic的方式来实现这些功能?
特别是如果函数也可以通过广播第一个缺失的轴在形状 (3,) 和 (3,3) 上工作,那就太好了。我想我需要省略号,但我不太明白如何实现结果。
我的crontab中有这一行:
0 0 * * * /usr/local/bin/php5 -d allow_url_fopen=on -f /path/to/file.php
Run Code Online (Sandbox Code Playgroud)
我找不到任何关于"-d"和"-f"标志含义的全面参考.他们的意思是什么?是否有一个页面解释这些和其他标志?
我做:
sudo pip install --upgrade tables
Run Code Online (Sandbox Code Playgroud)
我明白了:
/usr/bin/ld: cannot find -lhdf5
collect2: ld returned 1 exit status
.. ERROR:: Could not find a local HDF5 installation.
You may need to explicitly state where your local HDF5 headers and
library can be found by setting the ``HDF5_DIR`` environment
variable or by using the ``--hdf5`` command-line option.
Complete output from command python setup.py egg_info:
/usr/bin/ld: cannot find -lhdf5
Run Code Online (Sandbox Code Playgroud)
然而:
$ echo $HDF5_DIR
/opt/hdf5/
$ ls /opt/hdf5/
bin include lib share
$ ls …Run Code Online (Sandbox Code Playgroud) 我对"不是"运算符有点害怕,并且当"是(不是X)"意图时解释"不是X"的可能性.存在一些表达式A和B,使得:
A is not B
Run Code Online (Sandbox Code Playgroud)
不同于
A is (not B)
Run Code Online (Sandbox Code Playgroud)
?
附录.使用此运算符是否被视为良好做法?不not (A is B)应该是首选?
我有以下代码:
#include <stdio.h>
enum {A, B};
#define C A
int main() {
#if C == B
printf("%d==%d\n", C, B);
#else
printf("%d!=%d\n", C, B);
#endif
}
Run Code Online (Sandbox Code Playgroud)
这令人惊讶地给出了输出:
0==1
Run Code Online (Sandbox Code Playgroud)
现在,我理解代码是错误的,因为预处理器不知道枚举值.我不明白为什么没有生成错误... A和B应该在预处理时间未定义,预处理器如何不给出错误?
这是代码:
\n#include <Eigen/Dense>\nusing namespace Eigen;\nenum { TWO = 2};\nMatrix<float, Dynamic, TWO> m1(42, 2); // good...\nMatrix<float, Dynamic, TWO> m2(42, int(TWO)); // good...\nMatrix<float, Dynamic, TWO> m3(42, TWO); // error!\nRun Code Online (Sandbox Code Playgroud)\n当我编译 m3 的声明时,会引发错误,就像枚举值一样TWO是浮点数一样:
$ g++ -I/usr/include/eigen3 -c bug.cpp\nIn file included from /usr/include/eigen3/Eigen/Core:366,\n from /usr/include/eigen3/Eigen/Dense:1,\n from bug.cpp:1:\n/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h: In instantiation of \xe2\x80\x98void Eigen::PlainObjectBase<Derived>::_init2(Eigen::Index, Eigen::Index, typename Eigen::internal::enable_if<(typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime != 2), T0>::type*) [with T0 = int; T1 = <unnamed enum>; Derived = Eigen::Matrix<float, -1, 2>; Eigen::Index = long int; typename Eigen::internal::enable_if<(typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime …Run Code Online (Sandbox Code Playgroud) 在我的C#游戏TextQuest中,战斗系统正在进行中,我遇到了一个问题.如果玩家错过了Monster也错过了(总是在同一时间).这是使用过的代码:
private void button1_Click(object sender, EventArgs e)
{
Monster_Class mc = new Monster_Class();
Account_Class ac = new Account_Class();
if (hitOrMiss())
{
int hit = -1;
hit = mc.hitAmount(ac.getAtk(Properties.Settings.Default.CurrentUser), mc.getDef(monster));
mhealth -= hit;
if (hit == -1)
{
setText("You missed.");
}
else
{
setText("You hit " + hit + ".");
}
monsterHit();
update();
}
else
{
setText("You missed.");
monsterHit();
}
}
private void monsterHit()
{
Monster_Class mc = new Monster_Class();
Account_Class ac = new Account_Class();
if (hitOrMiss())
{
int hit = …Run Code Online (Sandbox Code Playgroud)