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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*
* PolynomialInterpolation.cpp
*
* Created on: May 13, 2010
* Author: bedutra
*/
#include "PolynomialInterpolation.h"
#include <iostream>
#include <cstdlib>
using namespace std;
/** Find the coefficients of any degree d polynomial over Q.
* degree: the degree of the unknown poly.
*/
PolynomialInterpolation::PolynomialInterpolation(unsigned int degree):
colSize(degree + 1),
rowSize(degree + 1),
matrix(NULL),
initCounter(0)
{
matrix = new mpq_class*[rowSize];
for(int row = 0; row < rowSize; ++row)
{
matrix[row] = new mpq_class[colSize + 1]; // [A | b}
for(int col = 0; col <= colSize; ++col)
{
//mpq_init(matrix[row][col]);
matrix[row][col] = 0;
}
}
}//constructor
//A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
//sets matrix(toRow,:) = matirx(toRow,:) - value * matrix(fromRow, :) (matlab syntax)
void PolynomialInterpolation::addMultRows(mpq_class &value, int fromRow, int toRow)
{
value.canonicalize();
for(int i = 0; i <= colSize; ++i)
{
matrix[toRow][i] = matrix[toRow][i] - value * matrix[fromRow][i];
}//for i
}//addMultRows
//adds the point (x, f(x)) to the matrix.
// adds the row [1, x, x^2, ..., f(x)] to the matrix.
void PolynomialInterpolation::addPoint(mpq_class x, mpq_class f)
{
mpq_class powerOfx(1);
f.canonicalize();
x.canonicalize();
if ( initCounter >= rowSize)
{
cerr << "Ops, you have too many points" << endl;
exit(1);
}
for(int col = 0; col < colSize; ++col)
{
matrix[initCounter][col] = powerOfx;
powerOfx = powerOfx * x;
}
matrix[initCounter][colSize] = f;
initCounter++;
}//addPoint
/* Returns f(xValue) where f is a polynomial. poly[i] is the coefficient of x^i.
*
*/
mpq_class PolynomialInterpolation::evaluatePoly(mpq_class const & xValue, vector<mpq_class> const & poly)
{
mpq_class power, sum;
sum = 0;
power = 1;
sum.canonicalize();
power.canonicalize();
for( int i = poly.size() - 1; i > 0; --i)
{
sum = (sum + poly[i])* xValue;
//sum.canonicalize();
//if ( ! isReduced(sum) )
//{
// cout << "ops, not in reduced form: " << sum << endl;
// exit(1);
//}
}//for i
mpq_class finalSum = sum + poly[0];
finalSum.canonicalize();
return finalSum;
}//evaluates.
void PolynomialInterpolation::GE()
{
//PolynomialInterpolation copy(rowSize - 1);
//copy = *this;
int perfectRow = 0; //the row with only zeros on the left.
int currentColumn = 0;
while( currentColumn < colSize && perfectRow < rowSize)
{
//find row with non-zero base
int currentRow = perfectRow;
while( currentRow < rowSize && matrix[currentRow][currentColumn] == 0)
{
currentRow = currentRow + 1;
}
if( currentRow > rowSize)
{
currentColumn++;
continue; //found column of zeros
}
else
{
swap(currentRow, perfectRow);
}//swap rows so that matrix[perfectRow][currentColum] is non-zero
if(matrix[perfectRow][currentColumn] == 0 )
{
cerr << "GE:assert matirx[perfectRow][currentColumn] != 0" << endl;
//cout << "perfectRow = " << perfectRow << ", curCol=" << currentColumn << endl;
//cout << "row, col size=" << rowSize << ", " << colSize << endl;
//printMatrix();
//cout << "origional matirx\n";
//copy.printMatrix();
exit(1);
}
timesRow(perfectRow, mpq_class(matrix[perfectRow][currentColumn].get_den(), matrix[perfectRow][currentColumn].get_num()));
if ( matrix[perfectRow][currentColumn] != 1)
{
cerr << "GE::assert matrix[perfectRow][currentColumn] == 1 failed" << endl;
//cerr << "pr = " << perfectRow << ", curCol=" << currentColumn << endl;
//printMatrix();
exit(1);
}
//use row 'perfectRow' to eliminate every other row.
for( int i = 0; i < rowSize; ++i)
{
if( matrix[i][currentColumn] != 0 && i != perfectRow)
{
mpq_class value(matrix[i][currentColumn].get_num(), matrix[i][currentColumn].get_den());
addMultRows(value, perfectRow, i);
if ( matrix[i][currentColumn] != 0)
{
cerr << "GE::matrix[i][currentColumn] != 0 failed." << endl;
exit(1);
}
}//do row additions on every row other than the current row.
}//for i.
perfectRow++;
currentColumn++;
}//end while
}//GE()
/** return b if the matrix is in the form [Id | b]
* else calls exit if the matrix is singular.
*/
vector<mpq_class> PolynomialInterpolation::getSolutionVector()
{
vector<mpq_class> answer(rowSize);
if ( isSingular() )
{
cerr << "ops, there is a nullspace!" << endl;
printMatrix();
exit(1);
}
for(int i = 0; i < rowSize; ++i)
{
answer[i] = matrix[i][colSize];
}
return answer;
}//getSolutionVector
void PolynomialInterpolation::printMatrix() const
{
cout << "PRINT MATRIX\n";
for(int row = 0; row < rowSize; ++row)
{
for(int col = 0; col <= colSize; ++col)
{
cout << setw(10) << matrix[row][col] << ", ";
}//for col
cout << endl;
}//for row.
}//printMatrix
/** Returns true if A != Id, where matrix = [A | b]
*
*/
bool PolynomialInterpolation::isSingular()
{
for(int i = 0; i < rowSize; ++i)
for(int k = 0; k < colSize; ++k)
if ( matrix[i][k] != 0 && i != k)
return true;
else if ( matrix[i][k] != 1 && i == k) //matrix[i][i] could be 0
return true;
return false;
}//isSingular
/** Does GE and returns the coeff. vector.
*
*/
vector<mpq_class> PolynomialInterpolation::solve()
{
GE();
return getSolutionVector();
}
//swap two rows.
void PolynomialInterpolation::swap(int a, int b)
{
mpq_class *temp;
temp = matrix[a];
matrix[a] = matrix[b];
matrix[b] = temp;
}
/* Times row "row" (including the b-column) by "value"
*
*/
void PolynomialInterpolation::timesRow(int row, mpq_class value)
{
value.canonicalize();
for(int i = 0; i <= colSize; ++i)
{
matrix[row][i] = matrix[row][i] * value;
}//for i. Also times the constant term.
}//timesRow
PolynomialInterpolation & PolynomialInterpolation::operator=(const PolynomialInterpolation & rhs)
{
if (this == &rhs) // Same object?
return *this; // Yes, so skip assignment, and just return *this.
colSize = rhs.colSize;
rowSize = rhs.rowSize;
initCounter = rhs.initCounter;
matrix = new mpq_class*[rowSize];
for(int row = 0; row < rowSize; ++row)
{
matrix[row] = new mpq_class[colSize + 1]; // [A | b}
for(int col = 0; col <= colSize; ++col)
{
matrix[row][col] = rhs.matrix[row][col];
}
}//for row
return *this;
}//operator=
|