Skip to content

Commit 7828761

Browse files
committed
Initial implementation.
1 parent 7f657dd commit 7828761

File tree

5 files changed

+120
-9
lines changed

5 files changed

+120
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData

ProgrammingAssignment2.Rproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX

cachematrix.R

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
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+
## Functions for memoization (http://en.wikipedia.org/wiki/Memoization) of
2+
## inverted matrices (http://en.wikipedia.org/wiki/Invertible_matrix).
53

4+
## Caches or memoizes matrix inversion
65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
# Initialize the inverted matrix to a value that an be used
7+
# for testing if inverted matrix has been calculated.
8+
s <- NULL
9+
# Set the matrix value to something other than the initial value
10+
# provided when creating the function.
11+
set <- function(y) {
12+
x <<- y
13+
s <<- NULL
14+
}
15+
# Return the matrix to be inverted
16+
get <- function() x
17+
# Set the inverted matrix
18+
sets <- function(solve) s <<- solve
19+
# Returns the cached inverted matrix
20+
gets <- function() s
21+
list(set = set, get = get,
22+
sets = sets,
23+
gets = gets)
824
}
925

10-
11-
## Write a short comment describing this function
12-
26+
## Returns a matrix that is the inverse of 'x'
1327
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
28+
# Check if inverted matrix already exists
29+
s <- x$gets()
30+
if(!is.null(s)) {
31+
message("Found inverted matrix")
32+
return(s)
33+
}
34+
# Retrieve original matrix
35+
data <- x$get()
36+
# Generate inverse
37+
s <- solve(data, ...)
38+
# Store inverse
39+
x$sets(s)
40+
s
1541
}
42+

cachemean.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Creates a special "vector", which is really a list containing a function to
2+
## - set the value of the vector
3+
## - get the value of the vector
4+
## - set the value of the mean
5+
## - get the value of the mean
6+
makeVector <- function(x = numeric()) {
7+
m <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setmean <- function(mean) m <<- mean
14+
getmean <- function() m
15+
list(set = set, get = get,
16+
setmean = setmean,
17+
getmean = getmean)
18+
}
19+
20+
## In this example we introduce the <<- operator which can be used to assign
21+
## a value to an object in an environment that is different from the current
22+
## environment. Below are two functions that are used to create a special
23+
## object that stores a numeric vector and caches its mean.
24+
25+
## The following function calculates the mean of the special "vector" created
26+
## with the above function. However, it first checks to see if the mean has
27+
## already been calculated.
28+
##
29+
## If so, it gets the mean from the cache and skips the computation. Otherwise,
30+
## it calculates the mean of the data and sets the value of the mean in the
31+
## cache via the setmean function.
32+
cachemean <- function(x, ...) {
33+
m <- x$getmean()
34+
if(!is.null(m)) {
35+
message("getting cached data")
36+
return(m)
37+
}
38+
data <- x$get()
39+
m <- mean(data, ...)
40+
x$setmean(m)
41+
m
42+
}

test.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# From the http://www.mathsisfun.com/algebra/matrix-inverse.html page
2+
mat <- matrix(c(4, 2, 7, 6), 2, 2)
3+
inv <- solve(mat)
4+
ide <- mat %*% inv
5+
6+
# See also http://www.purplemath.com/modules/mtrxmult.htm
7+
# for nice matrix multiplication algorithm graphic.
8+
9+
# Generate a random 2 x 2 matrix of integers from 1 to 10
10+
di <- 2
11+
mat <- matrix(sample(1:10, di * di), di, di)
12+
# Invert matrix
13+
inv <- solve(mat)
14+
# General identity matrix
15+
ide <- mat %*% inv
16+
# Should be true
17+
identical(ide, matrix(c(1, 0, 0, 1), 2, 2))
18+
19+
cm <- makeCacheMatrix(mat)
20+
# Should be true
21+
is.null(cm$gets())
22+
cs <- cacheSolve(cm)
23+
# Should be false
24+
is.null(cm$gets())
25+
26+
identical(inv, cacheSolve(cm))

0 commit comments

Comments
 (0)