Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #set of inputs
  2. E1 <- function (x0, x1, x2) 0.3 * x0 + -0.2 * x1 + 10 * x2 + -71.4;
  3. E2 <- function (x0, x1, x2) 3 * x0 + -0.2 * x2 + -0.1 * x1 + -7.85;
  4. E3 <- function (x0, x1, x2) 0.1 * x0 + 7 * x1 + -0.3 * x2 + 19.3;
  5. system <- list(E1, E2, E3);
  6.  
  7. CreateCoeffMatrix <- function (input) { #create a blank coefficient matrix with rownames
  8. coeffMatrix = matrix (0, nrow=length(input), ncol=length(input)+1, byrow=TRUE)
  9. rownames = c()
  10. for(i in 1:nrow(coeffMatrix)) {
  11. rownames[i] = i
  12. }
  13. rownames(coeffMatrix) <- rownames
  14. return(coeffMatrix)
  15. }
  16.  
  17. AugCoeffMatrix <- function (system) { #create the augmented coefficient matrix
  18. augcoeffmatrix = CreateCoeffMatrix(system)
  19. variables = c() #initially empty list of variables
  20.  
  21. for (i in 1:length(system)) { #parse the variables
  22. ex = deparse(system[i])[1]
  23. ex = gsub("list", "", ex) #remove excess characters
  24. ex = gsub("function", "", ex) #remove excess characters
  25. ex = gsub("[\\(\\) ]", "", ex, ",") #remove excess characters
  26. ex = strsplit(ex, ",") #split variables by commas
  27. varlist = unlist(ex) #store in varlist
  28. for (j in 1:length(varlist)) { #check variables if already in variable list. if not, store in variables
  29. if (varlist[j] %in% variables == FALSE) {
  30. variables = c(variables, varlist[j])
  31. }
  32. }
  33. }
  34.  
  35. if (length(variables) != length(system)){ #return NA if the number of variables is not equal to the number of equations
  36. return (NA)
  37. }
  38.  
  39. colnames = variables
  40. colnames = c(colnames, "RHS")
  41. colnames(augcoeffmatrix) <- colnames #provide column names for the augmented coefficient matrix
  42.  
  43. vals <- list()
  44. eqList = c()
  45.  
  46. for (i in 1:length(system)) { #parse expressions
  47. eq = deparse(system[i])[2]
  48. eqList = c(eqList, eq) #copy of equations
  49. eq = gsub("\\*", "", eq) #remove excess characters
  50. eq = gsub("\\s", "", eq) #remove excess characters
  51. eq = gsub("\\(", "", eq) #remove excess characters
  52. eq = gsub("\\)", "", eq) #remove excess characters
  53. eq = gsub("x[0-9]", "", eq) #remove excess characters
  54. eq = strsplit(eq, "\\+") #split by the + sign
  55. vals = c(vals, eq) #store numbers in vals
  56. }
  57. for (i in 1:nrow(augcoeffmatrix)) { #store coefficients in augcoeffmatrix
  58. nums = as.numeric(unlist(vals[[i]])) #gets each coefficient then turn it into a numeric typing
  59. numReference = unlist(vals[[i]]) #gets each coefficient as a string for string manipulation
  60. for (j in 1:length(variables)) {
  61. locVar = regexpr(numReference[j], eqList[i]) #gets location of coefficient in the expression
  62. varLen = attr(locVar, "match.length") #gets the length of the coefficient
  63. varEx = substr(eqList[i], locVar[1]+varLen+3, locVar[1]+varLen+4) #gets the corresponding variable to the coefficient
  64.  
  65. for(k in 1:ncol(augcoeffmatrix)){ #store in augcoeffmatrix
  66. if (varEx == colnames[k]) { #if the coefficient's variable is the same as the matrix column, store
  67. augcoeffmatrix[i,k] = nums[j]
  68. }
  69. if (k == ncol(augcoeffmatrix)) { #if the value is the last element, it is the constant. negate and add to the RHS column
  70. augcoeffmatrix[i,k] = -nums[k]
  71. }
  72. }
  73. }
  74. }
  75. returnList = list(augcoeffmatrix, variables) #lists the return values
  76. return(returnList)
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement