简体   繁体   English

R编程:缓存矩阵的逆

[英]R programming: cache the inverse of a matrix

I am following a Data Science course on Coursera and I have a question regarding one of the assignments where I have to inverse a Matrix and then cache that result. 我正在关注Coursera上的数据科学课程,我有一个关于其中一个分配的问题,我必须反转矩阵,然后缓存该结果。

Basically I have been googling away and I found the answer but there are parts of the answer that I do not yet understand. 基本上我一直在谷歌搜索,我找到了答案,但有部分答案,我还不明白。 For this reason I don't want to submit my assignment yet since I don't want to submit anything that I do not fully understand. 出于这个原因,我不想提交我的作业,因为我不想提交任何我不完全理解的内容。

The part that I do not understand from the code below is the part where setInverse is defined. 我从下面的代码中无法理解的部分是定义setInverse的部分。 where does the 'function(inverse) inv' come from? 'function(inverse)inv'来自哪里? especially the 'inverse' was never defined? 特别是'逆'从未被定义过?

After this a list is returned which does not make much sense to me as well? 在此之后返回一个对我来说没有多大意义的列表?

If someone could take the time to explain this function to me 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
}

I don't know your exact assignment, but I would change your function slightly: 我不知道你的具体任务,但我会略微改变你的功能:

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)
}

You can then use it like this: 然后你可以像这样使用它:

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

The exercise is probably intended to teach you closures . 练习可能是为了教你封闭 The point is that x and inv are stored in the enclosing environment of the set , get , setInverse , getInverse functions. 关键是xinv存储在setgetsetInversegetInverse函数的封闭环境中。 That means the environment within which they were defined, ie, the environment created by the makeCacheMatrix() call. 这意味着定义它们的环境,即makeCacheMatrix()调用创建的环境。 See this: 看到这个:

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

As you see not only are the four functions in this environment, but also the x and inv objects (a consequence of using <<- ). 正如您所看到的,不仅是这个环境中的四个函数,还有xinv对象(使用<<- )。 And the get and getInverse functions only fetch these from their enclosing environment. getgetInverse函数只从它们的封闭环境中获取这些函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM