Skip to content

Commit c6e545b

Browse files
committed
Better variable names
1 parent 658a8f1 commit c6e545b

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

cachematrix.R

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,54 @@
11
## Functions for computing cached matrix inversions
22

33
## makeCacheMatrix creates a cached matrix object for storing cached inversion results
4-
## Parameter x is the initial matrix stored in the cache
4+
## Parameter matrixValue is the initial matrix stored in the cache
5+
## Use cacheSolve() to compute its inverse
56

6-
makeCacheMatrix <- function(x = matrix()) {
7-
inv <- NULL
7+
makeCacheMatrix <- function(matrixValue = matrix()) {
8+
inverseValue <- NULL
89

9-
# Sets the matrix whose inverse is cached
10-
set <- function(y) {
11-
x <<- y
12-
inv <<- NULL
10+
# Sets the matrix whose inverse is cached (and resets the cached inverse)
11+
set <- function(newMatrixValue) {
12+
matrixValue <<- newMatrixValue
13+
inverseValue <<- NULL
1314
}
1415

1516
# Gets the matrix whose inverse is cached
1617
get <- function() {
17-
x
18+
matrixValue
1819
}
1920

20-
# Sets the inverse value of the matrix
21-
setinverse <- function(i) {
22-
inv <<- i
21+
# Sets the computed inverse value of the matrix
22+
setinverse <- function(newInverseValue) {
23+
inverseValue <<- newInverseValue
2324
}
2425

25-
# Gets the inverse value of the matrix
26+
# Gets the computed inverse value of the matrix
2627
getinverse <- function() {
27-
inv
28+
inverseValue
2829
}
2930

30-
# Return the object as a vector of functions
31+
# Return the cached matrix object as a vector of functions
3132
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
3233
}
3334

3435

35-
## cacheSolve computes the inverse of the given matrix x, using cached results when available
36-
## Parameter x must be a cached matrix object created using makeCacheMatrix()
36+
## cacheSolve computes the inverse of the given matrix, using cached results when available
37+
## Parameter cacheMatrix must be a cached matrix object created using makeCacheMatrix()
3738

38-
cacheSolve <- function(x, ...) {
39-
# Check if solution is cached
40-
inv <- x$getinverse()
41-
if (!is.null(inv)) {
42-
# Yes, it was cached, return it
43-
return(inv)
39+
cacheSolve <- function(cacheMatrix, ...) {
40+
# Check if solution is already cached
41+
inverseValue <- cacheMatrix$getinverse()
42+
if (!is.null(inverseValue)) {
43+
# Yes, solution was cached, return it
44+
return(inverseValue)
4445
}
4546

4647
# No, solution not yet cached, compute it now and cache it
47-
data <- x$get()
48-
inv <- solve(data, ...)
49-
x$setinverse(inv)
48+
matrixValue <- cacheMatrix$get()
49+
inverseValue <- solve(matrixValue, ...)
50+
cacheMatrix$setinverse(inverseValue)
5051

51-
# Return the inverse matrix
52-
inv
52+
# Return the computed inverse matrix
53+
inverseValue
5354
}

0 commit comments

Comments
 (0)