# What does power operator or this ^ really do in R?

So I am too embarassed to ask this on stackoverflow. For the record, I do have the R manual and it says,

^ Exponentiation, binary

``````X1 <- c(1,0,1,4,2)
X2 <- c(0,1,1,2,4)
X <- as.matrix(data.frame(X1=X1,X2=X2))
(Product <- (t(X)%*% X))
# [1,7;7,1] matrix
``````

The I can find the proper inverse of `Product` by doing

``````solve(Product)
#            X1          X2
# X1        1/7        -1/48
# X2       -1/48        1/7
``````

However, what I found was if I raise the `Product` to the -1 power, I get a different answer

``````Product ^ (-1)
#          X1        X2
# X1 1/7 1
# X2 1 1/7
``````

The behavior when you raise a matrix to a negative number is a little odd, can someone explain what the heck is going on? What is the "binary" operation of a power?

There finished my question. I hope an R expert out there can answer my very basic math question.

When I run Product ^(-1), I get the element-wise inverse of Product.

``````X1 <- c(1,0,1,4,2)
X2 <- c(0,1,1,2,4)
X <- as.matrix(data.frame(X1=X1,X2=X2))
(Product <- (t(X)%*% X))
#>    X1 X2
#> X1 22 17
#> X2 17 22

Product ^ (-1)
#>            X1         X2
#> X1 0.04545455 0.05882353
#> X2 0.05882353 0.04545455

matrix(c(1/22, 1/17, 1/17, 1/22), nrow = 2)
#>            [,1]       [,2]
#> [1,] 0.04545455 0.05882353
#> [2,] 0.05882353 0.04545455
``````

Created on 2023-01-27 with reprex v2.0.2

1 Like
``````16^-2
#>  0.00390625
c(16,16)^-2
#>  0.00390625 0.00390625
(matrix(rep(16,4), nrow = 4, ncol = 4))^-2
#>            [,1]       [,2]       [,3]       [,4]
#> [1,] 0.00390625 0.00390625 0.00390625 0.00390625
#> [2,] 0.00390625 0.00390625 0.00390625 0.00390625
#> [3,] 0.00390625 0.00390625 0.00390625 0.00390625
#> [4,] 0.00390625 0.00390625 0.00390625 0.00390625

# But that's wrong, use the reciprocal, instead

16^(1/2)
#>  4

# But don't forget to use () to force evaluation of the reciprocal before exponentiation

16^1/2 # 16^1 = 16 / 8 = 8
#>  8

c(16,16)^(1/2)
#>  4 4
(matrix(rep(16,4), nrow = 4, ncol = 4))^(1/2)
#>      [,1] [,2] [,3] [,4]
#> [1,]    4    4    4    4
#> [2,]    4    4    4    4
#> [3,]    4    4    4    4
#> [4,]    4    4    4    4
``````
``````X1 <- c(1,0,1,4,2)
X2 <- c(0,1,1,2,4)
X <- as.matrix(data.frame(X1=X1,X2=X2))
Product <- (t(X)%*% X)
Product^-1
#>            X1         X2
#> X1 0.04545455 0.05882353
#> X2 0.05882353 0.04545455
Product^(1/1) == Product
#>      X1   X2
#> X1 TRUE TRUE
#> X2 TRUE TRUE
``````

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.