Hi there
My Algebra 2 class just started matrices, and I don't want to go out and pay $100 for a graphing calculator, just for the matrices, when I can code one myself. So... this is my code, and I can do it without classes, but I decided to make things more tidy for myself. It's very basic for now, just defines two different 2x2 matrices, but I'm going slow so I don't screw up. The code works fine by itself, but if I put the functions in a class, I get an error. Here's the code:

#####################################
# I follow the matrices as such:
# matrix A =
# [ 1_1 1_2 1_3 ]
# [ 2_1 2_2 2_3 ]
# [ 3_1 3_2 3_3 ]
# [ 4_1 4_2 4_3 ]
# so on
print 'Simple Matrix Calculator'
print ''
print 'Enter the following:'
print '\'1 for Entering a matrix'
print '     \'1 for a 2x2 matrix'
print '          \'1\' for matrix A'
A = 0
B = 0


class matrix_2x2_define:
    def A(A):
        print 'First row, first column:'
        A_1_1 = int(raw_input())
        print 'First row, second column:'
        A_1_2 = int(raw_input())
        print 'Second row, second column:'
        A_2_1 = int(raw_input())
        print 'Second row, second column:'
        A_2_2 = int(raw_input())
        A_1 = (A_1_1, A_1_2)
        A_2 = (A_2_1, A_2_2)
        matrix_2x2_define.A.A = A_1, A_2
        print 'Matrix A is:'
        #print '[ '+A_1_1+' '+A_1_2+' ]'
        #print '[ '+A_2_1+' '+A_2_2+' ]'
        print A_1
        print A_2

    def B(B):
        print 'First row, first column:'
        B_1_1 = int(raw_input())
        print 'First row, second column:'
        B_1_2 = int(raw_input())
        print 'Second row, second column:'
        B_2_1 = int(raw_input())
        print 'Second row, second column:'
        B_2_2 = int(raw_input())
        B_1 = (B_1_1, B_1_2)
        B_2 = (B_2_1, B_2_2)
        matrix_2x2_define.B.B = B_1, B_2
        print 'Matrix B is:'
        #print '[ '+A_1_1+' '+A_1_2+' ]'
        #print '[ '+A_2_1+' '+A_2_2+' ]'
        print B_1
        print B_2

matrix_2x2_define
matrix_2x2_define.A(A)
matrix_2x2_define.B(B)

print matrix_define_2x2_A.A
print matrix_define_2x2_B.B
print matrix_2x2_define.A.A
print matrix_2x2_define.B.B
raw_input

Here's the error:

Traceback (most recent call last):
  File "C:/Documents and Settings/Student/My Documents/0000/Calculator/Matrix calculator.py", line 102, in <module>
    matrix_2x2_define.A(A)
TypeError: unbound method A() must be called with matrix2x2_define instance as first argument (got int instance instead)_

Any help would be greatly appreciated.

I suppose you want those methods to be instance methods, not class methods, so here goes...

Instance Methods must have their first paramter as `self`. Then, the self variable can be used like `this` in languages like java.
The variable self will be passed automatically by python.

Like so:

class matrix_2x2_define:
    def A(self, A):
        # do stuff

    def B(self, B):
        # do stuff

m = matrix_2x2_define()
m.A(A)
m.B(B)

Note that the name could also be `this`, but naming it `self` is a convention and I recommend you stick to it.

If you want them as class methods, tell me, I'll write the code again. (too lazy :P)

Looking at the line, I take it that you want to get some input? In which case that line will not do that. You have to call the `raw_input` function, like so `raw_input("Hit Enter...")`

Hope that helps :)

Member Avatar for masterofpuppets

hi,
well one of the main purposes of the classes is to be able to create multiple objects of the same type using just one class. So your code could be simplified so that the class creates only one matrix. You can of course create many matrices by having many class instances. Here's what I mean:

print ''
print 'Enter the following:'
print '\'1 for Entering a matrix'
print '     \'1 for a 2x2 matrix'
print '          \'1\' for matrix A'
a = 0
b = 0
 
 
class Matrix:
    """Class to create a single 2x2 matrix using user input."""
    def __init__( self ):
        self.matrix = self.createMatrix()
        self.showMatrix()
        
    def createMatrix( self ):
        matrix = []
        for i in range( 2 ):
            row = []
            for j in range( 2 ):
                print 'Row', i + 1, 'column', j + 1, ':'
                entry = input()
                row.append( entry )
            matrix.append( row )

        return matrix

    def showMatrix( self ):
        print "The matrix is:\n"
        for row in self.matrix:
            print row
 
m1 = Matrix() #Create the first matrix object
m2 = Matrix() #Create the second matrix object

if you don't understand parts of this I'll be happy to explain :)

hope this helps :)

P.S. of course you can extend this so that it can create mxn sized matrices as well! :)

Thank you for the help. It works now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.