[SciPy-user] Couple of Matrix() questions

Jeffrey B. Layton laytonjb at bellsouth.net
Sun Aug 31 10:27:52 EDT 2003


Pearu,

   Thanks for the help! I've got a follow-up below.

>>   My second question is how to concatenate matrices
>>create with mat(). I tried the following (complete with
>>error messages),
>>
>> >>> A=mat('[0,1];[2,3]')
>> >>> B=mat('[4,5];[6,7]')
>> >>> C=mat('[8,9];[10,11]')
>> >>> D=mat('[12,13];[14,15]')
>>    
>>
>>I know this is a holdover from Matlab, but I would like
>>to get is the following,
>>
>>E=[0, 1, 4 ,5;
>>    2, 3, 6, 7;
>>    8, 9, 12, 13;
>>    10, 11, 14, 15]
>>
>>How do I do this in scipy?
>>    
>>
>
>I hope there would be a better way but Numeric.concatenate does the job:
>
>  
>
>>>>E=mat(concatenate([concatenate([A,B],axis=1),concatenate([C,D],axis=1)]))
>>>>E
>>>>        
>>>>
>Matrix([[ 0,  1,  4,  5],
>       [ 2,  3,  6,  7],
>       [ 8,  9, 12, 13],
>       [10, 11, 14, 15]])
>

This is terrible! There should definitely be a better way to do this.
People doing control analysis or anything requiring large linear
system built up from smaller systems (e.g. FEM, BEM, etc.) would
like to do this. Is the function [ ] an overloaded function from Scipy
that it can process the matrix class? It would be very nice to add the
concatenation capability to it.

I guess another way would be to create E of the appropriate size and
then copy A, B, C, and D into the appropriate locations. Is there a
way to create E using mat() that initializes the matrix to zero? While
I'm on the topic, is there a good place for mat() documentation,
specifically a tutorial?

I did try the following:

 >>> Adum=mat('[0,0;0,0]')
 >>> print Adum
Matrix([[0, 0],
       [0, 0]])
 >>> E=mat('[Adum,Adum];[Adum,Adum]')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.2/site-packages/Numeric/Matrix.py", line 69, in 
__init__
    data = _convert_from_string(data)
  File "/usr/lib/python2.2/site-packages/Numeric/Matrix.py", line 36, in 
_convert_from_string
    newrow.extend(map(_eval,temp))
  File "/usr/lib/python2.2/site-packages/Numeric/Matrix.py", line 24, in 
_eval
    return eval(astr.translate(_table,_todelete))
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

I think this should work. Amy I way off base here?

TIA!

Jeff





More information about the SciPy-User mailing list