cheshirekow  v0.1.0
ostream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_POLYNOMIAL_OSTREAM_H_
28 #define MPBLOCKS_POLYNOMIAL_OSTREAM_H_
29 
30 #include <iostream>
31 #include <cstdio>
32 #include <iomanip>
33 #include <sstream>
34 #include <vector>
35 
36 namespace mpblocks {
37 namespace polynomial {
38 
39 
40 
41 template <typename Scalar, class Exp>
42 std::ostream& print( std::ostream& out,
43  RValue<Scalar,Exp> const& p,
44  const std::string& var = "x")
45 {
46  for(int i=0; i < p.size(); i++)
47  {
48  out << p[i]
49  << " " << var << "^"
50  << i;
51  if( i < p.size()-1 )
52  out << " + ";
53  }
54 
55  return out;
56 }
57 
58 template <typename Scalar, class Exp>
59 std::ostream& print( std::ostream& out,
60  RValue<Scalar,Exp> const& p,
61  const std::vector<int>& spec,
62  const std::string& var = "x")
63 {
64  std::stringstream strm;
65  for(int i=0; i < p.size(); i++)
66  {
67  strm.str("");
68  strm << p[i]
69  << " " << var << "^"
70  << i;
71  out << std::setw(spec[i])
72  << strm.str();
73  if( i < p.size()-1 )
74  out << " + ";
75  }
76 
77  return out;
78 }
79 
80 
81 template <typename Scalar, class Exp>
82 void preprint( std::vector<int>& spec,
83  RValue<Scalar,Exp> const& p,
84  const std::string& var = "x")
85 {
86  std::stringstream out;
87  if( spec.size() < p.size() )
88  spec.resize( p.size(), 0 );
89 
90  for(int i=0; i < p.size(); i++)
91  {
92  out.str("");
93  out << p[i]
94  << " " << var << "^"
95  << i;
96  if( out.str().size() > spec[i] )
97  spec[i] = out.str().size();
98  }
99 }
100 
101 
102 template <typename Scalar, class Exp>
103 std::ostream& operator<<( std::ostream& out,
104  RValue<Scalar,Exp> const& p )
105 {
106  return print(out,p);
107 }
108 
109 template <typename Scalar>
110 std::ostream& print( std::ostream& out,
111  SturmSequence<Scalar> const& seq,
112  const std::string& var="x")
113 {
114  std::vector<int> spec( seq[0].size(), 0 );
115 
116  for(int i=0; i < seq.size(); i++)
117  preprint(spec,seq[i],var);
118  for(int i=0; i < seq.size(); i++)
119  print(out,seq[i],spec,var) << "\n";
120  return out;
121 }
122 
123 template <typename Scalar>
124 std::ostream& operator<<( std::ostream& out,
125  SturmSequence<Scalar> const& seq )
126 {
127  return print(out,seq);
128 }
129 
130 
131 
132 
133 
134 
135 } // polynomial
136 } // mpblocks
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 #endif // IOSTREAM_H_
void preprint(std::vector< int > &spec, RValue< Scalar, Exp > const &p, const std::string &var="x")
Definition: ostream.h:82
std::ostream & print(std::ostream &out, RValue< Scalar, Exp > const &p, const std::string &var="x")
Definition: ostream.h:42
Size_t size() const
return the size for a vector
Definition: RValue.h:41
std::ostream & operator<<(std::ostream &out, RValue< Scalar, Exp > const &p)
Definition: ostream.h:103
expression template for rvalues
Definition: RValue.h:35