0

Good night. I have a homework of Coursera. But i have two days stuck trying to solve my problem.

My homework is:

Write the following functions:

makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve the inverse from the cache. Computing the inverse of a square matrix can be done with the solve function in R. For example, if X is a square invertible matrix, then solve(X) returns its inverse.

I use the library matlib for calculate the inverse of the matrix.

library(matlib)
makeCacheMatrix <- function(x = matrix()) {
if (ncol(x)==nrow(x) && det(x)!=0) {
        m<-NULL
        set<-function(y){
                x<<-y
                m<<-NULL
        }
        get<-function() x
        setinverse <- function() m <<- inv(x)
        getinverse<-function() m
        list(set=set,get=get,setinverse=setinverse,getinverse=getinverse)

}else{
        return(message("The matrix is'n invertible."))
}
}


cacheSolve <- function(x, ...) {
        m<-x$getinverse
        if (!is.null(m)) {
                message("getting cached data")
                return(m)
        }
        data<-x$get
        m <- inv(data, ...)
        x$setinverse(m)
        m
}

But when i'm trying for example test my code

x<-makeCacheMatrix(matrix(c(1,0,0,0,1,0,0,0,2),ncol=3,nrow=3))
x$get()
x$getinverse()

I obtain a NULL result. I don't know what's problem with my code. Can someone help me?

Bvss12
  • 101
  • 5

1 Answers1

1

Given the code in the OP, x$getinverse() should return NULL because one must execute cacheSolve() in order to populate the cache. I explain the details of how the sample code for this assignment works, including the need for the second function to populate the cache, in the stackoverflow answer Caching the Mean of a Vector.

That said, the program has three defects that prevent it from operating correctly.

  1. In cacheSolve(), m<-x$getinverse sets the value of m to a function, not the result of executing the getinverse() function

  2. In cacheSolve(), data<-x$get returns the address of the function get() instead of its contents.

  3. In cacheSolve(), x$setinverse(m) fails because the function setinverse() in makeCacheMatrix does not include an input argument.

Note that since I am a Community Mentor for the Hopkins R Programming course, I'm not allowed to post a complete solution because it would violate the Coursera Honor Code.

Once the errors are corrected, the code works like this:

> x <-makeCacheMatrix(matrix(c(1,0,0,0,1,0,0,0,2),ncol=3,nrow=3))
> cacheSolve(x)
     [,1] [,2] [,3]
[1,]    1    0  0.0
[2,]    0    1  0.0
[3,]    0    0  0.5
> x$get()
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    2
> x$getinverse()
     [,1] [,2] [,3]
[1,]    1    0  0.0
[2,]    0    1  0.0
[3,]    0    0  0.5
> 
Len Greski
  • 10,505
  • 2
  • 22
  • 33