Skip to content

Commit ff55a22

Browse files
author
Alex Zukowsky
committed
attempt rdpeng#1
1 parent 7f657dd commit ff55a22

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

cachematrix.R

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
1+
makeCacheMatrix <- function(x = integer()) {
2+
## This function creates a special "matrix" object that can cache its inverse
3+
## The function requires the user to enter a vector of integers and creates the matrix from those integers
4+
5+
## an n-by-n square matrix A is called invertible if there exists an n-by-n square matrix B such that...
6+
## AB = BA = I(n)
7+
## A matrix must be a square (same number of rows as columns) to be invertible however not all square...
8+
## matrices are invertible
9+
## This function assumes an invertible square matrix
10+
11+
## This line of code calculates the number of rows/columns for our square matrix
12+
## If required an error check would have been put into place to ensure the list of integers...
13+
## could be converted to a square matrix
14+
cnt <- sqrt(length(x))
15+
## This line of code creates out matrix from x
16+
MX <- matrix(x, cnt, cnt)
17+
18+
m <- NULL
19+
20+
## This function sets the value of the matrix
21+
set <- function(y) {
22+
x <<- y
23+
m <<- NULL
24+
}
25+
26+
## This function gets the value of the matrix
27+
get <- function() x
28+
29+
#This function sets the value of the inverse of the matrix
30+
setinverse <- function(solve) m <<- solve
31+
32+
#This function gets the value of the inverse of the matrix
33+
getinverse <- function() m
34+
35+
# This line of code contructs our list
36+
list(set = set, get = get,
37+
setinverse = setinverse,
38+
getinverse = getinverse)
839
}
940

1041

11-
## Write a short comment describing this function
1242

1343
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
44+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
45+
## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve
46+
## should retrieve the inverse from the cache.
47+
48+
## Check to see if the cashed inverse already exists
49+
m <- x$getinverse()
50+
if(!is.null(m)) {
51+
message("getting cached inverse")
52+
return(m)
53+
}
54+
55+
# Calculate the inverse of the matrix and sets the value of the matrix in the cache via a setinverse function
56+
data <- x$get()
57+
m <- solve(data, ...)
58+
x$setinverse(m)
59+
m
1560
}

0 commit comments

Comments
 (0)