Title: | Optimization Toolbox for Solving Linear Systems |
---|---|
Description: | Solves linear systems of form Ax=b via Gauss elimination, LU decomposition, Gauss-Seidel, Conjugate Gradient Method (CGM) and Cholesky methods. |
Authors: | Prakash (PKS Prakash) <[email protected]> |
Maintainer: | Prakash <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.2.5 |
Built: | 2024-10-31 20:34:28 UTC |
Source: | CRAN |
Function utilizes the Conjugate Gradient Method for optimization to solve equation Ax=b
cgm(A, b, x = NULL, iter = 500, tol = 1e-07)
cgm(A, b, x = NULL, iter = 500, tol = 1e-07)
A |
: Input matrix |
b |
: Response vector |
x |
: Initial solutions |
iter |
: Number of Iterations |
tol |
: Convergence tolerance |
optimal : Optimal solutions
initial : initial solution
itr.conv : Number of iterations for convergence
conv : Convergence array
A<-matrix(c(4,-1,1, -1,4,-2,1,-2,4), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(12,-1, 5), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="cgm", iter=500, tol=1e-7)
A<-matrix(c(4,-1,1, -1,4,-2,1,-2,4), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(12,-1, 5), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="cgm", iter=500, tol=1e-7)
Function perform choleski decomposition for positive definate matrix (A=LL')
choleskiDecomposition(A, tol = 1e-07)
choleskiDecomposition(A, tol = 1e-07)
A |
:Input Matrix |
tol |
: Tolerance |
L: Decomposition matrix
A<-matrix(c(4,-2,2, -2,2,-4,2,-4,11), nrow=3,ncol=3, byrow = TRUE) chdA<-choleskiDecomposition(A)
A<-matrix(c(4,-2,2, -2,2,-4,2,-4,11), nrow=3,ncol=3, byrow = TRUE) chdA<-choleskiDecomposition(A)
Function fits a linear model using Choleski Decomposition for positive definate matrix
choleskilm(A, b, tol = 1e-07)
choleskilm(A, b, tol = 1e-07)
A |
: Input matrix |
b |
: Response matrix |
tol |
: Tolerance |
U : Upper part of the triangele is (U) and Lower part of the triangular is L (Diagnoal for the L matrix is 1)
c : Lc=b (where Ux=c)
beta : Estimates
examples A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="choleski") # Solve Linear model using Gauss Elimination
Function to solve linear system using backsubsitution using Upper Triangular Matrix (Ux=c)
forwardsubsitution.optR(L, b)
forwardsubsitution.optR(L, b)
L |
: Lower triangular matrix |
b |
: response |
y : Estiamted value
Function utilizes the Gauss-Seidel optimization to solve equation Ax=b
gaussSeidel(A, b, x = NULL, iter = 500, tol = 1e-07, w = 1, witr = NULL)
gaussSeidel(A, b, x = NULL, iter = 500, tol = 1e-07, w = 1, witr = NULL)
A |
: Input matrix |
b |
: Response |
x |
: Initial solutions |
iter |
: Number of Iterations |
tol |
: Convergence tolerance |
w |
: Relaxation paramter used to compute weighted avg. of previous solution. w=1 represent no relaxation |
witr |
: Iteration after which relaxation parameter become active |
optimal : Optimal solutions
initial : initial solution
relaxationFactor : relaxation factor
A<-matrix(c(4,-1,1, -1,4,-2,1,-2,4), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(12,-1, 5), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gaussseidel", iter=500, tol=1e-7)
A<-matrix(c(4,-1,1, -1,4,-2,1,-2,4), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(12,-1, 5), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gaussseidel", iter=500, tol=1e-7)
Function hatMatrix determines the projection matrix for X from the form yhat=Hy. The projection matrix defines the influce of each variable on fitted value The diagonal elements of the projection matrix are the leverages or influence each sample has on the fitted value for that same observation. The projection matrix is evaluated with I.I.D assumtion ~N(0, 1)
hatMatrix(X, covmat = NULL)
hatMatrix(X, covmat = NULL)
X |
: Input Matrix |
covmat |
: covariance matrix for error, if the error are correlated for I.I.D covmat will be NULL matrix |
X: Projection Matrix
X<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) covmat <- matrix(rnorm(3 * 3), 3, 3) H<-hatMatrix(X) H<-hatMatrix(X, covmat) diag(H)
X<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) covmat <- matrix(rnorm(3 * 3), 3, 3) H<-hatMatrix(X) H<-hatMatrix(X, covmat) diag(H)
function invert a matrix A using LU decomposition such that A*inv(A)=I
inv.optR(A, tol = 1e-07)
inv.optR(A, tol = 1e-07)
A |
: Input matrix |
tol |
: tolerance |
A : Inverse of Matrix A
# Invert the matrix using LU decomposition A<-matrix(c(0.6,-0.4,1, -0.3,0.2,0.5,0.6,-1,0.5), nrow=3,ncol=3, byrow = TRUE) InvA<-inv.optR(A)
# Invert the matrix using LU decomposition A<-matrix(c(0.6,-0.4,1, -0.3,0.2,0.5,0.6,-1,0.5), nrow=3,ncol=3, byrow = TRUE) InvA<-inv.optR(A)
jacobian is function to determine the jacobian matrix for function f for input x
jacobian(f, x)
jacobian(f, x)
f |
: function to optimize |
x |
: Initial Solution |
jacobiabMatrix: Jacobian matrix
f0: Intial solution
The function decomposes matrix A into LU with L lower matrix and U as upper matrix
LU.decompose(A, tol = 1e-07)
LU.decompose(A, tol = 1e-07)
A |
: Input Matrix |
tol |
: tolerance |
A : Transformed matrix with Upper part of the triangele is (U) and Lower part of the triangular is L (Diagnoal for the L matrix is 1)
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) Z<-optR(A, tol=1e-7, method="LU")
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) Z<-optR(A, tol=1e-7, method="LU")
The function solves Ax=b using LU decomposition (LUx=b). The function handles multple responses
LU.optR(A, b, tol = 1e-07)
LU.optR(A, b, tol = 1e-07)
A |
: Input Matrix |
b |
: Response |
tol |
: tolerance |
U : Upper part of the triangele is (U) and Lower part of the triangular is L (Diagnoal for the L matrix is 1)
c : Lc=b (where Ux=c)
beta : Estimates
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(0,0, 1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="LU")
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(0,0, 1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="LU")
function to extract Lower and Upper matrix from LU decomposition
LUsplit(A)
LUsplit(A)
A |
: Input matrix |
U : upper triangular matrix
L : Lower triangular matrix
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) Z<-optR(A, method="LU") LUsplit(Z$U)
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) Z<-optR(A, method="LU") LUsplit(Z$U)
function to remove the machine precision error
machinePrecision(A)
machinePrecision(A)
A |
: Input matrix |
A : return matrix
newtonRapson function perform optimization
newtonRapson(f, x, iteration = 30, tol = 1e-09)
newtonRapson(f, x, iteration = 30, tol = 1e-09)
f |
: function to optimize |
x |
: Initial Solution |
iteration |
: Iterations |
tol |
: Tolerance |
x : optimal roots
Function for non-diagnoal multipication
nonDiagMultipication(i, A, beta)
nonDiagMultipication(i, A, beta)
i |
: Column Index of Matrix A |
A |
: Input matrix |
beta |
: Response |
asum: Non-diagnol contribution
Function re-order the matrix to make matrix pivot.diag at each iteration
opt.matrix.reorder(A, tol = 1e-16)
opt.matrix.reorder(A, tol = 1e-16)
A |
: Input Matrix |
tol |
: tolerance |
A : Updated Matrix
b.order : Order sequence of A updated matrix
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(0,0, 1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss")
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(0,0, 1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss")
optR function for solving linear systems using numerical approaches. Current toolbox supports Gauss Elimination, LU decomposition, Conjugate Gradiant Decent and Gauss-Sideal methods for solving the system of form AX=b For optimization using numerical methods cgm method performed faster in comparision with gaussseidel. For decomposition LU is utilized for multiple responses to enhance the speed of computation.
optR(x, ...)
optR(x, ...)
x |
: Input matrix |
... |
: S3 method |
optR : Return optR class
PKS Prakash
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using Gauss Elimination # Solve Linear model using LU decomposition (Supports Multi-response) Z<-optR(A, b, method="LU") # Solve the matrix using Gauss Elimination (1, -1, 2) A<-matrix(c(2,-2,6, -2,4,3,-1,8,4), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(16,0, -1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using Gauss Elimination require(utils) set.seed(129) n <- 10 ; p <- 4 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) Z<-optR(X, y, method="cgm")
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using Gauss Elimination # Solve Linear model using LU decomposition (Supports Multi-response) Z<-optR(A, b, method="LU") # Solve the matrix using Gauss Elimination (1, -1, 2) A<-matrix(c(2,-2,6, -2,4,3,-1,8,4), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(16,0, -1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using Gauss Elimination require(utils) set.seed(129) n <- 10 ; p <- 4 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) Z<-optR(X, y, method="cgm")
Function to solve linear system using backsubsitution using Upper Triangular Matrix (Ux=c)
## S3 method for class 'backsubsitution' optR(U, c)
## S3 method for class 'backsubsitution' optR(U, c)
U |
: Upper triangular matrix |
c |
: response |
beta : Estiamted value
soptR is the default function for optimization
## Default S3 method: optR(x, y = NULL, weights = NULL, method = c("gauss", "LU", "gaussseidel", "cgm", "choleski"), iter = 500, tol = 1e-07, keep.data = TRUE, ...)
## Default S3 method: optR(x, y = NULL, weights = NULL, method = c("gauss", "LU", "gaussseidel", "cgm", "choleski"), iter = 500, tol = 1e-07, keep.data = TRUE, ...)
x |
: Input data frame |
y |
: Response is data frame |
weights |
: Observation weights |
method |
: "gauss" for gaussian elimination and "LU" for LU factorization |
iter |
: Number of Iterations |
tol |
: Convergence tolerance |
keep.data |
: Returns Input dataset in object |
... |
: S3 Class |
U : Decomposed matrix for Gauss-ELimination Ax=b is converted into Ux=c where U is upper triangular matrix for LU decomposition U contain the values for L & U decomposition LUx=b
c : transformed b & for LU transformation c is y from equation Ux=y
estimates : Return x values for linear system
seq : sequence of A matrix re-ordered
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using LU decomposition (Supports Multi-response) Z<-optR(A, b, method="LU") # Solving the function using numerical method Z<-optR(A, b, method="cgm") require(utils) set.seed(129) n <- 7 ; p <- 2 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) Z<-optR(X, y, method="LU")
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using LU decomposition (Supports Multi-response) Z<-optR(A, b, method="LU") # Solving the function using numerical method Z<-optR(A, b, method="cgm") require(utils) set.seed(129) n <- 7 ; p <- 2 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) Z<-optR(X, y, method="LU")
optR.fit is fit function for determing x for System with form Ax=b
## S3 method for class 'fit' optR(x, y = NULL, method = c("gauss, LU, gaussseidel", "cgm"), iter = 500, tol = 1e-07, ...)
## S3 method for class 'fit' optR(x, y = NULL, method = c("gauss, LU, gaussseidel", "cgm"), iter = 500, tol = 1e-07, ...)
x |
: Input matrix |
y |
: Response is matrix |
method |
: "gauss" for gaussian elimination and "LU" for LU factorization |
iter |
: Number of Iterations |
tol |
: Convergence tolerance |
... |
: S3 Class |
U : Decomposed matrix for Gauss-ELimination Ax=b is converted into Ux=c where U is upper triangular matrix for LU decomposition U contain the values for L & U decomposition LUx=b
c : transformed b & for LU transformation c is y from equation Ux=y
estimates : Return x values for linear system
seq : sequence of A matrix re-ordered
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using LU decomposition (Supports Multi-response) Z<-optR(A, b, method="LU") # Solving the function using numerical method Z<-optR(A, b, method="cgm")
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") # Solve Linear model using LU decomposition (Supports Multi-response) Z<-optR(A, b, method="LU") # Solving the function using numerical method Z<-optR(A, b, method="cgm")
optR package to perform the optimization using numerical methods
## S3 method for class 'formula' optR(formula, data = list(), weights = NULL, method = c("gauss, LU, gaussseidel", "cgm", "choleski"), iter = 500, tol = 1e-07, keep.data = TRUE, contrasts = NULL, ...)
## S3 method for class 'formula' optR(formula, data = list(), weights = NULL, method = c("gauss, LU, gaussseidel", "cgm", "choleski"), iter = 500, tol = 1e-07, keep.data = TRUE, contrasts = NULL, ...)
formula |
: formula to build model |
data |
: data used to build model |
weights |
: Observation weights |
method |
: "gauss" for gaussian elimination and "LU" for LU factorization |
iter |
: Number of Iterations |
tol |
: Convergence tolerance |
keep.data |
: If TRUE returns input data |
contrasts |
: Data frame contract values |
... |
: S3 Class |
U : Decomposed matrix for Gauss-ELimination Ax=b is converted into Ux=c where U is upper triangular matrix for LU decomposition U contain the values for L & U decomposition LUx=b
c : transformed b & for LU transformation c is y from equation Ux=y
estimates : Return x values for linear system
PKS Prakash
# Solving equation Ax=b b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) Z<-optR(b~A-1, method="gauss") # -1 to remove the constant vector Z<-optR(b~A-1, method="LU") # -1 to remove the constant vector require(utils) set.seed(129) n <- 10 ; p <- 4 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) data<-cbind(X, y) colnames(data)<-c("var1", "var2", "var3", "var4", "y") Z<-optR(y~var1+var2+var3+var4+var1*var2-1, data=data.frame(data), method="cgm")
# Solving equation Ax=b b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) Z<-optR(b~A-1, method="gauss") # -1 to remove the constant vector Z<-optR(b~A-1, method="LU") # -1 to remove the constant vector require(utils) set.seed(129) n <- 10 ; p <- 4 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) data<-cbind(X, y) colnames(data)<-c("var1", "var2", "var3", "var4", "y") Z<-optR(y~var1+var2+var3+var4+var1*var2-1, data=data.frame(data), method="cgm")
Function solves linear systems using Gauss Elimination. The function solves equation of form Ax=b to Ux=c (where U is upper triangular matrix)
## S3 method for class 'gauss' optR(A, b, tol = 1e-07)
## S3 method for class 'gauss' optR(A, b, tol = 1e-07)
A |
: Input Matrix |
b |
: Response |
tol |
: Tolerance |
method |
: To be used to perform factorization |
U : Upper triangular matrix
c : Transformed b
beta : Estimates
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(0,0, 1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss")
A<-matrix(c(0,-1,1, -1,2,-1,2,-1,0), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(0,0, 1), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss")
Function esimates the lambda or multiplier factor for Elimination using the pivot row/column
## S3 method for class 'multiplyfactor' optR(rowindex, A, pivotindex)
## S3 method for class 'multiplyfactor' optR(rowindex, A, pivotindex)
rowindex |
: Row Index for the row to be used |
A |
: Input matrix |
pivotindex |
: Column index for the pivot |
lambda : Lambda
Function based optimization module
optRFun(formula, x0, iteration = 30, method = c("newtonrapson"), tol = 1e-09)
optRFun(formula, x0, iteration = 30, method = c("newtonrapson"), tol = 1e-09)
formula |
: Function to optimize |
x0 |
: Initial Solution |
iteration |
: Number of Iterations |
method |
: Method for solving the optimization |
tol |
: Tolerance |
optRFun : Optimal Solution class
Newton Rapshon based optimization
optRFun.newtonRapson(formula, x0, iteration = 30, tol = 1e-09)
optRFun.newtonRapson(formula, x0, iteration = 30, tol = 1e-09)
formula |
: Function to optimize |
x0 |
: Initial Solution |
iteration |
: Number of Iterations |
tol |
: Tolerance |
optRFun : Optimal Solution
Function for making predictions using OptR class
## S3 method for class 'optR' predict(object, newdata, na.action = na.pass, ...)
## S3 method for class 'optR' predict(object, newdata, na.action = na.pass, ...)
object |
: optR class fitted object |
newdata |
: data for prediction |
na.action |
: action for missing values |
... |
: S3 class |
fitted.val : Predicted values
terms : terms used for fitting
optR is the default function for optimization
## S3 method for class 'optR' print(x, ...)
## S3 method for class 'optR' print(x, ...)
x |
: Input of optR class |
... |
: S3 class |
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") print(Z)
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="gauss") print(Z)
summary function generates the summary for the optR class
## S3 method for class 'optR' summary(object, ...)
## S3 method for class 'optR' summary(object, ...)
object |
: Input of optR class |
... |
: S3 method |
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="cgm") summary(Z)
# Solving equation Ax=b A<-matrix(c(6,-4,1, -4,6,-4,1,-4,6), nrow=3,ncol=3, byrow = TRUE) b<-matrix(c(-14,36, 6), nrow=3,ncol=1,byrow=TRUE) Z<-optR(A, b, method="cgm") summary(Z)