diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..e2bde6a46fc --- /dev/null +++ b/.Rhistory @@ -0,0 +1,69 @@ +install.packages("swirl") +load(swirl) +library(swirl) +swirl() +swirl() +swirl() +swirl() +install_from_swirl("R Programming") +install_from_swirl("R Programming") +install_from_swirl("https://github.com/swirldev/swirl_courses/tree/master/R_Programming") +install_from_swirl("R Programming") +install_from_swirl("R Programming") +swirl() +identical(2,2.0) +library(datasets) +data("iris") +data(iris) +data("mtcars") +with(mtcars, tapply(mpg, cyl, mean)) +sapply(split(mtcars$mpg, mtcars$cyl), mean) +tapply(mtcars$mpg, mtcars$cyl, mean) +lapply(mtcars, mean) +tapply(mtcars$mpg, mtcars$cyl, +tapply(mtcars$cyl, mtcars$mpg, mean) +) +tapply(mtcars$cyl, mtcars$mpg, mean) +tapply(mtcars$cyl, mtcars$mpg, mean) +apply(mtcars, 2, mean) +mean(mtcars$mpg, mtcars$cyl) +sapply(mtcars, cyl, mean) +split(mtcars, mtcars$cyl) +apply(iris[, 1:4], 2, mean) +apply(iris[, 1:4], 2, mean) +result <- iris[iris$Species == "virginica", ] +lapply(result[1], mean) +matrix(c(1,2,3,4,5,6),nrow = 2, ncol = 3) +?matrix +mat<-matrix(c(1,2,3,4,5,6),nrow = 2, ncol = 3) +mat +solve(mat) +solve(mat) +mat<-matrix(c(1,2,3,4,5,6),nrow = 2, ncol = 3, byrow = TRUE) +mat +solve(mat) +library("MASS", lib.loc="C:/Program Files/R/R-3.2.3/library") +install.packages("MASS") +ginv +ls +ginv +getwd() +setwd("F:/data from desktop/MyDocuments/Learning/GitHubProj/datasciencecoursera/ProgrammingAssignment2") +getwd() +source("makeCacheMatrix") +source("makecacheMatrix") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +source("cachematrix.R") +swirl() +getwd() +swirl diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d1fc308c976 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,80 @@ +## Programming assignment 2 ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## This function will create a new matrix obj which can cache inverse that can be used later -makeCacheMatrix <- function(x = matrix()) { + makeCacheMatrix <- function(x = matrix()) { + ## @x: a square invertible matrix + ## return: a list containing functions to + ## 1. set the matrix + ## 2. get the matrix + ## 3. set the inverse + ## 4. get the inverse + ## this list is used as the input to cacheSolve() + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + ##print (x) + get <- function() x + setinverse <- function(inverse) m <<- inverse + ##print (m) + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) + + } + ## End of makecachematrix() + + ## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. + ## If the inverse has already been calculated (and the matrix has not changed), + ## then the cachesolve should retrieve the inverse from the cache. -} - - -## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inverse<- x$getinverse() + # if the inverse has already been calculated + if(!is.null(inverse)) { + # get it from the cache and skips the computation. + message("getting cached data") + return(inverse) + } + # otherwise, calculates the inverse + data<-x$get() + inverse = solve(data) + # sets the value of the inverse in the cache via the setinv function. + x$setinverse(inverse) + + return(inverse) } +## end of cacheSolve + +## test function +testInverseCache <- function(mat){ + ## Pass a matrix filled with random numbers + cachematdata <- makeCacheMatrix(mat) + # Lets do some profiling .. first run + start.time <-Sys.time() + # Pass matrix object to cacheSolve + cacheSolve(cachematdata) + timeTaken <- Sys.time() - start.time + print ("Time taken without caching") + print(timeTaken) + # Now call again same with same matix .. this time it should pick from cache so + ## shall take less time + start.time <- Sys.time() + cacheSolve(cachematdata) + timeTaken <- Sys.time() - start.time + print ("Time taken after caching") + print(timeTaken) +} +## End of testInverseCache + +## Sample Test data +set.seed(1) +r = rnorm(4000000) +mat = matrix(r, nrow=2000, ncol=2000) +testInverseCache(mat) diff --git a/cachematrix.html b/cachematrix.html new file mode 100644 index 00000000000..5923a7cdf55 --- /dev/null +++ b/cachematrix.html @@ -0,0 +1,229 @@ + + + + + + + + + + + + + + +cachematrix.R + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + +
## Programming assignment 2 
+## Put comments here that give an overall description of what your
+## functions do
+
+## This function will create a new matrix obj which can cache inverse that can be used later 
+
+  makeCacheMatrix <- function(x = matrix()) {
+    ## @x: a square invertible matrix
+    ## return: a list containing functions to
+    ##              1. set the matrix
+    ##              2. get the matrix
+    ##              3. set the inverse
+    ##              4. get the inverse
+    ##         this list is used as the input to cacheSolve() 
+    m <- NULL
+    set <- function(y) {
+      x <<- y
+      m <<- NULL
+    }
+    ##print (x)
+    get <- function() x
+    setinverse <- function(inverse) m <<- inverse
+    ##print (m)
+    getinverse <- function() m
+    list(set = set, get = get,
+         setinverse = setinverse,
+         getinverse = getinverse)
+  
+  }
+  ## End of makecachematrix()
+  
+  ## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. 
+  ## If the inverse has already been calculated (and the matrix has not changed), 
+  ## then the cachesolve should retrieve the inverse from the cache.
+
+
+cacheSolve <- function(x, ...) {
+  inverse<- x$getinverse()
+  # if the inverse has already been calculated
+  if(!is.null(inverse)) {
+    # get it from the cache and skips the computation. 
+    message("getting cached data")
+    return(inverse)
+  }
+  # otherwise, calculates the inverse 
+  data<-x$get()
+  inverse = solve(data)
+  # sets the value of the inverse in the cache via the setinv function.
+  x$setinverse(inverse)
+  
+  return(inverse)
+}
+## end of cacheSolve
+
+## test function
+testInverseCache <- function(mat){
+  ## Pass a matrix filled with random numbers
+  cachematdata <- makeCacheMatrix(mat)
+  # Lets do some profiling .. first run
+  start.time <-Sys.time()
+  # Pass matrix object to cacheSolve
+  cacheSolve(cachematdata)
+  timeTaken <- Sys.time() - start.time
+  print ("Time taken without caching")
+  print(timeTaken)
+  # Now call again same with same matix .. this time it should pick from cache so 
+  ## shall take less time 
+  start.time <- Sys.time()
+  cacheSolve(cachematdata)
+  timeTaken <- Sys.time() - start.time
+  print ("Time taken after caching")
+  print(timeTaken)
+}
+## End of testInverseCache
+
+## Sample Test data
+set.seed(1)
+r = rnorm(4000000)
+mat = matrix(r, nrow=2000, ncol=2000)
+testInverseCache(mat)
+
## [1] "Time taken without caching"
+## Time difference of 28.1537 secs
+
## getting cached data
+
## [1] "Time taken after caching"
+## Time difference of 0.002998114 secs
+ + + + +
+ + + + + + + +