1
$\begingroup$

I´m struggeling now a few hours on a very basic problem. I want to assign the value 0 to variable i, if i has another value, then it should be changed to 0 in the function.

Function

 ClearAll["Global`*"]
 ExpandSum[summand_, range_List] :=  Total[(Function @@ {range[[1]],HoldForm[summand]}) /@ Array[range[[2]] - 1 + # &, (range[[3]] - range[[2]] + 1)]]

Input:(It works perfectly with that:)

 ExpandSum[i^4, {i, 3, 20}]

It works not with that:

 i = 10;
 ExpandSum[i^4, {i, 3, 20}]

I tried clear[i], but it doesnt work. i=0 leeds to "Cannot unset raw object 10). tried to set i_Object or i_Integer - no help.

Sorry for that very basic question but I´m stuck...

Many thanks!

$\endgroup$
3
  • $\begingroup$ It is not really clear what you want to achieve. First, Table is a built-in symbol, so you "cannot" change its definitions – you should use some other symbol. Second, I suppose you are not showing us your actual code with the actual problem. Please edit the question and include your code that will clearly show what is the input and what is the desired output. $\endgroup$
    – Domen
    May 3, 2023 at 12:08
  • $\begingroup$ @Domen I added the function. $\endgroup$
    – Roberta
    May 3, 2023 at 12:34
  • $\begingroup$ Please look up docs for Module and With. Set i inside the module (locally) to zero and it doesn't need to be an argument bringing in a global value to the function. $\endgroup$
    – Syed
    May 3, 2023 at 15:58

2 Answers 2

2
$\begingroup$

Roberta, this is essentially the same problem as Passing argument to a function

You seem to be overthinking these scenarios. You're trying to layer abstractions on top of the abstractions that already exist and could be used directly.

Consider your usage pattern:

 ExpandSum[i^4, {i, 3, 20}]

Now, just from a design perspective, there is an immediate problem: you must pass in i in both arguments. That kind of repetition should be a big red flag. But other than that, we're right back to the problem from your other question, which is that you need to figure out how to control i when it's inside your function so that it doesn't take on its expected value from outside your function. As soon as the global i gets a value, your function will break if you try to pass in expressions with i. There are ways you could do that, but they are just way more complicated than just letting the built in functions do this for you:

theRange = Range[3, 20];
theFunction = HoldForm[#^4] &;
theResult = Total[theFunction /@ theRange]

Or:

Total@Table[theFunction[index], {index, theRange}]
(*which is equivalent to ...*)
Total@Table[theFunction[index], {index, 3, 20}]
(*which is equivalent to ...*)
Total@Table[HoldForm[#^4] &[index], {index, 3, 20}]

I'm not sure if you really wanted HoldForm in there or if you were just debugging. These could be simplified without the HoldForm.

$\endgroup$
0
2
$\begingroup$

If you do this:

ExpandSum[summand_, range_List] := Block[{},
  Echo[range]; 
  Total[(Function @@ {range[[1]], HoldForm[summand]}) /@ 
    Array[range[[2]] - 1 + # &, (range[[3]] - range[[2]] + 1)]]
  ]

Then you will see that your function is operating on {10,3,20}

So the function that ends up being mapped is function ends up being mapped

Function[10,10000]/@yourList

Perhaps you are looking for something like:

Total[HoldForm[#^4] & /@ Range[3, 20]]
$\endgroup$
2
  • $\begingroup$ PS. Clear[i] works for me. It doesn't give the Unset warning. $\endgroup$ May 3, 2023 at 13:46
  • $\begingroup$ Many thanks, but it doenst work. Range[1] is 10 but should be i $\endgroup$
    – Roberta
    May 3, 2023 at 14:18

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.