0

I had asked the same question before here Creating a list of functions with desired coefficients but did not get the desired answer, may be I was not clear in my question. I have defined this function to results some list of functions, however its not returning what I want

    d = 5;
    xs = Array[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 + 4 x[4]^2 + 5 x[5]^2, -E^x[2] + 
5 x[1]^2 + 2 x[3]^2 + 3 x[4]^2 + 4 x[5]^2, -E^x[3] + 4 x[1]^2 + 
5 x[2]^2 + 2 x[4]^2 + 3 x[5]^2, -E^x[4] + 3 x[1]^2 + 4 x[2]^2 + 
5 x[3]^2 + 2 x[5]^2, -E^x[5] + 2 x[1]^2 + 3 x[2]^2 + 4 x[3]^2 + 
5 x[4]^2}

What I want this to return

{-E^x[1] + 2 x[2]^2 + 3 x[3]^2 + 4 x[4]^2 + 5 x[5]^2, -E^x[2] + 
x[1]^2 + 3 x[3]^2 + 4 x[4]^2 + 5 x[5]^2, -E^x[3] + x[1]^2 + 
2 x[2]^2 + 4 x[4]^2 + 5 x[5]^2, -E^x[4] + x[1]^2 + 2 x[2]^2 + 
3 x[3]^2 + 5 x[5]^2, -E^x[5] + x[1]^2 + 2 x[2]^2 + 3 x[3]^2 + 
4 x[4]^2}

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

The input should be that array $xs.$ I want it for different values of 'd'.

Further I should be able to evaluate it at an array like $(1,1,1,1,1)$ or $(1,2,3,4,5).$ I mean is I can define

 ys = ConstantArray[1/10, d];

and evaluate

newF[ys]
Learner
  • 219
  • 1
  • 6

1 Answers1

0

Using TakeList:

Clear["Global`*"];

g[d_Integer] := Module[{ r = Range[d] , f = -Exp[x[First@#]] + Total[# x[#]^2 & /@ Last@#] & }, t = TakeList[Range[d], {{#}, All}] & /@ r // Map[FlattenAt[1]]; f /@ t ]


Usage:

g[5]

{-E^x[1] + 2 x[2]^2 + 3 x[3]^2 + 4 x[4]^2 + 5 x[5]^2, -E^x[2] +
x[1]^2 + 3 x[3]^2 + 4 x[4]^2 + 5 x[5]^2, -E^x[3] + x[1]^2 + 2 x[2]^2 + 4 x[4]^2 + 5 x[5]^2, -E^x[4] + x[1]^2 + 2 x[2]^2 + 3 x[3]^2 + 5 x[5]^2, -E^x[5] + x[1]^2 + 2 x[2]^2 + 3 x[3]^2 + 4 x[4]^2}


EDIT

To evaluate:

g[5] /. Thread[Array[x, 5] -> {1, 2, 3, 4, 5}]

{224 - E, 217 - E^2, 198 - E^3, 161 - E^4, 100 - E^5}

Syed
  • 52,495
  • 4
  • 30
  • 85