Calculating areas.

APL (Dyalog Unicode), 9 bytes

⊃⊥∘⌽⊢÷⍳∘≢

Try it online!

A tacit function that takes C, a0, a1, ... as a single vector argument.

12 bytes if a full program is needed. Note that, while the default representation for a numeric vector is the numbers separated by spaces (no comma or anything), it accepts comma-separated ones since evals the input and , is the "concatenate" function.

(⊃⊥∘⌽⊢÷⍳∘≢)⎕

Try it online!

Uses ⎕IO←0 and ⎕DIV←1, so that the range generation is 0-based and division by zero gives 0.

Because the polynomial is nonnegative and the integral starts at 0, it suffices to evaluate the antiderivative of the given polynomial at C.

The antiderivative of \$a_0 + a_1x + a_2x^2 + \dots\$ is \$a_0x + \frac{a_1}{2} x^2 + \frac{a_2}{3} x^3 + \dots\$. Because we have C attached to the start of the vector, we simply divide the entire array by their 0-based indexes:

       C  a0   a1   a2
input: 3  0    0    2
div  : 0  1    2    3
res  : 0  0    0    2/3
       0  a0/1 a1/2 a2/3

Then we simply evaluate the resulting polynomial at C.

How it works: the code

⊃⊥∘⌽⊢÷⍳∘≢  ⍝ Input: single array of C, a0, a1, a2, ...
      ⍳∘≢  ⍝ 0-based range 0, 1, 2, 3, ...
    ⊢÷     ⍝ Divide self by above
  ∘⌽       ⍝ Reverse
⊃⊥         ⍝ Convert from base C to real number
           ⍝ i.e. evaluate the polynomial, lowest term given last

Python - 71 63 chars:

a=input()
print sum(1.*a[i]*a[0]**i/i for i in range(1,len(a)))

It's a simple integration of a polynomial function between 0 and C. And I haven't tested it, but I'm quite sure it works for negative values.


J, 26 characters

f=:3 :'((1}.y)&p.d._1)0{y'

e.g.

   f 2 0 1
2
   f 3 0 0 2
18

Tags:

Code Golf