problem in using linalg solver in numpy

P

Pramod

Hi Friends


When run the below program in python i got error like this ,


Matrix
[[ 8 -6 2]
[-4 11 -7]
[ 4 -7 6]]
row vecotr X
[[ 28 -40 33]]
Traceback (most recent call last):
File "solve.py", line 16, in <module>
print A*B
File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
line 290, in __mul__
return N.dot(self, asmatrix(other))
ValueError: objects are not aligned




#!/usr/bin/python
N=3
from numpy import linalg
from numpy import matrix


fr=open('mat.txt','r')

A=matrix([[int(fr.readline()) for j in range(N)]for i in range(N)])
B=matrix([[int(fr.readline())for j in range(N)]])

print 'Matrix \n',A
print 'row vecotr X\n',B
#A=matrix([[3,4],[5,2]])
#B=matrix([[11],[9]])
print A*B

#y=linalg.solve(A,B)
#print 'Solution vectoris \n',y


The input file is
8
-6
2
-4
11
-7
4
-7
6
28
-40
33

please try to fix the error in the program


Thankx in advance
 
P

Peter Otten

Pramod said:
When run the below program in python i got error like this ,


Matrix
[[ 8 -6 2]
[-4 11 -7]
[ 4 -7 6]]
row vecotr X
[[ 28 -40 33]]
Traceback (most recent call last):
File "solve.py", line 16, in <module>
print A*B
File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
line 290, in __mul__
return N.dot(self, asmatrix(other))
ValueError: objects are not aligned
A=matrix([[int(fr.readline()) for j in range(N)]for i in range(N)])
B=matrix([[int(fr.readline())for j in range(N)]])
print A*B

Row or column; I can't remember which is which either. Try again with

B = matrix([[int(fr.readline())] for j in range(N)]

or

A*B.transpose()

Peter
 
M

MRAB

Pramod said:
Hi Friends


When run the below program in python i got error like this ,


Matrix
[[ 8 -6 2]
[-4 11 -7]
[ 4 -7 6]]
row vecotr X
[[ 28 -40 33]]
Traceback (most recent call last):
File "solve.py", line 16, in <module>
print A*B
File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
line 290, in __mul__
return N.dot(self, asmatrix(other))
ValueError: objects are not aligned
[snip]
If you're trying to multiply element-wise use the 'multiply' function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top