valarray

valarray vs. vector: Why was valarray introduced?

懵懂的女人 提交于 2019-12-19 18:27:08
问题 Yes, this has been asked before, and the answer has been: valarray s (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make a valarray of pointers so the compiler can make assumptions about the code and optimise it better. (The main reason that Fortran is so fast is that there is no pointer type so there can be no pointer aliasing.) or: valarray is also supposed to eliminate any possibility of aliasing [...] But these answers make no sense to me. valarray

Strange C++ compile error with valarrays

半城伤御伤魂 提交于 2019-12-13 14:47:22
问题 I have a strange compile error using valarrays in C++. This is a stripped down version of my code: #include <iostream> #include <valarray> using namespace std; bool test(const int &x,const valarray<int> &a,const valarray<int> &b) { return a*x==b; } int main() { int a1[3]= {1,2,3}; int b1[3]= {2,4,6}; valarray<int> a(a1,3); valarray<int> b(b1,3); int x=2; cout<<test(x,a,b); return 0; } Expected behavior: outputs some variant of true or 1 The compile error (using g++): main.cpp: In function

c++ reading fits file using ccfits

与世无争的帅哥 提交于 2019-12-11 03:00:07
问题 So... can anyone see what I'm doing wrong here?!? I'm trying to read a *.fits file in C++ using CCfits following their example at http://heasarc.gsfc.nasa.gov/fitsio/CCfits/html/readimage.html. #include <iostream> #include <valarray> #include <CCfits/CCfits.h> #include <CCfits/PHDU.h> namespace fit = CCfits; int main(int argc, char * argv[]) { fit::FITS inFile( "../data/example/example.fits", fit::Read, true ); fit::PHDU & phdu = inFile.pHDU(); std::valarray<unsigned int> fitsImage; phdu.read

Best way to test for equality of two valarray<double>?

纵饮孤独 提交于 2019-12-11 02:07:34
问题 I think that the default overload of == for valarray is not very convenient. By default x==y (for two valarrays x and y) returns a valarray<bool> , with true on the i th entry if x[i]==y[i] . Rather, I need a single bool , which tells me if both valarray<double> contain the same elements or not. I know I can do this with a cycle, but having to do the cycle every time is not convenient. What's the best workaround here? Is there a way for me to define my own overload of == (and also != , < ,

Is it a bad idea to replace POD C-style array with std::valarray?

偶尔善良 提交于 2019-12-10 13:33:30
问题 I'm working with a code base that is poorly written and has a lot of memory leaks. It uses a lot of structs that contains raw pointers, which are mostly used as dynamic arrays. Although the structs are often passed between functions, the allocation and deallocation of those pointers are placed at random places and cannot be easily tracked/reasoned/understood. I changed some of them to classes and those pointers to be RAIIed by the classes themselves. They works well and don't look very ugly

Is the glibcxx STL incorrect in its implementation of std::valarray::sum()?

假装没事ソ 提交于 2019-12-08 17:13:48
问题 I was toying with valarrays when I hit something I consider a bug in my compiler's STL implementation. Here is the smallest example I could produce: #include <iostream> #include <string> #include <vector> #include <iomanip> #include <valarray> using namespace std; int main() { valarray<int> Y(0xf00d, 1); valarray<valarray<int>> X(Y, 1); cout << "Y[0] = " << std::hex << Y[0] << '\n'; cout << "X[0][0] = " << std::hex << X[0][0] << '\n'; cout << "X[0].size() = " << X[0].size() << '\n'; cout <<

What's wrong with std::valarray's operator*?

落爺英雄遲暮 提交于 2019-12-05 01:10:26
Consider the following MCVE, where I have two value arrays where w is two times v ( try it out here ): #include <valarray> using namespace std; int main() { valarray<int> v { 1, 2, 3 }; for ([[maybe_unused]] auto x : v) {} // Ok auto w = v * 2; // Leads to failure in loop below //valarray<int> w = v * 2; // Works //auto w = v*=2; // Works //auto w = v; w *= 2; // Works for ([[maybe_unused]] auto x : w) {} // Failure here } This example fails to compile with clang and gcc at the last loop with (gcc output here): error: no matching function for call to 'begin(std::_Expr<std::__detail::_BinClos

valarray vs. vector: Why was valarray introduced?

孤街浪徒 提交于 2019-12-01 18:08:23
Yes, this has been asked before , and the answer has been: valarray s (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make a valarray of pointers so the compiler can make assumptions about the code and optimise it better. (The main reason that Fortran is so fast is that there is no pointer type so there can be no pointer aliasing.) or: valarray is also supposed to eliminate any possibility of aliasing [...] But these answers make no sense to me. valarray and vector are class templates , and as such, they don't even exist until instantiated. And of course,

Why is valarray so slow?

不问归期 提交于 2019-11-29 20:43:57
I am trying to use valarray since it is much like MATLAB while operating vector and matrices. I first did some performance check and found that valarray cannot achieve the performance declared as in the book C++ programming language by Stroustrup. The test program actually did 5 million multiplication of doubles. I thought that c = a*b would at least be comparable to the for loop double type element multiplication, but I am totally wrong. I tried on several computers and Microsoft Visual C++ 6.0 and Visual Studio 2008. By the way, I tested on MATLAB using the following code: len = 5*1024*1024;

Why is valarray so slow?

守給你的承諾、 提交于 2019-11-28 16:51:56
问题 I am trying to use valarray since it is much like MATLAB while operating vector and matrices. I first did some performance check and found that valarray cannot achieve the performance declared as in the book C++ programming language by Stroustrup. The test program actually did 5 million multiplication of doubles. I thought that c = a*b would at least be comparable to the for loop double type element multiplication, but I am totally wrong. I tried on several computers and Microsoft Visual C++