5
$\begingroup$

Suppose I have the following lists,

as = {1, 2, 3, 4};
bs = {10, 20, 30, 40};

and the following expression:

a1*(b1 - a1) + a2*(b2 - a2) + a3*(b3 - a3) + a4*(b4 - a4)

I want to substitute the elements of my lists into my expression such that a1 -> 1, a2 -> 2, and so on and b1 -> 10 and b2 -> 20 and so on. I wonder how to do this efficiently?

$\endgroup$
6
$\begingroup$

Try Dot:

Dot[as, bs - as]
270
aa = Array[Subscript[a, #] &, 4];
bb = Array[Subscript[b, #] &, 4];

Dot[aa, bb - aa] // TeXForm

$a_1 \left(b_1-a_1\right)+a_2 \left(b_2-a_2\right)+a_3 \left(b_3-a_3\right)+a_4 \left(b_4-a_4\right)$

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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