0

I have created a matrix (matA) of 1000 rows and 1000 columns, and I want to calculate powers of this matrix. It works very well up to calculating the third power of the matrix. But when I ask to calculate its fourth power, it gives a warning message saying,

In matA * matA * matA * matA : NAs produced by integer overflow

How can I resolve this problem?

3
  • 2
    Convert the integer to numeric? Commented Jul 28, 2016 at 14:42
  • 3
    You want elemetwise power or matrix power as in t(mat)%*%mat? Commented Jul 28, 2016 at 14:47
  • 1
    Need the matrix power. I tried numeric conversion. but it gives the same warning. Commented Jul 28, 2016 at 15:28

1 Answer 1

4

Since you didn't give an example:

set.seed(101)
z <- matrix(rnorm(1e6),1e3)
z2 <- round(z)*1000000
storage.mode(z2) <- "integer"

If you really want a matrix power (as in z2 %*% z2 %*% z2 %*% z2), it's best to use the Matrix or expm package.

library(expm)
z4C <- z2 %^% 4

On the other hand, if you really want the elementwise product

z4D <- z2*z2*z2*z2
## Warning message "NAs produced"

All you need to do is convert to numeric.

storage.mode(z2) <- "numeric"
z4E <- z2*z2*z2*z2  ## fine
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.