[Date Index]
[Thread Index]
[Author Index]
RE: Re: Automatic Display in MatrixForm
*To*: mathgroup at smc.vnet.net
*Subject*: [mg19445] RE: [mg19413] Re: Automatic Display in MatrixForm
*From*: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
*Date*: Wed, 25 Aug 1999 01:25:15 -0400
*Sender*: owner-wri-mathgroup at wolfram.com
Daniel Bruno wrote a function
(switchMatrixForm) where evaluating
switchMatrixForm[9,6] would cause matrices with fewer than 9 rows and fewer
than 6 columns to be displayed in the equivalent of MatrixForm.
The solution in question involved a complicated rule for MakeBoxes that Dave
Withoff helped me write.
------------------------------
Question:
Does the method using a rule for MakeBoxes have an advantage over something
along the lines:
$PrePrint =(#/.x_?MatrixQ:>MatrixForm[x])&;
--------------------------------
Defining switchMatrixForm based on the complicated rule using MakeBoxes as
Daniel Bruno wrote it has a problem. Suppose you evaluate
switchMatrixForm[10,10]
Later you decide you only want to use MatrixForm for matrices that are 3x3
or smaller. To do that you might evaluate.
switchMatrixForm[4,4]
But doing that will not undo the rule from the earlier use of
switchMatrixForm.
To correct this problem you can use my new version below. With my version
switchMatrixForm[m,n] doesn't make a new rule for MakeBoxes until it deletes
any and all MakeBoxes rules that switchMatrixForm would have made earlier,
but it leaves any other
rules for MakeBoxes as they are.
I can email a notebook with this code to anyone who would find that easier
to work with.
--------------------------------
RuleTest[a_]:=With[{MB,PT,Patt,RD,Fun,rule1},(
rule1=ReleaseHold[Extract[a,{1,1},Hold]/.
{MakeBoxes->MB,PatternTest->PT,Pattern->Patt,
RuleDelayed->RD,Function->Fun,Verbatim[Blank][]->Bk}];
MatchQ[rule1,
MB[PT[Patt[_Symbol,Bk],Fun[t_Symbol,
MatrixQ[Unevaluated[t]]&&t=!={{}}&&Length[t]<_&&
Last[Dimensions[t]]<_,HoldAll]],StandardForm]
]
)]
switchMatrixForm[rows_Integer?Positive,
columns_Integer?Positive]:=(
FormatValues[MakeBoxes]=
DeleteCases[FormatValues[MakeBoxes],_?RuleTest];
MakeBoxes[m_?(Function[t,MatrixQ[Unevaluated[t]]&&
(t=!={{}})&&Length[t]<=rows&&
Last[Dimensions[t]]<=columns,HoldAll]),StandardForm]:=
RowBox[{"(",
GridBox[Map[
Function[t,MakeBoxes[t,StandardForm],HoldAll],Unevaluated[m],{2}]],
")"
}])
switchMatrixForm[0,_Integer?NonNegative]:=
FormatValues[MakeBoxes]=DeleteCases[FormatValues[MakeBoxes],_?RuleTest];
switchMatrixForm[_Integer?NonNegative,0]:=
FormatValues[MakeBoxes]=DeleteCases[FormatValues[MakeBoxes],_?RuleTest];
-------------------
Other changes I made:
I think it's more intuitive to have
switchMatrixForm[m,n]
use MatrixForm for matrices with
(rows<=m) && (columns<=n)
That is the way I wrote it.
Using the code above
switchMatrixForm[0,_Integer?NonNegative]
switchMatrixForm[_Integer?NonNegative,0]
will remove any MakeBoxes definitions that
switchMatrixForm would have made, and nothing else.
It seems Last[Dimensions[m]]
is a little faster than
Part[Dimension[m],2]
I am splitting hairs here, since the impact is negligible.
I also think it's clear a package should be used with
(RuleTest) being a Private symbol in the package.
I left that part for you to do.
--------------------
Regards,
Ted Ersek
For Mathematica tips, tricks see
http://www.dot.net.au/~elisha/ersek/Tricks.html
Prev by Date:
**Re: Re: circumference of an ellipse**
Next by Date:
**getting rid of TraditionalForm**
Previous by thread:
**Re: Automatic Display in MatrixForm**
Next by thread:
**Re: Automatic Display in MatrixForm**
| |