0
$\begingroup$

Yesterday and about five months ago, I asked two similar questions but nobody could answer me; maybe because my question was ambiguous. But, I myself tried and I could finally find my desired answer. But, now my question is that is there a command to shorten the following and does not have the problem of memory?

My yesterday question: assume that you have an $n \times n$ matrix $A$; here for simplicity I suppose that $n=2$. Some of specified entries of this matrix is zero and the others "which we know which ones", are nonzero but the nonzero entries can have 3 different cases; for example for the $2 \times 2$ matrix, $a_{11}=a_{22}=0$ and $a_{12},a_{2,1} \neq 0 $ and can be 2,3 or 5. Now, I want a code to show me all possible values for the determinant of $A$ (that here are $-4,-6,-9,-10,-15,-25)$; My code is as follows:

a = Tuples[{2, 3, 5}, {1, 2}];

b = Table[0, {2}, {2}];

For[i = 1, i <= Length[a], i++,
 {b[[1, 2]] = a[[i]][[1]][[1]]; b[[2, 1]] = a[[i]][[1]][[2]]; 
  Print[Det[b]]}]

which gives

-4

-6

-10

-6

-9

-15

-10

-15

-25

My quesion:Now, my question is that if $A$ has for example 20 nonzero entries, this code doesn't help; because of the first line which now will be

 a = Tuples[{2, 3, 5}, {1, 20}];

and the memory will be fulled.

Bests,

$\endgroup$

1 Answer 1

5
$\begingroup$

Try this

a = {2, 3, 5};
n=7; (*example 7 by 7 matrix*)
b = Table[0, {n}, {n}];
m=20; (*with 20 nonzero entries*)
For[i=0, i<Length[a]^m, i++,(*3^20 iterations*)
 j=IntegerDigits[i,Length[a],m]+1;(*build 20 subscripts*)
 {b[[1,2]], b[[2,1]],b[[2,7]],(*...all 20 positions*)} = Map[a[[#]]&,j];(*assign 20 values*) 
 Print[Det[b]]
]
$\endgroup$
4
  • $\begingroup$ Yes! You made it! That's it! At last one could answer me! You are done! Thank you! $\endgroup$
    – A. Mpi
    Commented Jun 1, 2018 at 12:14
  • $\begingroup$ Just the ticket! $\endgroup$
    – A. Mpi
    Commented Jun 1, 2018 at 12:16
  • $\begingroup$ Everything is finished! $\endgroup$
    – A. Mpi
    Commented Jun 1, 2018 at 12:16
  • 2
    $\begingroup$ I suggest you study that code very carefully, character by character, to learn how and why it works. Mathematica is very different from other languages that you might have seen. There are at least six things in that which you could learn and which may help you with your next problem. $\endgroup$
    – Bill
    Commented Jun 1, 2018 at 13:23

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.