472,958 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

matrix class

Below is a module (matrix.py) with a class to implement some basic
matrix operations on a 2D list. Some things puzzle me about the best
way to do this (please don't refer to scipy, numpy and numeric because
this is a personal programming exercise for me in creating an
operational class in pure python for some *basic* matrix operations).

1. Please take a look at the __init__ function and comment on the
initialization of the list data with respect to unique memory
allocation.

2. In the operator overloading of __add__, __sub__, etc., the
statement isinstance(q, Matrix) raises exceptions every time. This
statement works fine outside of the class definition, but not during
the operator evaluation. What is going here?
## BEGIN MODULE FILE

class Matrix:
"""
Create and manipulate a matrix object

Matrix(data, dim)

data = list of lists (currently only 2D)
dim=(row,col) tuple of int

For example,

#data = [[0.0] * c for i in xrange(r)]
data = [[0.0,0.1],[1.0,1.1],[2.0,2.1]]
rowN =len(data)
colN =len(data[0])
m = Matrix(data)
m = Matrix(data,dim=(rowN, colN))

d1 = [[0.0, 0.1], [1.0, 1.1], [2.0, 2.1]] # 3x2 matrix
d2 = [[0.0, 0.1, 0.2], [1.0, 1.1, 1.2]] # 2x3 matrix
m1 = Matrix(d1)
m2 = Matrix(d2)
#m3 = m1 + m2 # dimension error
m3 = m1 + m2.transpose()
m3 = m1 - m2.transpose()
m3 = m1 * m2 # 3x3
m3 = m2 * m1 # 2x2

m1[2,:]
m1[:,2]
"""

def __init__(self, data=None, dim=None):
"""
create a matrix instance.

m = Matrix([data [, dim]])

<datais a 2D matrix comprised of a nested list of floats
<dimis a tuple of int values for the row and column size
(r,c)

eg:
data = [[0.0,0.1],[1.0,1.1],[2.0,2.1]]
dim = (3,2) # or (len(data),len(data[0]))
"""

if data != None:
self.data = data
r = len(data)
c = len(data[0])
# Are all the rows the same length?
rowLenCheck = sum([len(data[i]) != c for i in range(r)])
if rowLenCheck 0:
raise ValueError
else:
self.dim = (r,c)

if dim != None:
if (dim[0] == r) and (dim[1] == c):
self.dim = (r,c)
else:
# over-ride the dim input, do not reshape data!
# print a warning?
self.dim = (r,c)
else:
if dim != None:
if len(dim) == 2:
self.dim = tuple(dim)
r = dim[0]
c = dim[1]
else:
# maybe a new exception type?
arg = ("len(dim) != 2: ", dim)
raise ValueError, arg

# BEGIN ALT ----------------------------------------
# Does this give unique memory for each element?
# self.data = [[0.0] * c for i in xrange(r)]

# It seems that the initialization does not generate
# unique memory elements because all list elements
# refer to the same number object (0.0), but
# modification of any element creates a unique value,
# without changing any other values, eg:

##>>x = [[0.0] * 3 for i in xrange(2)]
##>>id(x)
# 3079625068L
# >>id(x[0][0])
# 136477300
# >>id(x[0][1])
# 136477300
# >>id(x[1][1])
# 136477300
# >>x[0][0] = 1.0
# >>x
# [[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
# >>>
# END ALT ----------------------------------------

# create a zero row vector, with unique memory for
each element
self.data = [[x * 0.0 for x in range(c)]]
for i in range(1,r):
self.data.append([x * 0.0 for x in range(c)])
else:
self.data = []
self.dim = (0,0)
#print self.__doc__

def __getitem__(self, i):
"""
matrix[r,c] returns values from matrix.data, eg:
>>data = [[0.0,0.1],[1.0,1.1],[2.0,2.1]]
m = Matrix(data)
m[2,:]
[2.0, 2.1000000000000001]
"""
r = i[0]
c = i[1]
#print "index: (%s, %s)" % (r,c)
#print "value: ", self.data[r][c]
return self.data[r][c]

def reshape(self, newdim=None):
'reshape a matrix object: matrix.reshape(newdim)'
print "something to implement later"
pass

def transpose(self):
'transpose a matrix: m2 = m1.transpose()'
m = Matrix(dim=(self.dim[1],self.dim[0]))
for r in range(self.dim[0]):
for c in range(self.dim[1]):
m.data[c][r] = self.data[r][c]
return m

def __add__(self, q):
'matrix addition: m3 = m1 + m2'
# if isinstance(q, Matrix):
# arg = ("q is not a matrix instance", q)
# raise TypeError, arg
if self.dim != q.dim:
arg = ("p.dim != q.dim", self.dim, q.dim)
raise IndexError, arg
else:
# do the addition
m = Matrix(dim=self.dim)
for r in range(self.dim[0]): # rows of p and q
m.data[r] = map(lambda x, y: x + y, self.data[r],
q.data[r])
return m

def __sub__(self, q):
'matrix subtraction: matrix - matrix'
# if isinstance(q, Matrix):
# arg = ("q is not a matrix instance", q)
# raise TypeError, arg
if self.dim != q.dim:
arg = ("p.dim != q.dim", self.dim, q.dim)
raise IndexError, arg
else:
# do the subtraction
m = Matrix(dim=self.dim)
for r in range(self.dim[0]): # rows of p and q
m.data[r] = map(lambda x, y: x - y, self.data[r],
q.data[r])
return m

def __mul__(self, q):
"""
multiply two matrices:
m = p * q # p and q are matrix objects and p.dim[1] ==
q.dim[0]
"""
# if isinstance(q, Matrix):
# arg = ("q is not a matrix instance", q)
# raise TypeError, arg
if self.dim[1] != q.dim[0]:
arg = ("p.dim[1] != q.dim[0]", self.dim[1], q.dim[0])
raise IndexError, arg
else:
# do the multiplication
m = Matrix(dim=(self.dim[0], q.dim[1]))
for r in range(self.dim[0]): # rows of p
for c in range(q.dim[1]): # cols of q
# get the dot product of p(r,:) with q(:,c)
pVec = self.data[r]
qVec = [q.data[a][c] for a in xrange(q.dim[0])]
m.data[r][c] = sum(map(lambda x, y: x * y, pVec,
qVec))
return m

# let's not try to divide for now (leave the inverse stuff to c/c+
+)

def __len__(self):
return self.dim[0] * self.dim[1]

def __str__(self):
# print the matrix data
s = ""
for r in range(self.dim[0]):
for c in range(self.dim[1]):
s += "%f " % (self.data[r][c])
s += "\n"
return s

def printFormat(self, format):
"""
print the matrix data nicely formatted, eg:
matrix.printFormat("%8.4f")
"""
for r in range(self.dim[0]):
for c in range(self.dim[1]):
print format % (self.data[r][c]),
print

def __repr__(self):
# return something that will recreate the object
return "Matrix(%s, %s)" % (self.data, self.dim)


#
--------------------------------------------------------------------------------
# Explore the functionality - should be unit testing

# >>m = Matrix(dim=(2,2))
# >>type(m)
# <class '__main__.matrix'>
# >>m.dim
#(2, 2)
# >>m.len()
# 4
# >>m.data
# [[0.0, 0.0], [0.0, 0.0]]
# >>m.dim
# (2, 2)
# >>id(m.data[0][0])
# 136477668
# >>id(m.data[0][1])
# 136477380
# >>id(m.data[1][0])
# 136477668
# >>id(m.data[1][1])
# 136477380
# >>m.data[0][0] = 1.0
# >>m.data[1][0] = 2.0
# >>m.data
# [[1.0, 0.0], [2.0, 0.0]]

testing = 1
if testing:
d1 = [[0.0, 0.1], [1.0, 1.1], [2.0, 2.1]] # 3x2 matrix
d2 = [[0.0, 0.1, 0.2], [1.0, 1.1, 1.2]] # 2x3 matrix
m1 = Matrix(d1)
m2 = Matrix(d2)
#m3 = m1 + m2 # "dimension" error
m3 = m1 + m2.transpose()
m3 = m1 - m2.transpose()
m3 = m1 * m2 # 3x3
m3 = m2 * m1 # 2x2

## END MODULE FILE

Jun 13 '07 #1
2 8491
On Jun 12, 7:31 pm, DarrenWeber <Darren.We...@radiology.ucsf.edu>
wrote:
Below is a module (matrix.py) with a class to implement some basic
matrix operations on a 2D list. Some things puzzle me about the best
way to do this (please don't refer to scipy, numpy and numeric because
this is a personal programming exercise for me in creating an
operational class in pure python for some *basic* matrix operations).

1. Please take a look at the __init__ function and comment on the
initialization of the list data with respect to unique memory
allocation.

2. In the operator overloading of __add__, __sub__, etc., the
statement isinstance(q, Matrix) raises exceptions every time. This
statement works fine outside of the class definition, but not during
the operator evaluation. What is going here?
....
>
def __add__(self, q):
'matrix addition: m3 = m1 + m2'
# if isinstance(q, Matrix):
# arg = ("q is not a matrix instance", q)
# raise TypeError, arg
Wouldn't it make more sense to raise an exception if q is NOT an
instance of Matrix?

Jun 13 '07 #2
On Jun 12, 6:20 pm, Dan Bishop <danb...@yahoo.comwrote:
On Jun 12, 7:31 pm, DarrenWeber <Darren.We...@radiology.ucsf.edu>
wrote:
Below is a module (matrix.py) with a class to implement some basic
matrix operations on a 2D list. Some things puzzle me about the best
way to do this (please don't refer to scipy, numpy and numeric because
this is a personal programming exercise for me in creating an
operational class in pure python for some *basic* matrix operations).
1. Please take a look at the __init__ function and comment on the
initialization of the list data with respect to unique memory
allocation.
2. In the operator overloading of __add__, __sub__, etc., the
statement isinstance(q, Matrix) raises exceptions every time. This
statement works fine outside of the class definition, but not during
the operator evaluation. What is going here?
...
def __add__(self, q):
'matrix addition: m3 = m1 + m2'
# if isinstance(q, Matrix):
# arg = ("q is not a matrix instance", q)
# raise TypeError, arg

Wouldn't it make more sense to raise an exception if q is NOT an
instance of Matrix?

Duh, yea! I don't recommend programming and sleep deprivation ;-)

Jun 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
3
by: Huibuh | last post by:
In one of my header-files I have a class named "matrix" with the function to construct a matrix. It works properly but at the time of destruction of the class the program stops. What have I done...
13
by: Charulatha Kalluri | last post by:
Hi, I'm implementing a Matrix class, as part of a project. This is the interface I've designed: class Matrix( )
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
7
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.