From bd3096c333b420da3c74193755121e3862c2394e Mon Sep 17 00:00:00 2001 From: TPMi Date: Sat, 21 Jun 2014 23:01:59 +0400 Subject: [PATCH 1/2] Solution --- .gitignore | 2 ++ cachematrix.R | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..44b737b7bc2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.Rhistory diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..dd0d50d8833 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,21 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + s <- NULL + set <- function(y) { + x <<- y + s <<- NULL + } + get <- function() { + x + } + setSolve <- function(solve) { + s <<- solve + } + getSolve <- function() { + s + } + list(set = set, get = get, setSolve = setSolve, getSolve = getSolve) } @@ -12,4 +26,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + s <- x$getSolve() + if(!is.null(s)) { + message("getting cached data") + return(s) + } + data <- x$get() + s <- solve(data, ...) + x$setSolve(s) + s } From 22c299f60bbc4cd3f5ad62fa82b05b938404ac90 Mon Sep 17 00:00:00 2001 From: TPMi Date: Sat, 21 Jun 2014 23:13:19 +0400 Subject: [PATCH 2/2] Comments written --- cachematrix.R | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index dd0d50d8833..db3e0a591b4 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,19 @@ -## Put comments here that give an overall description of what your -## functions do +# This file contains functions: +# makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. +# cacheSolve: 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 cacheSolve should retrieve the inverse from the cache. -## Write a short comment describing this function +# makeCacheMatrix +# Creates a special "matrix" object that can cache its inverse. +# The object does not calculate the inverse, just saves it inside. +# Saves the matrix to variable x and its inverse to variable s in scope. +# Returned object (actually it's a list) contains methods: +# set: sets matrix and resets cached inverse +# get: returns matrix +# setSolve: saves solve value +# getSolve: returns cached inverse value makeCacheMatrix <- function(x = matrix()) { s <- NULL @@ -21,11 +33,12 @@ makeCacheMatrix <- function(x = matrix()) { list(set = set, get = get, setSolve = setSolve, getSolve = getSolve) } - -## Write a short comment describing this function - +# Function to get the inversed matrix from a special object created by makeCacheMatrix. +# Takes the object of that type as an argument 'x', checks if the inverse value is already +# cached, and if it is returns the cached value; if not, this function calculates the +# inverse for the matrix saved in the 'x', saves it into 'x' cache using method 'setSolve' +# and returns the result. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' s <- x$getSolve() if(!is.null(s)) { message("getting cached data")