2

I have defined this function to results some list of functions, however its not returning what I want

    d = 3;
    xs = Array[Subscript[x, #] &, d];
    newF[xs_List] := (Sum[((i)*(#[[i]])^2), {i, 2, d}] - Exp[#[[1]]]) & /@ Partition[xs, d, 1, {1, 1}];
    newF[xs]

It results in $\{-e^{x_1}+2 x_2^2+3 x_3^2,-e^{x_2}+ 3 x_1^2+2 x_3^2,-e^{x_3}+2 x_1^2+3 x_2^2\}$

What I want is $\{-e^{x_1}+2 x_2^2+3 x_3^2,-e^{x_2}+x_1^2+3 x_3^2,-e^{x_3}+x_1^2+2 x_2^2\}$

Basically I want is $-e^{x_k}+\sum_{n\neq k}n\cdot x_n^2$

Further I should be able to evaluate it at an array like $(1,1,1)$ or $(1,2,3)$

Learner
  • 219
  • 1
  • 6

1 Answers1

5

Instead of using Subscript[x, 1], I suggest using x[1]. You can still use Format to display them as Subscript.

Also, your formula explicitly depends on $n$. Therefore, you need to know it when calculating the sum. Instead of passing a list of $x$s and then peeking into each $x$ to get its $n$, you can generate the variables inside newF.

Format[x[n_]] := Subscript[x, n]

newF[x_, d_] := Table[-Exp[x[k]] + Sum[n x[n]^2 (1 - KroneckerDelta[n, k]), {n, d}], {k, d}]

newF[x, 3] (* {-E^x[1] + 2 x[2]^2 + 3 x[3]^2, -E^x[2] + x[1]^2 + 3 x[3]^2, -E^x[3] + x[1]^2 + 2 x[2]^2} *)

To insert the actual values of $x_n$, just use ReplaceAll.

newF[x, 3] /. {x[1] -> 1, x[2] -> 4, x[3] -> -1}

newF[x, 3] /. {x[_] -> 1}

And if you really want to use a list as an argument:

newF[x_List] := 
 Table[-Exp[x[[k]]] + 
   Sum[First[x[[n]]] x[[n]]^2 (1 - KroneckerDelta[n, k]), {n, 
     Length[x]}], {k, Length[x]}]

newF[Array[x, 3]]

Domen
  • 23,608
  • 1
  • 27
  • 45
  • Or Table[-Exp[x[k]] + Sum[n x[n]^2 , {n, Delete[Range[d], k]}], {k, d}]. – march Nov 21 '23 at 21:12
  • @Domen Thanks. How do I evaluate it at (1,1,1) or (1,2,3)? – Learner Nov 22 '23 at 04:22
  • You tried to write your function in terms of the list of subscripted variables. But semantically, your function is only dependent on the scale (what you called d). You also hard-coded your formal variable to x, which is somewhat bad style for subtle reasons. Also, using Subscript seems attractive, but it's also not really the best style in the long run. So, Domen re-conceived of your function to be one that required as arguments only the scale and the name of the formal variable. Which means (if I'm understanding your question) you don't evaluate at (1,1,1) at all. – lericr Nov 22 '23 at 04:55
  • Or maybe you mean that you want to plug in to the result with these substitutions: x[1] = 1, x[2] = 1, x[3] = 3. To do that, you would just make those definitions explicitly. – lericr Nov 22 '23 at 04:57
  • @lericr I have defined the function which depends on an array. That array xs uses value of d. Then the function takes array as input – Learner Nov 22 '23 at 08:59
  • @lericr The value d is the number of variables (no. of entries in my array). I should be able to change the value of d. If d=3, the newF[xs] returns the functions in x[1],x[2],x[3] and If d=4, the newF[xs] returns the functions in x[1],x[2],x[3],x[4]. Later when I use xs = (1,1,1) it shlould evaluate, means x[1]=1,x[2]=1,x[3]=1. – Learner Nov 22 '23 at 09:12
  • Yes, I understand that. Do my comments not answer your question? – lericr Nov 22 '23 at 15:07
  • @Learner, please see if my update answers your question(s). – Domen Nov 22 '23 at 15:28
  • @Domen I really dont care about the subscript. All I care is function to evaluate at an array – Learner Nov 23 '23 at 05:57
  • @Learner, I've added a version with lists. – Domen Nov 23 '23 at 09:46