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