TransWikia.com

Call part of a replaced list

Mathematica Asked on May 18, 2021

I have what I believe to be a very simple problem. I want to use an equation which depends on the values inside a list, but to be able to evaluate that equation with different lists. So:

T = Sum[A + b[[i]], {i, 10}];

I then want to be able to evaluate this with different sets of values for A and the list b. But if I try

Vals1 = {A-> 1, b-> Table[j^2,{j,10}]};
T//.Vals1

I get the correct answer, but I also get the error code "Part specification b[[1]] is longer than depth of object."

What am I getting wrong?

One Answer

Clear["Global`*"]

Define T with arguments and require b to be a List

T[A_, b_List] := Sum[A + b[[i]], {i, 10}];

Vals1 = {A -> 1, b -> Table[j^2, {j, 10}]};

T[A, b] /. Vals1

(* 395 *)

T[1, Range[10]^2]

(* 395 *)

Correct answer by Bob Hanlon on May 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP