PARP Research Group University of Murcia, Spain


src/qvmath/qvrationalnumber.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <iostream>
00026 #include <qvmath/qvrationalnumber.h>
00027 
00028 std::ostream & operator<< (std::ostream &os, const QVRationalNumber &factInteger )
00029         {
00030         os << "QVRationalNumber(" << (double) factInteger;
00031 
00032         QList<int> keys = factInteger.keys();
00033         if (keys.size() > 0)
00034                 {
00035                 os << " = " << keys.at(0) << "^" << factInteger[keys.at(0)];
00036                 for (int i = 1; i < keys.size(); i++)
00037                         if (factInteger[keys.at(i)] != 0)
00038                                 os << " + " << keys.at(i) << "^" << factInteger[keys.at(i)];
00039                 }
00040         os << ")";
00041 
00042         return os;
00043         }
00044 
00045 QVRationalNumber QVRationalNumber::operator*(const QVRationalNumber &factInteger) const
00046         {
00047         QVRationalNumber result = *this;
00048         QList<int> keys = factInteger.keys();
00049 
00050         foreach(int key, factInteger.keys())
00051                 if (result.contains(key))
00052                         result[key] += factInteger[key];
00053                 else
00054                         result.insert(key,factInteger[key]);
00055 
00056         return result;
00057         };
00058 
00059 QVRationalNumber QVRationalNumber::operator/(const QVRationalNumber &factInteger) const
00060         {
00061         QVRationalNumber result = *this;
00062         QList<int> keys = factInteger.keys();
00063 
00064         foreach(int key, factInteger.keys())
00065                 if (result.contains(key))
00066                         result[key] -= factInteger[key];
00067                 else
00068                         result.insert(key,-factInteger[key]);
00069 
00070         return result;
00071         };
00072 
00073 QVRationalNumber::operator double() const
00074         {
00075         double result = 1;
00076         foreach(int key, keys())
00077                 result *= pow(key, operator[](key));
00078         return result;
00079         }
00080 
00081 void QVRationalNumber::mult(const int integer)
00082         {
00083         const int maxIndex = (int)sqrt(integer);
00084         
00085         int remain = integer;
00086         for (int i = 2; i <= maxIndex; i++)
00087                 if (remain % i == 0)
00088                         {
00089                         if (!contains(i))
00090                                 insert(i,0);
00091                         while(remain % i == 0)
00092                                 {
00093                                 operator[](i)++;
00094                                 remain = remain/i;
00095                                 }
00096                         }
00097                 else if (remain == 1)
00098                         break;
00099 
00100         if (remain != 1)
00101                 if (!contains(remain))
00102                         insert(remain,1);
00103                 else
00104                         operator[](remain)++;
00105         };
00106 



QVision framework. PARP research group, copyright 2007, 2008.