Skip to content

Commit

Permalink
add dot-test.R; edit vignette; bump pkg v0.9.91
Browse files Browse the repository at this point in the history
  • Loading branch information
friendly committed Aug 24, 2024
1 parent d52f7fd commit 748b737
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Package: matlib
Type: Package
Title: Matrix Functions for Teaching and Learning Linear Algebra and
Multivariate Statistics
Version: 0.9.9
Date: 2024-07-30
Version: 0.9.91
Date: 2024-08-24
Authors@R: c(person(given = "Michael", family = "Friendly",
role=c("aut", "cre"), email="[email protected]",
comment=c(ORCID="0000-0002-3237-0941")),
Expand Down
54 changes: 54 additions & 0 deletions dev/dot-test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Test using dot() for matrix mult

numericDimensions <- matlib:::numericDimensions
updateWrapper <- matlib:::updateWrapper
parenthesize <- matlib:::parenthesize

`%*%.latexMatrix` <- function(x, y){
if (!inherits(y, "latexMatrix")){
stop(deparse(substitute(y)),
" is not of class 'latexMatrix'")
}
numericDimensions(x)
numericDimensions(y)
X <- getBody(x)
Y <- getBody(y)
dimX <- dim(X)
dimY <- dim(Y)
if (dimX[2] != dimY[1]){
stop('matricies are not conformable for multiplication')
}

Z <- matrix("", nrow(X), ncol(Y))
for (i in 1:nrow(X)){
for (j in 1:ncol(Y)){
for (k in 1:ncol(X)){
Z[i, j] <- dot(X[i, ], Y[, j])
}
}
}
result <- latexMatrix(Z)
result <- updateWrapper(result, getWrapper(x))
result$dim <- dim(Z)
result
}

if(FALSE) {
(A <- latexMatrix(matrix(c(1, -3, 0, 1), 2, 2)))
(B <- latexMatrix(matrix(c(5, 3, -1, 4), 2, 2)))
(C <- latexMatrix(symbol="c", 2, 2))
(D <- latexMatrix(symbol="d", 2, 2))

A %*% B

as.double(A)
as.double(B)
as.double(A) %*% as.double(B)

A %*% C
A %*% (B - C)
A %*% -B
(A - 2*D) %*% B

A %*% solve(B)
}
28 changes: 26 additions & 2 deletions vignettes/latex-equations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,33 @@ If the elements of a matrix are valid R variable names, then it is also possible
as.double(-2*M, locals=c(a=1, b=0, c=-2, d=-4, e=7, f=-1, g=0, h=4, i=6))
```

### Transpose and matrix products
### Matrix products and transpose

The transpose, `t()`, and matrix product, `%*%` of `"latexMatrix"` objects are similarly straightforward. This directly transposes the matrix, as opposed to superscript notation,
The product of two matrices is given by `%*%` for `"latexMatrix"` objects.


```{r product1, results='asis'}
A %*% B
```

To get the numeric result evaluated, use `as.double()` and perform ordinary
matrix multiplication:

```{r product2, results='asis'}
as.double(A) %*% as.double(B) |> latexMatrix()
```

The LaTeX symbol for multiplication is a centered dot, `\\cdot` ($\cdot$), by default.
This can be changed by changing `options(latexMultSymbol)`,
e.g, to use the $\times$ symbol instead, use:

```{r multSymbol, eval=FALSE}
options(latexMultSymbol = "\\times")
```


The transpose, `t()` of `"latexMatrix"`
objects is similarly straightforward. This directly transposes the matrix, as opposed to superscript notation,
$\mathbf{D}^\top$ or $\mathbf{D}^\prime$ which is implicit.

```{r transpose}
Expand Down

0 comments on commit 748b737

Please sign in to comment.