2
$\begingroup$

I have about a 20 cell notebook which begins at n=3. By replacing 3 by other natural numbers, I can compute what I want using these values, but I do not understand how to automate this process. I'd like it to return all the values when $n$ is in some range (for instance $n\in [3,100]$). The fact that the are program has multiple lines makes different features seem to not work. I have tried setting everything as a function of $n$, but it does not seem to like indeterminately sized arrays (which are totally necessary). I do not really care about efficiency, I'd like a way to do this without modifying most of the code.

EDIT: Here's an example

A[n_] := DiagonalMatrix[ConstantArray[-1, 2 n]] + 
   ConstantArray[-1, {2 n, 2 n}];
B[n_] := SparseArray[{{i_, i_} -> 1, {i_, j_} /; i - j > 0 -> 
     1}, {2 n, 2 n}];
Y[n_] := Transpose[B[n]];
W[n_] := ConstantArray[0, {2 n, 2 n}];
mat[n_] := 
  Normal@SparseArray[{{i_, i_} -> a, {i_, j_} /; i - j == 1 -> 
      b, {i_, j_} /; j - i == 1 -> y, {i_, j_} /; Abs[i - j] > 1 -> 
      z}, {2 n - 2, 2 n - 2}];
Q[n_] := ArrayFlatten[mat /. {a -> A, b -> B, y -> Y, z -> W}];
h[n_] := Det[Q[n]]
h[3]

Gives multiple "tag protected errors" and returns completely erroneous output, while

    n = 3;
A = DiagonalMatrix[ConstantArray[-1, 2 n]] + 
   ConstantArray[-1, {2 n, 2 n}];

B = SparseArray[{{i_, i_} -> 1, {i_, j_} /; i - j > 0 -> 1}, {2 n, 
    2 n}];

Y = Transpose[B];

W = ConstantArray[0, {2 n, 2 n}];

mat = Normal@
   SparseArray[{{i_, i_} -> a, {i_, j_} /; i - j == 1 -> 
      b, {i_, j_} /; j - i == 1 -> y, {i_, j_} /; Abs[i - j] > 1 -> 
      z}, {2 n - 2, 2 n - 2}];

Q = ArrayFlatten[mat /. {a -> A, b -> B, y -> Y, z -> W}];

Det[Q]

returns the correct value of 1.

$\endgroup$

closed as off-topic by m_goldberg, MarcoB, user9660, Mr.Wizard Feb 10 '16 at 19:33

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

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, MarcoB, Community, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ " I have tried setting everything as a function of n," ... is the way to go. ...."but it does not seem to like indeterminately sized arrays" ... isn't true in general. Could you show an example? $\endgroup$ – Dr. belisarius Feb 10 '16 at 0:01
  • $\begingroup$ I have added an example. $\endgroup$ – PVAL Feb 10 '16 at 0:24
3
$\begingroup$

You have made the simple mistake of not being thorough enough when you wrote out the function-based version of your code. Only one function definition need be changed.

Q[n_] := ArrayFlatten[mat[n] /. {a -> A[n], b -> B[n], y -> Y[n], z -> W[n]}]

With this change h[3] returns 1.

$\endgroup$

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