symcrys 0.9.0 → 0.10.1

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 (56) hide show
  1. package/dist/crystalClasses.d.ts +1 -1
  2. package/dist/crystalClasses.d.ts.map +1 -1
  3. package/dist/crystalClasses.js +2 -3
  4. package/dist/crystalClasses.js.map +1 -1
  5. package/dist/operations.d.ts.map +1 -1
  6. package/dist/operations.js +1 -1
  7. package/dist/operations.js.map +1 -1
  8. package/dist/plane.d.ts +37 -0
  9. package/dist/plane.d.ts.map +1 -1
  10. package/dist/plane.js +94 -13
  11. package/dist/plane.js.map +1 -1
  12. package/dist/siteSymmetry.d.ts +1 -1
  13. package/dist/siteSymmetry.d.ts.map +1 -1
  14. package/dist/siteSymmetry.js +35 -30
  15. package/dist/siteSymmetry.js.map +1 -1
  16. package/dist/spaceGroups/cell.d.ts +2 -0
  17. package/dist/spaceGroups/cell.d.ts.map +1 -1
  18. package/dist/spaceGroups/cell.js +10 -2
  19. package/dist/spaceGroups/cell.js.map +1 -1
  20. package/dist/spaceGroups/describe.d.ts +7 -0
  21. package/dist/spaceGroups/describe.d.ts.map +1 -1
  22. package/dist/spaceGroups/describe.js +10 -1
  23. package/dist/spaceGroups/describe.js.map +1 -1
  24. package/dist/spaceGroups/positions.d.ts.map +1 -1
  25. package/dist/spaceGroups/positions.js +39 -22
  26. package/dist/spaceGroups/positions.js.map +1 -1
  27. package/dist/spaceGroups/snap.d.ts +20 -0
  28. package/dist/spaceGroups/snap.d.ts.map +1 -0
  29. package/dist/spaceGroups/snap.js +103 -0
  30. package/dist/spaceGroups/snap.js.map +1 -0
  31. package/dist/tensors/propertyTable.js +3 -3
  32. package/dist/tensors/propertyTable.js.map +1 -1
  33. package/dist/tensors/rowEchelon.d.ts +12 -4
  34. package/dist/tensors/rowEchelon.d.ts.map +1 -1
  35. package/dist/tensors/rowEchelon.js +23 -10
  36. package/dist/tensors/rowEchelon.js.map +1 -1
  37. package/dist/tensors/tensorForm.d.ts.map +1 -1
  38. package/dist/tensors/tensorForm.js +4 -3
  39. package/dist/tensors/tensorForm.js.map +1 -1
  40. package/dist/version.d.ts +1 -1
  41. package/dist/version.d.ts.map +1 -1
  42. package/dist/version.js +1 -1
  43. package/dist/version.js.map +1 -1
  44. package/package.json +1 -1
  45. package/src/crystalClasses.ts +2 -3
  46. package/src/operations.ts +3 -1
  47. package/src/plane.ts +122 -15
  48. package/src/siteSymmetry.ts +35 -31
  49. package/src/spaceGroups/cell.ts +13 -2
  50. package/src/spaceGroups/describe.ts +17 -1
  51. package/src/spaceGroups/positions.ts +52 -22
  52. package/src/spaceGroups/snap.ts +121 -0
  53. package/src/tensors/propertyTable.ts +3 -3
  54. package/src/tensors/rowEchelon.ts +26 -8
  55. package/src/tensors/tensorForm.ts +4 -3
  56. package/src/version.ts +1 -1
@@ -81,17 +81,38 @@ function sameModLattice(p: readonly number[], q: readonly number[], tol: number)
81
81
  });
82
82
  }
83
83
 
84
- const plainCache = new Map<string, PlainOperation[]>();
85
- function plainOperations(key: string): PlainOperation[] {
84
+ /**
85
+ * Per plane group, computed once: its operations as plain numbers and its group table modulo the
86
+ * lattice (`table[a][b]` is the index of the operation that equals a∘b modulo the lattice), so that
87
+ * closing a set of operations works on indices (#86).
88
+ */
89
+ interface GroupData {
90
+ readonly ops: readonly PlainOperation[];
91
+ readonly table: readonly (readonly number[])[];
92
+ }
93
+
94
+ const groupCache = new Map<string, GroupData>();
95
+ function groupData(key: string): GroupData {
86
96
  const k = planeGroupEntry(key).symbol;
87
- let ops = plainCache.get(k);
88
- if (!ops) {
89
- ops = planeGroupOperations(k).map(toPlain);
90
- plainCache.set(k, ops);
97
+ let data = groupCache.get(k);
98
+ if (!data) {
99
+ const exact = planeGroupOperations(k);
100
+ const table = exact.map((a) =>
101
+ exact.map((b) => {
102
+ const product = compose(a, b);
103
+ const i = exact.findIndex((op) => equalModLattice(op, product));
104
+ if (i < 0) throw new Error(`The operations of ${k} are not closed modulo the lattice`);
105
+ return i;
106
+ }),
107
+ );
108
+ data = { ops: exact.map(toPlain), table };
109
+ groupCache.set(k, data);
91
110
  }
92
- return ops;
111
+ return data;
93
112
  }
94
113
 
114
+ const plainOperations = (key: string): readonly PlainOperation[] => groupData(key).ops;
115
+
95
116
  /**
96
117
  * The orbit of a point under a plane group: its distinct images in the conventional cell, each in
97
118
  * [0, 1), in the order of the operations that first produce them. Coinciding images (within the
@@ -110,23 +131,21 @@ export function planeOrbit(key: string, point: readonly number[], options?: Poin
110
131
 
111
132
  /** Indices of `set` closed under products, modulo the lattice, within the operations of the group. */
112
133
  function closeWithin(key: string, set: readonly number[]): number[] {
113
- const exact = planeGroupOperations(planeGroupEntry(key).symbol);
114
- const out = new Set(set);
115
- let grew = true;
116
- while (grew) {
117
- grew = false;
118
- for (const a of [...out]) {
119
- for (const b of [...out]) {
120
- const product = compose(exact[a], exact[b]);
121
- const k = exact.findIndex((op) => equalModLattice(op, product));
122
- if (!out.has(k)) {
123
- out.add(k);
124
- grew = true;
134
+ const { table } = groupData(key);
135
+ const out = [...new Set(set)];
136
+ const member = new Set(out);
137
+ // Every product of two members is a member once each new member has been combined with all others.
138
+ for (let i = 0; i < out.length; i++) {
139
+ for (let j = 0; j <= i; j++) {
140
+ for (const k of [table[out[i]][out[j]], table[out[j]][out[i]]]) {
141
+ if (!member.has(k)) {
142
+ member.add(k);
143
+ out.push(k);
125
144
  }
126
145
  }
127
146
  }
128
147
  }
129
- return [...out].sort((a, b) => a - b);
148
+ return out.sort((a, b) => a - b);
130
149
  }
131
150
 
132
151
  /** The operations of `indices`, each shifted by the lattice translation that brings its image of `p` nearest to `p`. */
@@ -175,12 +194,23 @@ export function planeSiteSymmetry(key: string, point: readonly number[], options
175
194
  );
176
195
  }
177
196
 
197
+ /** A printed coordinate expression, parsed once. */
198
+ const expressionCache = new Map<string, PlainOperation>();
199
+ function parsed(expression: string): PlainOperation {
200
+ let op = expressionCache.get(expression);
201
+ if (!op) {
202
+ op = toPlain(parseOperation(expression));
203
+ expressionCache.set(expression, op);
204
+ }
205
+ return op;
206
+ }
207
+
178
208
  /**
179
209
  * True if some parameters x, y make the printed coordinate expression (e.g. `x,2x`, `-x+1/2,y`) give
180
210
  * the point modulo the lattice within `tol`: least squares over the nearby lattice translations.
181
211
  */
182
212
  function onExpression(expression: string, p: readonly number[], tol: number): boolean {
183
- const { matrix, translation } = toPlain(parseOperation(expression));
213
+ const { matrix, translation } = parsed(expression);
184
214
  const c = translation;
185
215
  const ex = [matrix[0][0], matrix[1][0]];
186
216
  const ey = [matrix[0][1], matrix[1][1]];
@@ -223,7 +253,7 @@ export function planeWyckoffPositionOf(
223
253
  const tol = tolerance(options);
224
254
  const site = planeSiteSymmetry(key, point, options);
225
255
  const entry = planeGroupEntry(key);
226
- const centrings = [[0, 0], ...entry.centring.map((t) => toPlain(parseOperation(t)).translation)];
256
+ const centrings = [[0, 0], ...entry.centring.map((t) => parsed(t).translation)];
227
257
  for (const [multiplicity, letter, siteSymmetry, coordinates] of entry.wyckoff) {
228
258
  if (multiplicity !== site.multiplicity) continue;
229
259
  for (const t of centrings) {
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Snapping a point of the plane onto the nearest Wyckoff position of a plane group, by the distance
3
+ * of the point to the position (#85; CLAUDE.md, "Snapping onto a Wyckoff position"). Unlike the site
4
+ * symmetry (`positions.ts`), which asks whether the images of a point lie within a tolerance of it,
5
+ * this asks whether the point lies within the tolerance of the point set of a position. Internal.
6
+ */
7
+
8
+ import { parseOperation, toPlain, type PlainOperation } from './affine.js';
9
+ import { planeGroupEntry } from './planeGroups.js';
10
+ import { EPSILON } from '../tolerances.js';
11
+
12
+ /** The point set of one printed coordinate expression, x ↦ M·(x, y) + c, with the columns of M that are not zero. */
13
+ interface Expression {
14
+ readonly matrix: readonly (readonly number[])[];
15
+ readonly translation: readonly number[];
16
+ /** The number of free parameters: 0 for a point, 1 for a line, 2 for the general position. */
17
+ readonly freedom: number;
18
+ }
19
+
20
+ const cache = new Map<string, readonly { row: number; expressions: Expression[] }[]>();
21
+
22
+ function expression(op: PlainOperation, shift: readonly number[]): Expression {
23
+ const m = op.matrix;
24
+ const columns = [0, 1].filter((k) => m[0][k] !== 0 || m[1][k] !== 0);
25
+ const det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
26
+ return {
27
+ matrix: m,
28
+ translation: [op.translation[0] + shift[0], op.translation[1] + shift[1]],
29
+ freedom: columns.length === 2 && det === 0 ? 1 : columns.length,
30
+ };
31
+ }
32
+
33
+ /** Per printed Wyckoff row (index into `wyckoff`), its coordinate expressions with every centring. */
34
+ function positionsOf(symbol: string) {
35
+ let rows = cache.get(symbol);
36
+ if (!rows) {
37
+ const entry = planeGroupEntry(symbol);
38
+ const shifts = [[0, 0], ...entry.centring.map((t) => toPlain(parseOperation(t)).translation)];
39
+ rows = entry.wyckoff.map(([, , , coordinates], row) => ({
40
+ row,
41
+ expressions: coordinates.flatMap((c) => shifts.map((s) => expression(toPlain(parseOperation(c)), s))),
42
+ }));
43
+ cache.set(symbol, rows);
44
+ }
45
+ return rows;
46
+ }
47
+
48
+ /**
49
+ * The nearest point of the set of `e` to `p` modulo the lattice: the parameters by least squares in
50
+ * the lattice coordinates, over the lattice translations that can reach it. Returns the point and
51
+ * its distance, the larger of the two coordinate differences.
52
+ */
53
+ function nearest(e: Expression, p: readonly number[]): { point: number[]; distance: number } {
54
+ if (e.freedom === 2) return { point: [...p], distance: 0 };
55
+ const m = e.matrix;
56
+ const c = e.translation;
57
+ let best = { point: [0, 0], distance: Infinity };
58
+ // c lies in [0, 2) (a printed coordinate in [0, 1) plus a centring), p in [0, 1).
59
+ for (let i = -2; i <= 2; i++) {
60
+ for (let j = -2; j <= 2; j++) {
61
+ const v = [p[0] - c[0] - i, p[1] - c[1] - j];
62
+ let r: number[];
63
+ if (e.freedom === 0) {
64
+ r = [0, 0];
65
+ } else {
66
+ const k = m[0][0] !== 0 || m[1][0] !== 0 ? 0 : 1;
67
+ const col = [m[0][k], m[1][k]];
68
+ const t = (col[0] * v[0] + col[1] * v[1]) / (col[0] * col[0] + col[1] * col[1]);
69
+ r = [t * col[0], t * col[1]];
70
+ }
71
+ const distance = Math.max(Math.abs(r[0] - v[0]), Math.abs(r[1] - v[1]));
72
+ if (distance < best.distance) best = { point: [r[0] + c[0] + i, r[1] + c[1] + j], distance };
73
+ }
74
+ }
75
+ return best;
76
+ }
77
+
78
+ const mod1 = (v: number): number => {
79
+ const r = v - Math.floor(v);
80
+ return r >= 1 ? 0 : r + 0;
81
+ };
82
+
83
+ /** A point snapped onto a Wyckoff position: the index of the printed row and the point on it, in [0, 1). */
84
+ export interface SnappedPoint {
85
+ readonly row: number;
86
+ readonly point: readonly number[];
87
+ }
88
+
89
+ /**
90
+ * Snaps a point (lattice basis) onto the most special Wyckoff position of a plane group that lies
91
+ * within `tolerance` of it (per coordinate, modulo the lattice): positions without a free parameter
92
+ * first, then lines, then the general position, which always holds the point. Among positions with
93
+ * the same number of free parameters the nearest wins. Throws a RangeError for a point that is not
94
+ * two finite numbers or a tolerance outside (0, 1/2).
95
+ */
96
+ export function planeSnapToWyckoffPosition(
97
+ key: string,
98
+ point: readonly number[],
99
+ tolerance: number = EPSILON,
100
+ ): SnappedPoint {
101
+ if (point.length !== 2 || !point.every(Number.isFinite)) {
102
+ throw new RangeError(`A point of the plane has two finite coordinates, not ${JSON.stringify(point)}`);
103
+ }
104
+ if (!(typeof tolerance === 'number' && tolerance > 0 && tolerance < 0.5)) {
105
+ throw new RangeError(`The tolerance must lie between 0 and 1/2, not ${String(tolerance)}`);
106
+ }
107
+ const p = point.map(mod1);
108
+ const rows = positionsOf(planeGroupEntry(key).symbol);
109
+ for (const freedom of [0, 1, 2]) {
110
+ let best: { row: number; point: number[]; distance: number } | undefined;
111
+ for (const { row, expressions } of rows) {
112
+ for (const e of expressions) {
113
+ if (e.freedom !== freedom) continue;
114
+ const n = nearest(e, p);
115
+ if (n.distance < tolerance && (!best || n.distance < best.distance)) best = { row, ...n };
116
+ }
117
+ }
118
+ if (best) return { row: best.row, point: best.point.map(mod1) };
119
+ }
120
+ throw new Error(`The general position of ${key} does not hold ${JSON.stringify(point)}`);
121
+ }
@@ -43,7 +43,7 @@ export const TENSOR_PROPERTIES = Object.freeze({
43
43
  piezoelectric: property(
44
44
  { rank: 3, parity: 'polar', timeParity: 'i', intrinsic: 'jk' },
45
45
  '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',
46
+ '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
47
  ),
48
48
  /** Optical activity: the symmetric axial tensor of rank 2. */
49
49
  gyrotropic: property(
@@ -61,13 +61,13 @@ export const TENSOR_PROPERTIES = Object.freeze({
61
61
  magnetoelectric: property(
62
62
  { rank: 2, parity: 'axial', timeParity: 'c', intrinsic: 'none' },
63
63
  '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',
64
+ '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
65
  ),
66
66
  /** The piezomagnetic effect: a time-odd axial tensor of rank 3, symmetric in j, k (magnetic API only). */
67
67
  piezomagnetic: property(
68
68
  { rank: 3, parity: 'axial', timeParity: 'c', intrinsic: 'jk' },
69
69
  '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',
70
+ '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
71
  ),
72
72
  } satisfies Record<string, TensorProperty>);
73
73
 
@@ -10,13 +10,20 @@ 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`) before its pivot, so the pivots are returned rather than read off the
19
+ * 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
+ ): { rows: number[][]; pivots: number[] } {
19
25
  const m = vectors.map((v) => Array.from(v));
26
+ const pivots: number[] = [];
20
27
  let row = 0;
21
28
  for (let c = 0; c < dim && row < m.length; c++) {
22
29
  let pivot = row;
@@ -30,11 +37,22 @@ export function reducedRowEchelon(vectors: readonly (readonly number[])[], dim:
30
37
  const f = m[r][c];
31
38
  for (let j = 0; j < dim; j++) m[r][j] -= f * m[row][j];
32
39
  }
40
+ pivots.push(c);
33
41
  row++;
34
42
  }
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;
43
+ const rows = m.slice(0, row);
44
+ rows.forEach((r, k) => {
45
+ for (let j = 0; j < dim; j++) {
46
+ if (j === pivots[k]) r[j] = 1;
47
+ else if (pivots.includes(j) || Math.abs(r[j]) < ELIMINATION_EPSILON) r[j] = 0;
48
+ }
49
+ });
50
+ return { rows, pivots };
51
+ }
52
+
53
+ /** The reduced row echelon form of the span of `vectors`, as `rowEchelonWithPivots` gives it, without the pivots. */
54
+ export function reducedRowEchelon(vectors: readonly (readonly number[])[], dim: number): number[][] {
55
+ return rowEchelonWithPivots(vectors, dim).rows;
38
56
  }
39
57
 
40
58
  /** The number of independent vectors among `vectors` (all of length `dim`). */
@@ -15,7 +15,7 @@ import { checkOrthogonal, checkTimeReversal, closeGroup, type Operation } from '
15
15
  import { operations, settings, type MagneticPointGroupKey, type SettingOptions } from '../pointGroups/pointGroups.js';
16
16
  import { specOf, type TensorPropertyName } from './propertyTable.js';
17
17
  import { EPSILON, SNAP_EPSILON } from '../tolerances.js';
18
- import { reducedRowEchelon } from './rowEchelon.js';
18
+ import { rowEchelonWithPivots } from './rowEchelon.js';
19
19
 
20
20
  /** Rank of a tensor: 0 (scalar) to 4. */
21
21
  export type TensorRank = 0 | 1 | 2 | 3 | 4;
@@ -180,8 +180,9 @@ function project(group: readonly Operation[], spec: TensorSpec, perms: number[][
180
180
  if (averaged.some((x) => Math.abs(x) >= EPSILON)) spanning.push(averaged);
181
181
  }
182
182
 
183
- const basis = reducedRowEchelon(spanning, dim).map((row) => row.map(snapEntry));
184
- const pivots = basis.map((row) => row.findIndex((x) => x !== 0));
183
+ const reduced = rowEchelonWithPivots(spanning, dim);
184
+ const basis = reduced.rows.map((row) => row.map(snapEntry));
185
+ const { pivots } = reduced;
185
186
  const relations: number[][] = [];
186
187
  for (let c = 0; c < dim; c++) {
187
188
  if (pivots.includes(c)) continue;
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  *
4
4
  * @remarks Kept equal to `version` in package.json (checked by version.test.ts).
5
5
  */
6
- export const VERSION = '0.9.0';
6
+ export const VERSION = '0.10.1';