Skip to content
Open
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/diverential/0.3.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Christopher Hecker

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.
50 changes: 50 additions & 0 deletions packages/preview/diverential/0.3.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Diverential
`diverential` is a [Typst](https://github.com/typst/typst) package simplifying the typesetting of differentials. It is the equivalent to LaTeX's `diffcoeff`, though not as mature.


## Overview
`diverential` allows normal, partial, compact, and separated derivatives with smart degree calculations.
```ts
#import "@preview/diverential:0.3.0": *

$ dv(f, x, deg: 2, eval: 0) $
$ dvp(f, x, y, eval: 0, evalsym: "[") $
$ dvpc(f, x) $
$ dvps(f, #([x], 2), #([y], [n]), #([z], [m]), eval: 0) $
```
<img src="examples/overview.jpg" width="150">


## `dv`
`dv` is an ordinary derivative. It takes the function as its first argument and the variable as its second one. A degree can be specified with `deg`. The derivate can be specified to be evaluated at a point with `eval`, the brackets of which can be changed with `evalsym`. `space` influences the space between derivative and evaluation bracket. Unless defined otherwise, no space is set by default, except for `|`, where a small gap is introduced.

### `dvs`
Same as `dv`, but separates the function from the derivative.
Example:
$$ \frac{\mathrm{d}}{\mathrm{d}x} f $$

### `dvc`
Same as `dv`, but uses a compact derivative.
Example:
$$ \mathrm{d}_x f $$


## `dvp`
`dv` is a partial derivative. It takes the function as its first argument and the variable as the rest. For information on `eval`, `evalsym`, and `space`, read the description of `dv`.
The variable can be one of these options:
- plain variable, e.g. `x`
- list of variables, e.g. `x, y`
- list of variables with higher degrees (type `(content, integer)`), e.g. `x, #([y], 2)`
The degree is smartly calculated: If all degrees of the variables are integers, the total degree is their sum. If some are content, the integer ones are summed (arithmetically) up and added to the visual sum of the content degrees. Example: `#([x], n), #([y], 2), z` → $\frac{\partial^{n+3}}{\partial x^n\,\partial y^2\,\partial z}$
Specifying `deg` manually is always possible and might be required in more complicated cases.

### `dvps`
Same as `dvp`, but separates the function from the derivative.
Example:
$$ \frac{\partial}{\partial x} f $$

### `dvpc`
Same as `dvp`, but uses a compact derivative.
Note: If supplying multiple variables, `deg` is ignored.
Example:
$$ \partial_x f $$
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
289 changes: 289 additions & 0 deletions packages/preview/diverential/0.3.0/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
#let _add-eval(expression, eval, evalsym: "|", space: none) = {
/// Add evalutation brackets.
/// - `expression`: Expression to wrap the brackets around.
/// - `eval`: Point of evalutation.
/// - `evalsym`: Brackets of evalutation. This must be in `(`, `[`, `{`, `|`.

if space != none {
expression += space
}

let left = ""
let right = ""
if evalsym == "(" or evalsym == ")" {
left = "("
right = ")"
} else if evalsym == "[" or evalsym == "]" {
left = "["
right = "]"
} else if evalsym == "{" or evalsym == "}" {
left = "{"
right = "}"
} else if evalsym == "|" {
left = ""
right = "|"
if space == none {
expression += h(0.1em)
}
} else {
panic("invalid eval symbol: " + evalsym)
}

return math.attach(math.lr([#left #expression #right]), b: eval)
}

#let _degree(array) = {
/// Calculate the degree of an array taking the value in tuples into account.
let acc = 0
let cont = []

for x in array {
if type(x) == "array" and x.len() > 1 {
if type(x.at(1)) == "integer" {
acc += x.at(1)
} else if type(x.at(1)) == "content" {
if cont == [] {
cont += x.at(1)
} else {
cont += [$+ #x.at(1)$]
}
} else {
panic("invalid degree: " + x.at(1))
}
} else {
acc += 1
}
}

if cont == [] {
acc
} else {
[#cont + #acc]
}
}

#let dv(f, x, deg: none, eval: none, evalsym: "|", space: none) = {
/// Normal differential.
/// `f`: Function to take the derivative of.
/// `x`: Variable with respect to which to take the derivative of.
/// `deg`: Degree of the derivative.
/// `eval`: Point of evaluation.
/// - `evalsym`: Brackets of evalutation. This must be in `(`, `[`, `{`, `|`.

let frac = math.frac([
#if deg != none {
math.attach(math.dif, t: deg)
} else {
math.dif
}
#f
],
[
#math.dif
#if deg != none {
math.attach(x, t: deg)
} else {
x
}
]
)
if eval == none {
frac
} else {
_add-eval(frac, eval, evalsym: evalsym, space: space)
}
}

#let dvc(f, x, deg: none, eval: none, evalsym: "|", space: none) = {
/// Compact differential.
/// `f`: Function to take the derivative of.
/// `x`: Variable with respect to which to take the derivative of.
/// `deg`: Degree of the derivative.
/// `eval`: Point of evaluation.
/// - `evalsym`: Brackets of evalutation. This must be in `(`, `[`, `{`, `|`.

let expression = [
#if deg != none {
math.attach(math.dif, t: deg, b: x)
} else {
math.attach(math.dif, b: x)
}
#f
]
if eval == none {
expression
} else {
_add-eval(expression, eval, evalsym: evalsym)
}
}

#let dvs(f, x, deg: none, eval: none, evalsym: "|", space: none) = {
/// Separate differential.
/// `f`: Function to take the derivative of.
/// `x`: Variable with respect to which to take the derivative of.
/// `deg`: Degree of the derivative.
/// `eval`: Point of evaluation.
/// - `evalsym`: Brackets of evalutation. This must be in `(`, `[`, `{`, `|`.

let frac = [
#math.frac(if deg != none {
math.attach(math.dif, t: deg)
} else {
math.dif
},
[
#math.dif
#if deg != none {
math.attach(x, t: deg)
} else {
x
}
]
)
#f
]
if eval == none {
frac
} else {
_add-eval(frac, eval, evalsym: evalsym, space: space)
}
}

#let dvp(f, ..x, deg: none, eval: none, evalsym: "|", space: none) = {
/// Partial differential.
/// `f`: Function to take the derivative of.
/// `x`: Variable(s) with respect to which to take the derivative of. Multiple variables can be supplied and a higher-order degree derivative with respect to one variable with a pair of variable and degree (e.g. `[x], ([y], 2)`). If the degree is an integer, the total degree will be the sum of the numbers. If it's content, the variables are added up. Specifying `deg` manually is always possible and might be required in more complicated cases.
/// `deg`: Degree of the derivative.
/// `eval`: Point of evaluation.
/// - `evalsym`: Brackets of evalutation. This must be in `(`, `[`, `{`, `|`.

if x.named() != (:) {
panic("invalid named argument: " + str(x.named().keys().at(0)))
}
x = x.pos()

let degree = deg
if deg == none {
degree = _degree(x)
}

let frac = math.frac([
#if deg != none or (type(degree) == "integer" and degree > 1) or type(degree) == "content" {
math.attach(math.partial, t: [#degree])
} else {
math.partial
}
#f
],
for (i, term) in x.enumerate() {
if i != 0 {
[#space]
}
if type(term) == "array" and ((type(term.at(1)) == "integer" and term.at(1) > 1) or type(term.at(1)) == "content") {
[#math.partial #math.attach(term.at(0), t: [#term.at(1)])]
} else if x.len() == 1 and deg != none {
[#math.partial #math.attach(term, t: deg)]
} else {
[#math.partial #term]
}
}
)

if eval == none {
frac
} else {
_add-eval(frac, eval, evalsym: evalsym, space: space)
}
}

#let pdv(f, ..x, deg: none, eval: none, evalsym: "|", space: none) = dvp(f, ..x, deg: deg, eval: eval, evalsym: evalsym)

#let dvpc(f, ..x, deg: none, eval: none, evalsym: "|", space: none) = {
/// Compact partial differential.
/// `f`: Function to take the derivative of.
/// `x`: Variable(s) with respect to which to take the derivative of. Multiple variables can be supplied and a higher-order degree derivative with respect to one variable with a pair of variable and degree (e.g. `[x], ([y], 2)`). In this case, `deg` is ignored.
/// `deg`: Degree of the derivative.
/// `eval`: Point of evaluation.
/// - `evalsym`: Brackets of evalutation. This must be in `(`, `[`, `{`, `|`.


if x.named() != (:) {
panic("invalid named argument: " + str(x.named().keys().at(0)))
}
x = x.pos()

let expression = for (i, term) in x.enumerate() {
if i != 0 {
[#space]
}
if type(term) == "array" and ((type(term.at(1)) == "integer" and term.at(1) > 1) or type(term.at(1)) == "content") {
[#math.attach(math.partial, b: term.at(0), t: [#term.at(1)])]
} else if x.len() == 1 and deg != none {
[#math.attach(math.partial, b: term, t: deg)]
} else {
[#math.attach(math.partial, b: term)]
}
}
expression += [#f]

if eval == none {
expression
} else {
_add-eval(expression, eval, evalsym: evalsym)
}
}

#let dvcp(f, x, deg: none, eval: none, evalsym: "|", space: none) = dvpc(f, x, deg: deg, eval: eval, evalsym: evalsym, space: space)
#let pdvc(f, x, deg: none, eval: none, evalsym: "|", space: none) = dvpc(f, x, deg: deg, eval: eval, evalsym: evalsym, space: space)

#let dvps(f, ..x, deg: none, eval: none, evalsym: "|", space: none) = {
/// Separate partial differential.
/// `f`: Function to take the derivative of.
/// `x`: Variable(s) with respect to which to take the derivative of. Multiple variables can be supplied and a higher-order degree derivative with respect to one variable with a pair of variable and degree (e.g. `[x], ([y], 2)`). If the degree is an integer, the total degree will be the sum of the numbers. If it's content, the variables are added up. Specifying `deg` manually is always possible and might be required in more complicated cases.
/// `deg`: Degree of the derivative.
/// `eval`: Point of evaluation.
/// - `evalsym`: Brackets of evalutation. This must be in `(`, `[`, `{`, `|`.

if x.named() != (:) {
panic("invalid named argument: " + str(x.named().keys().at(0)))
}
x = x.pos()

let degree = deg
if deg == none {
degree = _degree(x)
}
let frac = [
#math.frac([
#if deg != none or (type(degree) == "integer" and degree > 1) or type(degree) == "content" {
math.attach(math.partial, t: [#degree])
} else {
math.partial
}
],
for (i, term) in x.enumerate() {
if i != 0 {
[#space]
}
if type(term) == "array" and ((type(term.at(1)) == "integer" and term.at(1) > 1) or type(term.at(1)) == "content") {
[#math.partial #math.attach(term.at(0), t: [#term.at(1)])]
} else if x.len() == 1 and deg != none {
[#math.partial #math.attach(term, t: deg)]
} else {
[#math.partial #term]
}
}
)
#f
]

if eval == none {
frac
} else {
_add-eval(frac, eval, evalsym: evalsym, space: space)
}
}

#let dvsp(f, x, deg: none, eval: none, evalsym: "|", space: none) = dvps(f, x, deg: deg, eval: eval, evalsym: evalsym, space: space)

#let pdvs(f, x, deg: none, eval: none, evalsym: "|", space: none) = dvps(f, x, deg: deg, eval: eval, evalsym: evalsym, space: space)
8 changes: 8 additions & 0 deletions packages/preview/diverential/0.3.0/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "diverential"
version = "0.3.0"
entrypoint = "lib.typ"
authors = ["Christopher Hecker"]
license = "MIT"
description = "Format differentials conveniently."
exclude = ["examples"]