00001 /* 00002 00003 Copyright (c) 2003, Cornell University 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 - Redistributions of source code must retain the above copyright notice, 00010 this list of conditions and the following disclaimer. 00011 - Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 - Neither the name of Cornell University nor the names of its 00015 contributors may be used to endorse or promote products derived from 00016 this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00028 THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 */ 00031 00032 #if !defined(_NORMALIZ_H) 00033 #define _NORMALIZ_H 00034 00035 #include <math.h> 00036 #include <string> 00037 #include "general.h" 00038 #include "exceptions.h" 00039 00040 namespace CLUS 00041 { 00042 00043 /** The following structure is used for scaling the inputs and the outputs 00044 newVal=adit+mult*oldVal. 00045 The input values are scaled by the data generator when identification is 00046 in progres and by inference function when we use it. 00047 This applies also to output variables but infer() treats diferently input 00048 and output values and the data genetator that contained the training 00049 data and which computes the scaling factors must take in consideration 00050 this thing (input scaling computations are diferent from output ones). 00051 */ 00052 class Scale 00053 { 00054 00055 private: 00056 double adit; 00057 double mult; 00058 00059 public: 00060 enum NormType { Interval, Distribution }; 00061 NormType type; 00062 00063 Scale(Real Ad=0.0, Real Mu=1.0, NormType Type=Interval):adit(Ad),mult(Mu),type(Type) 00064 { } 00065 00066 inline void SetCoefForInput(double arg1, double arg2, NormType Type) 00067 { 00068 type=Type; 00069 00070 double sigma; 00071 00072 // can replace the if with case if more than two types 00073 if (type==Interval) 00074 { 00075 // arg1=Min, arg2=Max 00076 /*adit=-arg1/(arg2-arg1); 00077 mult=1/(arg2-arg1); */ 00078 // normalize to [-1:1] 00079 adit=-(arg1+arg2)/(arg2-arg1); 00080 mult=2.0/(arg2-arg1); 00081 } 00082 else 00083 { 00084 // arg1=Med, arg2=Med2 00085 sigma=sqrt(arg2-pow2(arg1)); 00086 adit=-arg1/sigma; 00087 mult=1/sigma; 00088 } 00089 } 00090 00091 inline void SetCoefForOutput(double arg1, double arg2, NormType Type) 00092 { 00093 type=Type; 00094 double sigma; 00095 00096 // can replace the if with case if more than two types 00097 if (type==Interval) 00098 { 00099 // arg1=Min, arg2=Max 00100 /*adit=arg1; 00101 mult=(arg2-arg1);*/ 00102 adit=(arg1+arg2)/2.0; 00103 mult=arg2-adit; 00104 } 00105 else 00106 { 00107 // arg1=Med, arg2=Med2 00108 sigma=sqrt(arg2-pow2(arg1)); 00109 adit=arg1; 00110 mult=sigma; 00111 } 00112 } 00113 00114 double Transform(double data) const 00115 { 00116 return data*mult+adit; 00117 } 00118 00119 double InverseTransform (double data) const 00120 { 00121 return (data-adit)/mult; 00122 } 00123 00124 inline void SaveToStream(ostream& out) 00125 { 00126 out << "[ " << adit << " " << mult << " ] "; 00127 } 00128 00129 inline void LoadFromStream(istream& in) 00130 { 00131 string s; 00132 in >> s; 00133 if (s!="[") 00134 throw ErrMsg("[ missing"); 00135 00136 in >> adit >> mult; 00137 in >> s; 00138 if (s!="]") 00139 throw ErrMsg("] missing)"); 00140 } 00141 }; 00142 00143 } 00144 00145 #endif /* _NORMALIZ_H */