0

I'm try to learning class properties in Python. I'm trying to solve a 2X2 equation. After getting 6 values ​​from the user, I try to make a transaction in the class but it gives an error. Can you help me?

import numpy as np

class HomogenEquation():

     def __init__(self,number): 
        self.number = number
        self.value=0  

     def isSolvable(self,a,d,b,c):
         return (a*d)-(b*c)==0

     def getXY(self,a,b,c,d,e,f):
         x = np.array([[a, b],[c,d]])
         y = np.array([e, f])
         print(np.linalg.solve(x,y))


a=int(input("a: "))
a_value= HomogenEquation(a)

b=int(input("b: "))
b_value= HomogenEquation(b)

e=int(input("e: "))
e_value= HomogenEquation(e)

c=int(input("c: "))
c_value= HomogenEquation(c)

d=int(input("d: "))
d_value= HomogenEquation(d)

f=int(input("f: "))
f_value= HomogenEquation(f)

if a.isSolvable(a,d,b,c):

    getXY(a,b,c,d,e,f)
else:
    print("The equation has no solution.")

2
  • 2
    a is an int, a_value is the object that has as the method isSolvable. You're calling the wrong object. Commented Nov 29, 2019 at 14:02
  • How do I send those values into isSolvable? Commented Nov 29, 2019 at 14:04

3 Answers 3

1

First of all, if I understood the purpose of your class, you're implementing it incorrectly.


Here are my thoughts:

You don't need to create multiple instances of this class for each number, because your class is not a number class. It's an equation class. Assuming that, your code needs to be restructured. There are multiple implementations that can take place, but your's make no sense to me.
Things you can improve:

  • Pass numbers all together to your equation class
  • Not creating multiple instances of equations for each number (makes no sense)

So, minimally changing your code getting the result:

import numpy as np


class HomogenEquation():
     def __init__(self, a, b, c, d, e, f): 
        # initializing class with all variables all together
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.e = e
        self.f = f

     def isSolvable(self):
         return (self.a * self.d) - (self.b * self.c) == 0

     def getXY(self):
         x = np.array([[self.a, self.b],[self.c, self.d]])
         y = np.array([self.e, self.f])
         return np.linalg.solve(x, y)

Allright, so now class structure makes sense (but there are some things to improve still)


So, to use it you just need to initialize an instance with all numbers and call t's methods:

# btw this input looks not right, but I'll leave it

a=int(input("a: "))
b=int(input("b: "))
e=int(input("e: "))
c=int(input("c: "))
d=int(input("d: "))
f=int(input("f: "))

equation = HomogenEquation(a,b,c,d,e,f) # creating one instance for equation

if equation.isSolvable():
    print(equation.getXY())             # and calling it's methods
else:
    print("The equation has no solution.")

Comment: This code still has multiple downsides, but class usage now make sense.
I hope this will help you in your future learning!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I'il check the code. I hope it works :) It will help me understand the classes in Python.
0

You're referencing the wrong object. a, b, c, d, e, f are of type int where a_value, b_value, c_value, d_value, e_value, f_value are of type HomogenEquation.

You probably want:

if a_value.isSolvable(a,d,b,c):
    getXY(a,b,c,d,e,f)
else:
    print("The equation has no solution.")

6 Comments

Ah okey. I got it now. Thank you.
Is the program logic correct? There is no solution every time.
@YusufUçan - Let's walk through this step by step, what problem are you having?
I'm currently getting values ​​from the user. According to the solution of homogeneous equations, if there is no solution, I get "No solution". I'm doing this in the "isSolvable" section. If the equation has a solution, I want to go to the "getXY" section and find the solution. However, he gives an error in that part. I'm new to Python. So I ask a lot of questions. Thanks for your interest.
numpy.linalg.LinAlgError: Singular matrix
|
0

You have the following class:

class HomogenEquation:

 def __init__(self,number): 
    self.number = number
    self.value=0  

 def isSolvable(self,a,d,b,c):
     return (a*d)-(b*c)==0

 def getXY(self,a,b,c,d,e,f):
     x = np.array([[a, b],[c,d]])
     y = np.array([e, f])
     print(np.linalg.solve(x,y))

If you want to collect user input and then create an object, you can do this:

# in the main method
a = int(input("Number: "))

# Collect all the other values the same way.

# Create an object of HomogenEquation
a_value = HomogenEquation(a)
print(a_value.isSolvable(a,b,c,d,e,f))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.