Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. # ================================================================================
  2. # Markus Lindenthal
  3. # Uebungsblatt 4
  4. # ================================================================================
  5.  
  6.  
  7. # ================================================================================
  8. # |--------------------------------Beispiel 1------------------------------------|
  9. # ================================================================================
  10.  
  11. # testmatrix
  12. M <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
  13.  
  14. #------------------------Recursive------------------------------
  15. powerRec <- function(X, n = 2) {
  16.  
  17. text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
  18. text2 <- "\nX muss eine quadratische Matrix sein!"
  19.  
  20. # checking if matrix can be multiplied by itself
  21. if (!is.matrix(X) | nrow(X) != ncol(X)) stop(text2)
  22.  
  23. # power needs to be a natural nr
  24. else if (!is.numeric(n) | n != round(n) | n < 0) stop(text1)
  25.  
  26. # identity matrix
  27. else if (n == 0) return(diag(1,nrow(X)))
  28.  
  29. # returning matrix
  30. else if (n == 1) return(X)
  31.  
  32. # recursion
  33. else return(X %*% powerRec(X, n-1))
  34. }
  35.  
  36.  
  37. M
  38. powerRec(M, 3)
  39.  
  40.  
  41.  
  42. #---------------------Function------------------------------
  43.  
  44. matrixPower <- function(mat, n)
  45. {
  46. text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
  47. text2 <- "\nM muss eine quadratische Matrix sein!"
  48.  
  49. # n has to be a natural nr >= 0
  50. if(!is.numeric(n) | n < 0 | !(n %% 1 == 0)) stop(text1)
  51.  
  52. # mat needs to be quadratic
  53. if(ncol(mat) != nrow(mat) | !(is.matrix(mat))) stop(text2)
  54.  
  55. # identity matrix
  56. if(n == 0) return(diag(ncol(mat)))
  57.  
  58. # return normal matrix
  59. if (n == 1) return(mat)
  60.  
  61. # Exponentiation by squaring
  62. temp <- diag(ncol(mat))
  63. while (n > 0) {
  64. if (n %% 2 != 0) {
  65. temp <- temp %*% mat
  66. n <- n - 1
  67. }
  68. mat <- mat %*% mat
  69. n <- n / 2
  70. }
  71. return(temp)
  72. }
  73.  
  74. matrixPower(M, 1)
  75.  
  76.  
  77.  
  78. # ================================================================================
  79. # |--------------------------------Beispiel 2------------------------------------|
  80. # ================================================================================
  81.  
  82. dist.matrix <- as.matrix(eurodist) #eurodist as matrix
  83.  
  84.  
  85. City.prob <- function(city = "Vienna") {
  86.  
  87. probCity <- dist.matrix[,city]/sum(dist.matrix[,city]) #Vektor mit Wahrscheinlichkeiten erstellen
  88.  
  89. return(probCity)
  90. }
  91.  
  92. asdf <- sum(City.prob())
  93.  
  94.  
  95. #---------------------Beispiel 2b--------------------------------------
  96.  
  97. probVienna <- function(n, city = "Vienna"){
  98.  
  99. City.names <- rownames(dist.matrix) #Vektor mit Staedtenamen
  100. Cities.visited <- vector(mode = "character", length = (n-1)) #Vektor um Reisepfad festzuhalten
  101.  
  102. for( i in 1:(n-1)){
  103.  
  104. prob.City <- City.prob(city)
  105. Cities.visited[i] <- city #Standort festhalten im Reisepfad
  106.  
  107. city <- sample(City.names, 1, FALSE, prob.City) #Reiseziel ermitteln
  108.  
  109.  
  110.  
  111. }
  112. print(Cities.visited) #Reisepfad ausgeben
  113. return(prob.City["Vienna"])
  114. }
  115.  
  116. probVienna(10)
  117.  
  118.  
  119.  
  120. # ================================================================================
  121. # |--------------------------------Beispiel 3------------------------------------|
  122. # ================================================================================
  123.  
  124.  
  125. timepath <- function(n, city = "Vienna"){
  126. City.names <- rownames(dist.matrix) #Vektor mit Staedtenamen
  127. Cities.visited <- vector(mode = "character", length = (n-1)) #Vektor um Reisepfad festzuhalten
  128. Time.arrival <- vector(mode = "character", length = n) #Vektor um ankunftszeiten festzuhalten
  129. start.time <- as.POSIXct("25.12.2019, 18:00", format ="%d.%m.%Y, %H:%M") #Startzeit definieren
  130. Times <- c(0, rgeom(n-1, 0.02)) #Aufenthaltszeiten
  131. for( i in 1:n){
  132.  
  133. prob.City <- City.prob(city)
  134. Cities.visited[i] <- city #Standort festhalten im Reisepfad
  135. if (i == 1) {
  136. Time.arrival[i] <- as.character(start.time) #bei i = 1 noch kein vorheriges reiseziel
  137. temp.time <- start.time
  138. }
  139. else {
  140. traveltime <- dist.matrix[city, Cities.visited[i-1]]/80 #Reisezeit berechnen in Stunden
  141.  
  142. temp.time <- temp.time + (Times[i-1] + traveltime) * 3600 #Gibt Datum Zurueck
  143. Time.arrival[i] <- as.character(temp.time) #Datum in vektor einfuegen
  144. }
  145.  
  146. city <- sample(City.names, 1, FALSE, prob.City) #Reiseziel ermitteln
  147.  
  148. }
  149. time.res <- format(as.POSIXct(Time.arrival), format = "Ankunft am %d:%m:%Y, um %H:%M Uhr in") #formatieren
  150. arrival.times <- paste(as.character(time.res), Cities.visited, sep = " ") #nochmal formatieren
  151.  
  152.  
  153.  
  154.  
  155.  
  156. return(arrival.times)
  157. }
  158.  
  159.  
  160.  
  161. timepath(5)
  162.  
  163.  
  164. #pos 2
  165. #temp.time <- start.time + (Time[i-1] + traveltime) * 3600 #gibt datum zurueck
  166. #Time.arrival[i] <- as.character(temp.time)
  167.  
  168. # ================================================================================
  169. # |--------------------------------Beispiel 4------------------------------------|
  170. # ================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement