"Fossies" - the Fresh Open Source Software Archive

Member "armadillo-9.800.3/include/armadillo_bits/spop_normalise_meat.hpp" (16 Jun 2016, 5360 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. For more information about "spop_normalise_meat.hpp" see the Fossies "Dox" file reference documentation.

    1 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
    2 // Copyright 2008-2016 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 //! \addtogroup spop_normalise
   18 //! @{
   19 
   20 
   21 
   22 template<typename T1>
   23 inline
   24 void
   25 spop_normalise::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_normalise>& expr)
   26   {
   27   arma_extra_debug_sigprint();
   28   
   29   const uword p   = expr.aux_uword_a;
   30   const uword dim = expr.aux_uword_b;
   31   
   32   arma_debug_check( (p   == 0), "normalise(): parameter 'p' must be greater than zero" );
   33   arma_debug_check( (dim >  1), "normalise(): parameter 'dim' must be 0 or 1"          );
   34   
   35   const unwrap_spmat<T1> U(expr.m);
   36   
   37   spop_normalise::apply_direct(out, U.M, p, dim);
   38   }
   39 
   40 
   41 
   42 template<typename eT>
   43 inline
   44 void
   45 spop_normalise::apply_direct(SpMat<eT>& out, const SpMat<eT>& X, const uword p, const uword dim)
   46   {
   47   arma_extra_debug_sigprint();
   48   
   49   typedef typename get_pod_type<eT>::result T;
   50   
   51   X.sync();
   52   
   53   if( X.is_empty() || (X.n_nonzero == 0) )  { return; }
   54   
   55   if(dim == 0)
   56     {
   57     podarray<T> norm_vals(X.n_cols);
   58     
   59     T* norm_vals_mem = norm_vals.memptr();
   60     
   61     for(uword i=0; i < norm_vals.n_elem; ++i)
   62       {
   63       const uword      col_offset = X.col_ptrs[i    ];
   64       const uword next_col_offset = X.col_ptrs[i + 1];
   65       
   66       const eT* start_ptr = &X.values[     col_offset];
   67       const eT*   end_ptr = &X.values[next_col_offset];
   68       
   69       const uword n_elem = end_ptr - start_ptr;
   70       
   71       const Col<eT> fake_vec(const_cast<eT*>(start_ptr), n_elem, false, false);
   72       
   73       const T norm_val = norm(fake_vec, p);
   74       
   75       norm_vals_mem[i] = (norm_val != T(0)) ? norm_val : T(1);
   76       }
   77     
   78     const uword N = X.n_nonzero;
   79     
   80     umat    locs(2, N);
   81     Col<eT> vals(   N);
   82     
   83     uword* locs_mem = locs.memptr();
   84     eT*    vals_mem = vals.memptr();
   85     
   86     typename SpMat<eT>::const_iterator it = X.begin();
   87     
   88     uword new_n_nonzero = 0;
   89     
   90     for(uword i=0; i < N; ++i)
   91       {
   92       const uword row = it.row();
   93       const uword col = it.col();
   94         
   95       const eT val = (*it) / norm_vals_mem[col];
   96       
   97       if(val != eT(0))
   98         {
   99         (*vals_mem) = val;  vals_mem++;
  100         
  101         (*locs_mem) = row;  locs_mem++;
  102         (*locs_mem) = col;  locs_mem++;
  103         
  104         new_n_nonzero++;
  105         }
  106       
  107       ++it;
  108       }
  109     
  110     const umat    tmp_locs(locs.memptr(), 2, new_n_nonzero, false, false);
  111     const Col<eT> tmp_vals(vals.memptr(),    new_n_nonzero, false, false);
  112     
  113     SpMat<eT> tmp(tmp_locs, tmp_vals, X.n_rows, X.n_cols, false, false);
  114     
  115     out.steal_mem(tmp);
  116     }
  117   else
  118   if(dim == 1)
  119     {
  120     podarray< T> norm_vals(X.n_rows);
  121     podarray<eT>  row_vals(X.n_cols);  // worst case scenario
  122     
  123     T* norm_vals_mem = norm_vals.memptr();
  124     eT* row_vals_mem =  row_vals.memptr();
  125     
  126     for(uword i=0; i < norm_vals.n_elem; ++i)
  127       {
  128       // typename SpMat<eT>::const_row_iterator row_it     = X.begin_row(i);
  129       // typename SpMat<eT>::const_row_iterator row_it_end = X.end_row(i);
  130       // 
  131       // uword count = 0;
  132       // 
  133       // for(; row_it != row_it_end; ++row_it)
  134       //   {
  135       //   row_vals_mem[count] = (*row_it);
  136       //   ++count;
  137       //   }
  138       
  139       
  140       // using the .at() accessor, as it's faster than const_row_iterator for accessing a single row
  141       
  142       uword count = 0;
  143       
  144       for(uword col=0; col < X.n_cols; ++col)
  145         {
  146         const eT val = X.at(i,col);
  147         
  148         if(val != eT(0))
  149           {
  150           row_vals_mem[count] = val;
  151           ++count;
  152           }
  153         }
  154       
  155       const Row<eT> fake_vec(row_vals_mem, count, false, false);
  156       
  157       const T norm_val = norm(fake_vec, p);
  158       
  159       norm_vals_mem[i] = (norm_val != T(0)) ? norm_val : T(1);
  160       }
  161     
  162     const uword N = X.n_nonzero;
  163     
  164     umat    locs(2, N);
  165     Col<eT> vals(   N);
  166     
  167     uword* locs_mem = locs.memptr();
  168     eT*    vals_mem = vals.memptr();
  169     
  170     typename SpMat<eT>::const_iterator it = X.begin();
  171     
  172     uword new_n_nonzero = 0;
  173     
  174     for(uword i=0; i < N; ++i)
  175       {
  176       const uword row = it.row();
  177       const uword col = it.col();
  178         
  179       const eT val = (*it) / norm_vals_mem[row];
  180       
  181       if(val != eT(0))
  182         {
  183         (*vals_mem) = val;  vals_mem++;
  184         
  185         (*locs_mem) = row;  locs_mem++;
  186         (*locs_mem) = col;  locs_mem++;
  187         
  188         new_n_nonzero++;
  189         }
  190       
  191       ++it;
  192       }
  193     
  194     const umat    tmp_locs(locs.memptr(), 2, new_n_nonzero, false, false);
  195     const Col<eT> tmp_vals(vals.memptr(),    new_n_nonzero, false, false);
  196     
  197     SpMat<eT> tmp(tmp_locs, tmp_vals, X.n_rows, X.n_cols, false, false);
  198     
  199     out.steal_mem(tmp);
  200     }
  201   }
  202 
  203 
  204 
  205 //! @}