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










1












$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!










share|improve this question









$endgroup$











  • $begingroup$
    NB: I must use numpy.linalg.solve().
    $endgroup$
    – basse
    Apr 1 at 12:49















1












$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!










share|improve this question









$endgroup$











  • $begingroup$
    NB: I must use numpy.linalg.solve().
    $endgroup$
    – basse
    Apr 1 at 12:49













1












1








1





$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!










share|improve this question









$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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 1 at 12:47









bassebasse

875




875











  • $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















$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










1 Answer
1






active

oldest

votes


















1












$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





share|improve this answer









$endgroup$













    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
    );



    );













    draft saved

    draft discarded


















    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









    1












    $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





    share|improve this answer









    $endgroup$

















      1












      $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





      share|improve this answer









      $endgroup$















        1












        1








        1





        $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





        share|improve this answer









        $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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 1 at 13:52









        EsmailianEsmailian

        3,311420




        3,311420



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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

            Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

            Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

            Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High