-2
$\begingroup$

I tried running a function f with rules and calling it directly with arguments specified directly but the one with rules returns zero while the one with direct arguments returns sensible answers (and what I expected).

The definition of f is:

f[x,t1,t2,c] := Sum[c[[k2]], Exp[ -Norm[aL2[x, t1]] - t2[[k2]], 2]^2, {k2, 1, Length[t2]}]

First I tried evaluating it with rules:

f[x,c,t2L, t1L]] /. {x -> 1, t2L->{2}, t2L-> {3}, c->{4}}

and got:

enter image description here

and I tried tracing it to see whats going on, but can't figure it out why the trace has a random zero at the end. I called:

Trace[f[x,c,t2L, t1L]] /. {x -> 1, t2L->{2}, t2L-> {3}, c->{4}}]

but got:

enter image description here

However, when I call the function directly with arguments:

f[1,{4},{3},{2}]

I get what I expected:

enter image description here

anyone know what is going on?


I apologize if this is a easy question, but I've read so much about Mathematica at this point without getting anywhere that I don't know what else to do except ask someone what the issue is.

$\endgroup$

closed as off-topic by Michael E2, ilian, Oleksandr R., Jens, Verbeia Aug 11 '15 at 23:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Michael E2, ilian, Oleksandr R., Jens, Verbeia
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ Please post Mathematica input code directly instead of inserting screenshots of code (screenshots of the behavior is fine, e,g, the last screenshot you've included). By the way, I'm pretty sure that the Sum is trying to evaluate before you are substituting for t2L: then, since Length[t2L] = 0 (because t2L is a symbol), the Sum will evaluate to 0 because it is not summing over anything. $\endgroup$ – march Aug 11 '15 at 19:25
  • $\begingroup$ You do not specify what the definition of f is and you do not give code except as (bizarrely blurry) screenshots. Probably your question can be answered straightforwardly, but you will need to ask it in a way that is more conducive to receiving help. For now, I vote to close it, since without f, the whole thing is meaningless. $\endgroup$ – Oleksandr R. Aug 11 '15 at 22:24
  • $\begingroup$ @march I have edited my question with valid code. Is my question approvable now or how can I further improve it? Am I missing anything else to reach the standards of the site? $\endgroup$ – Pinocchio Aug 16 '15 at 5:03
  • $\begingroup$ IMO, that's better. Sometimes it's the case that it is easy to see that a user has made a standard error. More often, it is necessary to see the user's code to see where-the-error-is/why-the-output-is-wrong/etc. So we like to see definitions of symbols/function you are using. We also like to see copy-and-pastable Mathematica code (it makes it easier for us to help). Finally, it's nice to see effort toward a solution, which you've done. Unfortunately, I do not have the rep to vote to re-open. However: your question has been answered below. And I think your code has errors beyond yielding 0. $\endgroup$ – march Aug 17 '15 at 16:28
4
$\begingroup$

I can illustrate what is going on with a simpler function than yours. Consider

f[x_, y_] := Sum[y[[i]], {i, Length[x]}

Then f[{"a", "b"}, {3, 4}] gives 7 as expected, but

f[x, y] /. {x -> {"a", "b"}, y -> {3, 4}}

gives 0, just as your function did. Now /. is infix operator version of ReplaceAll, so the above is equivalent of

ReplaceAll[f[x, y], {x -> {"a", "b"}, y -> {3, 4}}] (* 1 *)

ReplaceAll uses standard evaluation, which means all it arguments get evaluated before anything else is done. So f[x, y] gets evaluated before the substitution is done. And what does f[x, y] evaluate to?

f[x, y]
0

because Length[x] in Sum[y[[i]], {i, Length[x]}] is 0 and Sum[y[[i]], {i, 0]}] is identically 0.

So the expression tagged with (* 1 *) is same as

ReplaceAll[0, {x -> {"a", "b"}, y -> {3, 4}}]

which, of course, returns 0.

How can you get what you want? You can use Unevaluated to delay evaluation.

Unevaluated[f[x, y]] /. {x -> {"a", "b"}, y -> {3, 4}}
 7
$\endgroup$

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