title: Polynomials as functions in R
body: <p>Sorry in advance if the question is stupid/was answered somewhere else/... I could not find any nice solution.</p>
  
  <p>Based on the idea of power series I have</p>
  
  <p>A) a vector of real coefficients of lengths n which comes from an other loop and which can be rather long, but lets assume it is simple, for instance,</p>
  
  <pre><code>a&lt;-1:10
  </code></pre>
  
  <p>and </p>
  
  <p>B) a real center, e.g. </p>
  
  <pre><code>c&lt;-3
  </code></pre>
  
  <p>I would like to define the polynomial (in my example)</p>
  
  <pre><code>a[1]+a[2]*(x-3)+ a[3]*(x-3)^2+ .... + a[10]*(x-3)^9 
  </code></pre>
  
  <p>as a function. Unfortunately</p>
  
  <p>1) the function <code>as.polynomial(a)</code> only allows center 0 (as far as I understand) so I cannot use it and</p>
  
  <p>2) the list of coefficients can be long, too long to do it by hand</p>
  
  <p>3) I might later ever need a multivariable version.</p>
  
  <p>I would prefer to use a loop to define this "finite power series" but I do not know how loops and sums of functions can be realized in a clean fashion (and I did not find it either).</p>
  
  <p>Something like (very naive)</p>
  
  <p><code>t&lt;-function(x) 0
  for(i in 1:length(a)) 
  {t&lt;-function(x) {t(x) + a[i]*(x-c)^(i-1}}</code></p>
  
  <p>Thanks so much for your help.</p>