1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*!
* \file
* \brief Polynomial functions
* \author Tony Ottosson
*
* -------------------------------------------------------------------------
*
* IT++ - C++ library of mathematical, signal processing, speech processing,
* and communications classes and functions
*
* Copyright (C) 1995-2008 (see AUTHORS file for a list of contributors)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* -------------------------------------------------------------------------
*/
#include <itpp/signal/poly.h>
#include <itpp/base/converters.h>
#include <itpp/base/algebra/eigen.h>
#include <itpp/base/specmat.h>
#include <itpp/base/matfunc.h>
namespace itpp {
void poly(const vec &r, vec &p)
{
int n = r.size();
p.set_size(n+1, false);
p.zeros();
p(0) = 1.0;
for (int i=0; i<n; i++)
p.set_subvector(1, i+1, p(1,i+1) - r(i)*p(0,i));
}
void poly(const cvec &r, cvec &p)
{
int n = r.size();
p.set_size(n+1, false);
p.zeros();
p(0) = 1.0;
for (int i=0; i<n; i++)
p.set_subvector(1, i+1, p(1,i+1) - r(i)*p(0,i));
}
void roots(const vec &p, cvec &r)
{
int n = p.size(), m, l;
ivec f = find(p != 0.0);
m = f.size();
vec v = p;
mat A;
if (m > 0 && n > 1) {
v = v(f(0),f(m-1));
l = v.size();
if (l>1) {
A = diag(ones(l-2), -1);
A.set_row(0, -v(1,l-1)/v(0));
r = eig(A);
cvec d;
cmat V;
eig(A, d ,V);
if (f(m-1) < n)
r = concat(r, zeros_c(n-f(m-1)-1));
} else {
r.set_size(n-f(m-1)-1, false);
r.zeros();
}
} else
r.set_size(0, false);
}
void roots(const cvec &p, cvec &r)
{
int n = p.size(), m, l;
ivec f;
// find all non-zero elements
for (int i=0; i<n; i++)
if( p(i) != 0.0 )
f = concat(f, i);
m = f.size();
cvec v = p;
cmat A;
if (m > 0 && n > 1) {
v = v(f(0),f(m-1));
l = v.size();
if (l>1) {
A = diag(ones_c(l-2), -1);
A.set_row(0, -v(1,l-1)/v(0));
r = eig(A);
if (f(m-1) < n)
r = concat(r, zeros_c(n-f(m-1)-1));
} else {
r.set_size(n-f(m-1)-1, false);
r.zeros();
}
} else
r.set_size(0, false);
}
vec polyval(const vec &p, const vec &x)
{
it_error_if(p.size() == 0, "polyval: size of polynomial is zero");
it_error_if(x.size() == 0, "polyval: size of input value vector is zero");
vec out(x.size());
out = p(0);
for (int i=1; i<p.size(); i++)
out = p(i) + elem_mult(x, out);
return out;
}
cvec polyval(const vec &p, const cvec &x)
{
it_error_if(p.size() == 0, "polyval: size of polynomial is zero");
it_error_if(x.size() == 0, "polyval: size of input value vector is zero");
cvec out(x.size());
out = p(0);
for (int i=1; i<p.size(); i++)
out = std::complex<double>(p(i)) + elem_mult(x, out);
return out;
}
cvec polyval(const cvec &p, const vec &x)
{
it_error_if(p.size() == 0, "polyval: size of polynomial is zero");
it_error_if(x.size() == 0, "polyval: size of input value vector is zero");
cvec out(x.size());
out = p(0);
for (int i=1; i<p.size(); i++)
out = std::complex<double>(p(i)) + elem_mult(to_cvec(x), out);
return out;
}
cvec polyval(const cvec &p, const cvec &x)
{
it_error_if(p.size() == 0, "polyval: size of polynomial is zero");
it_error_if(x.size() == 0, "polyval: size of input value vector is zero");
cvec out(x.size());
out = p(0);
for (int i=1; i<p.size(); i++)
out = p(i) + elem_mult(x, out);
return out;
}
} // namespace itpp
|