5
$\begingroup$

So I have the following formula

I have found out that

c={0.308573, 0.404507, 0.356427, 0.652755, 0.402941}

I was just wondering if there is a easier way of computing this instead of having the following: Because when the time period gets bigger its going to take a while to write.

\[Beta]^0*Log[c[[1]]] + \[Beta]^1*Log[c[[2]]] + \[Beta]^2*
  Log[c[[3]]] + \[Beta]^3*Log[c[[4]]] + \[Beta]^4*Log[c[[5]]]
$\endgroup$

4 Answers 4

10
$\begingroup$

Use scalar product of lists here

Beta^(Range[5] - 1) .Log[c]

Because Attributes are Listable

Attributes[{Log, Power}] // TableForm
$\endgroup$
2
  • $\begingroup$ +1 I think Beta^Range[0, 4] . Log[c] is slightly more efficient (measured by RepeatedTiming). $\endgroup$
    – Bob Hanlon
    Commented Apr 4, 2022 at 13:06
  • 2
    $\begingroup$ More generally, Beta^(Range[0,Length[c] - 1]) . Log[c] $\endgroup$ Commented Apr 4, 2022 at 14:05
5
$\begingroup$

While the answer of @Akku14 is certainly fine, especially as modified by @"Daniel Lichblau", the following might be more communicative in your context:

u[c_, t_] := \[Beta]^(t - 1) Log[c]
Sum[u[c[[t]], t], {t, Length[c]}]
$\endgroup$
4
$\begingroup$

It should perhaps be noted that the OP is evaluating a polynomial in β, and thus, one should use Horner's method for evaluating it; the built-in function that can do this happens to be named FromDigits[]:

c = {0.308573, 0.404507, 0.356427, 0.652755, 0.402941};
FromDigits[Reverse[Log[c]], β] // Expand
   -1.1758 - 0.905086 β - 1.03163 β^2 - 0.426553 β^3 - 0.908965 β^4
$\endgroup$
3
  • 1
    $\begingroup$ Or if the polynomial is already put together by, say, @DanielLichtblau 's approach: Beta^(Range[0, Length[c] - 1]) . Log[c] // HornerForm. Does FromDigits actually give Horner's method? It looks like it only partially does so as FromDigits[Reverse[Log[c]], β] results in -1.1758 - 0.905086 Beta + (-1.03163 - 0.426553 Beta) Beta^2 - 0.908965 Beta^4 rather than -1.1758 + Beta (-0.905086 + Beta (-1.03163 + (-0.426553 - 0.908965 Beta) Beta)). $\endgroup$
    – JimB
    Commented May 17, 2022 at 21:18
  • $\begingroup$ @Jim, it's supposed to do Horner (at least in the numerical case), but it looks like it's trying to be clever with symbolic arguments. $\endgroup$ Commented May 17, 2022 at 23:37
  • 1
    $\begingroup$ Yep. What one sees and what's behind the curtain can be totally different. Mathematica is no different. $\endgroup$
    – JimB
    Commented May 18, 2022 at 0:21
1
$\begingroup$

MapIndexed is another possibility (but not as efficient as the dot product method)

MapIndexed[(Beta^(First@#2-1)) Log[#1]&, c]//Total

In Operator form:

MapIndexed[(Beta^(First@#2-1)) Log[#1]&]@c//Total
$\endgroup$
1
  • $\begingroup$ This is also Inner ('a generalization of Dot'): Inner[(Beta^#2) Log[#1]&, c, Range[0,Length@c-1], Plus] $\endgroup$
    – user1066
    Commented Apr 5, 2022 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.