symcrys 0.10.0 → 0.11.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 (218) hide show
  1. package/README.md +6 -6
  2. package/dist/cell.d.ts +19 -16
  3. package/dist/cell.d.ts.map +1 -1
  4. package/dist/cell.js +63 -35
  5. package/dist/cell.js.map +1 -1
  6. package/dist/checks.d.ts +29 -0
  7. package/dist/checks.d.ts.map +1 -0
  8. package/dist/checks.js +79 -0
  9. package/dist/checks.js.map +1 -0
  10. package/dist/closure.d.ts +43 -0
  11. package/dist/closure.d.ts.map +1 -0
  12. package/dist/closure.js +201 -0
  13. package/dist/closure.js.map +1 -0
  14. package/dist/crystalClasses.d.ts +146 -118
  15. package/dist/crystalClasses.d.ts.map +1 -1
  16. package/dist/crystalClasses.js +163 -124
  17. package/dist/crystalClasses.js.map +1 -1
  18. package/dist/freeze.d.ts +7 -0
  19. package/dist/freeze.d.ts.map +1 -0
  20. package/dist/freeze.js +15 -0
  21. package/dist/freeze.js.map +1 -0
  22. package/dist/index.d.ts +11 -10
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +4 -3
  25. package/dist/index.js.map +1 -1
  26. package/dist/indices.d.ts +31 -5
  27. package/dist/indices.d.ts.map +1 -1
  28. package/dist/indices.js +104 -13
  29. package/dist/indices.js.map +1 -1
  30. package/dist/lattice.d.ts +15 -1
  31. package/dist/lattice.d.ts.map +1 -1
  32. package/dist/lattice.js +30 -1
  33. package/dist/lattice.js.map +1 -1
  34. package/dist/linalg.d.ts +2 -2
  35. package/dist/linalg.d.ts.map +1 -1
  36. package/dist/linalg.js +3 -3
  37. package/dist/linalg.js.map +1 -1
  38. package/dist/magnetic.d.ts +12 -10
  39. package/dist/magnetic.d.ts.map +1 -1
  40. package/dist/magnetic.js +8 -6
  41. package/dist/magnetic.js.map +1 -1
  42. package/dist/notation.d.ts +16 -0
  43. package/dist/notation.d.ts.map +1 -0
  44. package/dist/notation.js +66 -0
  45. package/dist/notation.js.map +1 -0
  46. package/dist/operations.d.ts +40 -26
  47. package/dist/operations.d.ts.map +1 -1
  48. package/dist/operations.js +48 -60
  49. package/dist/operations.js.map +1 -1
  50. package/dist/plane.d.ts +141 -79
  51. package/dist/plane.d.ts.map +1 -1
  52. package/dist/plane.js +147 -77
  53. package/dist/plane.js.map +1 -1
  54. package/dist/pointGroups/crystalForms.d.ts +6 -21
  55. package/dist/pointGroups/crystalForms.d.ts.map +1 -1
  56. package/dist/pointGroups/crystalForms.js +9 -16
  57. package/dist/pointGroups/crystalForms.js.map +1 -1
  58. package/dist/pointGroups/crystalFormsData.d.ts +4 -1
  59. package/dist/pointGroups/crystalFormsData.d.ts.map +1 -1
  60. package/dist/pointGroups/crystalFormsData.js +64 -1
  61. package/dist/pointGroups/crystalFormsData.js.map +1 -1
  62. package/dist/pointGroups/elements.d.ts +19 -17
  63. package/dist/pointGroups/elements.d.ts.map +1 -1
  64. package/dist/pointGroups/elements.js +65 -83
  65. package/dist/pointGroups/elements.js.map +1 -1
  66. package/dist/pointGroups/identify.d.ts +10 -18
  67. package/dist/pointGroups/identify.d.ts.map +1 -1
  68. package/dist/pointGroups/identify.js +27 -79
  69. package/dist/pointGroups/identify.js.map +1 -1
  70. package/dist/pointGroups/itaOrder.d.ts +15 -14
  71. package/dist/pointGroups/itaOrder.d.ts.map +1 -1
  72. package/dist/pointGroups/itaOrder.js +116 -94
  73. package/dist/pointGroups/itaOrder.js.map +1 -1
  74. package/dist/pointGroups/itcData.d.ts +96 -0
  75. package/dist/pointGroups/itcData.d.ts.map +1 -0
  76. package/dist/pointGroups/itcData.js +336 -0
  77. package/dist/pointGroups/itcData.js.map +1 -0
  78. package/dist/pointGroups/itcMagneticData.d.ts +55 -0
  79. package/dist/pointGroups/itcMagneticData.d.ts.map +1 -0
  80. package/dist/pointGroups/itcMagneticData.js +172 -0
  81. package/dist/pointGroups/itcMagneticData.js.map +1 -0
  82. package/dist/pointGroups/keys.d.ts +32 -0
  83. package/dist/pointGroups/keys.d.ts.map +1 -0
  84. package/dist/pointGroups/keys.js +120 -0
  85. package/dist/pointGroups/keys.js.map +1 -0
  86. package/dist/pointGroups/magneticSymbols.d.ts +6 -5
  87. package/dist/pointGroups/magneticSymbols.d.ts.map +1 -1
  88. package/dist/pointGroups/magneticSymbols.js +15 -12
  89. package/dist/pointGroups/magneticSymbols.js.map +1 -1
  90. package/dist/pointGroups/orbits.d.ts +23 -42
  91. package/dist/pointGroups/orbits.d.ts.map +1 -1
  92. package/dist/pointGroups/orbits.js +20 -62
  93. package/dist/pointGroups/orbits.js.map +1 -1
  94. package/dist/pointGroups/pointGroups.d.ts +135 -67
  95. package/dist/pointGroups/pointGroups.d.ts.map +1 -1
  96. package/dist/pointGroups/pointGroups.js +409 -244
  97. package/dist/pointGroups/pointGroups.js.map +1 -1
  98. package/dist/pointGroups/properties.d.ts +21 -22
  99. package/dist/pointGroups/properties.d.ts.map +1 -1
  100. package/dist/pointGroups/properties.js +40 -37
  101. package/dist/pointGroups/properties.js.map +1 -1
  102. package/dist/pointGroups/seitzData.d.ts +26 -0
  103. package/dist/pointGroups/seitzData.d.ts.map +1 -0
  104. package/dist/pointGroups/seitzData.js +130 -0
  105. package/dist/pointGroups/seitzData.js.map +1 -0
  106. package/dist/pointGroups/wyckoff.d.ts +70 -40
  107. package/dist/pointGroups/wyckoff.d.ts.map +1 -1
  108. package/dist/pointGroups/wyckoff.js +98 -91
  109. package/dist/pointGroups/wyckoff.js.map +1 -1
  110. package/dist/pointGroups/wyckoffData.d.ts +7 -4
  111. package/dist/pointGroups/wyckoffData.d.ts.map +1 -1
  112. package/dist/pointGroups/wyckoffData.js +789 -168
  113. package/dist/pointGroups/wyckoffData.js.map +1 -1
  114. package/dist/siteSymmetry.d.ts +9 -17
  115. package/dist/siteSymmetry.d.ts.map +1 -1
  116. package/dist/siteSymmetry.js +62 -47
  117. package/dist/siteSymmetry.js.map +1 -1
  118. package/dist/spaceGroups/affine.d.ts.map +1 -1
  119. package/dist/spaceGroups/affine.js +3 -2
  120. package/dist/spaceGroups/affine.js.map +1 -1
  121. package/dist/spaceGroups/cell.d.ts +43 -31
  122. package/dist/spaceGroups/cell.d.ts.map +1 -1
  123. package/dist/spaceGroups/cell.js +173 -85
  124. package/dist/spaceGroups/cell.js.map +1 -1
  125. package/dist/spaceGroups/describe.d.ts +12 -10
  126. package/dist/spaceGroups/describe.d.ts.map +1 -1
  127. package/dist/spaceGroups/describe.js +11 -10
  128. package/dist/spaceGroups/describe.js.map +1 -1
  129. package/dist/spaceGroups/planeGroupData.d.ts +1 -1
  130. package/dist/spaceGroups/planeGroupData.d.ts.map +1 -1
  131. package/dist/spaceGroups/planeGroupData.js +8 -8
  132. package/dist/spaceGroups/planeGroupData.js.map +1 -1
  133. package/dist/spaceGroups/planeGroups.d.ts.map +1 -1
  134. package/dist/spaceGroups/planeGroups.js +2 -1
  135. package/dist/spaceGroups/planeGroups.js.map +1 -1
  136. package/dist/spaceGroups/positions.d.ts +48 -19
  137. package/dist/spaceGroups/positions.d.ts.map +1 -1
  138. package/dist/spaceGroups/positions.js +166 -67
  139. package/dist/spaceGroups/positions.js.map +1 -1
  140. package/dist/tensors/propertyTable.d.ts +3 -1
  141. package/dist/tensors/propertyTable.d.ts.map +1 -1
  142. package/dist/tensors/propertyTable.js +17 -10
  143. package/dist/tensors/propertyTable.js.map +1 -1
  144. package/dist/tensors/rowEchelon.d.ts +12 -4
  145. package/dist/tensors/rowEchelon.d.ts.map +1 -1
  146. package/dist/tensors/rowEchelon.js +24 -11
  147. package/dist/tensors/rowEchelon.js.map +1 -1
  148. package/dist/tensors/tensorForm.d.ts +75 -34
  149. package/dist/tensors/tensorForm.d.ts.map +1 -1
  150. package/dist/tensors/tensorForm.js +173 -68
  151. package/dist/tensors/tensorForm.js.map +1 -1
  152. package/dist/tensors/tensorProperties.d.ts +10 -9
  153. package/dist/tensors/tensorProperties.d.ts.map +1 -1
  154. package/dist/tensors/tensorProperties.js +22 -12
  155. package/dist/tensors/tensorProperties.js.map +1 -1
  156. package/dist/tolerances.d.ts +56 -1
  157. package/dist/tolerances.d.ts.map +1 -1
  158. package/dist/tolerances.js +51 -1
  159. package/dist/tolerances.js.map +1 -1
  160. package/dist/version.d.ts +1 -1
  161. package/dist/version.js +1 -1
  162. package/package.json +1 -1
  163. package/src/cell.ts +78 -51
  164. package/src/checks.ts +84 -0
  165. package/src/closure.ts +214 -0
  166. package/src/crystalClasses.ts +234 -160
  167. package/src/freeze.ts +14 -0
  168. package/src/index.ts +21 -10
  169. package/src/indices.ts +108 -18
  170. package/src/lattice.ts +41 -1
  171. package/src/linalg.ts +3 -3
  172. package/src/magnetic.ts +14 -9
  173. package/src/notation.ts +73 -0
  174. package/src/operations.ts +78 -69
  175. package/src/plane.ts +253 -127
  176. package/src/pointGroups/crystalForms.ts +11 -44
  177. package/src/pointGroups/crystalFormsData.ts +66 -1
  178. package/src/pointGroups/elements.ts +82 -92
  179. package/src/pointGroups/identify.ts +38 -97
  180. package/src/pointGroups/itaOrder.ts +117 -101
  181. package/src/pointGroups/itcData.ts +404 -0
  182. package/src/pointGroups/itcMagneticData.ts +206 -0
  183. package/src/pointGroups/keys.ts +144 -0
  184. package/src/pointGroups/magneticSymbols.ts +18 -17
  185. package/src/pointGroups/orbits.ts +41 -89
  186. package/src/pointGroups/pointGroups.ts +540 -297
  187. package/src/pointGroups/properties.ts +44 -40
  188. package/src/pointGroups/seitzData.ts +147 -0
  189. package/src/pointGroups/wyckoff.ts +137 -112
  190. package/src/pointGroups/wyckoffData.ts +793 -170
  191. package/src/siteSymmetry.ts +71 -64
  192. package/src/spaceGroups/affine.ts +3 -2
  193. package/src/spaceGroups/cell.ts +227 -116
  194. package/src/spaceGroups/describe.ts +22 -19
  195. package/src/spaceGroups/planeGroupData.ts +9 -9
  196. package/src/spaceGroups/planeGroups.ts +2 -1
  197. package/src/spaceGroups/positions.ts +202 -78
  198. package/src/tensors/propertyTable.ts +29 -17
  199. package/src/tensors/rowEchelon.ts +28 -9
  200. package/src/tensors/tensorForm.ts +260 -89
  201. package/src/tensors/tensorProperties.ts +34 -14
  202. package/src/tolerances.ts +66 -1
  203. package/src/version.ts +1 -1
  204. package/dist/pointGroups/data.d.ts +0 -75
  205. package/dist/pointGroups/data.d.ts.map +0 -1
  206. package/dist/pointGroups/data.js +0 -212
  207. package/dist/pointGroups/data.js.map +0 -1
  208. package/dist/pointGroups/magneticData.d.ts +0 -66
  209. package/dist/pointGroups/magneticData.d.ts.map +0 -1
  210. package/dist/pointGroups/magneticData.js +0 -184
  211. package/dist/pointGroups/magneticData.js.map +0 -1
  212. package/dist/spaceGroups/snap.d.ts +0 -20
  213. package/dist/spaceGroups/snap.d.ts.map +0 -1
  214. package/dist/spaceGroups/snap.js +0 -103
  215. package/dist/spaceGroups/snap.js.map +0 -1
  216. package/src/pointGroups/data.ts +0 -305
  217. package/src/pointGroups/magneticData.ts +0 -328
  218. package/src/spaceGroups/snap.ts +0 -121
@@ -2,21 +2,13 @@
2
2
  * Points of a plane group: their orbit, site symmetry and Wyckoff position (CLAUDE.md, "Plane
3
3
  * groups (and later space groups)"). Points come from callers as floating-point coordinates in the
4
4
  * lattice basis; whether an operation fixes a point is decided within a tolerance, as for the
5
- * point groups (`siteSymmetry`). Internal for now.
5
+ * point groups (`siteSymmetryOf`). Internal for now.
6
6
  */
7
7
 
8
+ import { shown } from '../checks.js';
8
9
  import { compose, equalModLattice, parseOperation, toPlain, type PlainOperation } from './affine.js';
9
10
  import { planeGroupEntry, planeGroupOperations } from './planeGroups.js';
10
- import { EPSILON } from '../tolerances.js';
11
-
12
- /** Options of the functions that take a point. */
13
- export interface PointOptions {
14
- /**
15
- * Two points count as the same, modulo the lattice, if each coordinate differs by less than this
16
- * from an integer (coordinates in the lattice basis). Default 10⁻⁶.
17
- */
18
- readonly tolerance?: number;
19
- }
11
+ import { toleranceOf, type ToleranceOptions } from '../tolerances.js';
20
12
 
21
13
  /** The site symmetry of a point under a plane group. */
22
14
  export interface PlaneSiteSymmetry {
@@ -49,18 +41,14 @@ export interface PlaneWyckoffPosition {
49
41
  readonly siteSymmetry: string;
50
42
  }
51
43
 
52
- function tolerance(options?: PointOptions): number {
53
- const t = options?.tolerance ?? EPSILON;
44
+ function tolerance(options?: ToleranceOptions): number {
54
45
  // Modulo the lattice two coordinates differ by at most 1/2, so a tolerance of 1/2 or more says nothing.
55
- if (!(typeof t === 'number' && t > 0 && t < 0.5)) {
56
- throw new RangeError(`The tolerance must lie between 0 and 1/2, not ${String(t)}`);
57
- }
58
- return t;
46
+ return toleranceOf(options, 'points');
59
47
  }
60
48
 
61
49
  function checkPoint(point: readonly number[]): void {
62
- if (point.length !== 2 || !point.every(Number.isFinite)) {
63
- throw new RangeError(`A point of the plane has two finite coordinates, not ${JSON.stringify(point)}`);
50
+ if (!Array.isArray(point) || point.length !== 2 || !point.every((x) => typeof x === 'number' && Number.isFinite(x))) {
51
+ throw new RangeError(`A point of the plane has two finite coordinates, not ${shown(point)}`);
64
52
  }
65
53
  }
66
54
 
@@ -114,18 +102,28 @@ function groupData(key: string): GroupData {
114
102
  const plainOperations = (key: string): readonly PlainOperation[] => groupData(key).ops;
115
103
 
116
104
  /**
117
- * The orbit of a point under a plane group: its distinct images in the conventional cell, each in
118
- * [0, 1), in the order of the operations that first produce them. Coinciding images (within the
119
- * tolerance) are merged.
105
+ * The orbit of a point under a plane group: the images of the point set onto its Wyckoff position
106
+ * (`planeSiteOfPosition`), each in [0, 1), in the order of the operations that first produce them,
107
+ * with the operations that map the point there: one entry per coset of the site symmetry.
120
108
  */
121
- export function planeOrbit(key: string, point: readonly number[], options?: PointOptions): number[][] {
122
- checkPoint(point);
123
- const tol = tolerance(options);
124
- const out: number[][] = [];
125
- for (const op of plainOperations(key)) {
126
- const q = image(op, point).map(mod1);
127
- if (!out.some((r) => sameModLattice(r, q, tol))) out.push(q);
128
- }
109
+ export function planeOrbit(
110
+ key: string,
111
+ point: readonly number[],
112
+ options?: ToleranceOptions,
113
+ ): { point: number[]; operations: number[] }[] {
114
+ // The point is first set onto its Wyckoff position (#137 F1), and the entries are the left cosets
115
+ // g·H of its site symmetry H: merging images by a distance is not transitive and depends on a
116
+ // threshold (reviews of #153 and of step 10a).
117
+ const site = planeSiteOfPosition(key, point, options);
118
+ const { ops, table } = groupData(key);
119
+ const done = new Set<number>();
120
+ const out: { point: number[]; operations: number[] }[] = [];
121
+ ops.forEach((op, i) => {
122
+ if (done.has(i)) return;
123
+ const coset = [...new Set(site.operations.map((h) => table[i][h]))].sort((a, b) => a - b);
124
+ for (const j of coset) done.add(j);
125
+ out.push({ point: image(op, site.point).map(mod1), operations: coset });
126
+ });
129
127
  return out;
130
128
  }
131
129
 
@@ -160,11 +158,15 @@ function fixingOperations(ops: readonly PlainOperation[], indices: readonly numb
160
158
  /**
161
159
  * The site symmetry of a point under a plane group. The operations that fix the point modulo the
162
160
  * lattice within the tolerance are closed under products and the point is set onto the fixed space
163
- * of the closure (the mean of its images), repeated until stable, as `siteSymmetry` does for the point
161
+ * of the closure (the mean of its images), repeated until stable, as `siteSymmetryOf` does for the point
164
162
  * groups; so the result is always a subgroup and the set point lies exactly on its elements. Throws a
165
163
  * RangeError if that does not settle (a tolerance far too large).
166
164
  */
167
- export function planeSiteSymmetry(key: string, point: readonly number[], options?: PointOptions): PlaneSiteSymmetry {
165
+ export function planeSiteSymmetry(
166
+ key: string,
167
+ point: readonly number[],
168
+ options?: ToleranceOptions,
169
+ ): PlaneSiteSymmetry {
168
170
  checkPoint(point);
169
171
  const tol = tolerance(options);
170
172
  const ops = plainOperations(key);
@@ -190,7 +192,7 @@ export function planeSiteSymmetry(key: string, point: readonly number[], options
190
192
  set = next;
191
193
  }
192
194
  throw new RangeError(
193
- `No common fixed point for the operations fixing ${JSON.stringify(point)} within ${tol} (tolerance too large?)`,
195
+ `No common fixed point for the operations fixing ${shown(point)} within ${tol} (tolerance too large?)`,
194
196
  );
195
197
  }
196
198
 
@@ -205,63 +207,185 @@ function parsed(expression: string): PlainOperation {
205
207
  return op;
206
208
  }
207
209
 
210
+ /** The distance of a point to one printed Wyckoff position, and the nearest point of the position. */
211
+ export interface PositionDistance {
212
+ /** The index of the position in the printed order (`entry.wyckoff`). */
213
+ readonly row: number;
214
+ /** The nearest point of the position's point set, in [0, 1). */
215
+ readonly point: number[];
216
+ /** The distance: per lattice coordinate without a basis, Euclidean with one. */
217
+ readonly distance: number;
218
+ }
219
+
220
+ /** The free directions of a printed coordinate expression (the columns of W that are not zero, independent ones). */
221
+ function freeDirections(op: PlainOperation): number[][] {
222
+ const cols = [0, 1].map((k) => [op.matrix[0][k], op.matrix[1][k]]).filter((v) => v[0] !== 0 || v[1] !== 0);
223
+ if (cols.length === 2 && Math.abs(cols[0][0] * cols[1][1] - cols[0][1] * cols[1][0]) < 1e-12) return [cols[0]];
224
+ return cols;
225
+ }
226
+
208
227
  /**
209
- * True if some parameters x, y make the printed coordinate expression (e.g. `x,2x`, `-x+1/2,y`) give
210
- * the point modulo the lattice within `tol`: least squares over the nearby lattice translations.
228
+ * The smallest distance of r to a multiple of v, and the multiple: in the metric G (Euclidean with
229
+ * a basis, by projection) or per coordinate (the largest of the two components; the minimum over t
230
+ * of max(|r₁ − t v₁|, |r₂ − t v₂|) lies where a component vanishes or the two are equal).
211
231
  */
212
- function onExpression(expression: string, p: readonly number[], tol: number): boolean {
213
- const { matrix, translation } = parsed(expression);
214
- const c = translation;
215
- const ex = [matrix[0][0], matrix[1][0]];
216
- const ey = [matrix[0][1], matrix[1][1]];
217
- for (let i = -2; i <= 2; i++) {
218
- for (let j = -2; j <= 2; j++) {
219
- const r = [p[0] - c[0] - i, p[1] - c[1] - j];
220
- // Solve min |a·ex + b·ey − r| over the columns that are not zero.
221
- const cols = [ex, ey].filter((v) => v[0] !== 0 || v[1] !== 0);
222
- let fit = [0, 0];
223
- if (cols.length === 1) {
224
- const v = cols[0];
225
- const t = (v[0] * r[0] + v[1] * r[1]) / (v[0] * v[0] + v[1] * v[1]);
226
- fit = [t * v[0], t * v[1]];
227
- } else if (cols.length === 2) {
228
- const det = ex[0] * ey[1] - ex[1] * ey[0];
229
- if (Math.abs(det) < 1e-12) {
230
- const v = ex;
231
- const t = (v[0] * r[0] + v[1] * r[1]) / (v[0] * v[0] + v[1] * v[1]);
232
- fit = [t * v[0], t * v[1]];
233
- } else {
234
- fit = r;
232
+ function nearestOnLine(r: readonly number[], v: readonly number[], g: Mat2 | undefined): { t: number; d: number } {
233
+ if (g) {
234
+ const gv = [g[0][0] * v[0] + g[0][1] * v[1], g[1][0] * v[0] + g[1][1] * v[1]];
235
+ const t = (r[0] * gv[0] + r[1] * gv[1]) / (v[0] * gv[0] + v[1] * gv[1]);
236
+ return { t, d: norm([r[0] - t * v[0], r[1] - t * v[1]], g) };
237
+ }
238
+ const candidates: number[] = [];
239
+ if (v[0] !== 0) candidates.push(r[0] / v[0]);
240
+ if (v[1] !== 0) candidates.push(r[1] / v[1]);
241
+ if (v[0] !== v[1]) candidates.push((r[0] - r[1]) / (v[0] - v[1]));
242
+ if (v[0] !== -v[1]) candidates.push((r[0] + r[1]) / (v[0] + v[1]));
243
+ let best = { t: 0, d: Infinity };
244
+ for (const t of candidates) {
245
+ const d = norm([r[0] - t * v[0], r[1] - t * v[1]], undefined);
246
+ if (d < best.d) best = { t, d };
247
+ }
248
+ return best;
249
+ }
250
+
251
+ type Mat2 = readonly (readonly number[])[];
252
+
253
+ /** The length of a vector of lattice coordinates: per coordinate (the larger component) or in the metric G. */
254
+ function norm(r: readonly number[], g: Mat2 | undefined): number {
255
+ if (!g) return Math.max(Math.abs(r[0]), Math.abs(r[1]));
256
+ return Math.sqrt(Math.max(0, r[0] * (g[0][0] * r[0] + g[0][1] * r[1]) + r[1] * (g[1][0] * r[0] + g[1][1] * r[1])));
257
+ }
258
+
259
+ /** The metric tensor G = BᵀB of a basis (columns a, b), checked: two rows of two finite numbers, not singular. */
260
+ export function planeMetric(basis: unknown): Mat2 {
261
+ const ok =
262
+ Array.isArray(basis) &&
263
+ basis.length === 2 &&
264
+ basis.every(
265
+ (row) => Array.isArray(row) && row.length === 2 && row.every((x) => typeof x === 'number' && Number.isFinite(x)),
266
+ );
267
+ const b = basis as Mat2;
268
+ // Singular if V/(ab) is below 10⁻⁹, computed with the columns scaled to unit length so that no
269
+ // product overflows or underflows (review of step 10a).
270
+ const na = ok ? Math.hypot(b[0][0], b[1][0]) : 0;
271
+ const nb = ok ? Math.hypot(b[0][1], b[1][1]) : 0;
272
+ const ratio = na > 0 && nb > 0 ? (b[0][0] / na) * (b[1][1] / nb) - (b[0][1] / nb) * (b[1][0] / na) : 0;
273
+ if (!ok || !(Math.abs(ratio) > 1e-9)) {
274
+ throw new RangeError(
275
+ `The basis of a plane lattice is a non-singular 2 × 2 matrix (columns a, b), not ${shown(basis)}`,
276
+ );
277
+ }
278
+ const col = (k: number) => [b[0][k], b[1][k]];
279
+ const dot = (u: number[], w: number[]) => u[0] * w[0] + u[1] * w[1];
280
+ return [
281
+ [dot(col(0), col(0)), dot(col(0), col(1))],
282
+ [dot(col(1), col(0)), dot(col(1), col(1))],
283
+ ];
284
+ }
285
+
286
+ /**
287
+ * The distance of a point (lattice basis) to every printed Wyckoff position of a plane group, in
288
+ * printed order, with the nearest point of each: the point set of a position is the union of its
289
+ * printed coordinate expressions with every centring and lattice translation, a point, a line or
290
+ * the plane. Without a metric the distance is the larger of the two coordinate differences, with
291
+ * one (G = BᵀB) Euclidean.
292
+ */
293
+ export function planeWyckoffDistances(key: string, given: readonly number[], g?: Mat2): PositionDistance[] {
294
+ checkPoint(given);
295
+ // Modulo the lattice first, so that large coordinates keep their digits (review of step 10a).
296
+ const point = given.map(mod1);
297
+ const entry = planeGroupEntry(key);
298
+ const centrings = [[0, 0], ...entry.centring.map((t) => parsed(t).translation)];
299
+ return entry.wyckoff.map(([, , , coordinates], row) => {
300
+ let best = { point: [...point], distance: Infinity };
301
+ for (const expression of coordinates) {
302
+ const op = parsed(expression);
303
+ const free = freeDirections(op);
304
+ for (const shift of centrings) {
305
+ const c = [op.translation[0] + shift[0], op.translation[1] + shift[1]];
306
+ if (free.length === 2) {
307
+ best = { point: [...point], distance: 0 };
308
+ continue;
309
+ }
310
+ const base = [Math.round(point[0] - c[0]), Math.round(point[1] - c[1])];
311
+ for (let i = -2; i <= 2; i++) {
312
+ for (let j = -2; j <= 2; j++) {
313
+ const origin = [c[0] + base[0] + i, c[1] + base[1] + j];
314
+ const r = [point[0] - origin[0], point[1] - origin[1]];
315
+ const { t, d } = free.length === 1 ? nearestOnLine(r, free[0], g) : { t: 0, d: norm(r, g) };
316
+ if (d < best.distance) {
317
+ const v = free.length === 1 ? free[0] : [0, 0];
318
+ best = { point: [origin[0] + t * v[0], origin[1] + t * v[1]], distance: d };
319
+ }
320
+ }
235
321
  }
236
322
  }
237
- if (Math.abs(fit[0] - r[0]) < tol && Math.abs(fit[1] - r[1]) < tol) return true;
238
323
  }
324
+ return { row, point: best.point.map(mod1), distance: best.distance };
325
+ });
326
+ }
327
+
328
+ /**
329
+ * The Wyckoff position a point lies on within the tolerance (#137 F1): the most special printed
330
+ * position (the smallest multiplicity) whose point set lies within the tolerance of the point,
331
+ * measured per coordinate modulo the lattice, the nearest among positions of equal multiplicity;
332
+ * the general position always holds the point. Returns the row and the nearest point on it.
333
+ */
334
+ export function planePosition(key: string, point: readonly number[], options?: ToleranceOptions): PositionDistance {
335
+ const tol = tolerance(options);
336
+ const entry = planeGroupEntry(key);
337
+ const within = planeWyckoffDistances(key, point).filter((d) => d.distance < tol);
338
+ within.sort((a, b) => entry.wyckoff[a.row][0] - entry.wyckoff[b.row][0] || a.distance - b.distance);
339
+ return within[0];
340
+ }
341
+
342
+ /**
343
+ * The site symmetry of a point under a plane group, as one notion with its Wyckoff position
344
+ * (#137 F1): the point is set onto the position it lies on within the tolerance (`planePosition`),
345
+ * and the site symmetry is that of the point there.
346
+ */
347
+ export function planeSiteOfPosition(
348
+ key: string,
349
+ point: readonly number[],
350
+ options?: ToleranceOptions,
351
+ ): PlaneSiteSymmetry {
352
+ const { row, point: onPosition } = planePosition(key, point, options);
353
+ const multiplicity = planeGroupEntry(key).wyckoff[row][0];
354
+ // The site symmetry follows from the position, not from a second tolerance (review of step 10a):
355
+ // the point lies on it, so its |G|/multiplicity operations are those whose images of the point
356
+ // lie nearest to it (all of them at rounding distance).
357
+ const ops = plainOperations(key);
358
+ const order = ops.length / multiplicity;
359
+ const offset = (op: PlainOperation) =>
360
+ Math.max(
361
+ ...image(op, onPosition).map((v, k) => {
362
+ const d = v - onPosition[k];
363
+ return Math.abs(d - Math.round(d));
364
+ }),
365
+ );
366
+ const indices = ops
367
+ .map((op, i) => ({ i, d: offset(op) }))
368
+ .sort((a, b) => a.d - b.d || a.i - b.i)
369
+ .slice(0, order)
370
+ .map((e) => e.i)
371
+ .sort((a, b) => a - b);
372
+ const { table } = groupData(key);
373
+ if (!indices.every((a) => indices.every((b) => indices.includes(table[a][b])))) {
374
+ throw new Error(`${key}: the operations nearest to fixing ${shown(onPosition)} on position ${row} form no group`);
239
375
  }
240
- return false;
376
+ return { operations: indices, fixing: fixingOperations(ops, indices, onPosition), point: onPosition, multiplicity };
241
377
  }
242
378
 
243
379
  /**
244
- * The Wyckoff position of a point under a plane group, as ITA prints it: the point is first set
245
- * onto the fixed space of its site symmetry (`planeSiteSymmetry`), then found among the printed
246
- * coordinates of the positions with its multiplicity, the centring included.
380
+ * The Wyckoff position of a point under a plane group, as ITA prints it: the position it lies on
381
+ * within the tolerance (`planePosition`).
247
382
  */
248
383
  export function planeWyckoffPositionOf(
249
384
  key: string,
250
385
  point: readonly number[],
251
- options?: PointOptions,
386
+ options?: ToleranceOptions,
252
387
  ): PlaneWyckoffPosition {
253
- const tol = tolerance(options);
254
- const site = planeSiteSymmetry(key, point, options);
255
- const entry = planeGroupEntry(key);
256
- const centrings = [[0, 0], ...entry.centring.map((t) => parsed(t).translation)];
257
- for (const [multiplicity, letter, siteSymmetry, coordinates] of entry.wyckoff) {
258
- if (multiplicity !== site.multiplicity) continue;
259
- for (const t of centrings) {
260
- const shifted = [site.point[0] - t[0], site.point[1] - t[1]];
261
- if (coordinates.some((expression) => onExpression(expression, shifted, tol))) {
262
- return { letter, multiplicity, siteSymmetry };
263
- }
264
- }
265
- }
266
- throw new RangeError(`No Wyckoff position of ${entry.symbol} holds the point ${JSON.stringify(point)}`);
388
+ const { row } = planePosition(key, point, options);
389
+ const [multiplicity, letter, siteSymmetry] = planeGroupEntry(key).wyckoff[row];
390
+ return { letter, multiplicity, siteSymmetry };
267
391
  }
@@ -5,6 +5,7 @@
5
5
  * name up here (docs/conventions.md, "Named tensor properties").
6
6
  */
7
7
 
8
+ import { shown } from '../checks.js';
8
9
  import type { TensorSpec } from './tensorForm.js';
9
10
 
10
11
  /** A named property: the tensor that describes it, what it is and where it is defined. */
@@ -29,45 +30,51 @@ const property = (spec: TensorSpec, description: string, source: string): Tensor
29
30
  export const TENSOR_PROPERTIES = Object.freeze({
30
31
  /** Spontaneous polarization: a time-even polar vector. */
31
32
  pyroelectric: property(
32
- { rank: 1, parity: 'polar', timeParity: 'i', intrinsic: 'none' },
33
+ { rank: 1, kind: 'polar', timeParity: 'even', intrinsic: 'none' },
33
34
  'Spontaneous electric polarization, which changes with temperature: a polar vector p_i.',
34
35
  'ITC Vol. D (2013), § 1.1.4.4.2, p. 12; classes: ITA (2016) Table 3.2.2.1, p. 738',
35
36
  ),
36
- /** Ferroaxial order: a time-even axial vector. */
37
+ /** Ferroaxial order: a time-even axial vector (not a term of ITC). */
37
38
  ferroaxial: property(
38
- { rank: 1, parity: 'axial', timeParity: 'i', intrinsic: 'none' },
39
- 'Ferroaxial (ferro-rotational) order, e.g. a net rotation of structural units about an axis: an axial vector A_i.',
40
- 'Hlinka, Privratska, Ondrejkovic & Janovec, Phys. Rev. Lett. 116, 177602 (2016); classes: Birss (1966), Tables 4a and 4c, pp. 56–58',
39
+ { rank: 1, kind: 'axial', timeParity: 'even', intrinsic: 'none' },
40
+ 'Ferroaxial (ferro-rotational) order, e.g. a net rotation of structural units about an axis: an axial vector A_i, the axial toroidal moment (Jahn symbol eV).',
41
+ 'Not in ITC: Hlinka, Privratska, Ondrejkovic & Janovec, Phys. Rev. Lett. 116, 177602 (2016); Gallego et al., Acta Cryst. A75, 438 (2019), p. 440 (axial toroidal moment, type eV); classes: Birss (1966), Tables 4a and 4c, pp. 56–58',
41
42
  ),
42
43
  /** The piezoelectric effect (and second-harmonic generation, which has the same tensor): polar, rank 3, symmetric in j, k. */
43
44
  piezoelectric: property(
44
- { rank: 3, parity: 'polar', timeParity: 'i', intrinsic: 'jk' },
45
+ { rank: 3, kind: 'polar', timeParity: 'even', intrinsic: 'symmetric-last-pair' },
45
46
  'Electric polarization linear in stress, P_i = d_ijk T_jk: a polar tensor of rank 3, symmetric in j, k.',
46
- 'ITC Vol. D (2013), § 1.1.4.4.3, p. 12; classes: ITA (2016) Table 3.2.2.1, p. 738',
47
+ 'ITC Vol. D (2013), § 1.1.4.4.3, p. 12, and § 1.1.4.10.1, eqs. (1.1.4.5), (1.1.4.7), p. 24; classes: ITA (2016) Table 3.2.2.1, p. 738',
47
48
  ),
48
49
  /** Optical activity: the symmetric axial tensor of rank 2. */
49
50
  gyrotropic: property(
50
- { rank: 2, parity: 'axial', timeParity: 'i', intrinsic: 'ij' },
51
+ { rank: 2, kind: 'axial', timeParity: 'even', intrinsic: 'symmetric-first-pair' },
51
52
  'Optical activity (optical rotation), G = g_ij α_i α_j: the symmetric part of the axial gyration tensor of rank 2.',
52
53
  'ITC Vol. D (2013), § 1.1.4.10.7.2, p. 30; classes: ITA (2016) Table 3.2.2.1, p. 738',
53
54
  ),
54
55
  /** Spontaneous magnetization: a time-odd axial vector (magnetic API only). */
55
56
  ferromagnetic: property(
56
- { rank: 1, parity: 'axial', timeParity: 'c', intrinsic: 'none' },
57
+ { rank: 1, kind: 'axial', timeParity: 'odd', intrinsic: 'none' },
57
58
  'Spontaneous magnetization M_s (ferromagnets, ferrimagnets, weak ferromagnets): an axial vector that changes sign under time reversal.',
58
59
  'ITC Vol. D (2013), § 1.5.2.1 with Table 1.5.2.4, p. 113',
59
60
  ),
61
+ /** A spontaneous toroidal moment: a time-odd polar vector (magnetic API only). */
62
+ ferrotoroidic: property(
63
+ { rank: 1, kind: 'polar', timeParity: 'odd', intrinsic: 'none' },
64
+ 'Spontaneous toroidal moment T_i (ferrotoroidic order): a polar vector that changes sign under time reversal (Jahn symbol aV); allowed exactly where the magnetoelectric tensor need not be symmetric.',
65
+ 'ITC Vol. D (2013), § 1.5.4.3, p. 128 (the term), and § 1.5.8.1, footnote 3, p. 140 (the 31 groups of Table 1.5.8.1 whose magnetoelectric tensor need not be symmetric); Gallego et al., Acta Cryst. A75, 438 (2019), p. 440 (polar toroidal moment, type aV)',
66
+ ),
60
67
  /** The linear magnetoelectric effect: a time-odd axial tensor of rank 2 (magnetic API only). */
61
68
  magnetoelectric: property(
62
- { rank: 2, parity: 'axial', timeParity: 'c', intrinsic: 'none' },
69
+ { rank: 2, kind: 'axial', timeParity: 'odd', intrinsic: 'none' },
63
70
  'Linear magnetoelectric effect, P_i = α_ij H_j: an axial tensor of rank 2 that changes sign under time reversal.',
64
- 'ITC Vol. D (2013), § 1.5.8.1 with eqs. (1.5.8.2), (1.5.8.3) and Table 1.5.8.1, pp. 139–140',
71
+ 'ITC Vol. D (2013), § 1.5.8, eqs. (1.5.8.2), (1.5.8.3), p. 139, and § 1.5.8.1 with Table 1.5.8.1, pp. 139–140',
65
72
  ),
66
73
  /** The piezomagnetic effect: a time-odd axial tensor of rank 3, symmetric in j, k (magnetic API only). */
67
74
  piezomagnetic: property(
68
- { rank: 3, parity: 'axial', timeParity: 'c', intrinsic: 'jk' },
75
+ { rank: 3, kind: 'axial', timeParity: 'odd', intrinsic: 'symmetric-last-pair' },
69
76
  'Magnetization linear in stress, M_i = Λ_ijk T_jk: an axial tensor of rank 3, symmetric in j, k, that changes sign under time reversal.',
70
- 'ITC Vol. D (2013), § 1.5.7.1 with eq. (1.5.7.2) and Table 1.5.7.1, pp. 134–135',
77
+ 'ITC Vol. D (2013), § 1.5.7, eq. (1.5.7.2), and § 1.5.7.1 with Table 1.5.7.1, pp. 134–135',
71
78
  ),
72
79
  } satisfies Record<string, TensorProperty>);
73
80
 
@@ -79,11 +86,16 @@ export const TENSOR_PROPERTY_NAMES = Object.freeze(Object.keys(TENSOR_PROPERTIES
79
86
 
80
87
  /** The specification behind a property name; a specification is returned as it is. */
81
88
  export function specOf(specOrName: TensorSpec | TensorPropertyName): TensorSpec {
82
- if (typeof specOrName !== 'string') return specOrName;
89
+ if (typeof specOrName !== 'string') {
90
+ if (typeof specOrName !== 'object' || specOrName === null || Array.isArray(specOrName)) {
91
+ throw new RangeError(
92
+ `A tensor is given by its specification { rank, kind, timeParity, intrinsic } or the name of a property, not ${shown(specOrName)}`,
93
+ );
94
+ }
95
+ return specOrName;
96
+ }
83
97
  if (!Object.hasOwn(TENSOR_PROPERTIES, specOrName)) {
84
- throw new RangeError(
85
- `Unknown tensor property: ${JSON.stringify(specOrName)} (known: ${TENSOR_PROPERTY_NAMES.join(', ')})`,
86
- );
98
+ throw new RangeError(`Unknown tensor property: ${shown(specOrName)} (known: ${TENSOR_PROPERTY_NAMES.join(', ')})`);
87
99
  }
88
100
  return TENSOR_PROPERTIES[specOrName].spec;
89
101
  }
@@ -10,18 +10,26 @@ export const PIVOT_EPSILON = 1e-7;
10
10
  export const ELIMINATION_EPSILON = 1e-12;
11
11
 
12
12
  /**
13
- * The reduced row echelon form of the span of `vectors` (all of length `dim`): one row per
14
- * independent direction, each with a 1 at its pivot and 0 at every other row's pivot, pivots
15
- * ascending. The rows are unique for a given span, so the pivots name the free parameters
16
- * unambiguously: each is the lowest-index component that is independent of the ones before it.
13
+ * The reduced row echelon form of the span of `vectors` (all of length `dim`), with its pivot
14
+ * columns: one row per independent direction, each with a 1 at its pivot and 0 at every other
15
+ * row's pivot, pivots ascending. The rows are unique for a given span, so the pivots name the free
16
+ * parameters unambiguously: each is the lowest-index component that is independent of the ones
17
+ * before it. For operations with float noise a row can keep entries too small to be a pivot
18
+ * (below `PIVOT_EPSILON`, or `pivotThreshold`) before its pivot, so the pivots are returned rather
19
+ * than read off the rows (#88); entries below `ELIMINATION_EPSILON` are set to zero.
17
20
  */
18
- export function reducedRowEchelon(vectors: readonly (readonly number[])[], dim: number): number[][] {
21
+ export function rowEchelonWithPivots(
22
+ vectors: readonly (readonly number[])[],
23
+ dim: number,
24
+ pivotThreshold = PIVOT_EPSILON,
25
+ ): { rows: number[][]; pivots: number[] } {
19
26
  const m = vectors.map((v) => Array.from(v));
27
+ const pivots: number[] = [];
20
28
  let row = 0;
21
29
  for (let c = 0; c < dim && row < m.length; c++) {
22
30
  let pivot = row;
23
31
  for (let r = row + 1; r < m.length; r++) if (Math.abs(m[r][c]) > Math.abs(m[pivot][c])) pivot = r;
24
- if (Math.abs(m[pivot][c]) < PIVOT_EPSILON) continue;
32
+ if (Math.abs(m[pivot][c]) < pivotThreshold) continue;
25
33
  [m[row], m[pivot]] = [m[pivot], m[row]];
26
34
  const p = m[row][c];
27
35
  for (let j = 0; j < dim; j++) m[row][j] /= p;
@@ -30,11 +38,22 @@ export function reducedRowEchelon(vectors: readonly (readonly number[])[], dim:
30
38
  const f = m[r][c];
31
39
  for (let j = 0; j < dim; j++) m[r][j] -= f * m[row][j];
32
40
  }
41
+ pivots.push(c);
33
42
  row++;
34
43
  }
35
- const out = m.slice(0, row);
36
- for (const r of out) for (let j = 0; j < dim; j++) if (Math.abs(r[j]) < ELIMINATION_EPSILON) r[j] = 0;
37
- return out;
44
+ const rows = m.slice(0, row);
45
+ rows.forEach((r, k) => {
46
+ for (let j = 0; j < dim; j++) {
47
+ if (j === pivots[k]) r[j] = 1;
48
+ else if (pivots.includes(j) || Math.abs(r[j]) < ELIMINATION_EPSILON) r[j] = 0;
49
+ }
50
+ });
51
+ return { rows, pivots };
52
+ }
53
+
54
+ /** The reduced row echelon form of the span of `vectors`, as `rowEchelonWithPivots` gives it, without the pivots. */
55
+ export function reducedRowEchelon(vectors: readonly (readonly number[])[], dim: number): number[][] {
56
+ return rowEchelonWithPivots(vectors, dim).rows;
38
57
  }
39
58
 
40
59
  /** The number of independent vectors among `vectors` (all of length `dim`). */