1
$\begingroup$

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]
$\endgroup$

2 Answers 2

0
$\begingroup$

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}

$\endgroup$
0
$\begingroup$

Barring errors:

f[a_] := 
 With[{w = Length@a}, 
  Table[(Range[w] a) . a - k  a[[k]]^2 - Exp[a[[k]]], {k, 1, w}]]

Some tests

xs = Array[x, 5];
f[xs] // MatrixForm
f[{1, 1, 1, 1, 1}] // MatrixForm
f[{1, 2, 3, 4, 5}] // MatrixForm

enter image description here

$\endgroup$

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.