Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62ca099

Browse files
committed
checkin rdpeng#1
1 parent 7f657dd commit 62ca099

File tree

5 files changed

+167
-3
lines changed

5 files changed

+167
-3
lines changed

.RData

3.29 KB
Binary file not shown.

.Rhistory

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
getwd()
2+
a = matrix(1:6,2,3)
3+
a
4+
solve(a)
5+
a = matrix(1:9,3,3)
6+
a
7+
solve(a)
8+
a = matrix(c(4,3,3,2),2,2)
9+
a
10+
solve(a)
11+
makeCacheMatrix <- function(x = matrix()) {
12+
# 1.makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
13+
m <- 0 # not inverted
14+
inverse <- function(y) {
15+
x <<- solve(y)
16+
m <<- 1 #inverted
17+
}
18+
#get <- function() x
19+
#setmean <- function(mean) m <<- mean
20+
#getmean <- function() m
21+
#list(set = set, get = get,
22+
# setmean = setmean,
23+
# getmean = getmean)
24+
inverse(x)
25+
}
26+
makecacheMarix
27+
makecacheMarix(a)
28+
makeCacheMarix(a)
29+
makeCacheMatrix(a)
30+
x
31+
m
32+
makeCacheMatrix <- function(x = matrix()) {
33+
# 1.makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
34+
m <- 0 # not inverted
35+
inverse <- function(y) {
36+
invertedmatrix <<- solve(y)
37+
flag <<- 1 #inverted
38+
}
39+
#get <- function() x
40+
#setmean <- function(mean) m <<- mean
41+
#getmean <- function() m
42+
#list(set = set, get = get,
43+
# setmean = setmean,
44+
# getmean = getmean)
45+
inverse(x)
46+
}
47+
makeCacheMatrix(a)
48+
a
49+
invertedmatrix
50+
flag

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: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,65 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4+
## Matrix inversion is usually a costly computation and their may be some benefit to caching the inverse of a matrix rather than compute it repeatedly (there are also alternatives to matrix inversion that we will not discuss here). Your assignment is to write a pair of functions that cache the inverse of a matrix.
5+
##
6+
## Write the following functions:
7+
## 1.makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
8+
## 2.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 the cachesolve should retrieve the inverse from the cache.
9+
##
10+
## Computing the inverse of a square matrix can be done with the solve function in R. For example, if X is a square invertible matrix, then solve(X) returns its inverse.
11+
##
12+
## For this assignment, assume that the matrix supplied is always invertible
13+
14+
415
## Write a short comment describing this function
16+
# 1.makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
517

6-
makeCacheMatrix <- function(x = matrix()) {
718

19+
makeCacheMatrix <- function(p = matrix()) {
20+
21+
#########m <- NULL
22+
set <- function(y) {
23+
x <<- y
24+
m <<- NULL
25+
}
26+
get <- function() x
27+
setSolve <- function() {
28+
m <<- solve(x)
29+
message("Calculated")
30+
}
31+
getSolve <- function() m
32+
list(set = set, get = get,
33+
setSolve = setSolve,
34+
getSolve = getSolve)
35+
36+
# x is the matrix
37+
# m is the inverted matrix
38+
839
}
940

1041

1142
## Write a short comment describing this function
43+
#2.cacheSolve: This function computes the inverse of the special "matrix"
44+
# returned by makeCacheMatrix above. If the inverse has already been calculated
45+
# (and the matrix has not changed), then the cachesolve should retrieve the inverse
46+
# from the cache.
1247

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
48+
cacheSolve <- function(p, ...) {
49+
50+
## Return a matrix that is the inverse of 'x'
51+
#print(1)
52+
localm <- p$getSolve()
53+
#print(2)
54+
if(identical(x,p$get()) && !is.null(localm)) {
55+
message("getting cached data")
56+
message("inverted matrix:")
57+
print(localm)
58+
return(localm)
59+
}
60+
data <- p$get()
61+
p$setSolve()
62+
message("inverted matrix:")
63+
print(m)
64+
1565
}

main.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
main <- function()
2+
{
3+
# initialize a
4+
a = matrix(c(4,3,3,2),2,2)
5+
b = matrix(c(1,3,2,4),2,2)
6+
7+
# call MakeCacheMatrix
8+
mcm <- makeCacheMatrix(a)
9+
10+
# Set
11+
message("aaaaaaaaaaaaaaaaaaa")
12+
mcm$set(a)
13+
14+
#make sure that x and m are objects
15+
print("making sure that x and m are objects")
16+
print("x")
17+
print(x)
18+
print("m")
19+
print(m)
20+
21+
# call cacheSolve() the first time, make sure that Solve is calculated
22+
print("should get calculated message")
23+
cacheSolve(mcm)
24+
25+
# call cacheSolve again, and make sure that solve is not calculated again
26+
print("shoulg NOT get calculated message")
27+
cacheSolve(mcm)
28+
29+
# call cacheSolve again, and make sure that solve is not calculated again
30+
print("shoulg NOT get calculated message")
31+
cacheSolve(mcm)
32+
33+
# Set b
34+
message("bbbbbbbbbbbbbbbbbb")
35+
mcm$set(b)
36+
37+
# call cacheSolve() the first time, make sure that Solve is calculated
38+
print("should get calculated message")
39+
cacheSolve(mcm)
40+
41+
# call cacheSolve again, and make sure that solve is not calculated again
42+
print("shoulg NOT get calculated message")
43+
cacheSolve(mcm)
44+
45+
# call cacheSolve again, and make sure that solve is not calculated again
46+
print("shoulg NOT get calculated message")
47+
cacheSolve(mcm)
48+
49+
message("main exiting")
50+
51+
}

0 commit comments

Comments
 (0)