symcrys 0.8.0 → 0.9.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.
- package/README.md +11 -2
- package/dist/cell.d.ts +5 -2
- package/dist/cell.d.ts.map +1 -1
- package/dist/cell.js +7 -7
- package/dist/cell.js.map +1 -1
- package/dist/crystalClasses.d.ts +13 -8
- package/dist/crystalClasses.d.ts.map +1 -1
- package/dist/crystalClasses.js +13 -8
- package/dist/crystalClasses.js.map +1 -1
- package/dist/indices.d.ts +3 -1
- package/dist/indices.d.ts.map +1 -1
- package/dist/indices.js +6 -2
- package/dist/indices.js.map +1 -1
- package/dist/linalg.d.ts +7 -1
- package/dist/linalg.d.ts.map +1 -1
- package/dist/linalg.js +20 -3
- package/dist/linalg.js.map +1 -1
- package/dist/operations.d.ts +6 -0
- package/dist/operations.d.ts.map +1 -1
- package/dist/operations.js +19 -0
- package/dist/operations.js.map +1 -1
- package/dist/plane.d.ts +225 -0
- package/dist/plane.d.ts.map +1 -0
- package/dist/plane.js +219 -0
- package/dist/plane.js.map +1 -0
- package/dist/pointGroups/identify.d.ts +4 -2
- package/dist/pointGroups/identify.d.ts.map +1 -1
- package/dist/pointGroups/identify.js +5 -18
- package/dist/pointGroups/identify.js.map +1 -1
- package/dist/pointGroups/orbits.d.ts +1 -1
- package/dist/pointGroups/orbits.d.ts.map +1 -1
- package/dist/pointGroups/orbits.js +3 -3
- package/dist/pointGroups/orbits.js.map +1 -1
- package/dist/pointGroups/properties.d.ts +6 -2
- package/dist/pointGroups/properties.d.ts.map +1 -1
- package/dist/pointGroups/properties.js +6 -2
- package/dist/pointGroups/properties.js.map +1 -1
- package/dist/siteSymmetry.d.ts +5 -3
- package/dist/siteSymmetry.d.ts.map +1 -1
- package/dist/siteSymmetry.js +6 -2
- package/dist/siteSymmetry.js.map +1 -1
- package/dist/spaceGroups/affine.d.ts +81 -0
- package/dist/spaceGroups/affine.d.ts.map +1 -0
- package/dist/spaceGroups/affine.js +245 -0
- package/dist/spaceGroups/affine.js.map +1 -0
- package/dist/spaceGroups/cell.d.ts +58 -0
- package/dist/spaceGroups/cell.d.ts.map +1 -0
- package/dist/spaceGroups/cell.js +297 -0
- package/dist/spaceGroups/cell.js.map +1 -0
- package/dist/spaceGroups/describe.d.ts +44 -0
- package/dist/spaceGroups/describe.d.ts.map +1 -0
- package/dist/spaceGroups/describe.js +92 -0
- package/dist/spaceGroups/describe.js.map +1 -0
- package/dist/spaceGroups/planeGroupData.d.ts +44 -0
- package/dist/spaceGroups/planeGroupData.d.ts.map +1 -0
- package/dist/spaceGroups/planeGroupData.js +371 -0
- package/dist/spaceGroups/planeGroupData.js.map +1 -0
- package/dist/spaceGroups/planeGroups.d.ts +28 -0
- package/dist/spaceGroups/planeGroups.d.ts.map +1 -0
- package/dist/spaceGroups/planeGroups.js +61 -0
- package/dist/spaceGroups/planeGroups.js.map +1 -0
- package/dist/spaceGroups/planePointGroups.d.ts +31 -0
- package/dist/spaceGroups/planePointGroups.d.ts.map +1 -0
- package/dist/spaceGroups/planePointGroups.js +77 -0
- package/dist/spaceGroups/planePointGroups.js.map +1 -0
- package/dist/spaceGroups/positions.d.ts +65 -0
- package/dist/spaceGroups/positions.d.ts.map +1 -0
- package/dist/spaceGroups/positions.js +183 -0
- package/dist/spaceGroups/positions.js.map +1 -0
- package/dist/spaceGroups/rational.d.ts +43 -0
- package/dist/spaceGroups/rational.d.ts.map +1 -0
- package/dist/spaceGroups/rational.js +95 -0
- package/dist/spaceGroups/rational.js.map +1 -0
- package/dist/tensors/tensorForm.d.ts +3 -1
- package/dist/tensors/tensorForm.d.ts.map +1 -1
- package/dist/tensors/tensorForm.js +3 -1
- package/dist/tensors/tensorForm.js.map +1 -1
- package/dist/tensors/tensorProperties.d.ts +4 -2
- package/dist/tensors/tensorProperties.d.ts.map +1 -1
- package/dist/tensors/tensorProperties.js +4 -2
- package/dist/tensors/tensorProperties.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +7 -2
- package/src/cell.ts +9 -8
- package/src/crystalClasses.ts +13 -8
- package/src/indices.ts +5 -2
- package/src/linalg.ts +21 -2
- package/src/operations.ts +20 -0
- package/src/plane.ts +455 -0
- package/src/pointGroups/identify.ts +5 -19
- package/src/pointGroups/orbits.ts +3 -3
- package/src/pointGroups/properties.ts +6 -2
- package/src/siteSymmetry.ts +6 -3
- package/src/spaceGroups/affine.ts +315 -0
- package/src/spaceGroups/cell.ts +371 -0
- package/src/spaceGroups/describe.ts +133 -0
- package/src/spaceGroups/planeGroupData.ts +407 -0
- package/src/spaceGroups/planeGroups.ts +75 -0
- package/src/spaceGroups/planePointGroups.ts +90 -0
- package/src/spaceGroups/positions.ts +237 -0
- package/src/spaceGroups/rational.ts +115 -0
- package/src/tensors/tensorForm.ts +3 -1
- package/src/tensors/tensorProperties.ts +4 -2
- package/src/version.ts +1 -1
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The asymmetric unit of a plane group and its symmetry elements in the conventional cell, as numbers
|
|
3
|
+
* (CLAUDE.md, "The API of `symcrys/plane`"): the asymmetric unit as inequalities with its vertices,
|
|
4
|
+
* the elements as rotation centres with their order and as mirror and glide lines with the segment
|
|
5
|
+
* that lies in the cell [0, 1]². Exact, in the lattice basis; drawing them stays with the
|
|
6
|
+
* applications. Internal for now.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { type ExactOperation } from './affine.js';
|
|
10
|
+
import { describePlaneOperation } from './describe.js';
|
|
11
|
+
import { planeGroupEntry, planeGroupOperations } from './planeGroups.js';
|
|
12
|
+
import {
|
|
13
|
+
ONE,
|
|
14
|
+
safeProduct,
|
|
15
|
+
safeSum,
|
|
16
|
+
ZERO,
|
|
17
|
+
add,
|
|
18
|
+
multiply,
|
|
19
|
+
parseRational,
|
|
20
|
+
rational,
|
|
21
|
+
rationalsEqual,
|
|
22
|
+
scale,
|
|
23
|
+
subtract,
|
|
24
|
+
toNumber,
|
|
25
|
+
type Rational,
|
|
26
|
+
} from './rational.js';
|
|
27
|
+
|
|
28
|
+
/** An inequality a·x + b·y ≤ c with integer coefficients without a common factor. */
|
|
29
|
+
export type Inequality = readonly [a: number, b: number, c: number];
|
|
30
|
+
|
|
31
|
+
/** The asymmetric unit of a plane group, a convex polygon in the lattice basis. */
|
|
32
|
+
export interface AsymmetricUnit {
|
|
33
|
+
/** Its vertices, counter-clockwise in the lattice basis (a towards b), starting at the vertex with the smallest y, then x. */
|
|
34
|
+
readonly vertices: readonly (readonly Rational[])[];
|
|
35
|
+
/** Its edges as inequalities: inequality i holds the edge from vertex i to vertex i + 1. */
|
|
36
|
+
readonly inequalities: readonly Inequality[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A rotation centre in the cell. */
|
|
40
|
+
export interface RotationCentre {
|
|
41
|
+
/** The highest order of a rotation about it. */
|
|
42
|
+
readonly order: number;
|
|
43
|
+
/** Its position in [0, 1]². */
|
|
44
|
+
readonly point: readonly Rational[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A mirror or glide line, the part of it in the cell. */
|
|
48
|
+
export interface SymmetryLine {
|
|
49
|
+
/** `'mirror'` or `'glide'`. */
|
|
50
|
+
readonly kind: 'mirror' | 'glide';
|
|
51
|
+
/** The direction of the line, the shortest integer vector, first non-zero component positive. */
|
|
52
|
+
readonly direction: readonly [number, number];
|
|
53
|
+
/** For a glide line: its glide vector s·direction with 0 < s < 1; for a mirror line (0, 0). */
|
|
54
|
+
readonly glide: readonly Rational[];
|
|
55
|
+
/** The ends of the segment of the line in the cell [0, 1]², in the order of `direction`. */
|
|
56
|
+
readonly segment: readonly [readonly Rational[], readonly Rational[]];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The symmetry elements of a plane group in the cell [0, 1]². */
|
|
60
|
+
export interface CellElements {
|
|
61
|
+
/** The rotation centres, by descending order, then by y and x. */
|
|
62
|
+
readonly rotations: readonly RotationCentre[];
|
|
63
|
+
/** The mirror and glide lines with a segment of non-zero length in the cell, by direction, then by the start of the segment (y, then x). */
|
|
64
|
+
readonly lines: readonly SymmetryLine[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------------------------
|
|
68
|
+
// Asymmetric unit
|
|
69
|
+
|
|
70
|
+
/** A linear expression c₀ + cx·x + cy·y. */
|
|
71
|
+
type Linear = readonly [cx: Rational, cy: Rational, c0: Rational];
|
|
72
|
+
|
|
73
|
+
const linAdd = (p: Linear, q: Linear): Linear => [add(p[0], q[0]), add(p[1], q[1]), add(p[2], q[2])];
|
|
74
|
+
const linScale = (k: Rational, p: Linear): Linear => [multiply(k, p[0]), multiply(k, p[1]), multiply(k, p[2])];
|
|
75
|
+
const isConstant = (p: Linear): boolean => p[0][0] === 0 && p[1][0] === 0;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Reads a linear expression as ITA prints the bounds of an asymmetric unit: `x`, `2/3`, `1/2 - x`,
|
|
79
|
+
* `(1 + y)/2`, `x/2`, `2x`. Throws on anything else.
|
|
80
|
+
*/
|
|
81
|
+
function parseLinear(text: string): Linear {
|
|
82
|
+
const s = text.replace(/\s+/g, '');
|
|
83
|
+
let i = 0;
|
|
84
|
+
const fail = (): never => {
|
|
85
|
+
throw new RangeError(`Cannot read the linear expression "${text}"`);
|
|
86
|
+
};
|
|
87
|
+
function number(): Rational | undefined {
|
|
88
|
+
const m = /^\d+(?:\/\d+)?/.exec(s.slice(i));
|
|
89
|
+
if (!m) return undefined;
|
|
90
|
+
i += m[0].length;
|
|
91
|
+
return parseRational(m[0]);
|
|
92
|
+
}
|
|
93
|
+
function factor(): Linear {
|
|
94
|
+
if (s[i] === '(') {
|
|
95
|
+
i++;
|
|
96
|
+
const e = sum();
|
|
97
|
+
if (s[i++] !== ')') fail();
|
|
98
|
+
return e;
|
|
99
|
+
}
|
|
100
|
+
const k = number();
|
|
101
|
+
if (s[i] === 'x' || s[i] === 'y') {
|
|
102
|
+
const v = s[i++];
|
|
103
|
+
const c = k ?? ONE;
|
|
104
|
+
return v === 'x' ? [c, ZERO, ZERO] : [ZERO, c, ZERO];
|
|
105
|
+
}
|
|
106
|
+
if (k === undefined) return fail();
|
|
107
|
+
return [ZERO, ZERO, k];
|
|
108
|
+
}
|
|
109
|
+
function term(): Linear {
|
|
110
|
+
let e = factor();
|
|
111
|
+
while (s[i] === '/') {
|
|
112
|
+
i++;
|
|
113
|
+
const d = number() ?? fail();
|
|
114
|
+
e = linScale(rational(d[1], d[0]), e);
|
|
115
|
+
}
|
|
116
|
+
return e;
|
|
117
|
+
}
|
|
118
|
+
function sum(): Linear {
|
|
119
|
+
let sign = 1;
|
|
120
|
+
if (s[i] === '-' || s[i] === '+') sign = s[i++] === '-' ? -1 : 1;
|
|
121
|
+
let e = linScale(rational(sign), term());
|
|
122
|
+
while (s[i] === '+' || s[i] === '-') {
|
|
123
|
+
const k = s[i++] === '-' ? -1 : 1;
|
|
124
|
+
e = linAdd(e, linScale(rational(k), term()));
|
|
125
|
+
}
|
|
126
|
+
return e;
|
|
127
|
+
}
|
|
128
|
+
const e = sum();
|
|
129
|
+
if (i !== s.length) fail();
|
|
130
|
+
return e;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function gcd(a: number, b: number): number {
|
|
134
|
+
return b === 0 ? Math.abs(a) : gcd(b, a % b);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** p ≤ 0 as an inequality with integer coefficients without a common factor. */
|
|
138
|
+
function toInequality(p: Linear): Inequality {
|
|
139
|
+
const lcm = p.reduce((l, r) => safeProduct(l, r[1]) / gcd(l, r[1]), 1);
|
|
140
|
+
const [a, b, c0] = p.map((r) => safeProduct(r[0], lcm / r[1]));
|
|
141
|
+
const g = gcd(gcd(a, b), c0) || 1;
|
|
142
|
+
return [a / g + 0, b / g + 0, -c0 / g + 0];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Reads the printed block "Asymmetric unit", e.g. `0 <= x <= 1/2; y <= min(1 - x, (1 + x)/2)`. */
|
|
146
|
+
export function parseAsymmetricUnit(text: string): Inequality[] {
|
|
147
|
+
const out: Inequality[] = [];
|
|
148
|
+
for (const clause of text.split(';')) {
|
|
149
|
+
const sides = clause.split('<=').map((side) => {
|
|
150
|
+
const m = /^\s*min\((.*)\)\s*$/.exec(side);
|
|
151
|
+
if (!m) return [parseLinear(side)];
|
|
152
|
+
// min(A, B): split at the comma outside parentheses.
|
|
153
|
+
let depth = 0;
|
|
154
|
+
const inner = m[1];
|
|
155
|
+
for (let k = 0; k < inner.length; k++) {
|
|
156
|
+
if (inner[k] === '(') depth++;
|
|
157
|
+
else if (inner[k] === ')') depth--;
|
|
158
|
+
else if (inner[k] === ',' && depth === 0)
|
|
159
|
+
return [parseLinear(inner.slice(0, k)), parseLinear(inner.slice(k + 1))];
|
|
160
|
+
}
|
|
161
|
+
throw new RangeError(`Cannot read "${side}"`);
|
|
162
|
+
});
|
|
163
|
+
if (sides.length < 2) throw new RangeError(`Not an inequality: "${clause}"`);
|
|
164
|
+
for (let k = 0; k + 1 < sides.length; k++) {
|
|
165
|
+
for (const lhs of sides[k]) {
|
|
166
|
+
for (const rhs of sides[k + 1]) {
|
|
167
|
+
const p = linAdd(lhs, linScale(rational(-1), rhs));
|
|
168
|
+
if (isConstant(p)) throw new RangeError(`No variable in "${clause}"`);
|
|
169
|
+
out.push(toInequality(p));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const r = (n: number): Rational => rational(n);
|
|
178
|
+
|
|
179
|
+
/** a·x + b·y − c for a point. */
|
|
180
|
+
const excess = (q: Inequality, p: readonly Rational[]): Rational =>
|
|
181
|
+
subtract(add(scale(q[0], p[0]), scale(q[1], p[1])), r(q[2]));
|
|
182
|
+
|
|
183
|
+
/** The vertices of the polygon {q_i ≤ 0}, counter-clockwise from the lowest (y, then x). */
|
|
184
|
+
function polygon(inequalities: readonly Inequality[]): Rational[][] {
|
|
185
|
+
const points: Rational[][] = [];
|
|
186
|
+
for (let i = 0; i < inequalities.length; i++) {
|
|
187
|
+
for (let j = i + 1; j < inequalities.length; j++) {
|
|
188
|
+
const [a1, b1, c1] = inequalities[i];
|
|
189
|
+
const [a2, b2, c2] = inequalities[j];
|
|
190
|
+
const cross = (u: number, v: number, x: number, y: number) => safeSum(safeProduct(u, v), -safeProduct(x, y));
|
|
191
|
+
const det = cross(a1, b2, a2, b1);
|
|
192
|
+
if (det === 0) continue;
|
|
193
|
+
const p = [rational(cross(c1, b2, c2, b1), det), rational(cross(a1, c2, a2, c1), det)];
|
|
194
|
+
if (inequalities.every((q) => excess(q, p)[0] <= 0) && !points.some((v) => samePoint(v, p))) points.push(p);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (points.length < 3) throw new RangeError('The asymmetric unit is not a polygon');
|
|
198
|
+
const cx = points.reduce((s, p) => s + toNumber(p[0]), 0) / points.length;
|
|
199
|
+
const cy = points.reduce((s, p) => s + toNumber(p[1]), 0) / points.length;
|
|
200
|
+
const angle = (p: readonly Rational[]) => Math.atan2(toNumber(p[1]) - cy, toNumber(p[0]) - cx);
|
|
201
|
+
points.sort((p, q) => angle(p) - angle(q));
|
|
202
|
+
const lowest = points.reduce((k, p, i) => {
|
|
203
|
+
const q = points[k];
|
|
204
|
+
const dy = toNumber(subtract(p[1], q[1]));
|
|
205
|
+
return dy < 0 || (dy === 0 && toNumber(subtract(p[0], q[0])) < 0) ? i : k;
|
|
206
|
+
}, 0);
|
|
207
|
+
return [...points.slice(lowest), ...points.slice(0, lowest)];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const samePoint = (p: readonly Rational[], q: readonly Rational[]): boolean =>
|
|
211
|
+
rationalsEqual(p[0], q[0]) && rationalsEqual(p[1], q[1]);
|
|
212
|
+
|
|
213
|
+
const unitCache = new Map<string, AsymmetricUnit>();
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The asymmetric unit of a plane group as ITA prints it, a convex polygon: its vertices and, for each
|
|
217
|
+
* edge, the inequality a·x + b·y ≤ c that holds it. Printed inequalities that hold no edge are
|
|
218
|
+
* left out. Frozen.
|
|
219
|
+
*/
|
|
220
|
+
export function planeAsymmetricUnit(key: string | number): AsymmetricUnit {
|
|
221
|
+
const entry = planeGroupEntry(key);
|
|
222
|
+
let unit = unitCache.get(entry.symbol);
|
|
223
|
+
if (!unit) {
|
|
224
|
+
const printed = parseAsymmetricUnit(entry.asymmetricUnit);
|
|
225
|
+
const vertices = polygon(printed);
|
|
226
|
+
const inequalities = vertices.map((p, i) => {
|
|
227
|
+
const q = vertices[(i + 1) % vertices.length];
|
|
228
|
+
const edge = printed.find((e) => excess(e, p)[0] === 0 && excess(e, q)[0] === 0);
|
|
229
|
+
if (!edge) throw new Error(`No printed inequality of ${entry.symbol} holds an edge`);
|
|
230
|
+
return Object.freeze(edge);
|
|
231
|
+
});
|
|
232
|
+
unit = Object.freeze({
|
|
233
|
+
vertices: Object.freeze(vertices.map((v) => Object.freeze(v))),
|
|
234
|
+
inequalities: Object.freeze(inequalities),
|
|
235
|
+
});
|
|
236
|
+
unitCache.set(entry.symbol, unit);
|
|
237
|
+
}
|
|
238
|
+
return unit;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ---------------------------------------------------------------------------------------------
|
|
242
|
+
// Symmetry elements in the cell
|
|
243
|
+
|
|
244
|
+
const inCell = (p: readonly Rational[]): boolean => p.every((v) => v[0] >= 0 && v[0] <= v[1]);
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* The lattice shifts t, |t_i| ≤ 3, added to each coset representative (W, w), w in [0, 1). Enough to
|
|
248
|
+
* reach every element that meets the cell: a centre p in [0, 1]² of (W, w + t) has
|
|
249
|
+
* t = (I − W)p − w, and the entries of I − W (at most 2 on the diagonal, 1 off it) give
|
|
250
|
+
* (I − W)p in [−2, 3]; a line through p in the cell, taken with its glide g along it reduced to
|
|
251
|
+
* g = d/2 (in a plane group s is 0 or 1/2, and |d_i| ≤ 2, so |g_i| ≤ 1), has t = (I − W)p + g − w, where for a reflection (I − W)p lies in [−1, 2].
|
|
252
|
+
*/
|
|
253
|
+
const SHIFTS: Rational[][] = [];
|
|
254
|
+
for (let i = -3; i <= 3; i++) for (let j = -3; j <= 3; j++) SHIFTS.push([r(i), r(j)]);
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The segment of the line through p along d in [0, 1]², or undefined if it has no length there.
|
|
258
|
+
* Exact: the parameter λ of p + λd is bounded by each coordinate.
|
|
259
|
+
*/
|
|
260
|
+
function clip(
|
|
261
|
+
p: readonly Rational[],
|
|
262
|
+
d: readonly [number, number],
|
|
263
|
+
): [readonly Rational[], readonly Rational[]] | undefined {
|
|
264
|
+
let lo: Rational | undefined;
|
|
265
|
+
let hi: Rational | undefined;
|
|
266
|
+
for (let k = 0; k < 2; k++) {
|
|
267
|
+
if (d[k] === 0) {
|
|
268
|
+
if (p[k][0] < 0 || p[k][0] > p[k][1]) return undefined;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
// 0 ≤ p_k + λ d_k ≤ 1
|
|
272
|
+
const a = rational(-p[k][0], p[k][1] * d[k]);
|
|
273
|
+
const b = rational(p[k][1] - p[k][0], p[k][1] * d[k]);
|
|
274
|
+
const [l, h] = d[k] > 0 ? [a, b] : [b, a];
|
|
275
|
+
if (lo === undefined || toNumber(subtract(l, lo)) > 0) lo = l;
|
|
276
|
+
if (hi === undefined || toNumber(subtract(h, hi)) < 0) hi = h;
|
|
277
|
+
}
|
|
278
|
+
if (lo === undefined || hi === undefined || toNumber(subtract(hi, lo)) <= 0) return undefined;
|
|
279
|
+
const at = (lambda: Rational) => [add(p[0], scale(d[0], lambda)), add(p[1], scale(d[1], lambda))];
|
|
280
|
+
return [at(lo), at(hi)];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** s mod 1 in [0, 1). */
|
|
284
|
+
function frac(s: Rational): Rational {
|
|
285
|
+
const n = Math.floor(s[0] / s[1]);
|
|
286
|
+
return subtract(s, r(n));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const elementCache = new Map<string, CellElements>();
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The symmetry elements of a plane group in its conventional cell [0, 1]², boundary included: every
|
|
293
|
+
* rotation centre with the highest order of a rotation about it, and every mirror and glide line
|
|
294
|
+
* that crosses the cell, with the segment inside it. A line that carries a glide is a glide line
|
|
295
|
+
* with its shortest glide vector along the line (between 0 and the lattice vector). Frozen.
|
|
296
|
+
*/
|
|
297
|
+
export function planeCellElements(key: string | number): CellElements {
|
|
298
|
+
const entry = planeGroupEntry(key);
|
|
299
|
+
const cached = elementCache.get(entry.symbol);
|
|
300
|
+
if (cached) return cached;
|
|
301
|
+
const rotations: { order: number; point: Rational[] }[] = [];
|
|
302
|
+
const lines: {
|
|
303
|
+
kind: 'mirror' | 'glide';
|
|
304
|
+
direction: [number, number];
|
|
305
|
+
s: Rational;
|
|
306
|
+
c: Rational;
|
|
307
|
+
segment: [readonly Rational[], readonly Rational[]];
|
|
308
|
+
}[] = [];
|
|
309
|
+
for (const op of planeGroupOperations(entry.symbol)) {
|
|
310
|
+
for (const t of SHIFTS) {
|
|
311
|
+
const shifted: ExactOperation = {
|
|
312
|
+
matrix: op.matrix,
|
|
313
|
+
translation: [add(op.translation[0], t[0]), add(op.translation[1], t[1])],
|
|
314
|
+
};
|
|
315
|
+
const d = describePlaneOperation(shifted);
|
|
316
|
+
if (d.kind === 'rotation') {
|
|
317
|
+
const p = [...(d.location ?? [])];
|
|
318
|
+
if (!inCell(p)) continue;
|
|
319
|
+
const known = rotations.find((x) => samePoint(x.point, p));
|
|
320
|
+
if (!known) rotations.push({ order: d.order, point: p });
|
|
321
|
+
else known.order = Math.max(known.order, d.order);
|
|
322
|
+
} else if (d.kind === 'mirror' || d.kind === 'glide') {
|
|
323
|
+
const dir = d.direction ?? [0, 0];
|
|
324
|
+
const p = d.location ?? [];
|
|
325
|
+
// The glide vector is s·dir; s modulo 1, since a lattice translation along the line changes nothing.
|
|
326
|
+
const g = d.glide ?? [ZERO, ZERO];
|
|
327
|
+
const s = frac(dir[0] !== 0 ? rational(g[0][0], g[0][1] * dir[0]) : rational(g[1][0], g[1][1] * dir[1]));
|
|
328
|
+
// The line is n·x = c with the normal n = (−dir_y, dir_x).
|
|
329
|
+
const c = add(scale(-dir[1], p[0]), scale(dir[0], p[1]));
|
|
330
|
+
if (
|
|
331
|
+
lines.some(
|
|
332
|
+
(l) =>
|
|
333
|
+
l.direction[0] === dir[0] &&
|
|
334
|
+
l.direction[1] === dir[1] &&
|
|
335
|
+
rationalsEqual(l.c, c) &&
|
|
336
|
+
rationalsEqual(l.s, s),
|
|
337
|
+
)
|
|
338
|
+
) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
const segment = clip(p, dir);
|
|
342
|
+
if (!segment) continue;
|
|
343
|
+
lines.push({ kind: s[0] === 0 ? 'mirror' : 'glide', direction: [dir[0], dir[1]], s, c, segment });
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const num = (v: Rational) => toNumber(v);
|
|
348
|
+
rotations.sort((a, b) => b.order - a.order || num(a.point[1]) - num(b.point[1]) || num(a.point[0]) - num(b.point[0]));
|
|
349
|
+
lines.sort(
|
|
350
|
+
(a, b) =>
|
|
351
|
+
a.direction[0] - b.direction[0] ||
|
|
352
|
+
a.direction[1] - b.direction[1] ||
|
|
353
|
+
num(a.segment[0][1]) - num(b.segment[0][1]) ||
|
|
354
|
+
num(a.segment[0][0]) - num(b.segment[0][0]),
|
|
355
|
+
);
|
|
356
|
+
const result: CellElements = Object.freeze({
|
|
357
|
+
rotations: Object.freeze(rotations.map((x) => Object.freeze({ order: x.order, point: Object.freeze(x.point) }))),
|
|
358
|
+
lines: Object.freeze(
|
|
359
|
+
lines.map((l) =>
|
|
360
|
+
Object.freeze({
|
|
361
|
+
kind: l.kind,
|
|
362
|
+
direction: Object.freeze(l.direction) as readonly [number, number],
|
|
363
|
+
glide: Object.freeze([scale(l.direction[0], l.s), scale(l.direction[1], l.s)]),
|
|
364
|
+
segment: Object.freeze(l.segment.map((p) => Object.freeze([...p]))) as unknown as SymmetryLine['segment'],
|
|
365
|
+
}),
|
|
366
|
+
),
|
|
367
|
+
),
|
|
368
|
+
});
|
|
369
|
+
elementCache.set(entry.symbol, result);
|
|
370
|
+
return result;
|
|
371
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The geometric description of an operation of a plane group, as ITA gives it under "Symmetry
|
|
3
|
+
* operations" (ITA 2016, § 2.1.3.9, pp. 160–161): what it is, its order and sense, where its
|
|
4
|
+
* symmetry element lies and its glide part. Structured data; the symbols such as `g(1/2,1/2) x,x`
|
|
5
|
+
* stay with the applications (CLAUDE.md, "Symmetry elements as structured data").
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { determinant, dimensionOf, trace, type ExactOperation, type IntMatrix } from './affine.js';
|
|
9
|
+
import { ZERO, add, multiply, rational, scale, type Rational } from './rational.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What an operation of a plane group is: `'mirror'` (a reflection in a line, as `'mirror'` of the
|
|
13
|
+
* point groups) and `'glide'` (a glide reflection).
|
|
14
|
+
*/
|
|
15
|
+
export type PlaneOperationKind = 'identity' | 'translation' | 'rotation' | 'mirror' | 'glide';
|
|
16
|
+
|
|
17
|
+
/** One operation of a plane group described geometrically, in the lattice basis. */
|
|
18
|
+
export interface PlaneOperationDescription {
|
|
19
|
+
/** What the operation is. */
|
|
20
|
+
readonly kind: PlaneOperationKind;
|
|
21
|
+
/** The order n of its linear part: 1 for identity and translation, 2 for a mirror or glide. */
|
|
22
|
+
readonly order: number;
|
|
23
|
+
/** For a rotation with n ≥ 3: `'+'` if it turns a towards b, `'-'` otherwise. */
|
|
24
|
+
readonly sense?: '+' | '-';
|
|
25
|
+
/** For a translation: the translation vector. */
|
|
26
|
+
readonly translation?: readonly Rational[];
|
|
27
|
+
/**
|
|
28
|
+
* For a rotation: its centre. For a mirror or glide: a point of its line, on y = 0 unless the line
|
|
29
|
+
* is parallel to it, then on x = 0. Not reduced modulo the lattice: the centre of `-y,x+1/2` is
|
|
30
|
+
* (−1/4, 1/4).
|
|
31
|
+
*/
|
|
32
|
+
readonly location?: readonly Rational[];
|
|
33
|
+
/** For a mirror or glide: the direction of its line, the shortest integer vector, first non-zero component positive. */
|
|
34
|
+
readonly direction?: readonly [number, number];
|
|
35
|
+
/**
|
|
36
|
+
* For a glide: the glide vector (w + W·w)/2, not reduced modulo the lattice. An operation whose
|
|
37
|
+
* glide vector is a non-zero lattice vector is a glide, as it is geometrically, although the
|
|
38
|
+
* symmetry element of its coset is the mirror line, which ITA lists.
|
|
39
|
+
*/
|
|
40
|
+
readonly glide?: readonly Rational[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function gcd(a: number, b: number): number {
|
|
44
|
+
return b === 0 ? Math.abs(a) : gcd(b, a % b);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The shortest integer vector along the non-zero column of a 2 × 2 matrix of rank 1, first non-zero component positive. */
|
|
48
|
+
function primitiveColumn(m: IntMatrix): [number, number] {
|
|
49
|
+
const col = m[0][0] !== 0 || m[1][0] !== 0 ? [m[0][0], m[1][0]] : [m[0][1], m[1][1]];
|
|
50
|
+
const g = gcd(col[0], col[1]);
|
|
51
|
+
const sign = col[0] < 0 || (col[0] === 0 && col[1] < 0) ? -1 : 1;
|
|
52
|
+
return [(sign * col[0]) / g + 0, (sign * col[1]) / g + 0];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function power(m: IntMatrix, n: number): IntMatrix {
|
|
56
|
+
let p: IntMatrix = [
|
|
57
|
+
[1, 0],
|
|
58
|
+
[0, 1],
|
|
59
|
+
];
|
|
60
|
+
for (let k = 0; k < n; k++) {
|
|
61
|
+
p = p.map((row) => [0, 1].map((j) => row[0] * m[0][j] + row[1] * m[1][j]));
|
|
62
|
+
}
|
|
63
|
+
return p;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const isIdentity = (m: IntMatrix): boolean => m[0][0] === 1 && m[0][1] === 0 && m[1][0] === 0 && m[1][1] === 1;
|
|
67
|
+
|
|
68
|
+
/** The order of the rotation part of a proper plane operation, from its trace (also in a hexagonal basis). */
|
|
69
|
+
const ORDER_BY_TRACE: Record<number, number> = { 2: 1, [-2]: 2, [-1]: 3, 0: 4, 1: 6 };
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Describes an operation of a plane group; the result is frozen. Throws unless the operation is two-dimensional with the
|
|
73
|
+
* linear part of a crystallographic plane point group (determinant ±1, order 1, 2, 3, 4 or 6).
|
|
74
|
+
*/
|
|
75
|
+
export function describePlaneOperation(op: ExactOperation): PlaneOperationDescription {
|
|
76
|
+
if (dimensionOf(op) !== 2) {
|
|
77
|
+
throw new RangeError('Not an operation of the plane');
|
|
78
|
+
}
|
|
79
|
+
const w = op.matrix;
|
|
80
|
+
const t = op.translation;
|
|
81
|
+
const det = determinant(w);
|
|
82
|
+
if (det === 1) {
|
|
83
|
+
const n = ORDER_BY_TRACE[trace(w)];
|
|
84
|
+
if (n === undefined || !isIdentity(power(w, n))) {
|
|
85
|
+
throw new RangeError(`Not a crystallographic rotation: ${JSON.stringify(w)}`);
|
|
86
|
+
}
|
|
87
|
+
if (n === 1) {
|
|
88
|
+
return t.every((v) => v[0] === 0)
|
|
89
|
+
? { kind: 'identity', order: 1 }
|
|
90
|
+
: { kind: 'translation', order: 1, translation: [...t] };
|
|
91
|
+
}
|
|
92
|
+
// The centre p solves (I − W) p = w; det(I − W) = 2 − trace W ≠ 0 for a rotation.
|
|
93
|
+
const a = [
|
|
94
|
+
[1 - w[0][0], -w[0][1]],
|
|
95
|
+
[-w[1][0], 1 - w[1][1]],
|
|
96
|
+
];
|
|
97
|
+
const d = a[0][0] * a[1][1] - a[0][1] * a[1][0];
|
|
98
|
+
const inv = rational(1, d);
|
|
99
|
+
const location = [
|
|
100
|
+
multiply(add(scale(a[1][1], t[0]), scale(-a[0][1], t[1])), inv),
|
|
101
|
+
multiply(add(scale(-a[1][0], t[0]), scale(a[0][0], t[1])), inv),
|
|
102
|
+
];
|
|
103
|
+
const description: PlaneOperationDescription = { kind: 'rotation', order: n, location: Object.freeze(location) };
|
|
104
|
+
// The sense: the sign of a × W·a with a = (1, 0), the same in any right-handed basis.
|
|
105
|
+
return Object.freeze(n === 2 ? description : { ...description, sense: w[1][0] > 0 ? '+' : '-' });
|
|
106
|
+
}
|
|
107
|
+
if (det !== -1 || trace(w) !== 0 || !isIdentity(power(w, 2))) {
|
|
108
|
+
throw new RangeError(`Not a crystallographic reflection: ${JSON.stringify(w)}`);
|
|
109
|
+
}
|
|
110
|
+
// A mirror or glide: W² = I. Glide vector g = (w + W·w)/2, along the line; the line is the set of p with (I − W) p = w − g.
|
|
111
|
+
const wt = [add(scale(w[0][0], t[0]), scale(w[0][1], t[1])), add(scale(w[1][0], t[0]), scale(w[1][1], t[1]))];
|
|
112
|
+
const half = rational(1, 2);
|
|
113
|
+
const glide = [multiply(add(t[0], wt[0]), half), multiply(add(t[1], wt[1]), half)];
|
|
114
|
+
const s = [add(t[0], scale(-1, glide[0])), add(t[1], scale(-1, glide[1]))];
|
|
115
|
+
const direction = primitiveColumn([
|
|
116
|
+
[w[0][0] + 1, w[0][1]],
|
|
117
|
+
[w[1][0], w[1][1] + 1],
|
|
118
|
+
]);
|
|
119
|
+
// (I − W) has rank 1; one of its non-zero rows a gives a·p = s_i. A point on y = 0 if the line meets it.
|
|
120
|
+
const a = [
|
|
121
|
+
[1 - w[0][0], -w[0][1]],
|
|
122
|
+
[-w[1][0], 1 - w[1][1]],
|
|
123
|
+
];
|
|
124
|
+
const i = a[0][0] !== 0 || a[0][1] !== 0 ? 0 : 1;
|
|
125
|
+
const location: Rational[] =
|
|
126
|
+
a[i][0] !== 0 ? [multiply(s[i], rational(1, a[i][0])), ZERO] : [ZERO, multiply(s[i], rational(1, a[i][1]))];
|
|
127
|
+
const line = { order: 2, location: Object.freeze(location), direction: Object.freeze(direction) };
|
|
128
|
+
return Object.freeze(
|
|
129
|
+
glide.some((v) => v[0] !== 0)
|
|
130
|
+
? { kind: 'glide' as const, ...line, glide: Object.freeze(glide) }
|
|
131
|
+
: { kind: 'mirror' as const, ...line },
|
|
132
|
+
);
|
|
133
|
+
}
|