4
$\begingroup$

I've started updating Mathematica code published in a 2000 paper:

Algorithms Behind Term Structure Models of Interest Rates II: The Hull-White Trinomial Tree of Interest Rates

The original code loads the legacy package LinearAlgebra`MatrixManipulation from which it used the function:

SubMatrix[]

For a bit of context, the author uses the function in a Which statement within an Append[]:

Append[SubMatrix[XX, {2, 2}, {-jmin + jmax - 1, -jmin + jmax + 1}],
 Delete[XX[[-1]], {{1}, {2}}]]

Can anyone describe what this function did in 2000 and then direct me to ways to do it in more recent versions of Mathematica (I currently use 10.3.0.0)?

$\endgroup$
5
  • $\begingroup$ Search (e.g., with Google) for "mathematica LinearAlgebra MatrixManipulation" to obtain several references. $\endgroup$ – bbgodfrey Sep 30 '17 at 4:03
  • $\begingroup$ Take[] or Part[] + Span[] should prove more than adequate to replace that old function. $\endgroup$ – J. M.'s ennui Sep 30 '17 at 5:34
  • $\begingroup$ @J.M. -- Appears that Take[] has an exit replacement. -- Thx. $\endgroup$ – Jagra Sep 30 '17 at 14:12
  • $\begingroup$ @J.M. - I spoke to soon. See below. $\endgroup$ – Jagra Sep 30 '17 at 15:12
  • $\begingroup$ Yes, I didn't mean that it was a direct plug-in replacement; just that Take[] with properly set parameters can be used instead. $\endgroup$ – J. M.'s ennui Sep 30 '17 at 15:32
3
$\begingroup$

As suggested by @J.M. in the comments Take[] appears to provide an exact replacement for the legacy, SubMatrix[] (although one has to dig into the documentation to find it, listed as the 6th example under Take...Scope in version 10.3.0.0).

SubMatrix[XX, {2, 2}, {-jmin + jmax - 1, -jmin + jmax + 1}]

Take[XX, {2, 2}, {-jmin + jmax - 1, -jmin + jmax + 1}]

Well... Mr.Wizard called it properly in the 1st comment to this question. The above is indeed incorrect.

Let's try again...with Mr.Wizard's example:

m = Partition[Alphabet[], 4];
SubMatrix[m, {1, 1}, {2, 2}]

{{"a", "b"}, {"e", "f"}}

Now look at the following Take[]

Take[m, {1, 2}, {1, 2}]


{{"a", "b"}, {"e", "f"}}

I think that gets it (even it my answer got there in a roundabout way). Still not certain why the legacy SubMatrix worked differently than Take.

$\endgroup$
2
  • $\begingroup$ FWIW a concise equivalent: Take[XX, {2}, {-1, 1} + jmax - jmin] -- however I believe that this answer is incorrect. $\endgroup$ – Mr.Wizard Sep 30 '17 at 14:20
  • $\begingroup$ @Mr.Wizard - As always, nice to have you on the case. $\endgroup$ – Jagra Sep 30 '17 at 14:35
2
$\begingroup$

The legacy package is here: http://library.wolfram.com/infocenter/MathSource/6770/

TakeMatrix[mat_?MatrixQ, start:{startR_Integer, startC_Integer},
end:{endR_Integer, endC_Integer}] :=
    Take[mat, {startR, endR}, {startC, endC}] /;
    And @@ Thread[Dimensions[mat] >= start] && 
    And @@ Thread[Dimensions[mat] >= end]

SubMatrix[mat_List, start:{_Integer, _Integer}, dim:{_Integer,_Integer}] :=
    TakeMatrix[mat, start, start+dim-1]

This is not directly equivalent to Take:

m = Partition[Alphabet[], 4];

SubMatrix[m, {1, 1}, {2, 2}]
Take[m, {1, 1}, {2, 2}]
{{"a", "b"}, {"e", "f"}}

{{"b"}}

A terse but inefficient equivalent, omitting the argument tests:

subMat[mat_, start_, dim_] := Array[mat[[##]] &, dim, start]
$\endgroup$
2
  • $\begingroup$ Take[m,{1,2},{1,2}] gives the same output as SubMatrix[m, {1, 1}, {2, 2}] in your example. Did that big a change happen in defining positions of Lists? Just trying to understand the thinking that took the legacy code to the current. $\endgroup$ – Jagra Sep 30 '17 at 14:55
  • $\begingroup$ @Jagra I think it is just two different ways of looking at the same thing. I don't see a big change, merely that SubMatrix wasn't apparently considered important enough to make a kernel function. Take has existed since version 1.0 so it's not a matter of one function replacing the other, but rather about the need or lack thereof for a complementary function. $\endgroup$ – Mr.Wizard Sep 30 '17 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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