Skip to content

Commit

Permalink
Add taylorterm(), the n-term in Taylor series; add 'tall' to J/H matr…
Browse files Browse the repository at this point in the history
…ices

Also, correct innerproduct to the more usual form <u,v>.
  • Loading branch information
Leedehai committed Dec 11, 2023
1 parent 142fbde commit d5a7f8a
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 37 deletions.
6 changes: 3 additions & 3 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/cf92bd59-986f-4132-b7db-7860058e70c9">
</p>

# The physica package for Typst
Expand All @@ -17,11 +17,11 @@ 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
Expand Down
Binary file modified physica-manual.pdf
Binary file not shown.
113 changes: 91 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,49 @@ All symbols need to be used in *math mode* `$...$`.
Jacobian matrix: `jacobianmatrix(`...`)`, i.e. `jmat(`...`)`.

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

[
#hl(`inline(pdv(f,x))`)
$ inline(pdv(f,x)) $
],
[
#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:"[") $
],
[
#hl(`pdv(f,x)`)
$ pdv(f,x) $
],
[
#hl(`jmat(f_1,f_2;x,y;tall:#true)`)
$ jmat(f_1,f_2;x,y;tall:#true) $
],
[
#hl(`jmat(f,g;x,y,z;delim:"[",tall:#true)`)
$ jmat(f,g;x,y,z;delim:"[",tall:#true) $
],
)

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

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

[
#hl(`inline(pdv(f,x,2))`)
$ inline(pdv(f,x,2)) $
],
[
#hl(`hmat(f; x,y)`)
$ hmat(f; x,y) $
Expand All @@ -322,6 +350,18 @@ Hessian matrix: `hessianmatrix(`...`)`, i.e. `hmat(`...`)`.
#hl(`hmat(; x,y,z; delim:"[")`)
$ hmat(; x,y,z; delim:"[") $
],
[
#hl(`pdv(f,x,2)`)
$ pdv(f,x,2) $
],
[
#hl(`hmat(f;x,y;tall:#true)`)
$ hmat(f;x,y;tall:#true) $
],
[
#hl(`hmat(;x,y,z;delim:"[",tall:#true)`)
$ hmat(; x,y,z;delim:"[",tall:#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 +439,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 +876,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, tall) = {
if tall {
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:"(", tall: 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$), tall)))
}
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:"(", tall: 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$ }
))
), tall))
}
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 d5a7f8a

Please sign in to comment.