Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/preview/auto-div/0.1.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 euwbah

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 65 additions & 0 deletions packages/preview/auto-div/0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# auto-div

For automatic polynomial division.

Currently, only integer and fractional coefficients are supported. More number systems planned for future releases.

## Quick start

For example, to evaluate $x^4 + 3x^3 + \frac{5}{2} x + 6$ divided by $x + 2$, specify the coefficients `(1, 3, 0, "5/2", 6)` and `(1, 2)` respectively.

```typst
#import "@preview/auto-div:0.1.0": poly-div, poly-div-working

Just the working itself (works both inside and outside math mode):

$ #poly-div-working((1, 3, 0, "5/2", 6), (1, 2)) $

Obtaining parts of the polynomial division as math:

#let result = poly-div((1, 3, 0, "5/2", 6), (1, 2))
$
result.dividend = (result.quotient) (result.divisor) + (result.remainder)
$

Obtaining quotient and remainder as an array of polynomial coefficients:

Quotient: #result.quot-coeff

Remainder: #result.rem-coeff
```

![Example image](imgs/example.png)

> 💡 To do calculations with the returned coefficients' fraction strings, use [`fractus`](https://typst.app/universe/package/fractus/).

## Methods

### `poly-div`

Polynomial long division.

If only displaying the working of a polynomial long division without needing results in an array of numbers, use
`poly-div-working` instead.

#### Parameters

- `dividend` (`array`, positional): Coefficients of the dividend. Last element is x^0. For fractional coefficients, specify as strings e.g. "15/4", otherwise use integers.
- `divisor` (`array`, positional): Coefficients of divisor. Last element is for x^0. For fractional coefficients, specify as strings e.g. "15/4", otherwise use integers.
- `var` (`content`, named): The variable of the polynomial. Default is $x$.

#### Returns

Returns a dict: `(working:, quotient:, remainder:, quot-coeff:, rem-coeff:)`:

- `working` (`content`): displayable content showing the working (as a table)
- `dividend` (`math.equation`): the dividend as a math equation object
- `divisor` (`math.equation`): the divisor as a math equation object
- `quotient` (`math.equation`): the quotient as a math equation object
- `remainder` (`math.equation`): the remainder as a math equation object
- `quot-coeff` (`array`): the coefficients of the quotient polynomial as an array of fraction strings
- `rem-coeff` (`array`): the coefficients of the remainder polynomial as an array of fraction strings

### `poly-div-working`

Same as `poly-div`, but returns only the `working`.
188 changes: 188 additions & 0 deletions packages/preview/auto-div/0.1.0/auto-div.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#import "@preview/fractus:0.1.0" as fr

/// Polynomial long division.
///
/// If only displaying the working of a polynomial long division without needing results in an array of numbers, use
/// `poly-div-working` instead.
///
/// == Returns
///
/// Returns a dict: `(working:, dividend:, divisor:, quotient:, remainder:, quot-coeff:, rem-coeff:)`
///
/// - `working` (`content`): displayable content showing the working (as a table)
/// - `dividend` (`math.equation`): the dividend as a math equation object
/// - `divisor` (`math.equation`): the divisor as a math equation object
/// - `quotient` (`math.equation`): the quotient as a math equation object
/// - `remainder` (`math.equation`): the remainder as a math equation object
/// - `quot-coeff` (`array`): the coefficients of the quotient polynomial as an array of fraction strings
/// - `rem-coeff` (`array`): the coefficients of the remainder polynomial as an array of fraction strings
///
///
/// - dividend (array): Coefficients of the dividend. Last element is x^0. For fractional coefficients, specify as strings e.g. "15/4".
/// - divisor (array): Coefficients of divisor. Last element is for x^0. For fractional coefficients, specify as strings e.g. "15/4".
/// - var (content): The variable of the polynomial. Default is `$x$`.
/// -> (working:, dividend:, divisor:, quotient:, remainder:, quot-coeff:, rem-coeff:)
#let poly-div(
dividend,
divisor,
var: $x$
) = {

let paren-space = 0.4em

/// Use this function to construct fractus fraction objects.
///
/// In fractus, fractions are represented as "n/d" strings, and operations operate on strings.
let simplify = fr.simplify

/// Use this to display a fraction string of the form `"n/d"` as a math mode fraction.
let as-math(frc) = {
if frc == none {
return none
}
let (n, d) = frc.replace("-", "−").split("/")
if d != "1" {
$#n/#d$
} else {
$#n$
}
}

let dividend = dividend.map(simplify)
let remainder = dividend.map(x => x) // shallow copy
let divisor = divisor.map(simplify)
let max-quotient-deg = dividend.len() - divisor.len()
let quotient = (simplify(0),) * (max-quotient-deg + 1)

// List of table cells for the columns below the dividend, excludes
// the divisor, quotient, and dividend itself.
//
// One element per table row.
let working-table-rows = ()

// List of start positions of table horizontal lines
// for denoting subtractions
let subtraction-underline-pos = ()

/// Express polynomial coefficients as list of table cells
/// (left padding not included, right padding included)
let expr-cells(poly) = {
let cells = ()
for (idx, coeff) in poly.enumerate() {
if fr.float(coeff) != 0 {
if idx != poly.len() - 1 {
// non-constant term, no need to specify 1 or -1, just use + or -
if idx != 0 and fr.float(coeff) > 0 {
// prepend explicit + for subsequent terms
coeff = if fr.float(coeff) == 1 {$+$} else {$+ #as-math(coeff)$}
} else if fr.float(coeff) == 1 {
// if leading term and positive 1 coeficient, no need coefficient.
coeff = $$
} else if fr.float(coeff) == -1 {
coeff = $-$
} else if fr.float(coeff) >= 0 {
coeff = as-math(coeff)
} else {
coeff = $-#as-math(fr.opposite(coeff))$
}
} else {
if idx != 0 and fr.float(coeff) > 0 {
coeff = $+#as-math(coeff)$
} else if fr.float(coeff) >= 0 {
coeff = as-math(coeff)
} else {
coeff = $-#as-math(fr.opposite(coeff))$
}
}
let deg = poly.len() - 1 - idx
if deg > 1 {
cells.push([$#coeff#var^#deg$])
} else if deg == 1 {
cells.push([#coeff#var])
} else {
cells.push([#coeff])
}
} else {
if idx != 0 {
cells.push([]) // empty cell for 0 coefficient
} else {
cells.push($0$)
}
}
}
return cells
}

while remainder.len() >= divisor.len() {
let quotient-degree = remainder.len() - divisor.len()
let quotient-coeff = fr.division(remainder.first(), divisor.first())
let subtract = divisor.map(x => fr.product(x, quotient-coeff)) + (simplify(0),) * quotient-degree
let rem = remainder.zip(subtract).map(((x, s)) => fr.difference(x, s))
if fr.float(rem.first()) != 0 {
panic("Failed to eliminate top degree coefficient")
}
quotient.at(quotient.len() - quotient-degree - 1) = quotient-coeff
remainder = rem
while remainder.len() >= 1 and fr.float(remainder.first()) == 0 {
remainder = remainder.slice(1)
}
if quotient-coeff != 0 {
let sub-cells = expr-cells(subtract)
sub-cells.first() = $- ($ + sub-cells.first()
sub-cells.last() = sub-cells.last() + $)$
working-table-rows.push(sub-cells)
let remainder-cells = expr-cells(remainder)
if remainder-cells.len() != 0 {
remainder-cells.last() = remainder-cells.last() + $#h(paren-space)$
}
working-table-rows.push(remainder-cells)
subtraction-underline-pos.push(1 + dividend.len() - divisor.len() - quotient-degree)
}
}

let columns = 1 + dividend.len()
let cells = ()
// quotient row
let quot-cells = expr-cells(quotient)
quot-cells.last() = quot-cells.last() + $#h(paren-space)$
cells += ([],) * (columns - quotient.len()) + quot-cells
// divisor & dividend row
let div-cells = expr-cells(dividend)
div-cells.last() = div-cells.last() + $#h(paren-space)$
cells += (expr-cells(divisor).reduce((acc, x) => acc + x), ) + div-cells
// working rows
for row-cells in working-table-rows {
cells += ([],) * (columns - row-cells.len()) + row-cells
}
let content = table(
align: right,
inset: (x: 0pt, y: 5pt),
column-gutter: 2pt,
columns: columns,
stroke: none,
table.vline(x: 1, start: 1, end: 2, stroke: 0.7pt),
table.hline(y: 1, start: 1, stroke: 0.7pt),
..subtraction-underline-pos.enumerate().map(((idx, start)) => table.hline(y: 3 + idx * 2, start: start, stroke: 0.7pt)),
..cells
)
(
working: content,
dividend: expr-cells(dividend).reduce((acc, x) => acc + x),
divisor: expr-cells(divisor).reduce((acc, x) => acc + x),
quotient: expr-cells(quotient).reduce((acc, x) => acc + x),
remainder: expr-cells(remainder).reduce((acc, x) => acc + x),
quot-coeff: quotient,
rem-coeff: remainder
)
}

/// Use this to directly show the working for a polynomial long division.
///
/// - dividend (array): Coefficients of the dividend. Last element is x^0. For fractional coefficients, specify as strings e.g. "15/4".
/// - divisor (array): Coefficients of divisor. Last element is for x^0. For fractional coefficients, specify as strings e.g. "15/4".
/// - var (content): The variable of the polynomial
///
/// -> (working:, quotient:, remainder:, quot-coeff:, rem-coeff:)
#let poly-div-working(dividend, divisor, var: $x$) = {
poly-div(dividend, divisor, var: var).at("working")
}
Binary file added packages/preview/auto-div/0.1.0/imgs/example.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 packages/preview/auto-div/0.1.0/lib.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions packages/preview/auto-div/0.1.0/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import "auto-div.typ": poly-div, poly-div-working
13 changes: 13 additions & 0 deletions packages/preview/auto-div/0.1.0/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "auto-div"
version = "0.1.0"
entrypoint = "lib.typ"
authors = ["euwbah"]
license = "MIT"
description = "Automatic polynomial division"
compiler = "0.13.1"
repository = "https://github.com/euwbah/typst-packages/tree/auto-div"
keywords = ["polynomial", "division", "math"]
categories = ["components", "visualization", "scripting"]
disciplines = ["mathematics", "education"]
exclude = ["./example.pdf", "./example.typ", "./imgs"]