python矩阵教程_Python 实现简单的矩阵

Python 实现简单的矩阵

#!/usr/bin/python

# -*- coding: utf-8 -*-

'''

Created on 2015-1-7

@author: beyondzhou

@name: myarray.py

'''

# Implementation of the Matrix ADT using a 2D array

from myarray import Array2D

class Matrix:

# Creates a matrix of size numRows * numCols initialized to 0

def __init__(self, numRows, numCols):

self._theGrid = Array2D(numRows, numCols)

self._theGrid.clear(0)

# Returns the number of rows in the matrix

def numRows(self):

return self._theGrid.numRows()

# Returns the number of columns in the matrix

def numCols(self):

return self._theGrid.numCols()

# Returns the value of element (i, j): x[i, j]

def __getitem__(self, ndxTuple):

return self._theGrid[ndxTuple[0], ndxTuple[1]]

# Sets the value of element (i,j) to the value s: x[i, j] = s

def __setitem__(self, ndxTuple, scalar):

self._theGrid[ndxTuple[0], ndxTuple[1]] = scalar

# Scales the matrix by the given scalar

def scaleBy(self, scalar):

for r in range(self.numRows()):

for c in range(self.numCols()):

self[r,c] *= scalar

# Creates and returns a new matrix that is the transpose of this matrix

def transpose(self):

# Create the new matrix

newMatrix = Matrix(self.numCols(), self.numRows())

# Add the corresponding elements in the two matrices

for r in range(self.numRows()):

for c in range(self.numCols()):

newMatrix[c,r] = self[r,c]

return newMatrix

# Creates and returns a new matrix that results from matrix addition

def __add__(self, rhsMatrix):

assert rhsMatrix.numRows() == self.numRows() and \

rhsMatrix.numCols() == self.numCols(), \

"Matrix sizes not compatible for the add operation."

# Create the new matrix

newMatrix = Matrix(self.numRows(), self.numCols())

# Add the corresponding elements in the two matrices

for r in range(self.numRows()):

for c in range(self.numCols()):

newMatrix[r,c] = self[r,c] + rhsMatrix[r,c]

return newMatrix

# Creates and returns a new matrix that results from matrix sub

def __sub__(self, rhsMatrix):

assert rhsMatrix.numRows() == self.numRows() and \

rhsMatrix.numCols() == self.numCols(), \

"Matrix sizes not compatible for the add operation."

# Create the new matrix

newMatrix = Matrix(self.numRows(), self.numCols())

# Add the corresponding elements in the two matrices

for r in range(self.numRows()):

for c in range(self.numCols()):

newMatrix[r,c] = self[r,c] - rhsMatrix[r,c]

return newMatrix

# Creates and returns a new matrix resulting from matrix multiplcation

def __mul__(self, rhsMatrix):

assert rhsMatrix.numRows() == self.numCols(), \

"Matrix sizes not compatible for the multi operation."

# Create the new matrix

newMatrix = Matrix(self.numRows(), rhsMatrix.numCols())

# Mul the corresponding elements in the two matrices

for r in range(self.numRows()):

for c in range(rhsMatrix.numCols()):

mysum = 0.0

for k in range(self.numCols()):

mysum += self[r,k] * rhsMatrix[k,r]

newMatrix[r,c] = mysum

return newMatrix

#!/usr/bin/python

# -*- coding: utf-8 -*-

'''

Created on 2015-1-7

@author: beyondzhou

@name: test_matrix.py

'''

def test_matrix():

# Import

from mymatrix import Matrix

import random

# set default value for matrix

aMatrix = Matrix(2,3)

bMatrix = Matrix(2,3)

fMatrix = Matrix(3,2)

for i in range(aMatrix.numRows()):

for j in range(aMatrix.numCols()):

aMatrix[i,j] = random.random()

bMatrix[i,j] = random.random()

for i in range(fMatrix.numRows()):

for j in range(fMatrix.numCols()):

fMatrix[i,j] = random.random()

print 'The primary value of amatrix'

for i in range(aMatrix.numRows()):

for j in range(aMatrix.numCols()):

print '%s ' % aMatrix[i,j],

print '\r'

print '\nThe primary value of bmatrix'

for i in range(bMatrix.numRows()):

for j in range(bMatrix.numCols()):

print '%s ' % bMatrix[i,j],

print '\r'

print '\nThe primary value of fmatrix'

for i in range(fMatrix.numRows()):

for j in range(fMatrix.numCols()):

print '%s ' % fMatrix[i,j],

print '\r'

# add amatrix and bmatrix to cmatrix

cMatrix = aMatrix + bMatrix

print '\nThe value of cMatrix (aMatrix + bMatrix)'

for i in range(cMatrix.numRows()):

for j in range(cMatrix.numCols()):

print '%s ' % cMatrix[i,j],

print '\r'

# sub amatrix and bmatrix to dmatrix

dMatrix = aMatrix - bMatrix

print '\nThe value of dMatrix (aMatrix - bMatrix)'

for i in range(dMatrix.numRows()):

for j in range(dMatrix.numCols()):

print '%s ' % dMatrix[i,j],

print '\r'

# Mul amatrix and fMatrix to ematrix

eMatrix = aMatrix * fMatrix

print '\nThe value of eMatrix (aMatrix * fMatrix)'

for i in range(eMatrix.numRows()):

for j in range(eMatrix.numCols()):

print '%s ' % eMatrix[i,j],

print '\r'

# Scale the amatrix by 3

aMatrix.scaleBy(3)

print '\nThe scale value of amatrix'

for i in range(aMatrix.numRows()):

for j in range(aMatrix.numCols()):

print '%s ' % aMatrix[i,j],

print '\r'

# Transpose the amatrix

dMatrix = aMatrix.transpose()

print '\nThe transpose value of amatrix'

for i in range(dMatrix.numRows()):

for j in range(dMatrix.numCols()):

print '%s ' % dMatrix[i,j],

print '\r'

if __name__ == "__main__":

test_matrix()

Result:

The primary value of amatrix

0.886197406941 0.304295996721 0.293469382347

0.154702139448 0.511075267985 0.387057640727

The primary value of bmatrix

0.574674206609 0.364815615899 0.493367650314

0.438101377839 0.801271107474 0.0891226289712

The primary value of fmatrix

0.00716087704081 0.537519043084

0.451888654276 0.234306298527

0.572987747957 0.479059183861

The value of cMatrix (aMatrix + bMatrix)

1.46087161355 0.66911161262 0.78683703266

0.592803517287 1.31234637546 0.476180269699

The value of dMatrix (aMatrix - bMatrix)

0.311523200332 -0.0605196191784 -0.199898267967

-0.283399238391 -0.290195839489 0.297935011756

The value of eMatrix (aMatrix * fMatrix)

0.31200821961 0.31200821961

0.388327017743 0.388327017743

The scale value of amatrix

2.65859222082 0.912887990162 0.88040814704

0.464106418343 1.53322580395 1.16117292218

The transpose value of amatrix

2.65859222082 0.464106418343

0.912887990162 1.53322580395

0.88040814704 1.16117292218

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值