File: lin_algebra.h

package info (click to toggle)
meshlab 1.3.0a%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 23,416 kB
  • sloc: cpp: 214,034; ansic: 4,493; makefile: 72
file content (658 lines) | stat: -rw-r--r-- 18,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2006                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program 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 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program 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 (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
/****************************************************************************
History

$Log: not supported by cvs2svn $
Revision 1.16  2007/01/29 00:18:20  pietroni
-added some explicit CASTs in order to avoid warning if one use float instead of double as ScalarType

Revision 1.15  2006/09/29 08:36:10  cignoni
Added missing typedef for gcc compiing

Revision 1.14  2006/09/28 22:49:49  fiorin
Removed some warnings

Revision 1.13  2006/07/28 12:39:05  zifnab1974
added some typename directives

Revision 1.12  2006/07/24 07:26:47  fiorin
Changed the template argument in JacobiRotate and added method for sorting eigenvalues and eigenvectors (SortEigenvaluesAndEigenvectors)

Revision 1.11  2006/05/25 09:35:55  cignoni
added missing internal prototype to Sort function

Revision 1.10  2006/05/17 09:26:35  cignoni
Added initial disclaimer

****************************************************************************/
#ifndef __VCGLIB_LINALGEBRA_H
#define __VCGLIB_LINALGEBRA_H

#include <vcg/math/base.h>
#include <vcg/math/matrix44.h>
#include <algorithm>

namespace vcg
{
	/** \addtogroup math */
	/* @{ */

	/*!
	*
	*/
	template< typename MATRIX_TYPE >
	static void JacobiRotate(MATRIX_TYPE &A, typename MATRIX_TYPE::ScalarType s, typename MATRIX_TYPE::ScalarType tau, int i,int j,int k,int l)
	{
		typename MATRIX_TYPE::ScalarType g=A[i][j];
		typename MATRIX_TYPE::ScalarType h=A[k][l];
		A[i][j]=g-s*(h+g*tau);
		A[k][l]=h+s*(g-h*tau);
	};

	/*!
	*	Computes all eigenvalues and eigenvectors of a real symmetric matrix .
	*	On output, elements of the input matrix above the diagonal are destroyed.
	* \param d  returns the eigenvalues of a.
	* \param v  is a matrix whose columns contain, the normalized eigenvectors
	* \param nrot returns the number of Jacobi rotations that were required.
	*/
	template <typename MATRIX_TYPE, typename POINT_TYPE>
	static void Jacobi(MATRIX_TYPE &w, POINT_TYPE &d, MATRIX_TYPE &v, int &nrot)
	{
       typedef typename MATRIX_TYPE::ScalarType ScalarType;
		assert(w.RowsNumber()==w.ColumnsNumber());
		int dimension = w.RowsNumber();

		int j,iq,ip,i;
		//assert(w.IsSymmetric());
		typename MATRIX_TYPE::ScalarType tresh, theta, tau, t, sm, s, h, g, c;
		POINT_TYPE b, z;

		v.SetIdentity();

		for (ip=0;ip<dimension;++ip)			//Initialize b and d to the diagonal of a.
		{
			b[ip]=d[ip]=w[ip][ip];
			z[ip]=ScalarType(0.0);							//This vector will accumulate terms of the form tapq as in equation (11.1.14).
		}
		nrot=0;
		for (i=0;i<50;i++)
		{
			sm=ScalarType(0.0);
			for (ip=0;ip<dimension-1;++ip)		// Sum off diagonal elements
			{
				for (iq=ip+1;iq<dimension;++iq)
					sm += math::Abs(w[ip][iq]);
			}
			if (sm == ScalarType(0.0))					//The normal return, which relies on quadratic convergence to machine underflow.
			{
				return;
			}
			if (i < 4)
				tresh=ScalarType(0.2)*sm/(dimension*dimension); //...on the first three sweeps.
			else
				tresh=ScalarType(0.0);				//...thereafter.
			for (ip=0;ip<dimension-1;++ip)
			{
				for (iq=ip+1;iq<dimension;iq++)
				{
					g=ScalarType(100.0)*fabs(w[ip][iq]);
					//After four sweeps, skip the rotation if the off-diagonal element is small.
					if(i>4 && (float)(fabs(d[ip])+g) == (float)fabs(d[ip]) && (float)(fabs(d[iq])+g) == (float)fabs(d[iq]))
						w[ip][iq]=ScalarType(0.0);
					else if (fabs(w[ip][iq]) > tresh)
					{
						h=d[iq]-d[ip];
						if ((float)(fabs(h)+g) == (float)fabs(h))
							t=(w[ip][iq])/h; //t =1/(2#)
						else
						{
							theta=ScalarType(0.5)*h/(w[ip][iq]); //Equation (11.1.10).
							t=ScalarType(1.0)/(fabs(theta)+sqrt(ScalarType(1.0)+theta*theta));
							if (theta < ScalarType(0.0)) t = -t;
						}
						c=ScalarType(1.0)/sqrt(ScalarType(1.0)+t*t);
						s=t*c;
						tau=s/(ScalarType(1.0)+c);
						h=t*w[ip][iq];
						z[ip] -= h;
						z[iq] += h;
						d[ip] -= h;
						d[iq] += h;
						w[ip][iq]=ScalarType(0.0);
						for (j=0;j<=ip-1;j++) { //Case of rotations 1 <= j < p.
							JacobiRotate<MATRIX_TYPE>(w,s,tau,j,ip,j,iq) ;
						}
						for (j=ip+1;j<=iq-1;j++) { //Case of rotations p < j < q.
							JacobiRotate<MATRIX_TYPE>(w,s,tau,ip,j,j,iq);
						}
						for (j=iq+1;j<dimension;j++) { //Case of rotations q< j <= n.
							JacobiRotate<MATRIX_TYPE>(w,s,tau,ip,j,iq,j);
						}
						for (j=0;j<dimension;j++) {
							JacobiRotate<MATRIX_TYPE>(v,s,tau,j,ip,j,iq);
						}
						++nrot;
					}
				}
			}
			for (ip=0;ip<dimension;ip++)
			{
				b[ip] += z[ip];
				d[ip]=b[ip]; //Update d with the sum of ta_pq ,
				z[ip]=0.0; //and reinitialize z.
			}
		}
	};


	/*!
	* Given the eigenvectors and the eigenvalues as output from JacobiRotate, sorts the eigenvalues
	* into descending order, and rearranges the columns of v correspondinlgy.
	* \param eigenvalues
	* \param eigenvector (in columns)
	* \param absComparison sort according to the absolute values of the eigenvalues.
	*/
	template < typename MATRIX_TYPE, typename POINT_TYPE >
	void SortEigenvaluesAndEigenvectors(POINT_TYPE &eigenvalues, MATRIX_TYPE &eigenvectors, bool absComparison = false)
	{
		assert(eigenvectors.ColumnsNumber()==eigenvectors.RowsNumber());
		int dimension = eigenvectors.ColumnsNumber();
		int i, j, k;
		float p,q;
		for (i=0; i<dimension-1; i++)
		{
			if (absComparison)
			{
				p = fabs(eigenvalues[k=i]);
				for (j=i+1; j<dimension; j++)
					if ((q=fabs(eigenvalues[j])) >= p)
					{
						p = q;
						k = j;
					}
				p = eigenvalues[k];
			}
			else
			{
				p = eigenvalues[ k=i ];
				for (j=i+1; j<dimension; j++)
					if (eigenvalues[j] >= p)
						p = eigenvalues[ k=j ];
			}

			if (k != i)
			{
				eigenvalues[k] = eigenvalues[i];  // i.e.
				eigenvalues[i] = p;								// swaps the value of the elements i-th and k-th

				for (j=0; j<dimension; j++)
				{
					p = eigenvectors[j][i];										// i.e.
					eigenvectors[j][i] = eigenvectors[j][k];	// swaps the eigenvectors stored in the
					eigenvectors[j][k] = p;										// i-th and the k-th column
				}
			}
		}
	};


	template <typename TYPE>
	inline static TYPE sqr(TYPE a)
	{
		TYPE sqr_arg = a;
		return (sqr_arg == 0 ? 0 : sqr_arg*sqr_arg);
	}

	// Computes (a^2 + b^2)^(1/2) without destructive underflow or overflow.
	template <typename TYPE>
	inline static TYPE pythagora(TYPE a, TYPE b)
	{
		TYPE abs_a = fabs(a);
		TYPE abs_b = fabs(b);
		if (abs_a > abs_b)
			return abs_a*sqrt((TYPE)1.0+sqr(abs_b/abs_a));
		else
			return (abs_b == (TYPE)0.0 ? (TYPE)0.0 : abs_b*sqrt((TYPE)1.0+sqr(abs_a/abs_b)));
	};

	template <typename TYPE>
	inline static TYPE sign(TYPE a, TYPE b)
	{
		return (b >= 0.0 ? fabs(a) : -fabs(a));
	};

	/*!
	*
	*/
	enum SortingStrategy {LeaveUnsorted=0, SortAscending=1, SortDescending=2};
	template< typename MATRIX_TYPE >
	void Sort(MATRIX_TYPE &U, typename MATRIX_TYPE::ScalarType W[], MATRIX_TYPE &V, const SortingStrategy sorting) ;


	/*!
	*	Given a matrix <I>A<SUB>mxn</SUB></I>, this routine computes its singular value decomposition,
	*	i.e. <I>A=UxWxV<SUP>T</SUP></I>. The matrix <I>A</I> will be destroyed!
	*	(This is the implementation described in <I>Numerical Recipies</I>).
	*	\param A	the matrix to be decomposed
	*	\param W	the diagonal matrix of singular values <I>W</I>, stored as a vector <I>W[1...N]</I>
	*	\param V	the matrix <I>V</I> (not the transpose <I>V<SUP>T</SUP></I>)
	*	\param max_iters	max iteration number (default = 30).
	*	\return
	*/
	template <typename MATRIX_TYPE>
		static bool SingularValueDecomposition(MATRIX_TYPE &A, typename MATRIX_TYPE::ScalarType *W, MATRIX_TYPE &V, const SortingStrategy sorting=LeaveUnsorted, const int max_iters=30)
	{
		typedef typename MATRIX_TYPE::ScalarType ScalarType;
		int m = (int) A.RowsNumber();
		int n = (int) A.ColumnsNumber();
		int flag,i,its,j,jj,k,l,nm;
		ScalarType anorm, c, f, g, h, s, scale, x, y, z, *rv1;
		bool convergence = true;

		rv1 = new ScalarType[n];
		g = scale = anorm = 0;
		// Householder reduction to bidiagonal form.
		for (i=0; i<n; i++)
		{
			l = i+1;
			rv1[i] = scale*g;
			g = s = scale = 0.0;
			if (i < m)
			{
				for (k = i; k<m; k++)
					scale += fabs(A[k][i]);
				if (scale)
				{
					for (k=i; k<m; k++)
					{
						A[k][i] /= scale;
						s += A[k][i]*A[k][i];
					}
					f=A[i][i];
					g = -sign<ScalarType>( sqrt(s), f );
					h = f*g - s;
					A[i][i]=f-g;
					for (j=l; j<n; j++)
					{
						for (s=0.0, k=i; k<m; k++)
							s += A[k][i]*A[k][j];
						f = s/h;
						for (k=i; k<m; k++)
							A[k][j] += f*A[k][i];
					}
					for (k=i; k<m; k++)
						A[k][i] *= scale;
				}
			}
			W[i] = scale *g;
			g = s = scale = 0.0;
			if (i < m && i != (n-1))
			{
				for (k=l; k<n; k++)
					scale += fabs(A[i][k]);
				if (scale)
				{
					for (k=l; k<n; k++)
					{
						A[i][k] /= scale;
						s += A[i][k]*A[i][k];
					}
					f = A[i][l];
					g = -sign<ScalarType>(sqrt(s),f);
					h = f*g - s;
					A[i][l] = f-g;
					for (k=l; k<n; k++)
						rv1[k] = A[i][k]/h;
					for (j=l; j<m; j++)
					{
						for (s=0.0, k=l; k<n; k++)
							s += A[j][k]*A[i][k];
						for (k=l; k<n; k++)
							A[j][k] += s*rv1[k];
					}
					for (k=l; k<n; k++)
						A[i][k] *= scale;
				}
			}
      anorm=std::max( anorm, (math::Abs(W[i])+math::Abs(rv1[i])) );
		}
		// Accumulation of right-hand transformations.
		for (i=(n-1); i>=0; i--)
		{
			//Accumulation of right-hand transformations.
			if (i < (n-1))
			{
				if (g)
				{
					for (j=l; j<n;j++) //Double division to avoid possible underflow.
						V[j][i]=(A[i][j]/A[i][l])/g;
					for (j=l; j<n; j++)
					{
						for (s=0.0, k=l; k<n; k++)
							s += A[i][k] * V[k][j];
						for (k=l; k<n; k++)
							V[k][j] += s*V[k][i];
					}
				}
				for (j=l; j<n; j++)
					V[i][j] = V[j][i] = 0.0;
			}
			V[i][i] = 1.0;
			g = rv1[i];
			l = i;
		}
		// Accumulation of left-hand transformations.
    for (i=std::min(m,n)-1; i>=0; i--)
		{
			l = i+1;
			g = W[i];
			for (j=l; j<n; j++)
				A[i][j]=0.0;
			if (g)
			{
				g = (ScalarType)1.0/g;
				for (j=l; j<n; j++)
				{
					for (s=0.0, k=l; k<m; k++)
						s += A[k][i]*A[k][j];
					f = (s/A[i][i])*g;
					for (k=i; k<m; k++)
						A[k][j] += f*A[k][i];
				}
				for (j=i; j<m; j++)
					A[j][i] *= g;
			}
			else
				for (j=i; j<m; j++)
					A[j][i] = 0.0;
			++A[i][i];
		}
		// Diagonalization of the bidiagonal form: Loop over
		// singular values, and over allowed iterations.
		for (k=(n-1); k>=0; k--)
		{
			for (its=1; its<=max_iters; its++)
			{
				flag=1;
				for (l=k; l>=0; l--)
				{
					// Test for splitting.
					nm=l-1;
					// Note that rv1[1] is always zero.
					if ((double)(fabs(rv1[l])+anorm) == anorm)
					{
						flag=0;
						break;
					}
					if ((double)(fabs(W[nm])+anorm) == anorm)
						break;
				}
				if (flag)
				{
					c=0.0;  //Cancellation of rv1[l], if l > 1.
					s=1.0;
					for (i=l ;i<=k; i++)
					{
						f = s*rv1[i];
						rv1[i] = c*rv1[i];
						if ((double)(fabs(f)+anorm) == anorm)
							break;
						g = W[i];
						h = pythagora<ScalarType>(f,g);
						W[i] = h;
						h = (ScalarType)1.0/h;
						c = g*h;
						s = -f*h;
						for (j=0; j<m; j++)
						{
							y = A[j][nm];
							z = A[j][i];
							A[j][nm]	= y*c + z*s;
							A[j][i]		= z*c - y*s;
						}
					}
				}
				z = W[k];
				if (l == k)  //Convergence.
				{
					if (z < 0.0) { // Singular value is made nonnegative.
						W[k] = -z;
						for (j=0; j<n; j++)
							V[j][k] = -V[j][k];
					}
					break;
				}
				if (its == max_iters)
				{
					convergence = false;
				}
				x = W[l]; // Shift from bottom 2-by-2 minor.
				nm = k-1;
				y = W[nm];
				g = rv1[nm];
				h = rv1[k];
				f = ((y-z)*(y+z) + (g-h)*(g+h))/((ScalarType)2.0*h*y);
				g = pythagora<ScalarType>(f,1.0);
				f=((x-z)*(x+z) + h*((y/(f+sign(g,f)))-h))/x;
				c=s=1.0;
				//Next QR transformation:
				for (j=l; j<= nm;j++)
				{
					i = j+1;
					g = rv1[i];
					y = W[i];
					h = s*g;
					g = c*g;
					z = pythagora<ScalarType>(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 *= c;
					for (jj=0; jj<n; jj++)
					{
						x = V[jj][j];
						z = V[jj][i];
						V[jj][j] = x*c + z*s;
						V[jj][i] = z*c - x*s;
					}
					z = pythagora<ScalarType>(f,h);
					W[j] = z;
					// Rotation can be arbitrary if z = 0.
					if (z)
					{
						z = (ScalarType)1.0/z;
						c = f*z;
						s = h*z;
					}
					f = c*g + s*y;
					x = c*y - s*g;
					for (jj=0; jj<m; jj++)
					{
						y = A[jj][j];
						z = A[jj][i];
						A[jj][j] = y*c + z*s;
						A[jj][i] = z*c - y*s;
					}
				}
				rv1[l] = 0.0;
				rv1[k] = f;
				W[k]	 = x;
			}
		}
		delete []rv1;

		if (sorting!=LeaveUnsorted)
			Sort<MATRIX_TYPE>(A, W, V, sorting);

		return convergence;
	};


	/*!
	*	Sort the singular values computed by the <CODE>SingularValueDecomposition</CODE> procedure and
	* modify the matrices <I>U</I> and <I>V</I> accordingly.
	*/
	// TODO modify the last parameter type
	template< typename MATRIX_TYPE >
	void Sort(MATRIX_TYPE &U, typename MATRIX_TYPE::ScalarType W[], MATRIX_TYPE &V, const SortingStrategy sorting)
	{
		typedef typename MATRIX_TYPE::ScalarType ScalarType;

		assert(U.ColumnsNumber()==V.ColumnsNumber());

		int mu = U.RowsNumber();
		int mv = V.RowsNumber();
		int n  = U.ColumnsNumber();

		//ScalarType* u = &U[0][0];
		//ScalarType* v = &V[0][0];

		for (int i=0; i<n; i++)
		{
			int  k = i;
			ScalarType p = W[i];
			switch (sorting)
			{
			case SortAscending:
				{
					for (int j=i+1; j<n; j++)
					{
						if (W[j] < p)
						{
							k = j;
							p = W[j];
						}
					}
					break;
				}
			case SortDescending:
				{
					for (int j=i+1; j<n; j++)
					{
						if (W[j] > p)
						{
							k = j;
							p = W[j];
						}
					}
					break;
				}
      case LeaveUnsorted: break; // nothing to do.
      }
			if (k != i)
			{
				W[k] = W[i];  // i.e.
				W[i] = p;			// swaps the i-th and the k-th elements

				int j = mu;
				//ScalarType* uji = u + i; // uji = &U[0][i]
				//ScalarType* ujk = u + k; // ujk = &U[0][k]
				//ScalarType* vji = v + i; // vji = &V[0][i]
				//ScalarType* vjk = v + k; // vjk = &V[0][k]
				//if (j)
				//{
				//	for(;;)									for( ; j!=0; --j, uji+=n, ujk+=n)
				//	{												{
				//		p = *uji;								p = *uji;			// i.e.
				//		*uji = *ujk;						*uji = *ujk;	// swap( U[s][i], U[s][k] )
				//		*ujk = p;								*ujk = p;			//
				//		if (!(--j))						}
				//			break;
				//		uji += n;
				//		ujk += n;
				//	}
				//}
				for(int s=0; j!=0; ++s, --j)
					std::swap(U[s][i], U[s][k]);

				j = mv;
				//if (j!=0)
				//{
				//	for(;;)									for ( ; j!=0; --j, vji+=n, ujk+=n)
				//	{												{
				//		p    = *vji;						p    = *vji;	// i.e.
				//		*vji = *vjk;						*vji = *vjk;	// swap( V[s][i], V[s][k] )
				//		*vjk = p;								*vjk = p;			//
				//		if (!(--j))						}
				//			break;
				//		vji += n;
				//		vjk += n;
				//	}
				//}
				for (int s=0; j!=0; ++s, --j)
					std::swap(V[s][i], V[s][k]);
			}
		}
	}


	/*!
	*	Solves AxX = B for a vector X, where A is specified by the matrices <I>U<SUB>mxn</SUB></I>,
	*	<I>W<SUB>nx1</SUB></I> and <I>V<SUB>nxn</SUB></I> as returned by <CODE>SingularValueDecomposition</CODE>.
	*	No input quantities are destroyed, so the routine may be called sequentially with different bxs.
	*	\param x	is the output solution vector (<I>x<SUB>nx1</SUB></I>)
	*	\param b	is the input right-hand side (<I>b<SUB>nx1</SUB></I>)
	*/
	template <typename MATRIX_TYPE>
		static void SingularValueBacksubstitution(const MATRIX_TYPE												&U,
																							const typename MATRIX_TYPE::ScalarType	*W,
																							const MATRIX_TYPE												&V,
																										typename MATRIX_TYPE::ScalarType	*x,
																							const typename MATRIX_TYPE::ScalarType	*b)
	{
		typedef typename MATRIX_TYPE::ScalarType ScalarType;
		unsigned int jj, j, i;
		unsigned int columns_number = U.ColumnsNumber();
		unsigned int rows_number    = U.RowsNumber();
		ScalarType s;
		ScalarType *tmp	=	new ScalarType[columns_number];
		for (j=0; j<columns_number; j++) //Calculate U^T * B.
		{
			s = 0;
			if (W[j]!=0)							//Nonzero result only if wj is nonzero.
			{
				for (i=0; i<rows_number; i++)
					s += U[i][j]*b[i];
				s /= W[j];							//This is the divide by wj .
			}
			tmp[j]=s;
		}
		for (j=0;j<columns_number;j++)	//Matrix multiply by V to get answer.
		{
			s = 0;
			for (jj=0; jj<columns_number; jj++)
				s += V[j][jj]*tmp[jj];
			x[j]=s;
		}
		delete []tmp;
	};

	/*! @} */
}; // end of namespace

#endif