def randMatrix(r, c=None, min=0, max=99, seed=None, symmetric=False, percent=100): """Create random matrix with dimensions ``r`` x ``c``. If ``c`` is omitted the matrix will be square. If ``symmetric`` is True the matrix must be square. If ``percent`` is less than 100 then only approximately the given percentage of elements will be non-zero. Examples ======== >>> from sympy.matrices import randMatrix >>> randMatrix(3) # doctest:+SKIP [25, 45, 27] [44, 54, 9] [23, 96, 46] >>> randMatrix(3, 2) # doctest:+SKIP [87, 29] [23, 37] [90, 26] >>> randMatrix(3, 3, 0, 2) # doctest:+SKIP [0, 2, 0] [2, 0, 1] [0, 0, 1] >>> randMatrix(3, symmetric=True) # doctest:+SKIP [85, 26, 29] [26, 71, 43] [29, 43, 57] >>> A = randMatrix(3, seed=1) >>> B = randMatrix(3, seed=2) >>> A == B # doctest:+SKIP False >>> A == randMatrix(3, seed=1) True >>> randMatrix(3, symmetric=True, percent=50) # doctest:+SKIP [0, 68, 43] [0, 68, 0] [0, 91, 34] """ if c is None: c = r if seed is None: prng = random.Random() # use system time else: prng = random.Random(seed) if symmetric and r != c: raise ValueError( 'For symmetric matrices, r must equal c, but %i != %i' % (r, c)) if not symmetric: m = Matrix._new(r, c, lambda i, j: prng.randint(min, max)) else: m = zeros(r) for i in range(r): for j in range(i, r): m[i, j] = prng.randint(min, max) for i in range(r): for j in range(i): m[i, j] = m[j, i] if percent == 100: return m else: z = int(r*c*percent // 100) m._mat[:z] = [S.Zero]*z random.shuffle(m._mat) return m
def randMatrix(r, c=None, min=0, max=99, seed=None, symmetric=False, percent=100): """Create random matrix with dimensions ``r`` x ``c``. If ``c`` is omitted the matrix will be square. If ``symmetric`` is True the matrix must be square. If ``percent`` is less than 100 then only approximately the given percentage of elements will be non-zero. Examples ======== >>> from sympy.matrices import randMatrix >>> randMatrix(3) # doctest:+SKIP [25, 45, 27] [44, 54, 9] [23, 96, 46] >>> randMatrix(3, 2) # doctest:+SKIP [87, 29] [23, 37] [90, 26] >>> randMatrix(3, 3, 0, 2) # doctest:+SKIP [0, 2, 0] [2, 0, 1] [0, 0, 1] >>> randMatrix(3, symmetric=True) # doctest:+SKIP [85, 26, 29] [26, 71, 43] [29, 43, 57] >>> A = randMatrix(3, seed=1) >>> B = randMatrix(3, seed=2) >>> A == B # doctest:+SKIP False >>> A == randMatrix(3, seed=1) True >>> randMatrix(3, symmetric=True, percent=50) # doctest:+SKIP [0, 68, 43] [0, 68, 0] [0, 91, 34] """ if c is None: c = r if seed is None: prng = random.Random() # use system time else: prng = random.Random(seed) if symmetric and r != c: raise ValueError( 'For symmetric matrices, r must equal c, but %i != %i' % (r, c)) if not symmetric: m = Matrix._new(r, c, lambda i, j: prng.randint(min, max)) else: m = zeros(r) for i in range(r): for j in range(i, r): m[i, j] = prng.randint(min, max) for i in range(r): for j in range(i): m[i, j] = m[j, i] if percent == 100: return m else: z = int(r*c*percent // 100) m._mat[:z] = [S.Zero]*z prng.shuffle(m._mat) return m