hello Seniors,
i am new to python development.i have started my internship in that language.

can any one provide me the code that show me how to add subtract and multiply matrices in python.???

what will be the code if i want to take input from the user to enter the value of the matrix and on that base it will add,subtract or multiply the two matrices??

any one who knows about it please help me...
i will really appreciate that.
Thank You
Punter

Well... here's what I would do:
1) Ask the user how many rows and columns
ex: 2x2
2) Ask the user to input each number seperately
ex: 2, 3, -1, -2
3) Ask the user which letter to save the matrix to
ex: A
4) Save the matrix as:
A = ((2, 3), (-1, -2))
5) Loop that for needed matrices, then ask user which operation
ex: B = ((1, 2), (-1, -2))
6) for addition:

C_0_0 = A[0][0] + B[0][0]C_0_1 = A[0][1] + B[0][1]C_1_0 = A[1][0] + B[1][0]C_1_1 = A[1][1] + B[1][1]C_0 = C_0_0, C_0_1C_1 = C_1_0, C_1_1C = C_0, C_1C_0_0 = A[0][0] + B[0][0]
C_0_1 = A[0][1] + B[0][1]
C_1_0 = A[1][0] + B[1][0]
C_1_1 = A[1][1] + B[1][1]
C_0 = C_0_0, C_0_1
C_1 = C_1_0, C_1_1
C = C_0, C_1

Pretty much the same for subtracting. Multiplying two matrices together would be a bit tougher, but if you know how to do it by hand, you can figure it out in Python. Just find a way to loop the RxC when storing the matrix, or you can type it all out by hand
Say:

if R == 2 and C == 2:    A = ((0, 0), (0, 0))if R == 1 and C == 2:    A = ((0, 0))if R == 2 and C == 2:
    A = ((0, 0), (0, 0))
if R == 1 and C == 2:
    A = ((0, 0))

But I'm sure you can figure it out

thank you for your help i am surely gonna try it out and figure out the results thanks again

The module numpy is your friend here:

# create matrix with arange().reshape()
# perform add and multiply matrix operations
# module numpy (numeric python extensions, high speed)
# free from: http://sourceforge.net/projects/numpy

import numpy as np

# create 2 matrices using
# arange([start,] stop[, step,], dtype=None) and
# reshape((row, col)) use (row, col) tuple
# list created by arange() has to fit 3x3 matrix
mx1 = np.arange(11, 20).reshape((3, 3))
mx2 = np.arange(31, 40).reshape((3, 3))

# add the 2 matrices
mx3 = np.add(mx1, mx2)
# multiply the 2 matrices
mx4 = np.multiply(mx1, mx2)

# show the result
print('mx1 =')
print(mx1)
print('-'*15)
print('mx2 =')
print(mx2)
print('-'*15)
print('mx1 + mx2 =')
print(mx3)
print('-'*15)
print('mx1 * mx2 =')
print(mx4)

"""my result -->
mx1 =
[[11 12 13]
 [14 15 16]
 [17 18 19]]
---------------
mx2 =
[[31 32 33]
 [34 35 36]
 [37 38 39]]
---------------
mx1 + mx2 =
[[42 44 46]
 [48 50 52]
 [54 56 58]]
---------------
mx1 * mx2 =
[[341 384 429]
 [476 525 576]
 [629 684 741]]
"""

:D I never knew about that module! I only knew about the math module... anyways, I like my method better, more work and thinking involved xD
Never mind the fact that I love math.

:D I never knew about that module! I only knew about the math module... anyways, I like my method better, more work and thinking involved xD
Never mind the fact that I love math.

It can be very interesting to roll your own functions, but an important part of learning Python is also to get to know the many modules it has to offer. These modules are fully tested and optimized, and help you to develop a program faster.

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.