forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramming Assignment 2 - Lexical Scoping.Rmd
More file actions
118 lines (80 loc) · 2.78 KB
/
Programming Assignment 2 - Lexical Scoping.Rmd
File metadata and controls
118 lines (80 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
title: 'Programming Assignment 2: Lexical Scoping'
author: "Oleksandr Titorchuk"
date: 'Sep 1, 2018'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### 1. Introduction
This report was prepared to test functions created as part of *Programming Assignment 2* for **R Programming** course (Coursera, Data Science Specialization, Johns Hopkins Bloomberg School of Public Health).
Credit for test scenarios goes to [Alan E. Berger] [1], who generously explained basic algebra which stands behind operations with matrices.
### 2. Definition of inverse matrix
``` {r}
#Lets define m1 as simple 2*2 matrix
m1 <- matrix(c(1/2, -1/4, -1, 3/4), nrow = 2, ncol = 2)
m1
# Lets define n1 also as simple 2*2 matrix
n1 <- matrix(c(6,2,8,4), nrow = 2, ncol = 2)
n1
# Matrix m1 is said to be inverse of matrix n1 (and vice versa)
# if their multiplication produces identity matrix i1
i1 <- matrix(c(1,0,0,1), nrow = 2, ncol = 2)
i1
# Lets check whether n1 is in fact inverse of m1 (and vice versa)
i2<-m1 %*% n1
i2
i2==i1
i3<-n1 %*% m1
i3
i3==i1
```
### 3. Calculation of inverse matrix
``` {r}
# Lets now calculate inverse matrix for m1 via solve()
# to make sure that result is the same as n1
# We will do the same thing for n1
n2 <- solve(m1)
n2
n2==n1
m2<-solve(n1)
m2
m2==m1
```
Hmmm, in the last example it seems that R does not agree that matrices `m1` and `m2` are equal. Lets find out why. Full answer to this question is presented here [stackoverflow] [2], but in short here is how `m2` matrix really looks like. Not quite what we expected, yeah?
```{r}
sprintf("%.54f",m2)
```
We can use `all.equal()` function to make our test again and receive proper answer.
```{r}
isTRUE(all.equal(m1, m2))
```
### 4. Testing makeCacheMatrix() and cacheSolve()
``` {r}
# To run the tests you must have both functions loaded
# in your environment
setwd("D:/Coursera/Data Science Specialization/2. R Programming/Programming Assignment 2/ProgrammingAssignment2")
source("cachematrix.r")
# myInvMatrix must be equal to n1
myMatrix <- makeCacheMatrix(m1)
myInvMatrix_m <- cacheSolve(myMatrix)
myInvMatrix_m
myInvMatrix_m == n1
# Calling cacheSolve() second time should retrieve cached matrix
# and not recalculate it
cacheSolve(myMatrix)
# We can put inside our fuction new matrix
# by using set function and get inverse of it
myMatrix$set(n1)
myInvMatrix_n <- cacheSolve(myMatrix)
myInvMatrix_n
myInvMatrix_n == m1
# Okay, and what about this? Seems better.
isTRUE(all.equal(m1, myInvMatrix_n))
# Later we can retrieve our cached matrix
# in the same way we did previously
cacheSolve(myMatrix)
```
[1]: https://www.coursera.org/learn/r-programming/discussions/weeks/3/threads/ePlO1eMdEeahzg7_4P4Vvg
[2]: https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal