1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
11
Rep Power
0

#### 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
2. Does U^2 mean U squared or does it mean U xor 2 ?
3. The sequence is cataloged here.[/edit]

Second problem (#3) is trivial in j, www.jsoftware.com. It's probably easy in python, also.
Code:
```   NB. All the 4x4 permutation matrices, tidied up for you to see
_6[\;/PERMUTATION_MATRICES =: (i.@:! A. =@:i.)4
┌───────┬───────┬───────┬───────┬───────┬───────┐
│1 0 0 0│1 0 0 0│1 0 0 0│1 0 0 0│1 0 0 0│1 0 0 0│
│0 1 0 0│0 1 0 0│0 0 1 0│0 0 1 0│0 0 0 1│0 0 0 1│
│0 0 1 0│0 0 0 1│0 1 0 0│0 0 0 1│0 1 0 0│0 0 1 0│
│0 0 0 1│0 0 1 0│0 0 0 1│0 1 0 0│0 0 1 0│0 1 0 0│
├───────┼───────┼───────┼───────┼───────┼───────┤
│0 1 0 0│0 1 0 0│0 1 0 0│0 1 0 0│0 1 0 0│0 1 0 0│
│1 0 0 0│1 0 0 0│0 0 1 0│0 0 1 0│0 0 0 1│0 0 0 1│
│0 0 1 0│0 0 0 1│1 0 0 0│0 0 0 1│1 0 0 0│0 0 1 0│
│0 0 0 1│0 0 1 0│0 0 0 1│1 0 0 0│0 0 1 0│1 0 0 0│
├───────┼───────┼───────┼───────┼───────┼───────┤
│0 0 1 0│0 0 1 0│0 0 1 0│0 0 1 0│0 0 1 0│0 0 1 0│
│1 0 0 0│1 0 0 0│0 1 0 0│0 1 0 0│0 0 0 1│0 0 0 1│
│0 1 0 0│0 0 0 1│1 0 0 0│0 0 0 1│1 0 0 0│0 1 0 0│
│0 0 0 1│0 1 0 0│0 0 0 1│1 0 0 0│0 1 0 0│1 0 0 0│
├───────┼───────┼───────┼───────┼───────┼───────┤
│0 0 0 1│0 0 0 1│0 0 0 1│0 0 0 1│0 0 0 1│0 0 0 1│
│1 0 0 0│1 0 0 0│0 1 0 0│0 1 0 0│0 0 1 0│0 0 1 0│
│0 1 0 0│0 0 1 0│1 0 0 0│0 0 1 0│1 0 0 0│0 1 0 0│
│0 0 1 0│0 1 0 0│0 0 1 0│1 0 0 0│0 1 0 0│1 0 0 0│
└───────┴───────┴───────┴───────┴───────┴───────┘
NB. Symmetric matrices match the transpose
Filter
(#~`)(`:6)

_5[\;/SYMMETRIC_PERMUTATION_MATRICES =: (-: |:)"2 Filter PERMUTATION_MATRICES
┌───────┬───────┬───────┬───────┬───────┐
│1 0 0 0│1 0 0 0│1 0 0 0│1 0 0 0│0 1 0 0│
│0 1 0 0│0 1 0 0│0 0 1 0│0 0 0 1│1 0 0 0│
│0 0 1 0│0 0 0 1│0 1 0 0│0 0 1 0│0 0 1 0│
│0 0 0 1│0 0 1 0│0 0 0 1│0 1 0 0│0 0 0 1│
├───────┼───────┼───────┼───────┼───────┤
│0 1 0 0│0 0 1 0│0 0 1 0│0 0 0 1│0 0 0 1│
│1 0 0 0│0 1 0 0│0 0 0 1│0 1 0 0│0 0 1 0│
│0 0 0 1│1 0 0 0│1 0 0 0│0 0 1 0│0 1 0 0│
│0 0 1 0│0 0 0 1│0 1 0 0│1 0 0 0│1 0 0 0│
└───────┴───────┴───────┴───────┴───────┘

# SYMMETRIC_PERMUTATION_MATRICES  NB. tallied
10```
(untested)
The algorithm. First, find all permutation matrices. You can get these from itertools.permutations(numpy.identity(4))

In j this is all factorial 4 anagrams (A.) of the identity matrix.

Adverb Filter copies data where the verb evaluates to 1. And, of course the test is "match transpose" computed at rank 2 ("2). You can use numpy.compress to same purpose.
Last edited by b49P23TIvg; April 18th, 2013 at 11:41 AM.