1
$\begingroup$

I have a Code (a long one), part of it is below. The following piece basically multiplies an adjacency matrix with a vector of equilibrium values. Then tries to pick non-zero elements to be used in other sections of the Code. The entire Code is in DynamicModule with a Manipulate inside since I wanted to simulate the model with different random numbers.

My question is that, although I know my matrix a exists with positive numbers, I cannot extract those numbers and hence the entire Code fails.

The matrix a is given as follows:

a = {{0, 0, 0, 7.55596 FE`alfa$$1563[4] FE`tao$$1563[1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7.55596 FE`alfa$$1563[19] FE`tao$$1563[1], 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.461227 FE`alfa$$1563[14] FE`tao$$1563[3], 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.665655 FE`alfa$$1563[17] FE`tao$$1563[4], 0, 0, 0}, {0, 0, 0, 0, 0, 3.14909 FE`alfa$$1563[6] FE`tao$$1563[5], 0, 0, 0, 0, 3.14909 FE`alfa$$1563[11] FE`tao$$1563[5], 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.34426 FE`alfa$$1563[11] FE`tao$$1563[6], 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1.66264 FE`alfa$$1563[4] FE`tao$$1563[8], .66264 FE`alfa$$1563[5] FE`tao$$1563[8], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0., 0, 0, 0, 0.}, {0, 0, 0, 0, 0, 0, 0, 4.32564 FE`alfa$$1563[8] FE`tao$$1563[10], 0, 0, 0, 4.32564 FE`alfa$$1563[12] FE`tao$$1563[10], 0, 0, 0, 0, 4.32564 FE`alfa$$1563[17] FE`tao$$1563[10], 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.02967 FE`alfa$$1563[14] FE`tao$$1563[11], 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 154.893 FE`alfa$$1563[7] FE`tao$$1563[13], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0.152293 FE`alfa$$1563[6] FE`tao$$1563[14], 0, 0, 0, 0, 0, 0, 0.152293 FE`alfa$$1563[13] FE`tao$$1563[14], 0, 0, 0, 0, 0.152293 FE`alfa$$1563[18] FE`tao$$1563[14], 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0., 0, 0, 0, 0, 0, 0., 0., 0, 0., 0., 0, 0, 0, 0., 0, 0, 0.}, {5.96944 FE`alfa$$1563[1] FE`tao$$1563[17], 0, 0, 0, 0, 0, 0, 0, 0, 5.96944 FE`alfa$$1563[10] FE`tao$$1563[17], 0, 0, 0, 0, 0, 0, 0, 5.96944 FE`alfa$$1563[18] FE`tao$$1563[17], 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1.93381 FE`alfa$$1563[3] FE`tao$$1563[19], 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.93381 FE`alfa$$1563[13] FE`tao$$1563[19], 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0., 0, 0, 0, 0., 0., 0., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

Now I try to extract all nonzero values with

nonzeroes = Reap[Sow[#, # > 0] & /@ Flatten[a];, True][[2]] // Flatten;

But for some reason, this does evaluate to the empty list {}.

Hope to receive some guidance. Thanks.

$\endgroup$
  • $\begingroup$ I also edited your question in order to boil it down to the essentials. Welcome at Mathematica.StackExchange! $\endgroup$ – Henrik Schumacher Sep 27 '18 at 16:04
  • $\begingroup$ Thank you very much. Excellent. $\endgroup$ – Tugrul Temel Sep 27 '18 at 16:06
  • $\begingroup$ You're welcome. $\endgroup$ – Henrik Schumacher Sep 27 '18 at 16:06
0
$\begingroup$

I do not understand your question correctly. But by using SparseArray, you can extract nonzero positions and nonzero values as follows:

spa = SparseArray[a];

Extracting the nonzero positions:

spa["NonzeroPositions"]


(*
{{1, 4}, {1, 19}, {3, 14}, {4, 17}, {5, 6}, {5, 11}, {6, 11}, {8, 4}, {8, 5}, {10, 8}, {10, 12}, {10, 17}, {11, 14}, {13, 7}, {14, 6}, {14, 13}, {14, 18}, {17, 1}, {17, 10}, {17, 18}, {19, 3}, {19, 13}}
*)

Extracting the nonzero values:

spa["NonzeroValues"]

(*
{7.55596 FE`alfa$$1563[4] FE`tao$$1563[1], 
 7.55596 FE`alfa$$1563[19] FE`tao$$1563[1], 
 0.461227 FE`alfa$$1563[14] FE`tao$$1563[3], 
 0.665655 FE`alfa$$1563[17] FE`tao$$1563[4], 
 3.14909 FE`alfa$$1563[6] FE`tao$$1563[5], 
 3.14909 FE`alfa$$1563[11] FE`tao$$1563[5], 
 3.34426 FE`alfa$$1563[11] FE`tao$$1563[6], 
 1.66264 FE`alfa$$1563[4] FE`tao$$1563[8], 
 1.66264 FE`alfa$$1563[5] FE`tao$$1563[8], 
 4.32564 FE`alfa$$1563[8] FE`tao$$1563[10], 
 4.32564 FE`alfa$$1563[12] FE`tao$$1563[10], 
 4.32564 FE`alfa$$1563[17] FE`tao$$1563[10], 
 1.02967 FE`alfa$$1563[14] FE`tao$$1563[11], 
 154.893 FE`alfa$$1563[7] FE`tao$$1563[13], 
 0.152293 FE`alfa$$1563[6] FE`tao$$1563[14], 
 0.152293 FE`alfa$$1563[13] FE`tao$$1563[14], 
 0.152293 FE`alfa$$1563[18] FE`tao$$1563[14], 
 5.96944 FE`alfa$$1563[1] FE`tao$$1563[17], 
 5.96944 FE`alfa$$1563[10] FE`tao$$1563[17], 
 5.96944 FE`alfa$$1563[18] FE`tao$$1563[17], 
 1.93381 FE`alfa$$1563[3] FE`tao$$1563[19], 
 1.93381 FE`alfa$$1563[13] FE`tao$$1563[19]}
 *)

If you insist on using Sow and Reap, then you should use UnsameQ as test for nonnullity as >, != and < won't evaluate in gereral if they are applied to expressions that involve symbols.

b = Reap[If[(# =!= 0) && (# =!= 0.), Sow[#]] & /@ Flatten[a], _][[2, 1]]
$\endgroup$
  • $\begingroup$ Dear Henrik, You have already answered my question. I prepared a simple executable example to post with the answer you gave me. I am a new comer and I do not know if have any reputation to vote for your answer. I will try to give all my reputations, which only 1, I think. Furthermore, I do not know if I should post the working example with your input. I would appreciate if you let me know. $\endgroup$ – Tugrul Temel Sep 27 '18 at 15:49
  • $\begingroup$ Okay, if this already settles your question: Fine. In order to accept my answer, just click the checkmark below the up- and downvote buttons. If you also click the upvote button, it will count as an upvote once you made the 15 reputation mark. The nice thing about stackexchange: Upvoting does not cost you any reputation. So you really don't have to "pay" other users: it is for free. (But everybody enjoys getting rep as sign of gratitude.) $\endgroup$ – Henrik Schumacher Sep 27 '18 at 15:58
  • $\begingroup$ I just noticed that "b" is not identical to spa["NonzeroValues"] because there is an extra "0" at the end of the b sequence. How can we remove that zero? $\endgroup$ – Tugrul Temel Sep 28 '18 at 11:18
  • $\begingroup$ @Tebernus Godd point. I added a test against the floating point zero 0.. $\endgroup$ – Henrik Schumacher Sep 28 '18 at 11:32
  • $\begingroup$ it now works perfect. Tugrul $\endgroup$ – Tugrul Temel Sep 28 '18 at 12:12

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.