symcrys 0.5.0 → 0.6.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 (86) hide show
  1. package/README.md +15 -3
  2. package/dist/crystalClasses.d.ts +222 -0
  3. package/dist/crystalClasses.d.ts.map +1 -0
  4. package/dist/crystalClasses.js +295 -0
  5. package/dist/crystalClasses.js.map +1 -0
  6. package/dist/index.d.ts +11 -9
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +6 -8
  9. package/dist/index.js.map +1 -1
  10. package/dist/lattice.d.ts +5 -1
  11. package/dist/lattice.d.ts.map +1 -1
  12. package/dist/lattice.js +3 -1
  13. package/dist/lattice.js.map +1 -1
  14. package/dist/magnetic.d.ts +36 -0
  15. package/dist/magnetic.d.ts.map +1 -0
  16. package/dist/magnetic.js +22 -0
  17. package/dist/magnetic.js.map +1 -0
  18. package/dist/operations.d.ts +10 -0
  19. package/dist/operations.d.ts.map +1 -1
  20. package/dist/operations.js +25 -0
  21. package/dist/operations.js.map +1 -1
  22. package/dist/pointGroups/data.d.ts +36 -1
  23. package/dist/pointGroups/data.d.ts.map +1 -1
  24. package/dist/pointGroups/data.js +29 -21
  25. package/dist/pointGroups/data.js.map +1 -1
  26. package/dist/pointGroups/elements.d.ts +16 -6
  27. package/dist/pointGroups/elements.d.ts.map +1 -1
  28. package/dist/pointGroups/elements.js +7 -2
  29. package/dist/pointGroups/elements.js.map +1 -1
  30. package/dist/pointGroups/magneticData.d.ts +66 -0
  31. package/dist/pointGroups/magneticData.d.ts.map +1 -0
  32. package/dist/pointGroups/magneticData.js +184 -0
  33. package/dist/pointGroups/magneticData.js.map +1 -0
  34. package/dist/pointGroups/magneticSymbols.d.ts +22 -0
  35. package/dist/pointGroups/magneticSymbols.d.ts.map +1 -0
  36. package/dist/pointGroups/magneticSymbols.js +103 -0
  37. package/dist/pointGroups/magneticSymbols.js.map +1 -0
  38. package/dist/pointGroups/pointGroups.d.ts +102 -24
  39. package/dist/pointGroups/pointGroups.d.ts.map +1 -1
  40. package/dist/pointGroups/pointGroups.js +293 -47
  41. package/dist/pointGroups/pointGroups.js.map +1 -1
  42. package/dist/pointGroups/properties.d.ts +25 -16
  43. package/dist/pointGroups/properties.d.ts.map +1 -1
  44. package/dist/pointGroups/properties.js +46 -56
  45. package/dist/pointGroups/properties.js.map +1 -1
  46. package/dist/pointGroups/wyckoff.d.ts +4 -1
  47. package/dist/pointGroups/wyckoff.d.ts.map +1 -1
  48. package/dist/pointGroups/wyckoff.js +6 -2
  49. package/dist/pointGroups/wyckoff.js.map +1 -1
  50. package/dist/siteSymmetry.d.ts +2 -1
  51. package/dist/siteSymmetry.d.ts.map +1 -1
  52. package/dist/siteSymmetry.js.map +1 -1
  53. package/dist/tensors/propertyTable.d.ts +45 -0
  54. package/dist/tensors/propertyTable.d.ts.map +1 -0
  55. package/dist/tensors/propertyTable.js +41 -0
  56. package/dist/tensors/propertyTable.js.map +1 -0
  57. package/dist/tensors/tensorForm.d.ts +21 -10
  58. package/dist/tensors/tensorForm.d.ts.map +1 -1
  59. package/dist/tensors/tensorForm.js +23 -24
  60. package/dist/tensors/tensorForm.js.map +1 -1
  61. package/dist/tensors/tensorProperties.d.ts +30 -0
  62. package/dist/tensors/tensorProperties.d.ts.map +1 -0
  63. package/dist/tensors/tensorProperties.js +44 -0
  64. package/dist/tensors/tensorProperties.js.map +1 -0
  65. package/dist/version.d.ts +6 -2
  66. package/dist/version.d.ts.map +1 -1
  67. package/dist/version.js +6 -2
  68. package/dist/version.js.map +1 -1
  69. package/package.json +6 -2
  70. package/src/crystalClasses.ts +451 -0
  71. package/src/index.ts +32 -38
  72. package/src/lattice.ts +5 -1
  73. package/src/magnetic.ts +88 -0
  74. package/src/operations.ts +31 -0
  75. package/src/pointGroups/data.ts +33 -22
  76. package/src/pointGroups/elements.ts +27 -8
  77. package/src/pointGroups/magneticData.ts +328 -0
  78. package/src/pointGroups/magneticSymbols.ts +110 -0
  79. package/src/pointGroups/pointGroups.ts +388 -65
  80. package/src/pointGroups/properties.ts +47 -56
  81. package/src/pointGroups/wyckoff.ts +17 -2
  82. package/src/siteSymmetry.ts +2 -1
  83. package/src/tensors/propertyTable.ts +89 -0
  84. package/src/tensors/tensorForm.ts +39 -28
  85. package/src/tensors/tensorProperties.ts +50 -0
  86. package/src/version.ts +6 -2
@@ -1,8 +1,11 @@
1
1
  /**
2
- * Public API for the crystallographic point groups: lookup, settings, operations, generators and
3
- * symbols, in the ITC convention (default) or the Birss convention.
2
+ * Public API for the point groups: the 32 crystallographic point groups (crystal classes) and the
3
+ * 90 magnetic point groups with time reversal (32 grey, 58 black-and-white). Lookup, settings,
4
+ * operations, generators and symbols, in the ITC convention (default) or the Birss convention.
4
5
  */
5
6
 
7
+ import { INVERSION, det, matricesEqual } from '../linalg.js';
8
+ import { EPSILON } from '../tolerances.js';
6
9
  import {
7
10
  MAX_GROUP_ORDER,
8
11
  closeGroup,
@@ -13,24 +16,62 @@ import {
13
16
  } from '../operations.js';
14
17
  import { inItaOrder, itaGeneratorOperations } from './itaOrder.js';
15
18
  import type { CrystalSystem } from '../lattice.js';
16
- import { POINT_GROUP_DATA, POINT_GROUP_KEYS, type PointGroupData, type PointGroupKey } from './data.js';
19
+ import { POINT_GROUP_DATA, POINT_GROUP_KEYS, type PointGroupKey, type SettingData } from './data.js';
20
+ import {
21
+ BLACK_WHITE_DATA,
22
+ MAGNETIC_POINT_GROUP_KEYS,
23
+ TIME_REVERSAL,
24
+ greySchoenflies,
25
+ type MagneticPointGroupKey,
26
+ } from './magneticData.js';
27
+ import { primedSymbols } from './magneticSymbols.js';
17
28
 
18
29
  export { POINT_GROUP_KEYS, type PointGroupKey } from './data.js';
30
+ export { MAGNETIC_POINT_GROUP_KEYS, type MagneticPointGroupKey } from './magneticData.js';
19
31
 
20
32
  /** `'itc'`: International Tables for Crystallography (default). `'birss'`: Birss, Symmetry and Magnetism (1966). */
21
33
  export type Convention = 'itc' | 'birss';
22
34
 
23
- /** Type I: crystallographic point group without time reversal (the only type so far). */
24
- export type PointGroupType = 'I';
35
+ /**
36
+ * Type of a magnetic point group. `'I'`: a crystallographic point group, no operation with time
37
+ * reversal. `'II'`: a grey group, every operation of a crystal class with and without time
38
+ * reversal. `'III'`: a black-and-white group, half of the operations of a crystal class combined
39
+ * with time reversal. ITC Vol. D numbers these types differently (docs/conventions.md, "Types").
40
+ */
41
+ export type PointGroupType = 'I' | 'II' | 'III';
25
42
 
26
- export interface PointGroup {
27
- readonly key: PointGroupKey;
43
+ /**
44
+ * A point group with the properties that are fixed by its operations. None of them depends on the
45
+ * setting or the convention (docs/conventions.md, "Properties").
46
+ */
47
+ export interface PointGroup<K extends MagneticPointGroupKey = PointGroupKey> {
48
+ /** The key (docs/conventions.md, "Keys"). */
49
+ readonly key: K;
50
+ /** `'I'` crystal class (white group), `'II'` grey group, `'III'` black-and-white group (docs/conventions.md, "Types"). */
28
51
  readonly type: PointGroupType;
52
+ /**
53
+ * The crystal class of the group: the group itself for a class, otherwise the class it becomes
54
+ * when time reversal is dropped from every operation.
55
+ */
56
+ readonly crystalClass: PointGroupKey;
57
+ /** The crystal system of the crystal class. */
29
58
  readonly crystalSystem: CrystalSystem;
30
59
  /** Number of operations. */
31
60
  readonly order: number;
61
+ /** Contains the inversion, not combined with time reversal. */
62
+ readonly centrosymmetric: boolean;
63
+ /**
64
+ * Polar (pyroelectric class): some direction is left unchanged by every operation. Time reversal
65
+ * does not change a polar vector, so a magnetic group is polar if its crystal class is.
66
+ */
67
+ readonly polar: boolean;
68
+ /** Chiral (enantiomorphic class): proper rotations only, with or without time reversal. */
69
+ readonly chiral: boolean;
70
+ /** The Laue class: the centrosymmetric crystal class generated by the crystal class and the inversion. */
71
+ readonly laueClass: PointGroupKey;
32
72
  }
33
73
 
74
+ /** A setting of a point group: an orientation of its symmetry directions in the Cartesian frame (docs/conventions.md, "Settings"). */
34
75
  export interface Setting {
35
76
  /** 1-based; the same physical frame in both conventions. */
36
77
  readonly index: number;
@@ -38,12 +79,18 @@ export interface Setting {
38
79
  readonly symbol: string;
39
80
  /** Full symbol, symmetry directions separated by single spaces. */
40
81
  readonly fullSymbol: string;
41
- /** Monoclinic: the unique axis; orthorhombic mm2: the axis of the twofold rotation. */
82
+ /**
83
+ * Monoclinic: the unique axis. Orthorhombic `mm2`: the axis of the twofold rotation. In the
84
+ * magnetic API also: orthorhombic groups with three settings, the axis that is c in setting 1;
85
+ * `2'm'm`, with six settings, two for each axis, the normal of the mirror plane without time
86
+ * reversal (there the axis does not identify a setting).
87
+ */
42
88
  readonly uniqueAxis?: 'a' | 'b' | 'c';
43
89
  /** True for the default setting of the requested convention. */
44
90
  readonly isDefault: boolean;
45
91
  }
46
92
 
93
+ /** Which convention and which setting a function answers in. */
47
94
  export interface SettingOptions {
48
95
  /** Default `'itc'`. */
49
96
  readonly convention?: Convention;
@@ -51,6 +98,7 @@ export interface SettingOptions {
51
98
  readonly setting?: number;
52
99
  }
53
100
 
101
+ /** Convention, setting and form of a symbol. */
54
102
  export interface SymbolOptions extends SettingOptions {
55
103
  /**
56
104
  * `'short'` (default): short symbol of the setting, e.g. `321` (ITC) for class `32`.
@@ -60,14 +108,83 @@ export interface SymbolOptions extends SettingOptions {
60
108
  readonly form?: 'short' | 'full' | 'schoenflies';
61
109
  }
62
110
 
63
- const BY_KEY = new Map<string, PointGroupData>(POINT_GROUP_DATA.map((d) => [d.key, d]));
111
+ /**
112
+ * What the functions below need to know about a group, of any type. For a crystal class it is its
113
+ * stored data; for a magnetic group it is put together from the data of its class.
114
+ */
115
+ interface GroupData {
116
+ readonly key: MagneticPointGroupKey;
117
+ readonly type: PointGroupType;
118
+ /** The crystal class: the group itself (type I), or the class it becomes without time reversal. */
119
+ readonly crystalClass: GroupData;
120
+ readonly crystalSystem: CrystalSystem;
121
+ readonly schoenflies: string;
122
+ /** Generators in the stored (Birss) frame, which is setting 1. */
123
+ readonly generators: readonly Operation[];
124
+ readonly settings: readonly { readonly frame: SettingData['frame']; readonly uniqueAxis?: 'a' | 'b' | 'c' }[];
125
+ readonly itcDefault: number;
126
+ /** Type I only: the stored symbols of each setting. */
127
+ readonly symbols?: readonly SettingData[];
128
+ }
129
+
130
+ const BY_KEY = new Map<string, GroupData>();
131
+ for (const d of POINT_GROUP_DATA) {
132
+ const self: GroupData = {
133
+ key: d.key,
134
+ type: 'I',
135
+ get crystalClass() {
136
+ return self;
137
+ },
138
+ crystalSystem: d.crystalSystem,
139
+ schoenflies: d.schoenflies,
140
+ generators: d.generators,
141
+ settings: d.settings,
142
+ itcDefault: d.itcDefault,
143
+ symbols: d.settings,
144
+ };
145
+ BY_KEY.set(d.key, self);
146
+ }
147
+ // Grey groups: the class and time reversal (ITC Vol. D, eq. (1.5.2.1)). birss-app generates them
148
+ // from the generators of the class followed by 1', without the identity generator of class 1.
149
+ for (const d of POINT_GROUP_DATA) {
150
+ const crystalClass = BY_KEY.get(d.key)!;
151
+ const key: MagneticPointGroupKey = `${d.key}1'`;
152
+ BY_KEY.set(key, {
153
+ key,
154
+ type: 'II',
155
+ crystalClass,
156
+ crystalSystem: d.crystalSystem,
157
+ schoenflies: greySchoenflies(d.schoenflies),
158
+ generators: [...(d.key === '1' ? [] : d.generators), TIME_REVERSAL],
159
+ settings: d.settings,
160
+ itcDefault: d.itcDefault,
161
+ });
162
+ }
163
+ for (const d of BLACK_WHITE_DATA) {
164
+ const crystalClass = BY_KEY.get(d.crystalClass)!;
165
+ BY_KEY.set(d.key, {
166
+ key: d.key,
167
+ type: 'III',
168
+ crystalClass,
169
+ crystalSystem: crystalClass.crystalSystem,
170
+ schoenflies: d.schoenflies,
171
+ generators: d.generators,
172
+ settings: d.settings,
173
+ itcDefault: d.itcDefault,
174
+ });
175
+ }
64
176
 
65
177
  /** True if `key` is the key of one of the 32 crystal classes. */
66
178
  export function isPointGroupKey(key: string): key is PointGroupKey {
179
+ return BY_KEY.get(key)?.type === 'I';
180
+ }
181
+
182
+ /** True if `key` is the key of one of the 122 magnetic point groups (the 32 crystal classes included). */
183
+ export function isMagneticPointGroupKey(key: string): key is MagneticPointGroupKey {
67
184
  return BY_KEY.has(key);
68
185
  }
69
186
 
70
- function data(key: string): PointGroupData {
187
+ function data(key: string): GroupData {
71
188
  const d = BY_KEY.get(key);
72
189
  if (!d) throw new RangeError(`Unknown point group key: ${JSON.stringify(key)}`);
73
190
  return d;
@@ -79,12 +196,12 @@ function convention(options?: SettingOptions): Convention {
79
196
  return c;
80
197
  }
81
198
 
82
- function defaultSetting(d: PointGroupData, c: Convention): number {
199
+ function defaultSetting(d: GroupData, c: Convention): number {
83
200
  return c === 'itc' ? d.itcDefault : 1;
84
201
  }
85
202
 
86
203
  /** The requested setting index, checked; the convention's default if none is given. */
87
- function settingIndex(d: PointGroupData, options?: SettingOptions): number {
204
+ function settingIndex(d: GroupData, options?: SettingOptions): number {
88
205
  const c = convention(options);
89
206
  const index = options?.setting ?? defaultSetting(d, c);
90
207
  if (!Number.isInteger(index) || index < 1 || index > d.settings.length) {
@@ -105,7 +222,7 @@ const generatorCache = new Map<string, readonly Operation[]>();
105
222
  const operationCache = new Map<string, readonly Operation[]>();
106
223
 
107
224
  /** The stored generators carried into the frame of a setting: the generators of the Birss convention. */
108
- function settingGenerators(d: PointGroupData, index: number): readonly Operation[] {
225
+ function settingGenerators(d: GroupData, index: number): readonly Operation[] {
109
226
  const cacheKey = `${d.key}#${index}`;
110
227
  let result = generatorCache.get(cacheKey);
111
228
  if (!result) {
@@ -116,17 +233,67 @@ function settingGenerators(d: PointGroupData, index: number): readonly Operation
116
233
  return result;
117
234
  }
118
235
 
236
+ /** The operation of `ops` with the matrix of `op`, whatever its time reversal. */
237
+ function withMatrix(ops: readonly Operation[], op: Operation): Operation {
238
+ const found = ops.find((x) => matricesEqual(x.matrix, op.matrix));
239
+ if (!found) throw new Error('Operation of the crystal class missing in the magnetic group');
240
+ return found;
241
+ }
242
+
243
+ const classSettingCache = new Map<string, number>();
244
+
245
+ /**
246
+ * The setting of the crystal class whose frame is that of setting `index` of the group: the one
247
+ * in which the class has the operations of the group without their time reversal. For a class and
248
+ * a grey group it is `index` itself. A black-and-white group may have its settings in another
249
+ * order than its class, and settings its class does not tell apart (docs/conventions.md,
250
+ * "Settings").
251
+ */
252
+ function classSetting(d: GroupData, index: number): number {
253
+ if (d.type !== 'III') return index;
254
+ const cacheKey = `${d.key}#${index}`;
255
+ let result = classSettingCache.get(cacheKey);
256
+ if (!result) {
257
+ const ops = closeGroup(settingGenerators(d, index));
258
+ const c = d.crystalClass;
259
+ result =
260
+ c.settings.findIndex((_, i) => {
261
+ const classOps = settingOperations(c, i + 1);
262
+ return (
263
+ classOps.length === ops.length && classOps.every((x) => ops.some((y) => matricesEqual(x.matrix, y.matrix)))
264
+ );
265
+ }) + 1;
266
+ if (!result) throw new Error(`Point group ${d.key}, setting ${index}: no setting of class ${c.key} has this frame`);
267
+ classSettingCache.set(cacheKey, result);
268
+ }
269
+ return result;
270
+ }
271
+
119
272
  const itaGeneratorCache = new Map<string, readonly Operation[]>();
120
273
 
121
- /** ITA's generators of a setting (see `generators`); throws if setting 1 has none. */
122
- function settingItaGenerators(d: PointGroupData, index: number): readonly Operation[] {
274
+ /**
275
+ * ITA's generators of a setting (see `generators`); throws if setting 1 of a class has none. For a
276
+ * magnetic group: ITA's generators of its class in the same frame, each with the time reversal it
277
+ * has in the group; for a grey group followed by time reversal 1'.
278
+ */
279
+ function settingItaGenerators(d: GroupData, index: number): readonly Operation[] {
123
280
  const cacheKey = `${d.key}#${index}`;
124
281
  let result = itaGeneratorCache.get(cacheKey);
125
282
  if (!result) {
126
- let gens = itaGeneratorOperations(d.key, index, d.crystalSystem);
127
- if (!gens) {
128
- if (index === 1) throw new Error(`Point group ${d.key}: no ITA generators for setting 1`);
129
- gens = settingItaGenerators(d, 1).map((op) => transformOperation(op, d.settings[index - 1].frame));
283
+ let gens: Operation[];
284
+ if (d.type === 'I') {
285
+ const ita = itaGeneratorOperations(d.key, index, d.crystalSystem);
286
+ if (ita) gens = ita;
287
+ else {
288
+ if (index === 1) throw new Error(`Point group ${d.key}: no ITA generators for setting 1`);
289
+ gens = settingItaGenerators(d, 1).map((op) => transformOperation(op, d.settings[index - 1].frame));
290
+ }
291
+ } else {
292
+ const classGens = settingItaGenerators(d.crystalClass, classSetting(d, index));
293
+ gens =
294
+ d.type === 'II'
295
+ ? [...classGens, TIME_REVERSAL]
296
+ : classGens.map((g) => withMatrix(settingOperations(d, index), g));
130
297
  }
131
298
  result = Object.freeze(gens.map(freezeOperation));
132
299
  itaGeneratorCache.set(cacheKey, result);
@@ -142,7 +309,7 @@ const birssOperationCache = new Map<string, readonly Operation[]>();
142
309
  * that append every new product g·h of two operations found before the pass, in the order of g,
143
310
  * then h, until a pass adds nothing.
144
311
  */
145
- function birssOperations(d: PointGroupData, index: number): readonly Operation[] {
312
+ function birssOperations(d: GroupData, index: number): readonly Operation[] {
146
313
  const cacheKey = `${d.key}#${index}`;
147
314
  let result = birssOperationCache.get(cacheKey);
148
315
  if (!result) {
@@ -171,20 +338,32 @@ function birssOperations(d: PointGroupData, index: number): readonly Operation[]
171
338
  }
172
339
 
173
340
  /**
174
- * The operations of a setting in ITA's order (docs/conventions.md, "Order of the operations"): for a
175
- * setting ITA lists, coset by coset from ITA's generators; for a setting ITA does not list (today
176
- * `mm2` settings 2 and 3), the order of setting 1, carried into the frame of the setting. Setting 1
177
- * is always one ITA lists; throws if its generators are missing or do not give the closure.
341
+ * The operations of a setting in ITA's order (docs/conventions.md, "Order of the operations"). A
342
+ * crystal class: for a setting ITA lists, coset by coset from ITA's generators; for a setting ITA
343
+ * does not list (today `mm2` settings 2 and 3), the order of setting 1, carried into the frame of
344
+ * the setting. Setting 1 is always one ITA lists; throws if its generators are missing or do not
345
+ * give the closure. A black-and-white group: the operations of its class in the same frame, in
346
+ * that order, each with the time reversal it has in the group. A grey group: the operations of its
347
+ * class, then the same sequence combined with time reversal.
178
348
  */
179
- function settingOperations(d: PointGroupData, index: number): readonly Operation[] {
349
+ function settingOperations(d: GroupData, index: number): readonly Operation[] {
180
350
  const cacheKey = `${d.key}#${index}`;
181
351
  let result = operationCache.get(cacheKey);
182
352
  if (!result) {
183
353
  const closed = closeGroup(settingGenerators(d, index));
184
- let ordered = inItaOrder(d.key, index, d.crystalSystem, closed);
185
- if (!ordered) {
186
- if (index === 1) throw new Error(`Point group ${d.key}: no ITA generators for setting 1`);
187
- ordered = settingOperations(d, 1).map((op) => transformOperation(op, d.settings[index - 1].frame));
354
+ let ordered: Operation[] | undefined;
355
+ if (d.type === 'I') {
356
+ ordered = inItaOrder(d.key, index, d.crystalSystem, closed);
357
+ if (!ordered) {
358
+ if (index === 1) throw new Error(`Point group ${d.key}: no ITA generators for setting 1`);
359
+ ordered = settingOperations(d, 1).map((op) => transformOperation(op, d.settings[index - 1].frame));
360
+ }
361
+ } else {
362
+ const classOps = settingOperations(d.crystalClass, classSetting(d, index));
363
+ ordered =
364
+ d.type === 'II'
365
+ ? [...classOps, ...classOps.map((op) => compose(TIME_REVERSAL, op))]
366
+ : classOps.map((op) => withMatrix(closed, op));
188
367
  }
189
368
  if (!sameSet(ordered, closed)) {
190
369
  throw new Error(`Point group ${d.key}, setting ${index}: ordered operations differ from the closure`);
@@ -203,47 +382,148 @@ function sameSet(a: readonly Operation[], b: readonly Operation[]): boolean {
203
382
  );
204
383
  }
205
384
 
206
- const groupCache = new Map<string, PointGroup>();
385
+ const symbolCache = new Map<string, { readonly short: string; readonly full: string }>();
386
+
387
+ /**
388
+ * Short and full symbol of a setting in a convention. A crystal class: as stored. A grey group:
389
+ * the symbols of its class followed by `1'`. A black-and-white group: the symbols of its class in
390
+ * the same frame, with the primes of its operations (magneticSymbols.ts).
391
+ */
392
+ function settingSymbols(d: GroupData, index: number, c: Convention): { readonly short: string; readonly full: string } {
393
+ if (d.symbols) return d.symbols[index - 1][c];
394
+ const cacheKey = `${d.key}#${index}#${c}`;
395
+ let result = symbolCache.get(cacheKey);
396
+ if (!result) {
397
+ const classSymbols = settingSymbols(d.crystalClass, classSetting(d, index), c);
398
+ result = Object.freeze(
399
+ d.type === 'II'
400
+ ? { short: `${classSymbols.short}1'`, full: `${classSymbols.full} 1'` }
401
+ : primedSymbols(classSymbols, d.crystalSystem, c, settingOperations(d, index)),
402
+ );
403
+ symbolCache.set(cacheKey, result);
404
+ }
405
+ return result;
406
+ }
407
+
408
+ // --- properties fixed by the operations (setting 1, the frame all stored data share) ---------------
409
+
410
+ /** Contains the inversion, not combined with time reversal. */
411
+ function hasInversion(ops: readonly Operation[]): boolean {
412
+ return ops.some((op) => !op.timeReversal && matricesEqual(op.matrix, INVERSION));
413
+ }
414
+
415
+ /**
416
+ * Some direction is left unchanged by every operation: the group average of the matrices, which
417
+ * projects onto the invariant vectors, is not zero.
418
+ */
419
+ function hasFixedDirection(ops: readonly Operation[]): boolean {
420
+ for (let i = 0; i < 3; i++) {
421
+ for (let j = 0; j < 3; j++) {
422
+ let sum = 0;
423
+ for (const op of ops) sum += op.matrix[i][j];
424
+ if (Math.abs(sum / ops.length) > EPSILON) return true;
425
+ }
426
+ }
427
+ return false;
428
+ }
429
+
430
+ const laueCache = new Map<string, PointGroupKey>();
431
+
432
+ /** The centrosymmetric crystal class generated by a crystal class and the inversion. */
433
+ function laueClassOf(crystalClass: GroupData): PointGroupKey {
434
+ const cached = laueCache.get(crystalClass.key);
435
+ if (cached) return cached;
436
+ const ops = settingOperations(crystalClass, 1);
437
+ const inversion: Operation = { matrix: INVERSION, timeReversal: false };
438
+ const withInversion = [...ops];
439
+ for (const op of ops) {
440
+ const product = compose(inversion, op);
441
+ if (!withInversion.some((x) => operationsEqual(x, product))) withInversion.push(product);
442
+ }
443
+ const sameSet = (other: readonly Operation[]): boolean =>
444
+ other.length === withInversion.length && other.every((x) => withInversion.some((y) => operationsEqual(x, y)));
445
+ const laue = POINT_GROUP_KEYS.find((key) => sameSet(settingOperations(data(key), 1)));
446
+ if (!laue) throw new Error(`No centrosymmetric class matches ${crystalClass.key} with the inversion`);
447
+ laueCache.set(crystalClass.key, laue);
448
+ return laue;
449
+ }
450
+
451
+ const groupCache = new Map<string, PointGroup<MagneticPointGroupKey>>();
207
452
 
208
- /** The point group with this key. Throws a RangeError for an unknown key. */
209
- export function pointGroup(key: PointGroupKey): PointGroup {
453
+ /**
454
+ * The point group with this key: a crystal class or a magnetic point group. Throws a RangeError
455
+ * for an unknown key.
456
+ */
457
+ export function pointGroup<K extends MagneticPointGroupKey>(key: K): PointGroup<K> {
210
458
  const d = data(key);
211
459
  let g = groupCache.get(d.key);
212
460
  if (!g) {
213
- g = Object.freeze({ key: d.key, type: 'I', crystalSystem: d.crystalSystem, order: settingOperations(d, 1).length });
461
+ const ops = settingOperations(d, 1);
462
+ g = Object.freeze({
463
+ key: d.key,
464
+ type: d.type,
465
+ crystalClass: d.crystalClass.key as PointGroupKey,
466
+ crystalSystem: d.crystalSystem,
467
+ order: ops.length,
468
+ centrosymmetric: hasInversion(ops),
469
+ polar: hasFixedDirection(ops),
470
+ chiral: ops.every((op) => det(op.matrix) > 0),
471
+ laueClass: laueClassOf(d.crystalClass),
472
+ });
214
473
  groupCache.set(d.key, g);
215
474
  }
216
- return g;
475
+ return g as PointGroup<K>;
217
476
  }
218
477
 
219
478
  /** All 32 crystal classes, in the order of `POINT_GROUP_KEYS` (by crystal system, as in ITA). */
220
479
  export function pointGroups(): readonly PointGroup[] {
221
- return POINT_GROUP_KEYS.map(pointGroup);
480
+ return POINT_GROUP_KEYS.map((key) => pointGroup(key));
481
+ }
482
+
483
+ /**
484
+ * All 122 magnetic point groups, in the order of `POINT_GROUP_KEYS` of the magnetic API: the 32 crystal
485
+ * classes (type I), the 32 grey groups (type II) in the same order, and the 58 black-and-white
486
+ * groups (type III) in the order of ITC Vol. D Table 1.5.2.3.
487
+ */
488
+ export function magneticPointGroups(): readonly PointGroup<MagneticPointGroupKey>[] {
489
+ return MAGNETIC_POINT_GROUP_KEYS.map((key) => pointGroup(key));
490
+ }
491
+
492
+ /**
493
+ * The key of the crystal class of a point group: the group itself for a class, otherwise the class
494
+ * it becomes when time reversal is dropped from every operation. Internal (not exported from
495
+ * index.ts).
496
+ */
497
+ export function crystalClassOf(key: MagneticPointGroupKey): PointGroupKey {
498
+ return data(key).crystalClass.key as PointGroupKey;
222
499
  }
223
500
 
224
501
  /** The settings of a point group, with symbols and the default of the given convention (default `'itc'`). */
225
- export function settings(key: PointGroupKey, conventionName: Convention = 'itc'): readonly Setting[] {
502
+ export function settings(key: MagneticPointGroupKey, conventionName: Convention = 'itc'): readonly Setting[] {
226
503
  const d = data(key);
227
504
  const c = convention({ convention: conventionName });
228
505
  const def = defaultSetting(d, c);
229
- return d.settings.map((s, i) =>
230
- Object.freeze({
506
+ return d.settings.map((s, i) => {
507
+ const symbols = settingSymbols(d, i + 1, c);
508
+ return Object.freeze({
231
509
  index: i + 1,
232
- symbol: s[c].short,
233
- fullSymbol: s[c].full,
510
+ symbol: symbols.short,
511
+ fullSymbol: symbols.full,
234
512
  ...(s.uniqueAxis ? { uniqueAxis: s.uniqueAxis } : {}),
235
513
  isDefault: i + 1 === def,
236
- }),
237
- );
514
+ });
515
+ });
238
516
  }
239
517
 
240
518
  /**
241
519
  * All operations of the point group in the requested setting (default: the convention's default
242
520
  * setting). ITC convention: in ITA's order, identity first, then as ITA numbers the general
243
- * position; Birss convention: in birss-app's order (docs/conventions.md, "Order of the
244
- * operations"). The matrices are Cartesian (docs/conventions.md, "Cartesian frame").
521
+ * position of the crystal class; an operation of a magnetic group has the number it has in the
522
+ * class, and a grey group lists the class first, then the same sequence with time reversal. Birss
523
+ * convention: in birss-app's order (docs/conventions.md, "Order of the operations"). The matrices
524
+ * are Cartesian (docs/conventions.md, "Cartesian frame").
245
525
  */
246
- export function operations(key: PointGroupKey, options?: SettingOptions): readonly Operation[] {
526
+ export function operations(key: MagneticPointGroupKey, options?: SettingOptions): readonly Operation[] {
247
527
  const d = data(key);
248
528
  const index = settingIndex(d, options);
249
529
  return convention(options) === 'birss' ? birssOperations(d, index) : settingOperations(d, index);
@@ -253,20 +533,21 @@ export function operations(key: PointGroupKey, options?: SettingOptions): readon
253
533
  * Generators of the point group in the requested setting (docs/conventions.md, "Generators"). ITC
254
534
  * convention: ITA's selected generators in printed order, without the identity and ITA's lattice
255
535
  * translations, so the list is empty for `1`; for `mm2` settings 2 and 3, which ITA does not list,
256
- * those of setting 1 carried into their frame. Birss convention: birss-app's generators (for `1`
257
- * the identity, as in birss-app).
536
+ * those of setting 1 carried into their frame; for a magnetic group, those of its class with the
537
+ * time reversal they have in the group, for a grey group followed by `1'`. Birss convention:
538
+ * birss-app's generators (for `1` the identity, as in birss-app).
258
539
  */
259
- export function generators(key: PointGroupKey, options?: SettingOptions): readonly Operation[] {
540
+ export function generators(key: MagneticPointGroupKey, options?: SettingOptions): readonly Operation[] {
260
541
  const d = data(key);
261
542
  const index = settingIndex(d, options);
262
543
  return convention(options) === 'birss' ? settingGenerators(d, index) : settingItaGenerators(d, index);
263
544
  }
264
545
 
265
546
  /** A symbol of the point group in the requested convention and setting. */
266
- export function symbol(key: PointGroupKey, options?: SymbolOptions): string {
547
+ export function symbol(key: MagneticPointGroupKey, options?: SymbolOptions): string {
267
548
  const d = data(key);
268
549
  const form = options?.form ?? 'short';
269
- const s = d.settings[settingIndex(d, options) - 1][convention(options)];
550
+ const s = settingSymbols(d, settingIndex(d, options), convention(options));
270
551
  if (form === 'schoenflies') return d.schoenflies;
271
552
  if (form === 'short') return s.short;
272
553
  if (form === 'full') return s.full;
@@ -274,31 +555,73 @@ export function symbol(key: PointGroupKey, options?: SymbolOptions): string {
274
555
  }
275
556
 
276
557
  /** A point group in a given setting. */
277
- export interface PointGroupInSetting {
278
- readonly key: PointGroupKey;
558
+ export interface PointGroupInSetting<K extends MagneticPointGroupKey = PointGroupKey> {
559
+ /** The key of the point group. */
560
+ readonly key: K;
561
+ /** The 1-based index of the setting. */
279
562
  readonly setting: number;
280
563
  }
281
564
 
282
565
  const compact = (s: string): string => s.replace(/\s+/g, '');
283
566
 
284
- /**
285
- * Finds a point group by a Hermann–Mauguin symbol in the given convention (default `'itc'`): the
286
- * key (giving the default setting), the short symbol of a setting, or a full symbol (spaces are
287
- * ignored). Returns undefined if nothing matches, also for a value that is not a string. Symbols
288
- * are written as in this library: `-` for the overbar, also in the Birss convention (`m-3m`, where
289
- * Birss prints `m3m`; docs/conventions.md, "Where ITC and Birss differ").
290
- */
291
- export function findPointGroup(hm: string, conventionName: Convention = 'itc'): PointGroupInSetting | undefined {
567
+ function find(
568
+ hm: string,
569
+ conventionName: Convention,
570
+ classesOnly: boolean,
571
+ ): PointGroupInSetting<MagneticPointGroupKey> | undefined {
292
572
  const c = convention({ convention: conventionName });
293
573
  if (typeof hm !== 'string') return undefined;
294
574
  const target = compact(hm);
295
- const byKey = BY_KEY.get(target);
296
- if (byKey) return { key: byKey.key, setting: defaultSetting(byKey, c) };
575
+ const candidates = [...BY_KEY.values()].filter((d) => !classesOnly || d.type === 'I');
576
+ /** The settings of `d` whose symbol of this form is the target. */
577
+ const named = (d: GroupData, form: 'short' | 'full'): number[] =>
578
+ d.settings.map((_, j) => j + 1).filter((i) => compact(settingSymbols(d, i, c)[form]) === target);
579
+ /** One setting named: that one. Several (a monoclinic short symbol) or none (a key): the default. */
580
+ const pick = (d: GroupData, settingsNamed: number[]): PointGroupInSetting<MagneticPointGroupKey> => ({
581
+ key: d.key,
582
+ setting: settingsNamed.length === 1 ? settingsNamed[0]! : defaultSetting(d, c),
583
+ });
584
+ const byKey = candidates.find((d) => d.key === target);
585
+ if (byKey) return pick(byKey, named(byKey, 'short'));
297
586
  for (const form of ['short', 'full'] as const) {
298
- for (const d of POINT_GROUP_DATA) {
299
- const i = d.settings.findIndex((s) => compact(s[c][form]) === target);
300
- if (i >= 0) return { key: d.key, setting: i + 1 };
587
+ for (const d of candidates) {
588
+ const settingsNamed = named(d, form);
589
+ if (settingsNamed.length > 0) return pick(d, settingsNamed);
301
590
  }
302
591
  }
303
592
  return undefined;
304
593
  }
594
+
595
+ /**
596
+ * Finds a crystal class by a Hermann–Mauguin symbol in the given convention (default `'itc'`): the
597
+ * short symbol of a setting, a full symbol, or the key (spaces are ignored). A symbol that names
598
+ * one setting gives that setting; the key, and a short symbol that several settings share (the
599
+ * monoclinic ones: `2` for unique axis b and c), give the default setting of the convention.
600
+ * Returns undefined if nothing matches, also for a value that is not a string and for the
601
+ * symbol of a magnetic group with time reversal (`findMagneticPointGroup`). Symbols are written as
602
+ * in this library: `-` for the overbar, also in the Birss convention (`m-3m`, where Birss prints
603
+ * `m3m`; docs/conventions.md, "Where ITC and Birss differ").
604
+ */
605
+ export function findPointGroup(hm: string, conventionName: Convention = 'itc'): PointGroupInSetting | undefined {
606
+ return find(hm, conventionName, true) as PointGroupInSetting | undefined;
607
+ }
608
+
609
+ /**
610
+ * Finds one of the 122 magnetic point groups (the 32 crystal classes included) by a symbol in the
611
+ * given convention (default `'itc'`): the short symbol of a setting, a full symbol, or the key
612
+ * (spaces are ignored). A prime `'` marks time reversal: `4'/mmm'`, `2221'`. A symbol that names
613
+ * one setting gives that setting; the key, and a short symbol that several settings share (the
614
+ * monoclinic ones: `2'` for unique axis b and c), give the default setting of the convention. In
615
+ * the Birss convention two keys are the Birss symbol of a setting that is not the default
616
+ * (`mm'm'` and `6'/mmm'`, setting 2; Birss prints `m'm'm` and `6'/mm'm` for these groups) and give
617
+ * that setting, like any symbol. Returns undefined if nothing matches, also for a value that is
618
+ * not a string. Symbols are written as in this library: `-` for the overbar, also in the Birss
619
+ * convention (`m'-3'm`, where Birss prints `m'3m`; docs/conventions.md, "Where ITC and Birss
620
+ * differ").
621
+ */
622
+ export function findMagneticPointGroup(
623
+ hm: string,
624
+ conventionName: Convention = 'itc',
625
+ ): PointGroupInSetting<MagneticPointGroupKey> | undefined {
626
+ return find(hm, conventionName, false);
627
+ }