Re: [eigen] SparseMatrix triangularView

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


In the templated case you need to add the 'template' keyword (analogue of typedef for templated methods) as required by the C++ standard:

M.template triangularView<Eigen::Lower>()

See also: https://eigen.tuxfamily.org/dox/TopicTemplateKeyword.html for more details.

Gael

On Mon, Aug 7, 2017 at 11:45 PM, Brad Bell <bradbell@xxxxxxxxxx> wrote:
Why does the template parameter Scalar in test_case<Scalar>, not act like 'double', when compiling
test_case<double> ?

Below you will find a bash script that demonstrates this.
When I run it on my system I get:

trash>./eigen_example.sh
g++ (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)
../eigen_example
x^T = 1 2 3
We got the expected result: x^T = 1 2 3
Now change SparseMatrix<double> to SparseMatrix<Scalar> and recompile:
eigen_example.cpp: In function ‘Eigen::Matrix<Scalar, -1, 1> test_case(Eigen::Matrix<Scalar, -1, 1>&)’:
eigen_example.cpp:13:64: error: expected primary-_expression_ before ‘)’ token
  Matrix<Scalar, Dynamic, 1> x = M.triangularView<Eigen::Lower>().solve(y);
.... snip ...


#!/bin/bash -e
eigen_prefix="$HOME/prefix/eigen"
if [ ! -e "$eigen_prefix/include/Eigen/Dense" ]
then
        echo "Must set eigen_prefix to proper value for this system"
        exit 1
fi
# ----------------------------------------------------------------------------
cat << EOF > eigen_example.cpp
# include <iostream>
# include <Eigen/Dense>
# include <Eigen/SparseCore>

using Eigen::Matrix;
using Eigen::Dynamic;
template <class Scalar> Matrix<Scalar, Dynamic, 1>
test_case(Matrix<Scalar, Dynamic, 1>& y)
{       int n = y.size();
        Eigen::SparseMatrix<double> M(n, n);
        for(int i = 0; i < n; i++)
                M.insert(i, i) = Scalar(1.0);
        Matrix<Scalar, Dynamic, 1> x = M.triangularView<Eigen::Lower>().solve(y);
        return x;
}
int main() {
        int n = 3;
        Matrix<double, Dynamic, 1> y(n);
        for(int i = 0; i < n; i++)
                y(i) = double(i + 1);
        Matrix<double, Dynamic, 1> x = test_case<double>(y);
        std::cout << "x^T = " << x.transpose() << "\n";
        return 0;
}
EOF
# ----------------------------------------------------------------------------
g++ --version | head -1
g++ -Wall -I$eigen_prefix/include eigen_example.cpp -o eigen_example
echo './eigen_example'
../eigen_example
echo 'We got the expected result: x^T = 1 2 3'
# ----------------------------------------------------------------------------
echo 'Now change SparseMatrix<double> to SparseMatrix<Scalar> and recompile:'
sed -i eigen_example.cpp -e 's|SparseMatrix<double>|SparseMatrix<Scalar>|'
g++ -Wall -I$eigen_prefix/include eigen_example.cpp -o eigen_example






Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/