"Fossies" - the Fresh Open Source Software Archive

Member "armadillo-9.800.3/tests/fn_sum.cpp" (16 Jun 2016, 3962 Bytes) of package /linux/misc/armadillo-9.800.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2015 Conrad Sanderson (http://conradsanderson.id.au)
    2 // Copyright 2015 National ICT Australia (NICTA)
    3 //
    4 // Licensed under the Apache License, Version 2.0 (the "License");
    5 // you may not use this file except in compliance with the License.
    6 // You may obtain a copy of the License at
    7 // http://www.apache.org/licenses/LICENSE-2.0
    8 //
    9 // Unless required by applicable law or agreed to in writing, software
   10 // distributed under the License is distributed on an "AS IS" BASIS,
   11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12 // See the License for the specific language governing permissions and
   13 // limitations under the License.
   14 // ------------------------------------------------------------------------
   15 
   16 
   17 #include <armadillo>
   18 #include "catch.hpp"
   19 
   20 using namespace arma;
   21 
   22 
   23 TEST_CASE("fn_sum_1")
   24   {
   25   vec a = linspace<vec>(1,5,5);
   26   vec b = linspace<vec>(1,5,6);
   27 
   28   REQUIRE(sum(a) == Approx(15.0));
   29   REQUIRE(sum(b) == Approx(18.0));
   30   }
   31 
   32 
   33 
   34 TEST_CASE("sum2")
   35   {
   36   mat A =
   37     {
   38     { -0.78838,  0.69298,  0.41084,  0.90142 },
   39     {  0.49345, -0.12020,  0.78987,  0.53124 },
   40     {  0.73573,  0.52104, -0.22263,  0.40163 }
   41     };
   42 
   43   rowvec colsums = { 0.44080, 1.09382, 0.97808, 1.83429 };
   44 
   45   colvec rowsums =
   46     {
   47     1.21686,
   48     1.69436,
   49     1.43577
   50     };
   51 
   52   REQUIRE( accu(abs(colsums - sum(A  ))) == Approx(0.0) );
   53   REQUIRE( accu(abs(colsums - sum(A,0))) == Approx(0.0) );
   54   REQUIRE( accu(abs(rowsums - sum(A,1))) == Approx(0.0) );
   55   }
   56 
   57 
   58 TEST_CASE("sum3")
   59   {
   60   mat AA =
   61     {
   62     { -0.78838,  0.69298,  0.41084,  0.90142 },
   63     {  0.49345, -0.12020,  0.78987,  0.53124 },
   64     {  0.73573,  0.52104, -0.22263,  0.40163 }
   65     };
   66 
   67   cx_mat A = cx_mat(AA, 0.5*AA);
   68 
   69   rowvec re_colsums = { 0.44080, 1.09382, 0.97808, 1.83429 };
   70 
   71   cx_rowvec cx_colsums = cx_rowvec(re_colsums, 0.5*re_colsums);
   72 
   73   colvec re_rowsums =
   74     {
   75     1.21686,
   76     1.69436,
   77     1.43577
   78     };
   79 
   80   cx_colvec cx_rowsums = cx_colvec(re_rowsums, 0.5*re_rowsums);
   81 
   82   REQUIRE( accu(abs(cx_colsums - sum(A  ))) == Approx(0.0) );
   83   REQUIRE( accu(abs(cx_colsums - sum(A,0))) == Approx(0.0) );
   84   REQUIRE( accu(abs(cx_rowsums - sum(A,1))) == Approx(0.0) );
   85   }
   86 
   87 
   88 TEST_CASE("sum4")
   89   {
   90   mat X(100,101, fill::randu);
   91 
   92   REQUIRE( (sum(sum(X))/X.n_elem)                      == Approx(0.5).epsilon(0.02) );
   93   REQUIRE( (sum(sum(X(span::all,span::all)))/X.n_elem) == Approx(0.5).epsilon(0.02) );
   94   }
   95 
   96 
   97 
   98 TEST_CASE("sum_spmat")
   99   {
  100   SpCol<double> a(5);
  101   a(0) = 3.0;
  102   a(2) = 1.5;
  103   a(3) = 1.0;
  104 
  105   double res = sum(a);
  106   REQUIRE( res == Approx(5.5) );
  107 
  108   SpRow<double> b(5);
  109   b(1) = 1.3;
  110   b(2) = 4.4;
  111   b(4) = 1.0;
  112 
  113   res = sum(b);
  114   REQUIRE( res == Approx(6.7) );
  115 
  116   SpMat<double> c(8, 8);
  117   c(0, 0) = 3.0;
  118   c(1, 0) = 2.5;
  119   c(6, 0) = 2.1;
  120   c(4, 1) = 3.2;
  121   c(5, 1) = 1.1;
  122   c(1, 2) = 1.3;
  123   c(2, 3) = 4.1;
  124   c(5, 5) = 2.3;
  125   c(6, 5) = 3.1;
  126   c(7, 5) = 1.2;
  127   c(7, 7) = 3.4;
  128 
  129   SpMat<double> result = sum(c, 0);
  130 
  131   REQUIRE( result.n_rows == 1 );
  132   REQUIRE( result.n_cols == 8 );
  133   REQUIRE( result.n_nonzero == 6 );
  134   REQUIRE( (double) result(0, 0) == Approx(7.6) );
  135   REQUIRE( (double) result(0, 1) == Approx(4.3) );
  136   REQUIRE( (double) result(0, 2) == Approx(1.3) );
  137   REQUIRE( (double) result(0, 3) == Approx(4.1) );
  138   REQUIRE( (double) result(0, 4) == Approx(0.0) );
  139   REQUIRE( (double) result(0, 5) == Approx(6.6) );
  140   REQUIRE( (double) result(0, 6) == Approx(0.0) );
  141   REQUIRE( (double) result(0, 7) == Approx(3.4) );
  142 
  143   result = sum(c, 1);
  144 
  145   REQUIRE( result.n_rows == 8 );
  146   REQUIRE( result.n_cols == 1 );
  147   REQUIRE( result.n_nonzero == 7 );
  148   REQUIRE( (double) result(0, 0) == Approx(3.0) );
  149   REQUIRE( (double) result(1, 0) == Approx(3.8) );
  150   REQUIRE( (double) result(2, 0) == Approx(4.1) );
  151   REQUIRE( (double) result(3, 0) == Approx(0.0) );
  152   REQUIRE( (double) result(4, 0) == Approx(3.2) );
  153   REQUIRE( (double) result(5, 0) == Approx(3.4) );
  154   REQUIRE( (double) result(6, 0) == Approx(5.2) );
  155   REQUIRE( (double) result(7, 0) == Approx(4.6) );
  156   }