문제

I have the following code:

nth <- expression(((1/p)*a0/2)+sum(((1/p)*a*cos(i*pi*x/p)))+sum((1/p)*b*sin(i*pi*x/p)))
  nth <- as.expression(gsub('pi',pi,nth))  
  nth <- as.expression(gsub('p',p,nth))
  nth <- as.expression(gsub('a0',a0,nth))
  nth <- as.expression(gsub('a',a,nth))
  nth <- as.expression(gsub('b',b,nth))

this results to the expression:

"((1/1) * 1.26424111790395/2) + sum(((1/1) * 0.251688909862584 * cos(i * 3.14159265358979 * x/1))) + sum((1/1) * -1.03501509824516e-16 * sin(i * 3.14159265358979 * x/1))"

What I want to do next is to evaluate i with a list (eg. i = 1:3) without evaluating x. So what I want to get is something like:

 "((1/1) * 1.26424111790395/2) + sum(((1/1) * 0.251688909862584 * cos(1 * 3.14159265358979 * x/1)), ((1/1) * 0.251688909862584 * cos(2 * 3.14159265358979 * x/1)), ((1/1) * 0.251688909862584 * cos(3 * 3.14159265358979 * x/1))) + sum(((1/1) * 0.251688909862584 * sin(1 * 3.14159265358979 * x/1)), ((1/1) * 0.251688909862584 * sin(2 * 3.14159265358979 * x/1)), ((1/1) * 0.251688909862584 * sin(3 * 3.14159265358979 * x/1)))"

How can I do this? Thanks.

도움이 되었습니까?

해결책

Why not try this. You will see that I wrapped the values you needed in a loop and put all output in a new "finished" dataframe that will be updated as you roll through the loop. You can specify how many i's there are and change the expression as you wish:

# Define the initial variables that might be changed here
var_1 <- 3.14159265358979 # This referred to pi in your initial expression
var_2 <- 1 # This referred to p in your initial expression
var_3 <- 1.26424111790395 # This refers to a0 in your initial expression
var_4 <- 0.251688909862584 # This refers to a in your initial expression
var_5 <- -1.03501509824516e-16 # This refers to b in your initial expression

n <- 3 # This is the number of equations that will be run through

# Create an empty dataframe to hold the outputted expressions
finished = c() # Empty data frame

# Create an array holding values from 1 to the number of n's that will be run through
cycle <- c(1:n)

# Convert cycle to a matrix
cycle <- as.matrix(cycle)

# The variable we will be changing is i ... Create the initial loop
for (i in 1:3 ) {
  nth <- expression(((1/p)*a0/2)+sum(((1/p)*a*cos(i*pi*x/p)))+sum((1/p)*b*sin(i*pi*x/p))) # Write the expression to be changed

  # Substitute in all the relevant values. Note that this is made to be more explicity
  nth <- as.expression(gsub('pi',var_1,nth))  
  nth <- as.expression(gsub('p',var_2,nth))
  nth <- as.expression(gsub('a0',var_3,nth))
  nth <- as.expression(gsub('a',var_4,nth))
  nth <- as.expression(gsub('b',var_5,nth))

  # I will also, for each value, substitue in relevant value from the cycle array
  # This will change the i values for you
  i_index <- cycle[i,1]
  i_index <- as.character(i_index)

  nth <- as.expression(gsub('i',i_index,nth)) # Append the nth equation

  # I will then bind this solution into the finished data frame to hold all solutions
  finished[i] = nth
}

This is the output that was generated after running the code:

expression("((1/1) * 1.26424111790395/2) + sum(((1/1) * 0.251688909862584 * cos(1 * 3.14159265358979 * x/1))) + sum((1/1) * -1.03501509824516e-16 * s1n(1 * 3.14159265358979 * x/1))", 
    "((1/1) * 1.26424111790395/2) + sum(((1/1) * 0.251688909862584 * cos(2 * 3.14159265358979 * x/1))) + sum((1/1) * -1.03501509824516e-16 * s2n(2 * 3.14159265358979 * x/1))", 
    "((1/1) * 1.26424111790395/2) + sum(((1/1) * 0.251688909862584 * cos(3 * 3.14159265358979 * x/1))) + sum((1/1) * -1.03501509824516e-16 * s3n(3 * 3.14159265358979 * x/1))")

다른 팁

This should get you going. You were definitely on the right track with gsub.
Notice that j in the original expression is being replaced with 1, 2, and 3, respectively. I went with j because gsub with i interferes with sin and pi. Hope it helps...

> expres <- "(((1/p)*a0/2)+sum(((1/p)*a*cos(j*pi*x/p)))+sum((1/p)*b*sin(j*pi*x/p)))"
> noquote(sapply(1:3, function(j){
      GS <- gsub("j", as.numeric(j), expres)
      paste0("expression(", GS, ")")
  }))
[1] expression((((1/p)*a0/2)+sum(((1/p)*a*cos(1*pi*x/p)))+sum((1/p)*b*sin(1*pi*x/p))))
[2] expression((((1/p)*a0/2)+sum(((1/p)*a*cos(2*pi*x/p)))+sum((1/p)*b*sin(2*pi*x/p))))
[3] expression((((1/p)*a0/2)+sum(((1/p)*a*cos(3*pi*x/p)))+sum((1/p)*b*sin(3*pi*x/p))))

Another possible solution is to use substitute

g = function(i){
    env = list(pi=pi, p=1, a0=1.26424111790395, a=0.251688909862584, b=-1.03501509824516e-16, i=i)
    as.character(as.expression(substitute(((1/p)*a0/2)+sum(((1/p)*a*cos(i*pi*x/p)))+sum((1/p)*b*sin(i*pi*x/p)), env)))
}
paste(lapply(1:3, g), collapse=", ")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top