#Derivatives

#Find the derivatives with respect to x

# 1.F(x|x???0)=1???e^?????x

deriv1 = D(expression(1-e^(-lambda*x)), "x")
deriv1
## e^(-lambda * x) * (log(e) * lambda)
# 2.F(x|b>a)=x???a/b???a

deriv2 = D(expression((x-a)/(b-a)),"x")
deriv2
## 1/(b - a)
# 3. F(x|a<x???c???b)=(x???a)^2/(b???a)*(c???a)

deriv3 = D(expression(((x-a)^2)/((b-a)*(c-a))), "x")
deriv3
## 2 * (x - a)/((b - a) * (c - a))
#4. F(x|a???c<x<b)=1???((b???x)^2/(b???a)*(c???a))

deriv4 = D(expression(1-(((b-x)^2)/((b-a)*(c-a)))), "x")
deriv4
## 2 * (b - x)/((b - a) * (c - a))
# Integrals

#Definite Integral

function1 <- function(x)
{
  3*(x^3)
}
integrate(function1,lower=0,upper = 10)
## 7500 with absolute error < 8.3e-11
#Indefinite Integral is calculated using anitDerivative
library(mosaicCalc)
## Loading required package: mosaicCore
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D
antiD(x*(lambda *(e^(-lambda*x))) ~ x)
## function (x, lambda, e, C = 0) 
## {
##     numerical_integration(.newf, .wrt, as.list(match.call())[-1], 
##         formals(), from, ciName = intC, .tol)
## }
## <environment: 0x0000000011844ca8>
# This returns error, Substituting the values of b=2, a =1, gives 0.5 as answer.
# Without substitution values for b,a returns error
#function3a <- function(x)
#{
 #  1/(b-a) * x^0
#}
#integrate(function3a,lower=0,upper = 0.5, stop.on.error = FALSE)

# Substituting values of b,a as 2,1 respectively
function3b <- function(x, b= 2, a =1)
{
   1/(b-a) * x^0
}
integrate(function3b,lower=0,upper = 0.5, stop.on.error = FALSE)
## 0.5 with absolute error < 5.6e-15
# Question 8, is tough, need more time to read the concepts of Probability distribution, I will try this again, once we cover those topics.
#Linear Algebra

# 1. Inverse using Gaussian row reduction of matrix A, matrix(c(1,2,3,3,3,1,4,6,8),3,3, byrow = TRUE )

A <- matrix(c(1,2,3,3,3,1,4,6,8),3,3, byrow = TRUE )
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    3    3    1
## [3,]    4    6    8
I <- diag(3)
I
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
library(matlib)
## Warning: package 'matlib' was built under R version 3.5.2
gaussianElimination(A,I,verbose = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    2    3    1    0    0
## [2,]    3    3    1    0    1    0
## [3,]    4    6    8    0    0    1
## 
## row: 1 
## 
##  exchange rows 1 and 3 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    4    6    8    0    0    1
## [2,]    3    3    1    0    1    0
## [3,]    1    2    3    1    0    0
## 
##  multiply row 1 by 0.25 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1  1.5    2    0    0 0.25
## [2,]    3  3.0    1    0    1 0.00
## [3,]    1  2.0    3    1    0 0.00
## 
##  multiply row 1 by 3 and subtract from row 2 
##      [,1] [,2] [,3] [,4] [,5]  [,6]
## [1,]    1  1.5    2    0    0  0.25
## [2,]    0 -1.5   -5    0    1 -0.75
## [3,]    1  2.0    3    1    0  0.00
## 
##  subtract row 1 from row 3 
##      [,1] [,2] [,3] [,4] [,5]  [,6]
## [1,]    1  1.5    2    0    0  0.25
## [2,]    0 -1.5   -5    0    1 -0.75
## [3,]    0  0.5    1    1    0 -0.25
## 
## row: 2 
## 
##  multiply row 2 by -0.6666667 
##      [,1] [,2]     [,3] [,4]       [,5]  [,6]
## [1,]    1  1.5 2.000000    0  0.0000000  0.25
## [2,]    0  1.0 3.333333    0 -0.6666667  0.50
## [3,]    0  0.5 1.000000    1  0.0000000 -0.25
## 
##  multiply row 2 by 1.5 and subtract from row 1 
##      [,1] [,2]      [,3] [,4]       [,5]  [,6]
## [1,]    1  0.0 -3.000000    0  1.0000000 -0.50
## [2,]    0  1.0  3.333333    0 -0.6666667  0.50
## [3,]    0  0.5  1.000000    1  0.0000000 -0.25
## 
##  multiply row 2 by 0.5 and subtract from row 3 
##      [,1] [,2]       [,3] [,4]       [,5] [,6]
## [1,]    1    0 -3.0000000    0  1.0000000 -0.5
## [2,]    0    1  3.3333333    0 -0.6666667  0.5
## [3,]    0    0 -0.6666667    1  0.3333333 -0.5
## 
## row: 3 
## 
##  multiply row 3 by -1.5 
##      [,1] [,2]      [,3] [,4]       [,5]  [,6]
## [1,]    1    0 -3.000000  0.0  1.0000000 -0.50
## [2,]    0    1  3.333333  0.0 -0.6666667  0.50
## [3,]    0    0  1.000000 -1.5 -0.5000000  0.75
## 
##  multiply row 3 by 3 and add to row 1 
##      [,1] [,2]     [,3] [,4]       [,5] [,6]
## [1,]    1    0 0.000000 -4.5 -0.5000000 1.75
## [2,]    0    1 3.333333  0.0 -0.6666667 0.50
## [3,]    0    0 1.000000 -1.5 -0.5000000 0.75
## 
##  multiply row 3 by 3.333333 and subtract from row 2 
##      [,1] [,2] [,3] [,4] [,5]  [,6]
## [1,]    1    0    0 -4.5 -0.5  1.75
## [2,]    0    1    0  5.0  1.0 -2.00
## [3,]    0    0    1 -1.5 -0.5  0.75
inv(A)
##      [,1] [,2]  [,3]
## [1,] -4.5 -0.5  1.75
## [2,]  5.0  1.0 -2.00
## [3,] -1.5 -0.5  0.75
#2. Determinant of A

det(A)
## [1] -4
#3. LU Decomposition for A

library(matrixcalc)
## Warning: package 'matrixcalc' was built under R version 3.5.2
## 
## Attaching package: 'matrixcalc'
## The following object is masked from 'package:matlib':
## 
##     vec
lu.decomposition(A)
## $L
##      [,1]      [,2] [,3]
## [1,]    1 0.0000000    0
## [2,]    3 1.0000000    0
## [3,]    4 0.6666667    1
## 
## $U
##      [,1] [,2]      [,3]
## [1,]    1    2  3.000000
## [2,]    0   -3 -8.000000
## [3,]    0    0  1.333333
#4. Multiply Matrix by Inverse

A %*% inv(A)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1