Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Symmetric Matrices in Numpy

I am trying to generate symmetric matrices in numpy. Specifically, these matrices are to have random places entries, and in each entry the contents can be random. Along the main diagonal we are not concerned with what enties are in there, so I have randomized those as well.

The approach I have taken is to first generate a nxn all zero matrix and simply loop over the indices of the matrices. However, given considering looping is relatively expensive in python, I'm wondering if I can acheive the same thing without using python's for loops.

Is there some things built into numpy that allow me to acheive my goal more efficiently?

Here is my current code:

import numpy as np import random  def empty(x, y):     return x*0  b = np.fromfunction(empty, (n, n), dtype = int)  for i in range(0, n):     for j in range(0, n):         if i == j:             b[i][j] = random.randrange(-2000, 2000)         else:             switch = random.random()             random.seed()             if switch > random.random():                 a = random.randrange(-2000, 2000)                 b[i][j] = a                 b[j][i] = a             else:                 b[i][j] = 0                 b[j][i] = 0 
like image 781
Ryan Avatar asked May 29 '12 21:05

Ryan


People also ask

How do you make a matrix symmetric in Python?

A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row. Recommended: Please try your approach on {IDE} first, before moving on to the solution.

What is symmetric matrix in Java?

A symmetric matrix is a square matrix in which the transpose of the square matrix is the same as the original square matrix. A square matrix is a matrix in which the number of columns is the same as the number of rows.

How do you know if a matrix is symmetric in Python?

numpy. transpose(a, axes=None) Parameters: array = The array to be passed to the function. axis = The default, axis=None Returns: Returns the transpose of array. Pass the two arrays to array_equal() method, if it returns true then print that the array is Symmetric else print that the array is not Symmetric.


1 Answers

You could just do something like:

import numpy as np  N = 100 b = np.random.random_integers(-2000,2000,size=(N,N)) b_symm = (b + b.T)/2 

Where you can choose from whatever distribution you want in the np.random or equivalent scipy module.

Update: If you are trying to build graph-like structures, definitely check out the networkx package:

http://networkx.lanl.gov

which has a number of built-in routines to build graphs:

http://networkx.lanl.gov/reference/generators.html

Also if you want to add some number of randomly placed zeros, you can always generate a random set of indices and replace the values with zero.

like image 173
JoshAdel Avatar answered Sep 26 '22 05:09

JoshAdel