how to calculate the QR decomposition of a matrix
来源:互联网 发布:最长公共子序列c语言 编辑:程序博客网 时间:2024/04/28 15:33
In many papers(such as [1], [2]), we need to solve the polar decomposition of a matrix which decompose a matrix A into a rotation matrix R and a symmetric matrix S, s.t. A = RS
Here, we will be using Eigen library to solve this problem contained in paper [2].
In Eigen library, there is no method can solve polar decomposition directly. Thus, we must find another ways to do this. Fortunately, up to now, I have already found two ways to deal with this problem. So let's cut into our theme:
First, I would like to introduce something about how to obtain matrix R, S from the polar decomposition, please look at the following picture:
Then CGAL source code is presented:
bool polar_eigen(const Matrix& A, Matrix& R) { if(A.determinant() < 0) { return false; } typedef Matrix::Scalar Scalar; const Scalar th = std::sqrt(Eigen::NumTraits<Scalar>::dummy_precision()); Eigen::SelfAdjointEigenSolver<Matrix> eig; CGAL::feclearexcept(FE_UNDERFLOW); eig.computeDirect(A.transpose()*A);//solve eigen values and eigen vectors if(CGAL::fetestexcept(FE_UNDERFLOW) || eig.eigenvalues()(0)/eig.eigenvalues()(2)<th) // to make sure the matrix is semi-positive { return false; } Vector S = eig.eigenvalues().cwiseSqrt(); R = A * eig.eigenvectors() * S.asDiagonal().inverse() * eig.eigenvectors().transpose(); if(std::abs(R.squaredNorm()-3.) > th || R.determinant() < 0)// because R is an orthonormal matrix the squared norms of which column vectors should be 1 { return false; } R.transposeInPlace(); // the optimal rotation matrix should be transpose of decomposition result, only in paper[2], generally we don't need tranpose it return true; }
feclearexcept and fetestexcept are used to detect some excetion which are defined in the source code below:
// Copyright (c) 2010-2011 GeometryFactory Sarl (France)//// This file is part of CGAL (www.cgal.org); you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public License as// published by the Free Software Foundation; either version 3 of the License,// or (at your option) any later version.//// Licensees holding a valid commercial license may use this file in// accordance with the commercial license agreement provided with the software.//// $URL$// $Id$////// Author(s) : Laurent Rineau#include <cfloat>#ifndef FE_INVALID# define FE_INEXACT _EM_INEXACT# define FE_UNDERFLOW _EM_UNDERFLOW# define FE_OVERFLOW _EM_OVERFLOW# define FE_DIVBYZERO _EM_ZERODIVIDE# define FE_INVALID _EM_INVALID#endifnamespace CGAL {// replacement for C99 functionsinline intfeclearexcept(int exceptions) { _clearfp(); return 0;}inline intfetestexcept(int exceptions){#if defined(_M_IX86) && _M_IX86_FP > 0 // On x86/x64 processors, when SSE is used. unsigned int i1; unsigned int i2; _statusfp2(&i1, &i2); return (i1 & exceptions) | (i2 & exceptions);#else // On x86 processors without SSE, or on other processors supported by MSVC return _statusfp() & exceptions;#endif}} // end namespace CGAL
the second way is to use single value decomposition which written in CGAL like below:
/// Computes the closest rotation to `m` and places it into `R` void compute_close_rotation(const Matrix& m, Matrix& R) { Eigen::JacobiSVD<Eigen::Matrix3d> solver; solver.compute( m, Eigen::ComputeFullU | Eigen::ComputeFullV ); const Matrix& u = solver.matrixU(); const Matrix& v = solver.matrixV(); R = v * u.transpose(); if( R.determinant() < 0 ) { Matrix u_copy = u; u_copy.col(2) *= -1; // singular values sorted ascendingly R = v * u_copy.transpose(); // re-extract rotation matrix } }
Here R is denoted as the way in the paper [2] needs. Normally R should be expressed as R = U * V.transpose();
[1] Meshless Deformations Based on Shape Matching
[2] As-Rigid-As-Possible surface Modeling
- how to calculate the QR decomposition of a matrix
- How to Calculate the Number of IOPS and Throughput of a Database (文档 ID 2206831.1)
- How to calculate the MD5 hash of a large file in C?
- How to calculate the SVG Path for an arc (of a circle)
- how to calculate the textsize of TLatex in CernRoot
- Matrix QR Decomposition using OpenCV
- How to calculate the number of parameters of Convolutional Neural Networks(CNNs) correctly?
- how to calculate receptive field of CNN
- Oracle table fragmentation how to calculate or get the actual used blocks of the table
- How to calculate the undo_retention time
- How to calculate the inversion of 0/1 array? 求逆序数
- calculate the square of a number
- QR Decomposition
- a matrix library of c++ to replace the matlab
- how to calculate ANOVA
- How To Change the Background Color of a Tab Control
- How to get the field descriptions of a table?
- How to print the content of a Rich Edit Control
- Python元编程-遗忘的远古凶兽
- quick-cocos2dx scheduler.scheduleGlobal坑
- POJ 3074 Sudoku(数独|Dancing Links精确覆盖)
- 深入安卓Package Manager和Package Installer
- Android PackageManagerService分析二:安装APK
- how to calculate the QR decomposition of a matrix
- POJ-3225 Help with Intervals-线段树/成段更新+倍增区间法
- 102. Binary Tree Level Order Traversal
- Codeforces 624A Save Luke
- 五分钟搞懂后缀数组!后缀数组解析以及应用(附详解代码)
- AIM Tech Round (Div. 2) C. Graph and String(二分图染色 | 贪心)
- 编程新手我觉得需要掌握的五个重要东西
- hdoj 1385 Minimum Transport Cost(floyd 记录最短路径)
- Ubuntu 学习笔记之——下载神器aria2