Skip to content

Commit 0d3fe06

Browse files
committed
Add implementation of functions makeCacheMatrix and cacheSolve
1 parent 7f657dd commit 0d3fe06

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

cachematrix.R

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,84 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## This script contains the following functions.
2+
# makeCacheMatrix(<invertible matrix>):
3+
# This function receives as input a matrix object.
4+
# It then creates a composite object which has two attributes.
5+
# Its first attribute is a matrix object to which it
6+
# assigns the input matrix. Its second attribute is the
7+
# inverse of the input matrix which gets initialized with
8+
# the NULL value upon the creation of the composite object.
9+
# The created composite object also has member functions.
10+
# set() initializes the matrix object and inverse matrix object
11+
# attributes of the composite object. get() returns the matrix
12+
# object of the composite object. setsolve() sets the inverse
13+
# matrix attribute of the composite object into an input matrix.
14+
# getsolve() returns the inverse matrix attribute of the composite
15+
# object. The makeCacheMatrix function returns a list of its member
16+
# functions which are accessible via the $ operator.
17+
#
18+
# cacheSolve(<composite object output of makeCacheMatrix function>):
19+
# This function receives as input a composite object created using
20+
# the makeCacheMatrix function. This function returns the value of
21+
# the inverse matrix attribute of the composite matrix if is not
22+
# NULL. It computes for the inverse matrix attribute of the composite
23+
# object if the object's inverse matrix attribute is NULL and saves its
24+
# value which is accessible using the getsolve function.
325

4-
## Write a short comment describing this function
26+
# Note: The names of the member functions of makeCacheMatrix have been revised
27+
# to make them more intuitive for usage. The signature of the makeCacheMatrix
28+
# function has been kept the same though instructed.
529

30+
## Write a short comment describing this function
31+
# x - a matrix; assumed to be invertible
32+
# s - the inverse of the input matrix
633
makeCacheMatrix <- function(x = matrix()) {
7-
34+
# initialize the inverse of the input matrix data into NULL
35+
s <- NULL
36+
37+
# initialize the matrix data object
38+
instantiate <- function(y){
39+
# initialize the matrix data into the value of the input matrix y
40+
x <<- y
41+
42+
# initialize the inverse of the matrix data into NULL
43+
s <<- NULL
44+
}
45+
46+
# get the matrix data
47+
getMatrix <- function() x
48+
49+
# set the inverse of the matrix data
50+
setInverseMatrix <- function(sol) s <<- sol
51+
52+
# get the inverse of the input matrix data
53+
getInverseMatrix <- function() s
54+
55+
# return a list of the member functions accessible using $ operator
56+
return(list(instantiate = instantiate, getMatrix = getMatrix, setInverseMatrix = setInverseMatrix, getInverseMatrix = getInverseMatrix))
857
}
958

1059

1160
## Write a short comment describing this function
12-
61+
# x - a matrix; assumed to be invertible
62+
# s - the inverse of the input matrix
1363
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
64+
## Return a matrix that is the inverse of 'x'
65+
s <- x$getInverseMatrix()
66+
67+
# check if the returned inverse of the input matrix x is NULL or not
68+
# if the returned matrix inverse is not null, return it
69+
if(!is.null(s)){
70+
message("getting the inverse of matrix")
71+
} else { # else if the returned inverse matrix is NULL, compute for it and return it
72+
# get the matrix data of the input x
73+
data <- x$getMatrix()
74+
75+
# compute for the inverse of the matrix data using solve()
76+
s <- solve(data)
77+
78+
# set the inverse matrix attribute of the input x to the computed inverse matrix
79+
x$setInverseMatrix(s)
80+
}
81+
82+
# return the retrieved/computed inverse matrix
83+
return(s)
1584
}

0 commit comments

Comments
 (0)