Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4927e65

Browse files
committed
working version of assignment functions
1 parent 7f657dd commit 4927e65

File tree

1 file changed

+79
-7
lines changed

1 file changed

+79
-7
lines changed

cachematrix.R

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,87 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
# WEEK3 ~ ASSIGNMENT
2+
# by John Barker
3+
# cachematrix.R
4+
# Pair of functions which cache the inverse of a matrix.
5+
# example usage:
6+
# cacheSolve(makeCacheMatrix(x = someInvertibleMatrix))
7+
#
8+
# The first function, makeCacheMatrix creates a special "matrix" object, makes a function to
9+
# set - the value of the matrix & reset cache
10+
# get - the value of the matrix object
11+
#
12+
# set - the value of the matrix that is the inverse of matrix x
13+
# get - ditto
14+
#
15+
makeCacheMatrix <- function(x = matrix()) {
316

4-
## Write a short comment describing this function
17+
# variable to store the inverse
18+
i <- NULL
519

6-
makeCacheMatrix <- function(x = matrix()) {
720

8-
}
21+
# function to invalidate the cache.
22+
# It sets x to new value y. It also resets the stored inverse: i.
23+
set <- function(y) {
24+
25+
#both lines make use of "Superassignment" operator:
26+
x <<- y #looks outside the current scope starting with the parents environment.
27+
i <<- NULL #looks outside the current scope starting with the parents environment.
28+
}
29+
30+
# simple getting of x.
31+
get <- function() x
932

33+
# function that sets i (again, looks in another environment) to the "solved" passed
34+
setinverse <- function(solved) i <<- solved
35+
36+
# returns the inverse
37+
getinverse <- function() i
38+
39+
# returns a list of functions:
40+
# it is a holder for the functions to get and set the values,
41+
# that are used by the partner function below, cacheSolve().
42+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
43+
44+
45+
46+
}
1047

11-
## Write a short comment describing this function
1248

49+
# computes the inverse of the "matrix" inside the obj. returned by makeCacheMatrix()
50+
# (i.e. the list of functions).
51+
#
52+
# If the inverse has already been calculated (and the matrix has not changed),
53+
# then cachesolve should retrieve the inverse from the cache.
54+
#
55+
#
56+
# NB assumes matrix supplied is invertible.
1357
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
58+
59+
60+
61+
# Return any cached matrix we may have that is the inverse of 'x',
62+
# by referencing the getinverse() function our partner function gave us.
63+
#
64+
i <- x$getinverse()
65+
66+
# if i was obtained with a value, we can just exit here
67+
if(!is.null(i)) {
68+
message("getting cached data")
69+
return(i) #exit here
70+
}
71+
72+
# if no cache exists, we use the passed matrix to generate the inverse:
73+
data <- x$get()
74+
75+
# R-function for obtaining the inverse of a matrix is solve(), assign this to i.
76+
#
77+
i <- solve(data, ...)
78+
79+
80+
# we need to cache this before exiting:
81+
x$setinverse(i)
82+
83+
# return the inverse value:
84+
return(i)
85+
86+
1587
}

0 commit comments

Comments
 (0)