1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17
18
19 //! \addtogroup glue_polyval
20 //! @{
21
22
23
24 template<typename eT>
25 inline
26 void
apply_noalias(Mat<eT> & out,const Mat<eT> & P,const Mat<eT> & X)27 glue_polyval::apply_noalias(Mat<eT>& out, const Mat<eT>& P, const Mat<eT>& X)
28 {
29 arma_extra_debug_sigprint();
30
31 out.set_size(X.n_rows, X.n_cols);
32
33 const eT* P_mem = P.memptr();
34 const uword P_n_elem = P.n_elem;
35
36 out.fill(P_mem[0]);
37
38 for(uword i=1; i < P_n_elem; ++i)
39 {
40 out = out % X + P_mem[i];
41 }
42 }
43
44
45
46 template<typename T1, typename T2>
47 inline
48 void
apply(Mat<typename T1::elem_type> & out,const Glue<T1,T2,glue_polyval> & expr)49 glue_polyval::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_polyval>& expr)
50 {
51 arma_extra_debug_sigprint();
52
53 typedef typename T1::elem_type eT;
54
55 const quasi_unwrap<T1> UP(expr.A);
56 const quasi_unwrap<T2> UX(expr.B);
57
58 const Mat<eT>& P = UP.M;
59 const Mat<eT>& X = UX.M;
60
61 arma_debug_check( ((P.is_vec() == false) && (P.is_empty() == false)), "polyval(): argument P must be a vector" );
62
63 if(P.is_empty() || X.is_empty())
64 {
65 out.zeros(X.n_rows, X.n_cols);
66 return;
67 }
68
69 if(UP.is_alias(out) || UX.is_alias(out))
70 {
71 Mat<eT> tmp;
72 glue_polyval::apply_noalias(tmp, P, X);
73 out.steal_mem(tmp);
74 }
75 else
76 {
77 glue_polyval::apply_noalias(out, P, X);
78 }
79 }
80
81
82
83 //! @}
84