Code Monkey home page Code Monkey logo

programmingassignment2's Introduction

Introduction

This second programming assignment is about writing an R function that is able to cache potentially time-consuming computations.

Assignment: Caching the Inverse of a Matrix

Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of a matrix rather than computing it repeatedly. The assignment is to write a pair of functions that cache the inverse of a matrix.

###The following functions were developed:

  1. makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
  2. cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. If the inverse has already been calculated, then cacheSolve should retrieve the inverse from the cache.

The inverse of a square matrix is done using the solve function in R. It should be assumed that the matrix supplied is always invertible.

###Specific tasks:

  1. Fork the GitHub repository containing the stub R files at https://github.com/rdpeng/ProgrammingAssignment2
  2. Clone your forked GitHub repository to your computer so that you can edit the files locally on your own machine.
  3. Edit the R file contained in the git repository and place your solution in that file.
  4. Commit the completed R file into my git repository and push my git branch to the GitHub repository under your account.
  5. Submit to Coursera the URL to your GitHub repository that contains the completed R code for the assignment.

Tasks Completion

Steps

####1 - Fork - Done ####2 - Clone - Done ####3 - New code - Done - Also, new Readme file was created (this)
####4 - Commit - Done ####5 - Submit URL - Done

R Code

## Following the provided examples (makeVector and cachemean) 

## Function makeCacheMatrix
## It defines four functions and uses scope rules to cache data

makeCacheMatrix <- function(x = matrix()) {
  ## functions definitions: set, get, setinverse, and getinverse 
  
  # Set
  inv <- NULL
  set <- function(y) {
    x <<- y
    inv <<- NULL
  }
  
  # Get
  get <- function() x
  
  # setinverse
  setinverse <- function(solve) inv <<- solve
  
  # getinverse
  getinverse <- function() inv
  
  ## creating functions list to return
  list(set = set, 
       get = get, 
       setinverse = setinverse, 
       getinverse = getinverse
  )
} # end of makeCacheMatrix

## Function cacheSolve
#### This function returns the inverse of matrix x 
#### It first tests if cache exists in memory
#### If cache data is not found, it calculate and returns the matrix inverse

cacheSolve <- function(x, ...) {
  
  # Tests if cache exists 
  invertedM <- x$getinverse()
  if(!is.null(invertedM)) {
    cat("Cache found! Inverse fetched from memory...\n")
    return(invertedM)
  }
  
  # If cache not found, get the matrix, then calculate and return its inverse
  cat("Cache not found! Calculating inverse...\n")
  M <- x$get()
  invertedM <- solve(M)
  x$setinverse(invertedM)
  invertedM
  
} # end of cacheSolve

Execution (example)

x <- matrix(rnorm(25,3), nrow = 5)
cx <- makeCacheMatrix(x)
cacheSolve(cx)
## Cache not found! Calculating inverse...
##           [,1]      [,2]      [,3]       [,4]       [,5]

[1,] 1.558695 2.453920 -1.428934 -2.095689 0.4610002

[2,] -2.178205 -3.302608 2.078142 3.515491 -1.1585498

[3,] -2.965667 -4.057525 2.853786 3.715735 -1.0226829

[4,] -3.704581 -4.977545 2.950744 5.195148 -1.1364158

[5,] 7.623896 10.217652 -6.565686 -10.685902 3.0612797

cacheSolve(cx)
## Cache found! Inverse fetched from memory...
##           [,1]      [,2]      [,3]       [,4]       [,5]

[1,] 1.558695 2.453920 -1.428934 -2.095689 0.4610002

[2,] -2.178205 -3.302608 2.078142 3.515491 -1.1585498

[3,] -2.965667 -4.057525 2.853786 3.715735 -1.0226829

[4,] -3.704581 -4.977545 2.950744 5.195148 -1.1364158

[5,] 7.623896 10.217652 -6.565686 -10.685902 3.0612797

programmingassignment2's People

Contributors

steniofernandes avatar rdpeng avatar gustavdelius avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.