1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
// Copyright (C) 2003--2004 Zbigniew Leyk (zbigniew.leyk@anu.edu.au)
// and David E. Stewart (david.stewart@anu.edu.au)
// and Ronan Collobert (collober@idiap.ch)
//
// This file is part of Torch 3.1.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Perm_operations.h"
#include "mx_lu_factor.h"
#include "mx_low_level.h"
#include "mx_solve.h"
namespace Torch {
/*
* Most matrix factorisation routines are in-situ unless otherwise specified
*/
/*
* LUfactor -- gaussian elimination with scaled partial pivoting -- Note:
* returns LU matrix which is A
*/
void mxLUFactor(Mat * mat, Perm * pivot)
{
int m, n;
int i_max;
real **mat_v, *mat_piv, *mat_row;
real max1, temp, tiny;
Vec *scale = new Vec(mat->m);
m = mat->m;
n = mat->n;
mat_v = mat->ptr;
tiny = 10.0 / INF;
/*
* initialise pivot with identity permutation
*/
for (int i = 0; i < m; i++)
pivot->ptr[i] = i;
/*
* set scale parameters
*/
for (int i = 0; i < m; i++)
{
max1 = 0.0;
for (int j = 0; j < n; j++)
{
temp = fabs(mat_v[i][j]);
if (max1 < temp)
max1 = temp;
}
scale->ptr[i] = max1;
}
/*
* main loop
*/
int k_max = (m < n ? m : n) - 1;
for (int k = 0; k < k_max; k++)
{
/*
* find best pivot row
*/
max1 = 0.0;
i_max = -1;
for (int i = k; i < m; i++)
{
if (fabs(scale->ptr[i]) >= tiny * fabs(mat_v[i][k]))
{
temp = fabs(mat_v[i][k]) / scale->ptr[i];
if (temp > max1)
{
max1 = temp;
i_max = i;
}
}
}
/*
* if no pivot then ignore column k...
*/
if (i_max == -1)
{
/*
* set pivot entry mat[k][k] exactly to zero, rather than just
* "small"
*/
mat_v[k][k] = 0.0;
continue;
}
/*
* do we pivot ?
*/
if (i_max != k)
{ /*
* yes we do...
*/
mxTrPerm(pivot, i_max, k);
for (int j = 0; j < n; j++)
{
temp = mat_v[i_max][j];
mat_v[i_max][j] = mat_v[k][j];
mat_v[k][j] = temp;
}
}
/*
* row operations
*/
for (int i = k + 1; i < m; i++)
{ /*
* for each row do...
*//*
* Note: divide by zero should never happen
*/
temp = mat_v[i][k] = mat_v[i][k] / mat_v[k][k];
mat_piv = &(mat_v[k][k + 1]);
mat_row = &(mat_v[i][k + 1]);
if (k + 1 < n)
mxRealMulAdd__(mat_row, mat_piv, -temp, n - (k + 1));
}
}
delete scale;
}
/*
* LUsolve -- given an LU factorisation in A, solve Ax=b
*/
void mxLUSolve(Mat * mat, Perm * pivot, Vec * b, Vec * x)
{
// x := P.b
mxPermVec(pivot, b, x);
// implicit diagonal = 1
mxLSolve(mat, x, x, 1.0);
// explicit diagonal
mxUSolve(mat, x, x, 0.0);
}
/*
* LUTsolve -- given an LU factorisation in A, solve A^T.x=b
*/
void mxLUTSolve(Mat * mat, Perm * pivot, Vec * b, Vec * x)
{
x->copy(b);
// explicit diagonal
mxUTSolve(mat, x, x, 0.0);
// implicit diagonal = 1
mxLTSolve(mat, x, x, 1.0);
// x := P^T.tmp
mxPermInvVec(pivot, x, x);
}
/*
* m_inverse -- returns inverse of A, provided A is not too rank deficient --
* uses LU factorisation
*/
void mxInverse(Mat * mat, Mat * out)
{
// That's me...
Mat *mat_cp = new Mat(mat->m, mat->n);
Vec *tmp = new Vec(mat->m);
Vec *tmp2 = new Vec(mat->m);
Perm *pivot = new Perm(mat->m);
mat_cp->copy(mat);
mxLUFactor(mat_cp, pivot);
for (int i = 0; i < mat->n; i++)
{
tmp->zero();
tmp->ptr[i] = 1.0;
mxLUSolve(mat_cp, pivot, tmp, tmp2);
out->setCol(i, tmp2);
}
delete mat_cp;
delete tmp;
delete tmp2;
delete pivot;
}
/*
* LUcondest -- returns an estimate of the condition number of LU given the
* LU factorisation in compact form
*/
real mxLUCondest(Mat * mat, Perm * pivot)
{
real cond_est, L_norm, U_norm, sum, tiny;
int n = mat->n;
Vec *y = new Vec(n);
Vec *z = new Vec(n);
tiny = 10.0 / INF;
for (int i = 0; i < n; i++)
{
sum = 0.0;
for (int j = 0; j < i; j++)
sum -= mat->ptr[j][i] * y->ptr[j];
sum -= (sum < 0.0) ? 1.0 : -1.0;
if (fabs(mat->ptr[i][i]) <= tiny * fabs(sum))
{
delete y;
delete z;
return INF;
}
y->ptr[i] = sum / mat->ptr[i][i];
}
mxLTSolve(mat, y, y, 1.0);
mxLUSolve(mat, pivot, y, z);
/*
* now estimate norm of A (even though it is not directly available)
*/
/*
* actually computes ||L||_inf.||U||_inf
*/
U_norm = 0.0;
for (int i = 0; i < n; i++)
{
sum = 0.0;
for (int j = i; j < n; j++)
sum += fabs(mat->ptr[i][j]);
if (sum > U_norm)
U_norm = sum;
}
L_norm = 0.0;
for (int i = 0; i < n; i++)
{
sum = 1.0;
for (int j = 0; j < i; j++)
sum += fabs(mat->ptr[i][j]);
if (sum > L_norm)
L_norm = sum;
}
cond_est = U_norm * L_norm * z->normInf() / y->normInf();
delete y;
delete z;
return cond_est;
}
/*
Given #A# and #b#, solve #A.x=b# */
void mxSolve(Mat *mat, Vec *b, Vec *x)
{
Perm *pivot = new Perm(mat->m);
mxLUFactor(mat, pivot);
mxLUSolve(mat, pivot, b, x);
delete pivot;
}
}
|