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.
Files changed (105) hide show
  1. package/README.md +11 -2
  2. package/dist/cell.d.ts +5 -2
  3. package/dist/cell.d.ts.map +1 -1
  4. package/dist/cell.js +7 -7
  5. package/dist/cell.js.map +1 -1
  6. package/dist/crystalClasses.d.ts +13 -8
  7. package/dist/crystalClasses.d.ts.map +1 -1
  8. package/dist/crystalClasses.js +13 -8
  9. package/dist/crystalClasses.js.map +1 -1
  10. package/dist/indices.d.ts +3 -1
  11. package/dist/indices.d.ts.map +1 -1
  12. package/dist/indices.js +6 -2
  13. package/dist/indices.js.map +1 -1
  14. package/dist/linalg.d.ts +7 -1
  15. package/dist/linalg.d.ts.map +1 -1
  16. package/dist/linalg.js +20 -3
  17. package/dist/linalg.js.map +1 -1
  18. package/dist/operations.d.ts +6 -0
  19. package/dist/operations.d.ts.map +1 -1
  20. package/dist/operations.js +19 -0
  21. package/dist/operations.js.map +1 -1
  22. package/dist/plane.d.ts +225 -0
  23. package/dist/plane.d.ts.map +1 -0
  24. package/dist/plane.js +219 -0
  25. package/dist/plane.js.map +1 -0
  26. package/dist/pointGroups/identify.d.ts +4 -2
  27. package/dist/pointGroups/identify.d.ts.map +1 -1
  28. package/dist/pointGroups/identify.js +5 -18
  29. package/dist/pointGroups/identify.js.map +1 -1
  30. package/dist/pointGroups/orbits.d.ts +1 -1
  31. package/dist/pointGroups/orbits.d.ts.map +1 -1
  32. package/dist/pointGroups/orbits.js +3 -3
  33. package/dist/pointGroups/orbits.js.map +1 -1
  34. package/dist/pointGroups/properties.d.ts +6 -2
  35. package/dist/pointGroups/properties.d.ts.map +1 -1
  36. package/dist/pointGroups/properties.js +6 -2
  37. package/dist/pointGroups/properties.js.map +1 -1
  38. package/dist/siteSymmetry.d.ts +5 -3
  39. package/dist/siteSymmetry.d.ts.map +1 -1
  40. package/dist/siteSymmetry.js +6 -2
  41. package/dist/siteSymmetry.js.map +1 -1
  42. package/dist/spaceGroups/affine.d.ts +81 -0
  43. package/dist/spaceGroups/affine.d.ts.map +1 -0
  44. package/dist/spaceGroups/affine.js +245 -0
  45. package/dist/spaceGroups/affine.js.map +1 -0
  46. package/dist/spaceGroups/cell.d.ts +58 -0
  47. package/dist/spaceGroups/cell.d.ts.map +1 -0
  48. package/dist/spaceGroups/cell.js +297 -0
  49. package/dist/spaceGroups/cell.js.map +1 -0
  50. package/dist/spaceGroups/describe.d.ts +44 -0
  51. package/dist/spaceGroups/describe.d.ts.map +1 -0
  52. package/dist/spaceGroups/describe.js +92 -0
  53. package/dist/spaceGroups/describe.js.map +1 -0
  54. package/dist/spaceGroups/planeGroupData.d.ts +44 -0
  55. package/dist/spaceGroups/planeGroupData.d.ts.map +1 -0
  56. package/dist/spaceGroups/planeGroupData.js +371 -0
  57. package/dist/spaceGroups/planeGroupData.js.map +1 -0
  58. package/dist/spaceGroups/planeGroups.d.ts +28 -0
  59. package/dist/spaceGroups/planeGroups.d.ts.map +1 -0
  60. package/dist/spaceGroups/planeGroups.js +61 -0
  61. package/dist/spaceGroups/planeGroups.js.map +1 -0
  62. package/dist/spaceGroups/planePointGroups.d.ts +31 -0
  63. package/dist/spaceGroups/planePointGroups.d.ts.map +1 -0
  64. package/dist/spaceGroups/planePointGroups.js +77 -0
  65. package/dist/spaceGroups/planePointGroups.js.map +1 -0
  66. package/dist/spaceGroups/positions.d.ts +65 -0
  67. package/dist/spaceGroups/positions.d.ts.map +1 -0
  68. package/dist/spaceGroups/positions.js +183 -0
  69. package/dist/spaceGroups/positions.js.map +1 -0
  70. package/dist/spaceGroups/rational.d.ts +43 -0
  71. package/dist/spaceGroups/rational.d.ts.map +1 -0
  72. package/dist/spaceGroups/rational.js +95 -0
  73. package/dist/spaceGroups/rational.js.map +1 -0
  74. package/dist/tensors/tensorForm.d.ts +3 -1
  75. package/dist/tensors/tensorForm.d.ts.map +1 -1
  76. package/dist/tensors/tensorForm.js +3 -1
  77. package/dist/tensors/tensorForm.js.map +1 -1
  78. package/dist/tensors/tensorProperties.d.ts +4 -2
  79. package/dist/tensors/tensorProperties.d.ts.map +1 -1
  80. package/dist/tensors/tensorProperties.js +4 -2
  81. package/dist/tensors/tensorProperties.js.map +1 -1
  82. package/dist/version.d.ts +1 -1
  83. package/dist/version.js +1 -1
  84. package/package.json +7 -2
  85. package/src/cell.ts +9 -8
  86. package/src/crystalClasses.ts +13 -8
  87. package/src/indices.ts +5 -2
  88. package/src/linalg.ts +21 -2
  89. package/src/operations.ts +20 -0
  90. package/src/plane.ts +455 -0
  91. package/src/pointGroups/identify.ts +5 -19
  92. package/src/pointGroups/orbits.ts +3 -3
  93. package/src/pointGroups/properties.ts +6 -2
  94. package/src/siteSymmetry.ts +6 -3
  95. package/src/spaceGroups/affine.ts +315 -0
  96. package/src/spaceGroups/cell.ts +371 -0
  97. package/src/spaceGroups/describe.ts +133 -0
  98. package/src/spaceGroups/planeGroupData.ts +407 -0
  99. package/src/spaceGroups/planeGroups.ts +75 -0
  100. package/src/spaceGroups/planePointGroups.ts +90 -0
  101. package/src/spaceGroups/positions.ts +237 -0
  102. package/src/spaceGroups/rational.ts +115 -0
  103. package/src/tensors/tensorForm.ts +3 -1
  104. package/src/tensors/tensorProperties.ts +4 -2
  105. package/src/version.ts +1 -1
package/src/cell.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * coordinates, as `latticeBasis` gives it; lengths in any unit, angles in degrees.
5
5
  */
6
6
 
7
- import { apply, det, inverse, multiply, transpose, type Mat3, type Vec3 } from './linalg.js';
7
+ import { apply, inverse, isSingular, multiply, transpose, type Mat3, type Vec3 } from './linalg.js';
8
8
  import { checkTimeReversal, type Operation as MagneticOperation } from './operations.js';
9
9
  import { EPSILON } from './tolerances.js';
10
10
 
@@ -44,7 +44,8 @@ export interface CellOperation {
44
44
  export interface IsValidCellOptions {
45
45
  /**
46
46
  * The smallest accepted ratio V/(abc) of the cell volume to the volume of the box of its edge
47
- * lengths; default 0, which accepts every cell that `isValidCell` otherwise accepts. A ratio near 0 is a nearly flat cell.
47
+ * lengths, from 0 to 1 (1 only for orthogonal axes); default 0, which accepts every cell that
48
+ * `isValidCell` otherwise accepts. A ratio near 0 is a nearly flat cell.
48
49
  */
49
50
  readonly minVolumeRatio?: number;
50
51
  }
@@ -79,13 +80,15 @@ function anglesOk({ alpha, beta, gamma }: CellAngles): boolean {
79
80
  * Whether three angles (degrees) belong to a cell: each between 0° and 180°, and the three axes not
80
81
  * in one plane (the volume radicand 1 − cos²α − cos²β − cos²γ + 2 cos α cos β cos γ exceeds 10⁻¹², i.e.
81
82
  * V/(abc) > 10⁻⁶: a cell that is flat within rounding is refused). With
82
- * `minVolumeRatio` also refuses nearly flat cells, whose ratio V/(abc), the square root of the radicand, is smaller.
83
+ * `minVolumeRatio` (from 0 to 1, default 0) also refuses nearly flat cells, whose ratio V/(abc), the
84
+ * square root of the radicand, is smaller. Throws a RangeError for a `minVolumeRatio` that is not a
85
+ * number from 0 to 1.
83
86
  */
84
87
  export function isValidCell(angles: CellAngles, options?: IsValidCellOptions): boolean {
85
88
  if (!anglesOk(angles)) return false;
86
89
  const min = options?.minVolumeRatio ?? 0;
87
- if (typeof min !== 'number' || Number.isNaN(min))
88
- throw new RangeError('isValidCell: minVolumeRatio must be a number');
90
+ if (!(typeof min === 'number' && min >= 0 && min <= 1))
91
+ throw new RangeError(`isValidCell: minVolumeRatio must be a number from 0 to 1, not ${String(min)}`);
89
92
  const r = radicand(angles);
90
93
  if (!(r > 1e-12)) return false;
91
94
  return Math.sqrt(r) >= min;
@@ -146,9 +149,7 @@ function checkBasis(basis: Mat3, who: string): void {
146
149
  (r) => Array.isArray(r) && r.length === 3 && r.every((x) => typeof x === 'number' && Number.isFinite(x)),
147
150
  );
148
151
  if (!ok) throw new RangeError(`${who}: the basis must be a 3×3 matrix of finite numbers`);
149
- const cols = columns(basis);
150
- const scale = Math.max(...cols.map(norm));
151
- if (!(Math.abs(det(basis)) > 1e-9 * scale ** 3)) throw new RangeError(`${who}: singular basis`);
152
+ if (isSingular(basis)) throw new RangeError(`${who}: singular basis`);
152
153
  }
153
154
 
154
155
  /**
@@ -286,7 +286,7 @@ export { holohedry } from './pointGroups/properties.js';
286
286
  /**
287
287
  * True if the group generated by `operations` contains the inversion, e.g. for the operations of a
288
288
  * distorted crystal. A list that is not closed is closed first. For a crystal class, read
289
- * `pointGroup(key).centrosymmetric`.
289
+ * `pointGroup(key).centrosymmetric`. Throws a RangeError for a matrix that is not orthogonal.
290
290
  */
291
291
  export function isCentrosymmetricOf(operations: readonly OperationArgument[]): boolean {
292
292
  return properties.isCentrosymmetricOf(unitary(operations));
@@ -295,7 +295,7 @@ export function isCentrosymmetricOf(operations: readonly OperationArgument[]): b
295
295
  /**
296
296
  * True if the group generated by `operations` is chiral: proper rotations only. For a crystal
297
297
  * class, read `pointGroup(key).chiral`. Polar is the named property `'pyroelectric'`:
298
- * `allowsPropertyOf(operations, 'pyroelectric')`.
298
+ * `allowsPropertyOf(operations, 'pyroelectric')`. Throws a RangeError for a matrix that is not orthogonal.
299
299
  */
300
300
  export function isChiralOf(operations: readonly OperationArgument[]): boolean {
301
301
  return properties.isChiralOf(unitary(operations));
@@ -401,8 +401,10 @@ export function symmetryDirections(
401
401
  * fixing the direction within the tolerance are closed under the products found in the list and
402
402
  * the direction is set onto their fixed space, repeated until stable, so that for a group the
403
403
  * result is a subgroup and the set direction lies exactly on its elements. For a list that is not a
404
- * group, `multiplicity` may not be an integer. Throws a RangeError for a zero vector, and if the
405
- * closed operations fix no common direction (a tolerance far too large, e.g. 0.5).
404
+ * group, `multiplicity` may not be an integer. The matrices must be Cartesian and orthogonal (not
405
+ * those of `latticeOperations`). Throws a RangeError for a matrix that is not orthogonal (within
406
+ * `tolerance`, at least `EPSILON`), for a zero vector, and if the closed operations fix no common
407
+ * direction (a tolerance far too large, e.g. 0.5).
406
408
  */
407
409
  export function siteSymmetry(
408
410
  operations: readonly OperationArgument[],
@@ -507,7 +509,8 @@ export function orbit(
507
509
  * operation, coinciding images merged (closer than `tolerance`, default `EPSILON`, as for
508
510
  * `siteSymmetry`), in the order in which the operations reach them, each with the indices of the
509
511
  * operations that map the direction there. The list is taken as it is, not closed into a group.
510
- * Throws a RangeError for a zero vector and for a matrix that is not orthogonal.
512
+ * Throws a RangeError for a zero vector and for a matrix that is not orthogonal (within
513
+ * `tolerance`, at least `EPSILON`).
511
514
  */
512
515
  export function orbitOf(
513
516
  operations: readonly OperationArgument[],
@@ -559,7 +562,8 @@ export function tensorForm(
559
562
  * The form of a tensor under the group generated by `operations`, e.g. the site symmetry of a
560
563
  * surface normal (`siteSymmetry`) or the operations of a distorted crystal. The tensor is given by
561
564
  * its specification or by the name of a property in `TENSOR_PROPERTIES`. The operations are
562
- * Cartesian and orthogonal; a list that is not closed is closed first.
565
+ * Cartesian and orthogonal; a list that is not closed is closed first. Throws a RangeError for a
566
+ * matrix that is not orthogonal.
563
567
  */
564
568
  export function tensorFormOf(
565
569
  operations: readonly OperationArgument[],
@@ -601,13 +605,14 @@ export function allowedProperties(key: PointGroupKey): TensorPropertyName[] {
601
605
 
602
606
  /**
603
607
  * True if the group generated by `operations` allows the named property, e.g. for the site
604
- * symmetry of a surface normal or the operations of a distorted crystal.
608
+ * symmetry of a surface normal or the operations of a distorted crystal. The matrices must be
609
+ * Cartesian and orthogonal; a list that is not closed is closed first. Throws a RangeError for a matrix that is not orthogonal.
605
610
  */
606
611
  export function allowsPropertyOf(operations: readonly OperationArgument[], name: TensorPropertyName): boolean {
607
612
  return tensors.tensorFormOf(unitary(operations), propertyName(name)).basis.length > 0;
608
613
  }
609
614
 
610
- /** The named properties the group generated by `operations` allows. */
615
+ /** The named properties the group generated by `operations` allows. Checks the operations as `allowsPropertyOf`. */
611
616
  export function allowedPropertiesOf(operations: readonly OperationArgument[]): TensorPropertyName[] {
612
617
  const ops = unitary(operations);
613
618
  return TENSOR_PROPERTY_NAMES.filter((name) => tensors.tensorFormOf(ops, name).basis.length > 0);
package/src/indices.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * (docs/conventions.md, "Cell, metric and indices"). Plain integers and matrices in and out.
4
4
  */
5
5
 
6
- import { det, inverse, transpose, type Mat3, type Vec3 } from './linalg.js';
6
+ import { det, inverse, isSingular, transpose, type Mat3, type Vec3 } from './linalg.js';
7
7
 
8
8
  /** A direction [UVTW] or a plane (hkil) in four indices (Miller–Bravais). */
9
9
  export type FourIndices = readonly [number, number, number, number];
@@ -98,7 +98,9 @@ export interface AxisChange {
98
98
  * The transformation of indices for a change of basis (a′, b′, c′) = (a, b, c)·P (ITA Vol. A, 2006,
99
99
  * Section 5.1.3): the columns of `P` are the new basis vectors in the old one. The coordinates of a
100
100
  * direction transform with Q = P⁻¹, the indices of a plane with Pᵀ, so that hu + kv + lw is the same in
101
- * both bases. Throws a RangeError unless det P > 0 (a right-handed basis).
101
+ * both bases. Throws a RangeError unless det P > 0 (a right-handed basis), and for a P that is
102
+ * singular within rounding (the absolute value of det P at most 10⁻⁹ times the product of its
103
+ * column lengths).
102
104
  */
103
105
  export function changeOfAxes(P: Mat3): AxisChange {
104
106
  if (
@@ -109,6 +111,7 @@ export function changeOfAxes(P: Mat3): AxisChange {
109
111
  throw new RangeError('changeOfAxes: P must be a 3×3 matrix of finite numbers');
110
112
  }
111
113
  if (!(det(P) > 0)) throw new RangeError('changeOfAxes: det P must be positive (a right-handed basis)');
114
+ if (isSingular(P)) throw new RangeError('changeOfAxes: P is singular within rounding');
112
115
  const clean = (m: Mat3): Mat3 => m.map((r) => r.map((x) => x + 0)) as unknown as Mat3; // no −0
113
116
  return { direction: clean(inverse(P)), plane: clean(transpose(P)) };
114
117
  }
package/src/linalg.ts CHANGED
@@ -120,10 +120,29 @@ export function matricesEqual(a: Mat3, b: Mat3): boolean {
120
120
  return true;
121
121
  }
122
122
 
123
- /** The inverse of a regular matrix (by the adjugate); throws for a singular one. */
123
+ /**
124
+ * Below this ratio of |det| to the product of the column lengths (Hadamard's bound, the largest
125
+ * |det| columns of these lengths can have) a matrix counts as singular. The ratio depends neither
126
+ * on the unit of the entries nor on the lengths of the columns relative to each other; for a cell
127
+ * it is V/(abc).
128
+ */
129
+ const SINGULAR_RATIO = 1e-9;
130
+
131
+ /**
132
+ * True if `a` is singular within rounding: |det a| at most `SINGULAR_RATIO` times the product of
133
+ * its column lengths. The one criterion for `inverse` and for the bases of the cell functions. Also
134
+ * true for a zero column and for a matrix with NaN or an infinite entry.
135
+ */
136
+ export function isSingular(a: Mat3): boolean {
137
+ const bound = [0, 1, 2].reduce((p, j) => p * Math.hypot(a[0][j], a[1][j], a[2][j]), 1);
138
+ // A comparison that is false for NaN, so a matrix with NaN counts as singular.
139
+ return !(Number.isFinite(bound) && Math.abs(det(a)) > SINGULAR_RATIO * bound);
140
+ }
141
+
142
+ /** The inverse of a regular matrix (by the adjugate); throws for a singular one (`isSingular`), whatever the scale of its entries. */
124
143
  export function inverse(a: Mat3): Mat3 {
144
+ if (isSingular(a)) throw new RangeError('inverse: singular matrix');
125
145
  const d = det(a);
126
- if (Math.abs(d) < EPSILON) throw new RangeError('inverse: singular matrix');
127
146
  const c = (i: number, j: number): number => {
128
147
  const [r0, r1] = [0, 1, 2].filter((k) => k !== i);
129
148
  const [s0, s1] = [0, 1, 2].filter((k) => k !== j);
package/src/operations.ts CHANGED
@@ -40,6 +40,26 @@ export function checkOrthogonal(m: Mat3, what: string): void {
40
40
  }
41
41
  }
42
42
 
43
+ /**
44
+ * Throws unless mᵀm = 1 within the tolerance: the check of `checkOrthogonal` for a function with a
45
+ * `tolerance` option, so that a matrix with rounding errors (e.g. rounded to 4 decimals) passes
46
+ * with a larger one. Never stricter than `checkOrthogonal`.
47
+ */
48
+ export function checkOrthogonalWithin(m: Mat3, what: string, tolerance: number): void {
49
+ if (tolerance <= EPSILON) {
50
+ checkOrthogonal(m, what);
51
+ return;
52
+ }
53
+ for (let i = 0; i < 3; i++) {
54
+ for (let j = 0; j < 3; j++) {
55
+ const d = m[0][i] * m[0][j] + m[1][i] * m[1][j] + m[2][i] * m[2][j];
56
+ if (!(Math.abs(d - (i === j ? 1 : 0)) < tolerance)) {
57
+ throw new RangeError(`${what} is not an orthogonal (Cartesian) matrix: ${JSON.stringify(m)}`);
58
+ }
59
+ }
60
+ }
61
+ }
62
+
43
63
  /**
44
64
  * Checks operations that a caller passes in: each must say whether it is combined with time
45
65
  * reversal. Without this, an operation that lacks `timeReversal` (as the crystallographic API
package/src/plane.ts ADDED
@@ -0,0 +1,455 @@
1
+ /**
2
+ * The plane API of symcrys (`symcrys/plane`): the 17 plane groups and the 10 plane point groups, in
3
+ * the lattice basis, as ITA (6th ed., 2016) prints them in Chapter 2.2 and Table 3.2.1.1.
4
+ * Everything exported here is covered by SemVer (see CLAUDE.md), like the exports of `index.ts`.
5
+ *
6
+ * Inside, the operations are exact (integer matrices, rational translations); here they are plain
7
+ * numbers: a translation of 1/3 is the nearest double. Keys are the symbols ITA prints: `'p2mg'`,
8
+ * `'c2mm'` for the plane groups, `'2mm'`, `'3m'` for the plane point groups; the two sets do not
9
+ * overlap. Functions that take a point take its coordinates in the lattice basis and an optional
10
+ * `tolerance` (docs/conventions.md, "Plane groups").
11
+ */
12
+
13
+ import { describePlaneOperation } from './spaceGroups/describe.js';
14
+ import { planeAsymmetricUnit, planeCellElements } from './spaceGroups/cell.js';
15
+ import { toPlain, type ExactOperation } from './spaceGroups/affine.js';
16
+ import { type PlaneSystem } from './spaceGroups/planeGroupData.js';
17
+ import {
18
+ PLANE_GROUP_KEYS as GROUP_KEYS,
19
+ planeGroupEntry,
20
+ planeGroupGenerators,
21
+ planeGroupOperations,
22
+ } from './spaceGroups/planeGroups.js';
23
+ import {
24
+ PLANE_POINT_GROUP_KEYS,
25
+ isPlanePointGroupKey,
26
+ planePointGroupGenerators,
27
+ planePointGroupOf,
28
+ planePointGroupOperations,
29
+ planePointGroupSettingCount,
30
+ planePointGroupSymbol,
31
+ planePointGroupSystem,
32
+ type PlanePointGroupKey,
33
+ } from './spaceGroups/planePointGroups.js';
34
+ import { planeOrbit, planeSiteSymmetry, planeWyckoffPositionOf } from './spaceGroups/positions.js';
35
+ import { parseRational, toNumber, type Rational } from './spaceGroups/rational.js';
36
+
37
+ export { VERSION } from './version.js';
38
+ export type { PlaneSystem } from './spaceGroups/planeGroupData.js';
39
+
40
+ /** The short symbol of a plane group, e.g. `'p2mg'`. */
41
+ export type PlaneGroupKey =
42
+ | 'p1'
43
+ | 'p2'
44
+ | 'pm'
45
+ | 'pg'
46
+ | 'cm'
47
+ | 'p2mm'
48
+ | 'p2mg'
49
+ | 'p2gg'
50
+ | 'c2mm'
51
+ | 'p4'
52
+ | 'p4mm'
53
+ | 'p4gm'
54
+ | 'p3'
55
+ | 'p3m1'
56
+ | 'p31m'
57
+ | 'p6'
58
+ | 'p6mm';
59
+
60
+ /** The key of a plane point group, e.g. `'2mm'`. */
61
+ export type PointGroupKey = PlanePointGroupKey;
62
+
63
+ /** The short symbols of the 17 plane groups, in ITA's order (plane group n at index n − 1). */
64
+ export const PLANE_GROUP_KEYS: readonly PlaneGroupKey[] = Object.freeze([...GROUP_KEYS] as PlaneGroupKey[]);
65
+
66
+ /** The keys of the 10 plane point groups, in the order of ITA Table 3.2.1.1. */
67
+ export const POINT_GROUP_KEYS: readonly PointGroupKey[] = Object.freeze([...PLANE_POINT_GROUP_KEYS]);
68
+
69
+ /** Options that choose the setting of a plane point group. */
70
+ export interface SettingOptions {
71
+ /** 1-based setting: for the point group `3m` 1 is `3m1` (default) and 2 is `31m`; the other point groups have one, and a plane group takes only 1. */
72
+ readonly setting?: number;
73
+ }
74
+
75
+ /** Options of the functions that take a point. */
76
+ export interface PointOptions {
77
+ /**
78
+ * Two points count as the same, modulo the lattice, if each coordinate (lattice basis) differs
79
+ * by less than this from an integer. Default 10⁻⁶; must be below 1/2.
80
+ */
81
+ readonly tolerance?: number;
82
+ }
83
+
84
+ /** An operation (W, w) in the lattice basis: x ↦ W·x + w. */
85
+ export interface PlaneOperation {
86
+ /** The linear part W, a 2 × 2 matrix with integer entries, rows first. */
87
+ readonly matrix: readonly (readonly number[])[];
88
+ /** The translation part w; for a plane group in [0, 1), for a point group zero. */
89
+ readonly translation: readonly number[];
90
+ }
91
+
92
+ /** A plane group. */
93
+ export interface PlaneGroup {
94
+ /** The short symbol, e.g. `'p2mg'`. */
95
+ readonly key: PlaneGroupKey;
96
+ /** The number of the plane group, 1 to 17. */
97
+ readonly number: number;
98
+ /** The full symbol as ITA prints it, e.g. `'p2mg'` or `'p1m1'`. */
99
+ readonly fullSymbol: string;
100
+ /** The plane point group. */
101
+ readonly pointGroup: PointGroupKey;
102
+ /** The setting of the point group: 2 for `p31m`, otherwise 1. */
103
+ readonly pointGroupSetting: number;
104
+ /** The crystal system. */
105
+ readonly crystalSystem: PlaneSystem;
106
+ /** The centring translations besides (0, 0): `[[0.5, 0.5]]` for `cm` and `c2mm`, otherwise none. */
107
+ readonly centring: readonly (readonly number[])[];
108
+ /** The block "Origin" as ITA prints it, e.g. `'at 2mm'`. */
109
+ readonly origin: string;
110
+ /** The order of the group modulo the lattice: the number of `operations`. */
111
+ readonly order: number;
112
+ }
113
+
114
+ /** A plane point group. */
115
+ export interface PointGroup {
116
+ /** The key, e.g. `'2mm'`. */
117
+ readonly key: PointGroupKey;
118
+ /** The crystal system. */
119
+ readonly crystalSystem: PlaneSystem;
120
+ /** The order of the group. */
121
+ readonly order: number;
122
+ }
123
+
124
+ /** A setting of a plane point group. */
125
+ export interface Setting {
126
+ /** 1-based. */
127
+ readonly index: number;
128
+ /** Its symbol: `'3m1'` or `'31m'` for `3m`, otherwise the key. */
129
+ readonly symbol: string;
130
+ /** True for the default setting (setting 1). */
131
+ readonly default: boolean;
132
+ }
133
+
134
+ /** What an operation is, geometrically, as ITA describes it under "Symmetry operations". */
135
+ export interface OperationDescription {
136
+ /** `'identity'`, `'translation'`, `'rotation'`, `'mirror'` (a reflection in a line) or `'glide'` (a glide reflection). */
137
+ readonly kind: 'identity' | 'translation' | 'rotation' | 'mirror' | 'glide';
138
+ /** The order of the linear part: 1 for the identity and a translation, 2 for a mirror or glide. */
139
+ readonly order: number;
140
+ /** For a rotation of order 3 or more: `'+'` if it turns a towards b, `'-'` otherwise. */
141
+ readonly sense?: '+' | '-';
142
+ /** For a translation: the translation vector. */
143
+ readonly translation?: readonly number[];
144
+ /**
145
+ * For a rotation: its centre. For a mirror or glide: a point of its line, on y = 0 unless the line
146
+ * is parallel to it, then on x = 0. Not reduced modulo the lattice.
147
+ */
148
+ readonly location?: readonly number[];
149
+ /** For a mirror or glide: the direction of its line, the shortest integer vector, first non-zero component positive. */
150
+ readonly direction?: readonly number[];
151
+ /** For a glide: the glide vector (w + W·w)/2, not reduced modulo the lattice. */
152
+ readonly glide?: readonly number[];
153
+ }
154
+
155
+ /** A Wyckoff position of a plane group, as ITA prints it. */
156
+ export interface WyckoffPosition {
157
+ /** The Wyckoff letter. */
158
+ readonly letter: string;
159
+ /** The multiplicity: the number of points in the conventional cell. */
160
+ readonly multiplicity: number;
161
+ /** The oriented site-symmetry symbol, e.g. `'.m.'`, `'2mm'` (without the spaces of the print). */
162
+ readonly siteSymmetry: string;
163
+ /** The printed coordinates of the (0,0)+ set, e.g. `['x,0', '-x,0']`; a leading `-` is an overbar. */
164
+ readonly coordinates: readonly string[];
165
+ }
166
+
167
+ /** The site symmetry of a point under a plane group. */
168
+ export interface SiteSymmetry {
169
+ /** Indices into `operations` of those that map the point onto itself modulo the lattice, ascending: a subgroup. */
170
+ readonly operations: readonly number[];
171
+ /** The point set onto the fixed space of its site symmetry, in [0, 1), which removes rounding errors within the tolerance. */
172
+ readonly point: readonly number[];
173
+ /** The number of points in the conventional cell equivalent to it. */
174
+ readonly multiplicity: number;
175
+ /** True if no operation other than the identity maps the point onto itself (general position). */
176
+ readonly general: boolean;
177
+ }
178
+
179
+ /** The asymmetric unit of a plane group, a convex polygon in the lattice basis. */
180
+ export interface AsymmetricUnit {
181
+ /** Its vertices, counter-clockwise (a towards b), starting at the lowest (smallest y, then x). */
182
+ readonly vertices: readonly (readonly number[])[];
183
+ /** Per edge, the inequality [a, b, c] for a·x + b·y ≤ c (integers, no common factor); entry i holds the edge from vertex i to vertex i + 1. */
184
+ readonly inequalities: readonly (readonly number[])[];
185
+ }
186
+
187
+ /** A rotation centre in the cell. */
188
+ export interface RotationCentre {
189
+ /** The highest order of a rotation about it. */
190
+ readonly order: number;
191
+ /** Its position in [0, 1]². */
192
+ readonly point: readonly number[];
193
+ }
194
+
195
+ /** A mirror or glide line: the part of it in the cell. */
196
+ export interface SymmetryLine {
197
+ /** `'mirror'` or `'glide'`. */
198
+ readonly kind: 'mirror' | 'glide';
199
+ /** The direction of the line, the shortest integer vector, first non-zero component positive. */
200
+ readonly direction: readonly number[];
201
+ /** For a glide line its glide vector s·direction, 0 < s < 1; for a mirror line (0, 0). */
202
+ readonly glide: readonly number[];
203
+ /** The two ends of the segment of the line in the cell [0, 1]², in the order of `direction`. */
204
+ readonly segment: readonly (readonly number[])[];
205
+ }
206
+
207
+ /** The symmetry elements of a plane group in its conventional cell [0, 1]², boundary included. */
208
+ export interface SymmetryElements {
209
+ /** The rotation centres, by descending order, then by y and x. */
210
+ readonly rotations: readonly RotationCentre[];
211
+ /** The mirror and glide lines that cross the cell, by direction, then by the start of the segment (y, then x). */
212
+ readonly lines: readonly SymmetryLine[];
213
+ }
214
+
215
+ // ---------------------------------------------------------------------------------------------
216
+
217
+ const numbers = (v: readonly Rational[]): number[] => v.map(toNumber);
218
+
219
+ function isPlaneGroupKey(key: unknown): key is PlaneGroupKey {
220
+ return typeof key === 'string' && (GROUP_KEYS as readonly string[]).includes(key);
221
+ }
222
+
223
+ function unknownKey(key: unknown, what = 'plane group or plane point group'): never {
224
+ throw new RangeError(`Unknown ${what}: ${JSON.stringify(key)}`);
225
+ }
226
+
227
+ /** The plane group of a key; throws for a point group or an unknown key. */
228
+ function groupKey(key: unknown, what: string): PlaneGroupKey {
229
+ if (isPlaneGroupKey(key)) return key;
230
+ if (isPlanePointGroupKey(key)) {
231
+ throw new RangeError(`${what} takes the key of a plane group, not of the point group ${key}`);
232
+ }
233
+ return unknownKey(key, 'plane group');
234
+ }
235
+
236
+ /** The key of a plane point group; throws for a plane group or an unknown key. */
237
+ function pointKey(key: unknown, what: string): PointGroupKey {
238
+ if (isPlanePointGroupKey(key)) return key;
239
+ if (isPlaneGroupKey(key)) {
240
+ throw new RangeError(`${what} takes the key of a plane point group; ${key} is a plane group (planeGroup)`);
241
+ }
242
+ return unknownKey(key, 'plane point group');
243
+ }
244
+
245
+ function settingOf(options: SettingOptions | undefined): number | undefined {
246
+ return options?.setting;
247
+ }
248
+
249
+ /** The exact operations for a key: a plane group (setting 1 only) or a point group in a setting. */
250
+ function exact(key: unknown, options: SettingOptions | undefined, generatorsOnly: boolean): ExactOperation[] {
251
+ const setting = settingOf(options);
252
+ if (isPlaneGroupKey(key)) {
253
+ if (setting !== undefined && setting !== 1) {
254
+ throw new RangeError(`The plane group ${key} has no setting ${String(setting)}`);
255
+ }
256
+ return generatorsOnly ? planeGroupGenerators(key) : planeGroupOperations(key);
257
+ }
258
+ if (isPlanePointGroupKey(key)) {
259
+ return generatorsOnly ? planePointGroupGenerators(key, setting) : planePointGroupOperations(key, setting);
260
+ }
261
+ return unknownKey(key);
262
+ }
263
+
264
+ const plain = (op: ExactOperation): PlaneOperation => {
265
+ const { matrix, translation } = toPlain(op);
266
+ return Object.freeze({
267
+ matrix: Object.freeze(matrix.map((row) => Object.freeze([...row]))),
268
+ translation: Object.freeze([...translation]),
269
+ });
270
+ };
271
+
272
+ /** The plane group with this key (short symbol). Throws a RangeError for an unknown key. */
273
+ export function planeGroup(key: PlaneGroupKey): PlaneGroup {
274
+ const entry = planeGroupEntry(groupKey(key, 'planeGroup'));
275
+ const point = planePointGroupOf(entry.symbol);
276
+ return Object.freeze({
277
+ key: entry.symbol as PlaneGroupKey,
278
+ number: entry.number,
279
+ fullSymbol: entry.fullSymbol,
280
+ pointGroup: point.key,
281
+ pointGroupSetting: point.setting,
282
+ crystalSystem: entry.system,
283
+ centring: Object.freeze(
284
+ entry.centring.map((t) => Object.freeze(t.split(',').map((c) => toNumber(parseRational(c))))),
285
+ ),
286
+ origin: entry.origin,
287
+ order: planeGroupOperations(entry.symbol).length,
288
+ });
289
+ }
290
+
291
+ /** The plane point group with this key. Throws a RangeError for an unknown key. */
292
+ export function pointGroup(key: PointGroupKey): PointGroup {
293
+ const k = pointKey(key, 'pointGroup');
294
+ return Object.freeze({ key: k, crystalSystem: planePointGroupSystem(k), order: planePointGroupOperations(k).length });
295
+ }
296
+
297
+ /**
298
+ * The settings of a plane point group: two for `3m` (`3m1`, the default, and `31m`), one for the
299
+ * others. Throws a RangeError for an unknown key.
300
+ */
301
+ export function settings(key: PointGroupKey): readonly Setting[] {
302
+ const k = pointKey(key, 'settings');
303
+ return Object.freeze(
304
+ Array.from({ length: planePointGroupSettingCount(k) }, (_, i) =>
305
+ Object.freeze({ index: i + 1, symbol: planePointGroupSymbol(k, i + 1), default: i === 0 }),
306
+ ),
307
+ );
308
+ }
309
+
310
+ /** The symbol of a plane point group in a setting: `'3m1'` or `'31m'` for `3m`, otherwise the key. */
311
+ export function symbol(key: PointGroupKey, options?: SettingOptions): string {
312
+ return planePointGroupSymbol(pointKey(key, 'symbol'), settingOf(options));
313
+ }
314
+
315
+ /**
316
+ * The operations of a plane group or a plane point group, in ITA's order, identity first. A plane
317
+ * group: one per coset of the integer translations, translations in [0, 1), the general position
318
+ * as numbered, then for a centred group the same sequence with the centring added (the
319
+ * "(1/2,1/2)+ set"). A point group: the linear parts of its symmorphic plane group (pm for m,
320
+ * p3m1 and p31m for the settings of 3m), translation zero. Throws a RangeError for an unknown key
321
+ * or setting.
322
+ */
323
+ export function operations(key: PlaneGroupKey | PointGroupKey, options?: SettingOptions): readonly PlaneOperation[] {
324
+ return Object.freeze(exact(key, options, false).map(plain));
325
+ }
326
+
327
+ /**
328
+ * ITA's selected generators, in printed order, without the identity and the integer translations:
329
+ * for a centred plane group the centring translation first. A point group: the linear parts of the
330
+ * generators of its symmorphic plane group. Throws a RangeError for an unknown key or setting.
331
+ */
332
+ export function generators(key: PlaneGroupKey | PointGroupKey, options?: SettingOptions): readonly PlaneOperation[] {
333
+ return Object.freeze(exact(key, options, true).map(plain));
334
+ }
335
+
336
+ /**
337
+ * Each operation of `operations` (same key and setting) described geometrically, index for index:
338
+ * what it is, its order and sense, where its symmetry element lies, its glide part. Throws a
339
+ * RangeError for an unknown key or setting.
340
+ */
341
+ export function describeOperations(
342
+ key: PlaneGroupKey | PointGroupKey,
343
+ options?: SettingOptions,
344
+ ): readonly OperationDescription[] {
345
+ return Object.freeze(
346
+ exact(key, options, false).map((op) => {
347
+ const d = describePlaneOperation(op);
348
+ return Object.freeze({
349
+ kind: d.kind,
350
+ order: d.order,
351
+ ...(d.sense ? { sense: d.sense } : {}),
352
+ ...(d.translation ? { translation: Object.freeze(numbers(d.translation)) } : {}),
353
+ ...(d.location ? { location: Object.freeze(numbers(d.location)) } : {}),
354
+ ...(d.direction ? { direction: Object.freeze([...d.direction]) } : {}),
355
+ ...(d.glide ? { glide: Object.freeze(numbers(d.glide)) } : {}),
356
+ });
357
+ }),
358
+ );
359
+ }
360
+
361
+ /**
362
+ * The Wyckoff positions of a plane group as ITA prints them, general position first. Throws a
363
+ * RangeError for an unknown key and for a plane point group, whose positions are not in symcrys
364
+ * yet.
365
+ */
366
+ export function wyckoffPositions(key: PlaneGroupKey): readonly WyckoffPosition[] {
367
+ const entry = planeGroupEntry(groupKey(key, 'wyckoffPositions'));
368
+ return Object.freeze(
369
+ entry.wyckoff.map(([multiplicity, letter, siteSymmetry, coordinates]) =>
370
+ Object.freeze({ letter, multiplicity, siteSymmetry, coordinates: Object.freeze([...coordinates]) }),
371
+ ),
372
+ );
373
+ }
374
+
375
+ /**
376
+ * The site symmetry of a point (lattice basis) under a plane group: the operations that map it
377
+ * onto itself modulo the lattice within the tolerance, closed into a group, and the point set onto
378
+ * their fixed space. Throws a RangeError for an unknown key, a point that is not two finite
379
+ * numbers, or a tolerance outside (0, 1/2).
380
+ */
381
+ export function siteSymmetry(key: PlaneGroupKey, point: readonly number[], options?: PointOptions): SiteSymmetry {
382
+ const site = planeSiteSymmetry(groupKey(key, 'siteSymmetry'), point, options);
383
+ return Object.freeze({
384
+ operations: Object.freeze([...site.operations]),
385
+ point: Object.freeze([...site.point]),
386
+ multiplicity: site.multiplicity,
387
+ general: site.operations.length === 1,
388
+ });
389
+ }
390
+
391
+ /**
392
+ * The Wyckoff position of a point (lattice basis) under a plane group, as `wyckoffPositions` gives
393
+ * it: the printed position of the point's multiplicity whose coordinates, with the centring, reach
394
+ * the point set onto its site symmetry. Throws a RangeError as `siteSymmetry`.
395
+ */
396
+ export function wyckoffPositionOf(
397
+ key: PlaneGroupKey,
398
+ point: readonly number[],
399
+ options?: PointOptions,
400
+ ): WyckoffPosition {
401
+ const k = groupKey(key, 'wyckoffPositionOf');
402
+ const { letter } = planeWyckoffPositionOf(k, point, options);
403
+ const found = wyckoffPositions(k).find((w) => w.letter === letter);
404
+ if (!found) throw new Error(`No Wyckoff position ${letter} in ${k}`);
405
+ return found;
406
+ }
407
+
408
+ /**
409
+ * The orbit of a point (lattice basis) under a plane group: its distinct images in the
410
+ * conventional cell, each in [0, 1), in the order of the operations that first give them; images
411
+ * that coincide within the tolerance are merged. Throws a RangeError as `siteSymmetry`.
412
+ */
413
+ export function orbit(
414
+ key: PlaneGroupKey,
415
+ point: readonly number[],
416
+ options?: PointOptions,
417
+ ): readonly (readonly number[])[] {
418
+ return Object.freeze(planeOrbit(groupKey(key, 'orbit'), point, options).map((p) => Object.freeze(p)));
419
+ }
420
+
421
+ /**
422
+ * The asymmetric unit of a plane group as ITA prints it: a convex polygon given by its vertices and,
423
+ * per edge, the printed inequality that holds it. Throws a RangeError for an unknown key.
424
+ */
425
+ export function asymmetricUnit(key: PlaneGroupKey): AsymmetricUnit {
426
+ const unit = planeAsymmetricUnit(groupKey(key, 'asymmetricUnit'));
427
+ return Object.freeze({
428
+ vertices: Object.freeze(unit.vertices.map((v) => Object.freeze(numbers(v)))),
429
+ inequalities: Object.freeze(unit.inequalities.map((q) => Object.freeze([...q]))),
430
+ });
431
+ }
432
+
433
+ /**
434
+ * The symmetry elements of a plane group in its conventional cell [0, 1]², boundary included:
435
+ * every rotation centre with the highest order of a rotation about it, and every mirror and glide
436
+ * line that crosses the cell, with the segment inside it. Throws a RangeError for an unknown key.
437
+ */
438
+ export function symmetryElements(key: PlaneGroupKey): SymmetryElements {
439
+ const e = planeCellElements(groupKey(key, 'symmetryElements'));
440
+ return Object.freeze({
441
+ rotations: Object.freeze(
442
+ e.rotations.map((r) => Object.freeze({ order: r.order, point: Object.freeze(numbers(r.point)) })),
443
+ ),
444
+ lines: Object.freeze(
445
+ e.lines.map((l) =>
446
+ Object.freeze({
447
+ kind: l.kind,
448
+ direction: Object.freeze([...l.direction]),
449
+ glide: Object.freeze(numbers(l.glide)),
450
+ segment: Object.freeze(l.segment.map((p) => Object.freeze(numbers(p)))),
451
+ }),
452
+ ),
453
+ ),
454
+ });
455
+ }