首页 \ 问答 \ scipy.special.binom和scipy.misc.comb有什么区别?(What's the difference between scipy.special.binom and scipy.misc.comb?)

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
更新时间:2022-01-19 16:01

最满意答案

流迭代器为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 ...
  • 你不需要 ,你需要 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:/ ...
  • 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 ...
  • 问题出在您的代码中。 简而言之,您输入的用于提交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 ...
  • 因为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 ...
  • 为什么不使用 ? 例: #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 ...
  • reserve()只是预先分配空间,以便后续附加不需要(尽可能多)重新分配 - 它不会调整向量的大小 。 要调整它的大小,你需要... resize() 。 :) std::vector分别跟踪它自己的大小 (元素数量)和容量 (它可以包含的最大元素数量,而不需要重新分配)。 查看reserve()不会改变大小的最简单方法是在reserve()之后调用向量上的size() reserve() 。 你会看到矢量仍然“相信”它的大小是0。 reserve() just preallocates space ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)