Skip to content

Commit ff5341b

Browse files
committed
Programming Assignment 2. It also contains two utility functions.
1 parent 7f657dd commit ff5341b

File tree

1 file changed

+109
-7
lines changed

1 file changed

+109
-7
lines changed

cachematrix.R

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,117 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## This function creates a special "matrix" object that
2+
## can cache its inverse.
3+
## Precondition: matrix should be a square invertible matrix
4+
##
5+
## @param: a matrix object,
6+
## @return: returns a list object consisting of functions
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
10+
if(!is.matrix(x) | is.null(x)){
11+
x = matrix()
12+
}
13+
14+
inverse <- NULL
15+
16+
set <- function(y) {
17+
if(!is.null(y) & is.matrix(y) & !NA %in% y){
18+
## update matrix only if the argument matrix isn't same as
19+
## the original matrix object, or if original matrix is empty
20+
if(!NA %in% x){
21+
if(!areMatricesEqual(x, y)){
22+
x <<- y
23+
inverse <<- NULL
24+
message("New matrix object has been set.")
25+
}
26+
} else{
27+
x <<- y
28+
}
29+
} else {
30+
message("Argument passed is NULL, empty or isn't matrix.")
31+
message("Please pass valid initialized matrix object.")
32+
}
33+
}
34+
35+
get <- function() {x}
36+
37+
setInverse <- function(inv) {
38+
if(!is.null(inv) & is.matrix(inv)){
39+
inverse <<- inv
40+
}
41+
}
42+
43+
getInverse <- function() {
44+
inverse
45+
}
46+
47+
list(set = set,
48+
get = get,
49+
setInverse = setInverse,
50+
getInverse = getInverse)
851
}
952

1053

11-
## Write a short comment describing this function
54+
## This function computes the inverse of a special "matrix" object
55+
## returned by makeCacheMatrix function. If the inverse has already
56+
## been calculated and the matrix has not changed, then this function
57+
## retrieve the inverse from cache.The parameter is checked if it is
58+
## a valid list object returned by makeCacheMatrix
59+
##
60+
## @param: a list object, that is returned by makeCacheMatrix function
61+
## @return: returns the mean of the special matrix object
1262

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+
66+
if(isParameterValidObject(x)){
67+
invMatrix <- x$getInverse()
68+
if(!is.null(invMatrix) ) {
69+
message("getting cached data...")
70+
return(invMatrix)
71+
}
72+
origMatrix <- x$get()
73+
if(!NA %in% origMatrix ){
74+
message("Calculating inverse...")
75+
invMatrix <- solve(origMatrix, ...)
76+
x$setInverse(invMatrix)
77+
return(invMatrix)
78+
} else {
79+
message("Matrix object of passed parameter is empty.")
80+
message("First set matrix value using 'set' function.")
81+
}
82+
} else{
83+
message("Please pass a valid makeCacheMatrix object.")
84+
}
85+
}
86+
87+
## Utility function that checks if two matrices are equal
88+
## Returns true if the matrices have equal dimensions and all
89+
## corresponding memeber elements are equal, otherwise false
90+
##
91+
## @param: x and y, the two matrices to be compared
92+
## @return: returns true if they are equal otherwise false
93+
94+
areMatricesEqual <- function(x, y) {
95+
#message("Comparing Matrix objects ...")
96+
if(is.matrix(x) & is.matrix(y)){
97+
if( (ncol(x) == ncol(y)) & (nrow(x) == nrow(y)) ){
98+
all(x == y)
99+
}
100+
}
101+
else {
102+
FALSE
103+
}
104+
}
105+
106+
## Utility function that checks if an object is a valid makeCacheMatrix object
107+
## That means it shoulb be a list of functions with apprpriate names
108+
## Returns true if it is valid object, otherwise false
109+
##
110+
## @param: param, the list object that will be validated
111+
## @return: returns true if makeChacheMatrix object otherwise false
112+
113+
isParameterValidObject <- function(param){
114+
#message("Validating makeChacheMatrix object ...")
115+
functionNames = c("set", "get", "setInverse", "getInverse")
116+
!is.null(param) & class(param) == "list" & all(length(param)== 4) & all(names(param)==functionNames)
15117
}

0 commit comments

Comments
 (0)