library(Ryacas)
## Warning: package 'Ryacas' was built under R version 4.3.3
##
## Attaching package: 'Ryacas'
## The following object is masked from 'package:stats':
##
## integrate
## The following objects are masked from 'package:base':
##
## %*%, det, diag, diag<-, lower.tri, upper.tri
library(Rmpfr)
## Warning: package 'Rmpfr' was built under R version 4.3.3
## Loading required package: gmp
## Warning: package 'gmp' was built under R version 4.3.3
##
## Attaching package: 'gmp'
## The following object is masked from 'package:Ryacas':
##
## %*%
## The following objects are masked from 'package:base':
##
## %*%, apply, crossprod, matrix, tcrossprod
## C code of R package 'Rmpfr': GMP using 64 bits per limb
##
## Attaching package: 'Rmpfr'
## The following object is masked from 'package:gmp':
##
## outer
## The following objects are masked from 'package:stats':
##
## dbinom, dgamma, dnbinom, dnorm, dpois, dt, pnorm
## The following objects are masked from 'package:base':
##
## cbind, pmax, pmin, rbind
## Create variable `my_name` that is equal to your first name and print it
my_name <- "Ott"
my_name
## [1] "Ott"
## Sometimes you may want to print a sentence instead
## of just the name
cat("I am", my_name, "\n")
## I am Ott
## 1.1.1
## Create a numeric variable my_age that is equal to your age
my_age <- 18
## 1.1.2
## How many seconds is there in a year? Compute this number!
## Assign it to a suitably named variable and print the result.
secondsInYear <- 365 * 24 * 60 * 60
## 1.1.3
## What is your age in seconds? Use the variable my_age computed above
## and seconds-in-year variable you just created, and compute it.
ageInSeconds <- 6911 * 86400
# 1.1.4
## How many seconds is a typical human lifetime?
secondsInHumanLifetime <- 2240543592
##
## Note: it is your task to find a good value for “typical” human lifetime!
## The rest of the questions are in the pdf. You do not need to copy the questions
## here, but please add the question numbers!
## Good luck!
## 1.2 Computing-------------------------------------------------------------------------
speedOfLight <- 300000
## 1.
lorentz <- function(m0, velocity){
calculatMass <- m0/sqrt(1 - (velocity^2/speedOfLight^2))
}
##2.yes you can get a positive number here
studentLorentz <- lorentz(60, 0.01)
print("studentLorentz")
## [1] "studentLorentz"
print("regular precision")
## [1] "regular precision"
print(studentLorentz)
## [1] 60
print("------")
## [1] "------"
print("regular precision - 60")
## [1] "regular precision - 60"
print(studentLorentz - 60)
## [1] 3.552714e-14
print("------")
## [1] "------"
#3.
#massProton <- (1.672 * 10^-27)/(sqrt(1 - (270000/speedOfLight^2)))
print("relativisticProtonMass")
## [1] "relativisticProtonMass"
relativisticProtonMass <- lorentz(1.672e-27, 270000)
print(relativisticProtonMass)
## [1] 3.835831e-27
print("------")
## [1] "------"
#4
print("MassRatio")
## [1] "MassRatio"
massRatio <- relativisticProtonMass/(1.672e-27)
print(massRatio)
## [1] 2.294157
print("------")
## [1] "------"
#EXTRA CREDIT
#There is a problem with computing the running student’s mass–namely, the mass difference is close
#to the numerical precision of standard 64 floating point numbers. Use high-precision math library
#to repeat the computations. How much different are your results when using ordinary math and
#when using high-precision math (i.e. more than 64 bits precision)? (Please print both results, not
#just their difference!) NB! Ensure that you don’t do just computations with high precision,
#but you enter the numeric data in high precision as well!
speedOfLight <- mpfr(300000, precBits = 120)
highLorentz <- function(m0, velocity){
# velocity <- mpfr(velocity, 120) # Converting velocity to high precision
calculatMass <- m0 / sqrt(1 - (velocity^2 / speedOfLight^2))
return(calculatMass)
}
d10 <- mpfr(10, 120)
highStudentLorentz <- highLorentz(60, 1/d10^2)
print("high precision")
## [1] "high precision"
print(highStudentLorentz)
## 1 'mpfr' number of precision 120 bits
## [1] 60.000000000000033333333333333361111114
print("-------------")
## [1] "-------------"
highRelativisticProtonMass <- highLorentz(1672/d10^30, mpfr(270000, precBits = 120))
print("highRelativisticProtonMass")
## [1] "highRelativisticProtonMass"
print(highRelativisticProtonMass)
## 1 'mpfr' number of precision 120 bits
## [1] 3.8358310703157927259685441457964617803e-27
print("------")
## [1] "------"
highMassRatio <- mpfr(highRelativisticProtonMass/(1672/d10^30), 120)
print("HighmassRatio")
## [1] "HighmassRatio"
print(highMassRatio)
## 1 'mpfr' number of precision 120 bits
## [1] 2.2941573387056176590720957809787450834
print("--------------------")
## [1] "--------------------"
##take the difference
print("differenceHighLowRunningStudent")
## [1] "differenceHighLowRunningStudent"
differenceHighLowRunningStudent <- highStudentLorentz - studentLorentz
print(differenceHighLowRunningStudent)
## 1 'mpfr' number of precision 120 bits
## [1] -2.1938034546716481824421819689417037457e-15
print("------")
## [1] "------"
print("differenceHighLowProton")
## [1] "differenceHighLowProton"
differenceHighLowProton <- highRelativisticProtonMass - relativisticProtonMass
print(differenceHighLowProton)
## 1 'mpfr' number of precision 120 bits
## [1] -3.7173028537156110588800005332390650722e-43
print("------")
## [1] "------"
##take the difference running student and low and high percision and then do the same for the proton
##1.
I_am_old <- ageInSeconds > 600000000
print("-----------------------")
## [1] "-----------------------"
print("I_am_old")
## [1] "I_am_old"
print(I_am_old)
## [1] FALSE
##2.Are you more than 550M seconds and less than 750M seconds old? Write a logical expression
#that checks both conditions and uses logical and to test if both conditions are right.
logicalExpression <- ageInSeconds > 550000000 && ageInSeconds < 750000000
##3.Are you more than 500M second old, but not more than 700M seconds old? Write a logical
#expression that checks both condition, and inverts the second one using logical not.
logicalNotExpression <- ageInSeconds > 500000000 && ageInSeconds >! 700000000
#1. download stringr package
## I was able to download the stringr package by going to the console and typing
##"install.packages("stringr")"
##2. then loading the package by:
library("stringr")
## Warning: package 'stringr' was built under R version 4.3.3
##3.
template <- "Hello, my name is <name>."
##4.
name <- "Reasha"
##5.
intro <- str_replace(template, "<name>.", name)
print(intro)
## [1] "Hello, my name is Reasha"
##6.
intro <- str_c(intro, " and I am ", my_age, " years old,")
print(intro)
## [1] "Hello, my name is Reasha and I am 18 years old,"
#7.
loud_intro <- toupper(intro)
print(loud_intro)
## [1] "HELLO, MY NAME IS REASHA AND I AM 18 YEARS OLD,"
#8.
count <- str_count(intro, "e")
##1.
for(number in 1:10) {
cat(number, "\n", sep = "")
}
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9
## 10
##2.
for(number in 1:10) {
cat(number^2, "\n", sep = "")
}
## 1
## 4
## 9
## 16
## 25
## 36
## 49
## 64
## 81
## 100
##3.
for(number in 1:10) {
cat(number, "^2 = ", number^2, "\n", sep = "")
}
## 1^2 = 1
## 2^2 = 4
## 3^2 = 9
## 4^2 = 16
## 5^2 = 25
## 6^2 = 36
## 7^2 = 49
## 8^2 = 64
## 9^2 = 81
## 10^2 = 100
##4.
total_sum <- 0
for (number in 1:100) {
total_sum <- total_sum + number
}
cat(total_sum, "\n", sep = "")
## 5050
##5.
factorial_result <- 1
# Loop through numbers from 1 to 7 and multiply them to compute factorial
for (i in 1:7) {
factorial_result <- factorial_result * i
}
# Print the factorial result
cat(factorial_result, "\n", sep = "")
## 5040
##6.
for (i in 10:1) {
result <- 1
for (j in i:1) {
result <- result * j
}
cat(i, "! =", result, "\n")
}
## 10 ! = 3628800
## 9 ! = 362880
## 8 ! = 40320
## 7 ! = 5040
## 6 ! = 720
## 5 ! = 120
## 4 ! = 24
## 3 ! = 6
## 2 ! = 2
## 1 ! = 1
##1.
greeting <- function(name, city){
return(paste("Hi, I am", name, "and I am from", city))
}
##2.
##display results
result <- greeting("Reasha", "Washington")
##3.
greeting <- function(name, city = "Seattle") {
return(paste("Hi, I am", name, "and I am from", city))
}
#without using city
withoutCity <- greeting("Reasha")
print(withoutCity)
## [1] "Hi, I am Reasha and I am from Seattle"
#different city
difCity <- greeting("Reasha", "Spokane")
print(difCity)
## [1] "Hi, I am Reasha and I am from Spokane"
##4.
removeDigits <- function(strings) {
removeStrings <- gsub("[0-9]", "", strings)
return(removeStrings)
}
#test it
exampleStrings <- c("INFO 201", "CSE 142", "mps-803c", "K2-team '21")
result <- removeDigits(exampleStrings)
print(result)
## [1] "INFO " "CSE " "mps-c" "K-team '"