R: cache inverse matrix

I am following Datara's Coursera course, and I have a question regarding one of the purposes in which I have to invert the matrix and then cache this result.

Mostly I went to the search engine and I found the answer, but there are parts of the answer that I still don't understand. For this reason, I do not want to present my task since I do not want to represent anything that I do not quite understand.

The part that I don’t understand from the code below is the part where setInverse is defined. where does the function (inverse) inv come from? especially the "reverse" has never been determined?

After that comes back a list that doesn't make much sense to me?

If someone can take the time to explain this feature, I would be very grateful!

    makeCacheMatrix <- function(x = matrix()) {
    inv <- NULL
    set <- function(y) {
        x <<- y
        inv <<- NULL
    }
    get <- function() x
    setInverse <- function(inverse) inv <<- inverse
    getInverse <- function() inv
    list(set = set,
         get = get,
         setInverse = setInverse,
         getInverse = getInverse)
}


## Write a short comment describing this function

cacheSolve <- function(x, ...) {
    ## Return a matrix that is the inverse of 'x'
    inv <- x$getInverse()
    if (!is.null(inv)) {
        message("getting cached data")
        return(inv)
    }
    mat <- x$get()
    inv <- solve(mat, ...)
    x$setInverse(inv)
    inv
}
+4
1

, :

makeCacheMatrix <- function(x = matrix()) {
  inv <- NULL
  set <- function(y) {
    x <<- y
    inv <<- NULL
  }
  get <- function() x
  setInverse <- function() inv <<- solve(x) #calculate the inverse
  getInverse <- function() inv
  list(set = set,
       get = get,
       setInverse = setInverse,
       getInverse = getInverse)
}

:

funs <- makeCacheMatrix()
funs$set(matrix(1:4, 2))
funs$get()
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4
funs$setInverse()
funs$getInverse()
#     [,1] [,2]
#[1,]   -2  1.5
#[2,]    1 -0.5

, , , closures. , x inv set, get, setInverse, getInverse. , , .. , makeCacheMatrix(). :

ls(environment(funs$set))
#[1] "get"        "getInverse" "inv"        "set"        "setInverse" "x"

, x inv ( <<-). get getInverse .

+6

Source: https://habr.com/ru/post/1616080/


All Articles