How to “reshape” into square matrix for numpy.linalg.solve()? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Moderator Election Q&A - Questionnaire 2019 Community Moderator Election ResultsCan't understand this simple matrix multiplication in pythonExporting Correlation Matrix (from function)Dimensions For Matrix MultiplicationOn minimizing matrix norm (AB-C)Are view() in Pytorch and reshape() in Numpy similar?Matrix multiplication issue (shapes not alligned)Normalize matrix in Python numpyStructures for incorporating linear functions into a nonlinear optimization problemFaster 3D Matrix Operation - PythonHaving difficult interpreting the eigenvectors for a simple 3x2 matrix
What are the possible ways to detect skin while classifying diseases?
How to do this path/lattice with tikz
How to deal with my PhD supervisors rudely critiquing all my draft papers?
Do I really need recursive chmod to restrict access to a folder?
Check which numbers satisfy the condition [A*B*C = A! + B! + C!]
What are the pros and cons of Aerospike nosecones?
"Seemed to had" is it correct?
Is the Standard Deduction better than Itemized when both are the same amount?
What happens to sewage if there is no river near by?
Right-skewed distribution with mean equals to mode?
Determinant is linear as a function of each of the rows of the matrix.
Why does Python start at index -1 (as opposed to 0) when indexing a list from the end?
What is a Meta algorithm?
Did Kevin spill real chili?
When to stop saving and start investing?
Is the address of a local variable a constexpr?
How do I keep my slimes from escaping their pens?
Using et al. for a last / senior author rather than for a first author
How do I stop a creek from eroding my steep embankment?
What LEGO pieces have "real-world" functionality?
What is the longest distance a 13th-level monk can jump while attacking on the same turn?
How to find all the available tools in macOS terminal?
ListPlot join points by nearest neighbor rather than order
Is the argument below valid?
How to “reshape” into square matrix for numpy.linalg.solve()?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election ResultsCan't understand this simple matrix multiplication in pythonExporting Correlation Matrix (from function)Dimensions For Matrix MultiplicationOn minimizing matrix norm (AB-C)Are view() in Pytorch and reshape() in Numpy similar?Matrix multiplication issue (shapes not alligned)Normalize matrix in Python numpyStructures for incorporating linear functions into a nonlinear optimization problemFaster 3D Matrix Operation - PythonHaving difficult interpreting the eigenvectors for a simple 3x2 matrix
$begingroup$
I'm trying to find the intersection of lines $y=a_1x+b_1$ and $y=a_2x+b_2$ using numpy.linalg.solve()
. What I can't get my head around is how to correctly make $A$ a square matrix for solve()
to work. I'm familiar with solving linear equation systems, but there's something here I don't get.
What I'd like to do is:
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1], [a2]])
b = np.array([b1, b2])
return np.linalg.solve(a, b)
def main():
a1=1
b1=4
a2=3
b2=2
y, x = meeting_lines(a1, b1, a2, b2)
Where I expect $y=-3$ and $x=1$. However, this fails with numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square
.
Thank you very much for your help, trying to figure this out has messed up my day already!
numpy linear-algebra
$endgroup$
add a comment |
$begingroup$
I'm trying to find the intersection of lines $y=a_1x+b_1$ and $y=a_2x+b_2$ using numpy.linalg.solve()
. What I can't get my head around is how to correctly make $A$ a square matrix for solve()
to work. I'm familiar with solving linear equation systems, but there's something here I don't get.
What I'd like to do is:
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1], [a2]])
b = np.array([b1, b2])
return np.linalg.solve(a, b)
def main():
a1=1
b1=4
a2=3
b2=2
y, x = meeting_lines(a1, b1, a2, b2)
Where I expect $y=-3$ and $x=1$. However, this fails with numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square
.
Thank you very much for your help, trying to figure this out has messed up my day already!
numpy linear-algebra
$endgroup$
$begingroup$
NB: I must usenumpy.linalg.solve()
.
$endgroup$
– basse
Apr 1 at 12:49
add a comment |
$begingroup$
I'm trying to find the intersection of lines $y=a_1x+b_1$ and $y=a_2x+b_2$ using numpy.linalg.solve()
. What I can't get my head around is how to correctly make $A$ a square matrix for solve()
to work. I'm familiar with solving linear equation systems, but there's something here I don't get.
What I'd like to do is:
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1], [a2]])
b = np.array([b1, b2])
return np.linalg.solve(a, b)
def main():
a1=1
b1=4
a2=3
b2=2
y, x = meeting_lines(a1, b1, a2, b2)
Where I expect $y=-3$ and $x=1$. However, this fails with numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square
.
Thank you very much for your help, trying to figure this out has messed up my day already!
numpy linear-algebra
$endgroup$
I'm trying to find the intersection of lines $y=a_1x+b_1$ and $y=a_2x+b_2$ using numpy.linalg.solve()
. What I can't get my head around is how to correctly make $A$ a square matrix for solve()
to work. I'm familiar with solving linear equation systems, but there's something here I don't get.
What I'd like to do is:
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1], [a2]])
b = np.array([b1, b2])
return np.linalg.solve(a, b)
def main():
a1=1
b1=4
a2=3
b2=2
y, x = meeting_lines(a1, b1, a2, b2)
Where I expect $y=-3$ and $x=1$. However, this fails with numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square
.
Thank you very much for your help, trying to figure this out has messed up my day already!
numpy linear-algebra
numpy linear-algebra
asked Apr 1 at 12:47
bassebasse
875
875
$begingroup$
NB: I must usenumpy.linalg.solve()
.
$endgroup$
– basse
Apr 1 at 12:49
add a comment |
$begingroup$
NB: I must usenumpy.linalg.solve()
.
$endgroup$
– basse
Apr 1 at 12:49
$begingroup$
NB: I must use
numpy.linalg.solve()
.$endgroup$
– basse
Apr 1 at 12:49
$begingroup$
NB: I must use
numpy.linalg.solve()
.$endgroup$
– basse
Apr 1 at 12:49
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
You should formulate your lines as follows to have $(x, y)$ as unknowns:
$$beginalign
left.beginmatrix
a_1x-y=-b_1\
a_2x-y=-b_2
endmatrixright}
rightarrow
overbrace
beginbmatrix
a_1& -1\
a_2& -1
endbmatrix
^boldsymbola
overbrace
beginbmatrix
x\
y
endbmatrix
^boldsymbolx
=
overbrace
beginbmatrix
-b_1\
-b_2
endbmatrix
^boldsymbolb
endalign$$
Therefore, the code should be:
import numpy as np
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1, -1], [a2, -1]])
b = np.array([-b1, -b2])
return np.linalg.solve(a, b)
a1=1
b1=4
a2=3
b2=2
x, y = meeting_lines(a1, b1, a2, b2)
print(x, y)
which outputs:
1.0 5.0
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdatascience.stackexchange.com%2fquestions%2f48345%2fhow-to-reshape-into-square-matrix-for-numpy-linalg-solve%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
$begingroup$
You should formulate your lines as follows to have $(x, y)$ as unknowns:
$$beginalign
left.beginmatrix
a_1x-y=-b_1\
a_2x-y=-b_2
endmatrixright}
rightarrow
overbrace
beginbmatrix
a_1& -1\
a_2& -1
endbmatrix
^boldsymbola
overbrace
beginbmatrix
x\
y
endbmatrix
^boldsymbolx
=
overbrace
beginbmatrix
-b_1\
-b_2
endbmatrix
^boldsymbolb
endalign$$
Therefore, the code should be:
import numpy as np
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1, -1], [a2, -1]])
b = np.array([-b1, -b2])
return np.linalg.solve(a, b)
a1=1
b1=4
a2=3
b2=2
x, y = meeting_lines(a1, b1, a2, b2)
print(x, y)
which outputs:
1.0 5.0
$endgroup$
add a comment |
$begingroup$
You should formulate your lines as follows to have $(x, y)$ as unknowns:
$$beginalign
left.beginmatrix
a_1x-y=-b_1\
a_2x-y=-b_2
endmatrixright}
rightarrow
overbrace
beginbmatrix
a_1& -1\
a_2& -1
endbmatrix
^boldsymbola
overbrace
beginbmatrix
x\
y
endbmatrix
^boldsymbolx
=
overbrace
beginbmatrix
-b_1\
-b_2
endbmatrix
^boldsymbolb
endalign$$
Therefore, the code should be:
import numpy as np
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1, -1], [a2, -1]])
b = np.array([-b1, -b2])
return np.linalg.solve(a, b)
a1=1
b1=4
a2=3
b2=2
x, y = meeting_lines(a1, b1, a2, b2)
print(x, y)
which outputs:
1.0 5.0
$endgroup$
add a comment |
$begingroup$
You should formulate your lines as follows to have $(x, y)$ as unknowns:
$$beginalign
left.beginmatrix
a_1x-y=-b_1\
a_2x-y=-b_2
endmatrixright}
rightarrow
overbrace
beginbmatrix
a_1& -1\
a_2& -1
endbmatrix
^boldsymbola
overbrace
beginbmatrix
x\
y
endbmatrix
^boldsymbolx
=
overbrace
beginbmatrix
-b_1\
-b_2
endbmatrix
^boldsymbolb
endalign$$
Therefore, the code should be:
import numpy as np
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1, -1], [a2, -1]])
b = np.array([-b1, -b2])
return np.linalg.solve(a, b)
a1=1
b1=4
a2=3
b2=2
x, y = meeting_lines(a1, b1, a2, b2)
print(x, y)
which outputs:
1.0 5.0
$endgroup$
You should formulate your lines as follows to have $(x, y)$ as unknowns:
$$beginalign
left.beginmatrix
a_1x-y=-b_1\
a_2x-y=-b_2
endmatrixright}
rightarrow
overbrace
beginbmatrix
a_1& -1\
a_2& -1
endbmatrix
^boldsymbola
overbrace
beginbmatrix
x\
y
endbmatrix
^boldsymbolx
=
overbrace
beginbmatrix
-b_1\
-b_2
endbmatrix
^boldsymbolb
endalign$$
Therefore, the code should be:
import numpy as np
def meeting_lines(a1, b1, a2, b2):
a = np.array([[a1, -1], [a2, -1]])
b = np.array([-b1, -b2])
return np.linalg.solve(a, b)
a1=1
b1=4
a2=3
b2=2
x, y = meeting_lines(a1, b1, a2, b2)
print(x, y)
which outputs:
1.0 5.0
answered Apr 1 at 13:52
EsmailianEsmailian
3,311420
3,311420
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fdatascience.stackexchange.com%2fquestions%2f48345%2fhow-to-reshape-into-square-matrix-for-numpy-linalg-solve%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
$begingroup$
NB: I must use
numpy.linalg.solve()
.$endgroup$
– basse
Apr 1 at 12:49