# Symbolic determinant is not computing [duplicate]

This question already has an answer here:

I'm trying to compute the determinant of a symbolic matrix made with the following code:

NewMatrix[n_] := Module[{i = 1, j = 1, M = Array[m, {n + 1, n + 1}]},
For[i = 1, i <= n + 1, i++,
For[j = 1, j <= n + 1, j++,
If[j < i, m[i, j] = a[[j]],
If[j == i, m[i, j] = x,
If[j > i, m[i, j] = a[[j - 1]], 0]]]
]
]; M // MatrixForm]


But when I apply:

Det[NewMatrix]


Mathematica returns this: If I try to give a and x numerical values it still doesn't show the numerical value of the determinant, just gives the same expression but with numbers. What is happening here?

## marked as duplicate by Feyre, user31159, Michael E2, Alexey Popkov, JensSep 11 '16 at 17:13

• Your output, NewMatrix, is in the MatrixForm, while Det can be applied to Lists; first, remove the //MatrixForm part and it will work. However, I'm getting an error: Part::partd: "Part specification a[] is longer than depth of object." – corey979 Sep 11 '16 at 16:11
• Regarding the error: insert also a = Array[a, n] into the Module, after i and j, and before M. – corey979 Sep 11 '16 at 16:19

Do not include MatrixForm in the definition of NewMatrix; wrappers are only used for display. Also use an indexed variable rather than using Part for a.

Format[a[n_]] := Subscript[a, n]

NewMatrix[n_Integer?Positive] :=
Module[
{i = 1, j = 1, M = Array[m, {n + 1, n + 1}]},
For[i = 1, i <= n + 1, i++,
For[j = 1, j <= n + 1, j++,
If[j < i, m[i, j] = a[j],
If[j == i, m[i, j] = x,
If[j > i, m[i, j] = a[j - 1], 0]]]]]; M]

NewMatrix // MatrixForm Det[NewMatrix] % // Simplify detNewMatrix[n_Integer?Positive] :=
Module[
{arr = Array[a, n]},
(x + Total[arr])*(Times @@ (x - arr))]


Verifying that this is the determinant

And @@ Table[detNewMatrix[n] == Det[NewMatrix[n]] //
Simplify, {n, 10}]

(*  True  *)


I'm assuming that a is (going to be) a vector, since you index it with Part. In that case, here is a simpler way:

newMatrix[n_Integer?Positive] :=
With[{v = Array[Indexed[a, #] &, n]},
Table[Insert[v, x, i], {i, n + 1}]];

mat = newMatrix;
mat // MatrixForm Det[mat] // Simplify You can inject a vector for a using ReplaceAll:

mat /. a -> {1, 12, 23, 34}
% // MatrixForm
(*
{{x, 1, 12, 23, 34},
{1, x, 12, 23, 34},
{1, 12, x, 23, 34},
{1, 12, 23, x, 34},
{1, 12, 23, 34, x}}
*) See Why does MatrixForm affect calculations? for answers to why MatrixForm messes up the computation.

For more alternatives to For loops, see this answer or search the site.