├── cachematrix.R └── README.md /cachematrix.R: -------------------------------------------------------------------------------- 1 | ## It was a R function which compute the inverse of a matrix 2 | ## In another hand,this function can chache the inverse of the matrix 3 | ## which the R has computed before. 4 | 5 | 6 | test<-function(x){ 7 | ## it is a test function showing that 8 | ## the symbol "<<-" is give the value to 9 | ## a variance in global Environment instead of 10 | ## current enviroment 11 | a<<-x^2 12 | ## give a value to a in global environment 13 | x 14 | } 15 | 16 | ## the first function makes a list of functions can be used to cache the 17 | ## inverse of the matrix 18 | ## for example:if you want to compute a matrix's inverse, 19 | ## use"y<-makeCacheMatrix(x)" to build this list. 20 | 21 | makeCacheMatrix <- function(x = matrix()) { 22 | m <- NULL 23 | set <- function(y) { 24 | x <<- y 25 | m <<- NULL 26 | } 27 | get <- function() x 28 | setinverse <- function(solve) m <<- solve 29 | getinverse <- function() m 30 | list(set = set, get = get, 31 | setinverse = setinverse, 32 | getinverse = getinverse) 33 | } 34 | 35 | 36 | ## The following function calculates the inverse of the list created with the above function. 37 | ## However, it first checks to see if the inverse has already been calculated. 38 | ## If so, it gets the inverse from the cache and skips the computation. 39 | ## Otherwise, it calculates the inverse of the matrix,and save it to the cache. 40 | cacheSolve <- function(x, ...) { 41 | 42 | m <- y$getinverse() 43 | if(!is.null(m)) { 44 | message("getting cached data") 45 | return(m) 46 | } 47 | data <- y$get() 48 | m <- solve(data, ...) 49 | y$setinverse(m) 50 | m ## Return a matrix that is the inverse of 'x' 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Introduction 2 | 3 | This second programming assignment will require you to write an R 4 | function is able to cache potentially time-consuming computations. For 5 | example, taking the mean of a numeric vector is typically a fast 6 | operation. However, for a very long vector, it may take too long to 7 | compute the mean, especially if it has to be computed repeatedly (e.g. 8 | in a loop). If the contents of a vector are not changing, it may make 9 | sense to cache the value of the mean so that when we need it again, it 10 | can be looked up in the cache rather than recomputed. In this 11 | Programming Assignment will take advantage of the scoping rules of the R 12 | language and how they can be manipulated to preserve state inside of an 13 | R object. 14 | 15 | ### Example: Caching the Mean of a Vector 16 | 17 | In this example we introduce the `<<-` operator which can be used to 18 | assign a value to an object in an environment that is different from the 19 | current environment. Below are two functions that are used to create a 20 | special object that stores a numeric vector and cache's its mean. 21 | 22 | The first function, `makeVector` creates a special "vector", which is 23 | really a list containing a function to 24 | 25 | 1. set the value of the vector 26 | 2. get the value of the vector 27 | 3. set the value of the mean 28 | 4. get the value of the mean 29 | 30 | 31 | 32 | makeVector <- function(x = numeric()) { 33 | m <- NULL 34 | set <- function(y) { 35 | x <<- y 36 | m <<- NULL 37 | } 38 | get <- function() x 39 | setmean <- function(mean) m <<- mean 40 | getmean <- function() m 41 | list(set = set, get = get, 42 | setmean = setmean, 43 | getmean = getmean) 44 | } 45 | 46 | The following function calculates the mean of the special "vector" 47 | created with the above function. However, it first checks to see if the 48 | mean has already been calculated. If so, it `get`s the mean from the 49 | cache and skips the computation. Otherwise, it calculates the mean of 50 | the data and sets the value of the mean in the cache via the `setmean` 51 | function. 52 | 53 | cachemean <- function(x, ...) { 54 | m <- x$getmean() 55 | if(!is.null(m)) { 56 | message("getting cached data") 57 | return(m) 58 | } 59 | data <- x$get() 60 | m <- mean(data, ...) 61 | x$setmean(m) 62 | m 63 | } 64 | 65 | ### Assignment: Caching the Inverse of a Matrix 66 | 67 | Matrix inversion is usually a costly computation and their may be some 68 | benefit to caching the inverse of a matrix rather than compute it 69 | repeatedly (there are also alternatives to matrix inversion that we will 70 | not discuss here). Your assignment is to write a pair of functions that 71 | cache the inverse of a matrix. 72 | 73 | Write the following functions: 74 | 75 | 1. `makeCacheMatrix`: This function creates a special "matrix" object 76 | that can cache its inverse. 77 | 2. `cacheSolve`: This function computes the inverse of the special 78 | "matrix" returned by `makeCacheMatrix` above. If the inverse has 79 | already been calculated (and the matrix has not changed), then the 80 | `cachesolve` should retrieve the inverse from the cache. 81 | 82 | Computing the inverse of a square matrix can be done with the `solve` 83 | function in R. For example, if `X` is a square invertible matrix, then 84 | `solve(X)` returns its inverse. 85 | 86 | For this assignment, assume that the matrix supplied is always 87 | invertible. 88 | 89 | In order to complete this assignment, you must do the following: 90 | 91 | 1. Clone the GitHub repository containing the stub R files at 92 | [https://github.com/rdpeng/ProgrammingAssignment2](https://github.com/rdpeng/ProgrammingAssignment2) 93 | 2. Edit the R file contained in the git repository and place your 94 | solution in that file (please do not rename the file). 95 | 3. Commit your completed R file into YOUR git repository and push your 96 | git branch to your GitHub account. 97 | 4. Submit to Coursera the URL to your GitHub repository that contains 98 | the completed R code for the assignment. 99 | 100 | ### Grading 101 | 102 | This assignment will be graded via peer assessment. 103 | --------------------------------------------------------------------------------