【问题标题】:Error in using Boost Ublas lu_Factorize使用 Boost Ublas lu_Factorize 时出错
【发布时间】:2024-04-25 11:40:01
【问题描述】:

我正在尝试制作一个有趣的项目,它可以制作矩阵幂函数 使用 BOOST Ublas。它与Numpy library power function for matrix 非常相似,它使用矩阵求幂来计算矩阵在对数时间内的 n 次方。 计算矩阵的 n 次方有 3 种情况:

  1. 如果幂 > 0,直接使用矩阵求幂
  2. 如果power = 0,检查矩阵是否有逆矩阵(检查Using lu_factorize),如果有,返回恒等矩阵
  3. 如果幂 逆(如果存在) 然后对其使用矩阵求幂

我擅长算法和实现,但这是 我第一次使用任何开源库时,我想要 学习这一点,以便我最终可以为提升做出贡献。

这是我的头文件

//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_UBLAS_POW_HPP
#define BOOST_UBLAS_POW_HPP 
#include <iostream>
#include <vector>
#include <iomanip>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/mpl/set.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::numeric::ublas;
namespace boost { namespace numeric { namespace ublas {

    typedef permutation_matrix<std::size_t> pmatrix;
    template< typename T,typename T2 >
        matrix<T> matrix_power(const matrix<T> input, T2 exponent)
        {
          matrix<T> resultant=input;
          BOOST_ASSERT_MSG(input.size1()==input.size2(),"Not a square matrix\n");
           if(exponent>0) 
            resultant=matrix_exponent(input,exponent);// this where you could directly use matrix exponentiation 

           else if(exponent==0)
            {
             pmatrix pm(input.size1());
             matrix<T> A(input);
             BOOST_ASSERT_MSG(lu_factorize(A, pm)==0,"Attempted to compute a  power of 0 in a matrix without inverse\n");
             resultant.assign(identity_matrix<T> (input.size1()));// if the matrix is invertible output identity matrix for the 0t hpower
            }
            else {
              matrix<T> A(input);
              pmatrix pm(A.size1());
              BOOST_ASSERT_MSG(lu_factorize(A, pm)==0,"Attempted to compute inverse in a singular matrix\n");
              resultant.assign(identity_matrix<T> (A.size1()));
              lu_substitute(A, pm, resultant);
              resultant=matrix_exponent(resultant,std::abs(exponent));
            }// in last case first we compute the inverse of the matrix and then we do matrix exponentiation 
          return resultant;
        }



    template< typename T,typename T2 >
        matrix<T> matrix_exponent(matrix<T> base,T2 exponent)    
       {
          matrix<T> resultant=base;
          exponent-=2;
          while(exponent>0)
            {
              if(exponent%2==1)resultant=prod(resultant,base);
              exponent=exponent >> 1;
              base=prod(base,base);
            }
          return resultant;  
       }
    }
  }
}
#endif

我正在使用这个测试这个头文件

#include "power.hpp"
using namespace boost::numeric::ublas;
using namespace std;
typedef permutation_matrix<std::size_t> pmatrix;
int main() 
{
matrix<double> m (3, 3);
for(int i=0;i<3;i++)for(int j=0;j<3;j++)m(i,j)=3*i+j+8;
m(0,0)=11;
matrix<double> c(3, 3);
int h=matrix_power(m,c,-1);// c stores -1 power of m
if(h)// h tells whether the power exists or not 
std:: cout << c << "\n\n\n";}

函数在幂 >0 的情况下工作得很好,因为它使用矩阵 直接取幂。该工作比重复乘法快得多,我已经看到大约 1000 次迭代的运行时间和使用循环的时间相差 100-1000 倍。你可以观察到 但是对于功率 (我使用矩阵和逆矩阵的乘积是单位矩阵的想法检查了这一点)

这可能与 lu_factorize 和 lu_substitute 进行某些检查以确保变量类型正确。

由于没有可用于 lu_factorize 的文档,我不知道如何使用它。 (我只是在 using Ublas lu_factorize and lu_substitute to compute matrix inverse 阅读了一个可用的示例)。我什至阅读了源代码,但由于缺乏经验,代码掌握得并不多。

我现在有几个关于我遇到的问题的问题。 由于这是我第一次尝试提升,如果我问一些愚蠢的问题,请不要对我苛刻。 所以这些是我的问题-

  1. 错误答案可能是由于数据类型之间的错误转换或类似原因。我该如何解决这个问题?我如何确保在每一步都使用正确的数据类型?
  2. 当用户输入不正确的类型时,我如何给出错误,我知道我可以使用 boost assert 但那是 给出大量难以理解的编译错误。什么是 确保输入类型有效的最简单方法。例如我想要 如果用户提供输入字符串,则给出错误。 你能举个例子吗?
  3. 我尝试了各种方法来解决编译错误 其中之一是使用 #define BOOST_UBLAS_TYPE_CHECK 0 这有助于绕过数据类型的编译错误,但后来我得到了不正确的答案。你能解释一下至少在这种情况下它是如何工作的吗?

  4. 由于这是我第一次尝试从提升中获得任何东西,我可以理解这当然可以做得更好。此头文件中还必须包含哪些其他内容以确保库标准,例如 错误处理、支持多编译器等?

【问题讨论】:

    标签: c++ matrix boost matrix-multiplication boost-ublas


    【解决方案1】:

    您的 matrix_power() 函数不会从最后的 else 块返回值。如果我在该块的末尾添加return true,那么您的代码将为我运行:

    $ clang++ --version
    Apple LLVM version 7.0.0 (clang-700.1.76)
    Target: x86_64-apple-darwin14.5.0
    Thread model: posix
    
    $ clang++ -std=c++11 -Wall -Wextra -g -I/opt/local/include lu.cpp -o lu
    $ ./lu
    [3,3]((-0.0644531,-0.00195312,0.861328),(1.09896,-0.0677083,-11.1406),(-0.9375,0.0625,9.4375))
    

    我还能够删除 BOOST_UBLAS_TYPE_CHECK 定义而不会出现编译时(或运行时)错误。我正在使用 Boost 1.59。

    【讨论】:

    • 非常感谢@rhasimoto,但错误有些不同,我能够解决其中的一部分,我已在编辑中更正,但其余问题仍未得到解答。