0
$\begingroup$

I have an array of functions in terms of a[0] through a[n] and I wish to replace the a[]'s with previously calculated values p[0] through p[n], as in below:

p = Table[Chop[NIntegrate[f[t]*wt[t]*Tn[t], {t, 0, 1}]/(Pi/2)], {n, 0, k}];
p[[1]] = p[[1]]/2;

s = Table[Coefficient[pmult[j], tstar[i], 1], {j, 0, k}, {i, 1, k}]

q3=s/.p

What is the best way of doing this? I have tried the Replace[], ReplaceAll[], and /. commands but none seem to be giving me what I want. I just want a[0] replaced with the number contained in p[0], a[1] replaced with p[1], etc.

Thanks in advance!

$\endgroup$
3
  • $\begingroup$ perhaps just q3 = s / a :> p $\endgroup$ Jan 24, 2014 at 17:49
  • 2
    $\begingroup$ @Belisarius I believe you have missed the point :-))) $\endgroup$
    – Peltio
    Jan 24, 2014 at 18:19
  • $\begingroup$ @Peltio The point was left as an exercise to the reader .) $\endgroup$ Jan 24, 2014 at 18:25

1 Answer 1

1
$\begingroup$

To use /., you have to supply a replacement Rule rather than just a value. On the left hand side of the Rule (indicated by "->") you supply a pattern; on the right hand side you supply an expression to substitute for the pattern.

In your target vector s, each element depends on symbols like a[i]. For example,

s = {f[1][a[1]], f[2][a[1],a[2]]}

You want to replace every a[i] with the corresponding element of the list p, p[[i]]. You need a list of replacement Rules to get them all at once:

s /. Table[a[i] -> p[[i]], {i, 2}]

or

s /. Thread[{a[1], a[2]} -> p]
$\endgroup$
1
  • $\begingroup$ That works perfectly! Thanks! $\endgroup$
    – gKirkland
    Jan 26, 2014 at 20:48

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.