4
$\begingroup$

Generate a 100 × 100 matrix

A = matrix(100, 100, lambda i, j: 1/(i-j) if i != j else 0)

I want the numerical value of A's determinant. Is it quicker to evaluate the determinant exactly first or to work from the start with a matrix with numerical entries?

Note: this matrix is given in Sage syntax, how would I write this in Mathematica syntax?

I know how to create a 100 x 100 matrix:

A[i_, j_] := Table[{i} {j}, {i, 100}, {j, 100}]

Any help wil be appreciated.

$\endgroup$
2

2 Answers 2

4
$\begingroup$
n = 100;
A = Array[{i, j} \[Function] If[i != j, 1/(i - j), 0], {n, n}];

or

A = Table[If[i != j, 1/(i - j), 0], {i, 1, n}, {j, 1, n}];

The latter is actually faster.

If you need it even faster, use

A = Compile[{{n, _Integer}},Table[If[i != j, 1/(i - j), 0], {i, 1, n}, {j, 1, n}]][
 n
];

That's about 20 times faster.

Edit

With the remark by AccidentalFourierTransform,

A = ToeplitzMatrix[
  Join[{0}, 1/Range[1, n - 1]], Join[{0}, -1/Range[1, n - 1]]
  ];

is another very fast possibility.

$\endgroup$
3
  • $\begingroup$ Interesting that the Table version is faster. I’d have expected similar speed given that the auto compilation should be able to handle both equivalently. $\endgroup$
    – b3m2a1
    Apr 8, 2018 at 22:00
  • $\begingroup$ @b3m2a1 I was also surprised. Maybe Array does not compile? I use it rather scarcely an now I know it's for some reason ;) $\endgroup$ Apr 8, 2018 at 22:07
  • $\begingroup$ @b3m2a1 Actually, I also wonder about the speed difference between (n = 1000, so above the compile threashold) Table[If[i != j, 1/(i - j), 0], {i, 1, n}, {j, 1, n}], With[{cf = Compile[{{i, _Integer}, {j, _Integer}}, If[i != j, 1/(i - j), 0]]}, Table[cf[i, j], {i, 1, n}, {j, 1, n}] ] (factor 2) and Compile[{{n, _Integer}}, Table[If[i != j, 1/(i - j), 0], {i, 1, n}, {j, 1, n}]][n] (another factor 10). Why isn't Mathematica using the latter? $\endgroup$ Apr 8, 2018 at 22:23
3
$\begingroup$

I shortened the matrix for demonstration.

{ii, ji} = {10, 10};
B = Table[If[i != j, 1/(i - j), 0], {i, 1, ii}, {j, 1, ji}];
MatrixForm[%]
Det[%]
N[%]

The output matrix

$\begin{array}{cccccccccc} 0 & -1 & -\frac{1}{2} & -\frac{1}{3} & -\frac{1}{4} & -\frac{1}{5} & -\frac{1}{6} & -\frac{1}{7} & -\frac{1}{8} & -\frac{1}{9} \\ 1 & 0 & -1 & -\frac{1}{2} & -\frac{1}{3} & -\frac{1}{4} & -\frac{1}{5} & -\frac{1}{6} & -\frac{1}{7} & -\frac{1}{8} \\ \frac{1}{2} & 1 & 0 & -1 & -\frac{1}{2} & -\frac{1}{3} & -\frac{1}{4} & -\frac{1}{5} & -\frac{1}{6} & -\frac{1}{7} \\ \frac{1}{3} & \frac{1}{2} & 1 & 0 & -1 & -\frac{1}{2} & -\frac{1}{3} & -\frac{1}{4} & -\frac{1}{5} & -\frac{1}{6} \\ \frac{1}{4} & \frac{1}{3} & \frac{1}{2} & 1 & 0 & -1 & -\frac{1}{2} & -\frac{1}{3} & -\frac{1}{4} & -\frac{1}{5} \\ \frac{1}{5} & \frac{1}{4} & \frac{1}{3} & \frac{1}{2} & 1 & 0 & -1 & -\frac{1}{2} & -\frac{1}{3} & -\frac{1}{4} \\ \frac{1}{6} & \frac{1}{5} & \frac{1}{4} & \frac{1}{3} & \frac{1}{2} & 1 & 0 & -1 & -\frac{1}{2} & -\frac{1}{3} \\ \frac{1}{7} & \frac{1}{6} & \frac{1}{5} & \frac{1}{4} & \frac{1}{3} & \frac{1}{2} & 1 & 0 & -1 & -\frac{1}{2} \\ \frac{1}{8} & \frac{1}{7} & \frac{1}{6} & \frac{1}{5} & \frac{1}{4} & \frac{1}{3} & \frac{1}{2} & 1 & 0 & -1 \\ \frac{1}{9} & \frac{1}{8} & \frac{1}{7} & \frac{1}{6} & \frac{1}{5} & \frac{1}{4} & \frac{1}{3} & \frac{1}{2} & 1 & 0 \\ \end{array}$

$\frac{2018199369318664984321}{878245079040000000000}$

Determinant = 2.29799

$\endgroup$

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.