Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

RealMapping.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: RealMapping.h,v 1.19 2004/07/21 16:30:50 chrish42 Exp $ 00038 * This file is part of the PLearn library. 00039 ******************************************************* */ 00040 00041 #ifndef RealMapping_INC 00042 #define RealMapping_INC 00043 00044 //#include "general.h" 00045 #include "Object.h" 00046 #include <plearn/math/TMat.h> 00047 #include <map> 00048 00049 namespace PLearn { 00050 using namespace std; 00051 00053 class RealRange 00054 { 00055 public: 00056 real low; 00057 real high; 00058 char leftbracket; // either '[' (inclusive left) or ']' (exclusive left) 00059 char rightbracket; // // either '[' (exclusive right) or ']' (inclusive right) 00060 00061 public: 00062 RealRange(): // default construvtor 00063 low(0), high(0), leftbracket(']'), rightbracket('[') 00064 {} 00065 00066 00067 RealRange(char leftbracket_, real low_, real high_, char rightbracket_): 00068 low(low_), high(high_), leftbracket(leftbracket_), rightbracket(rightbracket_) 00069 { checkbrackets(); } 00070 00071 real span(){return abs(high-low);} 00072 00073 void checkbrackets() const 00074 { 00075 if( (leftbracket!='[' && leftbracket!=']') || (rightbracket!='[' && rightbracket!=']') ) 00076 PLERROR("In RealRange: Brackets must be either '[' or ']'"); 00077 } 00078 00079 void print(ostream& out) const 00080 { out << leftbracket << low << ' ' << high << rightbracket; } 00081 00082 void write(ostream& out) const 00083 { out << leftbracket << low << ' ' << high << rightbracket; } 00084 00085 void read(istream& in) 00086 { in >> leftbracket >> low >> high >> rightbracket; checkbrackets(); } 00087 00088 00089 string getString() const; 00090 00096 bool contains(real val) const; 00097 bool operator<(real x) const; 00098 bool operator>(real x) const; 00099 00100 /* inline bool operator<(real x) const 00101 { return low < x || high == x && rightbracket == '['; } 00102 00103 inline bool operator>(real x) const 00104 { return low > x || low == x && leftbracket == ']'; } 00105 00106 */ 00111 bool operator<(const RealRange& x) const; 00112 bool operator>(const RealRange& x) const; 00113 bool operator==(const RealRange& rr) const; 00114 }; 00115 00116 inline void write(ostream& out, const RealRange& range) { range.write(out); } 00117 inline ostream& operator<<(ostream& out, const RealRange& range) { range.print(out); return out; } 00118 inline void read(istream& in, RealRange& range) { range.read(in); } 00119 00120 PStream& operator<<(PStream& out, const RealRange& x); 00121 PStream& operator>>(PStream& in, RealRange &x); 00122 00123 00124 class RealMapping: public Object 00125 { 00126 protected: 00127 static void declareOptions(OptionList& ol); 00128 00129 public: 00130 typedef pair<RealRange, real> single_mapping_t; 00131 typedef TVec< single_mapping_t > ordered_mapping_t; 00132 typedef map<RealRange, real> mapping_t; 00133 typedef mapping_t::iterator iterator; 00134 typedef mapping_t::const_iterator const_iterator; 00135 mapping_t mapping; // defines mapping from real ranges to values 00136 // o_mapping contains the same mappings as 'mapping', but they are 00137 // ordered so that the lower limits of ranges are in ascending order 00138 // NOTE : before any access, it must be created with a call to buildOrderedMapping() 00139 ordered_mapping_t o_mapping; 00140 real missing_mapsto; // value to which to map missing values (can be missing value) 00141 bool keep_other_as_is; // if true, values not in mapping are left as is, otherwise they're mappred to other_mapsto 00142 real other_mapsto; // value to which to map values not inmapping, if keep_other_as_is is false 00143 00144 public: 00145 typedef Object inherited; 00146 PLEARN_DECLARE_OBJECT(RealMapping); 00147 00148 RealMapping() 00149 :missing_mapsto(MISSING_VALUE), 00150 keep_other_as_is(true), 00151 other_mapsto(MISSING_VALUE) 00152 {} 00153 00154 int size() const { return (int)mapping.size(); } 00155 int length() const { return (int)mapping.size(); } 00156 00158 inline void clear() { mapping.clear(); } 00159 00160 void buildOrderedMapping(); 00161 00162 bool checkConsistency(); 00163 00164 void removeMapping(const RealRange& range) 00165 { 00166 mapping_t::iterator it= mapping.find(range); 00167 if(it != mapping.end()) 00168 mapping.erase(it); 00169 else 00170 PLWARNING("In RealMapping::removeMapping mapping not removed: does not exist."); 00171 } 00172 00173 void removeMapping(real x) //remove range where x falls 00174 { 00175 mapping_t::iterator it= mapping.lower_bound(RealRange('[',x,x,']')); 00176 if(it != mapping.end() && it->first.contains(x)) 00177 mapping.erase(it); 00178 else 00179 PLWARNING("In RealMapping::removeMapping mapping not removed: does not exist."); 00180 } 00181 00182 void addMapping(const RealRange& range, real val); 00183 00185 void setMappingForMissing(real what_missing_mapsto) 00186 { missing_mapsto = what_missing_mapsto; } 00187 00189 void setMappingForOther(real what_other_mapsto) 00190 { keep_other_as_is=false; other_mapsto = what_other_mapsto; } 00191 00192 void keepOtherAsIs() 00193 { keep_other_as_is=true; } 00194 00195 // returns the mapped value corresponding to val 00196 real map(real val) const; 00197 00198 // returns the number of the bin in which 'val' falls 00199 int binnumber(real val) const; 00200 00201 // transforms v by applying the mapping on all its elements 00202 void transform(const Vec& v) const; 00203 00204 pair<RealRange, real> lastMapping() 00205 { return *(mapping.rbegin()); } 00206 // { return mapping.lastElement(); } 00207 00208 /*** 00209 * map methods "forwarded" 00210 */ 00211 00212 iterator begin() 00213 { return mapping.begin(); } 00214 const_iterator begin() const 00215 { return mapping.begin(); } 00216 iterator end() 00217 { return mapping.end(); } 00218 const_iterator end() const 00219 { return mapping.end(); } 00220 void erase(iterator it) 00221 { mapping.erase(it); } 00222 00223 bool operator==(const RealMapping& rm) const; 00224 00225 virtual void print(ostream& out) const; 00226 virtual void write(ostream& out) const; 00227 virtual void read(istream& in); 00228 00229 real maxMappedToValue(); 00230 00233 Vec getCutPoints() const; 00234 00235 }; 00236 00237 DECLARE_OBJECT_PTR(RealMapping); 00238 00239 } // end of namespace PLearn 00240 00241 #endif

Generated on Tue Aug 17 16:03:32 2004 for PLearn by doxygen 1.3.7