diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..c41c93bcbe2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,61 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - -} - - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} +## makeCacheMatrix is the function that returns a closure +## that has methods initialize and retrieve matrix as wels as +## to set and return an attribute that is an inverse of the matrix + +## Example: lets generate positive semi-definite random matrix: + # M <- matrix(rnorm(100),10, 10) + # M <- M %*% t(M) + + +## + +## makeCacheMatrix is an object constructor, that initializes object with set + # and return methods + +makeCacheMatrix <- function(X = matrix()) { + myInverse<-NULL + + get <- function() X + #### get will return + + set <- function(Y) { + #### set will set matrix in the defining environemnt + #### and will reset inverse to NULL in the parent env + X<<-Y + myInverse <<- NULL + } + + #### 'methods' for working with our "object" + setInv<- function(XInv) myInverse <<- XInv + + getInv <- function() myInverse + + #return list + list(get = get, set = set, getInv = getInv, setInv = setInv) + +} + + +## cacheSolve returnsm inverse of the matrix + # it can also cache a particular solution if additional parameters are passed + # to solve() via ... + +cacheSolve <- function(x, ...) { + #### Returns a matrix that is the inverse of 'x' + #### WE rely on this method to work correctly with our closure. + #### this approach is set up by the framework of this assignment. It differs + #### from object oriented paradigm in that this method is not encapsulated. + INV <- x$getInv() + + if (!is.null(INV)) { + message("returning cached data") + return(INV) + } + + MX <- x$get() + INV <- solve(MX, ...) + x$setInv(INV) + #returned inverted matrix + INV +} + diff --git a/contact.md b/contact.md new file mode 100644 index 00000000000..b573c339183 --- /dev/null +++ b/contact.md @@ -0,0 +1,4 @@ +Contact +------- + +Should you want to contact me with regard to this assignment you can find me by spolgul on google. diff --git a/example/example.R b/example/example.R new file mode 100755 index 00000000000..b93069e9deb --- /dev/null +++ b/example/example.R @@ -0,0 +1,32 @@ +makeVector <- function(x = numeric()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmean <- function(mean) m <<- mean + getmean <- function() m + list(set = set, get = get, + setmean = setmean, + getmean = getmean) +} + +cachemean <- function(x, ...) { + m <- x$getmean() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- mean(data, ...) + x$setmean(m) + m +} + +v<-rnorm(10e7) + +mv<-makeVector(v) + +mean1<-cachemean(mv) +mean2<-vachemean(mv) \ No newline at end of file diff --git a/simpleTest.R b/simpleTest.R new file mode 100755 index 00000000000..df5cb9b3ded --- /dev/null +++ b/simpleTest.R @@ -0,0 +1,11 @@ +set.seed(1713) +#M <- matrix(rnorm(100),10, 10) +M <- matrix(runif(10e6),nrow=1000, ncol=1000) +M <- M%*% t(M) +dim(M) +XObj <- makeCacheMatrix (M) +#XObj$set(M) +system.time(I1<-cacheSolve(XObj)) +# second run +system.time(I2<-cacheSolve(XObj)) +