C++/Valarray/valarray

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Create valarray of bool value by using comparison operator on another valarray

 
 
#include <iostream>
#include <valarray>
#include <cmath>
using namespace std;
int main()
{
  valarray<int> v(10);
  for(int i=0; i<10; i++)
      v[i] = i;
  cout << "Original contents: ";
  for(int i=0; i<10; i++)
    cout << v[i] << " ";
  valarray<bool> vb = v < 5;
  cout << "Those elements less than 5: ";
  for(int i=0; i<10; i++)
    cout << vb[i] << " ";
  cout << endl;
}
/* 
Original contents: 0 1 2 3 4 5 6 7 8 9 Those elements less than 5: 1 1 1 1 1 0 0 0 0 0
 */


Do sqrt for all elements in a valarray

 
 
#include <iostream>
#include <valarray>
#include <cmath>
using namespace std;
int main()
{
  valarray<int> v(10);
  for(int i=0; i<10; i++)
      v[i] = i*i;
  cout << "Original contents: ";
  for(int i=0; i<10; i++)
    cout << v[i] << " \n\n";
  v = sqrt(v);
  for(int i=0; i<10; i++)
    cout << v[i] << " \n\n";

  cout << endl;
}
/* 
Original contents: 0
1
4
9
16
25
36
49
64
81
0
1
2
3
4
5
6
7
8
9

 */


Minus 10 from each element in a valarray

 
 

#include <iostream>
#include <valarray>
#include <cmath>
using namespace std;
int main()
{
  valarray<int> v(10);
  for(int i=0; i<10; i++)
      v[i] = i;
  cout << "Original contents: ";
  for(int i=0; i<10; i++)
    cout << v[i] << " \n\n";
  v = v - 10;
  for(int i=0; i<10; i++)
    cout << v[i] << " \n\n";

  cout << endl;
}
/* 
Original contents: 0
1
2
3
4
5
6
7
8
9
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1

 */


Print three-dimensional valarray line-by-line

 
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <valarray>
using namespace std;
// print three-dimensional valarray line-by-line
template<class T>
void printValarray3D (const valarray<T>& va, int dim1, int dim2)
{
    for (int i=0; i<va.size()/(dim1*dim2); ++i) {
        for (int j=0; j<dim2; ++j) {
            for (int k=0; k<dim1; ++k) {
                cout << va[i*dim1*dim2+j*dim1+k] << " ";
            }
            cout << "\n";
        }
        cout << "\n";
    }
    cout << endl;
}
int main()
{
    /* valarray with 24 elements
     * - two groups
     * - four rows
     * - three columns
     */
    valarray<double> va(24);
    // fill valarray with values
    for (int i=0; i<24; i++) {
        va[i] = i;
    }
    // print valarray
    printValarray3D (va, 3, 4);
    // we need two two-dimensional subsets of three times 3 values
    // in two 12-element arrays
    size_t lengthvalues[] = {  2, 3 };
    size_t stridevalues[] = { 12, 3 };
    valarray<size_t> length(lengthvalues,2);
    valarray<size_t> stride(stridevalues,2);
    // assign the second column of the first three rows
    // to the first column of the first three rows
    va[gslice(0,length,stride)]
        = valarray<double>(va[gslice(1,length,stride)]);
    // add and assign the third of the first three rows
    // to the first of the first three rows
    va[gslice(0,length,stride)]
        += valarray<double>(va[gslice(2,length,stride)]);
    
    // print valarray
    printValarray3D (va, 3, 4);
}
/* 
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 20
21 22 23

3 1 2
9 4 5
15 7 8
9 10 11
27 13 14
33 16 17
39 19 20
21 22 23

 */


Print valarray as two-dimensional array

 
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <valarray>
using namespace std;
// print valarray as two-dimensional array
template<class T>
void printValarray (const valarray<T>& va, int num)
{
    for (int i=0; i<va.size()/num; i++) {
        for (int j=0; j<num; j++) {
            cout << va[i*num+j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
int main()
{
    // create valarray for 12 elements
    valarray<double> va(12);
    // initialize valarray by values 1.01, 2.02, ... 12.12
    for (int i=0; i<12; i++) {
        va[i] = (i+1) * 1.01;
    }
    printValarray(va,4);
    /* create array of indexes
     * - note: element type has to be size_t
     */
    valarray<size_t> idx(4);
    idx[0] = 8;
    idx[1] = 0;
    idx[2] = 3;
    idx[3] = 7;
    // use array of indexes to print the ninth, first, fourth, and eighth elements
    printValarray(valarray<double>(va[idx]), 4);
    // change the first and fourth elements and print them again indirectly
    va[0] = 11.11;
    va[3] = 44.44;
    printValarray(valarray<double>(va[idx]), 4);
    // now select the second, third, sixth, and ninth elements
    // and assign 99 to them
    idx[0] = 1;
    idx[1] = 2;
    idx[2] = 5;
    idx[3] = 8;
    va[idx] = 99;
    // print the whole valarray again
    printValarray (va, 4);
}
/* 
1.01 2.02 3.03 4.04
5.05 6.06 7.07 8.08
9.09 10.1 11.11 12.12
9.09 1.01 4.04 8.08
9.09 11.11 44.44 8.08
11.11 99 99 44.44
5.05 99 7.07 8.08
99 10.1 11.11 12.12

 */


Use valarray + valarray to double the value for each element in valarray

 
 

#include <iostream>
#include <valarray>
#include <cmath>
using namespace std;
int main()
{
  valarray<int> v(10);
  for(int i=0; i<10; i++)
      v[i] = i;
  cout << "Original contents: ";
  for(int i=0; i<10; i++)
    cout << v[i] << " \n\n";
  v = v + v;
  for(int i=0; i<10; i++)
    cout << v[i] << " \n\n";

  cout << endl;
}
/* 
Original contents: 0
1
2
3
4
5
6
7
8
9
0
2
4
6
8
10
12
14
16
18

 */


valarray with double value inside

 
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <valarray>
using namespace std;
// print valarray line-by-line
template<class T>
void printValarray (const valarray<T>& va, int num)
{
    for (int i=0; i<va.size()/num; ++i) {
        for (int j=0; j<num; ++j) {
            cout << va[i*num+j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
int main()
{
    /* valarray with 12 elements
     * - four rows
     * - three columns
     */
    valarray<double> va(12);
    // fill valarray with values
    for (int i=0; i<12; i++) {
        va[i] = i;
    }
    printValarray (va, 3);
    // assign 77 to all values that are less than 5
    va[va<5.0] = 77.0;
    // add 100 to all values that are greater than 5 and less than 9
    va[va>5.0 && va<9.0] = valarray<double>(va[va>5.0 && va<9.0]) + 100.0;
    printValarray (va, 3);
}
/* 
0 1 2
3 4 5
6 7 8
9 10 11
77 77 77
77 77 5
106 107 108
9 10 11

 */