Creating sympy Matrices from columns










1















I'm trying to create a sympy Matrix by choosing columns from an existing Matrix (for calculating principal minors). At the moment I'm doing it like this:



>>> A = Matrix(3,5,[2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
>>> l = [A[:,i].T for i in [2,3,0]]
>>> M = Matrix(l).T
>>> M
Matrix([
[ 4, 1, 2],
[54, 5, 23],
[ 3, 4, 0]])


But this seems wasteful to me (especially the need to transpose twice. I don't know if this is time consuming). Is there a better way? Would there be a better way if i only need the determinant?










share|improve this question




























    1















    I'm trying to create a sympy Matrix by choosing columns from an existing Matrix (for calculating principal minors). At the moment I'm doing it like this:



    >>> A = Matrix(3,5,[2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
    >>> l = [A[:,i].T for i in [2,3,0]]
    >>> M = Matrix(l).T
    >>> M
    Matrix([
    [ 4, 1, 2],
    [54, 5, 23],
    [ 3, 4, 0]])


    But this seems wasteful to me (especially the need to transpose twice. I don't know if this is time consuming). Is there a better way? Would there be a better way if i only need the determinant?










    share|improve this question


























      1












      1








      1








      I'm trying to create a sympy Matrix by choosing columns from an existing Matrix (for calculating principal minors). At the moment I'm doing it like this:



      >>> A = Matrix(3,5,[2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
      >>> l = [A[:,i].T for i in [2,3,0]]
      >>> M = Matrix(l).T
      >>> M
      Matrix([
      [ 4, 1, 2],
      [54, 5, 23],
      [ 3, 4, 0]])


      But this seems wasteful to me (especially the need to transpose twice. I don't know if this is time consuming). Is there a better way? Would there be a better way if i only need the determinant?










      share|improve this question
















      I'm trying to create a sympy Matrix by choosing columns from an existing Matrix (for calculating principal minors). At the moment I'm doing it like this:



      >>> A = Matrix(3,5,[2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
      >>> l = [A[:,i].T for i in [2,3,0]]
      >>> M = Matrix(l).T
      >>> M
      Matrix([
      [ 4, 1, 2],
      [54, 5, 23],
      [ 3, 4, 0]])


      But this seems wasteful to me (especially the need to transpose twice. I don't know if this is time consuming). Is there a better way? Would there be a better way if i only need the determinant?







      python matrix sympy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 5:02









      Cœur

      17.9k9107147




      17.9k9107147










      asked May 4 '17 at 8:01









      pyrogenpyrogen

      373




      373






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use [2, 3, 0] as index.



          >>> A = Matrix(3, 5, [2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
          >>> A[:, [2,3,0]]
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])



          For lower version that does not support using list as a index, you can use Matrix.hstack:



          >>> Matrix.hstack(*(A.col(i) for i in [2,3,0]))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])


          or Matrix.row_join:



          >>> # from functools import reduce # For Python 3.x
          >>> reduce(Matrix.row_join, (A.col(i) for i in [2,3,0]), Matrix(3,0,))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])





          share|improve this answer

























          • This does not work for me. I get an IndexError: Invalid index a[[2, 3, 0]]. Do I have a wrong version (0.7.4.1)? or is this only possible with numpy matrices?

            – pyrogen
            May 12 '17 at 11:10











          • @pyrogen, I used sympy 1.0

            – falsetru
            May 12 '17 at 16:06











          • @pyrogen, I updated the answer to include alternative approaches. (tested with sympy 0.7.4.1)

            – falsetru
            May 12 '17 at 16:23












          • great! it works. I didnt know about hstack

            – pyrogen
            May 17 '17 at 9:58










          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%2f43777147%2fcreating-sympy-matrices-from-columns%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









          1














          You can use [2, 3, 0] as index.



          >>> A = Matrix(3, 5, [2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
          >>> A[:, [2,3,0]]
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])



          For lower version that does not support using list as a index, you can use Matrix.hstack:



          >>> Matrix.hstack(*(A.col(i) for i in [2,3,0]))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])


          or Matrix.row_join:



          >>> # from functools import reduce # For Python 3.x
          >>> reduce(Matrix.row_join, (A.col(i) for i in [2,3,0]), Matrix(3,0,))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])





          share|improve this answer

























          • This does not work for me. I get an IndexError: Invalid index a[[2, 3, 0]]. Do I have a wrong version (0.7.4.1)? or is this only possible with numpy matrices?

            – pyrogen
            May 12 '17 at 11:10











          • @pyrogen, I used sympy 1.0

            – falsetru
            May 12 '17 at 16:06











          • @pyrogen, I updated the answer to include alternative approaches. (tested with sympy 0.7.4.1)

            – falsetru
            May 12 '17 at 16:23












          • great! it works. I didnt know about hstack

            – pyrogen
            May 17 '17 at 9:58















          1














          You can use [2, 3, 0] as index.



          >>> A = Matrix(3, 5, [2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
          >>> A[:, [2,3,0]]
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])



          For lower version that does not support using list as a index, you can use Matrix.hstack:



          >>> Matrix.hstack(*(A.col(i) for i in [2,3,0]))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])


          or Matrix.row_join:



          >>> # from functools import reduce # For Python 3.x
          >>> reduce(Matrix.row_join, (A.col(i) for i in [2,3,0]), Matrix(3,0,))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])





          share|improve this answer

























          • This does not work for me. I get an IndexError: Invalid index a[[2, 3, 0]]. Do I have a wrong version (0.7.4.1)? or is this only possible with numpy matrices?

            – pyrogen
            May 12 '17 at 11:10











          • @pyrogen, I used sympy 1.0

            – falsetru
            May 12 '17 at 16:06











          • @pyrogen, I updated the answer to include alternative approaches. (tested with sympy 0.7.4.1)

            – falsetru
            May 12 '17 at 16:23












          • great! it works. I didnt know about hstack

            – pyrogen
            May 17 '17 at 9:58













          1












          1








          1







          You can use [2, 3, 0] as index.



          >>> A = Matrix(3, 5, [2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
          >>> A[:, [2,3,0]]
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])



          For lower version that does not support using list as a index, you can use Matrix.hstack:



          >>> Matrix.hstack(*(A.col(i) for i in [2,3,0]))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])


          or Matrix.row_join:



          >>> # from functools import reduce # For Python 3.x
          >>> reduce(Matrix.row_join, (A.col(i) for i in [2,3,0]), Matrix(3,0,))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])





          share|improve this answer















          You can use [2, 3, 0] as index.



          >>> A = Matrix(3, 5, [2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
          >>> A[:, [2,3,0]]
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])



          For lower version that does not support using list as a index, you can use Matrix.hstack:



          >>> Matrix.hstack(*(A.col(i) for i in [2,3,0]))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])


          or Matrix.row_join:



          >>> # from functools import reduce # For Python 3.x
          >>> reduce(Matrix.row_join, (A.col(i) for i in [2,3,0]), Matrix(3,0,))
          Matrix([
          [ 4, 1, 2],
          [54, 5, 23],
          [ 3, 4, 0]])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 12 '17 at 16:19

























          answered May 4 '17 at 8:14









          falsetrufalsetru

          246k34430427




          246k34430427












          • This does not work for me. I get an IndexError: Invalid index a[[2, 3, 0]]. Do I have a wrong version (0.7.4.1)? or is this only possible with numpy matrices?

            – pyrogen
            May 12 '17 at 11:10











          • @pyrogen, I used sympy 1.0

            – falsetru
            May 12 '17 at 16:06











          • @pyrogen, I updated the answer to include alternative approaches. (tested with sympy 0.7.4.1)

            – falsetru
            May 12 '17 at 16:23












          • great! it works. I didnt know about hstack

            – pyrogen
            May 17 '17 at 9:58

















          • This does not work for me. I get an IndexError: Invalid index a[[2, 3, 0]]. Do I have a wrong version (0.7.4.1)? or is this only possible with numpy matrices?

            – pyrogen
            May 12 '17 at 11:10











          • @pyrogen, I used sympy 1.0

            – falsetru
            May 12 '17 at 16:06











          • @pyrogen, I updated the answer to include alternative approaches. (tested with sympy 0.7.4.1)

            – falsetru
            May 12 '17 at 16:23












          • great! it works. I didnt know about hstack

            – pyrogen
            May 17 '17 at 9:58
















          This does not work for me. I get an IndexError: Invalid index a[[2, 3, 0]]. Do I have a wrong version (0.7.4.1)? or is this only possible with numpy matrices?

          – pyrogen
          May 12 '17 at 11:10





          This does not work for me. I get an IndexError: Invalid index a[[2, 3, 0]]. Do I have a wrong version (0.7.4.1)? or is this only possible with numpy matrices?

          – pyrogen
          May 12 '17 at 11:10













          @pyrogen, I used sympy 1.0

          – falsetru
          May 12 '17 at 16:06





          @pyrogen, I used sympy 1.0

          – falsetru
          May 12 '17 at 16:06













          @pyrogen, I updated the answer to include alternative approaches. (tested with sympy 0.7.4.1)

          – falsetru
          May 12 '17 at 16:23






          @pyrogen, I updated the answer to include alternative approaches. (tested with sympy 0.7.4.1)

          – falsetru
          May 12 '17 at 16:23














          great! it works. I didnt know about hstack

          – pyrogen
          May 17 '17 at 9:58





          great! it works. I didnt know about hstack

          – pyrogen
          May 17 '17 at 9:58

















          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%2f43777147%2fcreating-sympy-matrices-from-columns%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

          Darth Vader #20

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Ondo