Beispiel #1
0
arma_hot
inline
typename arma_not_cx<typename T1::elem_type>::result
op_dot::apply_proxy(const Proxy<T1>& PA, const Proxy<T2>& PB)
{
    arma_extra_debug_sigprint();

    typedef typename T1::elem_type      eT;
    typedef typename Proxy<T1>::ea_type ea_type1;
    typedef typename Proxy<T2>::ea_type ea_type2;

    const uword N = PA.get_n_elem();

    ea_type1 A = PA.get_ea();
    ea_type2 B = PB.get_ea();

    eT val1 = eT(0);
    eT val2 = eT(0);

    uword i,j;

    for(i=0, j=1; j<N; i+=2, j+=2)
    {
        val1 += A[i] * B[i];
        val2 += A[j] * B[j];
    }

    if(i < N)
    {
        val1 += A[i] * B[i];
    }

    return val1 + val2;
}
inline
void
glue_cross::apply(Mat<typename T1::elem_type>& out, const Glue<T1, T2, glue_cross>& X)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type1;
  typedef typename Proxy<T2>::ea_type ea_type2;
  
  const Proxy<T1> A(X.A);
  const Proxy<T2> B(X.B);
  
  arma_debug_check( ((A.get_n_elem() != 3) || (B.get_n_elem() != 3)), "cross(): input vectors must have 3 elements" );
  
  out.set_size(A.get_n_rows(), A.get_n_cols());
  
  eT*      out_mem = out.memptr();
  ea_type1 PA      = A.get_ea();
  ea_type2 PB      = B.get_ea();
  
  const eT ax = PA[0];
  const eT ay = PA[1];
  const eT az = PA[2];
  
  const eT bx = PB[0];
  const eT by = PB[1];
  const eT bz = PB[2];
  
  out_mem[0] = ay*bz - az*by;
  out_mem[1] = az*bx - ax*bz;
  out_mem[2] = ax*by - ay*bx;
  }
inline
uword
op_find::helper
  (
  Mat<uword>& indices,
  const mtGlue<uword, T1, T2, glue_type>& X,
  const typename arma_glue_rel_only<glue_type>::result       junk1,
  const typename arma_not_cx<typename T1::elem_type>::result junk2,
  const typename arma_not_cx<typename T2::elem_type>::result junk3
  )
  {
  arma_extra_debug_sigprint();
  arma_ignore(junk1);
  arma_ignore(junk2);
  arma_ignore(junk3);
  
  typedef typename T1::elem_type eT1;
  typedef typename T2::elem_type eT2;
  
  typedef typename Proxy<T1>::ea_type ea_type1;
  typedef typename Proxy<T2>::ea_type ea_type2;
  
  const Proxy<T1> A(X.A);
  const Proxy<T2> B(X.B);
  
  arma_debug_assert_same_size(A, B, "relational operator");
  
  ea_type1 PA = A.get_ea();
  ea_type2 PB = B.get_ea();
  
  const uword n_elem = B.get_n_elem();
  
  indices.set_size(n_elem, 1);
  
  uword* indices_mem = indices.memptr();
  uword  n_nz        = 0;
  
  for(uword i=0; i<n_elem; ++i)
    {
    const eT1 tmp1 = PA[i];
    const eT2 tmp2 = PB[i];
    
    bool not_zero;
    
         if(is_same_type<glue_type, glue_rel_lt    >::value == true)  { not_zero = (tmp1 <  tmp2); }
    else if(is_same_type<glue_type, glue_rel_gt    >::value == true)  { not_zero = (tmp1 >  tmp2); }
    else if(is_same_type<glue_type, glue_rel_lteq  >::value == true)  { not_zero = (tmp1 <= tmp2); }
    else if(is_same_type<glue_type, glue_rel_gteq  >::value == true)  { not_zero = (tmp1 >= tmp2); }
    else if(is_same_type<glue_type, glue_rel_eq    >::value == true)  { not_zero = (tmp1 == tmp2); }
    else if(is_same_type<glue_type, glue_rel_noteq >::value == true)  { not_zero = (tmp1 != tmp2); }
    else not_zero = false;
    
    if(not_zero == true)  { indices_mem[n_nz] = i;  ++n_nz; }
    }
  
  return n_nz;
  }
inline
void
glue_mixed_plus::apply(Mat<typename eT_promoter<T1,T2>::eT>& out, const mtGlue<typename eT_promoter<T1,T2>::eT, T1, T2, glue_mixed_plus>& X)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type eT1;
  typedef typename T2::elem_type eT2;
  
  typedef typename promote_type<eT1,eT2>::result out_eT;
  
  promote_type<eT1,eT2>::check();
  
  const Proxy<T1> A(X.A);
  const Proxy<T2> B(X.B);
  
  arma_debug_assert_same_size(A, B, "addition");
  
  const uword n_rows = A.get_n_rows();
  const uword n_cols = A.get_n_cols();
  
  out.set_size(n_rows, n_cols);
  
        out_eT* out_mem = out.memptr();
  const uword   n_elem  = out.n_elem;
    
  const bool prefer_at_accessor = (Proxy<T1>::prefer_at_accessor || Proxy<T2>::prefer_at_accessor);
  
  if(prefer_at_accessor == false)
    {
    typename Proxy<T1>::ea_type AA = A.get_ea();
    typename Proxy<T2>::ea_type BB = B.get_ea();
    
    for(uword i=0; i<n_elem; ++i)
      {
      out_mem[i] = upgrade_val<eT1,eT2>::apply(AA[i]) + upgrade_val<eT1,eT2>::apply(BB[i]);
      }
    }
  else
    {
    uword i = 0;
    
    for(uword col=0; col < n_cols; ++col)
    for(uword row=0; row < n_rows; ++row)
      {
      out_mem[i] = upgrade_val<eT1,eT2>::apply(A.at(row,col)) + upgrade_val<eT1,eT2>::apply(B.at(row,col));
      ++i;
      }
    }
  }
Beispiel #5
0
arma_hot
inline
typename T1::pod_type
arma_vec_norm_min(const Proxy<T1>& A)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::pod_type       T;
  typedef typename Proxy<T1>::ea_type ea_type;
  
        ea_type P = A.get_ea();
  const uword     N = A.get_n_elem();
  
  T min_val = (N != 1) ? priv::most_pos<T>() : std::abs(P[0]);
  
  uword i,j;
  
  for(i=0, j=1; j<N; i+=2, j+=2)
    {
    const T tmp_i = std::abs(P[i]);
    const T tmp_j = std::abs(P[j]);
    
    if(min_val > tmp_i) { min_val = tmp_i; }
    if(min_val > tmp_j) { min_val = tmp_j; }
    }
  
  if(i < N)
    {
    const T tmp_i = std::abs(P[i]);
    
    if(min_val > tmp_i) { min_val = tmp_i; }
    }
  
  return min_val;
  }
arma_hot
inline
void
op_fft_cx::copy_vec_proxy(typename Proxy<T1>::elem_type* dest, const Proxy<T1>& P, const uword N)
  {
  arma_extra_debug_sigprint();
  
  if(Proxy<T1>::use_at == false)
    {
    typename Proxy<T1>::ea_type X = P.get_ea();
    
    for(uword i=0; i < N; ++i)  { dest[i] = X[i]; }
    }
  else
    {
    if(P.get_n_cols() == 1)
      {
      for(uword i=0; i < N; ++i)  { dest[i] = P.at(i,0); }
      }
    else
      {
      for(uword i=0; i < N; ++i)  { dest[i] = P.at(0,i); }
      }
    }
  }
Beispiel #7
0
arma_hot
inline
typename T1::elem_type
accu_proxy_linear(const Proxy<T1>& P)
  {
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type;
  
        ea_type A      = P.get_ea();
  const uword   n_elem = P.get_n_elem();
  
  eT val1 = eT(0);
  eT val2 = eT(0);
  
  uword i,j;
  for(i=0, j=1; j < n_elem; i+=2, j+=2)
    {
    val1 += A[i];
    val2 += A[j];
    }
  
  if(i < n_elem)
    {
    val1 += A[i];   // equivalent to: val1 += A[n_elem-1];
    }
  
  return (val1 + val2);
  }
Beispiel #8
0
inline
bool
op_any::any_vec_helper(const Base<typename T1::elem_type, T1>& X)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type eT;
  
  const Proxy<T1> P(X.get_ref());
  
  const uword n_elem = P.get_n_elem();
  
  if(Proxy<T1>::prefer_at_accessor == false)
    {
    typename Proxy<T1>::ea_type Pea = P.get_ea();
    
    for(uword i=0; i<n_elem; ++i)
      {
      if(Pea[i] != eT(0))  { return true; }
      }
    }
  else
    {
    const uword n_rows = P.get_n_rows();
    const uword n_cols = P.get_n_cols();
    
    for(uword col=0; col < n_cols; ++col)
    for(uword row=0; row < n_rows; ++row)
      {
      if(P.at(row,col) != eT(0))  { return true; }
      }
    }
  
  return false;
  }
arma_hot
inline
typename T1::elem_type
accu(const Base<typename T1::elem_type,T1>& X)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type;
  
  const Proxy<T1> A(X.get_ref());
  
        ea_type P      = A.get_ea();
  const u32     n_elem = A.get_n_elem();
  
  eT val1 = eT(0);
  eT val2 = eT(0);
  
  u32 i,j;
  
  for(i=0, j=1; j<n_elem; i+=2, j+=2)
    {
    val1 += P[i];
    val2 += P[j];
    }
  
  if(i < n_elem)
    {
    val1 += P[i];
    }
  
  return val1 + val2;
  }
Beispiel #10
0
arma_hot
inline
typename T1::elem_type
op_dot::apply_proxy(const Base<typename T1::elem_type,T1>& X, const Base<typename T1::elem_type,T2>& Y)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type1;
  typedef typename Proxy<T2>::ea_type ea_type2;
  
  const Proxy<T1> A(X.get_ref());
  const Proxy<T2> B(Y.get_ref());
  
  const bool prefer_at_accessor = (Proxy<T1>::prefer_at_accessor) && (Proxy<T2>::prefer_at_accessor);
  
  if(prefer_at_accessor == false)
    {
    arma_debug_check( (A.get_n_elem() != B.get_n_elem()), "dot(): objects must have the same number of elements" );
  
    const uword    N  = A.get_n_elem();
          ea_type1 PA = A.get_ea();
          ea_type2 PB = B.get_ea();
    
    eT val1 = eT(0);
    eT val2 = eT(0);
    
    uword i,j;
    
    for(i=0, j=1; j<N; i+=2, j+=2)
      {
      val1 += PA[i] * PB[i];
      val2 += PA[j] * PB[j];
      }
    
    if(i < N)
      {
      val1 += PA[i] * PB[i];
      }
    
    return val1 + val2;
    }
  else
    {
    return op_dot::apply_unwrap(A.Q, B.Q);
    }
  }
inline
void
op_diagmat2::apply(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const uword row_offset, const uword col_offset)
  {
  arma_extra_debug_sigprint();
  
  const uword n_rows = P.get_n_rows();
  const uword n_cols = P.get_n_cols();
  const uword n_elem = P.get_n_elem();
  
  if(n_elem == 0)  { out.reset(); return; }
  
  const bool P_is_vec = (T1::is_row) || (T1::is_col) || (n_rows == 1) || (n_cols == 1);
  
  if(P_is_vec)
    {
    const uword n_pad = (std::max)(row_offset, col_offset);
    
    out.zeros(n_elem + n_pad, n_elem + n_pad);
    
    if(Proxy<T1>::prefer_at_accessor == false)
      {
      typename Proxy<T1>::ea_type Pea = P.get_ea();
      
      for(uword i=0; i < n_elem; ++i)
        {
        out.at(row_offset + i, col_offset + i) = Pea[i];
        }
      }
    else
      {
      const unwrap<typename Proxy<T1>::stored_type> U(P.Q);
      
      const Proxy<typename unwrap<typename Proxy<T1>::stored_type>::stored_type> PP(U.M);
      
      op_diagmat2::apply(out, PP, row_offset, col_offset);
      }
    }
  else  // P represents a matrix 
    {
    arma_debug_check
      (
      ((row_offset > 0) && (row_offset >= n_rows)) || ((col_offset > 0) && (col_offset >= n_cols)),
      "diagmat(): requested diagonal out of bounds"
      );
    
    out.zeros(n_rows, n_cols);
    
    const uword N = (std::min)(n_rows - row_offset, n_cols - col_offset);
    
    for(uword i=0; i<N; ++i)
      {
      const uword row = i + row_offset;
      const uword col = i + col_offset;
      
      out.at(row,col) = P.at(row,col);
      }
    }
  }
inline
void
glue_max::apply(Mat< std::complex<T> >& out, const Proxy<T1>& PA, const Proxy<T2>& PB)
  {
  arma_extra_debug_sigprint();
  
  typedef typename std::complex<T> eT;
  
  const uword n_rows = PA.get_n_rows();
  const uword n_cols = PA.get_n_cols();
  
  arma_debug_assert_same_size(n_rows, n_cols, PB.get_n_rows(), PB.get_n_cols(), "max(): given matrices must have the same size");
  
  out.set_size(n_rows, n_cols);
  
  eT* out_mem = out.memptr();
  
  if( (Proxy<T1>::use_at == false) && (Proxy<T2>::use_at == false) )
    {
    typename Proxy<T1>::ea_type A = PA.get_ea();
    typename Proxy<T2>::ea_type B = PB.get_ea();
    
    const uword N = PA.get_n_elem();
    
    for(uword i=0; i<N; ++i)
      {
      const eT A_val = A[i];
      const eT B_val = B[i];
      
      out_mem[i] = ( std::abs(A_val) > std::abs(B_val) ) ? A_val : B_val;
      }
    }
  else
    {
    for(uword col=0; col < n_cols; ++col)
    for(uword row=0; row < n_rows; ++row)
      {
      const eT A_val = PA.at(row,col);
      const eT B_val = PB.at(row,col);
      
      *out_mem = ( std::abs(A_val) > std::abs(B_val) ) ? A_val : B_val;
      
      ++out_mem;
      }
    }
  }
Beispiel #13
0
arma_hot
inline
typename T1::pod_type
arma_vec_norm_k
(
    const Proxy<T1>& P,
    const int k
)
{
    arma_extra_debug_sigprint();

    typedef typename T1::pod_type T;

    T acc = T(0);

    if(Proxy<T1>::prefer_at_accessor == false)
    {
        typename Proxy<T1>::ea_type A = P.get_ea();

        const uword N = P.get_n_elem();

        uword i,j;

        for(i=0, j=1; j<N; i+=2, j+=2)
        {
            acc += std::pow(std::abs(A[i]), k);
            acc += std::pow(std::abs(A[j]), k);
        }

        if(i < N)
        {
            acc += std::pow(std::abs(A[i]), k);
        }
    }
    else
    {
        const uword n_rows = P.get_n_rows();
        const uword n_cols = P.get_n_cols();

        if(n_rows != 1)
        {
            for(uword col=0; col < n_cols; ++col)
                for(uword row=0; row < n_rows; ++row)
                {
                    acc += std::pow(std::abs(P.at(row,col)), k);
                }
        }
        else
        {
            for(uword col=0; col < n_cols; ++col)
            {
                acc += std::pow(std::abs(P.at(0,col)), k);
            }
        }
    }

    return std::pow(acc, T(1)/T(k));
}
inline
void
op_clamp::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type eT;
  
  const uword n_rows = P.get_n_rows();
  const uword n_cols = P.get_n_cols();
  
  out.set_size(n_rows, n_cols);
  
  eT* out_mem = out.memptr();
  
  if(Proxy<T1>::use_at == false)
    {
    const uword N = P.get_n_elem();
    
    typename Proxy<T1>::ea_type A = P.get_ea();
    
    uword j;
    for(j=1; j<N; j+=2)
      {
      eT val_i = A[j-1];
      eT val_j = A[j  ];
      
      val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i);
      val_j = (val_j < min_val) ? min_val : ((val_j > max_val) ? max_val : val_j);
      
      (*out_mem) = val_i;  out_mem++;
      (*out_mem) = val_j;  out_mem++;
      }
    
    const uword i = j-1;
    
    if(i < N)
      {
      eT val_i = A[i];
      
      val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i);
      
      (*out_mem) = val_i;
      }
    }
  else
    {
    for(uword col=0; col<n_cols; ++col)
    for(uword row=0; row<n_rows; ++row)
      {
      eT val = P.at(row,col);
      
      val = (val < min_val) ? min_val : ((val > max_val) ? max_val : val);
      
      (*out_mem) = val;  out_mem++;
      }
    }
  }
Beispiel #15
0
arma_hot
inline
typename T1::elem_type
op_norm_dot::apply(const Base<typename T1::elem_type,T1>& X, const Base<typename T1::elem_type,T2>& Y)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type1;
  typedef typename Proxy<T2>::ea_type ea_type2;
  
  const bool prefer_at_accessor = (Proxy<T1>::prefer_at_accessor) && (Proxy<T2>::prefer_at_accessor);
  
  if(prefer_at_accessor == false)
    {
    const Proxy<T1> A(X.get_ref());
    const Proxy<T2> B(Y.get_ref());
    
    arma_debug_check( (A.get_n_elem() != B.get_n_elem()), "norm_dot(): objects must have the same number of elements" );
    
    const uword    N  = A.get_n_elem();
          ea_type1 PA = A.get_ea();
          ea_type2 PB = B.get_ea();
    
    eT acc1 = eT(0);
    eT acc2 = eT(0);
    eT acc3 = eT(0);
    
    for(uword i=0; i<N; ++i)
      {
      const eT tmpA = PA[i];
      const eT tmpB = PB[i];
      
      acc1 += tmpA * tmpA;
      acc2 += tmpB * tmpB;
      acc3 += tmpA * tmpB;
      }
      
    return acc3 / ( std::sqrt(acc1 * acc2) );
    }
  else
    {
    return op_norm_dot::apply_unwrap(X, Y);
    }
  }
Beispiel #16
0
inline
podarray<eT>::podarray(const Proxy<T1>& P)
  : n_elem(P.get_n_elem())
  {
  arma_extra_debug_sigprint_this(this);
  
  const uword P_n_elem = P.get_n_elem();
    
  init_cold(P_n_elem);
  
  eT* out_mem = (*this).memptr();
    
  if(Proxy<T1>::use_at == false)
    {
    typename Proxy<T1>::ea_type A = P.get_ea();
    
    uword i,j;
    for(i=0, j=1; j < P_n_elem; i+=2, j+=2)
      {
      const eT val_i = A[i];
      const eT val_j = A[j];
      
      out_mem[i] = val_i;
      out_mem[j] = val_j;
      }
    
    if(i < P_n_elem)
      {
      out_mem[i] = A[i];
      }
    }
  else
    {
    const uword P_n_rows = P.get_n_rows();
    const uword P_n_cols = P.get_n_cols();
    
    if(P_n_rows != 1)
      {
      uword count = 0;
      
      for(uword col=0; col < P_n_cols; ++col)
      for(uword row=0; row < P_n_rows; ++row, ++count)
        {
        out_mem[count] = P.at(row,col);
        }
      }
    else
      {
      for(uword col=0; col < P_n_cols; ++col)
        {
        out_mem[col] = P.at(0,col);
        }
      }
    }
  }
Beispiel #17
0
arma_hot
inline
typename T1::pod_type
arma_vec_norm_1(const Proxy<T1>& A)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::pod_type T;

  T acc = T(0);
    
  if(Proxy<T1>::prefer_at_accessor == false)
    {
    typename Proxy<T1>::ea_type P = A.get_ea();
    
    const uword N = A.get_n_elem();
    
    uword i,j;
    
    for(i=0, j=1; j<N; i+=2, j+=2)
      {
      acc += std::abs(P[i]);
      acc += std::abs(P[j]);
      }
    
    if(i < N)
      {
      acc += std::abs(P[i]);
      }
    }
  else
    {
    const uword n_rows = A.get_n_rows();
    const uword n_cols = A.get_n_cols();
    
    for(uword col=0; col<n_cols; ++col)
      {
      uword i,j;
      
      for(i=0, j=1; j<n_rows; i+=2, j+=2)
        {
        acc += std::abs(A.at(i,col));
        acc += std::abs(A.at(j,col));
        }
      
      if(i < n_rows)
        {
        acc += std::abs(A.at(i,col));
        }
      }
    }
    
  return acc;
  }
Beispiel #18
0
arma_hot
inline
typename T1::pod_type
arma_vec_norm_2
(
    const Proxy<T1>& P,
    const typename arma_cx_only<typename T1::elem_type>::result* junk = 0
)
{
    arma_extra_debug_sigprint();
    arma_ignore(junk);

    typedef typename T1::pod_type T;

    T acc = T(0);

    if(Proxy<T1>::prefer_at_accessor == false)
    {
        typename Proxy<T1>::ea_type A = P.get_ea();

        const uword N = P.get_n_elem();

        for(uword i=0; i<N; ++i)
        {
            const T tmp = std::abs(A[i]);
            acc += tmp*tmp;
        }
    }
    else
    {
        const uword n_rows = P.get_n_rows();
        const uword n_cols = P.get_n_cols();

        if(n_rows == 1)
        {
            for(uword col=0; col<n_cols; ++col)
            {
                const T tmp = std::abs(P.at(0,col));
                acc += tmp*tmp;
            }
        }
        else
        {
            for(uword col=0; col<n_cols; ++col)
                for(uword row=0; row<n_rows; ++row)
                {
                    const T tmp = std::abs(P.at(row,col));
                    acc += tmp*tmp;
                }
        }
    }

    return std::sqrt(acc);
}
Beispiel #19
0
arma_hot
inline
typename T1::elem_type
accu(const Base<typename T1::elem_type,T1>& X)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type;
  
  const Proxy<T1> A(X.get_ref());
  
  if(Proxy<T1>::prefer_at_accessor == false)
    {
          ea_type P      = A.get_ea();
    const uword   n_elem = A.get_n_elem();
    
    eT val1 = eT(0);
    eT val2 = eT(0);
    
    uword i,j;
    
    for(i=0, j=1; j<n_elem; i+=2, j+=2)
      {
      val1 += P[i];
      val2 += P[j];
      }
    
    if(i < n_elem)
      {
      val1 += P[i];
      }
    
    return val1 + val2;
    }
  else
    {
    const uword n_rows = A.get_n_rows();
    const uword n_cols = A.get_n_cols();
    
    eT val = eT(0);
    
    for(uword col=0; col<n_cols; ++col)
      {
      for(uword row=0; row<n_rows; ++row)
        {
        val += A.at(row,col);
        }
      }
    
    return val;
    }
  }
Beispiel #20
0
inline
bool
op_sqrtmat::apply_direct(Mat< std::complex<typename T1::elem_type> >& out, const Base<typename T1::elem_type,T1>& expr)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type       in_T;
  typedef typename std::complex<in_T> out_T;
  
  const Proxy<T1> P(expr.get_ref());
  
  arma_debug_check( (P.get_n_rows() != P.get_n_cols()), "sqrtmat(): given matrix must be square sized" );
  
  if(P.get_n_elem() == 0)
    {
    out.reset();
    return true;
    }
  
  typename Proxy<T1>::ea_type Pea = P.get_ea();
  
  Mat<out_T> U;
  Mat<out_T> S(P.get_n_rows(), P.get_n_cols());
  
  out_T* Smem = S.memptr();
  
  const uword N = P.get_n_elem();
  
  for(uword i=0; i<N; ++i)
    {
    Smem[i] = std::complex<in_T>( Pea[i] );
    }
  
  const bool schur_ok = auxlib::schur(U,S);
  
  if(schur_ok == false)
    {
    arma_extra_debug_print("sqrtmat(): schur decomposition failed");
    out.reset();
    return false;
    }
  
  const bool status = op_sqrtmat_cx::helper(S);
  
  const Mat<out_T> X = U*S;
  
  S.reset();
  
  out = X*U.t();
  
  return status;
  }
Beispiel #21
0
inline
arma_warn_unused
uword
accu(const mtOp<uword,T1,op_rel_noteq>& X)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type eT;
  
  const eT val = X.aux;
  
  const Proxy<T1> P(X.m);
  
  uword n_nonzero = 0;
    
  if(Proxy<T1>::prefer_at_accessor == false)
    {
    typedef typename Proxy<T1>::ea_type ea_type;
    
          ea_type A      = P.get_ea();
    const uword   n_elem = P.get_n_elem();
    
    for(uword i=0; i<n_elem; ++i)
      {
      if(A[i] != val) { ++n_nonzero; }
      }
    }
  else
    {
    const uword P_n_cols = P.get_n_cols();
    const uword P_n_rows = P.get_n_rows();
    
    if(P_n_rows == 1)
      {
      for(uword col=0; col < P_n_cols; ++col)
        {
        if(P.at(0,col) != val) { ++n_nonzero; }
        }
      }
    else
      {
      for(uword col=0; col < P_n_cols; ++col)
      for(uword row=0; row < P_n_rows; ++row)
        {
        if(P.at(row,col) != val) { ++n_nonzero; }
        }
      }
    }
  
  return n_nonzero;
  }
Beispiel #22
0
arma_hot
inline
typename T1::elem_type
op_dot::apply_proxy(const Base<typename T1::elem_type,T1>& X, const Base<typename T1::elem_type,T2>& Y)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type1;
  typedef typename Proxy<T2>::ea_type ea_type2;
  
  const Proxy<T1> A(X.get_ref());
  const Proxy<T2> B(Y.get_ref());
  
  arma_debug_check( (A.get_n_elem() != B.get_n_elem()), "dot(): objects must have the same number of elements" );
  
  const u32      N  = A.get_n_elem();
        ea_type1 PA = A.get_ea();
        ea_type2 PB = B.get_ea();
  
  eT val1 = eT(0);
  eT val2 = eT(0);
  
  u32 i,j;
  
  for(i=0, j=1; j<N; i+=2, j+=2)
    {
    val1 += PA[i] * PB[i];
    val2 += PA[j] * PB[j];
    }
  
  if(i < N)
    {
    val1 += PA[i] * PB[i];
    }
  
  return val1 + val2;
  }
Beispiel #23
0
arma_hot
inline
typename arma_cx_only<typename T1::elem_type>::result
op_dot::apply_proxy(const Proxy<T1>& PA, const Proxy<T2>& PB)
{
    arma_extra_debug_sigprint();

    typedef typename T1::elem_type            eT;
    typedef typename get_pod_type<eT>::result  T;

    typedef typename Proxy<T1>::ea_type ea_type1;
    typedef typename Proxy<T2>::ea_type ea_type2;

    const uword N = PA.get_n_elem();

    ea_type1 A = PA.get_ea();
    ea_type2 B = PB.get_ea();

    T val_real = T(0);
    T val_imag = T(0);

    for(uword i=0; i<N; ++i)
    {
        const std::complex<T> xx = A[i];
        const std::complex<T> yy = B[i];

        const T a = xx.real();
        const T b = xx.imag();

        const T c = yy.real();
        const T d = yy.imag();

        val_real += (a*c) - (b*d);
        val_imag += (a*d) + (b*c);
    }

    return std::complex<T>(val_real, val_imag);
}
inline
void
glue_max::apply(Mat<eT>& out, const Proxy<T1>& PA, const Proxy<T2>& PB)
  {
  arma_extra_debug_sigprint();
  
  const uword n_rows = PA.get_n_rows();
  const uword n_cols = PA.get_n_cols();
  
  arma_debug_assert_same_size(n_rows, n_cols, PB.get_n_rows(), PB.get_n_cols(), "max(): given matrices must have the same size");
  
  out.set_size(n_rows, n_cols);
  
  eT* out_mem = out.memptr();
  
  if( (Proxy<T1>::use_at == false) && (Proxy<T2>::use_at == false) )
    {
    typename Proxy<T1>::ea_type A = PA.get_ea();
    typename Proxy<T2>::ea_type B = PB.get_ea();
    
    const uword N = PA.get_n_elem();
    
    for(uword i=0; i<N; ++i)
      {
      out_mem[i] = (std::max)(A[i], B[i]);
      }
    }
  else
    {
    for(uword col=0; col < n_cols; ++col)
    for(uword row=0; row < n_rows; ++row)
      {
      *out_mem = (std::max)( PA.at(row,col), PB.at(row,col) );
      
      ++out_mem;
      }
    }
  }
Beispiel #25
0
arma_hot
arma_inline
typename T1::elem_type
op_norm_dot::apply(const Base<typename T1::elem_type,T1>& X, const Base<typename T1::elem_type,T2>& Y)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type1;
  typedef typename Proxy<T2>::ea_type ea_type2;
  
  const Proxy<T1> A(X.get_ref());
  const Proxy<T2> B(Y.get_ref());
  
  arma_debug_check( (A.get_n_elem() != B.get_n_elem()), "norm_dot(): objects must have the same number of elements" );
  
  const u32      N  = A.get_n_elem();
        ea_type1 PA = A.get_ea();
        ea_type2 PB = B.get_ea();
  
  eT acc1 = eT(0);
  eT acc2 = eT(0);
  eT acc3 = eT(0);
  
  for(u32 i=0; i<N; ++i)
    {
    const eT tmpA = PA[i];
    const eT tmpB = PB[i];
    
    acc1 += tmpA * tmpA;
    acc2 += tmpB * tmpB;
    acc3 += tmpA * tmpB;
    }
    
  return acc3 / ( std::sqrt(acc1 * acc2) );
  }
inline
uword
op_find::helper
  (
  Mat<uword>& indices,
  const Base<typename T1::elem_type, T1>& X
  )
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type eT;
  
  const Proxy<T1> A(X.get_ref());
  
  const uword n_elem = A.get_n_elem();
  
  indices.set_size(n_elem, 1);
  
  uword* indices_mem = indices.memptr();
  uword  n_nz        = 0;
  
  if(Proxy<T1>::prefer_at_accessor == false)
    {
    typename Proxy<T1>::ea_type PA = A.get_ea();
    
    for(uword i=0; i<n_elem; ++i)
      {
      if(PA[i] != eT(0))  { indices_mem[n_nz] = i;  ++n_nz; }
      }
    }
  else
    {
    const uword n_rows = A.get_n_rows();
    const uword n_cols = A.get_n_cols();
    
    uword i = 0;
    
    for(uword col=0; col < n_cols; ++col)
    for(uword row=0; row < n_rows; ++row)
      {
      if(A.at(row,col) != eT(0))  { indices_mem[n_nz] = i; ++n_nz; }
      
      ++i;
      }
    }
  
  return n_nz;
  }
inline
void
spdiagview<eT>::operator/=(const Base<eT,T1>& o)
  {
  arma_extra_debug_sigprint();
  
  spdiagview<eT>& d = *this;
  
  SpMat<eT>& d_m = const_cast< SpMat<eT>& >(d.m);
  
  const uword d_n_elem     = d.n_elem;
  const uword d_row_offset = d.row_offset;
  const uword d_col_offset = d.col_offset;
    
  const Proxy<T1> P( o.get_ref() );
  
  arma_debug_check
    (
    ( (d_n_elem != P.get_n_elem()) || ((P.get_n_rows() != 1) && (P.get_n_cols() != 1)) ),
    "spdiagview: given object has incompatible size"
    );
  
  if( (is_Mat<typename Proxy<T1>::stored_type>::value == true) || (Proxy<T1>::prefer_at_accessor == true) )
    {
    const unwrap<typename Proxy<T1>::stored_type> tmp(P.Q);
    const Mat<eT>& x = tmp.M;
    
    const eT* x_mem = x.memptr();

    for(uword i=0; i < d_n_elem; ++i)
      {
      d_m.at(i + d_row_offset, i + d_col_offset) /= x_mem[i];
      }
    }
  else
    {
    typename Proxy<T1>::ea_type Pea = P.get_ea();
      
    for(uword i=0; i < d_n_elem; ++i)
      {
      d_m.at(i + d_row_offset, i + d_col_offset) /= Pea[i];
      }
    }
  }
inline
uword
op_find::helper
  (
  Mat<uword>& indices,
  const mtOp<uword, T1, op_type>& X,
  const typename arma_op_rel_only<op_type>::result            junk1,
  const typename arma_cx_only<typename T1::elem_type>::result junk2
  )
  {
  arma_extra_debug_sigprint();
  arma_ignore(junk1);
  arma_ignore(junk2);
  
  typedef typename T1::elem_type      eT;
  typedef typename Proxy<T1>::ea_type ea_type;
  
  const eT val = X.aux;
  
  const Proxy<T1> A(X.m);
  
  ea_type     PA     = A.get_ea();
  const uword n_elem = A.get_n_elem();
  
  indices.set_size(n_elem, 1);
  
  uword* indices_mem = indices.memptr();
  uword  n_nz        = 0;
  
  for(uword i=0; i<n_elem; ++i)
    {
    const eT tmp = PA[i];
    
    bool not_zero;
    
         if(is_same_type<op_type, op_rel_eq   >::value == true)  { not_zero = (tmp == val); }
    else if(is_same_type<op_type, op_rel_noteq>::value == true)  { not_zero = (tmp != val); }
    else not_zero = false;
    
    if(not_zero == true) { indices_mem[n_nz] = i;  ++n_nz; }
    }
  
  return n_nz;
  }
Beispiel #29
0
inline
void
op_nonzeros::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type eT;
  
  const uword N_max = P.get_n_elem();
  
  Mat<eT> tmp(N_max, 1);
  
  eT* tmp_mem = tmp.memptr();
  
  uword N_nz = 0;
  
  if(Proxy<T1>::use_at == false)
    {
    typename Proxy<T1>::ea_type Pea = P.get_ea();
    
    for(uword i=0; i<N_max; ++i)
      {
      const eT val = Pea[i];
      
      if(val != eT(0))  { tmp_mem[N_nz] = val; ++N_nz; }
      }
    }
  else
    {
    const uword n_rows = P.get_n_rows();
    const uword n_cols = P.get_n_cols();
    
    for(uword col=0; col < n_cols; ++col)
    for(uword row=0; row < n_rows; ++row)
      {
      const eT val = P.at(row,col);
      
      if(val != eT(0))  { tmp_mem[N_nz] = val; ++N_nz; }
      }
    }
  
  out.steal_mem_col(tmp, N_nz);
  }
inline
bool
op_logmat::apply_direct(Mat< std::complex<typename T1::elem_type> >& out, const Base<typename T1::elem_type,T1>& expr, const uword n_iters)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T1::elem_type       in_T;
  typedef typename std::complex<in_T> out_T;
  
  const Proxy<T1> P(expr.get_ref());
  
  arma_debug_check( (P.get_n_rows() != P.get_n_cols()), "logmat(): given matrix must be square sized" );
  
  if(P.get_n_elem() == 0)
    {
    out.reset();
    return true;
    }
  else
  if(P.get_n_elem() == 1)
    {
    out.set_size(1,1);
    out[0] = std::log( std::complex<in_T>( P[0] ) );
    return true;
    }
  
  typename Proxy<T1>::ea_type Pea = P.get_ea();
  
  Mat<out_T> U;
  Mat<out_T> S(P.get_n_rows(), P.get_n_rows());
  
  out_T* Smem = S.memptr();
  
  const uword n_elem = P.get_n_elem();
  
  for(uword i=0; i<n_elem; ++i)
    {
    Smem[i] = std::complex<in_T>( Pea[i] );
    }
  
  return op_logmat_cx::apply_common(out, S, n_iters);
  }