I am beginner in mathematica. I have the following question:

Assume we have a matrix and some of its entries are nonzero and we know which one are. But the nonzero entries can be three different values and we want to calculate the determinant of the matrix for all different cases. How can I do that?

Thank you.


closed as off-topic by Daniel Lichtblau, MarcoB, anderstood, Sektor, Coolwater Jan 2 '18 at 13:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Daniel Lichtblau, MarcoB, anderstood, Sektor, Coolwater
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Have a look at SparseArray and Det. If the matrix is very large, the LU-decomposition provided by LinearSolve might help. Moreover, how large is the matrix (number of rows/columns and number of nonzero entries)? $\endgroup$ – Henrik Schumacher Dec 30 '17 at 16:50

Det is always zero

 mat = ConstantArray[0, {5, 5}]

Thread[{mat[[1, 2]], mat[[1, 4]], mat[[2, 3]], mat[[2, 5]], 
   mat[[3, 1]], mat[[3, 4]], mat[[4, 4]], mat[[5, 2]]} = 
  RandomChoice[{a, b, c}, 8]]


  • 1
    $\begingroup$ This seems to be a comment on the earlier answer by @Henrik-Schumacher $\endgroup$ – bbgodfrey Dec 30 '17 at 18:02

Here a test example with a SparseArray that is actually dense. For large matrices, Det might be not the best choice (I am not sure, probably, Det is already implemented very cleverly. An alternative can be obtained from the LU factorization provided by LinearSolve.

n = 12;
A = SparseArray[RandomReal[{-1, 1}, {n, n}]];

sol = LinearSolve[A];
det = Times[
  Times @@ Diagonal[sol["getU"]],
  Times @@ Diagonal[sol["getL"]]




In your particular case, you can build the matrix with your desired sparsity pattern but with arbitrary symbolic values a[1], ... ,a[8] as follows:

pat = {{1, 2}, {1, 4} , {2, 3}, {2, 5}, {3, 1} , {3, 4}, {4, 4}, {5, 2}};
vals = Array[a, Length[pat]];
A = SparseArray[pat -> vals, {5, 5}]

Mathematica tells us that a matrix with this sparsity pattern cannot be invertible; its determinant is always zero:



  • $\begingroup$ Thank you all. I think i asked my question ambiguous. The matrix is not huge. $\endgroup$ – A. Mpi Dec 30 '17 at 17:05
  • $\begingroup$ Okay, then Det should do the job. $\endgroup$ – Henrik Schumacher Dec 30 '17 at 17:10
  • 1
    $\begingroup$ Thank you all. I think i asked my question ambiguous. The matrix is not huge. Assume we have a 5 by 5 matrix and the entries (1,2) (1,4) (2,3) (2,5) (3,1) (3,4) (4,4) (5,2) are nonzero and the values of these entries can be a,b or c. I want to calculate Det of these matrices for all cases that can happen for nonzero entries. Thank you $\endgroup$ – A. Mpi Dec 30 '17 at 17:12

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