123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- // Copyright 2011-2017 Ryan Curtin (http://www.ratml.org/)
- // Copyright 2017 National ICT Australia (NICTA)
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ------------------------------------------------------------------------
- #include <armadillo>
- #include "catch.hpp"
- using namespace arma;
- TEST_CASE("fn_var_empty_sparse_test")
- {
- SpMat<double> m(100, 100);
- SpRow<double> result = var(m);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- result = var(m, 0, 0);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- result = var(m, 1, 0);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- result = var(m, 1);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- SpCol<double> colres = var(m, 1, 1);
- REQUIRE( colres.n_cols == 1 );
- REQUIRE( colres.n_rows == 100 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) colres[i] == Approx(0.0) );
- }
- colres = var(m, 0, 1);
- REQUIRE( colres.n_cols == 1 );
- REQUIRE( colres.n_rows == 100 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) colres[i] == Approx(0.0) );
- }
- }
- TEST_CASE("fn_var_empty_cx_sparse_test")
- {
- SpMat<std::complex<double> > m(100, 100);
- SpRow<double> result = var(m);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- result = var(m, 0, 0);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- result = var(m, 1, 0);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- result = var(m, 1);
- REQUIRE( result.n_cols == 100 );
- REQUIRE( result.n_rows == 1 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) result[i] == Approx(0.0) );
- }
- SpCol<double> colres = var(m, 1, 1);
- REQUIRE( colres.n_cols == 1 );
- REQUIRE( colres.n_rows == 100 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) colres[i] == Approx(0.0) );
- }
- colres = var(m, 0, 1);
- REQUIRE( colres.n_cols == 1 );
- REQUIRE( colres.n_rows == 100 );
- for (uword i = 0; i < 100; ++i)
- {
- REQUIRE( (double) colres[i] == Approx(0.0) );
- }
- }
- TEST_CASE("fn_var_sparse_test")
- {
- // Create a random matrix and do variance testing on it, with varying levels
- // of nonzero (eventually this becomes a fully dense matrix).
- for (int i = 0; i < 10; ++i)
- {
- SpMat<double> x;
- x.sprandu(50, 75, ((double) (i + 1)) / 10);
- mat d(x);
- SpRow<double> rr = var(x);
- rowvec drr = var(d);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x, 1, 0);
- drr = var(d, 1, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- SpCol<double> cr = var(x, 0, 1);
- vec dcr = var(d, 0, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(x, 1, 1);
- dcr = var(d, 1, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- // Now on a subview.
- rr = var(x.submat(11, 11, 30, 45), 0, 0);
- drr = var(d.submat(11, 11, 30, 45), 0, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 35 );
- for (uword j = 0; j < 35; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x.submat(11, 11, 30, 45), 1, 0);
- drr = var(d.submat(11, 11, 30, 45), 1, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 35 );
- for (uword j = 0; j < 35; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- cr = var(x.submat(11, 11, 30, 45), 0, 1);
- dcr = var(d.submat(11, 11, 30, 45), 0, 1);
- REQUIRE( cr.n_rows == 20 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 20; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(x.submat(11, 11, 30, 45), 1, 1);
- dcr = var(d.submat(11, 11, 30, 45), 1, 1);
- REQUIRE( cr.n_rows == 20 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 20; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- // Now on an SpOp (spop_scalar_times)
- rr = var(3.0 * x, 0, 0);
- drr = var(3.0 * d, 0, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(3.0 * x, 1, 0);
- drr = var(3.0 * d, 1, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- cr = var(4.5 * x, 0, 1);
- dcr = var(4.5 * d, 0, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(4.5 * x, 1, 1);
- dcr = var(4.5 * d, 1, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- // Now on an SpGlue!
- SpMat<double> y;
- y.sprandu(50, 75, 0.3);
- mat e(y);
- rr = var(x + y);
- drr = var(d + e);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x + y, 1);
- drr = var(d + e, 1);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- cr = var(x + y, 0, 1);
- dcr = var(d + e, 0, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(x + y, 1, 1);
- dcr = var(d + e, 1, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- }
- }
- TEST_CASE("fn_var_sparse_cx_test")
- {
- // Create a random matrix and do variance testing on it, with varying levels
- // of nonzero (eventually this becomes a fully dense matrix).
- for (int i = 0; i < 10; ++i)
- {
- SpMat<std::complex<double> > x;
- x.sprandu(50, 75, ((double) (i + 1)) / 10);
- cx_mat d(x);
- SpRow<double> rr = var(x);
- rowvec drr = var(d);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x, 1, 0);
- drr = var(d, 1, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- SpCol<double> cr = var(x, 0, 1);
- vec dcr = var(d, 0, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(x, 1, 1);
- dcr = var(d, 1, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- // Now on a subview.
- rr = var(x.submat(11, 11, 30, 45), 0, 0);
- drr = var(d.submat(11, 11, 30, 45), 0, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 35 );
- for (uword j = 0; j < 35; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x.submat(11, 11, 30, 45), 1, 0);
- drr = var(d.submat(11, 11, 30, 45), 1, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 35 );
- for (uword j = 0; j < 35; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- cr = var(x.submat(11, 11, 30, 45), 0, 1);
- dcr = var(d.submat(11, 11, 30, 45), 0, 1);
- REQUIRE( cr.n_rows == 20 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 20; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(x.submat(11, 11, 30, 45), 1, 1);
- dcr = var(d.submat(11, 11, 30, 45), 1, 1);
- REQUIRE( cr.n_rows == 20 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 20; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- // Now on an SpOp (spop_scalar_times)
- rr = var(3.0 * x, 0, 0);
- drr = var(3.0 * d, 0, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(3.0 * x, 1, 0);
- drr = var(3.0 * d, 1, 0);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- cr = var(4.5 * x, 0, 1);
- dcr = var(4.5 * d, 0, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(4.5 * x, 1, 1);
- dcr = var(4.5 * d, 1, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- // Now on an SpGlue!
- SpMat<std::complex<double> > y;
- y.sprandu(50, 75, 0.3);
- cx_mat e(y);
- rr = var(x + y);
- drr = var(d + e);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- rr = var(x + y, 1);
- drr = var(d + e, 1);
- REQUIRE( rr.n_rows == 1 );
- REQUIRE( rr.n_cols == 75 );
- for (uword j = 0; j < 75; ++j)
- {
- REQUIRE( drr[j] == Approx((double) rr[j]) );
- }
- cr = var(x + y, 0, 1);
- dcr = var(d + e, 0, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- cr = var(x + y, 1, 1);
- dcr = var(d + e, 1, 1);
- REQUIRE( cr.n_rows == 50 );
- REQUIRE( cr.n_cols == 1 );
- for (uword j = 0; j < 50; ++j)
- {
- REQUIRE( dcr[j] == Approx((double) cr[j]) );
- }
- }
- }
- TEST_CASE("fn_var_sparse_alias_test")
- {
- sp_mat s;
- s.sprandu(70, 70, 0.3);
- mat d(s);
- s = var(s);
- d = var(d);
- REQUIRE( d.n_rows == s.n_rows );
- REQUIRE( d.n_cols == s.n_cols );
- for (uword i = 0; i < d.n_elem; ++i)
- {
- REQUIRE(d[i] == Approx((double) s[i]) );
- }
- s.sprandu(70, 70, 0.3);
- d = s;
- s = var(s, 1);
- d = var(d, 1);
- for (uword i = 0; i < d.n_elem; ++i)
- {
- REQUIRE( d[i] == Approx((double) s[i]) );
- }
- }
|