coin-or / CppAD

A C++ Algorithmic Differentiation Package: Home Page
https://cppad.readthedocs.io
Other
446 stars 94 forks source link

About Eigen. I can't store the scalar result of a matrix operation at the index location of another std::vector or Eigen::Vector. #198

Closed dujinpeng0249 closed 3 months ago

dujinpeng0249 commented 4 months ago

The test code is as follows:

Eigen::Matrix<AD<double>, 2, 2> smooth_weight_Mat_;
smooth_weight_Mat_ << 1, 0, 0, 1;
Eigen::Matrix<AD<double>, 6, 2> C_ = Eigen::Matrix<double, 6, 2>::Ones();
std::vector<AD<double>> smooth_weigths_perPiece_(6);
smooth_weigths_perPiece_[1]= C_.row(6 * i + 3) * smooth_weight_Mat_ * C_.row(6 * i + 3).transpose(); 

the build error is: /usr/local/include/cppad/core/ad_assign.hpp:130:12: error: invalid cast from type ‘const Eigen::Product<Eigen::Product<Eigen::Block<Eigen::Matrix<CppAD::AD, 6, 2>, 1, 2, false>, Eigen::Matrix<CppAD::AD, 2, 2, 0, 2, 2>, 0>, Eigen::Transpose<Eigen::Block<Eigen::Matrix<CppAD::AD, 6, 2>, 1, 2, false> >, 0>’ to type ‘double’

I think the problem is in the last line smooth_weigths_perPiece_[1]= C_.row(6 * i + 3) * smooth_weight_Mat_ * C_.row(6 * i + 3).transpose(); However, I don't know what's wrong with that.

Also, even if I change the type of smooth_weigthsperPiece to Eigen::Matrix<AD,-1,1> it does not work too. Eigen::Matrix<AD<double>,-1,1> smooth_weigths_perPiece_(6);

The only way to build successfully is to:

Eigen::Matrix<AD<double>,-1,1> smooth_weigths_perPiece_(6);
smooth_weigths_perPiece_.head(1)= C_.row(6 * i + 3) * smooth_weight_Mat_ * C_.row(6 * i + 3).transpose(); 

But I think it's inconvenient and shouldn't be the right way. Can you help me see what the problem is, I would greatly appreciate it!

bradbell commented 4 months ago

You may want to look at the following example: https://cppad.readthedocs.io/latest/eigen.html

I am getting a different compiler error from you. Would you please be more precise in your example; i.e., specify the entire example file including includes and specify the compile command.

dujinpeng0249 commented 4 months ago

Thank you for your timely reply. I have provided the full cpp example and cmakelis as shown below, and at the end of the reply I have also provided the full error message, which is generated by vscode's cmaketools. test0.cpp

#include <Eigen/Eigen>
#include <cppad/cppad.hpp>
#include <cppad/example/cppad_eigen.hpp>

using std::cout;
using std::endl;
using CppAD::AD;

int main() {
    Eigen::Matrix<AD<double>, 2, 2> smooth_weight_Mat_;
    smooth_weight_Mat_ << 1, 0, 0, 1;
    Eigen::Matrix<AD<double>, 6, 2> C_ = Eigen::Matrix<double, 6, 2>::Ones();

    std::vector<AD<double>>  smooth_weigths_perPiece_(6);
    int i = 0;
    smooth_weigths_perPiece_[0] = C_.row(6 * i + 3)*smooth_weight_Mat_ * C_.row(6 * i + 3).transpose();

  return 0;
}

cmakelist.txt

# The cmake config kit is GCC 9.4.0 X86_64-linux-gnu
cmake_minimum_required(VERSION 3.20.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
    message("Setting default build type to Release")
endif()

project(my_project_name VERSION 0.0.1 LANGUAGES C CXX)

include_directories(
    ${PROJECT_SOURCE_DIR}/include
    /usr/include/eigen3
    /usr/include/python3.8
)

add_executable(test0
    test0.cpp
)

target_link_libraries(test0
    PUBLIC
    cppad_lib
)

The Full Error Message

[proc]  /usr/local/bin/cmake --build /home/dujpubuntu2004/drivingRiskAwarePlanner_240219/build --config Debug --target test0 -j 22 --
[build] [ 50%] Building CXX object test/CMakeFiles/test0.dir/test0.cpp.o
[build] In file included from /usr/local/include/cppad/core/user_ad.hpp:54,
[build]                  from /usr/local/include/cppad/cppad.hpp:49,
[build]                  from /home/dujpubuntu2004/drivingRiskAwarePlanner_240219/test/test0.cpp:3:
[build] /usr/local/include/cppad/core/ad_assign.hpp: In instantiation of ‘CppAD::AD<Base>& CppAD::AD<Base>::operator=(const T&) [with T = Eigen::Product<Eigen::Product<Eigen::Block<Eigen::Matrix<CppAD::AD<double>, 6, 2>, 1, 2, false>, Eigen::Matrix<CppAD::AD<double>, 2, 2>, 0>, Eigen::Transpose<Eigen::Block<Eigen::Matrix<CppAD::AD<double>, 6, 2>, 1, 2, false> >, 0>; Base = double]’:
[build] /home/dujpubuntu2004/drivingRiskAwarePlanner_240219/test/test0.cpp:17:102:   required from here
[build] /usr/local/include/cppad/core/ad_assign.hpp:130:12: error: invalid cast from type ‘const Eigen::Product<Eigen::Product<Eigen::Block<Eigen::Matrix<CppAD::AD<double>, 6, 2>, 1, 2, false>, Eigen::Matrix<CppAD::AD<double>, 2, 2>, 0>, Eigen::Transpose<Eigen::Block<Eigen::Matrix<CppAD::AD<double>, 6, 2>, 1, 2, false> >, 0>’ to type ‘double’
[build]   130 | {  *this = Base(t);
[build]       |            ^~~~~~~
[build] make[3]: *** [test/CMakeFiles/test0.dir/build.make:76: test/CMakeFiles/test0.dir/test0.cpp.o] Error 1
[build] make[2]: *** [CMakeFiles/Makefile2:250: test/CMakeFiles/test0.dir/all] Error 2
[build] make[1]: *** [CMakeFiles/Makefile2:257: test/CMakeFiles/test0.dir/rule] Error 2
[build] make: *** [Makefile:177: test0] Error 2

Looking forward to your reply, thanks a lot!

bradbell commented 3 months ago

Sorry for the delay. How about changing

smooth_weigths_perPiece_[0] = C_.row(6 * i + 3)*smooth_weight_Mat_ * C_.row(6 * i + 3).transpose();

to

Eigen::Matrix<AD<double>, 1, 1> temp =
    C_.row(6 * i + 3)*smooth_weight_Mat_ * C_.row(6 * i + 3).transpose();
smooth_weigths_perPiece_[0] = temp[0];
bradbell commented 3 months ago

@dujinpeng0249 Did the message above solve your problem ? If so, would you please close this issue.

dujinpeng0249 commented 3 months ago

Hi. Sorry for the delay. I have found a solution like this

smooth_weigths_perPiece_[0] = C_.row(6 * i + 3).dot(smooth_weight_Mat_ * C_.row(6 * i + 3).transpose());

Thanks for your patience, I'll close this issue.