## NUMPY HELP

This is the place for queries that don't fit in any of the other categories.

### NUMPY HELP

2- Find an upper triangular matrix U (not diagonal) with U^2 = I. (First 2x2, then 3x3 size)
3- How many 4x4 permutation matrices are symmetric? List all such matrices.

Using the modules scipy.linealg and numpy, i need to answer these two questions.

No need direct answer, the method is also acceptable

thx
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: NUMPY HELP

What do you have so far?
setrofim

Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

### Re: NUMPY HELP

Code: Select all
`closer_I = array ( [[1, 0.001], [0.001, 1]] )def upperU(A):    if  np.dot(np.triu(A),np.triu(A) - np.identity(2) <= closer_I:      print A    else:        upperU(np.random.randn(2, 2))`

for 2nd question i tried something like this but didnt work, whole logic may be wrong
the raised error is :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in upperU
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: NUMPY HELP

for 2nd question i edit the code as follow

Code: Select all
`def upperU(A):   smpl = np.triu(A) - np.identity(2)   y = np.sum(smpl)   if y <= 0.0000001:      print A    else:       upperU(np.random.randn(2, 2))`

this gives a result but I am not sure about the correctness
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: NUMPY HELP

First, when doing linear algebra it is better to use matrices than arrays, as matrix was developed for such operations. This will also get you away from having to specify dot() since
Code: Select all
`all(A*A==dot(A,A)) = True # When A is matrixall(A*A==dot(A,A)) = False # When A is array`

Code: Select all
`if  np.dot(np.triu(A),np.triu(A) - np.identity(2) <= closer_I:`

is missing a closing parenthesis, which was probably just a typo on the forum, but make sure it is correct in your code.

Third (possibly related to my second comment), the error "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" is probably due to your line:
Code: Select all
`if  np.dot(np.triu(A),np.triu(A) - np.identity(2) <= closer_I:`

Here, you are asking if the dot operation is less than another array, but numpy is confused because there are multiple elements in each array you are comparing. If you want to see if ALL elements are less than closer_I, you should do something like this:
Code: Select all
`if  np.all( np.dot( np.triu(A),np.triu(A) - np.identity(2) ) <= closer_I ):`

This will return True only if all dot-product values are less than closer_I.

Fourth, I am guessing that you gave your arrays shape [2,2] for example purposes, but I want to make sure. If you really do want A to be shape [2,2] then all( triu(A)==A ) = True. You probably already know that, but better to be sure and save an exercise in futility.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm