3
$\begingroup$

I will provide all the code I used at the end of the question.

I was trying to evaluate a function f with the following signature (which I'll give the details at the end):

f[x_List, t1L_List, t2L_List, c_List]

when I try to evaluate it arguments given to it directly it gives me one number:

f[xdata, t1data, t2data, cdata]

returns (after processing it numerically with N):

72.499

which I believe is the correct one because I've implemented f in matlab and they agree.

However, when I try to evaluate it with rules, it seems to give a different result:

f[x, t1, t2, c] /. Join[Thread[Flatten[x] -> Flatten[xdata]], Thread[Flatten[t1] -> Flatten[t1data]],Thread[Flatten[t2] -> Flatten[t2data]], Thread[Flatten[c] -> Flatten[cdata]]]

returns (after processing it numerically with N):

72.6803

It might look at little strange that I am doing:

Join[Thread[Flatten[x] -> Flatten[xdata]], Thread[Flatten[t1] -> Flatten[t1data]],Thread[Flatten[t2] -> Flatten[t2data]], Thread[Flatten[c] -> Flatten[cdata]]]

to do the rule substitution, but the issue is that x, t1, t2, c are all lists (of potentially nested lists) of symbols. Thus, its crucial that all the variables in those lists get assigned to the correct value.

I have tried to visually debug it by looking what values each variable in x is assigned too, but they all look fine. So I was wondering, if there was a good way to check what the difference between these two evaluations is? Is there a way to use equality checks to check this? I wish I even knew what the real issue was, maybe the rule substitution was done incorrectly?

Are there any suggestions people have to check why the computation of these two is different?

One may ask why I'd do such a strange thing to evaluate f, but the truth is that I am not interested in evaluating f, but evaluating its derivative, so the real intention is to first compute the derivative and then immediately after use rule substitution to evaluate it at a certain point. As in:

g[f_, theta_] := D[f[x,t1,t2,c], theta] /. Join[Thread[Flatten[x] -> Flatten[xdata]], Thread[Flatten[t1] -> Flatten[t1data]],Thread[Flatten[t2] -> Flatten[t2data]], Thread[Flatten[c] -> Flatten[cdata]]]

this actually runs for a value of theta that is a symbol inside the lists (as in t2L1x1y as an example at the end of the code), however, even if it runs with no errors, if the substitution:

Join[Thread[Flatten[x] -> Flatten[xdata]], Thread[Flatten[t1] -> Flatten[t1data]],Thread[Flatten[t2] -> Flatten[t2data]], Thread[Flatten[c] -> Flatten[cdata]]]

doesn't work for the simple case of evaluating f, then I can't trust it using the derivative either.


Appendix

I will paste the full code here so that people can reproduce the exact results that I am getting.

For the definition of f:

f[x_List, t1L_List, t2L_List, c_List] := Sum[ c[[k2]]  a3k2[x, t1L, t2L[[k2]]], {k2, 1, Length[c]}]

a3k2[x_List, t1L_List, t2k2_List] := Exp[-mynorm[ a2[x, t1L] - t2k2]^2]

a2[x_List, t1L_List] := Join[Flatten[Table[a2np[x[[np]], t1L[[np]]], {np, 1, Length[x]}]]]

a2np[xnp_List, t1Lnp_List] := Table[Exp[-mynorm[xnp - t1Lnp[[dd]]]^2], {dd, 1, Length[t1Lnp]}]

mynorm[x_List] := (Sum[(x[[i]])^2, {i, 1, Length[x]}])^(1/2)

For the definition of the actual data I used:

x = {{x1p1x, x1p2x, x1p3x}, {x2p1x, x2p2x, x2p3x}}

t2 = {{t2L1x1y, t2L1x2y, t2L1x3y, t2L1x4y}, {t2L2x1y, t2L2x2y, t2L2x3y, t2L2x4y}, {t2L3x1y, t2L3x2y, t2L3x3y, t2L3x4y}, {t2L4x1y, t2L4x2y, t2L4x3y, t2L4x4y}}

t1 = {t1L1p, t1L2p}

t1L1p = {t1L1p1v, t1L1p2v}

t1L2p = {t1L2p1v, t1L2p2v}

t1L1p1v = {t1L1p1v1x, t1L1p1v2x, t1L1p1v3x}

t1L1p2v = {t1L1p2v1x, t1L1p2v2x, t1L1p2v3x}

t1L2p1v = {t1L2p1v1x, t1L2p1v2x, t1L2p1v3x}

t1L2p2v = {t1L2p2v1x, t1L2p2v3x, t1L2p2v3x}

c = {c1, c2, c3, c4}

and for the actual numbers I used:

xdata = {{1, 2, 3}, {4, 5, 6}}/Norm[{1, 2, 3, 4, 5, 6}, 2]

t1data = {{Range[1, 3]/Norm[Range[1, 3], 2],Range[4, 6]/Norm[Range[4, 6], 2]}, {Range[7, 9]/ Norm[Range[7, 9], 2], Range[10, 12]/Norm[Range[10, 12], 2]}}

t2data = {Range[13, 16]/Norm[Range[13, 16], 2], Range[17, 20]/Norm[Range[17, 20], 2], Range[21, 24]/Norm[Range[21, 24], 2], Range[25, 28]/Norm[Range[25, 28], 2]}

cdata = {29, 30, 31, 32}
$\endgroup$

closed as off-topic by george2079, MarcoB, Öskå, Michael E2, ilian Aug 19 '15 at 23:34

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

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – george2079, MarcoB, Öskå, Michael E2, ilian
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Look at your definition for t1L2p2v. There are two t1L2p2v3x's. With code like this, you always need fresh eyes if something is not working. (By the way, this question was good: it supplied all the information. But: it's always a good idea to see if you can reproduce your problem for a simpler version of what you're doing, a minimal example in the common parlance here. You probably would have discovered that the code worked for the minimal example, which would have suggested to you that there was a typo in the real problem.) $\endgroup$ – march Aug 18 '15 at 17:57
  • $\begingroup$ Why don't you put semicolons at the ends? Don't all those unnecessary lines of output get in the way? $\endgroup$ – Michael E2 Aug 18 '15 at 18:33
  • $\begingroup$ @MichaelE2 I guess for the symbolic variables, you are right, the output is in the way, but for the ones were actual numerical values are used, I sort of looked at the values to make sure they were correct and matched with some of the other assignments. In particular I also print out: Join[Thread[Flatten[x] -> Flatten[xdata]], Thread[Flatten[t1] -> Flatten[t1data]], Thread[Flatten[t2] -> Flatten[t2data]], Thread[Flatten[c] -> Flatten[cdata]]] sorry should have mentioned this I guess. Thanks for the suggestion though! $\endgroup$ – Pinocchio Aug 18 '15 at 18:36
  • $\begingroup$ In the first case, I get this, not the approximate number. You must have used N, I guess? $\endgroup$ – Michael E2 Aug 18 '15 at 18:36
  • $\begingroup$ @MichaelE2 yes I did use N. Sorry for not saying that, its sort of hard to write a perfect question! Though I've updated it so that it makes sense. $\endgroup$ – Pinocchio Aug 18 '15 at 18:37