0
$\begingroup$

I have an $n$ x $n$ matrix $T$, where $t_{ij}$ is the element at the $i\,$th row and $j\,$th column, and unknowns $x_i$, $1\leq i \leq n$, where $$x_{n} = 1$$ $$x_{n-1} = 0$$ $$x_i = \sum_{1\leq j \leq n}{t_{ij}x_j};\;1\leq i \leq n-2$$ and am trying to package up this problem statement for use in LinearSolve. I can get everything working satisfactorily with

LinearSolve[T - DiagonalMatrix[Join[Table[1, {n - 2}], {0, 0}]], Append[Table[0, {n - 1}], 1]]

but feel like I've made things more complicated than they need to be.

Is there a better, more compact — perhaps more idiomatic — way to express this problem?

$\endgroup$

2 Answers 2

1
$\begingroup$

This is but a mild variant. I'll create an example using n=4.

SeedRandom[1111];
n = 4;
tmat = RandomInteger[{-10, 10}, {n - 2, n}]

(* Out[190]= {{-8, 5, -3, -8}, {-4, 2, 4, 2}} *)

LinearSolve[IdentityMatrix[n] - 
  Join[tmat, {ConstantArray[0, n], ConstantArray[0, n]}], UnitVector[n, n]]

(* Out[196]= {18/11, 50/11, 0, 1} *)

An alternative might be to use NullSpace but then you will need to renormalize based on the last value.

NullSpace[IdentityMatrix[n] - Join[tmat, {ConstantArray[0, n], UnitVector[n, n]}]]

(* Out[193]= {{18, 50, 0, 11}} *)
$\endgroup$
4
  • $\begingroup$ I get errors. And is that even the same thing? $\endgroup$
    – orome
    Nov 16, 2014 at 21:27
  • $\begingroup$ Anyway, I think I've found what I was looking for; though I'm open to anything that improves on it. $\endgroup$
    – orome
    Nov 16, 2014 at 21:38
  • $\begingroup$ How does this relate to LinearSolve[tmat - DiagonalMatrix[Join[Table[1, {n - 2}], {0, 0}]], Append[Table[0, {n - 1}], 1]] (from the original question)? $\endgroup$
    – orome
    Nov 17, 2014 at 14:59
  • $\begingroup$ We've defined the T matrix slightly differently. I dropped the last two rows because they do not seem to be used in the formulation of the linear system. $\endgroup$ Nov 17, 2014 at 17:00
0
$\begingroup$

A bit shorter and more expressive:

LinearSolve[T - PadRight[IdentityMatrix[n - 2], {n, n}], PadLeft[{1}, n]]
$\endgroup$
1
  • $\begingroup$ I'm still open to better. $\endgroup$
    – orome
    Nov 16, 2014 at 21:37

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.