00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00053 #define mat_textra_CXX "mat_textra.cxx"
00054 const static char *rcs_id = mat_textra_CXX "$Revision: 1.6 $";
00055
00056
00057 #define __STDC_LIMIT_MACROS
00058 #include <stdint.h>
00059 #include "lnopt_main.h"
00060 #include "mat.h"
00061 #include "lu_mat.h"
00062
00063
00064
00065 template <> MEM_POOL* MAT<mINT32>::_default_pool = NULL;
00066 template <> MEM_POOL* MAT<FRAC>::_default_pool = NULL;
00067 template <> MEM_POOL* MAT<double>::_default_pool = NULL;
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 #if 0
00079
00080 IMAT MAT<mINT32>::Inv() const
00081 {
00082 FmtAssert(_r == _c, ("Matrix not square"));
00083
00084 MEM_POOL* hold = FMAT::Set_Default_Pool(IMAT::Default_Pool());
00085 FMAT fmat = IMAT_to_FMAT(*this);
00086 fmat.D_Inv();
00087 FMAT::Set_Default_Pool(hold);
00088 return FMAT_to_IMAT(fmat, IMAT::Default_Pool());
00089 }
00090
00091 #else
00092
00093 template<>
00094 IMAT MAT<mINT32>::Inv() const
00095 {
00096 FmtAssert(_r == _c, ("Matrix not square"));
00097
00098 MEM_POOL* hold = DMAT::Set_Default_Pool(IMAT::Default_Pool());
00099 DMAT dmat = IMAT_to_DMAT(*this, IMAT::Default_Pool());
00100 dmat.D_Inv();
00101 DMAT::Set_Default_Pool(hold);
00102 return DMAT_to_IMAT(dmat, IMAT::Default_Pool());
00103 }
00104
00105 #endif
00106
00107
00108
00109 template<>
00110 FMAT MAT<FRAC>::Inv() const
00111 {
00112 FmtAssert(_r == _c, ("FMAT::Inv(): Matrix not square"));
00113 LU_FMAT lu(*this, FMAT::Default_Pool());
00114 return lu.Inv();
00115 }
00116
00117
00118
00119 template<>
00120 DMAT MAT<double>::Inv() const
00121 {
00122 FmtAssert(_r == _c, ("Matrix not square"));
00123 LU_DMAT lu(*this, DMAT::Default_Pool());
00124 return lu.Inv();
00125 }
00126
00127 template<>
00128 void DMAT::Print_Element(FILE* f, double e)
00129 {
00130 fprintf(f, "%g", e);
00131 }
00132
00133 template<>
00134 void IMAT::Print_Element(FILE* f, mINT32 e)
00135 {
00136 fprintf(f, "%d", e);
00137 }
00138
00139 template<>
00140 void FMAT::Print_Element(FILE* f, FRAC e)
00141 {
00142 e.Print(f);
00143 }
00144
00145
00146
00147 IMAT FMAT_to_IMAT(const FMAT& a, MEM_POOL* pool)
00148 {
00149 Is_True(IMAT::Default_Pool(), ("Missing default pool for IMAT"));
00150
00151 IMAT x(a.Rows(), a.Cols(), pool);
00152
00153 for (INT32 r = 0; r < a.Rows(); r++)
00154 for (INT32 c = 0; c < a.Cols(); c++)
00155 x(r,c) = a(r,c).Integer();
00156
00157 return x;
00158 }
00159
00160 IMAT DMAT_to_IMAT(const DMAT& a, MEM_POOL* pool)
00161 {
00162 Is_True(IMAT::Default_Pool(), ("Missing default pool for IMAT"));
00163
00164 IMAT x(a.Rows(), a.Cols(), pool);
00165
00166 for (INT32 r = 0; r < a.Rows(); r++) {
00167 for (INT32 c = 0; c < a.Cols(); c++) {
00168 double d = a(r, c);
00169 BOOL neg = (d < 0);
00170 if (neg) d = -d;
00171 INT32 i = INT32(d+0.5);
00172 FmtAssert(d-i < 1e-10 && d-i > -1e-10, ("Bad floating inverse"));
00173 x(r,c) = neg ? -i : i;
00174 }
00175 }
00176
00177 return x;
00178 }
00179
00180 DMAT IMAT_to_DMAT(const IMAT& a, MEM_POOL* pool)
00181 {
00182 Is_True(DMAT::Default_Pool(), ("Missing default pool for DMAT"));
00183
00184 DMAT x(a.Rows(), a.Cols(), pool);
00185
00186 for (INT32 r = 0; r < a.Rows(); r++)
00187 for (INT32 c = 0; c < a.Cols(); c++)
00188 x(r,c) = double(a(r,c));
00189
00190 return x;
00191 }
00192
00193 FMAT IMAT_to_FMAT(const IMAT& a, MEM_POOL* pool)
00194 {
00195 Is_True(FMAT::Default_Pool(), ("Missing default pool for FMAT"));
00196
00197 FMAT x(a.Rows(), a.Cols(), pool);
00198
00199 for (INT32 r = 0; r < a.Rows(); r++)
00200 for (INT32 c = 0; c < a.Cols(); c++)
00201 x(r,c) = FRAC(a(r,c));
00202
00203 return x;
00204 }
00205