Arne Goeteyn
Arne Goeteyn

Reputation: 166

Creating a big matrix where every element is the same

I'm trying to create a matrix of dimension nxn in Sage. But every element in the matrix has to be 1/n. The size of n is around 7000.

First I tried using creating a matrix of ones with the build in sagemethod, and then multiplying the matrix with 1/n. This is very slow and crashes my jupyter notebook kernel.

T =matrix.ones(7000) * 1/n

A second thing I tried is creating all the elements by list comprehension.

T = matrix(RDF,[[1/l for x in range(l)] for row in range(l)])

This also seems to be something my pc can't handle.

Upvotes: 3

Views: 274

Answers (3)

William Martens
William Martens

Reputation: 913

Well, you could do a all zero-matrix, like this:

matrix(3,2)

which will return Note: Treat all parentheses as one long

(0  0)
(0  0)
(0  0)

Upvotes: 0

Arne Goeteyn
Arne Goeteyn

Reputation: 166

@JamesKPolk gave me a working solution.

T = matrix(RDF, 6000, 6000, lambda i,j: 1/6000)

Upvotes: 3

Nimish Bansal
Nimish Bansal

Reputation: 1759

what about using zeros and adding a no what you want to array for e.g. if N=7000

then:

import numpy as np
N=7000
temp_array = np.zeros((N,N))
main_array = (1/N) + temp_array
print(main_array)

Upvotes: 0

Related Questions