Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ================================================================================
- # Markus Lindenthal
- # Uebungsblatt 4
- # ================================================================================
- # ================================================================================
- # |--------------------------------Beispiel 1------------------------------------|
- # ================================================================================
- # testmatrix
- M <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
- #------------------------Recursive------------------------------
- powerRec <- function(X, n = 2) {
- text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
- text2 <- "\nX muss eine quadratische Matrix sein!"
- # checking if matrix can be multiplied by itself
- if (!is.matrix(X) | nrow(X) != ncol(X)) stop(text2)
- # power needs to be a natural nr
- else if (!is.numeric(n) | n != round(n) | n < 0) stop(text1)
- # identity matrix
- else if (n == 0) return(diag(1,nrow(X)))
- # returning matrix
- else if (n == 1) return(X)
- # recursion
- else return(X %*% powerRec(X, n-1))
- }
- M
- powerRec(M, 3)
- #---------------------Function------------------------------
- matrixPower <- function(mat, n)
- {
- text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
- text2 <- "\nM muss eine quadratische Matrix sein!"
- # n has to be a natural nr >= 0
- if(!is.numeric(n) | n < 0 | !(n %% 1 == 0)) stop(text1)
- # mat needs to be quadratic
- if(ncol(mat) != nrow(mat) | !(is.matrix(mat))) stop(text2)
- # identity matrix
- if(n == 0) return(diag(ncol(mat)))
- # return normal matrix
- if (n == 1) return(mat)
- # Exponentiation by squaring
- temp <- diag(ncol(mat))
- while (n > 0) {
- if (n %% 2 != 0) {
- temp <- temp %*% mat
- n <- n - 1
- }
- mat <- mat %*% mat
- n <- n / 2
- }
- return(temp)
- }
- matrixPower(M, 1)
- # ================================================================================
- # |--------------------------------Beispiel 2------------------------------------|
- # ================================================================================
- dist.matrix <- as.matrix(eurodist) #eurodist as matrix
- City.prob <- function(city = "Vienna") {
- probCity <- dist.matrix[,city]/sum(dist.matrix[,city]) #Vektor mit Wahrscheinlichkeiten erstellen
- return(probCity)
- }
- asdf <- sum(City.prob())
- #---------------------Beispiel 2b--------------------------------------
- probVienna <- function(n, city = "Vienna"){
- City.names <- rownames(dist.matrix) #Vektor mit Staedtenamen
- Cities.visited <- vector(mode = "character", length = (n-1)) #Vektor um Reisepfad festzuhalten
- for( i in 1:(n-1)){
- prob.City <- City.prob(city)
- Cities.visited[i] <- city #Standort festhalten im Reisepfad
- city <- sample(City.names, 1, FALSE, prob.City) #Reiseziel ermitteln
- }
- print(Cities.visited) #Reisepfad ausgeben
- return(prob.City["Vienna"])
- }
- probVienna(10)
- # ================================================================================
- # |--------------------------------Beispiel 3------------------------------------|
- # ================================================================================
- timepath <- function(n, city = "Vienna"){
- City.names <- rownames(dist.matrix) #Vektor mit Staedtenamen
- Cities.visited <- vector(mode = "character", length = (n-1)) #Vektor um Reisepfad festzuhalten
- Time.arrival <- vector(mode = "character", length = n) #Vektor um ankunftszeiten festzuhalten
- start.time <- as.POSIXct("25.12.2019, 18:00", format ="%d.%m.%Y, %H:%M") #Startzeit definieren
- Times <- c(0, rgeom(n-1, 0.02)) #Aufenthaltszeiten
- for( i in 1:n){
- prob.City <- City.prob(city)
- Cities.visited[i] <- city #Standort festhalten im Reisepfad
- if (i == 1) {
- Time.arrival[i] <- as.character(start.time) #bei i = 1 noch kein vorheriges reiseziel
- temp.time <- start.time
- }
- else {
- traveltime <- dist.matrix[city, Cities.visited[i-1]]/80 #Reisezeit berechnen in Stunden
- temp.time <- temp.time + (Times[i-1] + traveltime) * 3600 #Gibt Datum Zurueck
- Time.arrival[i] <- as.character(temp.time) #Datum in vektor einfuegen
- }
- city <- sample(City.names, 1, FALSE, prob.City) #Reiseziel ermitteln
- }
- time.res <- format(as.POSIXct(Time.arrival), format = "Ankunft am %d:%m:%Y, um %H:%M Uhr in") #formatieren
- arrival.times <- paste(as.character(time.res), Cities.visited, sep = " ") #nochmal formatieren
- return(arrival.times)
- }
- timepath(5)
- #pos 2
- #temp.time <- start.time + (Time[i-1] + traveltime) * 3600 #gibt datum zurueck
- #Time.arrival[i] <- as.character(temp.time)
- # ================================================================================
- # |--------------------------------Beispiel 4------------------------------------|
- # ================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement