symcrys 0.11.0 → 0.12.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.
Files changed (73) hide show
  1. package/dist/coordinates.d.ts +143 -0
  2. package/dist/coordinates.d.ts.map +1 -0
  3. package/dist/coordinates.js +198 -0
  4. package/dist/coordinates.js.map +1 -0
  5. package/dist/crystalClasses.d.ts +28 -3
  6. package/dist/crystalClasses.d.ts.map +1 -1
  7. package/dist/crystalClasses.js +32 -3
  8. package/dist/crystalClasses.js.map +1 -1
  9. package/dist/index.d.ts +4 -2
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +3 -2
  12. package/dist/index.js.map +1 -1
  13. package/dist/indices.d.ts +3 -19
  14. package/dist/indices.d.ts.map +1 -1
  15. package/dist/indices.js +3 -24
  16. package/dist/indices.js.map +1 -1
  17. package/dist/magnetic.d.ts +3 -1
  18. package/dist/magnetic.d.ts.map +1 -1
  19. package/dist/magnetic.js +3 -1
  20. package/dist/magnetic.js.map +1 -1
  21. package/dist/plane.d.ts +158 -3
  22. package/dist/plane.d.ts.map +1 -1
  23. package/dist/plane.js +166 -41
  24. package/dist/plane.js.map +1 -1
  25. package/dist/pointGroups/pointGroups.d.ts +15 -6
  26. package/dist/pointGroups/pointGroups.d.ts.map +1 -1
  27. package/dist/pointGroups/pointGroups.js +52 -5
  28. package/dist/pointGroups/pointGroups.js.map +1 -1
  29. package/dist/pointGroups/shubnikovData.d.ts +11 -0
  30. package/dist/pointGroups/shubnikovData.d.ts.map +1 -0
  31. package/dist/pointGroups/shubnikovData.js +101 -0
  32. package/dist/pointGroups/shubnikovData.js.map +1 -0
  33. package/dist/spaceGroups/describe.d.ts +3 -2
  34. package/dist/spaceGroups/describe.d.ts.map +1 -1
  35. package/dist/spaceGroups/describe.js.map +1 -1
  36. package/dist/spaceGroups/planeGroupData.d.ts +2 -0
  37. package/dist/spaceGroups/planeGroupData.d.ts.map +1 -1
  38. package/dist/spaceGroups/planeGroupData.js +17 -0
  39. package/dist/spaceGroups/planeGroupData.js.map +1 -1
  40. package/dist/spaceGroups/planeLattice.d.ts +78 -0
  41. package/dist/spaceGroups/planeLattice.d.ts.map +1 -0
  42. package/dist/spaceGroups/planeLattice.js +143 -0
  43. package/dist/spaceGroups/planeLattice.js.map +1 -0
  44. package/dist/spaceGroups/planePointGroups.d.ts +6 -2
  45. package/dist/spaceGroups/planePointGroups.d.ts.map +1 -1
  46. package/dist/spaceGroups/planePointGroups.js +14 -2
  47. package/dist/spaceGroups/planePointGroups.js.map +1 -1
  48. package/dist/spaceGroups/rational.d.ts +18 -0
  49. package/dist/spaceGroups/rational.d.ts.map +1 -1
  50. package/dist/spaceGroups/rational.js +58 -0
  51. package/dist/spaceGroups/rational.js.map +1 -1
  52. package/dist/tensors/voigt.d.ts +56 -0
  53. package/dist/tensors/voigt.d.ts.map +1 -0
  54. package/dist/tensors/voigt.js +188 -0
  55. package/dist/tensors/voigt.js.map +1 -0
  56. package/dist/version.d.ts +1 -1
  57. package/dist/version.js +1 -1
  58. package/package.json +1 -1
  59. package/src/coordinates.ts +370 -0
  60. package/src/crystalClasses.ts +42 -3
  61. package/src/index.ts +12 -2
  62. package/src/indices.ts +3 -33
  63. package/src/magnetic.ts +10 -2
  64. package/src/plane.ts +302 -48
  65. package/src/pointGroups/pointGroups.ts +58 -6
  66. package/src/pointGroups/shubnikovData.ts +103 -0
  67. package/src/spaceGroups/describe.ts +3 -2
  68. package/src/spaceGroups/planeGroupData.ts +19 -0
  69. package/src/spaceGroups/planeLattice.ts +208 -0
  70. package/src/spaceGroups/planePointGroups.ts +23 -3
  71. package/src/spaceGroups/rational.ts +59 -0
  72. package/src/tensors/voigt.ts +268 -0
  73. package/src/version.ts +1 -1
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The Shubnikov symbols of the 90 magnetic point groups of types I and III, ITC Vol. D (2013) Table
3
+ * 1.5.2.3, column 3 (printed pp. 111–112), in the notation of references/itc/vol-d-table-1.5.2.3.md:
4
+ * a leading `_` is the printed underline (time reversal), a leading `-` the overbar, `.` the centred
5
+ * dot. The reference test compares every entry with the transcription. The grey groups have none in
6
+ * print (`symbol` derives theirs, docs/conventions.md).
7
+ */
8
+
9
+ import type { MagneticPointGroupKey } from './pointGroups.js';
10
+
11
+ /** Key → printed Shubnikov symbol, in the order of Table 1.5.2.3. */
12
+ export const SHUBNIKOV: Readonly<Partial<Record<MagneticPointGroupKey, string>>> = {
13
+ '1': '1',
14
+ '-1': '-2',
15
+ "-1'": '_-2',
16
+ '2': '2',
17
+ "2'": '_2',
18
+ m: 'm',
19
+ "m'": '_m',
20
+ '2/m': '2:m',
21
+ "2'/m'": '_2:_m',
22
+ "2/m'": '2:_m',
23
+ "2'/m": '_2:m',
24
+ '222': '2:2',
25
+ "2'2'2": '2:_2',
26
+ mm2: '2.m',
27
+ "m'm'2": '2._m',
28
+ "2'm'm": '_2.m',
29
+ mmm: 'm.2:m',
30
+ "mm'm'": '_m.2:m',
31
+ "m'm'm'": '_m.2:_m',
32
+ "mmm'": 'm.2:_m',
33
+ '4': '4',
34
+ "4'": '_4',
35
+ '-4': '-4',
36
+ "-4'": '_-4',
37
+ '4/m': '4:m',
38
+ "4'/m": '_4:m',
39
+ "4/m'": '4:_m',
40
+ "4'/m'": '_4:_m',
41
+ '422': '4:2',
42
+ "4'22'": '_4:2',
43
+ "42'2'": '4:_2',
44
+ '4mm': '4.m',
45
+ "4'mm'": '_4.m',
46
+ "4m'm'": '4._m',
47
+ '-42m': '-4.m',
48
+ "-4'2m'": '_-4._m',
49
+ "-4'm2'": '_-4.m',
50
+ "-42'm'": '-4._m',
51
+ '4/mmm': 'm.4:m',
52
+ "4'/mmm'": 'm._4:m',
53
+ "4/mm'm'": '_m.4:m',
54
+ "4/m'm'm'": '_m.4:_m',
55
+ "4/m'mm": 'm.4:_m',
56
+ "4'/m'm'm": 'm._4:_m',
57
+ '3': '3',
58
+ '-3': '-6',
59
+ "-3'": '_-6',
60
+ '32': '3:2',
61
+ "32'": '3:_2',
62
+ '3m': '3.m',
63
+ "3m'": '3._m',
64
+ '-3m': '-6.m',
65
+ "-3m'": '-6._m',
66
+ "-3'm'": '_-6._m',
67
+ "-3'm": '_-6.m',
68
+ '6': '6',
69
+ "6'": '_6',
70
+ '-6': '3:m',
71
+ "-6'": '3:_m',
72
+ '6/m': '6:m',
73
+ "6'/m'": '_6:_m',
74
+ "6/m'": '6:_m',
75
+ "6'/m": '_6:m',
76
+ '622': '6:2',
77
+ "6'22'": '_6:2',
78
+ "62'2'": '6:_2',
79
+ '6mm': '6.m',
80
+ "6'mm'": '_6.m',
81
+ "6m'm'": '6._m',
82
+ '-6m2': 'm.3:m',
83
+ "-6'2m'": '_m.3:_m',
84
+ "-6'm2'": 'm.3:_m',
85
+ "-6m'2'": '_m.3:m',
86
+ '6/mmm': 'm.6:m',
87
+ "6'/m'mm'": 'm._6:_m',
88
+ "6/mm'm'": '_m.6:m',
89
+ "6/m'm'm'": '_m.6:_m',
90
+ "6/m'mm": 'm.6:_m',
91
+ "6'/mmm'": 'm._6:m',
92
+ '23': '3/2',
93
+ 'm-3': '-6/2',
94
+ "m'-3'": '_-6/2',
95
+ '432': '3/4',
96
+ "4'32'": '3/_4',
97
+ '-43m': '3/-4',
98
+ "-4'3m'": '3/_-4',
99
+ 'm-3m': '-6/4',
100
+ "m-3m'": '-6/_4',
101
+ "m'-3'm'": '_-6/4',
102
+ "m'-3'm": '_-6/_4',
103
+ };
@@ -10,8 +10,9 @@ import { determinant, dimensionOf, trace, type ExactOperation, type IntMatrix }
10
10
  import { ZERO, add, multiply, rational, scale, type Rational } from './rational.js';
11
11
 
12
12
  /**
13
- * What an operation of a plane group is, as ITA names the operations (ITA 2016 Table 2.1.2.1, p. 145): `'reflection'`
14
- * (in a line, as `'reflection'` of the point groups) and `'glide-reflection'`.
13
+ * What an operation of a plane group or plane point group is, as ITA names the operations (ITA 2016
14
+ * Table 2.1.2.1, p. 145): `'reflection'` (in a line, as `'reflection'` of the point groups) and
15
+ * `'glide-reflection'`.
15
16
  */
16
17
  export type PlaneOperationKind = 'identity' | 'translation' | 'rotation' | 'reflection' | 'glide-reflection';
17
18
 
@@ -25,6 +25,8 @@ export interface PlaneGroupEntry {
25
25
  readonly pointGroup: string;
26
26
  /** The crystal system. */
27
27
  readonly system: PlaneSystem;
28
+ /** The Patterson symmetry as printed in the headline, the symbol of a plane group, e.g. `p2mm`. */
29
+ readonly pattersonSymmetry: string;
28
30
  /** Printed page. */
29
31
  readonly page: number;
30
32
  /** The block "Origin" as printed, its positions spaced, e.g. `at 2 1 g`. */
@@ -51,6 +53,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
51
53
  fullSymbol: 'p1',
52
54
  pointGroup: '1',
53
55
  system: 'oblique',
56
+ pattersonSymmetry: 'p2',
54
57
  page: 176,
55
58
  origin: 'arbitrary',
56
59
  asymmetricUnit: '0 <= x <= 1; 0 <= y <= 1',
@@ -65,6 +68,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
65
68
  fullSymbol: 'p2',
66
69
  pointGroup: '2',
67
70
  system: 'oblique',
71
+ pattersonSymmetry: 'p2',
68
72
  page: 177,
69
73
  origin: 'at 2',
70
74
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1',
@@ -85,6 +89,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
85
89
  fullSymbol: 'p1m1',
86
90
  pointGroup: 'm',
87
91
  system: 'rectangular',
92
+ pattersonSymmetry: 'p2mm',
88
93
  page: 178,
89
94
  origin: 'on m',
90
95
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1',
@@ -103,6 +108,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
103
108
  fullSymbol: 'p1g1',
104
109
  pointGroup: 'm',
105
110
  system: 'rectangular',
111
+ pattersonSymmetry: 'p2mm',
106
112
  page: 179,
107
113
  origin: 'on g',
108
114
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1',
@@ -117,6 +123,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
117
123
  fullSymbol: 'c1m1',
118
124
  pointGroup: 'm',
119
125
  system: 'rectangular',
126
+ pattersonSymmetry: 'c2mm',
120
127
  page: 180,
121
128
  origin: 'on m',
122
129
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1/2',
@@ -134,6 +141,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
134
141
  fullSymbol: 'p2mm',
135
142
  pointGroup: '2mm',
136
143
  system: 'rectangular',
144
+ pattersonSymmetry: 'p2mm',
137
145
  page: 181,
138
146
  origin: 'at 2 m m',
139
147
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1/2',
@@ -158,6 +166,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
158
166
  fullSymbol: 'p2mg',
159
167
  pointGroup: '2mm',
160
168
  system: 'rectangular',
169
+ pattersonSymmetry: 'p2mm',
161
170
  page: 182,
162
171
  origin: 'at 2 1 g',
163
172
  asymmetricUnit: '0 <= x <= 1/4; 0 <= y <= 1',
@@ -177,6 +186,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
177
186
  fullSymbol: 'p2gg',
178
187
  pointGroup: '2mm',
179
188
  system: 'rectangular',
189
+ pattersonSymmetry: 'p2mm',
180
190
  page: 183,
181
191
  origin: 'at 2',
182
192
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1/2',
@@ -195,6 +205,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
195
205
  fullSymbol: 'c2mm',
196
206
  pointGroup: '2mm',
197
207
  system: 'rectangular',
208
+ pattersonSymmetry: 'c2mm',
198
209
  page: 184,
199
210
  origin: 'at 2 m m',
200
211
  asymmetricUnit: '0 <= x <= 1/4; 0 <= y <= 1/2',
@@ -216,6 +227,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
216
227
  fullSymbol: 'p4',
217
228
  pointGroup: '4',
218
229
  system: 'square',
230
+ pattersonSymmetry: 'p4',
219
231
  page: 185,
220
232
  origin: 'at 4',
221
233
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1/2',
@@ -235,6 +247,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
235
247
  fullSymbol: 'p4mm',
236
248
  pointGroup: '4mm',
237
249
  system: 'square',
250
+ pattersonSymmetry: 'p4mm',
238
251
  page: 186,
239
252
  origin: 'at 4 m m',
240
253
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1/2; x <= y',
@@ -257,6 +270,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
257
270
  fullSymbol: 'p4gm',
258
271
  pointGroup: '4mm',
259
272
  system: 'square',
273
+ pattersonSymmetry: 'p4mm',
260
274
  page: 187,
261
275
  origin: 'at 4 1 g',
262
276
  asymmetricUnit: '0 <= x <= 1/2; 0 <= y <= 1/2; y <= 1/2 - x',
@@ -276,6 +290,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
276
290
  fullSymbol: 'p3',
277
291
  pointGroup: '3',
278
292
  system: 'hexagonal',
293
+ pattersonSymmetry: 'p6',
279
294
  page: 188,
280
295
  origin: 'at 3',
281
296
  asymmetricUnit: '0 <= x <= 2/3; 0 <= y <= 2/3; x <= (1 + y)/2; y <= min(1 - x, (1 + x)/2)',
@@ -296,6 +311,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
296
311
  fullSymbol: 'p3m1',
297
312
  pointGroup: '3m',
298
313
  system: 'hexagonal',
314
+ pattersonSymmetry: 'p6mm',
299
315
  page: 189,
300
316
  origin: 'at 3 m 1',
301
317
  asymmetricUnit: '0 <= x <= 2/3; 0 <= y <= 2/3; x <= 2y; y <= min(1 - x, 2x)',
@@ -317,6 +333,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
317
333
  fullSymbol: 'p31m',
318
334
  pointGroup: '3m',
319
335
  system: 'hexagonal',
336
+ pattersonSymmetry: 'p6mm',
320
337
  page: 190,
321
338
  origin: 'at 3 1 m',
322
339
  asymmetricUnit: '0 <= x <= 2/3; 0 <= y <= 1/2; x <= (1 + y)/2; y <= min(1 - x, x)',
@@ -337,6 +354,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
337
354
  fullSymbol: 'p6',
338
355
  pointGroup: '6',
339
356
  system: 'hexagonal',
357
+ pattersonSymmetry: 'p6',
340
358
  page: 191,
341
359
  origin: 'at 6',
342
360
  asymmetricUnit: '0 <= x <= 2/3; 0 <= y <= 1/2; x <= (1 + y)/2; y <= min(1 - x, x)',
@@ -357,6 +375,7 @@ export const PLANE_GROUP_DATA: readonly PlaneGroupEntry[] = [
357
375
  fullSymbol: 'p6mm',
358
376
  pointGroup: '6mm',
359
377
  system: 'hexagonal',
378
+ pattersonSymmetry: 'p6mm',
360
379
  page: 192,
361
380
  origin: 'at 6 m m',
362
381
  asymmetricUnit: '0 <= x <= 2/3; 0 <= y <= 1/3; x <= (1 + y)/2; y <= x/2',
@@ -0,0 +1,208 @@
1
+ /**
2
+ * The lattices of the plane: the crystal families with their Bravais lattices and cell restrictions
3
+ * (ITA 2016 Table 2.1.1.1, p. 143, block "Two dimensions"), the Laue class of a plane point group
4
+ * (ITA 2016 § 3.2.1.1, p. 720), and the cell functions for d = 2 (#119, #137 G5). Internal; the
5
+ * entry point is `symcrys/plane`.
6
+ */
7
+
8
+ import { checkObject, shown } from '../checks.js';
9
+ import type { PlaneSystem } from './planeGroupData.js';
10
+ import {
11
+ PLANE_POINT_GROUP_KEYS,
12
+ planePointGroupOperations,
13
+ planePointGroupSettingCount,
14
+ type PlanePointGroupKey,
15
+ } from './planePointGroups.js';
16
+
17
+ /** A Bravais lattice of the plane, by its symbol in ITA Table 2.1.1.1: family symbol and centring. */
18
+ export type PlaneBravaisLattice = 'mp' | 'op' | 'oc' | 'tp' | 'hp';
19
+
20
+ /** A parameter of a plane cell: the edge lengths a, b and the angle γ between them. */
21
+ export type PlaneCellParameter = 'a' | 'b' | 'gamma';
22
+
23
+ /** The angles of a plane cell fixed by its crystal system, in degrees. */
24
+ export interface PlaneFixedAngles {
25
+ /** The angle γ between a and b, where the system fixes it. */
26
+ readonly gamma?: number;
27
+ }
28
+
29
+ /** The restrictions on the cell parameters of a crystal system of the plane (ITA Table 2.1.1.1). */
30
+ export interface CellRestrictions {
31
+ /** The parameters to be determined (column "Parameters to be determined"), e.g. `['a']` for square. */
32
+ readonly parameters: readonly PlaneCellParameter[];
33
+ /** Pairs of parameters that are equal: `[['a', 'b']]` for square and hexagonal. */
34
+ readonly equal: readonly (readonly PlaneCellParameter[])[];
35
+ /** The angles fixed to a value, in degrees: gamma 90 for rectangular and square, 120 for hexagonal; none for oblique. */
36
+ readonly angles: PlaneFixedAngles;
37
+ /** The angles conventionally taken non-acute (≥ 90°), the footnote § of the table: `['gamma']` for oblique. */
38
+ readonly nonAcute: readonly PlaneCellParameter[];
39
+ }
40
+
41
+ /** One crystal family of the plane as Table 2.1.1.1 prints it; each has one crystal system. */
42
+ interface PlaneFamily {
43
+ /** The family symbol (column "Symbol"). */
44
+ readonly symbol: 'm' | 'o' | 't' | 'h';
45
+ /** The Bravais lattices. */
46
+ readonly bravaisLattices: readonly PlaneBravaisLattice[];
47
+ readonly restrictions: CellRestrictions;
48
+ }
49
+
50
+ /** ITA 2016 Table 2.1.1.1, p. 143, block "Two dimensions" (references/itc/vol-a-table-2.1.1.1.md). */
51
+ export const PLANE_FAMILIES: Readonly<Record<PlaneSystem, PlaneFamily>> = {
52
+ oblique: {
53
+ symbol: 'm',
54
+ bravaisLattices: ['mp'],
55
+ restrictions: { parameters: ['a', 'b', 'gamma'], equal: [], angles: {}, nonAcute: ['gamma'] },
56
+ },
57
+ rectangular: {
58
+ symbol: 'o',
59
+ bravaisLattices: ['op', 'oc'],
60
+ restrictions: { parameters: ['a', 'b'], equal: [], angles: { gamma: 90 }, nonAcute: [] },
61
+ },
62
+ square: {
63
+ symbol: 't',
64
+ bravaisLattices: ['tp'],
65
+ restrictions: { parameters: ['a'], equal: [['a', 'b']], angles: { gamma: 90 }, nonAcute: [] },
66
+ },
67
+ hexagonal: {
68
+ symbol: 'h',
69
+ bravaisLattices: ['hp'],
70
+ restrictions: { parameters: ['a'], equal: [['a', 'b']], angles: { gamma: 120 }, nonAcute: [] },
71
+ },
72
+ };
73
+
74
+ /** The Bravais lattice of a plane group: the family symbol of its system and its lattice letter. */
75
+ export function planeBravaisLattice(system: PlaneSystem, groupSymbol: string): PlaneBravaisLattice {
76
+ const family = PLANE_FAMILIES[system];
77
+ const lattice = `${family.symbol}${groupSymbol[0]}` as PlaneBravaisLattice;
78
+ if (!family.bravaisLattices.includes(lattice)) {
79
+ throw new Error(`The plane group ${groupSymbol} has the lattice ${lattice}, not one of its family`);
80
+ }
81
+ return lattice;
82
+ }
83
+
84
+ const matrixKey = (m: readonly (readonly number[])[]): string => m.map((r) => r.join(',')).join(';');
85
+
86
+ /**
87
+ * The Laue class of a plane point group: the point group with the twofold rotation added, which is
88
+ * the symmetry centre of the plane ("its Laue class is obtained if a symmetry centre is added to its
89
+ * point group", ITA 2016 § 3.2.1.1, p. 720). One of the six Laue classes 2, 2mm, 4, 4mm, 6, 6mm.
90
+ */
91
+ export function planeLaueClass(key: PlanePointGroupKey): PlanePointGroupKey {
92
+ const ops = planePointGroupOperations(key).map((op) => op.matrix);
93
+ const wanted = new Set([...ops, ...ops.map((m) => m.map((r) => r.map((x) => -x + 0)))].map(matrixKey));
94
+ for (const k of PLANE_POINT_GROUP_KEYS) {
95
+ for (let s = 1; s <= planePointGroupSettingCount(k); s++) {
96
+ const have = new Set(planePointGroupOperations(k, s).map((op) => matrixKey(op.matrix)));
97
+ if (have.size === wanted.size && [...wanted].every((m) => have.has(m))) return k;
98
+ }
99
+ }
100
+ throw new Error(`No Laue class for the plane point group ${key}`);
101
+ }
102
+
103
+ // --- cells ------------------------------------------------------------------------------------------
104
+
105
+ /** A 2 × 2 matrix, as two rows: a basis has the columns a and b. */
106
+ export type Mat2 = readonly [readonly [number, number], readonly [number, number]];
107
+
108
+ /** The parameters of a plane cell: the edge lengths a, b and the angle γ between them in degrees. */
109
+ export interface LatticeParameters {
110
+ /** Edge length a. */
111
+ readonly a: number;
112
+ /** Edge length b. */
113
+ readonly b: number;
114
+ /** Angle γ between a and b, in degrees. */
115
+ readonly gamma: number;
116
+ }
117
+
118
+ const DEG = Math.PI / 180;
119
+
120
+ /** cos of an angle in degrees, exact at the angles of the plane lattices (60°, 90°, 120°, …). */
121
+ function cosDeg(x: number): number {
122
+ const exact: Record<number, number> = { 0: 1, 60: 0.5, 90: 0, 120: -0.5, 180: -1 };
123
+ return x in exact ? exact[x] : Math.cos(x * DEG);
124
+ }
125
+
126
+ /**
127
+ * The Cartesian basis of a plane cell as the columns of a 2 × 2 matrix: a along x, b at γ from a
128
+ * towards +y, so the basis is right-handed. Throws a RangeError for an edge that is not a positive
129
+ * finite number, for γ outside (0°, 180°), and for a cell that is flat within rounding (sin γ, the
130
+ * ratio V/(ab), at most 10⁻⁶, as the cells of the point groups).
131
+ */
132
+ export function planeLatticeFromParameters(p: LatticeParameters, who: string): Mat2 {
133
+ checkObject(p, `${who} takes the parameters { a, b, gamma }`);
134
+ for (const name of ['a', 'b'] as const) {
135
+ const x = p[name];
136
+ if (!(typeof x === 'number' && Number.isFinite(x) && x > 0)) {
137
+ throw new RangeError(`${who}: edge ${name} must be a positive number, not ${shown(x)}`);
138
+ }
139
+ }
140
+ const g = p.gamma;
141
+ if (!(typeof g === 'number' && Number.isFinite(g) && g > 0 && g < 180)) {
142
+ throw new RangeError(`${who}: gamma must be an angle in degrees between 0 and 180, not ${shown(g)}`);
143
+ }
144
+ const c = cosDeg(g);
145
+ const s = Math.sqrt(1 - c * c);
146
+ if (!(s > 1e-6)) throw new RangeError(`${who}: gamma ${g}° gives no cell (a and b parallel within rounding)`);
147
+ const basis: Mat2 = [
148
+ [p.a + 0, p.b * c + 0],
149
+ [0, p.b * s + 0],
150
+ ];
151
+ // Edges so large or small that the cell cannot be computed with are refused as by the basis check.
152
+ planeBasisColumns(basis, who);
153
+ return basis;
154
+ }
155
+
156
+ /**
157
+ * The columns a, b of a basis, checked: two rows of two finite numbers, and regular by the criterion
158
+ * of the point groups (`isSingular`): the product of the column lengths finite and not zero, the
159
+ * determinant above 10⁻⁹ of it, so that no later product overflows or vanishes.
160
+ */
161
+ export function planeBasisColumns(basis: unknown, who: string): [readonly number[], readonly number[]] {
162
+ const ok =
163
+ Array.isArray(basis) &&
164
+ basis.length === 2 &&
165
+ basis.every(
166
+ (row) => Array.isArray(row) && row.length === 2 && row.every((x) => typeof x === 'number' && Number.isFinite(x)),
167
+ );
168
+ if (!ok) throw new RangeError(`${who}: the basis must be a 2 × 2 matrix of finite numbers, not ${shown(basis)}`);
169
+ const b = basis as unknown as Mat2;
170
+ const bound = Math.hypot(b[0][0], b[1][0]) * Math.hypot(b[0][1], b[1][1]);
171
+ const det = b[0][0] * b[1][1] - b[0][1] * b[1][0];
172
+ if (!(Number.isFinite(bound) && bound > 0 && Math.abs(det) > 1e-9 * bound)) {
173
+ throw new RangeError(`${who}: singular basis ${shown(basis)}`);
174
+ }
175
+ return [
176
+ [b[0][0], b[1][0]],
177
+ [b[0][1], b[1][1]],
178
+ ];
179
+ }
180
+
181
+ /** The parameters a, b, γ of a basis (columns a, b), independent of its orientation and handedness. */
182
+ export function planeLatticeParameters(basis: unknown, who: string): LatticeParameters {
183
+ const [a, b] = planeBasisColumns(basis, who);
184
+ const cross = a[0] * b[1] - a[1] * b[0];
185
+ const dot = a[0] * b[0] + a[1] * b[1];
186
+ return { a: Math.hypot(a[0], a[1]), b: Math.hypot(b[0], b[1]), gamma: Math.atan2(Math.abs(cross), dot) / DEG + 0 };
187
+ }
188
+
189
+ /** The metric tensor G = AᵀA of a basis A (columns a, b). */
190
+ export function planeMetricTensor(basis: unknown, who: string): Mat2 {
191
+ const [a, b] = planeBasisColumns(basis, who);
192
+ const dot = (u: readonly number[], v: readonly number[]) => u[0] * v[0] + u[1] * v[1] + 0;
193
+ return [
194
+ [dot(a, a), dot(a, b)],
195
+ [dot(b, a), dot(b, b)],
196
+ ];
197
+ }
198
+
199
+ /** The reciprocal basis A⁻ᵀ of a basis A (columns a, b) as the columns a*, b*, with a_i · a*_j = δ_ij. */
200
+ export function planeReciprocalBasis(basis: unknown, who: string): Mat2 {
201
+ const [a, b] = planeBasisColumns(basis, who);
202
+ const det = a[0] * b[1] - a[1] * b[0];
203
+ // A = [[a0, b0], [a1, b1]]; A⁻¹ = [[b1, -b0], [-a1, a0]] / det; A⁻ᵀ = [[b1, -a1], [-b0, a0]] / det.
204
+ return [
205
+ [b[1] / det + 0, -a[1] / det + 0],
206
+ [-b[0] / det + 0, a[0] / det + 0],
207
+ ];
208
+ }
@@ -12,10 +12,30 @@ import { planeGroupEntry, planeGroupGenerators, planeGroupOperations } from './p
12
12
  import { ZERO } from './rational.js';
13
13
 
14
14
  /** The keys of the ten plane point groups, in the order of ITA Table 3.2.1.1. */
15
- export const PLANE_POINT_GROUP_KEYS = ['1', '2', 'm', '2mm', '4', '4mm', '3', '3m', '6', '6mm'] as const;
15
+ /**
16
+ * The key of a plane point group. Written out rather than derived from the list below, so that the
17
+ * declarations keep its name (a derived type is expanded there), by which the Python generator
18
+ * reads it as a key (`str`).
19
+ */
20
+ export type PlanePointGroupKey = '1' | '2' | 'm' | '2mm' | '4' | '4mm' | '3' | '3m' | '6' | '6mm';
21
+
22
+ export const PLANE_POINT_GROUP_KEYS = [
23
+ '1',
24
+ '2',
25
+ 'm',
26
+ '2mm',
27
+ '4',
28
+ '4mm',
29
+ '3',
30
+ '3m',
31
+ '6',
32
+ '6mm',
33
+ ] as const satisfies readonly PlanePointGroupKey[];
16
34
 
17
- /** The key of a plane point group. */
18
- export type PlanePointGroupKey = (typeof PLANE_POINT_GROUP_KEYS)[number];
35
+ // Every key is in the list (the list holds no other value by `satisfies`).
36
+ type MissingKey = Exclude<PlanePointGroupKey, (typeof PLANE_POINT_GROUP_KEYS)[number]>;
37
+ const everyKeyListed: [MissingKey] extends [never] ? true : never = true;
38
+ void everyKeyListed;
19
39
 
20
40
  /** The symmorphic plane group of each point group and setting, whose linear parts it is. */
21
41
  const SOURCE: Record<PlanePointGroupKey, readonly string[]> = {
@@ -5,6 +5,8 @@
5
5
  * intermediate product is checked: a result outside the safe integers throws instead of rounding.
6
6
  */
7
7
 
8
+ import { shown } from '../checks.js';
9
+
8
10
  /** A rational number n/d: d > 0, gcd(n, d) = 1, both safe integers. */
9
11
  export type Rational = readonly [numerator: number, denominator: number];
10
12
 
@@ -113,3 +115,60 @@ export function parseRational(text: string): Rational {
113
115
  export function formatRational(a: Rational): string {
114
116
  return a[1] === 1 ? String(a[0]) : `${a[0]}/${a[1]}`;
115
117
  }
118
+
119
+ /** The largest denominator of a fraction read from a number (`nearestFraction`). */
120
+ export const MAX_DENOMINATOR = 12;
121
+
122
+ /**
123
+ * The fraction with a denominator of at most `MAX_DENOMINATOR` within `tolerance` of a number from
124
+ * a caller, e.g. a translation or an entry of a transformation matrix (`what` names it in the
125
+ * messages). Two such fractions can lie as close as 1/132 (1/11 and 1/12): a value within the
126
+ * tolerance of two different ones is refused, not read as either.
127
+ */
128
+ export function nearestFraction(value: unknown, tolerance: number, what: string): Rational {
129
+ if (typeof value === 'number' && Number.isFinite(value)) {
130
+ const found: Rational[] = [];
131
+ for (let d = 1; d <= MAX_DENOMINATOR; d++) {
132
+ for (const n of [Math.floor(value * d), Math.ceil(value * d)]) {
133
+ if (!Number.isSafeInteger(n) || Math.abs(value - n / d) >= tolerance) continue;
134
+ const r = rational(n, d);
135
+ if (!found.some((f) => f[0] === r[0] && f[1] === r[1])) found.push(r);
136
+ }
137
+ }
138
+ if (found.length === 1) return found[0];
139
+ if (found.length > 1) {
140
+ throw new RangeError(
141
+ `The ${what} ${shown(value)} lies within ${tolerance} of ${found.map((f) => `${f[0]}/${f[1]}`).join(' and ')}: pass a smaller tolerance`,
142
+ );
143
+ }
144
+ }
145
+ throw new RangeError(
146
+ `Not within ${tolerance} of a ${what} with a denominator of at most ${MAX_DENOMINATOR}: ${shown(value)}`,
147
+ );
148
+ }
149
+
150
+ /**
151
+ * The simplest fraction (smallest denominator) strictly within `tolerance` of a number from a
152
+ * caller, found by descending the Stern–Brocot tree (continued fractions): unique, and exactly n/d
153
+ * for the double nearest to n/d as long as two fractions of denominators up to d lie more than
154
+ * twice the tolerance apart (d up to about 700 at the tolerance 10⁻⁶). `what` names the number in
155
+ * the messages. Throws a RangeError for a value that is not a finite number, and where the fraction
156
+ * leaves the safe integers.
157
+ */
158
+ export function simplestFraction(value: unknown, tolerance: number, what: string): Rational {
159
+ if (!(typeof value === 'number' && Number.isFinite(value))) {
160
+ throw new RangeError(`The ${what} must be a finite number, not ${shown(value)}`);
161
+ }
162
+ const simplest = (lo: number, hi: number, depth: number): Rational => {
163
+ // The simplest fraction in the open interval (lo, hi), lo < hi.
164
+ if (lo < 0 && hi > 0) return ZERO;
165
+ if (hi <= 0) return negate(simplest(-hi, -lo, depth));
166
+ if (depth > 200) throw new RangeError(`The ${what} ${shown(value)} has no fraction within ${tolerance}`);
167
+ const f = Math.floor(lo);
168
+ if (f + 1 < hi) return rational(checkSafeInteger(f + 1, `The ${what}`));
169
+ // lo and hi lie in [f, f + 1]: f + 1/y with y in (1/(hi − f), 1/(lo − f)).
170
+ const inner = simplest(1 / (hi - f), lo === f ? Infinity : 1 / (lo - f), depth + 1);
171
+ return add(rational(checkSafeInteger(f, `The ${what}`)), rational(inner[1], inner[0]));
172
+ };
173
+ return simplest(value - tolerance, value + tolerance, 0);
174
+ }