Skip to content

Commit

Permalink
matrix: Enable rounding for matrix multiplication (#531)
Browse files Browse the repository at this point in the history
Round multiplied cells in `mul-mat`.
  • Loading branch information
johannes-wolf authored Mar 17, 2024
1 parent 2e2ddcb commit cacaf38
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Fixed `decorations.flat-brace` vertical positioning
- Fixed drawing of mirrored plot axis ticks.
- Fixed plots with only annotions.
- Added matrix rounding to fix rounding errors when using lots of transforms

# 0.2.1

Expand Down
11 changes: 6 additions & 5 deletions src/matrix.typ
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#import "vector.typ"

// Global rounding precision
#let precision = 8

#let cos(angle) = {
return calc.round(calc.cos(angle), digits: 10)
return calc.round(calc.cos(angle), digits: precision)
}

#let sin = calc.sin

#let pi = calc.pi

/// Create identity matrix with dimensions $m times n$
Expand Down Expand Up @@ -152,9 +154,8 @@
(0,0,0,1))
}

// Multiply matrices on top of each other.
/// Multiply matrices on top of each other.
#let mul-mat(..matrices) = {
// assert(ms.named() == (:), message: "Unexpected named arguments: " + repr(ms.named()))
matrices = matrices.pos()
let out = matrices.remove(0)
for matrix in matrices {
Expand All @@ -166,7 +167,7 @@
for i in range(m) {
(
for j in range(p) {
(range(n).map(k => out.at(i).at(k) * matrix.at(k).at(j)).sum(),)
(calc.round(range(n).map(k => out.at(i).at(k) * matrix.at(k).at(j)).sum(), digits: precision),)
}
,)
}
Expand Down
Binary file modified tests/chart/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/intersection/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot/contour/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot/line/between/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot/line/fill/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/transform/precission/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions tests/transform/precission/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#set page(width: auto, height: auto)
#import "/src/lib.typ": *
#import "/tests/helper.typ": *

#test-case({
import cetz.draw: *
for i in range(0, 90 + 1) {
rotate(22deg)
translate((0,1))
rotate(-25deg)
translate((0,-1))
scale(y: -1)
}

// With rounding errors, the line and decoration
// won't be at the same location.
line((-1,0), (1,0), stroke: red)

cetz.decorations.wave(line((-1,0), (1,0), stroke: green))
})

0 comments on commit cacaf38

Please sign in to comment.