doom3-bfg/neo/idlib/math/MatX.cpp

6149 lines
115 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
Copyright (C) 2012 Robert Beckebans
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../precompiled.h"
//===============================================================
//
// idMatX
//
//===============================================================
float idMatX::temp[MATX_MAX_TEMP + 4];
// RB: changed int to intptr_t
float* idMatX::tempPtr = ( float* )( ( ( intptr_t ) idMatX::temp + 15 ) & ~15 );
// RB end
2012-11-26 18:58:24 +00:00
int idMatX::tempIndex = 0;
/*
============
idMatX::ChangeSize
============
*/
void idMatX::ChangeSize( int rows, int columns, bool makeZero )
{
2012-11-26 18:58:24 +00:00
int alloc = ( rows * columns + 3 ) & ~3;
if( alloc > alloced && alloced != -1 )
{
float* oldMat = mat;
mat = ( float* ) Mem_Alloc16( alloc * sizeof( float ), TAG_MATH );
if( makeZero )
{
2012-11-26 18:58:24 +00:00
memset( mat, 0, alloc * sizeof( float ) );
}
alloced = alloc;
if( oldMat )
{
2012-11-26 18:58:24 +00:00
int minRow = Min( numRows, rows );
int minColumn = Min( numColumns, columns );
for( int i = 0; i < minRow; i++ )
{
for( int j = 0; j < minColumn; j++ )
{
2012-11-26 18:58:24 +00:00
mat[ i * columns + j ] = oldMat[ i * numColumns + j ];
}
}
Mem_Free16( oldMat );
}
}
else
{
if( columns < numColumns )
{
2012-11-26 18:58:24 +00:00
int minRow = Min( numRows, rows );
for( int i = 0; i < minRow; i++ )
{
for( int j = 0; j < columns; j++ )
{
2012-11-26 18:58:24 +00:00
mat[ i * columns + j ] = mat[ i * numColumns + j ];
}
}
}
else if( columns > numColumns )
{
for( int i = Min( numRows, rows ) - 1; i >= 0; i-- )
{
if( makeZero )
{
for( int j = columns - 1; j >= numColumns; j-- )
{
2012-11-26 18:58:24 +00:00
mat[ i * columns + j ] = 0.0f;
}
}
for( int j = numColumns - 1; j >= 0; j-- )
{
2012-11-26 18:58:24 +00:00
mat[ i * columns + j ] = mat[ i * numColumns + j ];
}
}
}
if( makeZero && rows > numRows )
{
2012-11-26 18:58:24 +00:00
memset( mat + numRows * columns, 0, ( rows - numRows ) * columns * sizeof( float ) );
}
}
numRows = rows;
numColumns = columns;
MATX_CLEAREND();
}
/*
============
idMatX::RemoveRow
============
*/
idMatX& idMatX::RemoveRow( int r )
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
assert( r < numRows );
2012-11-26 18:58:24 +00:00
numRows--;
for( i = r; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
memcpy( &mat[i * numColumns], &mat[( i + 1 ) * numColumns], numColumns * sizeof( float ) );
}
2012-11-26 18:58:24 +00:00
return *this;
}
/*
============
idMatX::RemoveColumn
============
*/
idMatX& idMatX::RemoveColumn( int r )
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
assert( r < numColumns );
2012-11-26 18:58:24 +00:00
numColumns--;
for( i = 0; i < numRows - 1; i++ )
{
2012-11-26 18:58:24 +00:00
memmove( &mat[i * numColumns + r], &mat[i * ( numColumns + 1 ) + r + 1], numColumns * sizeof( float ) );
}
memmove( &mat[i * numColumns + r], &mat[i * ( numColumns + 1 ) + r + 1], ( numColumns - r ) * sizeof( float ) );
2012-11-26 18:58:24 +00:00
return *this;
}
/*
============
idMatX::RemoveRowColumn
============
*/
idMatX& idMatX::RemoveRowColumn( int r )
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
assert( r < numRows && r < numColumns );
2012-11-26 18:58:24 +00:00
numRows--;
numColumns--;
if( r > 0 )
{
for( i = 0; i < r - 1; i++ )
{
2012-11-26 18:58:24 +00:00
memmove( &mat[i * numColumns + r], &mat[i * ( numColumns + 1 ) + r + 1], numColumns * sizeof( float ) );
}
memmove( &mat[i * numColumns + r], &mat[i * ( numColumns + 1 ) + r + 1], ( numColumns - r ) * sizeof( float ) );
}
2012-11-26 18:58:24 +00:00
memcpy( &mat[r * numColumns], &mat[( r + 1 ) * ( numColumns + 1 )], r * sizeof( float ) );
for( i = r; i < numRows - 1; i++ )
{
2012-11-26 18:58:24 +00:00
memcpy( &mat[i * numColumns + r], &mat[( i + 1 ) * ( numColumns + 1 ) + r + 1], numColumns * sizeof( float ) );
}
memcpy( &mat[i * numColumns + r], &mat[( i + 1 ) * ( numColumns + 1 ) + r + 1], ( numColumns - r ) * sizeof( float ) );
2012-11-26 18:58:24 +00:00
return *this;
}
/*
========================
idMatX::CopyLowerToUpperTriangle
========================
*/
void idMatX::CopyLowerToUpperTriangle()
{
2012-11-26 18:58:24 +00:00
assert( ( GetNumColumns() & 3 ) == 0 );
assert( GetNumColumns() >= GetNumRows() );
2012-11-26 18:58:24 +00:00
const int n = GetNumColumns();
const int m = GetNumRows();
2012-11-26 18:58:24 +00:00
const int n0 = 0;
const int n1 = n;
const int n2 = ( n << 1 );
const int n3 = ( n << 1 ) + n;
const int n4 = ( n << 2 );
2012-11-26 18:58:24 +00:00
const int b1 = ( ( m - 0 ) >> 1 ) & 1; // ( m & 3 ) > 1
const int b2 = ( ( m - 1 ) >> 1 ) & 1; // ( m & 3 ) > 2 (provided ( m & 3 ) > 0)
2012-11-26 18:58:24 +00:00
const int n1_masked = ( n & -b1 );
const int n2_masked = ( n & -b1 ) + ( n & -b2 );
const __m128 mask0 = __m128c( _mm_set_epi32( 0, 0, 0, -1 ) );
const __m128 mask1 = __m128c( _mm_set_epi32( 0, 0, -1, -1 ) );
const __m128 mask2 = __m128c( _mm_set_epi32( 0, -1, -1, -1 ) );
2012-11-26 18:58:24 +00:00
const __m128 mask3 = __m128c( _mm_set_epi32( -1, -1, -1, -1 ) );
2012-11-26 18:58:24 +00:00
const __m128 bottomMask[2] = { __m128c( _mm_set1_epi32( 0 ) ), __m128c( _mm_set1_epi32( -1 ) ) };
float* __restrict basePtr = ToFloatPtr();
for( int i = 0; i < m - 3; i += 4 )
{
2012-11-26 18:58:24 +00:00
// copy top left diagonal 4x4 block elements
__m128 r0 = _mm_and_ps( _mm_load_ps( basePtr + n0 ), mask0 );
__m128 r1 = _mm_and_ps( _mm_load_ps( basePtr + n1 ), mask1 );
__m128 r2 = _mm_and_ps( _mm_load_ps( basePtr + n2 ), mask2 );
__m128 r3 = _mm_and_ps( _mm_load_ps( basePtr + n3 ), mask3 );
2012-11-26 18:58:24 +00:00
__m128 t0 = _mm_unpacklo_ps( r0, r2 ); // x0, z0, x1, z1
__m128 t1 = _mm_unpackhi_ps( r0, r2 ); // x2, z2, x3, z3
__m128 t2 = _mm_unpacklo_ps( r1, r3 ); // y0, w0, y1, w1
__m128 t3 = _mm_unpackhi_ps( r1, r3 ); // y2, w2, y3, w3
2012-11-26 18:58:24 +00:00
__m128 s0 = _mm_unpacklo_ps( t0, t2 ); // x0, y0, z0, w0
__m128 s1 = _mm_unpackhi_ps( t0, t2 ); // x1, y1, z1, w1
__m128 s2 = _mm_unpacklo_ps( t1, t3 ); // x2, y2, z2, w2
__m128 s3 = _mm_unpackhi_ps( t1, t3 ); // x3, y3, z3, w3
2012-11-26 18:58:24 +00:00
r0 = _mm_or_ps( r0, s0 );
r1 = _mm_or_ps( r1, s1 );
r2 = _mm_or_ps( r2, s2 );
r3 = _mm_or_ps( r3, s3 );
2012-11-26 18:58:24 +00:00
_mm_store_ps( basePtr + n0, r0 );
_mm_store_ps( basePtr + n1, r1 );
_mm_store_ps( basePtr + n2, r2 );
_mm_store_ps( basePtr + n3, r3 );
2012-11-26 18:58:24 +00:00
// copy one column of 4x4 blocks to one row of 4x4 blocks
const float* __restrict srcPtr = basePtr;
float* __restrict dstPtr = basePtr;
for( int j = i + 4; j < m - 3; j += 4 )
{
2012-11-26 18:58:24 +00:00
srcPtr += n4;
dstPtr += 4;
2012-11-26 18:58:24 +00:00
__m128 r0 = _mm_load_ps( srcPtr + n0 );
__m128 r1 = _mm_load_ps( srcPtr + n1 );
__m128 r2 = _mm_load_ps( srcPtr + n2 );
__m128 r3 = _mm_load_ps( srcPtr + n3 );
2012-11-26 18:58:24 +00:00
__m128 t0 = _mm_unpacklo_ps( r0, r2 ); // x0, z0, x1, z1
__m128 t1 = _mm_unpackhi_ps( r0, r2 ); // x2, z2, x3, z3
__m128 t2 = _mm_unpacklo_ps( r1, r3 ); // y0, w0, y1, w1
__m128 t3 = _mm_unpackhi_ps( r1, r3 ); // y2, w2, y3, w3
2012-11-26 18:58:24 +00:00
r0 = _mm_unpacklo_ps( t0, t2 ); // x0, y0, z0, w0
r1 = _mm_unpackhi_ps( t0, t2 ); // x1, y1, z1, w1
r2 = _mm_unpacklo_ps( t1, t3 ); // x2, y2, z2, w2
r3 = _mm_unpackhi_ps( t1, t3 ); // x3, y3, z3, w3
2012-11-26 18:58:24 +00:00
_mm_store_ps( dstPtr + n0, r0 );
_mm_store_ps( dstPtr + n1, r1 );
_mm_store_ps( dstPtr + n2, r2 );
_mm_store_ps( dstPtr + n3, r3 );
}
2012-11-26 18:58:24 +00:00
// copy the last partial 4x4 block elements
if( m & 3 )
{
2012-11-26 18:58:24 +00:00
srcPtr += n4;
dstPtr += 4;
2012-11-26 18:58:24 +00:00
__m128 r0 = _mm_load_ps( srcPtr + n0 );
__m128 r1 = _mm_and_ps( _mm_load_ps( srcPtr + n1_masked ), bottomMask[b1] );
__m128 r2 = _mm_and_ps( _mm_load_ps( srcPtr + n2_masked ), bottomMask[b2] );
__m128 r3 = _mm_setzero_ps();
2012-11-26 18:58:24 +00:00
__m128 t0 = _mm_unpacklo_ps( r0, r2 ); // x0, z0, x1, z1
__m128 t1 = _mm_unpackhi_ps( r0, r2 ); // x2, z2, x3, z3
__m128 t2 = _mm_unpacklo_ps( r1, r3 ); // y0, w0, y1, w1
__m128 t3 = _mm_unpackhi_ps( r1, r3 ); // y2, w2, y3, w3
2012-11-26 18:58:24 +00:00
r0 = _mm_unpacklo_ps( t0, t2 ); // x0, y0, z0, w0
r1 = _mm_unpackhi_ps( t0, t2 ); // x1, y1, z1, w1
r2 = _mm_unpacklo_ps( t1, t3 ); // x2, y2, z2, w2
r3 = _mm_unpackhi_ps( t1, t3 ); // x3, y3, z3, w3
2012-11-26 18:58:24 +00:00
_mm_store_ps( dstPtr + n0, r0 );
_mm_store_ps( dstPtr + n1, r1 );
_mm_store_ps( dstPtr + n2, r2 );
_mm_store_ps( dstPtr + n3, r3 );
}
2012-11-26 18:58:24 +00:00
basePtr += n4 + 4;
}
2012-11-26 18:58:24 +00:00
// copy the lower right partial diagonal 4x4 block elements
if( m & 3 )
{
2012-11-26 18:58:24 +00:00
__m128 r0 = _mm_and_ps( _mm_load_ps( basePtr + n0 ), mask0 );
__m128 r1 = _mm_and_ps( _mm_load_ps( basePtr + n1_masked ), _mm_and_ps( mask1, bottomMask[b1] ) );
__m128 r2 = _mm_and_ps( _mm_load_ps( basePtr + n2_masked ), _mm_and_ps( mask2, bottomMask[b2] ) );
__m128 r3 = _mm_setzero_ps();
2012-11-26 18:58:24 +00:00
__m128 t0 = _mm_unpacklo_ps( r0, r2 ); // x0, z0, x1, z1
__m128 t1 = _mm_unpackhi_ps( r0, r2 ); // x2, z2, x3, z3
__m128 t2 = _mm_unpacklo_ps( r1, r3 ); // y0, w0, y1, w1
__m128 t3 = _mm_unpackhi_ps( r1, r3 ); // y2, w2, y3, w3
2012-11-26 18:58:24 +00:00
__m128 s0 = _mm_unpacklo_ps( t0, t2 ); // x0, y0, z0, w0
__m128 s1 = _mm_unpackhi_ps( t0, t2 ); // x1, y1, z1, w1
__m128 s2 = _mm_unpacklo_ps( t1, t3 ); // x2, y2, z2, w2
2012-11-26 18:58:24 +00:00
r0 = _mm_or_ps( r0, s0 );
r1 = _mm_or_ps( r1, s1 );
r2 = _mm_or_ps( r2, s2 );
2012-11-26 18:58:24 +00:00
_mm_store_ps( basePtr + n2_masked, r2 );
_mm_store_ps( basePtr + n1_masked, r1 );
_mm_store_ps( basePtr + n0, r0 );
}
2012-11-26 18:58:24 +00:00
#ifdef _DEBUG
for( int i = 0; i < numRows; i++ )
{
for( int j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
assert( mat[ i * numColumns + j ] == mat[ j * numColumns + i ] );
}
}
#endif
}
/*
============
idMatX::IsOrthogonal
returns true if (*this) * this->Transpose() == Identity
============
*/
bool idMatX::IsOrthogonal( const float epsilon ) const
{
float* ptr1, *ptr2, sum;
if( !IsSquare() )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
ptr1 = mat;
for( int i = 0; i < numRows; i++ )
{
for( int j = 0; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
ptr2 = mat + j;
sum = ptr1[0] * ptr2[0] - ( float )( i == j );
for( int n = 1; n < numColumns; n++ )
{
2012-11-26 18:58:24 +00:00
ptr2 += numColumns;
sum += ptr1[n] * ptr2[0];
}
if( idMath::Fabs( sum ) > epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
ptr1 += numColumns;
}
return true;
}
/*
============
idMatX::IsOrthonormal
returns true if (*this) * this->Transpose() == Identity and the length of each column vector is 1
============
*/
bool idMatX::IsOrthonormal( const float epsilon ) const
{
float* ptr1, *ptr2, sum;
if( !IsSquare() )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
ptr1 = mat;
for( int i = 0; i < numRows; i++ )
{
for( int j = 0; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
ptr2 = mat + j;
sum = ptr1[0] * ptr2[0] - ( float )( i == j );
for( int n = 1; n < numColumns; n++ )
{
2012-11-26 18:58:24 +00:00
ptr2 += numColumns;
sum += ptr1[n] * ptr2[0];
}
if( idMath::Fabs( sum ) > epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
ptr1 += numColumns;
2012-11-26 18:58:24 +00:00
ptr2 = mat + i;
sum = ptr2[0] * ptr2[0] - 1.0f;
for( int j = 1; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
ptr2 += numColumns;
sum += ptr2[j] * ptr2[j];
}
if( idMath::Fabs( sum ) > epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
return true;
}
/*
============
idMatX::IsPMatrix
returns true if the matrix is a P-matrix
A square matrix is a P-matrix if all its principal minors are positive.
============
*/
bool idMatX::IsPMatrix( const float epsilon ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
float d;
idMatX m;
if( !IsSquare() )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( numRows <= 0 )
{
2012-11-26 18:58:24 +00:00
return true;
}
if( ( *this )[0][0] <= epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( numRows <= 1 )
{
2012-11-26 18:58:24 +00:00
return true;
}
2012-11-26 18:58:24 +00:00
m.SetData( numRows - 1, numColumns - 1, MATX_ALLOCA( ( numRows - 1 ) * ( numColumns - 1 ) ) );
for( i = 1; i < numRows; i++ )
{
for( j = 1; j < numColumns; j++ )
{
m[i - 1][j - 1] = ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
}
if( !m.IsPMatrix( epsilon ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 1; i < numRows; i++ )
{
d = ( *this )[i][0] / ( *this )[0][0];
for( j = 1; j < numColumns; j++ )
{
m[i - 1][j - 1] = ( *this )[i][j] - d * ( *this )[0][j];
2012-11-26 18:58:24 +00:00
}
}
if( !m.IsPMatrix( epsilon ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::IsZMatrix
returns true if the matrix is a Z-matrix
A square matrix M is a Z-matrix if M[i][j] <= 0 for all i != j.
============
*/
bool idMatX::IsZMatrix( const float epsilon ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
if( !IsSquare() )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < numColumns; j++ )
{
if( ( *this )[i][j] > epsilon && i != j )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
}
return true;
}
/*
============
idMatX::IsPositiveDefinite
returns true if the matrix is Positive Definite (PD)
A square matrix M of order n is said to be PD if y'My > 0 for all vectors y of dimension n, y != 0.
============
*/
bool idMatX::IsPositiveDefinite( const float epsilon ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k;
float d, s;
idMatX m;
2012-11-26 18:58:24 +00:00
// the matrix must be square
if( !IsSquare() )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// copy matrix
m.SetData( numRows, numColumns, MATX_ALLOCA( numRows * numColumns ) );
m = *this;
2012-11-26 18:58:24 +00:00
// add transpose
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < numColumns; j++ )
{
m[i][j] += ( *this )[j][i];
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// test Positive Definiteness with Gaussian pivot steps
for( i = 0; i < numRows; i++ )
{
for( j = i; j < numColumns; j++ )
{
if( m[j][j] <= epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
2012-11-26 18:58:24 +00:00
d = 1.0f / m[i][i];
for( j = i + 1; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
s = d * m[j][i];
m[j][i] = 0.0f;
for( k = i + 1; k < numRows; k++ )
{
2012-11-26 18:58:24 +00:00
m[j][k] -= s * m[i][k];
}
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::IsSymmetricPositiveDefinite
returns true if the matrix is Symmetric Positive Definite (PD)
============
*/
bool idMatX::IsSymmetricPositiveDefinite( const float epsilon ) const
{
2012-11-26 18:58:24 +00:00
idMatX m;
2012-11-26 18:58:24 +00:00
// the matrix must be symmetric
if( !IsSymmetric( epsilon ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// copy matrix
m.SetData( numRows, numColumns, MATX_ALLOCA( numRows * numColumns ) );
m = *this;
2012-11-26 18:58:24 +00:00
// being able to obtain Cholesky factors is both a necessary and sufficient condition for positive definiteness
return m.Cholesky_Factor();
}
/*
============
idMatX::IsPositiveSemiDefinite
returns true if the matrix is Positive Semi Definite (PSD)
A square matrix M of order n is said to be PSD if y'My >= 0 for all vectors y of dimension n, y != 0.
============
*/
bool idMatX::IsPositiveSemiDefinite( const float epsilon ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k;
float d, s;
idMatX m;
2012-11-26 18:58:24 +00:00
// the matrix must be square
if( !IsSquare() )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// copy original matrix
m.SetData( numRows, numColumns, MATX_ALLOCA( numRows * numColumns ) );
m = *this;
2012-11-26 18:58:24 +00:00
// add transpose
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < numColumns; j++ )
{
m[i][j] += ( *this )[j][i];
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// test Positive Semi Definiteness with Gaussian pivot steps
for( i = 0; i < numRows; i++ )
{
for( j = i; j < numColumns; j++ )
{
if( m[j][j] < -epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( m[j][j] > epsilon )
{
2012-11-26 18:58:24 +00:00
continue;
}
for( k = 0; k < numRows; k++ )
{
if( idMath::Fabs( m[k][j] ) > epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( idMath::Fabs( m[j][k] ) > epsilon )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
}
if( m[i][i] <= epsilon )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
d = 1.0f / m[i][i];
for( j = i + 1; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
s = d * m[j][i];
m[j][i] = 0.0f;
for( k = i + 1; k < numRows; k++ )
{
2012-11-26 18:58:24 +00:00
m[j][k] -= s * m[i][k];
}
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::IsSymmetricPositiveSemiDefinite
returns true if the matrix is Symmetric Positive Semi Definite (PSD)
============
*/
bool idMatX::IsSymmetricPositiveSemiDefinite( const float epsilon ) const
{
2012-11-26 18:58:24 +00:00
// the matrix must be symmetric
if( !IsSymmetric( epsilon ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
return IsPositiveSemiDefinite( epsilon );
}
/*
============
idMatX::LowerTriangularInverse
in-place inversion of the lower triangular matrix
============
*/
bool idMatX::LowerTriangularInverse()
{
2012-11-26 18:58:24 +00:00
int i, j, k;
double d, sum;
for( i = 0; i < numRows; i++ )
{
d = ( *this )[i][i];
if( d == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[i][i] = d = 1.0f / d;
for( j = 0; j < i; j++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( k = j; k < i; k++ )
{
sum -= ( *this )[i][k] * ( *this )[k][j];
2012-11-26 18:58:24 +00:00
}
( *this )[i][j] = sum * d;
2012-11-26 18:58:24 +00:00
}
}
return true;
}
/*
============
idMatX::UpperTriangularInverse
in-place inversion of the upper triangular matrix
============
*/
bool idMatX::UpperTriangularInverse()
{
2012-11-26 18:58:24 +00:00
int i, j, k;
double d, sum;
for( i = numRows - 1; i >= 0; i-- )
{
d = ( *this )[i][i];
if( d == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[i][i] = d = 1.0f / d;
for( j = numRows - 1; j > i; j-- )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( k = j; k > i; k-- )
{
sum -= ( *this )[i][k] * ( *this )[k][j];
2012-11-26 18:58:24 +00:00
}
( *this )[i][j] = sum * d;
2012-11-26 18:58:24 +00:00
}
}
return true;
}
/*
=============
idMatX::ToString
=============
*/
const char* idMatX::ToString( int precision ) const
{
2012-11-26 18:58:24 +00:00
return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
}
/*
============
idMatX::Update_RankOne
Updates the matrix to obtain the matrix: A + alpha * v * w'
============
*/
void idMatX::Update_RankOne( const idVecX& v, const idVecX& w, float alpha )
{
2012-11-26 18:58:24 +00:00
int i, j;
float s;
2012-11-26 18:58:24 +00:00
assert( v.GetSize() >= numRows );
assert( w.GetSize() >= numColumns );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
s = alpha * v[i];
for( j = 0; j < numColumns; j++ )
{
( *this )[i][j] += s * w[j];
2012-11-26 18:58:24 +00:00
}
}
}
/*
============
idMatX::Update_RankOneSymmetric
Updates the matrix to obtain the matrix: A + alpha * v * v'
============
*/
void idMatX::Update_RankOneSymmetric( const idVecX& v, float alpha )
{
2012-11-26 18:58:24 +00:00
int i, j;
float s;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
s = alpha * v[i];
for( j = 0; j < numColumns; j++ )
{
( *this )[i][j] += s * v[j];
2012-11-26 18:58:24 +00:00
}
}
}
/*
============
idMatX::Update_RowColumn
Updates the matrix to obtain the matrix:
[ 0 a 0 ]
A + [ d b e ]
[ 0 c 0 ]
where: a = v[0,r-1], b = v[r], c = v[r+1,numRows-1], d = w[0,r-1], w[r] = 0.0f, e = w[r+1,numColumns-1]
============
*/
void idMatX::Update_RowColumn( const idVecX& v, const idVecX& w, int r )
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
assert( w[r] == 0.0f );
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
for( i = 0; i < numRows; i++ )
{
( *this )[i][r] += v[i];
2012-11-26 18:58:24 +00:00
}
for( i = 0; i < numColumns; i++ )
{
( *this )[r][i] += w[i];
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::Update_RowColumnSymmetric
Updates the matrix to obtain the matrix:
[ 0 a 0 ]
A + [ a b c ]
[ 0 c 0 ]
where: a = v[0,r-1], b = v[r], c = v[r+1,numRows-1]
============
*/
void idMatX::Update_RowColumnSymmetric( const idVecX& v, int r )
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
for( i = 0; i < r; i++ )
{
( *this )[i][r] += v[i];
( *this )[r][i] += v[i];
2012-11-26 18:58:24 +00:00
}
( *this )[r][r] += v[r];
for( i = r + 1; i < numRows; i++ )
{
( *this )[i][r] += v[i];
( *this )[r][i] += v[i];
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::Update_Increment
Updates the matrix to obtain the matrix:
[ A a ]
[ c b ]
where: a = v[0,numRows-1], b = v[numRows], c = w[0,numColumns-1]], w[numColumns] = 0
============
*/
void idMatX::Update_Increment( const idVecX& v, const idVecX& w )
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows + 1 );
assert( w.GetSize() >= numColumns + 1 );
ChangeSize( numRows + 1, numColumns + 1, false );
for( i = 0; i < numRows; i++ )
{
( *this )[i][numColumns - 1] = v[i];
2012-11-26 18:58:24 +00:00
}
for( i = 0; i < numColumns - 1; i++ )
{
( *this )[numRows - 1][i] = w[i];
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::Update_IncrementSymmetric
Updates the matrix to obtain the matrix:
[ A a ]
[ a b ]
where: a = v[0,numRows-1], b = v[numRows]
============
*/
void idMatX::Update_IncrementSymmetric( const idVecX& v )
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows + 1 );
ChangeSize( numRows + 1, numColumns + 1, false );
for( i = 0; i < numRows - 1; i++ )
{
( *this )[i][numColumns - 1] = v[i];
2012-11-26 18:58:24 +00:00
}
for( i = 0; i < numColumns; i++ )
{
( *this )[numRows - 1][i] = v[i];
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::Update_Decrement
Updates the matrix to obtain a matrix with row r and column r removed.
============
*/
void idMatX::Update_Decrement( int r )
{
2012-11-26 18:58:24 +00:00
RemoveRowColumn( r );
}
/*
============
idMatX::Inverse_GaussJordan
in-place inversion using Gauss-Jordan elimination
============
*/
bool idMatX::Inverse_GaussJordan()
{
2012-11-26 18:58:24 +00:00
int i, j, k, r, c;
float d, max;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
int* columnIndex = ( int* ) _alloca16( numRows * sizeof( int ) );
int* rowIndex = ( int* ) _alloca16( numRows * sizeof( int ) );
bool* pivot = ( bool* ) _alloca16( numRows * sizeof( bool ) );
2012-11-26 18:58:24 +00:00
memset( pivot, 0, numRows * sizeof( bool ) );
2012-11-26 18:58:24 +00:00
// elimination with full pivoting
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
// search the whole matrix except for pivoted rows for the maximum absolute value
max = 0.0f;
r = c = 0;
for( j = 0; j < numRows; j++ )
{
if( !pivot[j] )
{
for( k = 0; k < numRows; k++ )
{
if( !pivot[k] )
{
d = idMath::Fabs( ( *this )[j][k] );
if( d > max )
{
2012-11-26 18:58:24 +00:00
max = d;
r = j;
c = k;
}
}
}
}
}
if( max == 0.0f )
{
2012-11-26 18:58:24 +00:00
// matrix is not invertible
return false;
}
2012-11-26 18:58:24 +00:00
pivot[c] = true;
2012-11-26 18:58:24 +00:00
// swap rows such that entry (c,c) has the pivot entry
if( r != c )
{
2012-11-26 18:58:24 +00:00
SwapRows( r, c );
}
2012-11-26 18:58:24 +00:00
// keep track of the row permutation
rowIndex[i] = r;
columnIndex[i] = c;
2012-11-26 18:58:24 +00:00
// scale the row to make the pivot entry equal to 1
d = 1.0f / ( *this )[c][c];
( *this )[c][c] = 1.0f;
for( k = 0; k < numRows; k++ )
{
( *this )[c][k] *= d;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// zero out the pivot column entries in the other rows
for( j = 0; j < numRows; j++ )
{
if( j != c )
{
d = ( *this )[j][c];
( *this )[j][c] = 0.0f;
for( k = 0; k < numRows; k++ )
{
( *this )[j][k] -= ( *this )[c][k] * d;
2012-11-26 18:58:24 +00:00
}
}
}
}
2012-11-26 18:58:24 +00:00
// reorder rows to store the inverse of the original matrix
for( j = numRows - 1; j >= 0; j-- )
{
if( rowIndex[j] != columnIndex[j] )
{
for( k = 0; k < numRows; k++ )
{
d = ( *this )[k][rowIndex[j]];
( *this )[k][rowIndex[j]] = ( *this )[k][columnIndex[j]];
( *this )[k][columnIndex[j]] = d;
2012-11-26 18:58:24 +00:00
}
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::Inverse_UpdateRankOne
Updates the in-place inverse using the Sherman-Morrison formula to obtain the inverse for the matrix: A + alpha * v * w'
============
*/
bool idMatX::Inverse_UpdateRankOne( const idVecX& v, const idVecX& w, float alpha )
{
2012-11-26 18:58:24 +00:00
int i, j;
float beta, s;
idVecX y, z;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
2012-11-26 18:58:24 +00:00
y.SetData( numRows, VECX_ALLOCA( numRows ) );
z.SetData( numRows, VECX_ALLOCA( numRows ) );
2012-11-26 18:58:24 +00:00
Multiply( y, v );
TransposeMultiply( z, w );
beta = 1.0f + ( w * y );
if( beta == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
alpha /= beta;
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
s = y[i] * alpha;
for( j = 0; j < numColumns; j++ )
{
( *this )[i][j] -= s * z[j];
2012-11-26 18:58:24 +00:00
}
}
return true;
}
/*
============
idMatX::Inverse_UpdateRowColumn
Updates the in-place inverse to obtain the inverse for the matrix:
[ 0 a 0 ]
A + [ d b e ]
[ 0 c 0 ]
where: a = v[0,r-1], b = v[r], c = v[r+1,numRows-1], d = w[0,r-1], w[r] = 0.0f, e = w[r+1,numColumns-1]
============
*/
bool idMatX::Inverse_UpdateRowColumn( const idVecX& v, const idVecX& w, int r )
{
2012-11-26 18:58:24 +00:00
idVecX s;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
assert( r >= 0 && r < numRows && r < numColumns );
assert( w[r] == 0.0f );
2012-11-26 18:58:24 +00:00
s.SetData( Max( numRows, numColumns ), VECX_ALLOCA( Max( numRows, numColumns ) ) );
s.Zero();
s[r] = 1.0f;
if( !Inverse_UpdateRankOne( v, s, 1.0f ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( !Inverse_UpdateRankOne( s, w, 1.0f ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
============
idMatX::Inverse_UpdateIncrement
Updates the in-place inverse to obtain the inverse for the matrix:
[ A a ]
[ c b ]
where: a = v[0,numRows-1], b = v[numRows], c = w[0,numColumns-1], w[numColumns] = 0
============
*/
bool idMatX::Inverse_UpdateIncrement( const idVecX& v, const idVecX& w )
{
2012-11-26 18:58:24 +00:00
idVecX v2;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows + 1 );
assert( w.GetSize() >= numColumns + 1 );
ChangeSize( numRows + 1, numColumns + 1, true );
( *this )[numRows - 1][numRows - 1] = 1.0f;
2012-11-26 18:58:24 +00:00
v2.SetData( numRows, VECX_ALLOCA( numRows ) );
v2 = v;
v2[numRows - 1] -= 1.0f;
return Inverse_UpdateRowColumn( v2, w, numRows - 1 );
2012-11-26 18:58:24 +00:00
}
/*
============
idMatX::Inverse_UpdateDecrement
Updates the in-place inverse to obtain the inverse of the matrix with row r and column r removed.
v and w should store the column and row of the original matrix respectively.
============
*/
bool idMatX::Inverse_UpdateDecrement( const idVecX& v, const idVecX& w, int r )
{
2012-11-26 18:58:24 +00:00
idVecX v1, w1;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( w.GetSize() >= numColumns );
assert( r >= 0 && r < numRows && r < numColumns );
2012-11-26 18:58:24 +00:00
v1.SetData( numRows, VECX_ALLOCA( numRows ) );
w1.SetData( numRows, VECX_ALLOCA( numRows ) );
2012-11-26 18:58:24 +00:00
// update the row and column to identity
v1 = -v;
w1 = -w;
v1[r] += 1.0f;
w1[r] = 0.0f;
if( !Inverse_UpdateRowColumn( v1, w1, r ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// physically remove the row and column
Update_Decrement( r );
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::Inverse_Solve
Solve Ax = b with A inverted
============
*/
void idMatX::Inverse_Solve( idVecX& x, const idVecX& b ) const
{
2012-11-26 18:58:24 +00:00
Multiply( x, b );
}
/*
============
idMatX::LU_Factor
in-place factorization: LU
L is a triangular matrix stored in the lower triangle.
L has ones on the diagonal that are not stored.
U is a triangular matrix stored in the upper triangle.
If index != NULL partial pivoting is used for numerical stability.
If index != NULL it must point to an array of numRow integers and is used to keep track of the row permutation.
If det != NULL the determinant of the matrix is calculated and stored.
============
*/
bool idMatX::LU_Factor( int* index, float* det )
{
2012-11-26 18:58:24 +00:00
int i, j, k, newi, min;
double s, t, d, w;
2012-11-26 18:58:24 +00:00
// if partial pivoting should be used
if( index )
{
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
index[i] = i;
}
}
2012-11-26 18:58:24 +00:00
w = 1.0f;
min = Min( numRows, numColumns );
for( i = 0; i < min; i++ )
{
2012-11-26 18:58:24 +00:00
newi = i;
s = idMath::Fabs( ( *this )[i][i] );
if( index )
{
2012-11-26 18:58:24 +00:00
// find the largest absolute pivot
for( j = i + 1; j < numRows; j++ )
{
t = idMath::Fabs( ( *this )[j][i] );
if( t > s )
{
2012-11-26 18:58:24 +00:00
newi = j;
s = t;
}
}
}
if( s == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( newi != i && index )
{
2012-11-26 18:58:24 +00:00
w = -w;
2012-11-26 18:58:24 +00:00
// swap index elements
k = index[i];
index[i] = index[newi];
index[newi] = k;
2012-11-26 18:58:24 +00:00
// swap rows
for( j = 0; j < numColumns; j++ )
{
t = ( *this )[newi][j];
( *this )[newi][j] = ( *this )[i][j];
( *this )[i][j] = t;
2012-11-26 18:58:24 +00:00
}
}
if( i < numRows )
{
d = 1.0f / ( *this )[i][i];
for( j = i + 1; j < numRows; j++ )
{
( *this )[j][i] *= d;
2012-11-26 18:58:24 +00:00
}
}
if( i < min - 1 )
{
for( j = i + 1; j < numRows; j++ )
{
d = ( *this )[j][i];
for( k = i + 1; k < numColumns; k++ )
{
( *this )[j][k] -= d * ( *this )[i][k];
2012-11-26 18:58:24 +00:00
}
}
}
}
if( det )
{
for( i = 0; i < numRows; i++ )
{
w *= ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
*det = w;
}
2012-11-26 18:58:24 +00:00
return true;
}
2012-11-26 18:58:24 +00:00
/*
============
idMatX::LU_UpdateRankOne
Updates the in-place LU factorization to obtain the factors for the matrix: LU + alpha * v * w'
============
*/
bool idMatX::LU_UpdateRankOne( const idVecX& v, const idVecX& w, float alpha, int* index )
{
2012-11-26 18:58:24 +00:00
int i, j, max;
float* y, *z;
2012-11-26 18:58:24 +00:00
double diag, beta, p0, p1, d;
2012-11-26 18:58:24 +00:00
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
y = ( float* ) _alloca16( v.GetSize() * sizeof( float ) );
z = ( float* ) _alloca16( w.GetSize() * sizeof( float ) );
if( index != NULL )
{
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
y[i] = alpha * v[index[i]];
}
}
else
{
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
y[i] = alpha * v[i];
}
}
2012-11-26 18:58:24 +00:00
memcpy( z, w.ToFloatPtr(), w.GetSize() * sizeof( float ) );
2012-11-26 18:58:24 +00:00
max = Min( numRows, numColumns );
for( i = 0; i < max; i++ )
{
diag = ( *this )[i][i];
2012-11-26 18:58:24 +00:00
p0 = y[i];
p1 = z[i];
diag += p0 * p1;
if( diag == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
beta = p1 / diag;
( *this )[i][i] = diag;
for( j = i + 1; j < numColumns; j++ )
{
d = ( *this )[i][j];
2012-11-26 18:58:24 +00:00
d += p0 * z[j];
z[j] -= beta * d;
( *this )[i][j] = d;
}
for( j = i + 1; j < numRows; j++ )
{
d = ( *this )[j][i];
2012-11-26 18:58:24 +00:00
y[j] -= p0 * d;
d += beta * y[j];
( *this )[j][i] = d;
2012-11-26 18:58:24 +00:00
}
}
return true;
}
/*
============
idMatX::LU_UpdateRowColumn
Updates the in-place LU factorization to obtain the factors for the matrix:
[ 0 a 0 ]
LU + [ d b e ]
[ 0 c 0 ]
where: a = v[0,r-1], b = v[r], c = v[r+1,numRows-1], d = w[0,r-1], w[r] = 0.0f, e = w[r+1,numColumns-1]
============
*/
bool idMatX::LU_UpdateRowColumn( const idVecX& v, const idVecX& w, int r, int* index )
{
2012-11-26 18:58:24 +00:00
#if 0
idVecX s;
2012-11-26 18:58:24 +00:00
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
assert( r >= 0 && r < numRows && r < numColumns );
assert( w[r] == 0.0f );
2012-11-26 18:58:24 +00:00
s.SetData( Max( numRows, numColumns ), VECX_ALLOCA( Max( numRows, numColumns ) ) );
s.Zero();
s[r] = 1.0f;
if( !LU_UpdateRankOne( v, s, 1.0f, index ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( !LU_UpdateRankOne( s, w, 1.0f, index ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
2012-11-26 18:58:24 +00:00
#else
2012-11-26 18:58:24 +00:00
int i, j, min, max, rp;
float* y0, *y1, *z0, *z1;
2012-11-26 18:58:24 +00:00
double diag, beta0, beta1, p0, p1, q0, q1, d;
2012-11-26 18:58:24 +00:00
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
assert( r >= 0 && r < numColumns && r < numRows );
assert( w[r] == 0.0f );
y0 = ( float* ) _alloca16( v.GetSize() * sizeof( float ) );
z0 = ( float* ) _alloca16( w.GetSize() * sizeof( float ) );
y1 = ( float* ) _alloca16( v.GetSize() * sizeof( float ) );
z1 = ( float* ) _alloca16( w.GetSize() * sizeof( float ) );
if( index != NULL )
{
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
y0[i] = v[index[i]];
}
rp = r;
for( i = 0; i < numRows; i++ )
{
if( index[i] == r )
{
2012-11-26 18:58:24 +00:00
rp = i;
break;
}
}
}
else
{
2012-11-26 18:58:24 +00:00
memcpy( y0, v.ToFloatPtr(), v.GetSize() * sizeof( float ) );
rp = r;
}
2012-11-26 18:58:24 +00:00
memset( y1, 0, v.GetSize() * sizeof( float ) );
y1[rp] = 1.0f;
2012-11-26 18:58:24 +00:00
memset( z0, 0, w.GetSize() * sizeof( float ) );
z0[r] = 1.0f;
2012-11-26 18:58:24 +00:00
memcpy( z1, w.ToFloatPtr(), w.GetSize() * sizeof( float ) );
2012-11-26 18:58:24 +00:00
// update the beginning of the to be updated row and column
min = Min( r, rp );
for( i = 0; i < min; i++ )
{
2012-11-26 18:58:24 +00:00
p0 = y0[i];
beta1 = z1[i] / ( *this )[i][i];
( *this )[i][r] += p0;
for( j = i + 1; j < numColumns; j++ )
{
z1[j] -= beta1 * ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
for( j = i + 1; j < numRows; j++ )
{
y0[j] -= p0 * ( *this )[j][i];
2012-11-26 18:58:24 +00:00
}
( *this )[rp][i] += beta1;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// update the lower right corner starting at r,r
max = Min( numRows, numColumns );
for( i = min; i < max; i++ )
{
diag = ( *this )[i][i];
2012-11-26 18:58:24 +00:00
p0 = y0[i];
p1 = z0[i];
diag += p0 * p1;
if( diag == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
beta0 = p1 / diag;
2012-11-26 18:58:24 +00:00
q0 = y1[i];
q1 = z1[i];
diag += q0 * q1;
if( diag == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
beta1 = q1 / diag;
( *this )[i][i] = diag;
for( j = i + 1; j < numColumns; j++ )
{
d = ( *this )[i][j];
2012-11-26 18:58:24 +00:00
d += p0 * z0[j];
z0[j] -= beta0 * d;
2012-11-26 18:58:24 +00:00
d += q0 * z1[j];
z1[j] -= beta1 * d;
( *this )[i][j] = d;
2012-11-26 18:58:24 +00:00
}
for( j = i + 1; j < numRows; j++ )
{
d = ( *this )[j][i];
2012-11-26 18:58:24 +00:00
y0[j] -= p0 * d;
d += beta0 * y0[j];
2012-11-26 18:58:24 +00:00
y1[j] -= q0 * d;
d += beta1 * y1[j];
( *this )[j][i] = d;
2012-11-26 18:58:24 +00:00
}
}
return true;
2012-11-26 18:58:24 +00:00
#endif
}
/*
============
idMatX::LU_UpdateIncrement
Updates the in-place LU factorization to obtain the factors for the matrix:
[ A a ]
[ c b ]
where: a = v[0,numRows-1], b = v[numRows], c = w[0,numColumns-1], w[numColumns] = 0
============
*/
bool idMatX::LU_UpdateIncrement( const idVecX& v, const idVecX& w, int* index )
{
2012-11-26 18:58:24 +00:00
int i, j;
float sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows + 1 );
assert( w.GetSize() >= numColumns + 1 );
ChangeSize( numRows + 1, numColumns + 1, true );
2012-11-26 18:58:24 +00:00
// add row to L
for( i = 0; i < numRows - 1; i++ )
{
2012-11-26 18:58:24 +00:00
sum = w[i];
for( j = 0; j < i; j++ )
{
sum -= ( *this )[numRows - 1][j] * ( *this )[j][i];
2012-11-26 18:58:24 +00:00
}
( *this )[numRows - 1 ][i] = sum / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// add row to the permutation index
if( index != NULL )
{
2012-11-26 18:58:24 +00:00
index[numRows - 1] = numRows - 1;
}
2012-11-26 18:58:24 +00:00
// add column to U
for( i = 0; i < numRows; i++ )
{
if( index != NULL )
{
2012-11-26 18:58:24 +00:00
sum = v[index[i]];
}
else
{
2012-11-26 18:58:24 +00:00
sum = v[i];
}
for( j = 0; j < i; j++ )
{
sum -= ( *this )[i][j] * ( *this )[j][numRows - 1];
2012-11-26 18:58:24 +00:00
}
( *this )[i][numRows - 1] = sum;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::LU_UpdateDecrement
Updates the in-place LU factorization to obtain the factors for the matrix with row r and column r removed.
v and w should store the column and row of the original matrix respectively.
If index != NULL then u should store row index[r] of the original matrix. If index == NULL then u = w.
============
*/
bool idMatX::LU_UpdateDecrement( const idVecX& v, const idVecX& w, const idVecX& u, int r, int* index )
{
2012-11-26 18:58:24 +00:00
int i, p;
idVecX v1, w1;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
assert( r >= 0 && r < numRows && r < numColumns );
2012-11-26 18:58:24 +00:00
v1.SetData( numRows, VECX_ALLOCA( numRows ) );
w1.SetData( numRows, VECX_ALLOCA( numRows ) );
if( index != NULL )
{
2012-11-26 18:58:24 +00:00
// find the pivot row
for( p = i = 0; i < numRows; i++ )
{
if( index[i] == r )
{
2012-11-26 18:58:24 +00:00
p = i;
break;
}
}
2012-11-26 18:58:24 +00:00
// update the row and column to identity
v1 = -v;
w1 = -u;
if( p != r )
{
2012-11-26 18:58:24 +00:00
SwapValues( v1[index[r]], v1[index[p]] );
SwapValues( index[r], index[p] );
}
2012-11-26 18:58:24 +00:00
v1[r] += 1.0f;
w1[r] = 0.0f;
if( !LU_UpdateRowColumn( v1, w1, r, index ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( p != r )
{
if( idMath::Fabs( u[p] ) < 1e-4f )
{
2012-11-26 18:58:24 +00:00
// NOTE: an additional row interchange is required for numerical stability
}
2012-11-26 18:58:24 +00:00
// move row index[r] of the original matrix to row index[p] of the original matrix
v1.Zero();
v1[index[p]] = 1.0f;
w1 = u - w;
if( !LU_UpdateRankOne( v1, w1, 1.0f, index ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
2012-11-26 18:58:24 +00:00
// remove the row from the permutation index
for( i = r; i < numRows - 1; i++ )
{
index[i] = index[i + 1];
}
for( i = 0; i < numRows - 1; i++ )
{
if( index[i] > r )
{
2012-11-26 18:58:24 +00:00
index[i]--;
}
}
}
else
{
2012-11-26 18:58:24 +00:00
v1 = -v;
w1 = -w;
v1[r] += 1.0f;
w1[r] = 0.0f;
if( !LU_UpdateRowColumn( v1, w1, r, index ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
2012-11-26 18:58:24 +00:00
// physically remove the row and column
Update_Decrement( r );
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::LU_Solve
Solve Ax = b with A factored in-place as: LU
============
*/
void idMatX::LU_Solve( idVecX& x, const idVecX& b, const int* index ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum;
2012-11-26 18:58:24 +00:00
assert( x.GetSize() == numColumns && b.GetSize() == numRows );
2012-11-26 18:58:24 +00:00
// solve L
for( i = 0; i < numRows; i++ )
{
if( index != NULL )
{
2012-11-26 18:58:24 +00:00
sum = b[index[i]];
}
else
{
2012-11-26 18:58:24 +00:00
sum = b[i];
}
for( j = 0; j < i; j++ )
{
sum -= ( *this )[i][j] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum;
}
2012-11-26 18:58:24 +00:00
// solve U
for( i = numRows - 1; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
sum = x[i];
for( j = i + 1; j < numRows; j++ )
{
sum -= ( *this )[i][j] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::LU_Inverse
Calculates the inverse of the matrix which is factored in-place as LU
============
*/
void idMatX::LU_Inverse( idMatX& inv, const int* index ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
idVecX x, b;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
x.SetData( numRows, VECX_ALLOCA( numRows ) );
b.SetData( numRows, VECX_ALLOCA( numRows ) );
b.Zero();
inv.SetSize( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
b[i] = 1.0f;
LU_Solve( x, b, index );
for( j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
inv[j][i] = x[j];
}
b[i] = 0.0f;
}
}
/*
============
idMatX::LU_UnpackFactors
Unpacks the in-place LU factorization.
============
*/
void idMatX::LU_UnpackFactors( idMatX& L, idMatX& U ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
2012-11-26 18:58:24 +00:00
L.Zero( numRows, numColumns );
U.Zero( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < i; j++ )
{
L[i][j] = ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
L[i][i] = 1.0f;
for( j = i; j < numColumns; j++ )
{
U[i][j] = ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
}
}
/*
============
idMatX::LU_MultiplyFactors
Multiplies the factors of the in-place LU factorization to form the original matrix.
============
*/
void idMatX::LU_MultiplyFactors( idMatX& m, const int* index ) const
{
2012-11-26 18:58:24 +00:00
int r, rp, i, j;
double sum;
2012-11-26 18:58:24 +00:00
m.SetSize( numRows, numColumns );
for( r = 0; r < numRows; r++ )
{
if( index != NULL )
{
2012-11-26 18:58:24 +00:00
rp = index[r];
}
else
{
2012-11-26 18:58:24 +00:00
rp = r;
}
2012-11-26 18:58:24 +00:00
// calculate row of matrix
for( i = 0; i < numColumns; i++ )
{
if( i >= r )
{
sum = ( *this )[r][i];
}
else
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
}
for( j = 0; j <= i && j < r; j++ )
{
sum += ( *this )[r][j] * ( *this )[j][i];
2012-11-26 18:58:24 +00:00
}
m[rp][i] = sum;
}
}
}
/*
============
idMatX::QR_Factor
in-place factorization: QR
Q is an orthogonal matrix represented as a product of Householder matrices stored in the lower triangle and c.
R is a triangular matrix stored in the upper triangle except for the diagonal elements which are stored in d.
The initial matrix has to be square.
============
*/
bool idMatX::QR_Factor( idVecX& c, idVecX& d )
{
2012-11-26 18:58:24 +00:00
int i, j, k;
double scale, s, t, sum;
bool singular = false;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( c.GetSize() >= numRows && d.GetSize() >= numRows );
for( k = 0; k < numRows - 1; k++ )
{
2012-11-26 18:58:24 +00:00
scale = 0.0f;
for( i = k; i < numRows; i++ )
{
s = idMath::Fabs( ( *this )[i][k] );
if( s > scale )
{
2012-11-26 18:58:24 +00:00
scale = s;
}
}
if( scale == 0.0f )
{
2012-11-26 18:58:24 +00:00
singular = true;
c[k] = d[k] = 0.0f;
}
else
{
2012-11-26 18:58:24 +00:00
s = 1.0f / scale;
for( i = k; i < numRows; i++ )
{
( *this )[i][k] *= s;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( i = k; i < numRows; i++ )
{
s = ( *this )[i][k];
2012-11-26 18:58:24 +00:00
sum += s * s;
}
2012-11-26 18:58:24 +00:00
s = idMath::Sqrt( sum );
if( ( *this )[k][k] < 0.0f )
{
2012-11-26 18:58:24 +00:00
s = -s;
}
( *this )[k][k] += s;
c[k] = s * ( *this )[k][k];
2012-11-26 18:58:24 +00:00
d[k] = -scale * s;
for( j = k + 1; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( i = k; i < numRows; i++ )
{
sum += ( *this )[i][k] * ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
t = sum / c[k];
for( i = k; i < numRows; i++ )
{
( *this )[i][j] -= t * ( *this )[i][k];
2012-11-26 18:58:24 +00:00
}
}
}
}
d[numRows - 1] = ( *this )[( numRows - 1 ) ][( numRows - 1 ) ];
if( d[numRows - 1] == 0.0f )
{
2012-11-26 18:58:24 +00:00
singular = true;
}
2012-11-26 18:58:24 +00:00
return !singular;
}
/*
============
idMatX::QR_Rotate
Performs a Jacobi rotation on the rows i and i+1 of the unpacked QR factors.
============
*/
void idMatX::QR_Rotate( idMatX& R, int i, float a, float b )
{
2012-11-26 18:58:24 +00:00
int j;
float f, c, s, w, y;
if( a == 0.0f )
{
2012-11-26 18:58:24 +00:00
c = 0.0f;
s = ( b >= 0.0f ) ? 1.0f : -1.0f;
}
else if( idMath::Fabs( a ) > idMath::Fabs( b ) )
{
2012-11-26 18:58:24 +00:00
f = b / a;
c = idMath::Fabs( 1.0f / idMath::Sqrt( 1.0f + f * f ) );
if( a < 0.0f )
{
2012-11-26 18:58:24 +00:00
c = -c;
}
s = f * c;
}
else
{
2012-11-26 18:58:24 +00:00
f = a / b;
s = idMath::Fabs( 1.0f / idMath::Sqrt( 1.0f + f * f ) );
if( b < 0.0f )
{
2012-11-26 18:58:24 +00:00
s = -s;
}
c = f * s;
}
for( j = i; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
y = R[i][j];
w = R[i + 1][j];
2012-11-26 18:58:24 +00:00
R[i][j] = c * y - s * w;
R[i + 1][j] = s * y + c * w;
2012-11-26 18:58:24 +00:00
}
for( j = 0; j < numRows; j++ )
{
y = ( *this )[j][i];
w = ( *this )[j][i + 1];
( *this )[j][i] = c * y - s * w;
( *this )[j][i + 1] = s * y + c * w;
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::QR_UpdateRankOne
Updates the unpacked QR factorization to obtain the factors for the matrix: QR + alpha * v * w'
============
*/
bool idMatX::QR_UpdateRankOne( idMatX& R, const idVecX& v, const idVecX& w, float alpha )
{
2012-11-26 18:58:24 +00:00
int i, k;
float f;
idVecX u;
2012-11-26 18:58:24 +00:00
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
2012-11-26 18:58:24 +00:00
u.SetData( v.GetSize(), VECX_ALLOCA( v.GetSize() ) );
TransposeMultiply( u, v );
u *= alpha;
for( k = v.GetSize() - 1; k > 0; k-- )
{
if( u[k] != 0.0f )
{
2012-11-26 18:58:24 +00:00
break;
}
}
for( i = k - 1; i >= 0; i-- )
{
QR_Rotate( R, i, u[i], -u[i + 1] );
if( u[i] == 0.0f )
{
u[i] = idMath::Fabs( u[i + 1] );
}
else if( idMath::Fabs( u[i] ) > idMath::Fabs( u[i + 1] ) )
{
f = u[i + 1] / u[i];
2012-11-26 18:58:24 +00:00
u[i] = idMath::Fabs( u[i] ) * idMath::Sqrt( 1.0f + f * f );
}
else
{
f = u[i] / u[i + 1];
u[i] = idMath::Fabs( u[i + 1] ) * idMath::Sqrt( 1.0f + f * f );
2012-11-26 18:58:24 +00:00
}
}
for( i = 0; i < v.GetSize(); i++ )
{
2012-11-26 18:58:24 +00:00
R[0][i] += u[0] * w[i];
}
for( i = 0; i < k; i++ )
{
QR_Rotate( R, i, -R[i][i], R[i + 1][i] );
2012-11-26 18:58:24 +00:00
}
return true;
}
/*
============
idMatX::QR_UpdateRowColumn
Updates the unpacked QR factorization to obtain the factors for the matrix:
[ 0 a 0 ]
QR + [ d b e ]
[ 0 c 0 ]
where: a = v[0,r-1], b = v[r], c = v[r+1,numRows-1], d = w[0,r-1], w[r] = 0.0f, e = w[r+1,numColumns-1]
============
*/
bool idMatX::QR_UpdateRowColumn( idMatX& R, const idVecX& v, const idVecX& w, int r )
{
2012-11-26 18:58:24 +00:00
idVecX s;
2012-11-26 18:58:24 +00:00
assert( v.GetSize() >= numColumns );
assert( w.GetSize() >= numRows );
assert( r >= 0 && r < numRows && r < numColumns );
assert( w[r] == 0.0f );
2012-11-26 18:58:24 +00:00
s.SetData( Max( numRows, numColumns ), VECX_ALLOCA( Max( numRows, numColumns ) ) );
s.Zero();
s[r] = 1.0f;
if( !QR_UpdateRankOne( R, v, s, 1.0f ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( !QR_UpdateRankOne( R, s, w, 1.0f ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
============
idMatX::QR_UpdateIncrement
Updates the unpacked QR factorization to obtain the factors for the matrix:
[ A a ]
[ c b ]
where: a = v[0,numRows-1], b = v[numRows], c = w[0,numColumns-1], w[numColumns] = 0
============
*/
bool idMatX::QR_UpdateIncrement( idMatX& R, const idVecX& v, const idVecX& w )
{
2012-11-26 18:58:24 +00:00
idVecX v2;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows + 1 );
assert( w.GetSize() >= numColumns + 1 );
ChangeSize( numRows + 1, numColumns + 1, true );
( *this )[numRows - 1][numRows - 1] = 1.0f;
R.ChangeSize( R.numRows + 1, R.numColumns + 1, true );
R[R.numRows - 1][R.numRows - 1] = 1.0f;
2012-11-26 18:58:24 +00:00
v2.SetData( numRows, VECX_ALLOCA( numRows ) );
v2 = v;
v2[numRows - 1] -= 1.0f;
return QR_UpdateRowColumn( R, v2, w, numRows - 1 );
2012-11-26 18:58:24 +00:00
}
/*
============
idMatX::QR_UpdateDecrement
Updates the unpacked QR factorization to obtain the factors for the matrix with row r and column r removed.
v and w should store the column and row of the original matrix respectively.
============
*/
bool idMatX::QR_UpdateDecrement( idMatX& R, const idVecX& v, const idVecX& w, int r )
{
2012-11-26 18:58:24 +00:00
idVecX v1, w1;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( w.GetSize() >= numColumns );
assert( r >= 0 && r < numRows && r < numColumns );
2012-11-26 18:58:24 +00:00
v1.SetData( numRows, VECX_ALLOCA( numRows ) );
w1.SetData( numRows, VECX_ALLOCA( numRows ) );
2012-11-26 18:58:24 +00:00
// update the row and column to identity
v1 = -v;
w1 = -w;
v1[r] += 1.0f;
w1[r] = 0.0f;
if( !QR_UpdateRowColumn( R, v1, w1, r ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// physically remove the row and column
Update_Decrement( r );
R.Update_Decrement( r );
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::QR_Solve
Solve Ax = b with A factored in-place as: QR
============
*/
void idMatX::QR_Solve( idVecX& x, const idVecX& b, const idVecX& c, const idVecX& d ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum, t;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( x.GetSize() >= numRows && b.GetSize() >= numRows );
assert( c.GetSize() >= numRows && d.GetSize() >= numRows );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
x[i] = b[i];
}
2012-11-26 18:58:24 +00:00
// multiply b with transpose of Q
for( i = 0; i < numRows - 1; i++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( j = i; j < numRows; j++ )
{
sum += ( *this )[j][i] * x[j];
2012-11-26 18:58:24 +00:00
}
t = sum / c[i];
for( j = i; j < numRows; j++ )
{
x[j] -= t * ( *this )[j][i];
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// backsubstitution with R
for( i = numRows - 1; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
sum = x[i];
for( j = i + 1; j < numRows; j++ )
{
sum -= ( *this )[i][j] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum / d[i];
}
}
/*
============
idMatX::QR_Solve
Solve Ax = b with A factored as: QR
============
*/
void idMatX::QR_Solve( idVecX& x, const idVecX& b, const idMatX& R ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
// multiply b with transpose of Q
TransposeMultiply( x, b );
2012-11-26 18:58:24 +00:00
// backsubstitution with R
for( i = numRows - 1; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
sum = x[i];
for( j = i + 1; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
sum -= R[i][j] * x[j];
}
x[i] = sum / R[i][i];
}
}
/*
============
idMatX::QR_Inverse
Calculates the inverse of the matrix which is factored in-place as: QR
============
*/
void idMatX::QR_Inverse( idMatX& inv, const idVecX& c, const idVecX& d ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
idVecX x, b;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
x.SetData( numRows, VECX_ALLOCA( numRows ) );
b.SetData( numRows, VECX_ALLOCA( numRows ) );
b.Zero();
inv.SetSize( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
b[i] = 1.0f;
QR_Solve( x, b, c, d );
for( j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
inv[j][i] = x[j];
}
b[i] = 0.0f;
}
}
/*
============
idMatX::QR_UnpackFactors
Unpacks the in-place QR factorization.
============
*/
void idMatX::QR_UnpackFactors( idMatX& Q, idMatX& R, const idVecX& c, const idVecX& d ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k;
double sum;
2012-11-26 18:58:24 +00:00
Q.Identity( numRows, numColumns );
for( i = 0; i < numColumns - 1; i++ )
{
if( c[i] == 0.0f )
{
2012-11-26 18:58:24 +00:00
continue;
}
for( j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( k = i; k < numColumns; k++ )
{
sum += ( *this )[k][i] * Q[j][k];
2012-11-26 18:58:24 +00:00
}
sum /= c[i];
for( k = i; k < numColumns; k++ )
{
Q[j][k] -= sum * ( *this )[k][i];
2012-11-26 18:58:24 +00:00
}
}
}
2012-11-26 18:58:24 +00:00
R.Zero( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
R[i][i] = d[i];
for( j = i + 1; j < numColumns; j++ )
{
R[i][j] = ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
}
}
/*
============
idMatX::QR_MultiplyFactors
Multiplies the factors of the in-place QR factorization to form the original matrix.
============
*/
void idMatX::QR_MultiplyFactors( idMatX& m, const idVecX& c, const idVecX& d ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k;
double sum;
idMatX Q;
2012-11-26 18:58:24 +00:00
Q.Identity( numRows, numColumns );
for( i = 0; i < numColumns - 1; i++ )
{
if( c[i] == 0.0f )
{
2012-11-26 18:58:24 +00:00
continue;
}
for( j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( k = i; k < numColumns; k++ )
{
sum += ( *this )[k][i] * Q[j][k];
2012-11-26 18:58:24 +00:00
}
sum /= c[i];
for( k = i; k < numColumns; k++ )
{
Q[j][k] -= sum * ( *this )[k][i];
2012-11-26 18:58:24 +00:00
}
}
}
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
sum = Q[i][j] * d[i];
for( k = 0; k < i; k++ )
{
sum += Q[i][k] * ( *this )[j][k];
2012-11-26 18:58:24 +00:00
}
m[i][j] = sum;
}
}
}
/*
============
idMatX::Pythag
Computes (a^2 + b^2)^1/2 without underflow or overflow.
============
*/
float idMatX::Pythag( float a, float b ) const
{
2012-11-26 18:58:24 +00:00
double at, bt, ct;
2012-11-26 18:58:24 +00:00
at = idMath::Fabs( a );
bt = idMath::Fabs( b );
if( at > bt )
{
2012-11-26 18:58:24 +00:00
ct = bt / at;
return at * idMath::Sqrt( 1.0f + ct * ct );
}
else
{
if( bt )
{
2012-11-26 18:58:24 +00:00
ct = at / bt;
return bt * idMath::Sqrt( 1.0f + ct * ct );
}
else
{
2012-11-26 18:58:24 +00:00
return 0.0f;
}
}
}
/*
============
idMatX::SVD_BiDiag
============
*/
void idMatX::SVD_BiDiag( idVecX& w, idVecX& rv1, float& anorm )
{
2012-11-26 18:58:24 +00:00
int i, j, k, l;
double f, h, r, g, s, scale;
2012-11-26 18:58:24 +00:00
anorm = 0.0f;
g = s = scale = 0.0f;
for( i = 0; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
l = i + 1;
rv1[i] = scale * g;
g = s = scale = 0.0f;
if( i < numRows )
{
for( k = i; k < numRows; k++ )
{
scale += idMath::Fabs( ( *this )[k][i] );
2012-11-26 18:58:24 +00:00
}
if( scale )
{
for( k = i; k < numRows; k++ )
{
( *this )[k][i] /= scale;
s += ( *this )[k][i] * ( *this )[k][i];
2012-11-26 18:58:24 +00:00
}
f = ( *this )[i][i];
2012-11-26 18:58:24 +00:00
g = idMath::Sqrt( s );
if( f >= 0.0f )
{
2012-11-26 18:58:24 +00:00
g = -g;
}
h = f * g - s;
( *this )[i][i] = f - g;
if( i != ( numColumns - 1 ) )
{
for( j = l; j < numColumns; j++ )
{
for( s = 0.0f, k = i; k < numRows; k++ )
{
s += ( *this )[k][i] * ( *this )[k][j];
2012-11-26 18:58:24 +00:00
}
f = s / h;
for( k = i; k < numRows; k++ )
{
( *this )[k][j] += f * ( *this )[k][i];
2012-11-26 18:58:24 +00:00
}
}
}
for( k = i; k < numRows; k++ )
{
( *this )[k][i] *= scale;
2012-11-26 18:58:24 +00:00
}
}
}
w[i] = scale * g;
g = s = scale = 0.0f;
if( i < numRows && i != ( numColumns - 1 ) )
{
for( k = l; k < numColumns; k++ )
{
scale += idMath::Fabs( ( *this )[i][k] );
2012-11-26 18:58:24 +00:00
}
if( scale )
{
for( k = l; k < numColumns; k++ )
{
( *this )[i][k] /= scale;
s += ( *this )[i][k] * ( *this )[i][k];
2012-11-26 18:58:24 +00:00
}
f = ( *this )[i][l];
2012-11-26 18:58:24 +00:00
g = idMath::Sqrt( s );
if( f >= 0.0f )
{
2012-11-26 18:58:24 +00:00
g = -g;
}
h = 1.0f / ( f * g - s );
( *this )[i][l] = f - g;
for( k = l; k < numColumns; k++ )
{
rv1[k] = ( *this )[i][k] * h;
2012-11-26 18:58:24 +00:00
}
if( i != ( numRows - 1 ) )
{
for( j = l; j < numRows; j++ )
{
for( s = 0.0f, k = l; k < numColumns; k++ )
{
s += ( *this )[j][k] * ( *this )[i][k];
2012-11-26 18:58:24 +00:00
}
for( k = l; k < numColumns; k++ )
{
( *this )[j][k] += s * rv1[k];
2012-11-26 18:58:24 +00:00
}
}
}
for( k = l; k < numColumns; k++ )
{
( *this )[i][k] *= scale;
2012-11-26 18:58:24 +00:00
}
}
}
r = idMath::Fabs( w[i] ) + idMath::Fabs( rv1[i] );
if( r > anorm )
{
2012-11-26 18:58:24 +00:00
anorm = r;
}
}
}
/*
============
idMatX::SVD_InitialWV
============
*/
void idMatX::SVD_InitialWV( idVecX& w, idMatX& V, idVecX& rv1 )
{
2012-11-26 18:58:24 +00:00
int i, j, k, l;
double f, g, s;
2012-11-26 18:58:24 +00:00
g = 0.0f;
for( i = ( numColumns - 1 ); i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
l = i + 1;
if( i < ( numColumns - 1 ) )
{
if( g )
{
for( j = l; j < numColumns; j++ )
{
V[j][i] = ( ( *this )[i][j] / ( *this )[i][l] ) / g;
2012-11-26 18:58:24 +00:00
}
// double division to reduce underflow
for( j = l; j < numColumns; j++ )
{
for( s = 0.0f, k = l; k < numColumns; k++ )
{
s += ( *this )[i][k] * V[k][j];
2012-11-26 18:58:24 +00:00
}
for( k = l; k < numColumns; k++ )
{
2012-11-26 18:58:24 +00:00
V[k][j] += s * V[k][i];
}
}
}
for( j = l; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
V[i][j] = V[j][i] = 0.0f;
}
}
V[i][i] = 1.0f;
g = rv1[i];
}
for( i = numColumns - 1 ; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
l = i + 1;
g = w[i];
if( i < ( numColumns - 1 ) )
{
for( j = l; j < numColumns; j++ )
{
( *this )[i][j] = 0.0f;
2012-11-26 18:58:24 +00:00
}
}
if( g )
{
2012-11-26 18:58:24 +00:00
g = 1.0f / g;
if( i != ( numColumns - 1 ) )
{
for( j = l; j < numColumns; j++ )
{
for( s = 0.0f, k = l; k < numRows; k++ )
{
s += ( *this )[k][i] * ( *this )[k][j];
2012-11-26 18:58:24 +00:00
}
f = ( s / ( *this )[i][i] ) * g;
for( k = i; k < numRows; k++ )
{
( *this )[k][j] += f * ( *this )[k][i];
2012-11-26 18:58:24 +00:00
}
}
}
for( j = i; j < numRows; j++ )
{
( *this )[j][i] *= g;
2012-11-26 18:58:24 +00:00
}
}
else
{
for( j = i; j < numRows; j++ )
{
( *this )[j][i] = 0.0f;
2012-11-26 18:58:24 +00:00
}
}
( *this )[i][i] += 1.0f;
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::SVD_Factor
in-place factorization: U * Diag(w) * V.Transpose()
known as the Singular Value Decomposition.
U is a column-orthogonal matrix which overwrites the original matrix.
w is a diagonal matrix with all elements >= 0 which are the singular values.
V is the transpose of an orthogonal matrix.
============
*/
bool idMatX::SVD_Factor( idVecX& w, idMatX& V )
{
2012-11-26 18:58:24 +00:00
int flag, i, its, j, jj, k, l, nm;
double c, f, h, s, x, y, z, r, g = 0.0f;
float anorm = 0.0f;
idVecX rv1;
if( numRows < numColumns )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
rv1.SetData( numColumns, VECX_ALLOCA( numColumns ) );
rv1.Zero();
w.Zero( numColumns );
V.Zero( numColumns, numColumns );
2012-11-26 18:58:24 +00:00
SVD_BiDiag( w, rv1, anorm );
SVD_InitialWV( w, V, rv1 );
for( k = numColumns - 1; k >= 0; k-- )
{
for( its = 1; its <= 30; its++ )
{
2012-11-26 18:58:24 +00:00
flag = 1;
nm = 0;
for( l = k; l >= 0; l-- )
{
2012-11-26 18:58:24 +00:00
nm = l - 1;
if( ( idMath::Fabs( rv1[l] ) + anorm ) == anorm /* idMath::Fabs( rv1[l] ) < idMath::FLT_EPSILON */ )
{
2012-11-26 18:58:24 +00:00
flag = 0;
break;
}
if( ( idMath::Fabs( w[nm] ) + anorm ) == anorm /* idMath::Fabs( w[nm] ) < idMath::FLT_EPSILON */ )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( flag )
{
2012-11-26 18:58:24 +00:00
c = 0.0f;
s = 1.0f;
for( i = l; i <= k; i++ )
{
2012-11-26 18:58:24 +00:00
f = s * rv1[i];
if( ( idMath::Fabs( f ) + anorm ) != anorm /* idMath::Fabs( f ) > idMath::FLT_EPSILON */ )
{
2012-11-26 18:58:24 +00:00
g = w[i];
h = Pythag( f, g );
w[i] = h;
h = 1.0f / h;
c = g * h;
s = -f * h;
for( j = 0; j < numRows; j++ )
{
y = ( *this )[j][nm];
z = ( *this )[j][i];
( *this )[j][nm] = y * c + z * s;
( *this )[j][i] = z * c - y * s;
2012-11-26 18:58:24 +00:00
}
}
}
}
z = w[k];
if( l == k )
{
if( z < 0.0f )
{
2012-11-26 18:58:24 +00:00
w[k] = -z;
for( j = 0; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
V[j][k] = -V[j][k];
}
}
break;
}
if( its == 30 )
{
2012-11-26 18:58:24 +00:00
return false; // no convergence
}
x = w[l];
nm = k - 1;
y = w[nm];
g = rv1[nm];
h = rv1[k];
f = ( ( y - z ) * ( y + z ) + ( g - h ) * ( g + h ) ) / ( 2.0f * h * y );
g = Pythag( f, 1.0f );
r = ( f >= 0.0f ? g : - g );
f = ( ( x - z ) * ( x + z ) + h * ( ( y / ( f + r ) ) - h ) ) / x;
2012-11-26 18:58:24 +00:00
c = s = 1.0f;
for( j = l; j <= nm; j++ )
{
2012-11-26 18:58:24 +00:00
i = j + 1;
g = rv1[i];
y = w[i];
h = s * g;
g = c * g;
z = Pythag( f, h );
rv1[j] = z;
c = f / z;
s = h / z;
f = x * c + g * s;
g = g * c - x * s;
h = y * s;
y = y * c;
for( jj = 0; jj < numColumns; jj++ )
{
2012-11-26 18:58:24 +00:00
x = V[jj][j];
z = V[jj][i];
V[jj][j] = x * c + z * s;
V[jj][i] = z * c - x * s;
}
z = Pythag( f, h );
w[j] = z;
if( z )
{
2012-11-26 18:58:24 +00:00
z = 1.0f / z;
c = f * z;
s = h * z;
}
f = ( c * g ) + ( s * y );
x = ( c * y ) - ( s * g );
for( jj = 0; jj < numRows; jj++ )
{
y = ( *this )[jj][j];
z = ( *this )[jj][i];
( *this )[jj][j] = y * c + z * s;
( *this )[jj][i] = z * c - y * s;
2012-11-26 18:58:24 +00:00
}
}
rv1[l] = 0.0f;
rv1[k] = f;
w[k] = x;
}
}
return true;
}
/*
============
idMatX::SVD_Solve
Solve Ax = b with A factored as: U * Diag(w) * V.Transpose()
============
*/
void idMatX::SVD_Solve( idVecX& x, const idVecX& b, const idVecX& w, const idMatX& V ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum;
idVecX tmp;
2012-11-26 18:58:24 +00:00
assert( x.GetSize() >= numColumns );
assert( b.GetSize() >= numColumns );
assert( w.GetSize() == numColumns );
assert( V.GetNumRows() == numColumns && V.GetNumColumns() == numColumns );
2012-11-26 18:58:24 +00:00
tmp.SetData( numColumns, VECX_ALLOCA( numColumns ) );
for( i = 0; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
if( w[i] >= idMath::FLT_EPSILON )
{
for( j = 0; j < numRows; j++ )
{
sum += ( *this )[j][i] * b[j];
2012-11-26 18:58:24 +00:00
}
sum /= w[i];
}
tmp[i] = sum;
}
for( i = 0; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( j = 0; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
sum += V[i][j] * tmp[j];
}
x[i] = sum;
}
}
/*
============
idMatX::SVD_Inverse
Calculates the inverse of the matrix which is factored in-place as: U * Diag(w) * V.Transpose()
============
*/
void idMatX::SVD_Inverse( idMatX& inv, const idVecX& w, const idMatX& V ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k;
double wi, sum;
idMatX V2;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
V2 = V;
2012-11-26 18:58:24 +00:00
// V * [diag(1/w[i])]
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
wi = w[i];
wi = ( wi < idMath::FLT_EPSILON ) ? 0.0f : 1.0f / wi;
for( j = 0; j < numColumns; j++ )
{
2012-11-26 18:58:24 +00:00
V2[j][i] *= wi;
}
}
2012-11-26 18:58:24 +00:00
// V * [diag(1/w[i])] * Ut
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < numColumns; j++ )
{
sum = V2[i][0] * ( *this )[j][0];
for( k = 1; k < numColumns; k++ )
{
sum += V2[i][k] * ( *this )[j][k];
2012-11-26 18:58:24 +00:00
}
inv[i][j] = sum;
}
}
}
/*
============
idMatX::SVD_MultiplyFactors
Multiplies the factors of the in-place SVD factorization to form the original matrix.
============
*/
void idMatX::SVD_MultiplyFactors( idMatX& m, const idVecX& w, const idMatX& V ) const
{
2012-11-26 18:58:24 +00:00
int r, i, j;
double sum;
2012-11-26 18:58:24 +00:00
m.SetSize( numRows, V.GetNumRows() );
for( r = 0; r < numRows; r++ )
{
2012-11-26 18:58:24 +00:00
// calculate row of matrix
if( w[r] >= idMath::FLT_EPSILON )
{
for( i = 0; i < V.GetNumRows(); i++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( j = 0; j < numColumns; j++ )
{
sum += ( *this )[r][j] * V[i][j];
2012-11-26 18:58:24 +00:00
}
m[r][i] = sum * w[r];
}
}
else
{
for( i = 0; i < V.GetNumRows(); i++ )
{
2012-11-26 18:58:24 +00:00
m[r][i] = 0.0f;
}
}
}
}
/*
============
idMatX::Cholesky_Factor
in-place Cholesky factorization: LL'
L is a triangular matrix stored in the lower triangle.
The upper triangle is not cleared.
The initial matrix has to be symmetric positive definite.
============
*/
bool idMatX::Cholesky_Factor()
{
2012-11-26 18:58:24 +00:00
int i, j, k;
float* invSqrt;
2012-11-26 18:58:24 +00:00
double sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
invSqrt = ( float* ) _alloca16( numRows * sizeof( float ) );
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < i; j++ )
{
sum = ( *this )[i][j];
for( k = 0; k < j; k++ )
{
sum -= ( *this )[i][k] * ( *this )[j][k];
2012-11-26 18:58:24 +00:00
}
( *this )[i][j] = sum * invSqrt[j];
2012-11-26 18:58:24 +00:00
}
sum = ( *this )[i][i];
for( k = 0; k < i; k++ )
{
sum -= ( *this )[i][k] * ( *this )[i][k];
2012-11-26 18:58:24 +00:00
}
if( sum <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
invSqrt[i] = idMath::InvSqrt( sum );
( *this )[i][i] = invSqrt[i] * sum;
2012-11-26 18:58:24 +00:00
}
return true;
}
/*
============
idMatX::Cholesky_UpdateRankOne
Updates the in-place Cholesky factorization to obtain the factors for the matrix: LL' + alpha * v * v'
If offset > 0 only the lower right corner starting at (offset, offset) is updated.
============
*/
bool idMatX::Cholesky_UpdateRankOne( const idVecX& v, float alpha, int offset )
{
2012-11-26 18:58:24 +00:00
int i, j;
float* y;
2012-11-26 18:58:24 +00:00
double diag, invDiag, diagSqr, newDiag, newDiagSqr, beta, p, d;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( offset >= 0 && offset < numRows );
y = ( float* ) _alloca16( v.GetSize() * sizeof( float ) );
2012-11-26 18:58:24 +00:00
memcpy( y, v.ToFloatPtr(), v.GetSize() * sizeof( float ) );
for( i = offset; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
p = y[i];
diag = ( *this )[i][i];
2012-11-26 18:58:24 +00:00
invDiag = 1.0f / diag;
diagSqr = diag * diag;
newDiagSqr = diagSqr + alpha * p * p;
if( newDiagSqr <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[i][i] = newDiag = idMath::Sqrt( newDiagSqr );
2012-11-26 18:58:24 +00:00
alpha /= newDiagSqr;
beta = p * alpha;
alpha *= diagSqr;
for( j = i + 1; j < numRows; j++ )
{
d = ( *this )[j][i] * invDiag;
2012-11-26 18:58:24 +00:00
y[j] -= p * d;
d += beta * y[j];
( *this )[j][i] = d * newDiag;
2012-11-26 18:58:24 +00:00
}
}
return true;
}
/*
============
idMatX::Cholesky_UpdateRowColumn
Updates the in-place Cholesky factorization to obtain the factors for the matrix:
[ 0 a 0 ]
LL' + [ a b c ]
[ 0 c 0 ]
where: a = v[0,r-1], b = v[r], c = v[r+1,numRows-1]
============
*/
bool idMatX::Cholesky_UpdateRowColumn( const idVecX& v, int r )
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum;
float* original, *y;
2012-11-26 18:58:24 +00:00
idVecX addSub;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( r >= 0 && r < numRows );
addSub.SetData( numColumns, ( float* ) _alloca16( numColumns * sizeof( float ) ) );
if( r == 0 )
{
if( numColumns == 1 )
{
2012-11-26 18:58:24 +00:00
double v0 = v[0];
sum = ( *this )[0][0];
sum = sum * sum;
sum = sum + v0;
if( sum <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[0][0] = idMath::Sqrt( sum );
2012-11-26 18:58:24 +00:00
return true;
}
for( i = 0; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
addSub[i] = v[i];
}
}
else
{
original = ( float* ) _alloca16( numColumns * sizeof( float ) );
y = ( float* ) _alloca16( numColumns * sizeof( float ) );
2012-11-26 18:58:24 +00:00
// calculate original row/column of matrix
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( j = 0; j <= i; j++ )
{
sum += ( *this )[r][j] * ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
original[i] = sum;
}
2012-11-26 18:58:24 +00:00
// solve for y in L * y = original + v
for( i = 0; i < r; i++ )
{
2012-11-26 18:58:24 +00:00
sum = original[i] + v[i];
for( j = 0; j < i; j++ )
{
sum -= ( *this )[r][j] * ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
( *this )[r][i] = sum / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// if the last row/column of the matrix is updated
if( r == numColumns - 1 )
{
2012-11-26 18:58:24 +00:00
// only calculate new diagonal
sum = original[r] + v[r];
for( j = 0; j < r; j++ )
{
sum -= ( *this )[r][j] * ( *this )[r][j];
2012-11-26 18:58:24 +00:00
}
if( sum <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[r][r] = idMath::Sqrt( sum );
2012-11-26 18:58:24 +00:00
return true;
}
2012-11-26 18:58:24 +00:00
// calculate the row/column to be added to the lower right sub matrix starting at (r, r)
for( i = r; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( j = 0; j <= r; j++ )
{
sum += ( *this )[r][j] * ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
addSub[i] = v[i] - ( sum - original[i] );
}
}
2012-11-26 18:58:24 +00:00
// add row/column to the lower right sub matrix starting at (r, r)
2012-11-26 18:58:24 +00:00
#if 0
2012-11-26 18:58:24 +00:00
idVecX v1, v2;
double d;
v1.SetData( numColumns, ( float* ) _alloca16( numColumns * sizeof( float ) ) );
v2.SetData( numColumns, ( float* ) _alloca16( numColumns * sizeof( float ) ) );
2012-11-26 18:58:24 +00:00
d = idMath::SQRT_1OVER2;
v1[r] = ( 0.5f * addSub[r] + 1.0f ) * d;
v2[r] = ( 0.5f * addSub[r] - 1.0f ) * d;
for( i = r + 1; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
v1[i] = v2[i] = addSub[i] * d;
}
2012-11-26 18:58:24 +00:00
// update
if( !Cholesky_UpdateRankOne( v1, 1.0f, r ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
// downdate
if( !Cholesky_UpdateRankOne( v2, -1.0f, r ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
#else
float* v1, *v2;
2012-11-26 18:58:24 +00:00
double diag, invDiag, diagSqr, newDiag, newDiagSqr;
double alpha1, alpha2, beta1, beta2, p1, p2, d;
v1 = ( float* ) _alloca16( numColumns * sizeof( float ) );
v2 = ( float* ) _alloca16( numColumns * sizeof( float ) );
2012-11-26 18:58:24 +00:00
d = idMath::SQRT_1OVER2;
v1[r] = ( 0.5f * addSub[r] + 1.0f ) * d;
v2[r] = ( 0.5f * addSub[r] - 1.0f ) * d;
for( i = r + 1; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
v1[i] = v2[i] = addSub[i] * d;
}
2012-11-26 18:58:24 +00:00
alpha1 = 1.0f;
alpha2 = -1.0f;
2012-11-26 18:58:24 +00:00
// simultaneous update/downdate of the sub matrix starting at (r, r)
for( i = r; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
p1 = v1[i];
diag = ( *this )[i][i];
2012-11-26 18:58:24 +00:00
invDiag = 1.0f / diag;
diagSqr = diag * diag;
newDiagSqr = diagSqr + alpha1 * p1 * p1;
if( newDiagSqr <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
alpha1 /= newDiagSqr;
beta1 = p1 * alpha1;
alpha1 *= diagSqr;
2012-11-26 18:58:24 +00:00
p2 = v2[i];
diagSqr = newDiagSqr;
newDiagSqr = diagSqr + alpha2 * p2 * p2;
if( newDiagSqr <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[i][i] = newDiag = idMath::Sqrt( newDiagSqr );
2012-11-26 18:58:24 +00:00
alpha2 /= newDiagSqr;
beta2 = p2 * alpha2;
alpha2 *= diagSqr;
for( j = i + 1; j < numRows; j++ )
{
d = ( *this )[j][i] * invDiag;
2012-11-26 18:58:24 +00:00
v1[j] -= p1 * d;
d += beta1 * v1[j];
2012-11-26 18:58:24 +00:00
v2[j] -= p2 * d;
d += beta2 * v2[j];
( *this )[j][i] = d * newDiag;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
#endif
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::Cholesky_UpdateIncrement
Updates the in-place Cholesky factorization to obtain the factors for the matrix:
[ A a ]
[ a b ]
where: a = v[0,numRows-1], b = v[numRows]
============
*/
bool idMatX::Cholesky_UpdateIncrement( const idVecX& v )
{
2012-11-26 18:58:24 +00:00
int i, j;
float* x;
2012-11-26 18:58:24 +00:00
double sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows + 1 );
ChangeSize( numRows + 1, numColumns + 1, false );
x = ( float* ) _alloca16( numRows * sizeof( float ) );
2012-11-26 18:58:24 +00:00
// solve for x in L * x = v
for( i = 0; i < numRows - 1; i++ )
{
2012-11-26 18:58:24 +00:00
sum = v[i];
for( j = 0; j < i; j++ )
{
sum -= ( *this )[i][j] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// calculate new row of L and calculate the square of the diagonal entry
sum = v[numRows - 1];
for( i = 0; i < numRows - 1; i++ )
{
( *this )[numRows - 1][i] = x[i];
2012-11-26 18:58:24 +00:00
sum -= x[i] * x[i];
}
if( sum <= 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// store the diagonal entry
( *this )[numRows - 1][numRows - 1] = idMath::Sqrt( sum );
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::Cholesky_UpdateDecrement
Updates the in-place Cholesky factorization to obtain the factors for the matrix with row r and column r removed.
v should store the row of the original matrix.
============
*/
bool idMatX::Cholesky_UpdateDecrement( const idVecX& v, int r )
{
2012-11-26 18:58:24 +00:00
idVecX v1;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( r >= 0 && r < numRows );
2012-11-26 18:58:24 +00:00
v1.SetData( numRows, VECX_ALLOCA( numRows ) );
2012-11-26 18:58:24 +00:00
// update the row and column to identity
v1 = -v;
v1[r] += 1.0f;
2012-11-26 18:58:24 +00:00
// NOTE: msvc compiler bug: the this pointer stored in edi is expected to stay
// untouched when calling Cholesky_UpdateRowColumn in the if statement
#if 0
if( !Cholesky_UpdateRowColumn( v1, r ) )
{
2012-11-26 18:58:24 +00:00
#else
bool ret = Cholesky_UpdateRowColumn( v1, r );
if( !ret )
{
2012-11-26 18:58:24 +00:00
#endif
return false;
}
2012-11-26 18:58:24 +00:00
// physically remove the row and column
Update_Decrement( r );
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::Cholesky_Solve
Solve Ax = b with A factored in-place as: LL'
============
*/
void idMatX::Cholesky_Solve( idVecX& x, const idVecX& b ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( x.GetSize() >= numRows && b.GetSize() >= numRows );
2012-11-26 18:58:24 +00:00
// solve L
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
sum = b[i];
for( j = 0; j < i; j++ )
{
sum -= ( *this )[i][j] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// solve Lt
for( i = numRows - 1; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
sum = x[i];
for( j = i + 1; j < numRows; j++ )
{
sum -= ( *this )[j][i] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::Cholesky_Inverse
Calculates the inverse of the matrix which is factored in-place as: LL'
============
*/
void idMatX::Cholesky_Inverse( idMatX& inv ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
idVecX x, b;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
x.SetData( numRows, VECX_ALLOCA( numRows ) );
b.SetData( numRows, VECX_ALLOCA( numRows ) );
b.Zero();
inv.SetSize( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
b[i] = 1.0f;
Cholesky_Solve( x, b );
for( j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
inv[j][i] = x[j];
}
b[i] = 0.0f;
}
}
/*
============
idMatX::Cholesky_MultiplyFactors
Multiplies the factors of the in-place Cholesky factorization to form the original matrix.
============
*/
void idMatX::Cholesky_MultiplyFactors( idMatX& m ) const
{
2012-11-26 18:58:24 +00:00
int r, i, j;
double sum;
2012-11-26 18:58:24 +00:00
m.SetSize( numRows, numColumns );
for( r = 0; r < numRows; r++ )
{
2012-11-26 18:58:24 +00:00
// calculate row of matrix
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( j = 0; j <= i && j <= r; j++ )
{
sum += ( *this )[r][j] * ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
m[r][i] = sum;
}
}
}
/*
============
idMatX::LDLT_Factor
in-place factorization: LDL'
L is a triangular matrix stored in the lower triangle.
L has ones on the diagonal that are not stored.
D is a diagonal matrix stored on the diagonal.
The upper triangle is not cleared.
The initial matrix has to be symmetric.
============
*/
bool idMatX::LDLT_Factor()
{
2012-11-26 18:58:24 +00:00
int i, j, k;
float* v;
2012-11-26 18:58:24 +00:00
double d, sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
v = ( float* ) _alloca16( numRows * sizeof( float ) );
for( i = 0; i < numRows; i++ )
{
sum = ( *this )[i][i];
for( j = 0; j < i; j++ )
{
d = ( *this )[i][j];
v[j] = ( *this )[j][j] * d;
sum -= v[j] * d;
}
if( sum == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[i][i] = sum;
2012-11-26 18:58:24 +00:00
d = 1.0f / sum;
for( j = i + 1; j < numRows; j++ )
{
sum = ( *this )[j][i];
for( k = 0; k < i; k++ )
{
sum -= ( *this )[j][k] * v[k];
2012-11-26 18:58:24 +00:00
}
( *this )[j][i] = sum * d;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::LDLT_UpdateRankOne
Updates the in-place LDL' factorization to obtain the factors for the matrix: LDL' + alpha * v * v'
If offset > 0 only the lower right corner starting at (offset, offset) is updated.
============
*/
bool idMatX::LDLT_UpdateRankOne( const idVecX& v, float alpha, int offset )
{
2012-11-26 18:58:24 +00:00
int i, j;
float* y;
2012-11-26 18:58:24 +00:00
double diag, newDiag, beta, p, d;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( offset >= 0 && offset < numRows );
y = ( float* ) _alloca16( v.GetSize() * sizeof( float ) );
2012-11-26 18:58:24 +00:00
memcpy( y, v.ToFloatPtr(), v.GetSize() * sizeof( float ) );
for( i = offset; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
p = y[i];
diag = ( *this )[i][i];
( *this )[i][i] = newDiag = diag + alpha * p * p;
if( newDiag == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
alpha /= newDiag;
beta = p * alpha;
alpha *= diag;
for( j = i + 1; j < numRows; j++ )
{
d = ( *this )[j][i];
2012-11-26 18:58:24 +00:00
y[j] -= p * d;
d += beta * y[j];
( *this )[j][i] = d;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::LDLT_UpdateRowColumn
Updates the in-place LDL' factorization to obtain the factors for the matrix:
[ 0 a 0 ]
LDL' + [ a b c ]
[ 0 c 0 ]
where: a = v[0,r-1], b = v[r], c = v[r+1,numRows-1]
============
*/
bool idMatX::LDLT_UpdateRowColumn( const idVecX& v, int r )
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum;
float* original, *y;
2012-11-26 18:58:24 +00:00
idVecX addSub;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( r >= 0 && r < numRows );
addSub.SetData( numColumns, ( float* ) _alloca16( numColumns * sizeof( float ) ) );
if( r == 0 )
{
if( numColumns == 1 )
{
( *this )[0][0] += v[0];
2012-11-26 18:58:24 +00:00
return true;
}
for( i = 0; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
addSub[i] = v[i];
}
}
else
{
original = ( float* ) _alloca16( numColumns * sizeof( float ) );
y = ( float* ) _alloca16( numColumns * sizeof( float ) );
2012-11-26 18:58:24 +00:00
// calculate original row/column of matrix
for( i = 0; i < r; i++ )
{
y[i] = ( *this )[r][i] * ( *this )[i][i];
}
for( i = 0; i < numColumns; i++ )
{
if( i < r )
{
sum = ( *this )[i][i] * ( *this )[r][i];
2012-11-26 18:58:24 +00:00
}
else if( i == r )
{
sum = ( *this )[r][r];
}
else
{
sum = ( *this )[r][r] * ( *this )[i][r];
}
for( j = 0; j < i && j < r; j++ )
{
sum += ( *this )[i][j] * y[j];
2012-11-26 18:58:24 +00:00
}
original[i] = sum;
}
2012-11-26 18:58:24 +00:00
// solve for y in L * y = original + v
for( i = 0; i < r; i++ )
{
2012-11-26 18:58:24 +00:00
sum = original[i] + v[i];
for( j = 0; j < i; j++ )
{
sum -= ( *this )[i][j] * y[j];
2012-11-26 18:58:24 +00:00
}
y[i] = sum;
}
2012-11-26 18:58:24 +00:00
// calculate new row of L
for( i = 0; i < r; i++ )
{
( *this )[r][i] = y[i] / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// if the last row/column of the matrix is updated
if( r == numColumns - 1 )
{
2012-11-26 18:58:24 +00:00
// only calculate new diagonal
sum = original[r] + v[r];
for( j = 0; j < r; j++ )
{
sum -= ( *this )[r][j] * y[j];
2012-11-26 18:58:24 +00:00
}
if( sum == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
( *this )[r][r] = sum;
2012-11-26 18:58:24 +00:00
return true;
}
2012-11-26 18:58:24 +00:00
// calculate the row/column to be added to the lower right sub matrix starting at (r, r)
for( i = 0; i < r; i++ )
{
y[i] = ( *this )[r][i] * ( *this )[i][i];
}
for( i = r; i < numColumns; i++ )
{
if( i == r )
{
sum = ( *this )[r][r];
2012-11-26 18:58:24 +00:00
}
else
{
sum = ( *this )[r][r] * ( *this )[i][r];
}
for( j = 0; j < r; j++ )
{
sum += ( *this )[i][j] * y[j];
2012-11-26 18:58:24 +00:00
}
addSub[i] = v[i] - ( sum - original[i] );
}
}
2012-11-26 18:58:24 +00:00
// add row/column to the lower right sub matrix starting at (r, r)
2012-11-26 18:58:24 +00:00
#if 0
2012-11-26 18:58:24 +00:00
idVecX v1, v2;
double d;
v1.SetData( numColumns, ( float* ) _alloca16( numColumns * sizeof( float ) ) );
v2.SetData( numColumns, ( float* ) _alloca16( numColumns * sizeof( float ) ) );
2012-11-26 18:58:24 +00:00
d = idMath::SQRT_1OVER2;
v1[r] = ( 0.5f * addSub[r] + 1.0f ) * d;
v2[r] = ( 0.5f * addSub[r] - 1.0f ) * d;
for( i = r + 1; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
v1[i] = v2[i] = addSub[i] * d;
}
2012-11-26 18:58:24 +00:00
// update
if( !LDLT_UpdateRankOne( v1, 1.0f, r ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
// downdate
if( !LDLT_UpdateRankOne( v2, -1.0f, r ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
#else
float* v1, *v2;
2012-11-26 18:58:24 +00:00
double d, diag, newDiag, p1, p2, alpha1, alpha2, beta1, beta2;
v1 = ( float* ) _alloca16( numColumns * sizeof( float ) );
v2 = ( float* ) _alloca16( numColumns * sizeof( float ) );
2012-11-26 18:58:24 +00:00
d = idMath::SQRT_1OVER2;
v1[r] = ( 0.5f * addSub[r] + 1.0f ) * d;
v2[r] = ( 0.5f * addSub[r] - 1.0f ) * d;
for( i = r + 1; i < numColumns; i++ )
{
2012-11-26 18:58:24 +00:00
v1[i] = v2[i] = addSub[i] * d;
}
2012-11-26 18:58:24 +00:00
alpha1 = 1.0f;
alpha2 = -1.0f;
2012-11-26 18:58:24 +00:00
// simultaneous update/downdate of the sub matrix starting at (r, r)
for( i = r; i < numColumns; i++ )
{
diag = ( *this )[i][i];
2012-11-26 18:58:24 +00:00
p1 = v1[i];
newDiag = diag + alpha1 * p1 * p1;
if( newDiag == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
alpha1 /= newDiag;
beta1 = p1 * alpha1;
alpha1 *= diag;
2012-11-26 18:58:24 +00:00
diag = newDiag;
p2 = v2[i];
newDiag = diag + alpha2 * p2 * p2;
if( newDiag == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
alpha2 /= newDiag;
beta2 = p2 * alpha2;
alpha2 *= diag;
( *this )[i][i] = newDiag;
for( j = i + 1; j < numRows; j++ )
{
d = ( *this )[j][i];
2012-11-26 18:58:24 +00:00
v1[j] -= p1 * d;
d += beta1 * v1[j];
2012-11-26 18:58:24 +00:00
v2[j] -= p2 * d;
d += beta2 * v2[j];
( *this )[j][i] = d;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
#endif
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::LDLT_UpdateIncrement
Updates the in-place LDL' factorization to obtain the factors for the matrix:
[ A a ]
[ a b ]
where: a = v[0,numRows-1], b = v[numRows]
============
*/
bool idMatX::LDLT_UpdateIncrement( const idVecX& v )
{
2012-11-26 18:58:24 +00:00
int i, j;
float* x;
2012-11-26 18:58:24 +00:00
double sum, d;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows + 1 );
ChangeSize( numRows + 1, numColumns + 1, false );
x = ( float* ) _alloca16( numRows * sizeof( float ) );
2012-11-26 18:58:24 +00:00
// solve for x in L * x = v
for( i = 0; i < numRows - 1; i++ )
{
2012-11-26 18:58:24 +00:00
sum = v[i];
for( j = 0; j < i; j++ )
{
sum -= ( *this )[i][j] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum;
}
2012-11-26 18:58:24 +00:00
// calculate new row of L and calculate the diagonal entry
sum = v[numRows - 1];
for( i = 0; i < numRows - 1; i++ )
{
( *this )[numRows - 1][i] = d = x[i] / ( *this )[i][i];
2012-11-26 18:58:24 +00:00
sum -= d * x[i];
}
if( sum == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// store the diagonal entry
( *this )[numRows - 1][numRows - 1] = sum;
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::LDLT_UpdateDecrement
Updates the in-place LDL' factorization to obtain the factors for the matrix with row r and column r removed.
v should store the row of the original matrix.
============
*/
bool idMatX::LDLT_UpdateDecrement( const idVecX& v, int r )
{
2012-11-26 18:58:24 +00:00
idVecX v1;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( v.GetSize() >= numRows );
assert( r >= 0 && r < numRows );
2012-11-26 18:58:24 +00:00
v1.SetData( numRows, VECX_ALLOCA( numRows ) );
2012-11-26 18:58:24 +00:00
// update the row and column to identity
v1 = -v;
v1[r] += 1.0f;
2012-11-26 18:58:24 +00:00
// NOTE: msvc compiler bug: the this pointer stored in edi is expected to stay
// untouched when calling LDLT_UpdateRowColumn in the if statement
#if 0
if( !LDLT_UpdateRowColumn( v1, r ) )
{
2012-11-26 18:58:24 +00:00
#else
bool ret = LDLT_UpdateRowColumn( v1, r );
if( !ret )
{
2012-11-26 18:58:24 +00:00
#endif
return false;
}
2012-11-26 18:58:24 +00:00
// physically remove the row and column
Update_Decrement( r );
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::LDLT_Solve
Solve Ax = b with A factored in-place as: LDL'
============
*/
void idMatX::LDLT_Solve( idVecX& x, const idVecX& b ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
double sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( x.GetSize() >= numRows && b.GetSize() >= numRows );
2012-11-26 18:58:24 +00:00
// solve L
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
sum = b[i];
for( j = 0; j < i; j++ )
{
sum -= ( *this )[i][j] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum;
}
2012-11-26 18:58:24 +00:00
// solve D
for( i = 0; i < numRows; i++ )
{
x[i] /= ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// solve Lt
for( i = numRows - 2; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
sum = x[i];
for( j = i + 1; j < numRows; j++ )
{
sum -= ( *this )[j][i] * x[j];
2012-11-26 18:58:24 +00:00
}
x[i] = sum;
}
}
/*
============
idMatX::LDLT_Inverse
Calculates the inverse of the matrix which is factored in-place as: LDL'
============
*/
void idMatX::LDLT_Inverse( idMatX& inv ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
idVecX x, b;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
x.SetData( numRows, VECX_ALLOCA( numRows ) );
b.SetData( numRows, VECX_ALLOCA( numRows ) );
b.Zero();
inv.SetSize( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
b[i] = 1.0f;
LDLT_Solve( x, b );
for( j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
inv[j][i] = x[j];
}
b[i] = 0.0f;
}
}
/*
============
idMatX::LDLT_UnpackFactors
Unpacks the in-place LDL' factorization.
============
*/
void idMatX::LDLT_UnpackFactors( idMatX& L, idMatX& D ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
2012-11-26 18:58:24 +00:00
L.Zero( numRows, numColumns );
D.Zero( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
for( j = 0; j < i; j++ )
{
L[i][j] = ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
L[i][i] = 1.0f;
D[i][i] = ( *this )[i][i];
2012-11-26 18:58:24 +00:00
}
}
/*
============
idMatX::LDLT_MultiplyFactors
Multiplies the factors of the in-place LDL' factorization to form the original matrix.
============
*/
void idMatX::LDLT_MultiplyFactors( idMatX& m ) const
{
2012-11-26 18:58:24 +00:00
int r, i, j;
float* v;
2012-11-26 18:58:24 +00:00
double sum;
v = ( float* ) _alloca16( numRows * sizeof( float ) );
2012-11-26 18:58:24 +00:00
m.SetSize( numRows, numColumns );
for( r = 0; r < numRows; r++ )
{
2012-11-26 18:58:24 +00:00
// calculate row of matrix
for( i = 0; i < r; i++ )
{
v[i] = ( *this )[r][i] * ( *this )[i][i];
}
for( i = 0; i < numColumns; i++ )
{
if( i < r )
{
sum = ( *this )[i][i] * ( *this )[r][i];
}
else if( i == r )
{
sum = ( *this )[r][r];
2012-11-26 18:58:24 +00:00
}
else
{
sum = ( *this )[r][r] * ( *this )[i][r];
}
for( j = 0; j < i && j < r; j++ )
{
sum += ( *this )[i][j] * v[j];
2012-11-26 18:58:24 +00:00
}
m[r][i] = sum;
}
}
}
/*
============
idMatX::TriDiagonal_ClearTriangles
============
*/
void idMatX::TriDiagonal_ClearTriangles()
{
2012-11-26 18:58:24 +00:00
int i, j;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
for( i = 0; i < numRows - 2; i++ )
{
for( j = i + 2; j < numColumns; j++ )
{
( *this )[i][j] = 0.0f;
( *this )[j][i] = 0.0f;
2012-11-26 18:58:24 +00:00
}
}
}
/*
============
idMatX::TriDiagonal_Solve
Solve Ax = b with A being tridiagonal.
============
*/
bool idMatX::TriDiagonal_Solve( idVecX& x, const idVecX& b ) const
{
2012-11-26 18:58:24 +00:00
int i;
float d;
idVecX tmp;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
assert( x.GetSize() >= numRows && b.GetSize() >= numRows );
2012-11-26 18:58:24 +00:00
tmp.SetData( numRows, VECX_ALLOCA( numRows ) );
d = ( *this )[0][0];
if( d == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
d = 1.0f / d;
x[0] = b[0] * d;
for( i = 1; i < numRows; i++ )
{
tmp[i] = ( *this )[i - 1][i] * d;
d = ( *this )[i][i] - ( *this )[i][i - 1] * tmp[i];
if( d == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
d = 1.0f / d;
x[i] = ( b[i] - ( *this )[i][i - 1] * x[i - 1] ) * d;
2012-11-26 18:58:24 +00:00
}
for( i = numRows - 2; i >= 0; i-- )
{
x[i] -= tmp[i + 1] * x[i + 1];
2012-11-26 18:58:24 +00:00
}
return true;
}
/*
============
idMatX::TriDiagonal_Inverse
Calculates the inverse of a tri-diagonal matrix.
============
*/
void idMatX::TriDiagonal_Inverse( idMatX& inv ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
idVecX x, b;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
x.SetData( numRows, VECX_ALLOCA( numRows ) );
b.SetData( numRows, VECX_ALLOCA( numRows ) );
b.Zero();
inv.SetSize( numRows, numColumns );
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
b[i] = 1.0f;
TriDiagonal_Solve( x, b );
for( j = 0; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
inv[j][i] = x[j];
}
b[i] = 0.0f;
}
}
/*
============
idMatX::HouseholderReduction
Householder reduction to symmetric tri-diagonal form.
The original matrix is replaced by an orthogonal matrix effecting the accumulated householder transformations.
The diagonal elements of the diagonal matrix are stored in diag.
The off-diagonal elements of the diagonal matrix are stored in subd.
The initial matrix has to be symmetric.
============
*/
void idMatX::HouseholderReduction( idVecX& diag, idVecX& subd )
{
2012-11-26 18:58:24 +00:00
int i0, i1, i2, i3;
float h, f, g, invH, halfFdivH, scale, invScale, sum;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
diag.SetSize( numRows );
subd.SetSize( numRows );
for( i0 = numRows - 1, i3 = numRows - 2; i0 >= 1; i0--, i3-- )
{
2012-11-26 18:58:24 +00:00
h = 0.0f;
scale = 0.0f;
if( i3 > 0 )
{
for( i2 = 0; i2 <= i3; i2++ )
{
scale += idMath::Fabs( ( *this )[i0][i2] );
2012-11-26 18:58:24 +00:00
}
if( scale == 0 )
{
subd[i0] = ( *this )[i0][i3];
}
else
{
2012-11-26 18:58:24 +00:00
invScale = 1.0f / scale;
for( i2 = 0; i2 <= i3; i2++ )
2012-11-26 18:58:24 +00:00
{
( *this )[i0][i2] *= invScale;
h += ( *this )[i0][i2] * ( *this )[i0][i2];
2012-11-26 18:58:24 +00:00
}
f = ( *this )[i0][i3];
2012-11-26 18:58:24 +00:00
g = idMath::Sqrt( h );
if( f > 0.0f )
{
2012-11-26 18:58:24 +00:00
g = -g;
}
subd[i0] = scale * g;
h -= f * g;
( *this )[i0][i3] = f - g;
2012-11-26 18:58:24 +00:00
f = 0.0f;
invH = 1.0f / h;
for( i1 = 0; i1 <= i3; i1++ )
{
( *this )[i1][i0] = ( *this )[i0][i1] * invH;
2012-11-26 18:58:24 +00:00
g = 0.0f;
for( i2 = 0; i2 <= i1; i2++ )
{
g += ( *this )[i1][i2] * ( *this )[i0][i2];
2012-11-26 18:58:24 +00:00
}
for( i2 = i1 + 1; i2 <= i3; i2++ )
{
g += ( *this )[i2][i1] * ( *this )[i0][i2];
2012-11-26 18:58:24 +00:00
}
subd[i1] = g * invH;
f += subd[i1] * ( *this )[i0][i1];
2012-11-26 18:58:24 +00:00
}
halfFdivH = 0.5f * f * invH;
for( i1 = 0; i1 <= i3; i1++ )
{
f = ( *this )[i0][i1];
2012-11-26 18:58:24 +00:00
g = subd[i1] - halfFdivH * f;
subd[i1] = g;
for( i2 = 0; i2 <= i1; i2++ )
{
( *this )[i1][i2] -= f * subd[i2] + g * ( *this )[i0][i2];
2012-11-26 18:58:24 +00:00
}
}
}
2012-11-26 18:58:24 +00:00
}
else
{
subd[i0] = ( *this )[i0][i3];
}
2012-11-26 18:58:24 +00:00
diag[i0] = h;
}
2012-11-26 18:58:24 +00:00
diag[0] = 0.0f;
subd[0] = 0.0f;
for( i0 = 0, i3 = -1; i0 <= numRows - 1; i0++, i3++ )
{
if( diag[i0] )
{
for( i1 = 0; i1 <= i3; i1++ )
{
2012-11-26 18:58:24 +00:00
sum = 0.0f;
for( i2 = 0; i2 <= i3; i2++ )
{
sum += ( *this )[i0][i2] * ( *this )[i2][i1];
2012-11-26 18:58:24 +00:00
}
for( i2 = 0; i2 <= i3; i2++ )
{
( *this )[i2][i1] -= sum * ( *this )[i2][i0];
2012-11-26 18:58:24 +00:00
}
}
}
diag[i0] = ( *this )[i0][i0];
( *this )[i0][i0] = 1.0f;
for( i1 = 0; i1 <= i3; i1++ )
{
( *this )[i1][i0] = 0.0f;
( *this )[i0][i1] = 0.0f;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// re-order
for( i0 = 1, i3 = 0; i0 < numRows; i0++, i3++ )
{
2012-11-26 18:58:24 +00:00
subd[i3] = subd[i0];
}
subd[numRows - 1] = 0.0f;
2012-11-26 18:58:24 +00:00
}
/*
============
idMatX::QL
QL algorithm with implicit shifts to determine the eigenvalues and eigenvectors of a symmetric tri-diagonal matrix.
diag contains the diagonal elements of the symmetric tri-diagonal matrix on input and is overwritten with the eigenvalues.
subd contains the off-diagonal elements of the symmetric tri-diagonal matrix and is destroyed.
This matrix has to be either the identity matrix to determine the eigenvectors for a symmetric tri-diagonal matrix,
or the matrix returned by the Householder reduction to determine the eigenvalues for the original symmetric matrix.
============
*/
bool idMatX::QL( idVecX& diag, idVecX& subd )
{
const int maxIter = 32;
2012-11-26 18:58:24 +00:00
int i0, i1, i2, i3;
float a, b, f, g, r, p, s, c;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
for( i0 = 0; i0 < numRows; i0++ )
{
for( i1 = 0; i1 < maxIter; i1++ )
{
for( i2 = i0; i2 <= numRows - 2; i2++ )
{
a = idMath::Fabs( diag[i2] ) + idMath::Fabs( diag[i2 + 1] );
if( idMath::Fabs( subd[i2] ) + a == a )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i2 == i0 )
{
2012-11-26 18:58:24 +00:00
break;
}
g = ( diag[i0 + 1] - diag[i0] ) / ( 2.0f * subd[i0] );
2012-11-26 18:58:24 +00:00
r = idMath::Sqrt( g * g + 1.0f );
if( g < 0.0f )
{
2012-11-26 18:58:24 +00:00
g = diag[i2] - diag[i0] + subd[i0] / ( g - r );
}
else
{
2012-11-26 18:58:24 +00:00
g = diag[i2] - diag[i0] + subd[i0] / ( g + r );
}
s = 1.0f;
c = 1.0f;
p = 0.0f;
for( i3 = i2 - 1; i3 >= i0; i3-- )
{
2012-11-26 18:58:24 +00:00
f = s * subd[i3];
b = c * subd[i3];
if( idMath::Fabs( f ) >= idMath::Fabs( g ) )
{
2012-11-26 18:58:24 +00:00
c = g / f;
r = idMath::Sqrt( c * c + 1.0f );
subd[i3 + 1] = f * r;
2012-11-26 18:58:24 +00:00
s = 1.0f / r;
c *= s;
}
else
{
2012-11-26 18:58:24 +00:00
s = f / g;
r = idMath::Sqrt( s * s + 1.0f );
subd[i3 + 1] = g * r;
2012-11-26 18:58:24 +00:00
c = 1.0f / r;
s *= c;
}
g = diag[i3 + 1] - p;
2012-11-26 18:58:24 +00:00
r = ( diag[i3] - g ) * s + 2.0f * b * c;
p = s * r;
diag[i3 + 1] = g + p;
2012-11-26 18:58:24 +00:00
g = c * r - b;
for( int i4 = 0; i4 < numRows; i4++ )
{
f = ( *this )[i4][i3 + 1];
( *this )[i4][i3 + 1] = s * ( *this )[i4][i3] + c * f;
( *this )[i4][i3] = c * ( *this )[i4][i3] - s * f;
2012-11-26 18:58:24 +00:00
}
}
diag[i0] -= p;
subd[i0] = g;
subd[i2] = 0.0f;
}
if( i1 == maxIter )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
return true;
}
/*
============
idMatX::Eigen_SolveSymmetricTriDiagonal
Determine eigen values and eigen vectors for a symmetric tri-diagonal matrix.
The eigen values are stored in 'eigenValues'.
Column i of the original matrix will store the eigen vector corresponding to the eigenValues[i].
The initial matrix has to be symmetric tri-diagonal.
============
*/
bool idMatX::Eigen_SolveSymmetricTriDiagonal( idVecX& eigenValues )
{
2012-11-26 18:58:24 +00:00
int i;
idVecX subd;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
subd.SetData( numRows, VECX_ALLOCA( numRows ) );
eigenValues.SetSize( numRows );
for( i = 0; i < numRows - 1; i++ )
{
eigenValues[i] = ( *this )[i][i];
subd[i] = ( *this )[i + 1][i];
2012-11-26 18:58:24 +00:00
}
eigenValues[numRows - 1] = ( *this )[numRows - 1][numRows - 1];
2012-11-26 18:58:24 +00:00
Identity();
2012-11-26 18:58:24 +00:00
return QL( eigenValues, subd );
}
/*
============
idMatX::Eigen_SolveSymmetric
Determine eigen values and eigen vectors for a symmetric matrix.
The eigen values are stored in 'eigenValues'.
Column i of the original matrix will store the eigen vector corresponding to the eigenValues[i].
The initial matrix has to be symmetric.
============
*/
bool idMatX::Eigen_SolveSymmetric( idVecX& eigenValues )
{
2012-11-26 18:58:24 +00:00
idVecX subd;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
subd.SetData( numRows, VECX_ALLOCA( numRows ) );
eigenValues.SetSize( numRows );
2012-11-26 18:58:24 +00:00
HouseholderReduction( eigenValues, subd );
return QL( eigenValues, subd );
}
/*
============
idMatX::HessenbergReduction
Reduction to Hessenberg form.
============
*/
void idMatX::HessenbergReduction( idMatX& H )
{
2012-11-26 18:58:24 +00:00
int i, j, m;
int low = 0;
int high = numRows - 1;
float scale, f, g, h;
idVecX v;
2012-11-26 18:58:24 +00:00
v.SetData( numRows, VECX_ALLOCA( numRows ) );
for( m = low + 1; m <= high - 1; m++ )
{
2012-11-26 18:58:24 +00:00
scale = 0.0f;
for( i = m; i <= high; i++ )
{
scale = scale + idMath::Fabs( H[i][m - 1] );
2012-11-26 18:58:24 +00:00
}
if( scale != 0.0f )
{
2012-11-26 18:58:24 +00:00
// compute Householder transformation.
h = 0.0f;
for( i = high; i >= m; i-- )
{
v[i] = H[i][m - 1] / scale;
2012-11-26 18:58:24 +00:00
h += v[i] * v[i];
}
g = idMath::Sqrt( h );
if( v[m] > 0.0f )
{
2012-11-26 18:58:24 +00:00
g = -g;
}
h = h - v[m] * g;
v[m] = v[m] - g;
2012-11-26 18:58:24 +00:00
// apply Householder similarity transformation
// H = (I-u*u'/h)*H*(I-u*u')/h)
for( j = m; j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
f = 0.0f;
for( i = high; i >= m; i-- )
{
2012-11-26 18:58:24 +00:00
f += v[i] * H[i][j];
}
f = f / h;
for( i = m; i <= high; i++ )
{
2012-11-26 18:58:24 +00:00
H[i][j] -= f * v[i];
}
}
for( i = 0; i <= high; i++ )
{
2012-11-26 18:58:24 +00:00
f = 0.0f;
for( j = high; j >= m; j-- )
{
2012-11-26 18:58:24 +00:00
f += v[j] * H[i][j];
}
f = f / h;
for( j = m; j <= high; j++ )
{
2012-11-26 18:58:24 +00:00
H[i][j] -= f * v[j];
}
}
v[m] = scale * v[m];
H[m][m - 1] = scale * g;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// accumulate transformations
Identity();
for( int m = high - 1; m >= low + 1; m-- )
{
if( H[m][m - 1] != 0.0f )
{
for( i = m + 1; i <= high; i++ )
{
v[i] = H[i][m - 1];
2012-11-26 18:58:24 +00:00
}
for( j = m; j <= high; j++ )
{
2012-11-26 18:58:24 +00:00
g = 0.0f;
for( i = m; i <= high; i++ )
{
g += v[i] * ( *this )[i][j];
2012-11-26 18:58:24 +00:00
}
// float division to avoid possible underflow
g = ( g / v[m] ) / H[m][m - 1];
for( i = m; i <= high; i++ )
{
( *this )[i][j] += g * v[i];
2012-11-26 18:58:24 +00:00
}
}
}
}
}
/*
============
idMatX::ComplexDivision
Complex scalar division.
============
*/
void idMatX::ComplexDivision( float xr, float xi, float yr, float yi, float& cdivr, float& cdivi )
{
2012-11-26 18:58:24 +00:00
float r, d;
if( idMath::Fabs( yr ) > idMath::Fabs( yi ) )
{
2012-11-26 18:58:24 +00:00
r = yi / yr;
d = yr + r * yi;
cdivr = ( xr + r * xi ) / d;
cdivi = ( xi - r * xr ) / d;
}
else
{
2012-11-26 18:58:24 +00:00
r = yr / yi;
d = yi + r * yr;
cdivr = ( r * xr + xi ) / d;
cdivi = ( r * xi - xr ) / d;
}
}
/*
============
idMatX::HessenbergToRealSchur
Reduction from Hessenberg to real Schur form.
============
*/
bool idMatX::HessenbergToRealSchur( idMatX& H, idVecX& realEigenValues, idVecX& imaginaryEigenValues )
{
2012-11-26 18:58:24 +00:00
int i, j, k;
int n = numRows - 1;
int low = 0;
int high = numRows - 1;
float eps = 2e-16f, exshift = 0.0f;
float p = 0.0f, q = 0.0f, r = 0.0f, s = 0.0f, z = 0.0f, t, w, x, y;
2012-11-26 18:58:24 +00:00
// store roots isolated by balanc and compute matrix norm
float norm = 0.0f;
for( i = 0; i < numRows; i++ )
{
if( i < low || i > high )
{
2012-11-26 18:58:24 +00:00
realEigenValues[i] = H[i][i];
imaginaryEigenValues[i] = 0.0f;
}
for( j = Max( i - 1, 0 ); j < numRows; j++ )
{
2012-11-26 18:58:24 +00:00
norm = norm + idMath::Fabs( H[i][j] );
}
}
2012-11-26 18:58:24 +00:00
int iter = 0;
while( n >= low )
{
2012-11-26 18:58:24 +00:00
// look for single small sub-diagonal element
int l = n;
while( l > low )
{
s = idMath::Fabs( H[l - 1][l - 1] ) + idMath::Fabs( H[l][l] );
if( s == 0.0f )
{
2012-11-26 18:58:24 +00:00
s = norm;
}
if( idMath::Fabs( H[l][l - 1] ) < eps * s )
{
2012-11-26 18:58:24 +00:00
break;
}
l--;
}
2012-11-26 18:58:24 +00:00
// check for convergence
if( l == n ) // one root found
{
2012-11-26 18:58:24 +00:00
H[n][n] = H[n][n] + exshift;
realEigenValues[n] = H[n][n];
imaginaryEigenValues[n] = 0.0f;
n--;
iter = 0;
}
else if( l == n - 1 ) // two roots found
{
w = H[n][n - 1] * H[n - 1][n];
p = ( H[n - 1][n - 1] - H[n][n] ) / 2.0f;
2012-11-26 18:58:24 +00:00
q = p * p + w;
z = idMath::Sqrt( idMath::Fabs( q ) );
H[n][n] = H[n][n] + exshift;
H[n - 1][n - 1] = H[n - 1][n - 1] + exshift;
2012-11-26 18:58:24 +00:00
x = H[n][n];
if( q >= 0.0f ) // real pair
{
if( p >= 0.0f )
{
2012-11-26 18:58:24 +00:00
z = p + z;
}
else
{
2012-11-26 18:58:24 +00:00
z = p - z;
}
realEigenValues[n - 1] = x + z;
realEigenValues[n] = realEigenValues[n - 1];
if( z != 0.0f )
{
2012-11-26 18:58:24 +00:00
realEigenValues[n] = x - w / z;
}
imaginaryEigenValues[n - 1] = 0.0f;
2012-11-26 18:58:24 +00:00
imaginaryEigenValues[n] = 0.0f;
x = H[n][n - 1];
2012-11-26 18:58:24 +00:00
s = idMath::Fabs( x ) + idMath::Fabs( z );
p = x / s;
q = z / s;
r = idMath::Sqrt( p * p + q * q );
p = p / r;
q = q / r;
2012-11-26 18:58:24 +00:00
// modify row
for( j = n - 1; j < numRows; j++ )
{
z = H[n - 1][j];
H[n - 1][j] = q * z + p * H[n][j];
2012-11-26 18:58:24 +00:00
H[n][j] = q * H[n][j] - p * z;
}
2012-11-26 18:58:24 +00:00
// modify column
for( i = 0; i <= n; i++ )
{
z = H[i][n - 1];
H[i][n - 1] = q * z + p * H[i][n];
2012-11-26 18:58:24 +00:00
H[i][n] = q * H[i][n] - p * z;
}
2012-11-26 18:58:24 +00:00
// accumulate transformations
for( i = low; i <= high; i++ )
{
z = ( *this )[i][n - 1];
( *this )[i][n - 1] = q * z + p * ( *this )[i][n];
( *this )[i][n] = q * ( *this )[i][n] - p * z;
2012-11-26 18:58:24 +00:00
}
}
else // complex pair
{
realEigenValues[n - 1] = x + p;
2012-11-26 18:58:24 +00:00
realEigenValues[n] = x + p;
imaginaryEigenValues[n - 1] = z;
2012-11-26 18:58:24 +00:00
imaginaryEigenValues[n] = -z;
}
n = n - 2;
iter = 0;
}
else // no convergence yet
{
2012-11-26 18:58:24 +00:00
// form shift
x = H[n][n];
y = 0.0f;
w = 0.0f;
if( l < n )
{
y = H[n - 1][n - 1];
w = H[n][n - 1] * H[n - 1][n];
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// Wilkinson's original ad hoc shift
if( iter == 10 )
{
2012-11-26 18:58:24 +00:00
exshift += x;
for( i = low; i <= n; i++ )
{
2012-11-26 18:58:24 +00:00
H[i][i] -= x;
}
s = idMath::Fabs( H[n][n - 1] ) + idMath::Fabs( H[n - 1][n - 2] );
2012-11-26 18:58:24 +00:00
x = y = 0.75f * s;
w = -0.4375f * s * s;
}
2012-11-26 18:58:24 +00:00
// new ad hoc shift
if( iter == 30 )
{
2012-11-26 18:58:24 +00:00
s = ( y - x ) / 2.0f;
s = s * s + w;
if( s > 0 )
{
2012-11-26 18:58:24 +00:00
s = idMath::Sqrt( s );
if( y < x )
{
2012-11-26 18:58:24 +00:00
s = -s;
}
s = x - w / ( ( y - x ) / 2.0f + s );
for( i = low; i <= n; i++ )
{
2012-11-26 18:58:24 +00:00
H[i][i] -= s;
}
exshift += s;
x = y = w = 0.964f;
}
}
2012-11-26 18:58:24 +00:00
iter = iter + 1;
2012-11-26 18:58:24 +00:00
// look for two consecutive small sub-diagonal elements
int m;
for( m = n - 2; m >= l; m-- )
{
2012-11-26 18:58:24 +00:00
z = H[m][m];
r = x - z;
s = y - z;
p = ( r * s - w ) / H[m + 1][m] + H[m][m + 1];
q = H[m + 1][m + 1] - z - r - s;
r = H[m + 2][m + 1];
2012-11-26 18:58:24 +00:00
s = idMath::Fabs( p ) + idMath::Fabs( q ) + idMath::Fabs( r );
p = p / s;
q = q / s;
r = r / s;
if( m == l )
{
2012-11-26 18:58:24 +00:00
break;
}
if( idMath::Fabs( H[m][m - 1] ) * ( idMath::Fabs( q ) + idMath::Fabs( r ) ) <
eps * ( idMath::Fabs( p ) * ( idMath::Fabs( H[m - 1][m - 1] ) + idMath::Fabs( z ) + idMath::Fabs( H[m + 1][m + 1] ) ) ) )
{
2012-11-26 18:58:24 +00:00
break;
}
}
for( i = m + 2; i <= n; i++ )
{
H[i][i - 2] = 0.0f;
if( i > m + 2 )
{
H[i][i - 3] = 0.0f;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// double QR step involving rows l:n and columns m:n
for( k = m; k <= n - 1; k++ )
{
bool notlast = ( k != n - 1 );
if( k != m )
{
p = H[k][k - 1];
q = H[k + 1][k - 1];
r = ( notlast ? H[k + 2][k - 1] : 0.0f );
2012-11-26 18:58:24 +00:00
x = idMath::Fabs( p ) + idMath::Fabs( q ) + idMath::Fabs( r );
if( x != 0.0f )
{
2012-11-26 18:58:24 +00:00
p = p / x;
q = q / x;
r = r / x;
}
}
if( x == 0.0f )
{
2012-11-26 18:58:24 +00:00
break;
}
s = idMath::Sqrt( p * p + q * q + r * r );
if( p < 0.0f )
{
2012-11-26 18:58:24 +00:00
s = -s;
}
if( s != 0.0f )
{
if( k != m )
{
H[k][k - 1] = -s * x;
}
else if( l != m )
{
H[k][k - 1] = -H[k][k - 1];
2012-11-26 18:58:24 +00:00
}
p = p + s;
x = p / s;
y = q / s;
z = r / s;
q = q / p;
r = r / p;
2012-11-26 18:58:24 +00:00
// modify row
for( j = k; j < numRows; j++ )
{
p = H[k][j] + q * H[k + 1][j];
if( notlast )
{
p = p + r * H[k + 2][j];
H[k + 2][j] = H[k + 2][j] - p * z;
2012-11-26 18:58:24 +00:00
}
H[k][j] = H[k][j] - p * x;
H[k + 1][j] = H[k + 1][j] - p * y;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// modify column
for( i = 0; i <= Min( n, k + 3 ); i++ )
{
p = x * H[i][k] + y * H[i][k + 1];
if( notlast )
{
p = p + z * H[i][k + 2];
H[i][k + 2] = H[i][k + 2] - p * r;
2012-11-26 18:58:24 +00:00
}
H[i][k] = H[i][k] - p;
H[i][k + 1] = H[i][k + 1] - p * q;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// accumulate transformations
for( i = low; i <= high; i++ )
{
p = x * ( *this )[i][k] + y * ( *this )[i][k + 1];
if( notlast )
{
p = p + z * ( *this )[i][k + 2];
( *this )[i][k + 2] = ( *this )[i][k + 2] - p * r;
2012-11-26 18:58:24 +00:00
}
( *this )[i][k] = ( *this )[i][k] - p;
( *this )[i][k + 1] = ( *this )[i][k + 1] - p * q;
2012-11-26 18:58:24 +00:00
}
}
}
}
}
// backsubstitute to find vectors of upper triangular form
if( norm == 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( n = numRows - 1; n >= 0; n-- )
{
2012-11-26 18:58:24 +00:00
p = realEigenValues[n];
q = imaginaryEigenValues[n];
if( q == 0.0f ) // real vector
{
2012-11-26 18:58:24 +00:00
int l = n;
H[n][n] = 1.0f;
for( i = n - 1; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
w = H[i][i] - p;
r = 0.0f;
for( j = l; j <= n; j++ )
{
2012-11-26 18:58:24 +00:00
r = r + H[i][j] * H[j][n];
}
if( imaginaryEigenValues[i] < 0.0f )
{
2012-11-26 18:58:24 +00:00
z = w;
s = r;
}
else
{
2012-11-26 18:58:24 +00:00
l = i;
if( imaginaryEigenValues[i] == 0.0f )
{
if( w != 0.0f )
{
2012-11-26 18:58:24 +00:00
H[i][n] = -r / w;
}
else
{
2012-11-26 18:58:24 +00:00
H[i][n] = -r / ( eps * norm );
}
}
else // solve real equations
{
x = H[i][i + 1];
y = H[i + 1][i];
2012-11-26 18:58:24 +00:00
q = ( realEigenValues[i] - p ) * ( realEigenValues[i] - p ) + imaginaryEigenValues[i] * imaginaryEigenValues[i];
t = ( x * s - z * r ) / q;
H[i][n] = t;
if( idMath::Fabs( x ) > idMath::Fabs( z ) )
{
H[i + 1][n] = ( -r - w * t ) / x;
}
else
{
H[i + 1][n] = ( -s - y * t ) / z;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// overflow control
t = idMath::Fabs( H[i][n] );
if( ( eps * t ) * t > 1 )
{
for( j = i; j <= n; j++ )
{
2012-11-26 18:58:24 +00:00
H[j][n] = H[j][n] / t;
}
}
}
}
}
else if( q < 0.0f ) // complex vector
{
int l = n - 1;
2012-11-26 18:58:24 +00:00
// last vector component imaginary so matrix is triangular
if( idMath::Fabs( H[n][n - 1] ) > idMath::Fabs( H[n - 1][n] ) )
{
H[n - 1][n - 1] = q / H[n][n - 1];
H[n - 1][n] = -( H[n][n] - p ) / H[n][n - 1];
}
else
{
ComplexDivision( 0.0f, -H[n - 1][n], H[n - 1][n - 1] - p, q, H[n - 1][n - 1], H[n - 1][n] );
2012-11-26 18:58:24 +00:00
}
H[n][n - 1] = 0.0f;
2012-11-26 18:58:24 +00:00
H[n][n] = 1.0f;
for( i = n - 2; i >= 0; i-- )
{
2012-11-26 18:58:24 +00:00
float ra, sa, vr, vi;
ra = 0.0f;
sa = 0.0f;
for( j = l; j <= n; j++ )
{
ra = ra + H[i][j] * H[j][n - 1];
2012-11-26 18:58:24 +00:00
sa = sa + H[i][j] * H[j][n];
}
w = H[i][i] - p;
if( imaginaryEigenValues[i] < 0.0f )
{
2012-11-26 18:58:24 +00:00
z = w;
r = ra;
s = sa;
}
else
{
2012-11-26 18:58:24 +00:00
l = i;
if( imaginaryEigenValues[i] == 0.0f )
{
ComplexDivision( -ra, -sa, w, q, H[i][n - 1], H[i][n] );
}
else
{
2012-11-26 18:58:24 +00:00
// solve complex equations
x = H[i][i + 1];
y = H[i + 1][i];
2012-11-26 18:58:24 +00:00
vr = ( realEigenValues[i] - p ) * ( realEigenValues[i] - p ) + imaginaryEigenValues[i] * imaginaryEigenValues[i] - q * q;
vi = ( realEigenValues[i] - p ) * 2.0f * q;
if( vr == 0.0f && vi == 0.0f )
{
2012-11-26 18:58:24 +00:00
vr = eps * norm * ( idMath::Fabs( w ) + idMath::Fabs( q ) + idMath::Fabs( x ) + idMath::Fabs( y ) + idMath::Fabs( z ) );
}
ComplexDivision( x * r - z * ra + q * sa, x * s - z * sa - q * ra, vr, vi, H[i][n - 1], H[i][n] );
if( idMath::Fabs( x ) > ( idMath::Fabs( z ) + idMath::Fabs( q ) ) )
{
H[i + 1][n - 1] = ( -ra - w * H[i][n - 1] + q * H[i][n] ) / x;
H[i + 1][n] = ( -sa - w * H[i][n] - q * H[i][n - 1] ) / x;
}
else
{
ComplexDivision( -r - y * H[i][n - 1], -s - y * H[i][n], z, q, H[i + 1][n - 1], H[i + 1][n] );
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// overflow control
t = Max( idMath::Fabs( H[i][n - 1] ), idMath::Fabs( H[i][n] ) );
if( ( eps * t ) * t > 1 )
{
for( j = i; j <= n; j++ )
{
H[j][n - 1] = H[j][n - 1] / t;
2012-11-26 18:58:24 +00:00
H[j][n] = H[j][n] / t;
}
}
}
}
}
}
2012-11-26 18:58:24 +00:00
// vectors of isolated roots
for( i = 0; i < numRows; i++ )
{
if( i < low || i > high )
{
for( j = i; j < numRows; j++ )
{
( *this )[i][j] = H[i][j];
2012-11-26 18:58:24 +00:00
}
}
}
2012-11-26 18:58:24 +00:00
// back transformation to get eigenvectors of original matrix
for( j = numRows - 1; j >= low; j-- )
{
for( i = low; i <= high; i++ )
{
2012-11-26 18:58:24 +00:00
z = 0.0f;
for( k = low; k <= Min( j, high ); k++ )
{
z = z + ( *this )[i][k] * H[k][j];
2012-11-26 18:58:24 +00:00
}
( *this )[i][j] = z;
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idMatX::Eigen_Solve
Determine eigen values and eigen vectors for a square matrix.
The eigen values are stored in 'realEigenValues' and 'imaginaryEigenValues'.
Column i of the original matrix will store the eigen vector corresponding to the realEigenValues[i] and imaginaryEigenValues[i].
============
*/
bool idMatX::Eigen_Solve( idVecX& realEigenValues, idVecX& imaginaryEigenValues )
{
idMatX H;
2012-11-26 18:58:24 +00:00
assert( numRows == numColumns );
2012-11-26 18:58:24 +00:00
realEigenValues.SetSize( numRows );
imaginaryEigenValues.SetSize( numRows );
2012-11-26 18:58:24 +00:00
H = *this;
// reduce to Hessenberg form
HessenbergReduction( H );
// reduce Hessenberg to real Schur form
return HessenbergToRealSchur( H, realEigenValues, imaginaryEigenValues );
2012-11-26 18:58:24 +00:00
}
/*
============
idMatX::Eigen_SortIncreasing
============
*/
void idMatX::Eigen_SortIncreasing( idVecX& eigenValues )
{
for( int i = 0, j = 0; i <= numRows - 2; i++ )
{
2012-11-26 18:58:24 +00:00
j = i;
float min = eigenValues[j];
for( int k = i + 1; k < numRows; k++ )
{
if( eigenValues[k] < min )
{
2012-11-26 18:58:24 +00:00
j = k;
min = eigenValues[j];
}
}
if( j != i )
{
2012-11-26 18:58:24 +00:00
eigenValues.SwapElements( i, j );
SwapColumns( i, j );
}
}
}
/*
============
idMatX::Eigen_SortDecreasing
============
*/
void idMatX::Eigen_SortDecreasing( idVecX& eigenValues )
{
for( int i = 0, j = 0; i <= numRows - 2; i++ )
{
2012-11-26 18:58:24 +00:00
j = i;
float max = eigenValues[j];
for( int k = i + 1; k < numRows; k++ )
{
if( eigenValues[k] > max )
{
2012-11-26 18:58:24 +00:00
j = k;
max = eigenValues[j];
}
}
if( j != i )
{
2012-11-26 18:58:24 +00:00
eigenValues.SwapElements( i, j );
SwapColumns( i, j );
}
}
}
/*
============
idMatX::DeterminantGeneric
============
*/
float idMatX::DeterminantGeneric() const
{
int* index;
2012-11-26 18:58:24 +00:00
float det;
idMatX tmp;
index = ( int* ) _alloca16( numRows * sizeof( int ) );
2012-11-26 18:58:24 +00:00
tmp.SetData( numRows, numColumns, MATX_ALLOCA( numRows * numColumns ) );
tmp = *this;
if( !tmp.LU_Factor( index, &det ) )
{
2012-11-26 18:58:24 +00:00
return 0.0f;
}
2012-11-26 18:58:24 +00:00
return det;
}
/*
============
idMatX::InverseSelfGeneric
============
*/
bool idMatX::InverseSelfGeneric()
{
2012-11-26 18:58:24 +00:00
int i, j, *index;
idMatX tmp;
idVecX x, b;
index = ( int* ) _alloca16( numRows * sizeof( int ) );
2012-11-26 18:58:24 +00:00
tmp.SetData( numRows, numColumns, MATX_ALLOCA( numRows * numColumns ) );
tmp = *this;
if( !tmp.LU_Factor( index ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
x.SetData( numRows, VECX_ALLOCA( numRows ) );
b.SetData( numRows, VECX_ALLOCA( numRows ) );
b.Zero();
for( i = 0; i < numRows; i++ )
{
2012-11-26 18:58:24 +00:00
b[i] = 1.0f;
tmp.LU_Solve( x, b, index );
for( j = 0; j < numRows; j++ )
{
( *this )[j][i] = x[j];
2012-11-26 18:58:24 +00:00
}
b[i] = 0.0f;
}
return true;
}
/*
============
idMatX::Test
============
*/
void idMatX::Test()
{
2012-11-26 18:58:24 +00:00
idMatX original, m1, m2, m3, q1, q2, r1, r2;
idVecX v, w, u, c, d;
int offset, size, *index1, *index2;
2012-11-26 18:58:24 +00:00
size = 6;
original.Random( size, size, 0 );
original = original * original.Transpose();
index1 = ( int* ) _alloca16( ( size + 1 ) * sizeof( index1[0] ) );
index2 = ( int* ) _alloca16( ( size + 1 ) * sizeof( index2[0] ) );
2012-11-26 18:58:24 +00:00
/*
idMatX::LowerTriangularInverse
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m1.ClearUpperTriangle();
m2 = m1;
2012-11-26 18:58:24 +00:00
m2.InverseSelf();
m1.LowerTriangularInverse();
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LowerTriangularInverse failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::UpperTriangularInverse
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m1.ClearLowerTriangle();
m2 = m1;
2012-11-26 18:58:24 +00:00
m2.InverseSelf();
m1.UpperTriangularInverse();
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::UpperTriangularInverse failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Inverse_GaussJordan
*/
2012-11-26 18:58:24 +00:00
m1 = original;
2012-11-26 18:58:24 +00:00
m1.Inverse_GaussJordan();
m1 *= original;
if( !m1.IsIdentity( 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Inverse_GaussJordan failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Inverse_UpdateRankOne
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
w.Random( size, 1 );
v.Random( size, 2 );
2012-11-26 18:58:24 +00:00
// invert m1
m1.Inverse_GaussJordan();
// modify and invert m2
2012-11-26 18:58:24 +00:00
m2.Update_RankOne( v, w, 1.0f );
if( !m2.Inverse_GaussJordan() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update inverse of m1
m1.Inverse_UpdateRankOne( v, w, 1.0f );
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Inverse_UpdateRankOne failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Inverse_UpdateRowColumn
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.Random( size, 1 );
w.Random( size, 2 );
w[offset] = 0.0f;
2012-11-26 18:58:24 +00:00
// invert m1
m1.Inverse_GaussJordan();
2012-11-26 18:58:24 +00:00
// modify and invert m2
m2.Update_RowColumn( v, w, offset );
if( !m2.Inverse_GaussJordan() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update inverse of m1
m1.Inverse_UpdateRowColumn( v, w, offset );
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Inverse_UpdateRowColumn failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Inverse_UpdateIncrement
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.Random( size + 1, 1 );
w.Random( size + 1, 2 );
w[size] = 0.0f;
2012-11-26 18:58:24 +00:00
// invert m1
m1.Inverse_GaussJordan();
// modify and invert m2
2012-11-26 18:58:24 +00:00
m2.Update_Increment( v, w );
if( !m2.Inverse_GaussJordan() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update inverse of m1
m1.Inverse_UpdateIncrement( v, w );
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Inverse_UpdateIncrement failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Inverse_UpdateDecrement
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.SetSize( 6 );
w.SetSize( 6 );
for( int i = 0; i < size; i++ )
{
2012-11-26 18:58:24 +00:00
v[i] = original[i][offset];
w[i] = original[offset][i];
}
2012-11-26 18:58:24 +00:00
// invert m1
m1.Inverse_GaussJordan();
2012-11-26 18:58:24 +00:00
// modify and invert m2
m2.Update_Decrement( offset );
if( !m2.Inverse_GaussJordan() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update inverse of m1
m1.Inverse_UpdateDecrement( v, w, offset );
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Inverse_UpdateDecrement failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LU_Factor
*/
2012-11-26 18:58:24 +00:00
m1 = original;
2012-11-26 18:58:24 +00:00
m1.LU_Factor( NULL ); // no pivoting
m1.LU_UnpackFactors( m2, m3 );
m1 = m2 * m3;
if( !original.Compare( m1, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LU_Factor failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LU_UpdateRankOne
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
w.Random( size, 1 );
v.Random( size, 2 );
2012-11-26 18:58:24 +00:00
// factor m1
m1.LU_Factor( index1 );
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_RankOne( v, w, 1.0f );
if( !m2.LU_Factor( index2 ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.LU_MultiplyFactors( m3, index2 );
m2 = m3;
2012-11-26 18:58:24 +00:00
// update factored m1
m1.LU_UpdateRankOne( v, w, 1.0f, index1 );
m1.LU_MultiplyFactors( m3, index1 );
m1 = m3;
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LU_UpdateRankOne failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LU_UpdateRowColumn
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.Random( size, 1 );
w.Random( size, 2 );
w[offset] = 0.0f;
2012-11-26 18:58:24 +00:00
// factor m1
m1.LU_Factor( index1 );
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_RowColumn( v, w, offset );
if( !m2.LU_Factor( index2 ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.LU_MultiplyFactors( m3, index2 );
m2 = m3;
2012-11-26 18:58:24 +00:00
// update m1
m1.LU_UpdateRowColumn( v, w, offset, index1 );
2012-11-26 18:58:24 +00:00
m1.LU_MultiplyFactors( m3, index1 );
m1 = m3;
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LU_UpdateRowColumn failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LU_UpdateIncrement
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.Random( size + 1, 1 );
w.Random( size + 1, 2 );
w[size] = 0.0f;
2012-11-26 18:58:24 +00:00
// factor m1
m1.LU_Factor( index1 );
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_Increment( v, w );
if( !m2.LU_Factor( index2 ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.LU_MultiplyFactors( m3, index2 );
m2 = m3;
2012-11-26 18:58:24 +00:00
// update factored m1
m1.LU_UpdateIncrement( v, w, index1 );
m1.LU_MultiplyFactors( m3, index1 );
m1 = m3;
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LU_UpdateIncrement failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LU_UpdateDecrement
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.SetSize( 6 );
w.SetSize( 6 );
for( int i = 0; i < size; i++ )
{
2012-11-26 18:58:24 +00:00
v[i] = original[i][offset];
w[i] = original[offset][i];
}
2012-11-26 18:58:24 +00:00
// factor m1
m1.LU_Factor( index1 );
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_Decrement( offset );
if( !m2.LU_Factor( index2 ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.LU_MultiplyFactors( m3, index2 );
m2 = m3;
2012-11-26 18:58:24 +00:00
u.SetSize( 6 );
for( int i = 0; i < size; i++ )
{
2012-11-26 18:58:24 +00:00
u[i] = original[index1[offset]][i];
}
2012-11-26 18:58:24 +00:00
// update factors of m1
m1.LU_UpdateDecrement( v, w, u, offset, index1 );
m1.LU_MultiplyFactors( m3, index1 );
m1 = m3;
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LU_UpdateDecrement failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LU_Inverse
*/
2012-11-26 18:58:24 +00:00
m2 = original;
2012-11-26 18:58:24 +00:00
m2.LU_Factor( NULL );
m2.LU_Inverse( m1, NULL );
m1 *= original;
if( !m1.IsIdentity( 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LU_Inverse failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::QR_Factor
*/
2012-11-26 18:58:24 +00:00
c.SetSize( size );
d.SetSize( size );
2012-11-26 18:58:24 +00:00
m1 = original;
2012-11-26 18:58:24 +00:00
m1.QR_Factor( c, d );
m1.QR_UnpackFactors( q1, r1, c, d );
m1 = q1 * r1;
if( !original.Compare( m1, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::QR_Factor failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::QR_UpdateRankOne
*/
2012-11-26 18:58:24 +00:00
c.SetSize( size );
d.SetSize( size );
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
w.Random( size, 0 );
v = w;
2012-11-26 18:58:24 +00:00
// factor m1
m1.QR_Factor( c, d );
m1.QR_UnpackFactors( q1, r1, c, d );
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_RankOne( v, w, 1.0f );
if( !m2.QR_Factor( c, d ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.QR_UnpackFactors( q2, r2, c, d );
m2 = q2 * r2;
2012-11-26 18:58:24 +00:00
// update factored m1
q1.QR_UpdateRankOne( r1, v, w, 1.0f );
m1 = q1 * r1;
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::QR_UpdateRankOne failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::QR_UpdateRowColumn
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
c.SetSize( size );
d.SetSize( size );
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.Random( size, 1 );
w.Random( size, 2 );
w[offset] = 0.0f;
2012-11-26 18:58:24 +00:00
// factor m1
m1.QR_Factor( c, d );
m1.QR_UnpackFactors( q1, r1, c, d );
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_RowColumn( v, w, offset );
if( !m2.QR_Factor( c, d ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.QR_UnpackFactors( q2, r2, c, d );
m2 = q2 * r2;
2012-11-26 18:58:24 +00:00
// update m1
q1.QR_UpdateRowColumn( r1, v, w, offset );
m1 = q1 * r1;
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::QR_UpdateRowColumn failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::QR_UpdateIncrement
*/
c.SetSize( size + 1 );
d.SetSize( size + 1 );
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.Random( size + 1, 1 );
w.Random( size + 1, 2 );
w[size] = 0.0f;
2012-11-26 18:58:24 +00:00
// factor m1
m1.QR_Factor( c, d );
m1.QR_UnpackFactors( q1, r1, c, d );
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_Increment( v, w );
if( !m2.QR_Factor( c, d ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.QR_UnpackFactors( q2, r2, c, d );
m2 = q2 * r2;
2012-11-26 18:58:24 +00:00
// update factored m1
q1.QR_UpdateIncrement( r1, v, w );
m1 = q1 * r1;
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::QR_UpdateIncrement failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::QR_UpdateDecrement
*/
for( offset = 0; offset < size; offset++ )
{
c.SetSize( size + 1 );
d.SetSize( size + 1 );
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.SetSize( 6 );
w.SetSize( 6 );
for( int i = 0; i < size; i++ )
{
2012-11-26 18:58:24 +00:00
v[i] = original[i][offset];
w[i] = original[offset][i];
}
2012-11-26 18:58:24 +00:00
// factor m1
m1.QR_Factor( c, d );
m1.QR_UnpackFactors( q1, r1, c, d );
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_Decrement( offset );
if( !m2.QR_Factor( c, d ) )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.QR_UnpackFactors( q2, r2, c, d );
m2 = q2 * r2;
2012-11-26 18:58:24 +00:00
// update factors of m1
q1.QR_UpdateDecrement( r1, v, w, offset );
m1 = q1 * r1;
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::QR_UpdateDecrement failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::QR_Inverse
*/
2012-11-26 18:58:24 +00:00
m2 = original;
2012-11-26 18:58:24 +00:00
m2.QR_Factor( c, d );
m2.QR_Inverse( m1, c, d );
m1 *= original;
if( !m1.IsIdentity( 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::QR_Inverse failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::SVD_Factor
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m3.Zero( size, size );
w.Zero( size );
2012-11-26 18:58:24 +00:00
m1.SVD_Factor( w, m3 );
m2.Diag( w );
m3.TransposeSelf();
m1 = m1 * m2 * m3;
if( !original.Compare( m1, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::SVD_Factor failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::SVD_Inverse
*/
2012-11-26 18:58:24 +00:00
m2 = original;
2012-11-26 18:58:24 +00:00
m2.SVD_Factor( w, m3 );
m2.SVD_Inverse( m1, w, m3 );
m1 *= original;
if( !m1.IsIdentity( 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::SVD_Inverse failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Cholesky_Factor
*/
2012-11-26 18:58:24 +00:00
m1 = original;
2012-11-26 18:58:24 +00:00
m1.Cholesky_Factor();
m1.Cholesky_MultiplyFactors( m2 );
if( !original.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Cholesky_Factor failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Cholesky_UpdateRankOne
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
w.Random( size, 0 );
2012-11-26 18:58:24 +00:00
// factor m1
m1.Cholesky_Factor();
m1.ClearUpperTriangle();
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_RankOneSymmetric( w, 1.0f );
if( !m2.Cholesky_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.ClearUpperTriangle();
2012-11-26 18:58:24 +00:00
// update factored m1
m1.Cholesky_UpdateRankOne( w, 1.0f, 0 );
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Cholesky_UpdateRankOne failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Cholesky_UpdateRowColumn
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
// factor m1
m1.Cholesky_Factor();
m1.ClearUpperTriangle();
2012-11-26 18:58:24 +00:00
int pdtable[] = { 1, 0, 1, 0, 0, 0 };
w.Random( size, pdtable[offset] );
w *= 0.1f;
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_RowColumnSymmetric( w, offset );
if( !m2.Cholesky_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.ClearUpperTriangle();
2012-11-26 18:58:24 +00:00
// update m1
m1.Cholesky_UpdateRowColumn( w, offset );
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Cholesky_UpdateRowColumn failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Cholesky_UpdateIncrement
*/
2012-11-26 18:58:24 +00:00
m1.Random( size + 1, size + 1, 0 );
m3 = m1 * m1.Transpose();
2012-11-26 18:58:24 +00:00
m1.SquareSubMatrix( m3, size );
m2 = m1;
2012-11-26 18:58:24 +00:00
w.SetSize( size + 1 );
for( int i = 0; i < size + 1; i++ )
{
2012-11-26 18:58:24 +00:00
w[i] = m3[size][i];
}
2012-11-26 18:58:24 +00:00
// factor m1
m1.Cholesky_Factor();
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_IncrementSymmetric( w );
if( !m2.Cholesky_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update factored m1
m1.Cholesky_UpdateIncrement( w );
2012-11-26 18:58:24 +00:00
m1.ClearUpperTriangle();
m2.ClearUpperTriangle();
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Cholesky_UpdateIncrement failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Cholesky_UpdateDecrement
*/
for( offset = 0; offset < size; offset += size - 1 )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.SetSize( 6 );
for( int i = 0; i < size; i++ )
{
2012-11-26 18:58:24 +00:00
v[i] = original[i][offset];
}
2012-11-26 18:58:24 +00:00
// factor m1
m1.Cholesky_Factor();
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_Decrement( offset );
if( !m2.Cholesky_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update factors of m1
m1.Cholesky_UpdateDecrement( v, offset );
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Cholesky_UpdateDecrement failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Cholesky_Inverse
*/
2012-11-26 18:58:24 +00:00
m2 = original;
2012-11-26 18:58:24 +00:00
m2.Cholesky_Factor();
m2.Cholesky_Inverse( m1 );
m1 *= original;
if( !m1.IsIdentity( 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Cholesky_Inverse failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LDLT_Factor
*/
2012-11-26 18:58:24 +00:00
m1 = original;
2012-11-26 18:58:24 +00:00
m1.LDLT_Factor();
m1.LDLT_MultiplyFactors( m2 );
if( !original.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LDLT_Factor failed" );
}
2012-11-26 18:58:24 +00:00
m1.LDLT_UnpackFactors( m2, m3 );
m2 = m2 * m3 * m2.Transpose();
if( !original.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LDLT_Factor failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LDLT_UpdateRankOne
*/
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
w.Random( size, 0 );
2012-11-26 18:58:24 +00:00
// factor m1
m1.LDLT_Factor();
m1.ClearUpperTriangle();
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_RankOneSymmetric( w, 1.0f );
if( !m2.LDLT_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.ClearUpperTriangle();
2012-11-26 18:58:24 +00:00
// update factored m1
m1.LDLT_UpdateRankOne( w, 1.0f, 0 );
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LDLT_UpdateRankOne failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LDLT_UpdateRowColumn
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
w.Random( size, 0 );
2012-11-26 18:58:24 +00:00
// factor m1
m1.LDLT_Factor();
m1.ClearUpperTriangle();
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_RowColumnSymmetric( w, offset );
if( !m2.LDLT_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
m2.ClearUpperTriangle();
2012-11-26 18:58:24 +00:00
// update m1
m1.LDLT_UpdateRowColumn( w, offset );
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LDLT_UpdateRowColumn failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LDLT_UpdateIncrement
*/
2012-11-26 18:58:24 +00:00
m1.Random( size + 1, size + 1, 0 );
m3 = m1 * m1.Transpose();
2012-11-26 18:58:24 +00:00
m1.SquareSubMatrix( m3, size );
m2 = m1;
2012-11-26 18:58:24 +00:00
w.SetSize( size + 1 );
for( int i = 0; i < size + 1; i++ )
{
2012-11-26 18:58:24 +00:00
w[i] = m3[size][i];
}
2012-11-26 18:58:24 +00:00
// factor m1
m1.LDLT_Factor();
// modify and factor m2
2012-11-26 18:58:24 +00:00
m2.Update_IncrementSymmetric( w );
if( !m2.LDLT_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update factored m1
m1.LDLT_UpdateIncrement( w );
2012-11-26 18:58:24 +00:00
m1.ClearUpperTriangle();
m2.ClearUpperTriangle();
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LDLT_UpdateIncrement failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LDLT_UpdateDecrement
*/
for( offset = 0; offset < size; offset++ )
{
2012-11-26 18:58:24 +00:00
m1 = original;
m2 = original;
2012-11-26 18:58:24 +00:00
v.SetSize( 6 );
for( int i = 0; i < size; i++ )
{
2012-11-26 18:58:24 +00:00
v[i] = original[i][offset];
}
2012-11-26 18:58:24 +00:00
// factor m1
m1.LDLT_Factor();
2012-11-26 18:58:24 +00:00
// modify and factor m2
m2.Update_Decrement( offset );
if( !m2.LDLT_Factor() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
}
2012-11-26 18:58:24 +00:00
// update factors of m1
m1.LDLT_UpdateDecrement( v, offset );
if( !m1.Compare( m2, 1e-3f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LDLT_UpdateDecrement failed" );
}
}
2012-11-26 18:58:24 +00:00
/*
idMatX::LDLT_Inverse
*/
2012-11-26 18:58:24 +00:00
m2 = original;
2012-11-26 18:58:24 +00:00
m2.LDLT_Factor();
m2.LDLT_Inverse( m1 );
m1 *= original;
if( !m1.IsIdentity( 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::LDLT_Inverse failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Eigen_SolveSymmetricTriDiagonal
*/
2012-11-26 18:58:24 +00:00
m3 = original;
m3.TriDiagonal_ClearTriangles();
m1 = m3;
2012-11-26 18:58:24 +00:00
v.SetSize( size );
2012-11-26 18:58:24 +00:00
m1.Eigen_SolveSymmetricTriDiagonal( v );
2012-11-26 18:58:24 +00:00
m3.TransposeMultiply( m2, m1 );
for( int i = 0; i < size; i++ )
{
for( int j = 0; j < size; j++ )
{
2012-11-26 18:58:24 +00:00
m1[i][j] *= v[j];
}
}
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Eigen_SolveSymmetricTriDiagonal failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Eigen_SolveSymmetric
*/
2012-11-26 18:58:24 +00:00
m3 = original;
m1 = m3;
2012-11-26 18:58:24 +00:00
v.SetSize( size );
2012-11-26 18:58:24 +00:00
m1.Eigen_SolveSymmetric( v );
2012-11-26 18:58:24 +00:00
m3.TransposeMultiply( m2, m1 );
for( int i = 0; i < size; i++ )
{
for( int j = 0; j < size; j++ )
{
2012-11-26 18:58:24 +00:00
m1[i][j] *= v[j];
}
}
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Eigen_SolveSymmetric failed" );
}
2012-11-26 18:58:24 +00:00
/*
idMatX::Eigen_Solve
*/
2012-11-26 18:58:24 +00:00
m3 = original;
m1 = m3;
2012-11-26 18:58:24 +00:00
v.SetSize( size );
w.SetSize( size );
2012-11-26 18:58:24 +00:00
m1.Eigen_Solve( v, w );
2012-11-26 18:58:24 +00:00
m3.TransposeMultiply( m2, m1 );
for( int i = 0; i < size; i++ )
{
for( int j = 0; j < size; j++ )
{
2012-11-26 18:58:24 +00:00
m1[i][j] *= v[j];
}
}
if( !m1.Compare( m2, 1e-4f ) )
{
2012-11-26 18:58:24 +00:00
idLib::common->Warning( "idMatX::Eigen_Solve failed" );
}
}