"Fossies" - the Fresh Open Source Software Archive

Member "armadillo-9.800.3/tests/fn_min.cpp" (16 Jun 2016, 11884 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 2011-2017 Ryan Curtin (http://www.ratml.org/)
    2 // Copyright 2017 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 #include <armadillo>
   17 
   18 #include "catch.hpp"
   19 
   20 using namespace arma;
   21 
   22 TEST_CASE("fn_min_weird_operation")
   23   {
   24   mat a(10, 10);
   25   mat b(25, 10);
   26   a.randn();
   27   b.randn();
   28 
   29   mat output = a * b.t();
   30 
   31   uword real_min;
   32   uword operation_min;
   33 
   34   const double mval = output.min(real_min);
   35   const double other_mval = (a * b.t()).min(operation_min);
   36 
   37   REQUIRE( real_min == operation_min );
   38   REQUIRE( mval == Approx(other_mval) );
   39   }
   40 
   41 
   42 
   43 TEST_CASE("fn_min_weird_sparse_operation")
   44   {
   45   sp_mat a(10, 10);
   46   sp_mat b(25, 10);
   47   a.sprandn(10, 10, 0.3);
   48   b.sprandn(25, 10, 0.3);
   49 
   50   sp_mat output = a * b.t();
   51 
   52   uword real_min;
   53   uword operation_min;
   54 
   55   const double mval = output.min(real_min);
   56   const double other_mval = (a * b.t()).min(operation_min);
   57 
   58   REQUIRE( real_min == operation_min );
   59   REQUIRE( mval == Approx(other_mval) );
   60   }
   61 
   62 
   63 
   64 TEST_CASE("fn_min_sp_subview_test")
   65   {
   66   // We will assume subview.at() works and returns points within the bounds of
   67   // the matrix, so we just have to ensure the results are the same as
   68   // Mat.min()...
   69   for (size_t r = 50; r < 150; ++r)
   70     {
   71     sp_mat x;
   72     x.sprandn(r, r, 0.3);
   73 
   74     uword x_min;
   75     uword x_subview_min1;
   76     uword x_subview_min2;
   77     uword x_subview_min3;
   78 
   79     const double mval = x.min(x_min);
   80     const double mval1 = x.submat(0, 0, r - 1, r - 1).min(x_subview_min1);
   81     const double mval2 = x.cols(0, r - 1).min(x_subview_min2);
   82     const double mval3 = x.rows(0, r - 1).min(x_subview_min3);
   83 
   84     if (mval != 0.0)
   85       {
   86       REQUIRE( x_min == x_subview_min1 );
   87       REQUIRE( x_min == x_subview_min2 );
   88       REQUIRE( x_min == x_subview_min3 );
   89 
   90       REQUIRE( mval == Approx(mval1) );
   91       REQUIRE( mval == Approx(mval2) );
   92       REQUIRE( mval == Approx(mval3) );
   93       }
   94     }
   95   }
   96 
   97 
   98 
   99 TEST_CASE("fn_min_spsubview_col_test")
  100   {
  101   for (size_t r = 10; r < 50; ++r)
  102     {
  103     sp_vec x;
  104     x.sprandn(r, 1, 0.3);
  105 
  106     uword x_min;
  107     uword x_subview_min1;
  108     uword x_subview_min2;
  109 
  110     const double mval = x.min(x_min);
  111     const double mval1 = x.submat(0, 0, r - 1, 0).min(x_subview_min1);
  112     const double mval2 = x.rows(0, r - 1).min(x_subview_min2);
  113 
  114     if (mval != 0.0)
  115       {
  116       REQUIRE( x_min == x_subview_min1 );
  117       REQUIRE( x_min == x_subview_min2 );
  118 
  119       REQUIRE( mval == Approx(mval1) );
  120       REQUIRE( mval == Approx(mval2) );
  121       }
  122     }
  123   }
  124 
  125 
  126 
  127 TEST_CASE("fn_min_spsubview_row_min_test")
  128   {
  129   for (size_t r = 10; r < 50; ++r)
  130     {
  131     sp_rowvec x;
  132     x.sprandn(1, r, 0.3);
  133 
  134     uword x_min;
  135     uword x_subview_min1;
  136     uword x_subview_min2;
  137 
  138     const double mval = x.min(x_min);
  139     const double mval1 = x.submat(0, 0, 0, r - 1).min(x_subview_min1);
  140     const double mval2 = x.cols(0, r - 1).min(x_subview_min2);
  141 
  142     if (mval != 0.0)
  143       {
  144       REQUIRE( x_min == x_subview_min1 );
  145       REQUIRE( x_min == x_subview_min2 );
  146 
  147       REQUIRE( mval == Approx(mval1) );
  148       REQUIRE( mval == Approx(mval2) );
  149       }
  150     }
  151   }
  152 
  153 
  154 
  155 TEST_CASE("fn_min_spincompletesubview_min_test")
  156   {
  157   for (size_t r = 50; r < 150; ++r)
  158     {
  159     sp_mat x;
  160     x.sprandn(r, r, 0.3);
  161 
  162     uword x_min;
  163     uword x_subview_min1;
  164     uword x_subview_min2;
  165     uword x_subview_min3;
  166 
  167     const double mval = x.min(x_min);
  168     const double mval1 = x.submat(1, 1, r - 2, r - 2).min(x_subview_min1);
  169     const double mval2 = x.cols(1, r - 2).min(x_subview_min2);
  170     const double mval3 = x.rows(1, r - 2).min(x_subview_min3);
  171 
  172     uword row, col;
  173     x.min(row, col);
  174 
  175     if (row != 0 && row != r - 1 && col != 0 && col != r - 1 && mval != 0.0)
  176       {
  177       uword srow, scol;
  178 
  179       srow = x_subview_min1 % (r - 2);
  180       scol = x_subview_min1 / (r - 2);
  181       REQUIRE( x_min == (srow + 1) + r * (scol + 1) );
  182       REQUIRE( x_min == x_subview_min2 + r );
  183 
  184       srow = x_subview_min3 % (r - 2);
  185       scol = x_subview_min3 / (r - 2);
  186       REQUIRE( x_min == (srow + 1) + r * scol );
  187 
  188       REQUIRE( mval == Approx(mval1) );
  189       REQUIRE( mval == Approx(mval2) );
  190       REQUIRE( mval == Approx(mval3) );
  191       }
  192     }
  193   }
  194 
  195 
  196 
  197 TEST_CASE("fn_min_spincompletesubview_col_min_test")
  198   {
  199   for (size_t r = 10; r < 50; ++r)
  200     {
  201     sp_vec x;
  202     x.sprandu(r, 1, 0.3);
  203 
  204     uword x_min;
  205     uword x_subview_min1;
  206     uword x_subview_min2;
  207 
  208     const double mval = x.min(x_min);
  209     const double mval1 = x.submat(1, 0, r - 2, 0).min(x_subview_min1);
  210     const double mval2 = x.rows(1, r - 2).min(x_subview_min2);
  211 
  212     if (x_min != 0 && x_min != r - 1 && mval != 0.0)
  213       {
  214       REQUIRE( x_min == x_subview_min1 + 1 );
  215       REQUIRE( x_min == x_subview_min2 + 1 );
  216 
  217       REQUIRE( mval == Approx(mval1) );
  218       REQUIRE( mval == Approx(mval2) );
  219       }
  220     }
  221   }
  222 
  223 
  224 
  225 TEST_CASE("fn_min_spincompletesubview_row_min_test")
  226   {
  227   for (size_t r = 10; r < 50; ++r)
  228     {
  229     sp_rowvec x;
  230     x.sprandn(1, r, 0.3);
  231 
  232     uword x_min;
  233     uword x_subview_min1;
  234     uword x_subview_min2;
  235 
  236     const double mval = x.min(x_min);
  237     const double mval1 = x.submat(0, 1, 0, r - 2).min(x_subview_min1);
  238     const double mval2 = x.cols(1, r - 2).min(x_subview_min2);
  239 
  240     if (mval != 0.0 && x_min != 0 && x_min != r - 1)
  241       {
  242       REQUIRE( x_min == x_subview_min1 + 1 );
  243       REQUIRE( x_min == x_subview_min2 + 1 );
  244 
  245       REQUIRE( mval == Approx(mval1) );
  246       REQUIRE( mval == Approx(mval2) );
  247       }
  248     }
  249   }
  250 
  251 
  252 
  253 TEST_CASE("fn_min_sp_cx_subview_min_test")
  254   {
  255   // We will assume subview.at() works and returns points within the bounds of
  256   // the matrix, so we just have to ensure the results are the same as
  257   // Mat.min()...
  258   for (size_t r = 50; r < 150; ++r)
  259     {
  260     sp_cx_mat x;
  261     x.sprandn(r, r, 0.3);
  262 
  263     uword x_min;
  264     uword x_subview_min1;
  265     uword x_subview_min2;
  266     uword x_subview_min3;
  267 
  268     const std::complex<double> mval = x.min(x_min);
  269     const std::complex<double> mval1 = x.submat(0, 0, r - 1, r - 1).min(x_subview_min1);
  270     const std::complex<double> mval2 = x.cols(0, r - 1).min(x_subview_min2);
  271     const std::complex<double> mval3 = x.rows(0, r - 1).min(x_subview_min3);
  272 
  273     if (mval != std::complex<double>(0.0))
  274       {
  275       REQUIRE( x_min == x_subview_min1 );
  276       REQUIRE( x_min == x_subview_min2 );
  277       REQUIRE( x_min == x_subview_min3 );
  278 
  279       REQUIRE( mval.real() == Approx(mval1.real()) );
  280       REQUIRE( mval.imag() == Approx(mval1.imag()) );
  281       REQUIRE( mval.real() == Approx(mval2.real()) );
  282       REQUIRE( mval.imag() == Approx(mval2.imag()) );
  283       REQUIRE( mval.real() == Approx(mval3.real()) );
  284       REQUIRE( mval.imag() == Approx(mval3.imag()) );
  285       }
  286     }
  287   }
  288 
  289 
  290 
  291 TEST_CASE("fn_min_sp_cx_subview_col_min_test")
  292   {
  293   for (size_t r = 10; r < 50; ++r)
  294     {
  295     sp_cx_vec x;
  296     x.sprandn(r, 1, 0.3);
  297 
  298     uword x_min;
  299     uword x_subview_min1;
  300     uword x_subview_min2;
  301 
  302     const std::complex<double> mval = x.min(x_min);
  303     const std::complex<double> mval1 = x.submat(0, 0, r - 1, 0).min(x_subview_min1);
  304     const std::complex<double> mval2 = x.rows(0, r - 1).min(x_subview_min2);
  305 
  306     if (mval != std::complex<double>(0.0))
  307       {
  308       REQUIRE( x_min == x_subview_min1 );
  309       REQUIRE( x_min == x_subview_min2 );
  310 
  311       REQUIRE( mval.real() == Approx(mval1.real()) );
  312       REQUIRE( mval.imag() == Approx(mval1.imag()) );
  313       REQUIRE( mval.real() == Approx(mval2.real()) );
  314       REQUIRE( mval.imag() == Approx(mval2.imag()) );
  315       }
  316     }
  317   }
  318 
  319 
  320 
  321 TEST_CASE("fn_min_sp_cx_subview_row_min_test")
  322   {
  323   for (size_t r = 10; r < 50; ++r)
  324     {
  325     sp_cx_rowvec x;
  326     x.sprandn(1, r, 0.3);
  327 
  328     uword x_min;
  329     uword x_subview_min1;
  330     uword x_subview_min2;
  331 
  332     const std::complex<double> mval = x.min(x_min);
  333     const std::complex<double> mval1 = x.submat(0, 0, 0, r - 1).min(x_subview_min1);
  334     const std::complex<double> mval2 = x.cols(0, r - 1).min(x_subview_min2);
  335 
  336     if (mval != std::complex<double>(0.0))
  337       {
  338       REQUIRE( x_min == x_subview_min1 );
  339       REQUIRE( x_min == x_subview_min2 );
  340 
  341       REQUIRE( mval.real() == Approx(mval1.real()) );
  342       REQUIRE( mval.imag() == Approx(mval1.imag()) );
  343       REQUIRE( mval.real() == Approx(mval2.real()) );
  344       REQUIRE( mval.imag() == Approx(mval2.imag()) );
  345       }
  346     }
  347   }
  348 
  349 
  350 
  351 TEST_CASE("fn_min_sp_cx_incomplete_subview_min_test")
  352   {
  353   for (size_t r = 50; r < 150; ++r)
  354     {
  355     sp_cx_mat x;
  356     x.sprandn(r, r, 0.3);
  357 
  358     uword x_min;
  359     uword x_subview_min1;
  360     uword x_subview_min2;
  361     uword x_subview_min3;
  362 
  363     const std::complex<double> mval = x.min(x_min);
  364     const std::complex<double> mval1 = x.submat(1, 1, r - 2, r - 2).min(x_subview_min1);
  365     const std::complex<double> mval2 = x.cols(1, r - 2).min(x_subview_min2);
  366     const std::complex<double> mval3 = x.rows(1, r - 2).min(x_subview_min3);
  367 
  368     uword row, col;
  369     x.min(row, col);
  370 
  371     if (row != 0 && row != r - 1 && col != 0 && col != r - 1 && mval != std::complex<double>(0.0))
  372       {
  373       uword srow, scol;
  374 
  375       srow = x_subview_min1 % (r - 2);
  376       scol = x_subview_min1 / (r - 2);
  377       REQUIRE( x_min == (srow + 1) + r * (scol + 1) );
  378       REQUIRE( x_min == x_subview_min2 + r );
  379 
  380       srow = x_subview_min3 % (r - 2);
  381       scol = x_subview_min3 / (r - 2);
  382       REQUIRE( x_min == (srow + 1) + r * scol );
  383 
  384       REQUIRE( mval.real() == Approx(mval1.real()) );
  385       REQUIRE( mval.imag() == Approx(mval1.imag()) );
  386       REQUIRE( mval.real() == Approx(mval2.real()) );
  387       REQUIRE( mval.imag() == Approx(mval2.imag()) );
  388       REQUIRE( mval.real() == Approx(mval3.real()) );
  389       REQUIRE( mval.imag() == Approx(mval3.imag()) );
  390       }
  391     }
  392   }
  393 
  394 
  395 
  396 TEST_CASE("fn_min_sp_cx_incomplete_subview_col_min_test")
  397   {
  398   for (size_t r = 10; r < 50; ++r)
  399     {
  400     arma::sp_cx_vec x;
  401     x.sprandn(r, 1, 0.3);
  402 
  403     uword x_min;
  404     uword x_subview_min1;
  405     uword x_subview_min2;
  406 
  407     const std::complex<double> mval = x.min(x_min);
  408     const std::complex<double> mval1 = x.submat(1, 0, r - 2, 0).min(x_subview_min1);
  409     const std::complex<double> mval2 = x.rows(1, r - 2).min(x_subview_min2);
  410 
  411     if (x_min != 0 && x_min != r - 1 && mval != std::complex<double>(0.0))
  412       {
  413       REQUIRE( x_min == x_subview_min1 + 1 );
  414       REQUIRE( x_min == x_subview_min2 + 1 );
  415 
  416       REQUIRE( mval.real() == Approx(mval1.real()) );
  417       REQUIRE( mval.imag() == Approx(mval1.imag()) );
  418       REQUIRE( mval.real() == Approx(mval2.real()) );
  419       REQUIRE( mval.imag() == Approx(mval2.imag()) );
  420       }
  421     }
  422   }
  423 
  424 
  425 
  426 TEST_CASE("fn_min_sp_cx_incomplete_subview_row_min_test")
  427   {
  428   for (size_t r = 10; r < 50; ++r)
  429     {
  430     sp_cx_rowvec x;
  431     x.sprandn(1, r, 0.3);
  432 
  433     uword x_min;
  434     uword x_subview_min1;
  435     uword x_subview_min2;
  436 
  437     const std::complex<double> mval = x.min(x_min);
  438     const std::complex<double> mval1 = x.submat(0, 1, 0, r - 2).min(x_subview_min1);
  439     const std::complex<double> mval2 = x.cols(1, r - 2).min(x_subview_min2);
  440 
  441     if (x_min != 0 && x_min != r - 1 && mval != std::complex<double>(0.0))
  442       {
  443       REQUIRE( x_min == x_subview_min1 + 1 );
  444       REQUIRE( x_min == x_subview_min2 + 1 );
  445 
  446       REQUIRE( mval.real() == Approx(mval1.real()) );
  447       REQUIRE( mval.imag() == Approx(mval1.imag()) );
  448       REQUIRE( mval.real() == Approx(mval2.real()) );
  449       REQUIRE( mval.imag() == Approx(mval2.imag()) );
  450       }
  451     }
  452   }