-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
latexMatrix arithmetic: trivial simplifications? #58
Comments
It's certainly possible to do this. I did something similar in |
I think that sparse = TRUE is different: replacing 0s with blanks.
That's a worthwhile option, but it's different from, and simpler than,
replacing expressions that evaluate to 0 with 0 (which the kronecker()
method also does) and removing multiplication by 1.
I made good progress on a simplify() function yesterday evening. I have
appointments today, so probably won't get much done, but should have a
preliminary implementation soon.
simplify() could optionally be called by print.latexMatrix() or used
directly.
John
…On 2024-08-21 11:07 p.m., Phil Chalmers wrote:
Caution: External email.
It's certainly possible to do this. I did something similar in |dev/
Arith2.R| for the kronecker product, and I was glad to see this idea
made into John's implementation as well. Perhaps the best way to do this
is to add a |print(..., sparse = TRUE)| to detect any zero
multiplications in defined matrices and have them drop out?
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2303586612>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQU67XI3LYXXCNX4KBTZSVIWFAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBTGU4DMNRRGI>.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Another option is to have a parallel function, matmult(A, B), that takes a simplify argument
More on this later
Get Outlook for iOS<https://aka.ms/o0ukef>
________________________________
From: John Fox ***@***.***>
Sent: Thursday, August 22, 2024 1:28:29 PM
To: friendly/matlib ***@***.***>
Cc: Michael L Friendly ***@***.***>; Author ***@***.***>
Subject: Re: [friendly/matlib] latexMatrix arithmetic: trivial simplifications? (Issue #58)
I think that sparse = TRUE is different: replacing 0s with blanks.
That's a worthwhile option, but it's different from, and simpler than,
replacing expressions that evaluate to 0 with 0 (which the kronecker()
method also does) and removing multiplication by 1.
I made good progress on a simplify() function yesterday evening. I have
appointments today, so probably won't get much done, but should have a
preliminary implementation soon.
simplify() could optionally be called by print.latexMatrix() or used
directly.
John
On 2024-08-21 11:07 p.m., Phil Chalmers wrote:
Caution: External email.
It's certainly possible to do this. I did something similar in |dev/
Arith2.R| for the kronecker product, and I was glad to see this idea
made into John's implementation as well. Perhaps the best way to do this
is to add a |print(..., sparse = TRUE)| to detect any zero
multiplications in defined matrices and have them drop out?
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2303586612>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQU67XI3LYXXCNX4KBTZSVIWFAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBTGU4DMNRRGI>.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
—
Reply to this email directly, view it on GitHub<#58 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AADPNIKKQEG6KQSQ3NO2ZE3ZSXDN3AVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBUGQZTINZSGY>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
On 2024-08-22 7:38 a.m., Michael Friendly wrote:
Caution: External email.
Another option is to have a parallel function, matmult(A, B), that takes
a simplify argument
More on this later
I don't see a need for that, and simplification is more general than for
matrix multiplication.
Because I can't get back to this until later in the day, I've uploaded
simplify.R to dev/ with the work I've done so far -- a generic
simplify() function with a "default" method and some examples. I need to
add a "latexMatrix" method and do some more testing.
John
…
Get Outlook for iOS<https://aka.ms/o0ukef>
________________________________
From: John Fox ***@***.***>
Sent: Thursday, August 22, 2024 1:28:29 PM
To: friendly/matlib ***@***.***>
Cc: Michael L Friendly ***@***.***>; Author ***@***.***>
Subject: Re: [friendly/matlib] latexMatrix arithmetic: trivial
simplifications? (Issue #58)
I think that sparse = TRUE is different: replacing 0s with blanks.
That's a worthwhile option, but it's different from, and simpler than,
replacing expressions that evaluate to 0 with 0 (which the kronecker()
method also does) and removing multiplication by 1.
I made good progress on a simplify() function yesterday evening. I have
appointments today, so probably won't get much done, but should have a
preliminary implementation soon.
simplify() could optionally be called by print.latexMatrix() or used
directly.
John
On 2024-08-21 11:07 p.m., Phil Chalmers wrote:
>
> Caution: External email.
>
>
> It's certainly possible to do this. I did something similar in |dev/
> Arith2.R| for the kronecker product, and I was glad to see this idea
> made into John's implementation as well. Perhaps the best way to do this
> is to add a |print(..., sparse = TRUE)| to detect any zero
> multiplications in defined matrices and have them drop out?
>
> —
> Reply to this email directly, view it on GitHub <https://github.com/
> friendly/matlib#58#issuecomment-2303586612>, or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/
>
ADLSAQU67XI3LYXXCNX4KBTZSVIWFAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBTGU4DMNRRGI>.
> You are receiving this because you are subscribed to this thread.Message
> ID: ***@***.***>
>
—
Reply to this email directly, view it on GitHub<https://github.com/
friendly/matlib#58#issuecomment-2304434726>, or
unsubscribe<https://github.com/notifications/unsubscribe-auth/
AADPNIKKQEG6KQSQ3NO2ZE3ZSXDN3AVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBUGQZTINZSGY>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2304452996>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQRLJTNFGBTSNQRDCOTZSXETRAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBUGQ2TEOJZGY>.
You are receiving this because you commented.Message ID: <friendly/
***@***.***>
|
I had some time between appointments, so I added a For Michael's original example, this produces
In my opinion, that's not more complicated than something like The code in |
But I was thinking of some way, hopefully more elegant, that would avoid having to parse what
Just for fun, I'll try to sketch out a function that works this way. |
I added a
assuming that |
Hi,
On 2024-08-22 2:43 p.m., Michael Friendly wrote:
Caution: External email.
|simplify.R| is very nice. Hard to believe it was something you could do
between appointments!
I just wrote the "latexMatrix" method between appointments; that's
pretty simple.
…
But I was thinking of some way, hopefully more elegant, that would avoid
having to parse what |latexMatrix| generates. That is, the best place to
simplify would be as source, where the elements
of the dot product, |dot(a, b)| can be recognized as in 3 cases:
* 0 in either a[i ] or b[j-]> ignore term
* 1 -> use the other
* -1 -> use the other, and combine with |-|
For another thing, |\cdot| is hard-coded throughout, and we might at
sometime want to change it or allow an argument, `times = c("\cdot",
"\times, "")
Just for fun, I'll try to sketch out a function that works this way.
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2305411510>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQQVVN52ZWXZLRYZF53ZSYWN5AVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBVGQYTCNJRGA>.
You are receiving this because you commented.Message ID: <friendly/
***@***.***>
|
I added
If it was OK to just simplify
replacing this with
|
I tried this, fixing the typos in the last line: The resulting
Some comments: I like your approach, which I'm sure can be made more robust. As you suggest, it's simpler than simplifying at the end. You need to test with a more diverse set of examples and deal with cases like the one above. The reason that I originally didn't simplify by default is that I thought that it was useful to show how the matrix product is formed, given the general purpose of the matlib package. That could still be done by including the You mentioned that you'd like to make the LaTeX multiplication character flexible. I think that can be done by supplying a variable in the package, say |
On second thought, a |
I see why your %*% example failed, where the 0 is actually desired. This calls for a
Or in |
I think that makes the issue more complicated than it needs to be. You should be able to handle this and other problems that may arise in In this case, adding the line
as the next-to-last command in
as desired. More generally, however, I think that there's an argument for making Since I spent quite a bit of time trying to make |
Another issue -- handling
|
Yes, I had that on my todo list, but didn't do it in my initial draft, partly because that was a little trickier given the initial logic. |
I generally agree with your earlier comments about When would it make sense to move |
I was about to propose something very similar, so I think we're on the same wavelength. I suggest the following:
I don't think that it's a good idea to move Does all that sound reasonable? |
I hope that this isn't counterproductive, but for curiosity, I tried to fix
it seems to work OK. |
OK, I'll work from your version of In terms of making it 'robust', I'm not sure what to test for, other than the combinations of |
OK, I incorporated
W/o simplify
|
Sorry; I copy/pasted those tests out of order, after I had changed
|
All this looks promising. |
Then, I'd have to incorporate |
Here's what I'm using:
|
OK -- your test code above, along with some tests is now in My first test gives a strange result, which I don't see the reason for
Gives a naked
We're going out to dinner shortly, so I'm probably done for the day. |
A simple fix is to add
That's correct but clearly could be simplified further. |
Here's another approach, which converts the matrices to numeric if that's possible. Code:
Behaviour:
|
I like this very much. I don't see any reason to stick with latex expressions that evaluate to a number when both X and Y are numeric. Needs more testing, though |
Your code and a bunch of tests is now in I'm going to move |
I agree that it would be best to document the arithmetic functions and operators separately. |
Hmm. My last emailed response doesn't seem to have made it here. I coincidentally added the ability to show partition lines to I wouldn't, of course, have done that if I knew you were working on something similar! I think I've had enough for the night, so will look at what you did tomorrow. |
Hi John, No problem at all. I wasn't sure if we wanted to go down the LaTeX Note that to behave well with |
I took at look at your solution. I think that there's a problem with altering the elements in the On the other hand, one could add a In addition, I didn't realize that one could add Assuming that this plan is sound, I should have time today to try to work everything out along the lines I just suggested. |
Interesting, I hadn't considered carrying the partitions through to other operations. That makes sense for some matrix operators (addition, Hadamard, Kronecker), but for others, like multiplication, I'd have to think more clearly about what it would mean or if it's even useful as the resulting row/col dimensions change. |
@john-d-fox Is it OK for me to edit the documentation for What I did before was mainly a bit of extra text in the
|
Actually, I wasn't suggesting using the partitions in further operations
(as you say, that doesn't necessarily make sense), just that your
approach of modifying the elements of a matrix would interfere with,
e.g., adding, multiplying, etc., matrices.
Also, after further thought, the plan I proposed is too elaborate. I
think that I can do what's needed more simply.
…On 2024-08-29 10:20 a.m., Phil Chalmers wrote:
Caution: External email.
Interesting, I hadn't considered carrying the partitions through to
other operations. That makes sense for some matrix operators (addition,
Hadamard, Kronecker), but for others, like multiplication, I'd have to
think more clearly about what it would mean or if it's even useful as
the resulting row/col dimensions change.
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2317843548>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQU6D5532X36FRKJ35TZT4U2VAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJXHA2DGNJUHA>.
You are receiving this because you were mentioned.Message ID: <friendly/
***@***.***>
|
I added some stuff on partitioned matrices to the vignette. Maybe this is enough? I like the result of
|
Ah, I see what you mean now; thanks for clarifying. Agreed, my approach would needlessly break too much, and I like the idea of adding the partitions as a separate object element. Should it make sense to apply some logic to the operations with partitions down the road we could do so. |
You might want to hold off on partitioned matrices for now since this is
in flux.
I just added dev/partition.R with a partition() function that alters
just the printed representation of the object, and is, e.g., used as
X <- latexMatrix(nrow=5, ncol=6)
partition(X, rows=c(2, 4), columns=c(3, 5))
\begin{pmatrix}
\begin{array}{c c c | c c | c}
x_{11} & x_{12} & x_{13} & x_{14} & x_{15} & x_{16}\\
x_{21} & x_{22} & x_{23} & x_{24} & x_{25} & x_{26}\\
\hline x_{31} & x_{32} & x_{33} & x_{34} & x_{35} & x_{36}\\
x_{41} & x_{42} & x_{43} & x_{44} & x_{45} & x_{46}\\
\hline x_{51} & x_{52} & x_{53} & x_{54} & x_{55} & x_{56}\\
\end{array}
\end{pmatrix}
If this proves sound (and it would be a good idea to try it out to test
it some more), then I would add it to R/latexMatrix.R
…On 2024-08-29 11:45 a.m., Michael Friendly wrote:
Caution: External email.
I added some stuff on partitioned matrices to the vignette. |[| indexing
and |r/c-bind| work nicely here.
Maybe this is enough? I like the result of |addPartitions()|, but not at
the expense of making "latexMatrix" objects
more complicated.
|> addPartitions(M, row=c(2,4), col = c(2,4)) \begin{pmatrix} \beta_{11}
& \beta_{12} \bigm| & \beta_{13} & \beta_{14} \bigm| & \beta_{15} &
\beta_{16} \\ \beta_{21} & \beta_{22} \bigm| & \beta_{23} & \beta_{24}
\bigm| & \beta_{25} & \beta_{26} \\ \hline \beta_{31} & \beta_{32}
\bigm| & \beta_{33} & \beta_{34} \bigm| & \beta_{35} & \beta_{36} \\
\beta_{41} & \beta_{42} \bigm| & \beta_{43} & \beta_{44} \bigm| &
\beta_{45} & \beta_{46} \\ \hline \beta_{51} & \beta_{52} \bigm| &
\beta_{53} & \beta_{54} \bigm| & \beta_{55} & \beta_{56} \\ \end{pmatrix} |
image.png (view on web) <https://github.com/user-attachments/assets/
ecd4fe9e-1d4c-49f8-9088-5f77dce5fa80>
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2318192330>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQWTMBV4WYAS26TEVX3ZT46ZNAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJYGE4TEMZTGA>.
You are receiving this because you were mentioned.Message ID: <friendly/
***@***.***>
|
I think that adding the partition information to the object is more
elaborate than necessary. The partition() function I just added in
dev/partition.R simply modifies the $matrix slot of the "latexMatrix"
object and so affects only the printed representation of the object.
…On 2024-08-29 11:47 a.m., Phil Chalmers wrote:
Caution: External email.
Ah, I see what you mean now; thanks for clarifying. Agreed, my approach
would needlessly break too much, and I like the idea of adding the
partitions as a separate object element. Should it make sense to apply
some logic to the operations with partitions down the road we could do so.
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2318198664>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQS6O7PV2YWQWGG5CIDZT47CXAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJYGE4TQNRWGQ>.
You are receiving this because you were mentioned.Message ID: <friendly/
***@***.***>
|
Your |
I did some further testing and then added I wonder whether this "issue" has gotten too long! |
This issue has certainly grown past its original intent... and, I honestly can't tell if it has been resolved. Does the |
An interesting edge-case with matrix indexing:
This is certainly OK, in that it renders as a 1x1 pmatrix: (1). Would we ever want it otherwise, i.e., just "1"? |
I don't think that we should *always* make 1 x 1 matrices into "scalars"
but in this case I think that it makes sense to return the contents of
the single element as a scalar (i.e., one-element R character vector).
…On 2024-08-30 5:47 p.m., Michael Friendly wrote:
Caution: External email.
An interesting edge-case with matrix indexing:
|> A <- matrix(1:4, nrow =2) |> latexMatrix() > A[1,1] \begin{pmatrix} 1
\\ \end{pmatrix} |
This is certainly OK, in that it renders as a 1x1 pmatrix: (1). Would we
ever want it otherwise, i.e., just "1"?
—
Reply to this email directly, view it on GitHub <https://github.com/
friendly/matlib#58#issuecomment-2322382905>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/
ADLSAQULY4ZWCZ5MI52GHYDZUDR7RAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSGM4DEOJQGU>.
You are receiving this because you were mentioned.Message ID: <friendly/
***@***.***>
|
An |
I wrote sections on Partitioned matrices and Kronecker products in the vignette. Take a look, make edits or suggestions, etc. |
I'm looking at the Aside from the fact that some generated equations aren't rendered, I note:
|
My emailed comments don't seem to have made it here. (1) I can make the (2) I can alter the indexing method so that it returns a one-element character vector when a single element is selected. Unless someone objects, I'll go ahead and do this. I'll wait awhile in case someone else is working on With respect to the vignette. I think that it would be desirable to add partition lines to the final Kronecker-product example. Also, it is probably worth mentioning that |
Oh, one more thing. With respect to returning a single element as a "scalar": Should this always happen? If not, Phil seems suggest that we add an |
As a thought, the ambiguity would be removed if bracket indexing always returned the matrix elements rather than a |
That approach loses the LaTeX matrix environment if it's not |
No disagreement on that, though by the same token why not use the getBody()
logic to get the scalar element?
Perhaps instead of A[,] with a drop logical a A[,,body=TRUE] could be used?
…--- Sent from my mobile phone. Please excuse any tpyos or unusual
autocorrect-ships
On Sat, Aug 31, 2024, 12:00 PM John Fox ***@***.***> wrote:
That approach loses the LaTeX matrix environment if it's not pmatrix. If
you want the character sub-matrix, why not just use, e.g., getBody(X[1:2,
3:4]) or getBody(X)[1:2, 3:4]?
—
Reply to this email directly, view it on GitHub
<#58 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJXEYDFP6GOVZ6STIPG4A3ZUHSCFAVCNFSM6AAAAABM42L7RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSHE2DINRVHE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Too many things in this thread now! (1) Re: (2) Re: indexing: Just use (3) Re: the Kronecker product example -- I'll add the partition lines. (4) I won't touch (5) Any idea about:
|
"Too many things in this thread now!" Indeed. I hoped we could close the thread and open another (or other) more specific thread(s) as necessary. With respect to: (1) I've gone with (2) Yes, I had (3) Thank you. (4) I committed the small change I made to the (5) I have no idea. More generally, I often find the behaviour of Roxygen and GitHub actions mysterious. |
Sorry -- I accidentally closed and then reopened this issue. I do think that we should close it, but I didn't mean to preempt the decision... |
Closing now... |
In my vignette example of linear hypotheses, the following generates the C & B matrices:
which generates a correct result, but one that is hard to look at:
and looks like this:
Trivial simplification of this could:
1 \cdot
Just removing the 0 * terms:
Is this possible? An operator function like
%*%
can't take other arguments, but could it handle an option in the environment?The text was updated successfully, but these errors were encountered: