scipy.special.binom和scipy.misc.comb有什么区别?(What's the difference between scipy.special.binom and scipy.misc.comb?)
scipy.special.binom和scipy.misc.comb有什么不同?
在ipython中,我可以看到它们返回不同的类型,并且具有不同的准确性。
scipy.special.binom(4,3) 4.0 scipy.misc.comb(4,3) array(4.000000000000001)
然而,他们究竟做了什么不同呢?
看看https://github.com/scipy/scipy/blob/master/scipy/special/generate_ufuncs.py,scipy.special.binom说
binom -- binom: dd->d -- orthogonal_eval.pxd
scipy.misc.comb调用scipy.special.gammaln,它的行在generate_ufuncs.py中说
gammaln -- lgam: d->d, clngamma_wrap: D->D -- cephes.h, specfun_wrappers.h
What is the difference between scipy.special.binom and scipy.misc.comb?
In ipython I can see they return different types and also have different accuracy.
scipy.special.binom(4,3) 4.0 scipy.misc.comb(4,3) array(4.000000000000001)
However what exactly are they doing differently?
Looking at https://github.com/scipy/scipy/blob/master/scipy/special/generate_ufuncs.py , scipy.special.binom says
binom -- binom: dd->d -- orthogonal_eval.pxd
scipy.misc.comb calls scipy.special.gammaln whose line in generate_ufuncs.py says
gammaln -- lgam: d->d, clngamma_wrap: D->D -- cephes.h, specfun_wrappers.h
原文:https://stackoverflow.com/questions/21535852
最满意答案
流迭代器为iostream的格式化提取/插入操作提供迭代器接口。 例如,请考虑以下几点:
std::vector<int> v; for (int n; std::cin >> n; ) v.push_back(n);
这相当于:
std::vector<int> v(std::istream_iterator<int>(std::cin), std::istream_iterator<int>{});
推进迭代器执行一次提取,类似于
std::cin >> n
; 如果提取失败,则迭代器将采用奇异状态,这也是缺省构造的迭代器的状态。就其性质而言,这些流迭代器是单遍的 ,即最弱的迭代器:您只能访问一次“范围”中的每个元素,并且永远不会返回; 并且从同一个流构建的两个非结束迭代器相等,尽管对于取消引用的值没有任何意义。 (但是请注意,如果你没有对迭代器进行解引用,那么是否曾经尝试过第一次提取是一种奇怪的缺乏特异性。)
为了完整性,可以类似地使用输出流迭代器来将流转换为“容器”,这对于使用迭代器的算法很有用:
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "\n"));
输出流迭代器在分配给它时执行其工作; 其他操作是空操作。
有趣的是,没有一个包含
getline
的流迭代器; 人们经常自己写一个,因为迭代器接口在很多方面都很有用。Stream iterators provide an iterator interface to the formatted extraction/insertion operations of iostreams. For example, consider the following:
std::vector<int> v; for (int n; std::cin >> n; ) v.push_back(n);
This is equivalent to:
std::vector<int> v(std::istream_iterator<int>(std::cin), std::istream_iterator<int>{});
Advancing the iterator performs one exraction, akin to
std::cin >> n
; if the extraction fails, the iterator assumes the singular state, which is also the state of the default-constructed iterator.By their very nature, these stream iterators are single-pass, i.e. the weakest kind of iterator: You can only visit each element in the "range" once, and never go back; and two non-end iterators built from the same stream compare equal although that does not have any meaning regarding the dereferenced value. (But note that there's a curious lack of specificity as to whether the first extraction is ever attempted if you don't dereference the iterator ever.)
Just for completeness, output stream iterators can be used similarly to turn a stream into a "container", useful for algorithms that work with iterators:
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "\n"));
Ouput stream iterators perform their work when assigned to; the other operations are no-ops.
Interestingly enough, there isn't a stream iterator that wraps
getline
; people often write one themselves, because iterator interfaces are useful in many ways.
相关问答
更多-
流迭代器为iostream的格式化提取/插入操作提供迭代器接口。 例如,请考虑以下几点: std::vector
v; for (int n; std::cin >> n; ) v.push_back(n); 这相当于: std::vector v(std::istream_iterator (std::cin), std::istream_iterator {}); 推进迭代器执行一次提取,类似于std::cin > ... -
标题和类之间的关系不一定是一对一的; 这只是经常教给新手程序员的经验法则。 事实上,C ++语言标准并没有在类,实现文件( 翻译单元 )和头文件之间指定任何直接关系,标准库经常偏离这个规则。 std::iostream是模板类std::basic_iostream (特别是basic_iostream
)的typedef。 在我的平台上, 包括定义basic_iostream ,以及包含typedef 。 The relation bet ... -
为什么我的代码需要
?(Why does my code need [2023-05-29]?) 你不需要,你需要 for std::string ,你可能通过 间接得到它。 You do not need , you need for std::string, which you may be getting indirectly via . -
问题不在于记录器,而在于使用stringstream。 当std :: stringstream被初始化时,流的位置指示符被定位在流的开始处。 现在,当您开始使用'<<'写入字符串时,您就开始在位置指示器上书写,替换之前的任何东西。 为了解决这个问题,你可以用std :: stringstream(“Another”,stringstream :: in | stringstream :: out | std :: stringstream :: ate)初始化stringstream。 (按照http:/ ...
-
用iostream处理错误(Handling errors with iostream)[2023-04-01]
C ++标准库的头文件( iostream , vector等)最后没有.h 。 (然而,大多数第三方库确实使用.h 。由于std_lib_facilities.h确实有.h,所以假设它不是一个标准库并不是Visual C ++应该提供的东西是合理的。 在这种情况下,快速的谷歌搜索显示它随书提供,可从http://www.stroustrup.com/Programming/std_lib_facilities.h下载。 The C++ Standard Library's header files (io ... -
iostream的问题(Problem with iostream)[2023-12-26]
问题出在您的代码中。 简而言之,您输入的用于提交n的数字的换行仍然存储在输入缓冲区中,因为它不是数字输入,因此不会被n消耗。 然后getline函数吸收换行符并完成。 The problem is in your code. In a nutshell, the newline you type to commit the number for n is still stored in the input buffer as it is not numerical input, so is not cons ... -
为什么istream,ostream,iostream,ios_base都在这里工作(why istream,ostream,iostream,ios_base all work here)[2023-04-03]
因为failbit是ios_base的public成员,所有其他类public继承自ios_base 。 Because the failbit is a public member of ios_base and all the other classes publicly inherit from ios_base. -
因为 cout::operator <<(void*) 打印一个内存地址,并且 cout::operator <<(char*) 打印一个以空字符结尾的字符数组,当您尝试从0x10读取char数组时,会遇到未定义的行为。 Because cout::operator <<(void*) prints a memory address, and cout::operator <<(char*) prints a null-terminated character array, and you ru ...
-
重载Iostream C ++(Overloading Iostream C++)[2023-05-22]
为什么不使用? 例: #include class Example { public: Example(int i) : i(i) {} private: int i; friend std::ostream& operator<<(std::ostream& os, Example const& example); }; #include int main() { Example e(123); std ... -
std :: cin >> vector [i]不会让迭代器正常工作吗?(std::cin >> vector[i] won't let iterators work properly?)[2022-02-19]
reserve()只是预先分配空间,以便后续附加不需要(尽可能多)重新分配 - 它不会调整向量的大小 。 要调整它的大小,你需要... resize() 。 :) std::vector分别跟踪它自己的大小 (元素数量)和容量 (它可以包含的最大元素数量,而不需要重新分配)。 查看reserve()不会改变大小的最简单方法是在reserve()之后调用向量上的size() reserve() 。 你会看到矢量仍然“相信”它的大小是0。 reserve() just preallocates space ...