How to construct matrix from vectors?

2015-06-20 Thread Nasser M. Abbasi

I just started to learn some python today for first time,
so be easy on me.

I am having some trouble figuring how do the problem shown in this link

http://12000.org/my_notes/mma_matlab_control/KERNEL/KEse44.htm

Given 4 column vectors, v1,v2,v3,v4, each is 3 rows.

I want to use these to construct matrix mat, which is

  [[v1,v2],
   [v3,v4]]

So the resulting matrix is as shown in the link.  i.e.
it will be 6 rows and 2 columns.

This is what I tried:

import numpy as np
v1=np.array([1,2,3]);
v2=np.array([4,5,6]);
v3=np.array([7,8,9]);
v4=np.array([10,11,12]);

And now I get stuck, I tried

m=np.array([[v1,v2],[v3,v4]])  #no good

Also

m=np.array([v1,v2,v3,v4])
m.shape
   Out[153]: (4L, 3L)
m.T

array([[ 1,  4,  7, 10],
   [ 2,  5,  8, 11],
   [ 3,  6,  9, 12]])

Not what I want.

I need to get the shape as in the above link, 6 rows by 2 columns,
where each column vector is stacked as shown.  I also tried

v1=np.array([1,2,3]); v1.shape=3,1
v2=np.array([4,5,6]); v2.shape=3,1
v3=np.array([7,8,9]); v3.shape=3,1
v4=np.array([10,11,12]); v4.shape=3,1
mat=np.array([[v1,v2],[v3,v4]])

What is the correct way to do this in Python?

thanks,
--Nasser
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to construct matrix from vectors?

2015-06-20 Thread Nasser M. Abbasi

On 6/20/2015 9:20 PM, MRAB wrote:


Here's one way, one step at a time:


r1 = np.concatenate([v1, v2])
r1

array([1, 2, 3, 4, 5, 6])

r2 = np.concatenate([v3, v4])
r2

array([ 7,  8,  9, 10, 11, 12])

m = np.array([r1, r2])
m

array([[ 1,  2,  3,  4,  5,  6],
 [ 7,  8,  9, 10, 11, 12]])

m.transpose()

array([[ 1,  7],
 [ 2,  8],
 [ 3,  9],
 [ 4, 10],
 [ 5, 11],
 [ 6, 12]])






But your output is wrong.

I did manage to find a way:

-
r1 =np.hstack([(v1,v2)]).T
r2 =np.hstack([(v3,v4)]).T
mat = np.vstack((r1,r2))
-

Out[211]:
array([[ 1,  4],
   [ 2,  5],
   [ 3,  6],
   [ 7, 10],
   [ 8, 11],
   [ 9, 12]])

But it is not as intuitive as with Matlab, where one can just write

---
  v1=[1,2,3]'; v2=[4,5,6]';
  v3=[7,8,9]'; v4=[10,11,12]';
  m=[v1 v2;v3 v4]
---

--Nasser



--
https://mail.python.org/mailman/listinfo/python-list


Re: How to construct matrix from vectors?

2015-06-20 Thread Nasser M. Abbasi

On 6/20/2015 10:47 PM, Nasser M. Abbasi wrote:


I did manage to find a way:

-
r1 =np.hstack([(v1,v2)]).T
r2 =np.hstack([(v3,v4)]).T
mat = np.vstack((r1,r2))
-

Out[211]:
array([[ 1,  4],
 [ 2,  5],
 [ 3,  6],
 [ 7, 10],
 [ 8, 11],
 [ 9, 12]])

But it is not as intuitive as with Matlab, where one can just write

---
v1=[1,2,3]'; v2=[4,5,6]';
v3=[7,8,9]'; v4=[10,11,12]';
m=[v1 v2;v3 v4]
---


Here is a way a little closer to Matlab's method: First
make all the vectors column vectors

v1=np.array([(1,2,3)]).T
v2=np.array([(4,5,6)]).T
v3=np.array([(7,8,9)]).T
v4=np.array([(10,11,12)]).T

mat =np.hstack(( np.vstack((v1,v3)), np.vstack((v2,v4))) )

Out[236]:
array([[ 1,  4],
   [ 2,  5],
   [ 3,  6],
   [ 7, 10],
   [ 8, 11],
   [ 9, 12]])

There are way too many '(([[]]))' things  in Python :)


--
https://mail.python.org/mailman/listinfo/python-list


Re: numpy (matrix solver) - python vs. matlab

2012-04-29 Thread Nasser M. Abbasi

On 04/29/2012 05:17 PM, someone wrote:


I would also kindly ask about how to avoid this problem in
the future, I mean, this maybe means that I have to check the condition
number at all times before doing anything at all ? How to do that?



I hope you'll check the condition number all the time.

You could be designing a building where people will live in it.

If do not check the condition number, you'll end up with a building that 
will fall down when a small wind hits it and many people will die all 
because you did not bother to check the condition number when you solved 
the equations you used in your design.


Also, as was said, do not use INV(A) directly to solve equations.

--Nasser
--
http://mail.python.org/mailman/listinfo/python-list


Re: numpy (matrix solver) - python vs. matlab

2012-04-29 Thread Nasser M. Abbasi

On 04/29/2012 07:59 PM, someone wrote:



 Also, as was said, do not use INV(A) directly to solve equations.


In Matlab I used x=A\b.



good.


I used inv(A) in python. Should I use some kind of pseudo-inverse or
what do you suggest?



I do not use python much myself, but a quick google showed that pyhton 
scipy has API for linalg, so use, which is from the documentation, the 
following code example


  X = scipy.linalg.solve(A, B)

But you still need to check the cond(). If it is too large, not good. 
How large and all that, depends on the problem itself. But the rule of 
thumb, the lower the better. Less than 100 can be good in general, but I 
really can't give you a fixed number to use, as I am not an expert in 
this subjects, others who know more about it might have better 
recommendations.


--Nasser



--
http://mail.python.org/mailman/listinfo/python-list


Re: numpy (matrix solver) - python vs. matlab

2012-04-29 Thread Nasser M. Abbasi

On 04/29/2012 07:17 PM, someone wrote:


Ok. When do you define it to be singular, btw?



There are things you can see right away about a matrix A being singular 
without doing any computation. By just looking at it.


For example, If you see a column (or row) being a linear combination of 
other column(s) (or row(s)) then this is a no no.


In your case you have

 1 2 3
 111213
 212223

You can see right away that if you multiply the second row by 2, and 
subtract from that one times the first row, then you obtain the third row.


Hence the third row is a linear combination of the first row and the 
second row. no good.


When you get a row (or a column) being a linear combination of others 
rows (or columns), then this means the matrix is singular.


--Nasser
--
http://mail.python.org/mailman/listinfo/python-list