In: Computer Science
Design a Python class named HomogenEquation for a system of 2x2 linear homogeneous equations:
ax+by=e cx+dy=f
The class should contain:
• The data fields a, b, c, d, e and f.
• A constructor with the arguments a, b, c, d, e and f.
• A method named isSolvable() that returns true if − is not 0.
• The method getXY() that returns the solution for the equation.
Write a test program that prompts the user to enter a, b, c, d, e and f and displays the result. If the equation is not solvable, report that “The equation has no solution.”
The below is the code for the above problem statement:
---------------------------------------------------------------CODE---------------------------------------------------------------------
import numpy as np
class HomogenEquation:
def __init__(self,a,b,c,d,e,f):
self.a=a
self.b=b
self.c=c
self.d=d
self.e=e
self.f=f
def isSolvable(self):
'''
The linear
equation is only solvable
if (a/b) !=
(d/e)
otherwise the
equation does not posses an
unique
solution
'''
if (self.a/self.b)-(self.d/self.e)
!= 0:
return
True
else:
return
False
def getXY(self):
'''
We solve the
linear equation with the help of the
built in
function solve() which is defined inside
numpy.linalg
'''
A=np.array([[self.a,self.b],[self.d,self.e]])
B=np.array([self.c,self.f])
x,y=np.linalg.solve(A,B)
return x,y
#Driver Function to Test the
functionality of the class
def main():
a,b,c=map(int,input("Enter Values of a,b,c :
").split())
d,e,f=map(int,input("Enter Values of d,e,f :
").split())
Eq=HomogenEquation(a,b,c,d,e,f)
if Eq.isSolvable():
x,y=Eq.getXY()
print("X = ",x,"\nY = ",y)
else:
print("The equation has no
solution.")
main()
--------------------------------------------------------------------------------END------------------------------------------------------------
Code in sublime text:
OUTPUT: