source/tool/global.cxx

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  The Contents of this file are made available subject to
00004  *  the terms of GNU Lesser General Public License Version 2.1.
00005  *
00006  *
00007  *    GNU Lesser General Public License Version 2.1
00008  *    =============================================
00009  *    Copyright 2005 by Kohei Yoshida.
00010  *    1039 Kingsway Dr., Apex, NC 27502, USA
00011  *
00012  *    This library is free software; you can redistribute it and/or
00013  *    modify it under the terms of the GNU Lesser General Public
00014  *    License version 2.1, as published by the Free Software Foundation.
00015  *
00016  *    This library is distributed in the hope that it will be useful,
00017  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *    Lesser General Public License for more details.
00020  *
00021  *    You should have received a copy of the GNU Lesser General Public
00022  *    License along with this library; if not, write to the Free Software
00023  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024  *    MA  02111-1307  USA
00025  *
00026  ************************************************************************/
00027 
00028 #include "tool/global.hxx"
00029 #include "numeric/matrix.hxx"
00030 #include <sstream>
00031 #include <stdio.h>
00032 
00033 using ::std::vector;
00034 using ::std::string;
00035 using ::std::ostringstream;
00036 using ::scsolver::numeric::Matrix;
00037 
00038 namespace scsolver {
00039 
00040 #if SCSOLVER_DEBUG
00041 void Debug( const char* s )
00042 {
00043         fprintf(stdout, "%s\n", s);
00044         fflush(stdout);
00045 }
00046 #else
00047 void Debug( const char* )
00048 {
00049 }
00050 #endif
00051 
00052 string repeatString( const char* str, unsigned long nNum )
00053 {
00054     ostringstream os;
00055         for ( unsigned int i = 0; i < nNum; ++i )
00056                 os << str;
00057         return os.str();
00058 }
00059 
00060 void vectorToMatrix(const vector<double>& vec, Matrix& mx, bool rowMatrix)
00061 {
00062     size_t vecSize = vec.size();
00063     if (rowMatrix)
00064     {
00065         Matrix tmp(1, vecSize);
00066         for (size_t i = 0; i < vecSize; ++i)
00067             tmp(0, i) = vec[i];
00068         mx.swap(tmp);
00069     }
00070     else
00071     {
00072         Matrix tmp(vecSize, 1);
00073         for (size_t i = 0; i < vecSize; ++i)
00074             tmp(i, 0) = vec[i];
00075         mx.swap(tmp);
00076     }
00077 }
00078 
00079 void matrixToVector(const Matrix& mx, vector<double>& vec)
00080 {
00081     bool rowMatrix = mx.cols() > 1;
00082     if (rowMatrix)
00083     {
00084         // single row & multi-column matrix.
00085 
00086         size_t n = mx.cols();
00087         vector<double> tmp;
00088         tmp.reserve(n);
00089         for (size_t i = 0; i < n; ++i)
00090             tmp.push_back(mx(0, i));
00091         vec.swap(tmp);
00092     }
00093     else
00094     {
00095         // single column & multi-row matrix.
00096 
00097         size_t n = mx.rows();
00098         vector<double> tmp;
00099         tmp.reserve(n);
00100         for (size_t i = 0; i < n; ++i)
00101             tmp.push_back(mx(i, 0));
00102         vec.swap(tmp);
00103     }
00104 }
00105 
00106 }

Generated on Mon Jul 28 09:13:20 2008 for scsolver by  doxygen 1.5.3