Numpy.linalg.solve() - system of linear equations

Hello i have this exercise ;

and my code is this:

import numpy as np

def meeting_planes(a1, b1, c1, a2, b2, c2, a3, b3, c3):
    # Coefficients matrix
    A = np.array([[-a1, -b1, 1], [-a2, -b2, 1], [-a3, -b3, 1]])
    # Constants vector
    B = np.array([c1, c2, c3])
    # Solve the system of linear equations
    x, y, z = np.linalg.solve(A, B)
    return x, y, z

def main():
    a1 = 1
    b1 = 4
    c1 = 5
    a2 = 3
    b2 = 2
    c2 = 1
    a3 = 2
    b3 = 4
    c3 = 1

    x, y, z = meeting_planes(a1, b1, c1, a2, b2, c2, a3, b3, c3)
    print(f"Planes meet at x={x}, y={y} and z={z}")


if __name__ == "__main__":
    main()

# output
# Planes meet at x=3.999999999999997, y=1.9999999999999973 and z=16.999999999999986

from my understanding i do the right thing but, this question cime with test

#!/usr/bin/python3

import unittest
from unittest.mock import patch

import numpy as np
from numpy.linalg.linalg import LinAlgError

from tmc import points

from tmc.utils import load, get_stdout, patch_helper

module_name = 'src.meeting_planes'
meeting_planes = load(module_name, 'meeting_planes')
ph = patch_helper(module_name)

@points('p03-07.1')
class MeetingPlanes(unittest.TestCase):

    
    def test_first(self):
        a1=1
        b1=4
        c1=5
        
        a2=3
        b2=2
        c2=1
        
        a3=2
        b3=4
        c3=1
        p=(a1,b1,c1,
           a2,b2,c2,
           a3,b3,c3)
        system="(a1=%i, b1=%i, c1=%i, a2=%i, b2=%i, c2=%i, a3=%i, b3=%i, c3=%i)" % p
        
        x, y, z = meeting_planes(a1, b1, c1,
                                 a2, b2, c2,
                                 a3, b3, c3)
        
        self.assertAlmostEqual(z, a1*y + b1*x+c1, msg="Solution %f does not satisfy the first equation of system %s!" % (z, system))
        self.assertAlmostEqual(z, a2*y + b2*x+c2, msg="Solution %f does not satisfy the second equation of system %s!" % (z, system))
        self.assertAlmostEqual(z, a3*y + b3*x+c3, msg="Solution %f does not satisfy the third equation of system %s!" % (z, system))

    def test_calls(self):
        a1=1
        b1=4
        c1=5
        
        a2=3
        b2=2
        c2=1
        
        a3=2
        b3=4
        c3=1

        with patch(ph("np.linalg.solve"), wraps=np.linalg.solve) as psolve:
            meeting_planes(a1, b1, c1, a2, b2, c2, a3, b3, c3)
            psolve.assert_called_once()
            
    def test_underdetermined(self):
        a1=1
        b1=4
        c1=2
        p=(a1,b1,c1,
           a1,b1,c1,
           a1,b1,c1)
        system="(a1=%i, b1=%i, c1=%i, a2=%i, b2=%i, c2=%i, a3=%i, b3=%i, c3=%i)" % p
        with self.assertRaises(np.linalg.linalg.LinAlgError,
                               msg="Under determined system %s should raise an exception!" % system):
            meeting_planes(*p)


    def test_inconsistent(self):
        a1=1
        b1=4
        c1=2
        p=(a1,b1,c1,
           a1,b1,c1,
           a1,b1,c1+1)
        system="(a1=%i, b1=%i, c1=%i, a2=%i, b2=%i, c2=%i, a3=%i, b3=%i, c3=%i)" % p
        with self.assertRaises(np.linalg.linalg.LinAlgError,
                               msg="Inconsistent system %s should raise an exception!" % system):
            meeting_planes(*p)



if __name__ == '__main__':
    unittest.main()

and the my answer not same with the test, i am confuse

## FAIL:

### MeetingPlanes: test_first

16.999999999999986 != 22.999999999999986 within 7 places (6.0 difference) : Solution 17.000000 does not satisfy the first equation of system (a1=1, b1=4, c1=5, a2=3, b2=2, c2=1, a3=2, b3=4, c3=1)!

Thank you, i need advice for this problem.

from wolframAlpha i got the same answer with my solution

I think you have x and y swapped. The equation has y first, then x, but I believe your solution swaps them around.

1 Like

3 hours debug is gone in 1 seconds, thank you :sob:

the correct order

A = np.array([[-b1, -a1, 1], [-b2, -a2, 1], [-b3, -a3, 1]])