Numerical Python Question

Janko Hauser hauser at ifm.uni-kiel.de
Tue Dec 4 07:47:56 EST 2001


Uwe Schmitt <uwe at rocksport.de> writes:

> Hi,
> 
> I tried the following code:
> 
> 
>     from Numeric import *
>     from Matrix import *
> 
>     n=5
> 
>     hilb=zeros((n,n),Float)
> 
>     for i in range(n):
> 	for j in range(n):
> 	    val= 1.0/(i+j+1)
> 	    hilb[i,j]=val
> 
>     x=ones((n,1),Float)
> 
>     b=hilb*x 
> 
>     print "x= ", x
> 
>     print "b= ", b
> 
> 
> As hilb is a n x n matrix and x is n x 1 the result b should be n x 1.
> But the program outputs a n x n matrix !!!
> 
hilb is a NxN array not a matrix. You need to wrap it into a matrix,
so that the multiplication operator is overloaded.
mHilb=Matrix(hilb)
b=mHilb*x
print b.shape

HTH
__Janko


-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser at ifm.uni-kiel.de
  24105 Kiel, Germany



More information about the Python-list mailing list