symcrys 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Snapping a point of the plane onto the nearest Wyckoff position of a plane group, by the distance
3
+ * of the point to the position (#85; CLAUDE.md, "Snapping onto a Wyckoff position"). Unlike the site
4
+ * symmetry (`positions.ts`), which asks whether the images of a point lie within a tolerance of it,
5
+ * this asks whether the point lies within the tolerance of the point set of a position. Internal.
6
+ */
7
+
8
+ import { parseOperation, toPlain, type PlainOperation } from './affine.js';
9
+ import { planeGroupEntry } from './planeGroups.js';
10
+ import { EPSILON } from '../tolerances.js';
11
+
12
+ /** The point set of one printed coordinate expression, x ↦ M·(x, y) + c, with the columns of M that are not zero. */
13
+ interface Expression {
14
+ readonly matrix: readonly (readonly number[])[];
15
+ readonly translation: readonly number[];
16
+ /** The number of free parameters: 0 for a point, 1 for a line, 2 for the general position. */
17
+ readonly freedom: number;
18
+ }
19
+
20
+ const cache = new Map<string, readonly { row: number; expressions: Expression[] }[]>();
21
+
22
+ function expression(op: PlainOperation, shift: readonly number[]): Expression {
23
+ const m = op.matrix;
24
+ const columns = [0, 1].filter((k) => m[0][k] !== 0 || m[1][k] !== 0);
25
+ const det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
26
+ return {
27
+ matrix: m,
28
+ translation: [op.translation[0] + shift[0], op.translation[1] + shift[1]],
29
+ freedom: columns.length === 2 && det === 0 ? 1 : columns.length,
30
+ };
31
+ }
32
+
33
+ /** Per printed Wyckoff row (index into `wyckoff`), its coordinate expressions with every centring. */
34
+ function positionsOf(symbol: string) {
35
+ let rows = cache.get(symbol);
36
+ if (!rows) {
37
+ const entry = planeGroupEntry(symbol);
38
+ const shifts = [[0, 0], ...entry.centring.map((t) => toPlain(parseOperation(t)).translation)];
39
+ rows = entry.wyckoff.map(([, , , coordinates], row) => ({
40
+ row,
41
+ expressions: coordinates.flatMap((c) => shifts.map((s) => expression(toPlain(parseOperation(c)), s))),
42
+ }));
43
+ cache.set(symbol, rows);
44
+ }
45
+ return rows;
46
+ }
47
+
48
+ /**
49
+ * The nearest point of the set of `e` to `p` modulo the lattice: the parameters by least squares in
50
+ * the lattice coordinates, over the lattice translations that can reach it. Returns the point and
51
+ * its distance, the larger of the two coordinate differences.
52
+ */
53
+ function nearest(e: Expression, p: readonly number[]): { point: number[]; distance: number } {
54
+ if (e.freedom === 2) return { point: [...p], distance: 0 };
55
+ const m = e.matrix;
56
+ const c = e.translation;
57
+ let best = { point: [0, 0], distance: Infinity };
58
+ // c lies in [0, 2) (a printed coordinate in [0, 1) plus a centring), p in [0, 1).
59
+ for (let i = -2; i <= 2; i++) {
60
+ for (let j = -2; j <= 2; j++) {
61
+ const v = [p[0] - c[0] - i, p[1] - c[1] - j];
62
+ let r: number[];
63
+ if (e.freedom === 0) {
64
+ r = [0, 0];
65
+ } else {
66
+ const k = m[0][0] !== 0 || m[1][0] !== 0 ? 0 : 1;
67
+ const col = [m[0][k], m[1][k]];
68
+ const t = (col[0] * v[0] + col[1] * v[1]) / (col[0] * col[0] + col[1] * col[1]);
69
+ r = [t * col[0], t * col[1]];
70
+ }
71
+ const distance = Math.max(Math.abs(r[0] - v[0]), Math.abs(r[1] - v[1]));
72
+ if (distance < best.distance) best = { point: [r[0] + c[0] + i, r[1] + c[1] + j], distance };
73
+ }
74
+ }
75
+ return best;
76
+ }
77
+
78
+ const mod1 = (v: number): number => {
79
+ const r = v - Math.floor(v);
80
+ return r >= 1 ? 0 : r + 0;
81
+ };
82
+
83
+ /** A point snapped onto a Wyckoff position: the index of the printed row and the point on it, in [0, 1). */
84
+ export interface SnappedPoint {
85
+ readonly row: number;
86
+ readonly point: readonly number[];
87
+ }
88
+
89
+ /**
90
+ * Snaps a point (lattice basis) onto the most special Wyckoff position of a plane group that lies
91
+ * within `tolerance` of it (per coordinate, modulo the lattice): positions without a free parameter
92
+ * first, then lines, then the general position, which always holds the point. Among positions with
93
+ * the same number of free parameters the nearest wins. Throws a RangeError for a point that is not
94
+ * two finite numbers or a tolerance outside (0, 1/2).
95
+ */
96
+ export function planeSnapToWyckoffPosition(
97
+ key: string,
98
+ point: readonly number[],
99
+ tolerance: number = EPSILON,
100
+ ): SnappedPoint {
101
+ if (point.length !== 2 || !point.every(Number.isFinite)) {
102
+ throw new RangeError(`A point of the plane has two finite coordinates, not ${JSON.stringify(point)}`);
103
+ }
104
+ if (!(typeof tolerance === 'number' && tolerance > 0 && tolerance < 0.5)) {
105
+ throw new RangeError(`The tolerance must lie between 0 and 1/2, not ${String(tolerance)}`);
106
+ }
107
+ const p = point.map(mod1);
108
+ const rows = positionsOf(planeGroupEntry(key).symbol);
109
+ for (const freedom of [0, 1, 2]) {
110
+ let best: { row: number; point: number[]; distance: number } | undefined;
111
+ for (const { row, expressions } of rows) {
112
+ for (const e of expressions) {
113
+ if (e.freedom !== freedom) continue;
114
+ const n = nearest(e, p);
115
+ if (n.distance < tolerance && (!best || n.distance < best.distance)) best = { row, ...n };
116
+ }
117
+ }
118
+ if (best) return { row: best.row, point: best.point.map(mod1) };
119
+ }
120
+ throw new Error(`The general position of ${key} does not hold ${JSON.stringify(point)}`);
121
+ }
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  *
4
4
  * @remarks Kept equal to `version` in package.json (checked by version.test.ts).
5
5
  */
6
- export const VERSION = '0.9.0';
6
+ export const VERSION = '0.10.0';