0
$\begingroup$

I have $f:\Bbb R^n\rightarrow\Bbb R^m$ and I want to create a table as follows

$$ \begin{align} \text{variable} && \text{value} && \text{numbers} && \text{negatives} \\ x && f(x) && a && b \end{align} $$

where $a$ is the number of components in $f(x)$ that are actually numbers (my $f$ involves Solve and in a few instances it doesn't return numbers) and $b$ the number that are negative. I have all the variables in list x (so each x[[i]] is $x$) and I was thinking of writing something like

Table[{x[[i]],f(x[[i]]),Count[f(x[[i]]),u_/;NumberQ[u]],Count[f(x[[i]]),u_/;u<0]},{i,1,Lenght[x]}]

but I'm sure there's a better way of doing it.

Edit:

As per Szabolcs request, I'm posting my full code

A = {{1, 1, 3}, {2, 2, 4}, {1, 5, 5}, {2, 3, 4}};
qVec = Array[q, 4]; 
lVec = Array[l, 3];
mVec = Array[m, 4];
onesVec = ConstantArray[1, 4];
c = 1/2;
needs = qVec^(1/c).A;
step = 1/3;
p = Flatten[Permutations /@ IntegerPartitions[1, {3}, Range[-2/3, 5/3, step]],1];
num = Length[p];
f = DeleteDuplicates[Table[Minimize[{needs.p[[i]], qVec.onesVec == 1, 
   Thread[qVec >= 0]}, qVec][[2]], {i, 1, num}]];
num = Length[f];
Table[
 {qVec /. f[[i]], 
  lVec /. Flatten[Solve[Flatten[{Thread[D[qVec.onesVec - lVec.needs - mVec.qVec, {qVec}]== 0], Thread[mVec*qVec == 0]}] /. f[[i]], 
 Flatten[{lVec, mVec}]]], 
  Count[lVec /.Flatten[Solve[Flatten[{Thread[D[qVec.onesVec - lVec.needs - mVec.qVec, {qVec}] == 0], Thread[mVec*qVec == 0]}] /. f[[i]], 
 Flatten[{lVec, mVec}]]], u_ /; u < 0], 
  Count[lVec /. Flatten[Solve[Flatten[{Thread[D[qVec.onesVec - lVec.needs - mVec.qVec, {qVec}] == 0], Thread[mVec*qVec == 0]}] /. f[[i]], 
 Flatten[{lVec, mVec}]]], u_ /; NumberQ[u]]}, {i, 1, num}]
$\endgroup$
2
  • $\begingroup$ Please show what you tried. Post working code. $\endgroup$
    – Szabolcs
    Dec 2 '20 at 13:10
  • $\begingroup$ Table[f[x],{x,xList}] is more clear. $\endgroup$
    – wuyudi
    Dec 2 '20 at 13:24
1
$\begingroup$

You may want to limit the calls of the function $f$, perhaps with something like:

Table[{x, #, Count[#, u_/;NumberQ[u]], Count[#, u_/;u<0]}& [f[x]], {x, xList}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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