5

How to initialize a matrix to a very large number, say to infinity.

Similar to initializing all elements to zero: sample = np.matrix((np.zeros(50,50))

I want to initalize to infinity

How to do it in python?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Santle Camilus
  • 945
  • 2
  • 12
  • 20
  • You must understand that a computer exists in a finite world. Perhaps what you're asking is how to dynamically allocate an array. – Lescurel Sep 11 '17 at 08:42
  • "infinity" is _not_ "a very large number". – tobias_k Sep 11 '17 at 08:44
  • 2
    @Lescurel infinity *is* a value representable in float numbers. Check out [the format specification for IEEE 754](https://en.wikipedia.org/wiki/IEEE_754-1985) for details on how that works – GPhilo Sep 11 '17 at 08:45
  • 1
    @GPhilo Thanks, I've learned something today – Lescurel Sep 11 '17 at 08:54

2 Answers2

12

There is np.infin numpy

>>> sample = np.matrix(np.ones((50,50)) * np.inf)
>>> sample
matrix([[ inf,  inf,  inf, ...,  inf,  inf,  inf],
    [ inf,  inf,  inf, ...,  inf,  inf,  inf],
    [ inf,  inf,  inf, ...,  inf,  inf,  inf],
    ..., 
    [ inf,  inf,  inf, ...,  inf,  inf,  inf],
    [ inf,  inf,  inf, ...,  inf,  inf,  inf],
    [ inf,  inf,  inf, ...,  inf,  inf,  inf]])
Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99
3

Numpy has infinity object, you can call it by np.inf.

cs95
  • 379,657
  • 97
  • 704
  • 746
Firman
  • 928
  • 7
  • 15