Eigen non constant MatrixReplacement for sparse solver





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I want to use matrix free sparse solvers with custom matrix-vector product object. Here is great example how to to it - https://eigen.tuxfamily.org/dox/group__MatrixfreeSolverExample.html



But in this example custom matrix-product object should be constant due to generic_product_impl signature



template<typename Dest>
static void scaleAndAddTo(
Dest& dst,
const MatrixReplacement& lhs,
const Rhs& rhs,
const Scalar& alpha)


In many my problems i need a lot of temporary buffers for each product call. It's pretty wise to allocate them once but i can't store them inside MatrixReplacement because it passed as const.



Is it possible in Eigen to overcome this problem?










share|improve this question































    1















    I want to use matrix free sparse solvers with custom matrix-vector product object. Here is great example how to to it - https://eigen.tuxfamily.org/dox/group__MatrixfreeSolverExample.html



    But in this example custom matrix-product object should be constant due to generic_product_impl signature



    template<typename Dest>
    static void scaleAndAddTo(
    Dest& dst,
    const MatrixReplacement& lhs,
    const Rhs& rhs,
    const Scalar& alpha)


    In many my problems i need a lot of temporary buffers for each product call. It's pretty wise to allocate them once but i can't store them inside MatrixReplacement because it passed as const.



    Is it possible in Eigen to overcome this problem?










    share|improve this question



























      1












      1








      1








      I want to use matrix free sparse solvers with custom matrix-vector product object. Here is great example how to to it - https://eigen.tuxfamily.org/dox/group__MatrixfreeSolverExample.html



      But in this example custom matrix-product object should be constant due to generic_product_impl signature



      template<typename Dest>
      static void scaleAndAddTo(
      Dest& dst,
      const MatrixReplacement& lhs,
      const Rhs& rhs,
      const Scalar& alpha)


      In many my problems i need a lot of temporary buffers for each product call. It's pretty wise to allocate them once but i can't store them inside MatrixReplacement because it passed as const.



      Is it possible in Eigen to overcome this problem?










      share|improve this question
















      I want to use matrix free sparse solvers with custom matrix-vector product object. Here is great example how to to it - https://eigen.tuxfamily.org/dox/group__MatrixfreeSolverExample.html



      But in this example custom matrix-product object should be constant due to generic_product_impl signature



      template<typename Dest>
      static void scaleAndAddTo(
      Dest& dst,
      const MatrixReplacement& lhs,
      const Rhs& rhs,
      const Scalar& alpha)


      In many my problems i need a lot of temporary buffers for each product call. It's pretty wise to allocate them once but i can't store them inside MatrixReplacement because it passed as const.



      Is it possible in Eigen to overcome this problem?







      c++ eigen eigen3 mutability






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 13:47







      Dark_Daiver

















      asked Nov 16 '18 at 13:38









      Dark_DaiverDark_Daiver

      7401932




      7401932
























          1 Answer
          1






          active

          oldest

          votes


















          3














          There are two immediate options:




          1. Use the mutable keyword for the members that need to change in const methods (i.e. your temporary buffers). This keyword makes sense where observable behavior of your class is const, despite you needing to modify members. Examples include cached values, mutexes, or your buffers.


          2. C++ is not perfectly strict with propagating const. A const unique_ptr<T> will return a (non-const) T& when dereferenced (because the const says "you can't change the pointer", not "you can't change the pointee"; this is the same with builtin pointers). You can similarly wrap your "real" sparse matrix class in something that pretends to be const but allows non-const access to the matrix if the STL smart pointers are insufficient. If you give it an appropriate name then it's not as terrible as it sounds.



          I recommend option 1.






          share|improve this answer
























          • Thank you for answer! Do i understand correctly that first option is preferable because of "explicitness"?

            – Dark_Daiver
            Nov 16 '18 at 14:01






          • 1





            It's just an appropriate place to use mutable, it is simply idiomatic. The second feels a lot like a hack (because it is circumventing the original intent of the interface entirely). I'm still suggesting it because sometimes mutable is not the "correct"/idiomatic solution.

            – Max Langhof
            Nov 16 '18 at 14:42














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338985%2feigen-non-constant-matrixreplacement-for-sparse-solver%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          There are two immediate options:




          1. Use the mutable keyword for the members that need to change in const methods (i.e. your temporary buffers). This keyword makes sense where observable behavior of your class is const, despite you needing to modify members. Examples include cached values, mutexes, or your buffers.


          2. C++ is not perfectly strict with propagating const. A const unique_ptr<T> will return a (non-const) T& when dereferenced (because the const says "you can't change the pointer", not "you can't change the pointee"; this is the same with builtin pointers). You can similarly wrap your "real" sparse matrix class in something that pretends to be const but allows non-const access to the matrix if the STL smart pointers are insufficient. If you give it an appropriate name then it's not as terrible as it sounds.



          I recommend option 1.






          share|improve this answer
























          • Thank you for answer! Do i understand correctly that first option is preferable because of "explicitness"?

            – Dark_Daiver
            Nov 16 '18 at 14:01






          • 1





            It's just an appropriate place to use mutable, it is simply idiomatic. The second feels a lot like a hack (because it is circumventing the original intent of the interface entirely). I'm still suggesting it because sometimes mutable is not the "correct"/idiomatic solution.

            – Max Langhof
            Nov 16 '18 at 14:42


















          3














          There are two immediate options:




          1. Use the mutable keyword for the members that need to change in const methods (i.e. your temporary buffers). This keyword makes sense where observable behavior of your class is const, despite you needing to modify members. Examples include cached values, mutexes, or your buffers.


          2. C++ is not perfectly strict with propagating const. A const unique_ptr<T> will return a (non-const) T& when dereferenced (because the const says "you can't change the pointer", not "you can't change the pointee"; this is the same with builtin pointers). You can similarly wrap your "real" sparse matrix class in something that pretends to be const but allows non-const access to the matrix if the STL smart pointers are insufficient. If you give it an appropriate name then it's not as terrible as it sounds.



          I recommend option 1.






          share|improve this answer
























          • Thank you for answer! Do i understand correctly that first option is preferable because of "explicitness"?

            – Dark_Daiver
            Nov 16 '18 at 14:01






          • 1





            It's just an appropriate place to use mutable, it is simply idiomatic. The second feels a lot like a hack (because it is circumventing the original intent of the interface entirely). I'm still suggesting it because sometimes mutable is not the "correct"/idiomatic solution.

            – Max Langhof
            Nov 16 '18 at 14:42
















          3












          3








          3







          There are two immediate options:




          1. Use the mutable keyword for the members that need to change in const methods (i.e. your temporary buffers). This keyword makes sense where observable behavior of your class is const, despite you needing to modify members. Examples include cached values, mutexes, or your buffers.


          2. C++ is not perfectly strict with propagating const. A const unique_ptr<T> will return a (non-const) T& when dereferenced (because the const says "you can't change the pointer", not "you can't change the pointee"; this is the same with builtin pointers). You can similarly wrap your "real" sparse matrix class in something that pretends to be const but allows non-const access to the matrix if the STL smart pointers are insufficient. If you give it an appropriate name then it's not as terrible as it sounds.



          I recommend option 1.






          share|improve this answer













          There are two immediate options:




          1. Use the mutable keyword for the members that need to change in const methods (i.e. your temporary buffers). This keyword makes sense where observable behavior of your class is const, despite you needing to modify members. Examples include cached values, mutexes, or your buffers.


          2. C++ is not perfectly strict with propagating const. A const unique_ptr<T> will return a (non-const) T& when dereferenced (because the const says "you can't change the pointer", not "you can't change the pointee"; this is the same with builtin pointers). You can similarly wrap your "real" sparse matrix class in something that pretends to be const but allows non-const access to the matrix if the STL smart pointers are insufficient. If you give it an appropriate name then it's not as terrible as it sounds.



          I recommend option 1.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 13:55









          Max LanghofMax Langhof

          12.1k22241




          12.1k22241













          • Thank you for answer! Do i understand correctly that first option is preferable because of "explicitness"?

            – Dark_Daiver
            Nov 16 '18 at 14:01






          • 1





            It's just an appropriate place to use mutable, it is simply idiomatic. The second feels a lot like a hack (because it is circumventing the original intent of the interface entirely). I'm still suggesting it because sometimes mutable is not the "correct"/idiomatic solution.

            – Max Langhof
            Nov 16 '18 at 14:42





















          • Thank you for answer! Do i understand correctly that first option is preferable because of "explicitness"?

            – Dark_Daiver
            Nov 16 '18 at 14:01






          • 1





            It's just an appropriate place to use mutable, it is simply idiomatic. The second feels a lot like a hack (because it is circumventing the original intent of the interface entirely). I'm still suggesting it because sometimes mutable is not the "correct"/idiomatic solution.

            – Max Langhof
            Nov 16 '18 at 14:42



















          Thank you for answer! Do i understand correctly that first option is preferable because of "explicitness"?

          – Dark_Daiver
          Nov 16 '18 at 14:01





          Thank you for answer! Do i understand correctly that first option is preferable because of "explicitness"?

          – Dark_Daiver
          Nov 16 '18 at 14:01




          1




          1





          It's just an appropriate place to use mutable, it is simply idiomatic. The second feels a lot like a hack (because it is circumventing the original intent of the interface entirely). I'm still suggesting it because sometimes mutable is not the "correct"/idiomatic solution.

          – Max Langhof
          Nov 16 '18 at 14:42







          It's just an appropriate place to use mutable, it is simply idiomatic. The second feels a lot like a hack (because it is circumventing the original intent of the interface entirely). I'm still suggesting it because sometimes mutable is not the "correct"/idiomatic solution.

          – Max Langhof
          Nov 16 '18 at 14:42






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338985%2feigen-non-constant-matrixreplacement-for-sparse-solver%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly