Creating sympy Matrices from columns
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
add a comment |
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
add a comment |
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
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
python matrix sympy
edited Nov 13 '18 at 5:02
Cœur
17.9k9107147
17.9k9107147
asked May 4 '17 at 8:01
pyrogenpyrogen
373
373
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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]])
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 usedsympy 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]])
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 usedsympy 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
add a comment |
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]])
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 usedsympy 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
add a comment |
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]])
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]])
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 usedsympy 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
add a comment |
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 usedsympy 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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