[SciPy-user] Newbie type questions
Travis E. Oliphant
oliphant at ee.byu.edu
Tue Aug 5 19:42:43 EDT 2003
Jeffrey B. Layton wrote:
> Hello,
>
> I've played with Scipy off and on for a few months, but now
> I need to get serious. I am co-authoring an engineering book
> and will have a large number of examples in it. We are already
> using MATLAB (and Octave), but I have convinced my
> co-authors and perhaps the publishers to put Python/Scipy in
> there as well. This text is a graduate level text and the models
> in the book can be quite large and time-consuming.
> Towards this, we have an appendix where we explain the basics
> of MATLAB and Octave for those who need to brush up or who
> need a quick intro (enough to run the examples). I'm writing the
> companion appendix for Python/Scipy. So, I'm working through
> the examples. This leads me to my questions
> If I declare a complex matrix A,
>
> A=array([[complex(1,1),complex(2,2)],[complex(3,3),complex(4,4)]])
>
> and I want to take the transpose, which I'm assuming to be the
> complex conjugate, I need to type
>
> AT=transpose(conjugate(A))
>
> to get the actual complex conjugate. Is there an easier way to do this?
> A somewhat related question, if I declare a vector X,
>
There is also a Matrix class which can be useful for matrix algorithms.
For your example (assuming you have scipy):
A = mat('[1+1j, 2+2j; 3+3j, 4+4j]') # notice the quotes
AH = A.H # the conjugate transpose
AT = A.T # just the tranpose
AI = A.I # the inverse
> X=array([1,2,3])
>
> and then try to take the transpose of it,
>
> Y=transpose(X)
>
> then X=Y. I sort of understand this since the shape of X
> is (3, ). Shouldn't the shape of X be (3,1)? (I'm sorry this is
> such a newbie question).
This is a typical mistake people with only Matlab experience make. I
had this question years ago too. SciPy handles multi-dimensional arrays
more naturally than Matlab so that there is a real difference between
arrays and Matrices (thus the special subclass of arrays to handle just
matrices). Most commands that you understand to work with matrices
(2-d arrays) also have precise defintions for working with arrays.
In this example X is a 1-dimensional array. Therefore it's transpose
does nothing (there are not two dimensions to interchange).
Notice that tranpose(X) will work if X is 4-dimensional. It's default
behavior is to reverse the dimensions so that if X has a shape of
(3,4,5,6) then tranpose(X) has a shape of (6,5,4,3). You can change
this behavior by passing a second argument to transpose giving the
permutation of the axes that you would like. Thus
transpose(X,(2,3,0,1)) would have shape (5,6,3,4).
Good luck,
Feel free to ask more questions and if somebody has a moment they will
be happy to respond.
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
More information about the SciPy-User
mailing list