Skip to content

Commit

Permalink
Add taylorterm() for Taylor series' n-th term; add 'big' to J/H matrices
Browse files Browse the repository at this point in the history
Also, correct innerproduct to the more usual form <u,v>.
  • Loading branch information
Leedehai committed Dec 11, 2023
1 parent 142fbde commit 4434543
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 46 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
:green_book: The [manual](https://github.com/Leedehai/typst-physics/blob/master/physica-manual.pdf).
<p align="center">
<img width="545" alt="logo" src="https://github.com/Leedehai/typst-physics/assets/18319900/781d5747-4842-49f6-bb41-a1fd4d17a1ff">
<img width="545" alt="logo" src="https://github.com/Leedehai/typst-physics/assets/18319900/ed86198a-8ddb-4473-aed3-8111d5ecde60">
</p>

# The physica package for Typst
Expand All @@ -17,27 +17,21 @@ This [Typst](https://typst.app) package provides handy typesetting utilities for
natural sciences, including:
* Braces,
* Vectors and vector fields,
* Matrices,
* Matrices, including Jacobian and Hessian,
* Dirac braket notations,
* Common math functions,
* Differentials and derivatives, including partial derivatives of mixed orders with automatic order summation,
* Familiar "h-bar", tensor abstract index notations, isotopes,
* Familiar "h-bar", tensor abstract index notations, isotopes, Taylor series term,
* Signal sequences i.e. digital timing diagrams.

:warning: [Typst](https://typst.app) is in beta and evolving, and this package
evolves with it. As a result, no backward compatibility is guaranteed yet.

:information_source: In response to the official Typst package [guideline](https://github.com/typst/packages/tree/main#submission-guidelines)
"Package names should not be merely descriptive to create level grounds for
everybody", this package was renamed from `physics` to `physica`.

## A quick look

See the [manual](https://github.com/Leedehai/typst-physics/blob/master/physica-manual.pdf) for more details.

![demo-quick](https://github.com/Leedehai/typst-physics/assets/18319900/4a9f40df-f753-4324-8114-c682d270e9c7)

![demo-larger](https://github.com/Leedehai/typst-physics/assets/18319900/3a5cc613-bf36-4b13-ac28-790150c5fb94)
[demo.typ](https://github.com/Leedehai/typst-physics/blob/master/demo.typ)
![demo-larger](https://github.com/Leedehai/typst-physics/assets/18319900/ed2d95af-4bb9-4131-957f-ffba4f932a03)

## Using physica in your Typst document

Expand Down
6 changes: 5 additions & 1 deletion demo.typ
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ curl vb(E) = - pdv(vb(B), t),
quad
tensor(Lambda,+mu,-nu) = dmat(1,RR),
quad
dd(x,y),
quad
dd(vb(x),y,[3]),
quad
dd(x,y,2,d:Delta,p:and),
quad
dv(phi,t,d:upright(D)) = pdv(phi,t) + vb(u) grad phi \
H(f) = hmat(f;x,y),
H(f) = hmat(f;x,y;delim:"[",big:#true),
quad
vb(v^a) = sum_(i=1)^n alpha_i vu(u^i),
quad
Expand Down
Binary file modified physica-manual.pdf
Binary file not shown.
109 changes: 87 additions & 22 deletions physica-manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#align(center)[
Leedehai \
#linkurl("GitHub repo", "https://github.com/leedehai/typst-physics") |
#linkurl("Typst index", "https://typst.app/docs/packages/")
#linkurl("GitHub", "https://github.com/leedehai/typst-physics") |
#linkurl("Typst", "https://typst.app/docs/packages/")
]

#set par(justify: true)
Expand Down Expand Up @@ -211,6 +211,14 @@ All symbols need to be used in *math mode* `$...$`.
[`cprod`],
[`a cprod b` #sym.arrow $a cprod b$],
[cross product],

[`innerproduct`],
[`iprod`],
[
`iprod(u, v)` #sym.arrow $iprod(u, v)$ \
`iprod(sum_i a_i, b)` \ #sym.arrow $iprod(sum_i a_i, b)$
],
[inner product],
)

== Matrix notations
Expand Down Expand Up @@ -291,29 +299,46 @@ All symbols need to be used in *math mode* `$...$`.
Jacobian matrix: `jacobianmatrix(`...`)`, i.e. `jmat(`...`)`.

#table(
columns: (1fr, 1fr),
columns: (25%, auto, auto),
align: center,
stroke: none,
column-gutter: 1em,

[
\ Typst (like LaTeX) \ cramps fractions in a matrix...
],
[
#hl(`jmat(f_1,f_2; x,y)`)
$ jmat(f_1,f_2;x,y) $
],
[
#hl(`jmat(f_1,f_2; x,y,z; delim:"[")`)
$ jmat(f_1,f_2;x,y,z;delim:"[") $
#hl(`jmat(f,g; x,y,z; delim:"[")`)
$ jmat(f,g;x,y,z;delim:"[") $
],
[
\ ...but you can uncramp them using argument #hl(`big:#true`) here
],
[
#hl(`jmat(f_1,f_2;x,y;big:#true)`)
$ jmat(f_1,f_2;x,y;big:#true) $
],
[
#hl(`jmat(f,g;x,y,z;delim:"|",big:#true)`)
$ jmat(f,g;x,y,z;delim:"|",big:#true) $
],
)

Hessian matrix: `hessianmatrix(`...`)`, i.e. `hmat(`...`)`.

#table(
columns: (1fr, 1fr),
columns: (25%, auto, auto),
align: center,
stroke: none,
column-gutter: 1em,

[
\ Typst (like LaTeX) \ cramps fractions in a matrix...
],
[
#hl(`hmat(f; x,y)`)
$ hmat(f; x,y) $
Expand All @@ -322,6 +347,17 @@ Hessian matrix: `hessianmatrix(`...`)`, i.e. `hmat(`...`)`.
#hl(`hmat(; x,y,z; delim:"[")`)
$ hmat(; x,y,z; delim:"[") $
],
[
\ ...but you can uncramp them using argument #hl(`big:#true`) here
],
[
#hl(`hmat(f;x,y;big:#true)`)
$ hmat(f;x,y;big:#true) $
],
[
#hl(`hmat(;x,y,z;delim:"|",big:#true)`)
$ hmat(; x,y,z;delim:"|",big:#true) $
],
)

Matrix built with an element building function: `xmatrix(`_m, n, func_`)`, i.e. `xmat(`...`)`. The element building function _func_ takes two integers which are the row and column numbers starting from 1.
Expand Down Expand Up @@ -399,22 +435,6 @@ xmat(2, 2, #g)`)
],
[ketbra],

[`innerproduct(`_a_, _b_`)`],
[`iprod`],
[
`iprod(a), iprod(u, v)` \ #sym.arrow $iprod(a), iprod(u, v)$ \
`iprod(a, vec(1,2))` #sym.arrow $iprod(a, vec(1,2))$
],
[innerproduct\ = braket],

[`outerproduct(`_a_, _b_`)`],
[`oprod`],
[
`oprod(a), oprod(u, v)` \ #sym.arrow $oprod(a), oprod(u, v)$ \
`oprod(a, vec(1,2))` #sym.arrow $oprod(a, vec(1,2))$
],
[outerproduct\ = ketbra],

[`matrixelement(`_n_, _M_, _m_`)`],
[`mel`],
[
Expand Down Expand Up @@ -852,6 +872,51 @@ $ isotope("Bi",a:211,z:83) --> isotope("Tl",a:207,z:81) + isotope("He",a:4,z:2)
*(4)* #hl(`isotope("Tl",a:207,z:81) --> isotope("Pb",a:207,z:82) + isotope(e,a:0,z:-1)`)
$ isotope("Tl",a:207,z:81) --> isotope("Pb",a:207,z:82) + isotope(e,a:0,z:-1) $

=== The n-th term in Taylor series

#v(1em)

Function: `taylorterm(`_func_, _x_, _x0_, _idx_`)`.
- _func_: the function e.g. `f`, `(f+g)`,
- _x_: the variable name e.g. `x`,
- _x0_: the variable value at the expansion point e.g. `x_0`, `(1+a)`,
- _idx_: the index of the term, e.g. `0`, `1`, `2`, `n`, `(n+1)`.
If _x0_ or _idx_ is in a parenthesis e.g. `(1+k)`, then the function
automatically removes the outer parenthesis where appropriate.

#align(center, [*Examples*])

#grid(
columns: (50%, 50%),
row-gutter: 1em,
column-gutter: 2em,
[
*(1)* #hl(`taylorterm(f,x,x_0,0)`) \
$ taylorterm(f,x,x_0,0) $
],
[
*(2)* #hl(`taylorterm(f,x,x_0,1)`) \
$ taylorterm(f,x,x_0,1) $
],
[
*(3)* #hl(`taylorterm(f,x,(1+a),2)`) \
$ taylorterm(f,x,(1+a),2) $
],
[
*(4)* #hl(`taylorterm(f,x,x_0,n)`) \
$ taylorterm(f,x,x_0,n) $
],
[
*(5)* #hl(`taylorterm(F,x^nu,x^nu_0,n)`) \
$ taylorterm(F,x^nu,x^nu_0,n) $
],
[
*(6)* #hl(`taylorterm(f_p,x,x_0,(n+1))`) \
$ taylorterm(f_p,x,x_0,(n+1)) $
],
)


=== Signal sequences (digital timing diagrams)

In engineering, people often need to draw digital timing diagrams for signals, like $signals("1|0|1|0")$.
Expand Down
55 changes: 43 additions & 12 deletions physica.typ
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,25 @@
#let crossproduct = $times$
#let cprod = crossproduct

#let innerproduct(u, v) = {
$lr(angle.l #u, #v angle.r)$
}
#let iprod = innerproduct

// == Matrices

// Display matrix element in display/inline style. The latter vertically
// compresses a tall content (e.g. a fraction) while the former doesn't.
// In Typst and LaTeX, a matrix element is automatically cramped, even if
// the matrix is in a standalone math block.
#let __mate(content, big) = {
if big {
math.display(content)
} else {
math.inline(content)
}
}

#let matrixdet(..sink) = {
math.mat(..sink, delim:"|")
}
Expand Down Expand Up @@ -276,20 +293,20 @@
}
#let zmat = zeromatrix

#let jacobianmatrix(fs, xs, delim:"(") = {
#let jacobianmatrix(fs, xs, delim:"(", big: false) = {
assert(type(fs) == array, message: "expecting an array of function names")
assert(type(xs) == array, message: "expecting an array of variable names")
let arrays = () // array of arrays
for f in fs {
arrays.push(xs.map((x) => math.frac($diff#f$, $diff#x$)))
arrays.push(xs.map((x) => __mate(math.frac($diff#f$, $diff#x$), big)))
}
math.mat(delim: delim, ..arrays)
}
#let jmat = jacobianmatrix

#let hessianmatrix(fs, xs, delim:"(") = {
assert(type(fs) == array, message: "expecting a one-element array")
assert(fs.len() == 1, message: "expecting only one function name")
#let hessianmatrix(fs, xs, delim:"(", big: false) = {
assert(type(fs) == array, message: "usage: hessianmatrix(f; x, y...)")
assert(fs.len() == 1, message: "usage: hessianmatrix(f; x, y...)")
let f = fs.at(0)
assert(type(xs) == array, message: "expecting an array of variable names")
let row_arrays = () // array of arrays
Expand All @@ -299,10 +316,10 @@
let xr = xs.at(r)
for c in range(order) {
let xc = xs.at(c)
row_array.push(math.frac(
row_array.push(__mate(math.frac(
$diff^2 #f$,
if xr == xc { $diff #xr^2$ } else { $diff #xr diff #xc$ }
))
), big))
}
row_arrays.push(row_array)
}
Expand Down Expand Up @@ -364,11 +381,6 @@
$ lr(bar.v ket#h(0pt)mid(angle.r#h(0pt)angle.l)#h(0pt)bra bar.v) $
})

#let innerproduct = braket
#let iprod = innerproduct
#let outerproduct = ketbra
#let oprod = outerproduct

#let matrixelement(n, M, m) = style(styles => {
$ lr(angle.l #n#h(0pt)mid(bar.v)#h(0pt)#M#h(0pt)mid(bar.v)#h(0pt)#m angle.r) $
})
Expand Down Expand Up @@ -663,6 +675,25 @@
math.attach((T,hphantom(sym.zwj)).join(), t: uppers.join(), b: lowers.join())
}

#let taylorterm(fn, xv, x0, idx) = {
let noparen(expr) = {
if type(expr) == content and expr.func() == math.lr {
let children = expr.at("body").at("children")
children.slice(1, children.len() - 1).join()
} else {
expr
}
}

if idx == [0] or idx == 0 {
$fn (noparen(x0))$
} else if idx == [1] or idx == 1 {
$fn^((1)) (noparen(x0))(xv - x0)$
} else {
$frac(fn^((noparen(idx))) (noparen(x0)), idx !)(xv - x0)^noparen(idx)$
}
}

#let isotope(element, /*atomic mass*/a: none, /*atomic number*/z: none) = {
$attach(upright(element), tl: #a, bl: #z)$
}
Expand Down

0 comments on commit 4434543

Please sign in to comment.