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
@@ -10,39 +10,75 @@
10
10
  * `tensorProjection.ts`, `linalg.ts`, revision 2be5fe3).
11
11
  */
12
12
 
13
+ import { SETTING_KEYS, TOLERANCE_KEYS, checkNumbers, checkOptions, shown } from '../checks.js';
13
14
  import { det, type Mat3 } from '../linalg.js';
14
- import { checkOrthogonal, checkTimeReversal, closeGroup, type Operation } from '../operations.js';
15
- import { operations, settings, type MagneticPointGroupKey, type SettingOptions } from '../pointGroups/pointGroups.js';
15
+ import {
16
+ checkCartesianOperations,
17
+ checkOrthogonalWithin,
18
+ checkTimeReversal,
19
+ type CartesianOperation,
20
+ } from '../operations.js';
21
+ import { idealGroup, type IdealGroup } from '../closure.js';
22
+ import {
23
+ operations,
24
+ settingIndexOf,
25
+ type MagneticPointGroupKey,
26
+ type SettingOptions,
27
+ } from '../pointGroups/pointGroups.js';
16
28
  import { specOf, type TensorPropertyName } from './propertyTable.js';
17
- import { EPSILON, SNAP_EPSILON } from '../tolerances.js';
18
- import { reducedRowEchelon } from './rowEchelon.js';
29
+ import { EPSILON, MAX_PIVOT_THRESHOLD, SNAP_EPSILON, toleranceOf, type ToleranceOptions } from '../tolerances.js';
30
+ import { PIVOT_EPSILON, rowEchelonWithPivots } from './rowEchelon.js';
31
+ import { deepFreeze } from '../freeze.js';
19
32
 
20
33
  /** Rank of a tensor: 0 (scalar) to 4. */
21
34
  export type TensorRank = 0 | 1 | 2 | 3 | 4;
22
35
 
23
- /** `'polar'`: transforms with R; `'axial'`: with det(R)·R (pseudotensor). */
24
- export type TensorParity = 'polar' | 'axial';
36
+ /**
37
+ * How the tensor behaves under an improper operation: `'polar'` transforms with R, `'axial'` with
38
+ * det(R)·R (a pseudotensor; the prefix e of its Jahn symbol, e.g. eV for an axial vector). ITC calls the behaviour under inversion "parity", under which a polar
39
+ * vector is odd, so the field is named `kind`.
40
+ */
41
+ export type TensorKind = 'polar' | 'axial';
25
42
 
26
- /** `'i'`: time-even (invariant under time reversal); `'c'`: time-odd (changes sign). */
27
- export type TimeParity = 'i' | 'c';
43
+ /**
44
+ * How the tensor behaves under time reversal (ITC Vol. D: time-even, time-odd or
45
+ * time-antisymmetric): `'even'` unchanged, `'odd'` changes sign (the prefix a, "magnetic", of its
46
+ * Jahn symbol, e.g. aeV for the magnetization; Gallego et al. (2019), p. 439).
47
+ */
48
+ export type TimeParity = 'even' | 'odd';
28
49
 
29
50
  /**
30
- * Symmetry of the tensor under permutation of its own indices, independent of the crystal:
31
- * `'none'`; `'ij'`: the first two indices are interchangeable; `'jk'`: the last two (for rank 3 the
32
- * index symmetry of second-harmonic generation and the piezoelectric effect); `'ij_kl'` (rank 4):
33
- * each of the two pairs symmetric, the pairs not interchangeable (e.g. electric-quadrupole SHG,
34
- * photoelasticity); `'voigt'` (rank 4): `'ij_kl'` and the pairs interchangeable (elastic
35
- * stiffness). At rank 2, `'ij'` and `'jk'` both mean a symmetric tensor.
51
+ * Symmetry of the tensor under permutation of its own indices, independent of the crystal, with its
52
+ * Jahn symbol as Gallego et al. write them (_Acta Cryst._ A75, 438 (2019), pp. 439–440, after Jahn
53
+ * (1949): square brackets for a symmetric, curly brackets for an antisymmetric set of indices):
54
+ * - `'none'`: no index symmetry, Vⁿ;
55
+ * - `'symmetric-first-pair'` (rank 2 and above): the first two indices are interchangeable, [V²]V…;
56
+ * - `'symmetric-last-pair'` (rank 2 and above): the last two, V…[V²] (at rank 3 second-harmonic
57
+ * generation and the piezoelectric effect); at rank 2 both mean a symmetric tensor, [V²];
58
+ * - `'symmetric-pairs'` (rank 4): each of the two pairs symmetric, the pairs not interchangeable,
59
+ * [V²][V²] (e.g. electric-quadrupole SHG, photoelasticity; Jahn 1949 writes [V²]²);
60
+ * - `'symmetric-pairs-exchangeable'` (rank 4): in addition the pairs interchangeable, [[V²][V²]]
61
+ * (elastic stiffness, the symmetry Voigt's notation uses; Jahn 1949 writes [[V²]²]);
62
+ * - `'antisymmetric'` (rank 2): t_ij = −t_ji, {V²};
63
+ * - `'totally-symmetric'` (rank 2 and above): every index interchangeable, [Vⁿ] (e.g. second-harmonic
64
+ * generation under Kleinman's condition).
36
65
  */
37
- export type IntrinsicSymmetry = 'none' | 'ij' | 'jk' | 'ij_kl' | 'voigt';
66
+ export type IntrinsicSymmetry =
67
+ | 'none'
68
+ | 'symmetric-first-pair'
69
+ | 'symmetric-last-pair'
70
+ | 'symmetric-pairs'
71
+ | 'symmetric-pairs-exchangeable'
72
+ | 'antisymmetric'
73
+ | 'totally-symmetric';
38
74
 
39
- /** The kind of tensor, as in birss-app. */
75
+ /** The kind of tensor (#137 E1, E2). */
40
76
  export interface TensorSpec {
41
77
  /** Number of indices, 0 to 4. */
42
78
  readonly rank: TensorRank;
43
79
  /** `'polar'` or `'axial'`: whether an improper operation changes the sign (factor det R). */
44
- readonly parity: TensorParity;
45
- /** `'i'` (time-even) or `'c'` (time-odd): whether time reversal changes the sign. */
80
+ readonly kind: TensorKind;
81
+ /** `'even'` or `'odd'`: whether time reversal changes the sign. */
46
82
  readonly timeParity: TimeParity;
47
83
  /** The index symmetry of the tensor. */
48
84
  readonly intrinsic: IntrinsicSymmetry;
@@ -50,27 +86,28 @@ export interface TensorSpec {
50
86
 
51
87
  /**
52
88
  * The form of a tensor under a point group: the subspace of tensors invariant under every
53
- * operation and with the requested index symmetry. Every entry is a plain number; a vector of
54
- * components has length 3^rank (flat index, see the module documentation).
89
+ * operation and with the requested index symmetry. Every entry is a plain number; a component is
90
+ * named by its flat index, the first index slowest (see the module documentation).
55
91
  */
56
92
  export interface TensorForm {
57
93
  /** The rank of the tensor. */
58
94
  readonly rank: TensorRank;
59
95
  /**
60
- * A basis of the allowed tensors in reduced row echelon form: one row per free parameter, with 1
61
- * at the parameter's own component and 0 at every other parameter's component. Every allowed
96
+ * A basis of the allowed tensors in reduced row echelon form: one tensor per free parameter, with
97
+ * 1 at the parameter's own component and 0 at every other parameter's component. Every allowed
62
98
  * tensor is Σₖ pₖ·basis[k], where pₖ is the value of its component `parameters[k]`. Empty if the
63
99
  * tensor vanishes.
64
100
  */
65
101
  readonly basis: readonly (readonly number[])[];
66
102
  /**
67
103
  * The free parameters, as the index lists of their components (e.g. `[0, 1, 1]` for xyy), one per
68
- * basis row and in its order: each is the lowest component independent of those before it.
104
+ * basis tensor and in its order: each is the lowest component independent of those before it.
69
105
  */
70
106
  readonly parameters: readonly (readonly number[])[];
71
107
  /**
72
108
  * The constraints: one row r per component that is not a parameter, in ascending flat index,
73
- * with r·t = 0 for every allowed tensor t. Row c reads t_c − Σₖ basis[k][c]·t_{parameters[k]} = 0;
109
+ * with r·t = 0 for every allowed tensor t, both by flat index. Row c reads
110
+ * t_c − Σₖ b_{k,c}·t_{parameters[k]} = 0, with b_{k,c} the component c of basis[k];
74
111
  * a row with a single 1 says that the component vanishes.
75
112
  */
76
113
  readonly relations: readonly (readonly number[])[];
@@ -78,32 +115,76 @@ export interface TensorForm {
78
115
 
79
116
  const RANKS: readonly number[] = [0, 1, 2, 3, 4];
80
117
 
81
- /** Checks a specification and returns the index permutations that generate its intrinsic symmetry. */
82
- function checkSpec(spec: TensorSpec): number[][] {
83
- const { rank, parity, timeParity, intrinsic } = spec;
84
- if (!RANKS.includes(rank)) throw new RangeError(`Tensor rank must be 0 to 4, not ${JSON.stringify(rank)}`);
85
- if (parity !== 'polar' && parity !== 'axial')
86
- throw new RangeError(`Unknown tensor parity: ${JSON.stringify(parity)}`);
87
- if (timeParity !== 'i' && timeParity !== 'c')
88
- throw new RangeError(`Unknown time parity: ${JSON.stringify(timeParity)}`);
89
- const swap = (a: number, b: number) => {
90
- const p = Array.from({ length: rank }, (_, k) => k);
91
- [p[a], p[b]] = [p[b], p[a]];
92
- return p;
118
+ /** A permutation of the indices of a tensor (`out[k] = in[perm[k]]`) with the sign it gives. */
119
+ export interface SignedPermutation {
120
+ readonly perm: readonly number[];
121
+ readonly sign: 1 | -1;
122
+ }
123
+
124
+ /** The names of the fields and values up to 0.10, with those that replace them (#137 E1, E2). */
125
+ const RENAMED_INTRINSIC: Readonly<Record<string, IntrinsicSymmetry>> = {
126
+ ij: 'symmetric-first-pair',
127
+ jk: 'symmetric-last-pair',
128
+ ij_kl: 'symmetric-pairs',
129
+ voigt: 'symmetric-pairs-exchangeable',
130
+ };
131
+ const RENAMED_TIME_PARITY: Readonly<Record<string, TimeParity>> = { i: 'even', c: 'odd' };
132
+
133
+ /** Checks a specification and returns the signed index permutations that generate its intrinsic symmetry. */
134
+ function checkSpec(spec: TensorSpec): SignedPermutation[] {
135
+ const { rank, kind, timeParity, intrinsic } = spec;
136
+ if ('parity' in spec) {
137
+ throw new RangeError(
138
+ `The field parity of a tensor specification is named kind since symcrys 0.11 ('polar' or 'axial')`,
139
+ );
140
+ }
141
+ if (!RANKS.includes(rank)) throw new RangeError(`Tensor rank must be 0 to 4, not ${shown(rank)}`);
142
+ if (kind !== 'polar' && kind !== 'axial') {
143
+ throw new RangeError(`The kind of a tensor is 'polar' or 'axial', not ${shown(kind)}`);
144
+ }
145
+ if (timeParity !== 'even' && timeParity !== 'odd') {
146
+ const renamed = typeof timeParity === 'string' ? RENAMED_TIME_PARITY[timeParity] : undefined;
147
+ throw new RangeError(
148
+ `The time parity of a tensor is 'even' or 'odd', not ${shown(timeParity)}` +
149
+ (renamed ? ` ('${timeParity}' up to symcrys 0.10 is '${renamed}')` : ''),
150
+ );
151
+ }
152
+ const swap = (a: number, b: number, sign: 1 | -1 = 1): SignedPermutation => {
153
+ const perm = Array.from({ length: rank }, (_, k) => k);
154
+ [perm[a], perm[b]] = [perm[b], perm[a]];
155
+ return { perm, sign };
156
+ };
157
+ const needs = (condition: boolean, what: string) => {
158
+ if (!condition) throw new RangeError(`Intrinsic symmetry '${intrinsic}' needs ${what}, not rank ${rank}`);
93
159
  };
94
160
  switch (intrinsic) {
95
161
  case 'none':
96
162
  return [];
97
- case 'ij':
98
- case 'jk':
99
- if (rank < 2) throw new RangeError(`Intrinsic symmetry '${intrinsic}' needs rank 2 or more, not ${rank}`);
100
- return [intrinsic === 'ij' ? swap(0, 1) : swap(rank - 2, rank - 1)];
101
- case 'ij_kl':
102
- case 'voigt':
103
- if (rank !== 4) throw new RangeError(`Intrinsic symmetry '${intrinsic}' needs rank 4, not ${rank}`);
104
- return intrinsic === 'ij_kl' ? [swap(0, 1), swap(2, 3)] : [swap(0, 1), swap(2, 3), [2, 3, 0, 1]];
105
- default:
106
- throw new RangeError(`Unknown intrinsic symmetry: ${JSON.stringify(intrinsic)}`);
163
+ case 'symmetric-first-pair':
164
+ needs(rank >= 2, 'rank 2 or more');
165
+ return [swap(0, 1)];
166
+ case 'symmetric-last-pair':
167
+ needs(rank >= 2, 'rank 2 or more');
168
+ return [swap(rank - 2, rank - 1)];
169
+ case 'symmetric-pairs':
170
+ needs(rank === 4, 'rank 4');
171
+ return [swap(0, 1), swap(2, 3)];
172
+ case 'symmetric-pairs-exchangeable':
173
+ needs(rank === 4, 'rank 4');
174
+ return [swap(0, 1), swap(2, 3), { perm: [2, 3, 0, 1], sign: 1 }];
175
+ case 'antisymmetric':
176
+ needs(rank === 2, 'rank 2');
177
+ return [swap(0, 1, -1)];
178
+ case 'totally-symmetric':
179
+ needs(rank >= 2, 'rank 2 or more');
180
+ return Array.from({ length: rank - 1 }, (_, k) => swap(k, k + 1));
181
+ default: {
182
+ const renamed = typeof intrinsic === 'string' ? RENAMED_INTRINSIC[intrinsic] : undefined;
183
+ throw new RangeError(
184
+ `Unknown intrinsic symmetry: ${shown(intrinsic)}` +
185
+ (renamed ? ` ('${intrinsic as string}' up to symcrys 0.10 is '${renamed}')` : ''),
186
+ );
187
+ }
107
188
  }
108
189
  }
109
190
 
@@ -120,23 +201,36 @@ function digits(rank: number): readonly (readonly number[])[] {
120
201
  return table;
121
202
  }
122
203
 
123
- /** The flat indices reached from `flat` by the index permutations (`out[k] = in[perm[k]]`). */
124
- function orbit(flat: number, rank: number, perms: readonly number[][]): number[] {
204
+ /**
205
+ * The flat indices reached from `flat` by the signed index permutations (`out[k] = in[perm[k]]`),
206
+ * each with the product of the signs on the way; undefined if one is reached with both signs (the
207
+ * seed of an antisymmetric tensor with two equal indices, which vanishes).
208
+ */
209
+ function orbit(
210
+ flat: number,
211
+ rank: number,
212
+ perms: readonly SignedPermutation[],
213
+ ): { flat: number; sign: 1 | -1 }[] | undefined {
125
214
  const d = digits(rank);
126
- const seen = new Set([flat]);
215
+ const seen = new Map<number, 1 | -1>([[flat, 1]]);
127
216
  const stack = [flat];
128
217
  while (stack.length > 0) {
129
218
  const cur = stack.pop()!;
130
- for (const p of perms) {
219
+ const curSign = seen.get(cur)!;
220
+ for (const { perm, sign } of perms) {
131
221
  let next = 0;
132
- for (let k = 0; k < rank; k++) next = next * 3 + d[cur][p[k]];
133
- if (!seen.has(next)) {
134
- seen.add(next);
222
+ for (let k = 0; k < rank; k++) next = next * 3 + d[cur][perm[k]];
223
+ const nextSign = (curSign * sign) as 1 | -1;
224
+ const known = seen.get(next);
225
+ if (known === undefined) {
226
+ seen.set(next, nextSign);
135
227
  stack.push(next);
228
+ } else if (known !== nextSign) {
229
+ return undefined;
136
230
  }
137
231
  }
138
232
  }
139
- return [...seen];
233
+ return [...seen].map(([f, sign]) => ({ flat: f, sign }));
140
234
  }
141
235
 
142
236
  function freezeRows(rows: readonly (readonly number[])[]): readonly (readonly number[])[] {
@@ -148,28 +242,38 @@ function freezeRows(rows: readonly (readonly number[])[]): readonly (readonly nu
148
242
  * (a unit tensor on one orbit of the intrinsic symmetry) projects it onto the invariant subspace;
149
243
  * the projections span it, and the reduced row echelon form makes the basis minimal and canonical.
150
244
  */
151
- function project(group: readonly Operation[], spec: TensorSpec, perms: number[][]): TensorForm {
245
+ function project(
246
+ group: readonly CartesianOperation[],
247
+ spec: TensorSpec,
248
+ perms: readonly SignedPermutation[],
249
+ deviation = 0,
250
+ ): TensorForm {
152
251
  const { rank } = spec;
153
252
  const dim = 3 ** rank;
154
253
  const d = digits(rank);
155
254
  const ops = group.map((op) => ({
156
- m: op.matrix.flat(),
255
+ m: op.cartesian.flat(),
157
256
  sign:
158
- (spec.parity === 'axial' ? Math.sign(det(op.matrix)) : 1) * (spec.timeParity === 'c' && op.timeReversal ? -1 : 1),
257
+ (spec.kind === 'axial' ? Math.sign(det(op.cartesian)) : 1) *
258
+ (spec.timeParity === 'odd' && op.timeReversal ? -1 : 1),
159
259
  }));
160
260
 
161
261
  const spanning: number[][] = [];
262
+ // The trace of the group average on the tensors of this index symmetry: the dimension of the
263
+ // invariant subspace, since the average is an orthogonal projector for orthogonal operations. The
264
+ // sums over the orbits of the index symmetry are an orthogonal basis of those tensors.
265
+ let trace = 0;
162
266
  for (let seed = 0; seed < dim; seed++) {
163
267
  const members = orbit(seed, rank, perms);
164
- if (seed !== Math.min(...members)) continue;
268
+ if (!members || seed !== Math.min(...members.map((m) => m.flat))) continue;
165
269
  const averaged = new Array<number>(dim).fill(0);
166
270
  for (const { m, sign } of ops) {
167
271
  for (let out = 0; out < dim; out++) {
168
272
  const o = d[out];
169
273
  let acc = 0;
170
274
  for (const s of members) {
171
- const i = d[s];
172
- let p = 1;
275
+ const i = d[s.flat];
276
+ let p = s.sign;
173
277
  for (let r = 0; r < rank; r++) p *= m[o[r] * 3 + i[r]];
174
278
  acc += p;
175
279
  }
@@ -177,11 +281,13 @@ function project(group: readonly Operation[], spec: TensorSpec, perms: number[][
177
281
  }
178
282
  }
179
283
  for (let k = 0; k < dim; k++) averaged[k] /= ops.length;
284
+ trace += members.reduce((sum, s) => sum + s.sign * averaged[s.flat], 0) / members.length;
180
285
  if (averaged.some((x) => Math.abs(x) >= EPSILON)) spanning.push(averaged);
181
286
  }
182
287
 
183
- const basis = reducedRowEchelon(spanning, dim).map((row) => row.map(snapEntry));
184
- const pivots = basis.map((row) => row.findIndex((x) => x !== 0));
288
+ const reduced = reduce(spanning, dim, deviation, Math.round(trace));
289
+ const basis = reduced.rows.map((row) => row.map(snapEntry));
290
+ const { pivots } = reduced;
185
291
  const relations: number[][] = [];
186
292
  for (let c = 0; c < dim; c++) {
187
293
  if (pivots.includes(c)) continue;
@@ -200,6 +306,30 @@ function project(group: readonly Operation[], spec: TensorSpec, perms: number[][
200
306
  });
201
307
  }
202
308
 
309
+ /**
310
+ * The reduced row echelon form of the projections, with as many pivots as the invariant subspace
311
+ * has dimensions (`rank`, the trace of the group average, exact for an exact group in any frame).
312
+ * For a group made exact from operations of a caller (`idealGroup`), which lies `deviation` from
313
+ * them, a component smaller than about that is not known to be independent: the pivots are first
314
+ * sought at 10 × `deviation` (at least `PIVOT_EPSILON`) and the threshold is lowered tenfold until
315
+ * the pivots reach the rank, so that the free parameters are those of the exact form unless one of
316
+ * its pivots is itself of the order of the deviation (#89). Should the pivots exceed the rank even
317
+ * at `PIVOT_EPSILON` (a component of the order of 10⁻⁷ in a general orientation), the threshold is
318
+ * raised instead until they reach it.
319
+ */
320
+ function reduce(spanning: readonly number[][], dim: number, deviation: number, rank: number) {
321
+ const start = Math.max(10 * deviation, PIVOT_EPSILON);
322
+ for (let threshold = start; threshold >= PIVOT_EPSILON * 0.999; threshold /= 10) {
323
+ const reduced = rowEchelonWithPivots(spanning, dim, threshold);
324
+ if (reduced.pivots.length === rank) return reduced;
325
+ }
326
+ for (let threshold = start * 10; threshold <= MAX_PIVOT_THRESHOLD; threshold *= 10) {
327
+ const reduced = rowEchelonWithPivots(spanning, dim, threshold);
328
+ if (reduced.pivots.length === rank) return reduced;
329
+ }
330
+ throw new Error(`The projections of a tensor form give no ${rank} independent components`);
331
+ }
332
+
203
333
  /**
204
334
  * An entry within 10⁻⁹ of a multiple of 1/12 is set to it, so that the exact values of the
205
335
  * crystallographic forms (0, ±1/2, ±1, ±2 for the 32 classes in all settings) come out exact instead
@@ -211,27 +341,27 @@ function snapEntry(x: number): number {
211
341
  }
212
342
 
213
343
  function specKey(spec: TensorSpec): string {
214
- return `${spec.rank}:${spec.parity}:${spec.timeParity}:${spec.intrinsic}`;
344
+ return `${spec.rank}:${spec.kind}:${spec.timeParity}:${spec.intrinsic}`;
215
345
  }
216
346
 
217
347
  const formCache = new Map<string, TensorForm>();
218
348
 
219
349
  /**
220
350
  * The form of a tensor under a point group, a crystal class or a magnetic point group, in the
221
- * requested setting (default: the default setting of the convention, `'itc'` unless given). The
222
- * tensor is given by its specification or by the name of a property in `TENSOR_PROPERTIES`
223
- * (`'piezoelectric'`). The convention matters only through the setting: the same setting index
224
- * gives the same form in both conventions (docs/conventions.md, "Tensors").
351
+ * requested setting (default: the ITC default setting). The tensor is given by its specification
352
+ * or by the name of a property in `TENSOR_PROPERTIES` (`'piezoelectric'`; docs/conventions.md,
353
+ * "Tensors").
225
354
  */
226
355
  export function tensorForm(
227
356
  key: MagneticPointGroupKey,
228
357
  spec: TensorSpec | TensorPropertyName,
229
358
  options?: SettingOptions,
230
359
  ): TensorForm {
360
+ checkOptions(options, SETTING_KEYS, 'tensorForm');
231
361
  const resolved = specOf(spec);
232
362
  const perms = checkSpec(resolved);
233
- const ops = operations(key, options); // checks key, convention and setting
234
- const index = options?.setting ?? settings(key, options?.convention).find((s) => s.isDefault)!.index;
363
+ const ops = operations(key, options); // checks key and setting
364
+ const index = settingIndexOf(key, options);
235
365
  const cacheKey = `${key}#${index}#${specKey(resolved)}`;
236
366
  let form = formCache.get(cacheKey);
237
367
  if (!form) {
@@ -244,42 +374,83 @@ export function tensorForm(
244
374
  /**
245
375
  * The form of a tensor under the group generated by `operations`, e.g. the site symmetry of a
246
376
  * surface normal (`siteSymmetry`) or the operations of a distorted crystal. The tensor is given by
247
- * its specification or by the name of a property in `TENSOR_PROPERTIES`. The operations are
248
- * Cartesian and orthogonal, and each says whether it is combined with time reversal
249
- * (`timeReversal`, `true` or `false`); a list that is not closed is closed first. Throws a
250
- * RangeError for a matrix that is not orthogonal and for an operation that does not say whether it
251
- * is combined with time reversal.
377
+ * its specification or by the name of a property in `TENSOR_PROPERTIES`. Each operation is read by
378
+ * its Cartesian matrix (`cartesian`), which must be orthogonal, and each says whether it is
379
+ * combined with time reversal (`timeReversal`, `true` or `false`). The list is closed into a group
380
+ * within the tolerance, and that group is made exact before the form is computed, so that matrices
381
+ * with rounding errors (e.g. rounded to 6 decimals) give the form of the exact group, with its
382
+ * number of free parameters (docs/conventions.md, "Tolerances"). Throws a RangeError for an
383
+ * operation without an orthogonal Cartesian matrix (`cartesian`; within the tolerance), for an
384
+ * operation that does not say whether it is combined with time reversal, and for operations that
385
+ * generate no finite group within the tolerance.
252
386
  */
253
- export function tensorFormOf(operations: readonly Operation[], spec: TensorSpec | TensorPropertyName): TensorForm {
387
+ export function tensorFormOf(
388
+ operations: readonly CartesianOperation[],
389
+ spec: TensorSpec | TensorPropertyName,
390
+ options?: ToleranceOptions,
391
+ ): TensorForm {
392
+ checkOptions(options, TOLERANCE_KEYS, 'tensorFormOf');
254
393
  const resolved = specOf(spec);
255
394
  const perms = checkSpec(resolved);
256
- operations.forEach((op, i) => checkOrthogonal(op.matrix, `Operation ${i}`));
395
+ return formOfGroup(callerGroup(operations, options), resolved, perms);
396
+ }
397
+
398
+ /**
399
+ * The operations of a caller, checked (orthogonal within the tolerance, time reversal given),
400
+ * closed within the tolerance and made exact: the group the functions for a list of operations
401
+ * compute with.
402
+ */
403
+ export function callerGroup(operations: readonly CartesianOperation[], options?: ToleranceOptions): IdealGroup {
404
+ const tolerance = toleranceOf(options, 'operations');
405
+ checkCartesianOperations(operations, tolerance);
257
406
  checkTimeReversal(operations);
258
- return project(closeGroup(operations), resolved, perms);
407
+ return idealGroup(operations, tolerance);
408
+ }
409
+
410
+ /** The form of a tensor (checked specification and its permutations) under a group made exact. */
411
+ export function formOfGroup(group: IdealGroup, spec: TensorSpec, perms: readonly SignedPermutation[]): TensorForm {
412
+ return project(group.operations, spec, perms, group.deviation);
413
+ }
414
+
415
+ /** The specification of a tensor given by its specification or name, checked, with its permutations. */
416
+ export function checkedSpec(spec: TensorSpec | TensorPropertyName): {
417
+ spec: TensorSpec;
418
+ perms: readonly SignedPermutation[];
419
+ } {
420
+ const resolved = specOf(spec);
421
+ return { spec: resolved, perms: checkSpec(resolved) };
259
422
  }
260
423
 
261
424
  /**
262
- * The components of `tensor` (length 3^rank, flat index) in a frame rotated by the orthogonal
425
+ * The tensor `tensor` in a frame rotated by the orthogonal
263
426
  * matrix `rotation`: t′_{i…} = f·Σ R_{ia}…R_{…}·t_{a…}, with f = det(R) for an axial tensor and 1
264
427
  * for a polar one. To express a tensor in the frame of a surface, pass the matrix whose rows are
265
428
  * the new axes in the old frame. Time parity plays no part. The kind of tensor is given by its
266
- * specification or by the name of a property in `TENSOR_PROPERTIES`.
429
+ * specification or by the name of a property in `TENSOR_PROPERTIES`. Throws a RangeError for a
430
+ * rotation that is not orthogonal within the tolerance (per matrix entry of RᵀR).
267
431
  */
268
432
  export function rotateTensor(
269
433
  tensor: readonly number[],
270
434
  rotation: Mat3,
271
- specOrName: TensorSpec | TensorPropertyName,
272
- ): number[] {
273
- const spec = specOf(specOrName);
274
- checkSpec(spec);
275
- checkOrthogonal(rotation, 'The rotation');
276
- const { rank } = spec;
435
+ spec: TensorSpec | TensorPropertyName,
436
+ options?: ToleranceOptions,
437
+ ): readonly number[] {
438
+ checkOptions(options, TOLERANCE_KEYS, 'rotateTensor');
439
+ const resolved = specOf(spec);
440
+ checkSpec(resolved);
441
+ if (!Array.isArray(rotation) || rotation.length !== 3) {
442
+ throw new RangeError(`The rotation must be a 3 × 3 matrix, not ${shown(rotation)}`);
443
+ }
444
+ rotation.forEach((row, i) => checkNumbers(row, 3, `Row ${i} of the rotation`));
445
+ checkOrthogonalWithin(rotation, 'The rotation', toleranceOf(options, 'operations'));
446
+ const { rank } = resolved;
277
447
  const dim = 3 ** rank;
278
- if (tensor.length !== dim)
279
- throw new RangeError(`A tensor of rank ${rank} has ${dim} components, not ${tensor.length}`);
448
+ if (!Array.isArray(tensor) || tensor.length !== dim)
449
+ throw new RangeError(`A tensor of rank ${rank} has ${dim} components, not ${shown(tensor)}`);
450
+ checkNumbers(tensor, dim, 'The tensor');
280
451
  const d = digits(rank);
281
452
  const m = rotation.flat();
282
- const f = spec.parity === 'axial' ? Math.sign(det(rotation)) : 1;
453
+ const f = resolved.kind === 'axial' ? Math.sign(det(rotation)) : 1;
283
454
  const out = new Array<number>(dim).fill(0);
284
455
  for (let o = 0; o < dim; o++) {
285
456
  let acc = 0;
@@ -291,5 +462,5 @@ export function rotateTensor(
291
462
  }
292
463
  out[o] = f * acc;
293
464
  }
294
- return out;
465
+ return deepFreeze(out);
295
466
  }
@@ -11,42 +11,62 @@
11
11
  * (docs/conventions.md, "Two entry points").
12
12
  */
13
13
 
14
- import type { Operation } from '../operations.js';
14
+ import { checkOptions, shown, TOLERANCE_KEYS } from '../checks.js';
15
+ import type { CartesianOperation } from '../operations.js';
15
16
  import type { MagneticPointGroupKey } from '../pointGroups/pointGroups.js';
16
17
  import { TENSOR_PROPERTY_NAMES, specOf, type TensorPropertyName } from './propertyTable.js';
17
- import { tensorForm, tensorFormOf } from './tensorForm.js';
18
+ import { callerGroup, checkedSpec, formOfGroup, tensorForm } from './tensorForm.js';
19
+ import type { ToleranceOptions } from '../tolerances.js';
20
+ import type { IdealGroup } from '../closure.js';
21
+ import { deepFreeze } from '../freeze.js';
18
22
 
19
23
  /** A name of the table; anything else, a specification included, is an error here. */
20
24
  function named(name: TensorPropertyName): TensorPropertyName {
21
- if (typeof name !== 'string') throw new RangeError(`Unknown tensor property: ${JSON.stringify(name)}`);
25
+ if (typeof name !== 'string') throw new RangeError(`Unknown tensor property: ${shown(name)}`);
22
26
  specOf(name);
23
27
  return name;
24
28
  }
25
29
 
26
30
  /**
27
31
  * True if the point group allows the named property, e.g. `allowsProperty('-6m2', 'piezoelectric')`.
28
- * It does not depend on setting or convention. Throws a RangeError for an unknown key or name.
32
+ * It does not depend on the setting. Throws a RangeError for an unknown key or name.
29
33
  */
30
34
  export function allowsProperty(key: MagneticPointGroupKey, name: TensorPropertyName): boolean {
31
35
  return tensorForm(key, named(name), { setting: 1 }).basis.length > 0;
32
36
  }
33
37
 
34
38
  /** The named properties the point group allows, in the order of `TENSOR_PROPERTIES`. */
35
- export function allowedProperties(key: MagneticPointGroupKey): TensorPropertyName[] {
36
- return TENSOR_PROPERTY_NAMES.filter((name) => allowsProperty(key, name));
39
+ export function allowedProperties(key: MagneticPointGroupKey): readonly TensorPropertyName[] {
40
+ return deepFreeze(TENSOR_PROPERTY_NAMES.filter((name) => allowsProperty(key, name)));
37
41
  }
38
42
 
39
43
  /**
40
44
  * True if the group generated by `operations` allows the named property, e.g. for the site
41
- * symmetry of a surface normal or the operations of a distorted crystal. The matrices must be
42
- * Cartesian and orthogonal; a list that is not closed is closed first. Throws a RangeError for a matrix that is not
43
- * orthogonal and for an operation that does not say whether it is combined with time reversal.
45
+ * symmetry of a surface normal or the operations of a distorted crystal. The operations are read
46
+ * and closed as `tensorFormOf` reads them, within the tolerance, and it throws a RangeError as
47
+ * `tensorFormOf` does.
44
48
  */
45
- export function allowsPropertyOf(operations: readonly Operation[], name: TensorPropertyName): boolean {
46
- return tensorFormOf(operations, named(name)).basis.length > 0;
49
+ export function allowsPropertyOf(
50
+ operations: readonly CartesianOperation[],
51
+ name: TensorPropertyName,
52
+ options?: ToleranceOptions,
53
+ ): boolean {
54
+ checkOptions(options, TOLERANCE_KEYS, 'allowsPropertyOf');
55
+ const checked = named(name);
56
+ return allows(callerGroup(operations, options), checked);
47
57
  }
48
58
 
49
- /** The named properties the group generated by `operations` allows. Checks the operations as `allowsPropertyOf`. */
50
- export function allowedPropertiesOf(operations: readonly Operation[]): TensorPropertyName[] {
51
- return TENSOR_PROPERTY_NAMES.filter((name) => allowsPropertyOf(operations, name));
59
+ /** The named properties the group generated by `operations` allows. Reads the operations as `allowsPropertyOf`. */
60
+ export function allowedPropertiesOf(
61
+ operations: readonly CartesianOperation[],
62
+ options?: ToleranceOptions,
63
+ ): readonly TensorPropertyName[] {
64
+ checkOptions(options, TOLERANCE_KEYS, 'allowedPropertiesOf');
65
+ const group = callerGroup(operations, options);
66
+ return deepFreeze(TENSOR_PROPERTY_NAMES.filter((name) => allows(group, name)));
67
+ }
68
+
69
+ function allows(group: IdealGroup, name: TensorPropertyName): boolean {
70
+ const { spec, perms } = checkedSpec(name);
71
+ return formOfGroup(group, spec, perms).basis.length > 0;
52
72
  }