symcrys 0.10.1 → 0.12.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 (248) 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/coordinates.d.ts +143 -0
  15. package/dist/coordinates.d.ts.map +1 -0
  16. package/dist/coordinates.js +198 -0
  17. package/dist/coordinates.js.map +1 -0
  18. package/dist/crystalClasses.d.ts +170 -117
  19. package/dist/crystalClasses.d.ts.map +1 -1
  20. package/dist/crystalClasses.js +190 -121
  21. package/dist/crystalClasses.js.map +1 -1
  22. package/dist/freeze.d.ts +7 -0
  23. package/dist/freeze.d.ts.map +1 -0
  24. package/dist/freeze.js +15 -0
  25. package/dist/freeze.js.map +1 -0
  26. package/dist/index.d.ts +13 -10
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +5 -3
  29. package/dist/index.js.map +1 -1
  30. package/dist/indices.d.ts +30 -20
  31. package/dist/indices.d.ts.map +1 -1
  32. package/dist/indices.js +101 -31
  33. package/dist/indices.js.map +1 -1
  34. package/dist/lattice.d.ts +15 -1
  35. package/dist/lattice.d.ts.map +1 -1
  36. package/dist/lattice.js +30 -1
  37. package/dist/lattice.js.map +1 -1
  38. package/dist/linalg.d.ts +2 -2
  39. package/dist/linalg.d.ts.map +1 -1
  40. package/dist/linalg.js +3 -3
  41. package/dist/linalg.js.map +1 -1
  42. package/dist/magnetic.d.ts +14 -10
  43. package/dist/magnetic.d.ts.map +1 -1
  44. package/dist/magnetic.js +10 -6
  45. package/dist/magnetic.js.map +1 -1
  46. package/dist/notation.d.ts +16 -0
  47. package/dist/notation.d.ts.map +1 -0
  48. package/dist/notation.js +66 -0
  49. package/dist/notation.js.map +1 -0
  50. package/dist/operations.d.ts +40 -26
  51. package/dist/operations.d.ts.map +1 -1
  52. package/dist/operations.js +48 -60
  53. package/dist/operations.js.map +1 -1
  54. package/dist/plane.d.ts +297 -80
  55. package/dist/plane.d.ts.map +1 -1
  56. package/dist/plane.js +289 -94
  57. package/dist/plane.js.map +1 -1
  58. package/dist/pointGroups/crystalForms.d.ts +6 -21
  59. package/dist/pointGroups/crystalForms.d.ts.map +1 -1
  60. package/dist/pointGroups/crystalForms.js +9 -16
  61. package/dist/pointGroups/crystalForms.js.map +1 -1
  62. package/dist/pointGroups/crystalFormsData.d.ts +4 -1
  63. package/dist/pointGroups/crystalFormsData.d.ts.map +1 -1
  64. package/dist/pointGroups/crystalFormsData.js +64 -1
  65. package/dist/pointGroups/crystalFormsData.js.map +1 -1
  66. package/dist/pointGroups/elements.d.ts +19 -17
  67. package/dist/pointGroups/elements.d.ts.map +1 -1
  68. package/dist/pointGroups/elements.js +65 -83
  69. package/dist/pointGroups/elements.js.map +1 -1
  70. package/dist/pointGroups/identify.d.ts +10 -18
  71. package/dist/pointGroups/identify.d.ts.map +1 -1
  72. package/dist/pointGroups/identify.js +27 -79
  73. package/dist/pointGroups/identify.js.map +1 -1
  74. package/dist/pointGroups/itaOrder.d.ts +15 -14
  75. package/dist/pointGroups/itaOrder.d.ts.map +1 -1
  76. package/dist/pointGroups/itaOrder.js +116 -94
  77. package/dist/pointGroups/itaOrder.js.map +1 -1
  78. package/dist/pointGroups/itcData.d.ts +96 -0
  79. package/dist/pointGroups/itcData.d.ts.map +1 -0
  80. package/dist/pointGroups/itcData.js +336 -0
  81. package/dist/pointGroups/itcData.js.map +1 -0
  82. package/dist/pointGroups/itcMagneticData.d.ts +55 -0
  83. package/dist/pointGroups/itcMagneticData.d.ts.map +1 -0
  84. package/dist/pointGroups/itcMagneticData.js +172 -0
  85. package/dist/pointGroups/itcMagneticData.js.map +1 -0
  86. package/dist/pointGroups/keys.d.ts +32 -0
  87. package/dist/pointGroups/keys.d.ts.map +1 -0
  88. package/dist/pointGroups/keys.js +120 -0
  89. package/dist/pointGroups/keys.js.map +1 -0
  90. package/dist/pointGroups/magneticSymbols.d.ts +6 -5
  91. package/dist/pointGroups/magneticSymbols.d.ts.map +1 -1
  92. package/dist/pointGroups/magneticSymbols.js +15 -12
  93. package/dist/pointGroups/magneticSymbols.js.map +1 -1
  94. package/dist/pointGroups/orbits.d.ts +23 -42
  95. package/dist/pointGroups/orbits.d.ts.map +1 -1
  96. package/dist/pointGroups/orbits.js +20 -62
  97. package/dist/pointGroups/orbits.js.map +1 -1
  98. package/dist/pointGroups/pointGroups.d.ts +145 -68
  99. package/dist/pointGroups/pointGroups.d.ts.map +1 -1
  100. package/dist/pointGroups/pointGroups.js +455 -243
  101. package/dist/pointGroups/pointGroups.js.map +1 -1
  102. package/dist/pointGroups/properties.d.ts +21 -22
  103. package/dist/pointGroups/properties.d.ts.map +1 -1
  104. package/dist/pointGroups/properties.js +40 -37
  105. package/dist/pointGroups/properties.js.map +1 -1
  106. package/dist/pointGroups/seitzData.d.ts +26 -0
  107. package/dist/pointGroups/seitzData.d.ts.map +1 -0
  108. package/dist/pointGroups/seitzData.js +130 -0
  109. package/dist/pointGroups/seitzData.js.map +1 -0
  110. package/dist/pointGroups/shubnikovData.d.ts +11 -0
  111. package/dist/pointGroups/shubnikovData.d.ts.map +1 -0
  112. package/dist/pointGroups/shubnikovData.js +101 -0
  113. package/dist/pointGroups/shubnikovData.js.map +1 -0
  114. package/dist/pointGroups/wyckoff.d.ts +70 -40
  115. package/dist/pointGroups/wyckoff.d.ts.map +1 -1
  116. package/dist/pointGroups/wyckoff.js +98 -91
  117. package/dist/pointGroups/wyckoff.js.map +1 -1
  118. package/dist/pointGroups/wyckoffData.d.ts +7 -4
  119. package/dist/pointGroups/wyckoffData.d.ts.map +1 -1
  120. package/dist/pointGroups/wyckoffData.js +789 -168
  121. package/dist/pointGroups/wyckoffData.js.map +1 -1
  122. package/dist/siteSymmetry.d.ts +8 -16
  123. package/dist/siteSymmetry.d.ts.map +1 -1
  124. package/dist/siteSymmetry.js +34 -24
  125. package/dist/siteSymmetry.js.map +1 -1
  126. package/dist/spaceGroups/affine.d.ts.map +1 -1
  127. package/dist/spaceGroups/affine.js +3 -2
  128. package/dist/spaceGroups/affine.js.map +1 -1
  129. package/dist/spaceGroups/cell.d.ts +43 -31
  130. package/dist/spaceGroups/cell.d.ts.map +1 -1
  131. package/dist/spaceGroups/cell.js +173 -85
  132. package/dist/spaceGroups/cell.js.map +1 -1
  133. package/dist/spaceGroups/describe.d.ts +13 -10
  134. package/dist/spaceGroups/describe.d.ts.map +1 -1
  135. package/dist/spaceGroups/describe.js +11 -10
  136. package/dist/spaceGroups/describe.js.map +1 -1
  137. package/dist/spaceGroups/planeGroupData.d.ts +3 -1
  138. package/dist/spaceGroups/planeGroupData.d.ts.map +1 -1
  139. package/dist/spaceGroups/planeGroupData.js +25 -8
  140. package/dist/spaceGroups/planeGroupData.js.map +1 -1
  141. package/dist/spaceGroups/planeGroups.d.ts.map +1 -1
  142. package/dist/spaceGroups/planeGroups.js +2 -1
  143. package/dist/spaceGroups/planeGroups.js.map +1 -1
  144. package/dist/spaceGroups/planeLattice.d.ts +78 -0
  145. package/dist/spaceGroups/planeLattice.d.ts.map +1 -0
  146. package/dist/spaceGroups/planeLattice.js +143 -0
  147. package/dist/spaceGroups/planeLattice.js.map +1 -0
  148. package/dist/spaceGroups/planePointGroups.d.ts +6 -2
  149. package/dist/spaceGroups/planePointGroups.d.ts.map +1 -1
  150. package/dist/spaceGroups/planePointGroups.js +14 -2
  151. package/dist/spaceGroups/planePointGroups.js.map +1 -1
  152. package/dist/spaceGroups/positions.d.ts +48 -19
  153. package/dist/spaceGroups/positions.d.ts.map +1 -1
  154. package/dist/spaceGroups/positions.js +166 -67
  155. package/dist/spaceGroups/positions.js.map +1 -1
  156. package/dist/spaceGroups/rational.d.ts +18 -0
  157. package/dist/spaceGroups/rational.d.ts.map +1 -1
  158. package/dist/spaceGroups/rational.js +58 -0
  159. package/dist/spaceGroups/rational.js.map +1 -1
  160. package/dist/tensors/propertyTable.d.ts +3 -1
  161. package/dist/tensors/propertyTable.d.ts.map +1 -1
  162. package/dist/tensors/propertyTable.js +17 -10
  163. package/dist/tensors/propertyTable.js.map +1 -1
  164. package/dist/tensors/rowEchelon.d.ts +3 -3
  165. package/dist/tensors/rowEchelon.d.ts.map +1 -1
  166. package/dist/tensors/rowEchelon.js +4 -4
  167. package/dist/tensors/rowEchelon.js.map +1 -1
  168. package/dist/tensors/tensorForm.d.ts +75 -34
  169. package/dist/tensors/tensorForm.d.ts.map +1 -1
  170. package/dist/tensors/tensorForm.js +171 -67
  171. package/dist/tensors/tensorForm.js.map +1 -1
  172. package/dist/tensors/tensorProperties.d.ts +10 -9
  173. package/dist/tensors/tensorProperties.d.ts.map +1 -1
  174. package/dist/tensors/tensorProperties.js +22 -12
  175. package/dist/tensors/tensorProperties.js.map +1 -1
  176. package/dist/tensors/voigt.d.ts +56 -0
  177. package/dist/tensors/voigt.d.ts.map +1 -0
  178. package/dist/tensors/voigt.js +188 -0
  179. package/dist/tensors/voigt.js.map +1 -0
  180. package/dist/tolerances.d.ts +56 -1
  181. package/dist/tolerances.d.ts.map +1 -1
  182. package/dist/tolerances.js +51 -1
  183. package/dist/tolerances.js.map +1 -1
  184. package/dist/version.d.ts +1 -1
  185. package/dist/version.js +1 -1
  186. package/package.json +1 -1
  187. package/src/cell.ts +78 -51
  188. package/src/checks.ts +84 -0
  189. package/src/closure.ts +214 -0
  190. package/src/coordinates.ts +370 -0
  191. package/src/crystalClasses.ts +271 -157
  192. package/src/freeze.ts +14 -0
  193. package/src/index.ts +33 -12
  194. package/src/indices.ts +102 -42
  195. package/src/lattice.ts +41 -1
  196. package/src/linalg.ts +3 -3
  197. package/src/magnetic.ts +24 -11
  198. package/src/notation.ts +73 -0
  199. package/src/operations.ts +78 -71
  200. package/src/plane.ts +531 -151
  201. package/src/pointGroups/crystalForms.ts +11 -44
  202. package/src/pointGroups/crystalFormsData.ts +66 -1
  203. package/src/pointGroups/elements.ts +82 -92
  204. package/src/pointGroups/identify.ts +38 -97
  205. package/src/pointGroups/itaOrder.ts +117 -101
  206. package/src/pointGroups/itcData.ts +404 -0
  207. package/src/pointGroups/itcMagneticData.ts +206 -0
  208. package/src/pointGroups/keys.ts +144 -0
  209. package/src/pointGroups/magneticSymbols.ts +18 -17
  210. package/src/pointGroups/orbits.ts +41 -89
  211. package/src/pointGroups/pointGroups.ts +593 -298
  212. package/src/pointGroups/properties.ts +44 -40
  213. package/src/pointGroups/seitzData.ts +147 -0
  214. package/src/pointGroups/shubnikovData.ts +103 -0
  215. package/src/pointGroups/wyckoff.ts +137 -112
  216. package/src/pointGroups/wyckoffData.ts +793 -170
  217. package/src/siteSymmetry.ts +43 -40
  218. package/src/spaceGroups/affine.ts +3 -2
  219. package/src/spaceGroups/cell.ts +227 -116
  220. package/src/spaceGroups/describe.ts +23 -19
  221. package/src/spaceGroups/planeGroupData.ts +28 -9
  222. package/src/spaceGroups/planeGroups.ts +2 -1
  223. package/src/spaceGroups/planeLattice.ts +208 -0
  224. package/src/spaceGroups/planePointGroups.ts +23 -3
  225. package/src/spaceGroups/positions.ts +202 -78
  226. package/src/spaceGroups/rational.ts +59 -0
  227. package/src/tensors/propertyTable.ts +26 -14
  228. package/src/tensors/rowEchelon.ts +4 -3
  229. package/src/tensors/tensorForm.ts +258 -88
  230. package/src/tensors/tensorProperties.ts +34 -14
  231. package/src/tensors/voigt.ts +268 -0
  232. package/src/tolerances.ts +66 -1
  233. package/src/version.ts +1 -1
  234. package/dist/pointGroups/data.d.ts +0 -75
  235. package/dist/pointGroups/data.d.ts.map +0 -1
  236. package/dist/pointGroups/data.js +0 -212
  237. package/dist/pointGroups/data.js.map +0 -1
  238. package/dist/pointGroups/magneticData.d.ts +0 -66
  239. package/dist/pointGroups/magneticData.d.ts.map +0 -1
  240. package/dist/pointGroups/magneticData.js +0 -184
  241. package/dist/pointGroups/magneticData.js.map +0 -1
  242. package/dist/spaceGroups/snap.d.ts +0 -20
  243. package/dist/spaceGroups/snap.d.ts.map +0 -1
  244. package/dist/spaceGroups/snap.js +0 -103
  245. package/dist/spaceGroups/snap.js.map +0 -1
  246. package/src/pointGroups/data.ts +0 -305
  247. package/src/pointGroups/magneticData.ts +0 -328
  248. package/src/spaceGroups/snap.ts +0 -121
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Voigt's two-index notation of tensors with symmetric index pairs, as ITC Vol. D (2013) writes it
3
+ * (§ 1.1.4.10, pp. 24–27; eq. (1.5.7.4), p. 134): an index pair ij becomes one index α = 1…6 for
4
+ * 11, 22, 33, 23, 31, 12 (eq. (1.1.4.6)), and the components with α = 4, 5, 6 carry a factor 1 or 2
5
+ * per pair, as ITC defines the quantity (#137 E3, decided by Thomas on 2026-11-25).
6
+ */
7
+ import { checkOptions, shown } from '../checks.js';
8
+ import { deepFreeze } from '../freeze.js';
9
+ import { toleranceOf } from '../tolerances.js';
10
+ import {} from './propertyTable.js';
11
+ import { checkedSpec } from './tensorForm.js';
12
+ /** The index pair of α = 1…6, eq. (1.1.4.6): 11, 22, 33, 23, 31, 12. */
13
+ const PAIR = [
14
+ [0, 0],
15
+ [1, 1],
16
+ [2, 2],
17
+ [1, 2],
18
+ [2, 0],
19
+ [0, 1],
20
+ ];
21
+ /** α (0-based) of an index pair. */
22
+ const ALPHA = [
23
+ [0, 5, 4],
24
+ [5, 1, 3],
25
+ [4, 3, 2],
26
+ ];
27
+ /**
28
+ * ITC's factors of the named properties that ITC writes in Voigt's notation with one convention
29
+ * only; 'piezoelectric' has none, since its tensor is also that of second-harmonic generation, which
30
+ * ITC contracts without the factor 2 (§ 1.7.2.2.1.3, p. 185).
31
+ */
32
+ const PROPERTY_FACTORS = {
33
+ piezomagnetic: [2],
34
+ };
35
+ function layoutOf(spec, who) {
36
+ const { rank, intrinsic } = spec;
37
+ const symmetric = ['symmetric-first-pair', 'symmetric-last-pair', 'totally-symmetric'];
38
+ if (rank === 2 && symmetric.includes(intrinsic))
39
+ return { rank, pairs: [0], shape: [6] };
40
+ if (rank === 3 && (intrinsic === 'symmetric-last-pair' || intrinsic === 'totally-symmetric')) {
41
+ return { rank, pairs: [1], shape: [3, 6] };
42
+ }
43
+ if (rank === 3 && intrinsic === 'symmetric-first-pair')
44
+ return { rank, pairs: [0], shape: [6, 3] };
45
+ if (rank === 4 && ['symmetric-pairs', 'symmetric-pairs-exchangeable', 'totally-symmetric'].includes(intrinsic)) {
46
+ return { rank, pairs: [0, 2], shape: [6, 6] };
47
+ }
48
+ throw new RangeError(`${who}: Voigt's notation needs a symmetric index pair (rank 2 'symmetric-first-pair', 'symmetric-last-pair' ` +
49
+ `or 'totally-symmetric'; rank 3 'symmetric-last-pair', 'symmetric-first-pair' or 'totally-symmetric'; rank 4 ` +
50
+ `'symmetric-pairs', 'symmetric-pairs-exchangeable' or 'totally-symmetric'), not rank ${rank} '${intrinsic}'`);
51
+ }
52
+ function factorsOf(specOrName, layout, options, who) {
53
+ const given = options?.factors;
54
+ const factors = given !== undefined ? given : typeof specOrName === 'string' ? PROPERTY_FACTORS[specOrName] : undefined;
55
+ if (factors === undefined) {
56
+ const piezoelectric = specOrName === 'piezoelectric'
57
+ ? ': [2] for the piezoelectric d (ITC Vol. D § 1.1.4.10.3), [1] for second-harmonic generation (§ 1.7.2.2.1.3)'
58
+ : ', 2 where ITC sums the two symmetric components (strain, d, s), 1 where it takes one (stress, e, c)';
59
+ throw new RangeError(`${who}: give the factors of the ${layout.pairs.length === 1 ? 'index pair' : 'index pairs'} ` +
60
+ `({ factors: [${layout.pairs.map(() => '1 or 2').join(', ')}] })${piezoelectric}`);
61
+ }
62
+ if (!Array.isArray(factors) || factors.length !== layout.pairs.length || !factors.every((f) => f === 1 || f === 2)) {
63
+ throw new RangeError(`${who}: factors must be ${layout.pairs.length} of 1 or 2, one per contracted index pair, not ${shown(factors)}`);
64
+ }
65
+ return factors;
66
+ }
67
+ const flatOf = (digits) => digits.reduce((s, d) => s * 3 + d, 0);
68
+ const digitsOf = (flat, rank) => Array.from({ length: rank }, (_, k) => Math.floor(flat / 3 ** (rank - 1 - k)) % 3);
69
+ /** Checks that a tensor has its intrinsic symmetry, within the tolerance relative to its largest component. */
70
+ function checkSymmetry(t, rank, perms, tolerance, who) {
71
+ const scale = Math.max(...t.map(Math.abs));
72
+ for (let o = 0; o < t.length; o++) {
73
+ const d = digitsOf(o, rank);
74
+ for (const { perm, sign } of perms) {
75
+ const other = flatOf(perm.map((k) => d[k]));
76
+ if (Math.abs(t[o] - sign * t[other]) > tolerance * scale) {
77
+ throw new RangeError(`${who}: the tensor lacks the index symmetry of its specification (component ${o} is ${t[o]}, ` +
78
+ `its partner ${other} ${t[other]})`);
79
+ }
80
+ }
81
+ }
82
+ }
83
+ /** The Voigt indices (0-based) of a flat tensor index, and the product of the factors of its pairs. */
84
+ function voigtIndex(flat, layout, factors) {
85
+ const d = digitsOf(flat, layout.rank);
86
+ const index = [];
87
+ let factor = 1;
88
+ let k = 0;
89
+ layout.pairs.forEach((first, p) => {
90
+ while (k < first)
91
+ index.push(d[k++]);
92
+ const alpha = ALPHA[d[first]][d[first + 1]];
93
+ index.push(alpha);
94
+ if (alpha >= 3)
95
+ factor *= factors[p];
96
+ k = first + 2;
97
+ });
98
+ while (k < layout.rank)
99
+ index.push(d[k++]);
100
+ return { index, factor };
101
+ }
102
+ function resolve(specOrName, options, who) {
103
+ checkOptions(options, ['factors', 'tolerance'], who);
104
+ const { spec, perms } = checkedSpec(specOrName);
105
+ const layout = layoutOf(spec, who);
106
+ const factors = factorsOf(specOrName, layout, options, who);
107
+ const tolerance = toleranceOf(options, 'metric');
108
+ return { spec, perms, layout, factors, tolerance };
109
+ }
110
+ /**
111
+ * A tensor in Voigt's two-index notation (ITC Vol. D (2013) § 1.1.4.10, pp. 24–27): each contracted
112
+ * index pair ij becomes α = 1…6 for 11, 22, 33, 23, 31, 12 (eq. (1.1.4.6)), and a component with
113
+ * α = 4, 5, 6 is the tensor component times the factor of its pair (`factors`). A symmetric tensor of
114
+ * rank 2 gives 6 components, one of rank 3 symmetric in its last pair a 3 × 6 matrix (d_iα), in its
115
+ * first pair a 6 × 3 matrix (e_αk), one of rank 4 a 6 × 6 matrix (s_αβ, c_αβ). Throws a RangeError for a specification
116
+ * without a symmetric index pair, factors that are missing or not one 1 or 2 per pair, and a tensor
117
+ * that lacks the index symmetry of its specification (within the tolerance, relative to its largest
118
+ * component).
119
+ *
120
+ * @remarks The tensor is flat, 3^rank components, as `tensorForm` gives a basis tensor.
121
+ */
122
+ export function toVoigt(tensor, specOrName, options) {
123
+ const who = 'toVoigt';
124
+ const { spec, perms, layout, factors, tolerance } = resolve(specOrName, options, who);
125
+ const dim = 3 ** spec.rank;
126
+ if (!Array.isArray(tensor) ||
127
+ tensor.length !== dim ||
128
+ !tensor.every((x) => typeof x === 'number' && Number.isFinite(x))) {
129
+ throw new RangeError(`${who}: a tensor of rank ${spec.rank} is ${dim} finite numbers, not ${shown(tensor)}`);
130
+ }
131
+ checkSymmetry(tensor, spec.rank, perms, tolerance, who);
132
+ const out = layout.shape.length === 1
133
+ ? new Array(6).fill(0)
134
+ : Array.from({ length: layout.shape[0] }, () => new Array(layout.shape[1]).fill(0));
135
+ for (let o = 0; o < dim; o++) {
136
+ const { index, factor } = voigtIndex(o, layout, factors);
137
+ // The representative index of each Voigt cell: the pair in the order of PAIR.
138
+ const d = digitsOf(o, spec.rank);
139
+ const canonical = layout.pairs.every((first) => {
140
+ const alpha = ALPHA[d[first]][d[first + 1]];
141
+ return PAIR[alpha][0] === d[first] && PAIR[alpha][1] === d[first + 1];
142
+ });
143
+ if (!canonical)
144
+ continue;
145
+ const value = factor * tensor[o] + 0;
146
+ if (index.length === 1)
147
+ out[index[0]] = value;
148
+ else
149
+ out[index[0]][index[1]] = value;
150
+ }
151
+ return deepFreeze(out);
152
+ }
153
+ /**
154
+ * The tensor of a Voigt array (`toVoigt` in reverse): each tensor component is the Voigt component
155
+ * of its index pairs over the factors of the pairs, so that the tensor has the index symmetry of its
156
+ * specification. Throws a RangeError for a Voigt array of
157
+ * the wrong shape, and for one that is no tensor of the specification (for rank 4 with interchangeable
158
+ * pairs a 6 × 6 matrix that is not symmetric, for a totally symmetric tensor one whose
159
+ * components do not agree), within the tolerance relative to its largest component.
160
+ *
161
+ * @remarks The tensor is flat, 3^rank components, as `tensorForm` gives a basis tensor.
162
+ */
163
+ export function fromVoigt(voigt, specOrName, options) {
164
+ const who = 'fromVoigt';
165
+ const { spec, perms, layout, factors, tolerance } = resolve(specOrName, options, who);
166
+ const shapeOk = layout.shape.length === 1
167
+ ? Array.isArray(voigt) && voigt.length === 6 && voigt.every((x) => typeof x === 'number' && Number.isFinite(x))
168
+ : Array.isArray(voigt) &&
169
+ voigt.length === layout.shape[0] &&
170
+ voigt.every((row) => Array.isArray(row) &&
171
+ row.length === layout.shape[1] &&
172
+ row.every((x) => typeof x === 'number' && Number.isFinite(x)));
173
+ if (!shapeOk) {
174
+ throw new RangeError(`${who}: a Voigt array of shape ${layout.shape.join(' × ')} of finite numbers, not ${shown(voigt)}`);
175
+ }
176
+ const dim = 3 ** spec.rank;
177
+ const out = new Array(dim);
178
+ for (let o = 0; o < dim; o++) {
179
+ const { index, factor } = voigtIndex(o, layout, factors);
180
+ const v = index.length === 1
181
+ ? voigt[index[0]]
182
+ : voigt[index[0]][index[1]];
183
+ out[o] = v / factor + 0;
184
+ }
185
+ checkSymmetry(out, spec.rank, perms, tolerance, who);
186
+ return deepFreeze(out);
187
+ }
188
+ //# sourceMappingURL=voigt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"voigt.js","sourceRoot":"","sources":["../../src/tensors/voigt.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EA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AC,CAAC,CAAC,CAAC;YACxC,CAAC,CAAE,KAAwC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACpE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC;IAC1B,CAAC;IACD,aAAa,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;IACrD,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
@@ -1,11 +1,66 @@
1
1
  /**
2
2
  * Numeric tolerances, in one place. The values differ on purpose: each gates a different quantity.
3
+ * The tolerance a caller passes is one contract for every function (docs/conventions.md,
4
+ * "Tolerances"): one option, one metric per kind of object, one check.
5
+ */
6
+ /**
7
+ * The default tolerance of every function that compares numbers a caller passes in (operations,
8
+ * directions, points): two numbers closer than this count as equal, in the metric of their kind
9
+ * (docs/conventions.md, "Tolerances").
3
10
  */
4
- /** Two matrix entries (or vector components) closer than this are equal. */
5
11
  export declare const EPSILON = 0.000001;
6
12
  /**
7
13
  * During group closure, an entry closer than this to one of the exact values in `SNAP_VALUES`
8
14
  * (linalg.ts) is replaced by it, so rounding errors cannot accumulate over repeated products.
9
15
  */
10
16
  export declare const SNAP_EPSILON = 1e-9;
17
+ /** The tolerance of a function that compares numbers a caller passes in. */
18
+ export interface ToleranceOptions {
19
+ /**
20
+ * Two numbers closer than this count as equal; default `EPSILON` (10⁻⁶). It is measured per
21
+ * matrix entry for operations, as the distance of unit vectors for directions and per coordinate
22
+ * modulo the lattice for points (docs/conventions.md, "Tolerances").
23
+ */
24
+ readonly tolerance?: number;
25
+ }
26
+ /**
27
+ * The upper bound of the tolerance per kind of object, above which it says nothing: two distinct
28
+ * operations A, B of a crystallographic point group differ in some entry by at least √2/3 ≈ 0.47 in
29
+ * any orthonormal frame (‖A − B‖ = ‖1 − A⁻¹B‖ ≥ √2 in the Frobenius norm, the least for a sixfold
30
+ * rotation), so 0.1 keeps them apart; two unit vectors are at most 2 apart; two points modulo the
31
+ * lattice at most 1/2 per coordinate; a relative deviation of the metric tensor is at most of the
32
+ * order of 1.
33
+ */
34
+ export declare const TOLERANCE_BOUND: Readonly<{
35
+ operations: 0.1;
36
+ directions: 1;
37
+ points: 0.5;
38
+ metric: 1;
39
+ }>;
40
+ /**
41
+ * The tolerance of the options, checked: a finite number above 0 and below the bound of the kind
42
+ * of object it measures; the default `EPSILON`.
43
+ */
44
+ export declare function toleranceOf(options: ToleranceOptions | undefined, kind: keyof typeof TOLERANCE_BOUND): number;
45
+ /**
46
+ * Making a group of a caller exact (`idealGroup`, closure.ts): a product is matched to the nearest
47
+ * element of the group, which must lie closer than `PRODUCT_MATCH` per entry (distinct operations
48
+ * of a point group differ by at least √2/3 ≈ 0.47 in some entry, so a nearer one is the product);
49
+ * the averaging has settled when no entry changes by `IDEAL_SETTLED` or more in a pass, and fails
50
+ * if after `IDEAL_PASSES` passes an entry still changes by `IDEAL_UNSETTLED` (it settles in one to
51
+ * six passes for every point group, exact or rounded); entries within `IDEAL_SNAP` of an exact value
52
+ * are set to it, which catches the rounding of the last bits but not a group turned by a small
53
+ * angle.
54
+ */
55
+ export declare const PRODUCT_MATCH = 0.25;
56
+ export declare const IDEAL_SETTLED = 1e-15;
57
+ export declare const IDEAL_UNSETTLED = 1e-12;
58
+ export declare const IDEAL_PASSES = 50;
59
+ export declare const IDEAL_SNAP = 1e-12;
60
+ /**
61
+ * The largest pivot threshold of a tensor form (tensorForm.ts, `reduce`): the threshold is raised
62
+ * up to this only in a general orientation, when components of the order of 10⁻⁷ would otherwise be
63
+ * taken for independent ones; a genuine pivot of a form of a point group is far larger.
64
+ */
65
+ export declare const MAX_PIVOT_THRESHOLD = 0.01;
11
66
  //# sourceMappingURL=tolerances.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tolerances.d.ts","sourceRoot":"","sources":["../src/tolerances.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,4EAA4E;AAC5E,eAAO,MAAM,OAAO,WAAO,CAAC;AAE5B;;;GAGG;AACH,eAAO,MAAM,YAAY,OAAO,CAAC"}
1
+ {"version":3,"file":"tolerances.d.ts","sourceRoot":"","sources":["../src/tolerances.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;GAIG;AACH,eAAO,MAAM,OAAO,WAAO,CAAC;AAE5B;;;GAGG;AACH,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC,4EAA4E;AAC5E,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe;;;;;EAA4E,CAAC;AAEzG;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,OAAO,eAAe,GAAG,MAAM,CAQ7G;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,OAAO,CAAC;AAClC,eAAO,MAAM,aAAa,QAAQ,CAAC;AACnC,eAAO,MAAM,eAAe,QAAQ,CAAC;AACrC,eAAO,MAAM,YAAY,KAAK,CAAC;AAC/B,eAAO,MAAM,UAAU,QAAQ,CAAC;AAEhC;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,OAAO,CAAC"}
@@ -1,11 +1,61 @@
1
1
  /**
2
2
  * Numeric tolerances, in one place. The values differ on purpose: each gates a different quantity.
3
+ * The tolerance a caller passes is one contract for every function (docs/conventions.md,
4
+ * "Tolerances"): one option, one metric per kind of object, one check.
5
+ */
6
+ import { shown } from './checks.js';
7
+ /**
8
+ * The default tolerance of every function that compares numbers a caller passes in (operations,
9
+ * directions, points): two numbers closer than this count as equal, in the metric of their kind
10
+ * (docs/conventions.md, "Tolerances").
3
11
  */
4
- /** Two matrix entries (or vector components) closer than this are equal. */
5
12
  export const EPSILON = 1e-6;
6
13
  /**
7
14
  * During group closure, an entry closer than this to one of the exact values in `SNAP_VALUES`
8
15
  * (linalg.ts) is replaced by it, so rounding errors cannot accumulate over repeated products.
9
16
  */
10
17
  export const SNAP_EPSILON = 1e-9;
18
+ /**
19
+ * The upper bound of the tolerance per kind of object, above which it says nothing: two distinct
20
+ * operations A, B of a crystallographic point group differ in some entry by at least √2/3 ≈ 0.47 in
21
+ * any orthonormal frame (‖A − B‖ = ‖1 − A⁻¹B‖ ≥ √2 in the Frobenius norm, the least for a sixfold
22
+ * rotation), so 0.1 keeps them apart; two unit vectors are at most 2 apart; two points modulo the
23
+ * lattice at most 1/2 per coordinate; a relative deviation of the metric tensor is at most of the
24
+ * order of 1.
25
+ */
26
+ export const TOLERANCE_BOUND = Object.freeze({ operations: 0.1, directions: 1, points: 0.5, metric: 1 });
27
+ /**
28
+ * The tolerance of the options, checked: a finite number above 0 and below the bound of the kind
29
+ * of object it measures; the default `EPSILON`.
30
+ */
31
+ export function toleranceOf(options, kind) {
32
+ // Only a missing tolerance takes the default; null is a wrong value, not a missing one.
33
+ const t = options?.tolerance === undefined ? EPSILON : options.tolerance;
34
+ const bound = TOLERANCE_BOUND[kind];
35
+ if (!(typeof t === 'number' && t > 0 && t < bound)) {
36
+ throw new RangeError(`The tolerance must be a number above 0 and below ${bound}, not ${shown(t)}`);
37
+ }
38
+ return t;
39
+ }
40
+ /**
41
+ * Making a group of a caller exact (`idealGroup`, closure.ts): a product is matched to the nearest
42
+ * element of the group, which must lie closer than `PRODUCT_MATCH` per entry (distinct operations
43
+ * of a point group differ by at least √2/3 ≈ 0.47 in some entry, so a nearer one is the product);
44
+ * the averaging has settled when no entry changes by `IDEAL_SETTLED` or more in a pass, and fails
45
+ * if after `IDEAL_PASSES` passes an entry still changes by `IDEAL_UNSETTLED` (it settles in one to
46
+ * six passes for every point group, exact or rounded); entries within `IDEAL_SNAP` of an exact value
47
+ * are set to it, which catches the rounding of the last bits but not a group turned by a small
48
+ * angle.
49
+ */
50
+ export const PRODUCT_MATCH = 0.25;
51
+ export const IDEAL_SETTLED = 1e-15;
52
+ export const IDEAL_UNSETTLED = 1e-12;
53
+ export const IDEAL_PASSES = 50;
54
+ export const IDEAL_SNAP = 1e-12;
55
+ /**
56
+ * The largest pivot threshold of a tensor form (tensorForm.ts, `reduce`): the threshold is raised
57
+ * up to this only in a general orientation, when components of the order of 10⁻⁷ would otherwise be
58
+ * taken for independent ones; a genuine pivot of a form of a point group is far larger.
59
+ */
60
+ export const MAX_PIVOT_THRESHOLD = 1e-2;
11
61
  //# sourceMappingURL=tolerances.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tolerances.js","sourceRoot":"","sources":["../src/tolerances.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,4EAA4E;AAC5E,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,CAAC;AAE5B;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC"}
1
+ {"version":3,"file":"tolerances.js","sourceRoot":"","sources":["../src/tolerances.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpC;;;;GAIG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,CAAC;AAE5B;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC;AAYjC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;AAEzG;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,OAAqC,EAAE,IAAkC;IACnG,wFAAwF;IACxF,MAAM,CAAC,GAAY,OAAO,EAAE,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC;IAClF,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CAAC,oDAAoD,KAAK,SAAS,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACrG,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC;AAClC,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAC;AACnC,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAC;AACrC,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAC;AAC/B,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,CAAC;AAEhC;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC"}
package/dist/version.d.ts CHANGED
@@ -3,5 +3,5 @@
3
3
  *
4
4
  * @remarks Kept equal to `version` in package.json (checked by version.test.ts).
5
5
  */
6
- export declare const VERSION = "0.10.1";
6
+ export declare const VERSION = "0.12.0";
7
7
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -3,5 +3,5 @@
3
3
  *
4
4
  * @remarks Kept equal to `version` in package.json (checked by version.test.ts).
5
5
  */
6
- export const VERSION = '0.10.1';
6
+ export const VERSION = '0.12.0';
7
7
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "symcrys",
3
- "version": "0.10.1",
3
+ "version": "0.12.0",
4
4
  "description": "Crystallographic symmetry calculations: point groups (crystallographic and magnetic) and plane groups, their settings, symbols and properties, and property tensors. TypeScript library without dependencies.",
5
5
  "keywords": [
6
6
  "crystallography",
package/src/cell.ts CHANGED
@@ -4,9 +4,11 @@
4
4
  * coordinates, as `latticeBasis` gives it; lengths in any unit, angles in degrees.
5
5
  */
6
6
 
7
+ import { checkObject, checkOptions, shown, TOLERANCE_KEYS } from './checks.js';
7
8
  import { apply, inverse, isSingular, multiply, transpose, type Mat3, type Vec3 } from './linalg.js';
8
- import { checkTimeReversal, type Operation as MagneticOperation } from './operations.js';
9
- import { EPSILON } from './tolerances.js';
9
+ import { checkTimeReversal, type CartesianOperation as MagneticOperation } from './operations.js';
10
+ import { toleranceOf, type ToleranceOptions } from './tolerances.js';
11
+ import { deepFreeze } from './freeze.js';
10
12
 
11
13
  /** The lattice parameters of a cell: edge lengths and angles in degrees (α between b and c, β between a and c, γ between a and b). */
12
14
  export interface LatticeParameters {
@@ -34,9 +36,9 @@ export interface CellAngles {
34
36
  readonly gamma: number;
35
37
  }
36
38
 
37
- /** An operation of a list given to `residualOperations`: at least its matrix; other fields are kept. */
39
+ /** An operation of a list given to `residualOperations`: at least its matrix W; other fields are kept. */
38
40
  export interface CellOperation {
39
- /** The 3×3 matrix W acting on lattice coordinates. */
41
+ /** The integer 3×3 matrix W acting on lattice coordinates (the `matrix` of an operation). */
40
42
  readonly matrix: Mat3;
41
43
  }
42
44
 
@@ -50,12 +52,6 @@ export interface IsValidCellOptions {
50
52
  readonly minVolumeRatio?: number;
51
53
  }
52
54
 
53
- /** Options of `residualOperations`. */
54
- export interface ResidualOperationsOptions {
55
- /** The largest accepted deviation of WᵀGW from G, relative to the largest entry of G; default `EPSILON`. */
56
- readonly tolerance?: number;
57
- }
58
-
59
55
  const DEG = Math.PI / 180;
60
56
 
61
57
  /** Cosine of an angle in degrees, exact at the multiples of 30° that cells use (90°, 60°, 120°, 180°). */
@@ -85,6 +81,15 @@ function anglesOk({ alpha, beta, gamma }: CellAngles): boolean {
85
81
  * number from 0 to 1.
86
82
  */
87
83
  export function isValidCell(angles: CellAngles, options?: IsValidCellOptions): boolean {
84
+ checkOptions(options, ['minVolumeRatio'], 'isValidCell');
85
+ checkObject(angles, 'isValidCell takes the angles { alpha, beta, gamma }');
86
+ // Numbers out of range are no cell (false); anything else is wrong input.
87
+ for (const name of ['alpha', 'beta', 'gamma'] as const) {
88
+ const x = (angles as Partial<CellAngles>)[name];
89
+ if (!(typeof x === 'number' && Number.isFinite(x))) {
90
+ throw new RangeError(`The angle ${name} must be a finite number of degrees, not ${shown(x)}`);
91
+ }
92
+ }
88
93
  if (!anglesOk(angles)) return false;
89
94
  const min = options?.minVolumeRatio ?? 0;
90
95
  if (!(typeof min === 'number' && min >= 0 && min <= 1))
@@ -94,14 +99,16 @@ export function isValidCell(angles: CellAngles, options?: IsValidCellOptions): b
94
99
  return Math.sqrt(r) >= min;
95
100
  }
96
101
 
97
- function checkEdges({ a, b, c }: LatticeParameters): void {
102
+ function checkEdges(p: LatticeParameters): void {
103
+ checkObject(p, 'latticeFromParameters takes the parameters { a, b, c, alpha, beta, gamma }');
104
+ const { a, b, c } = p;
98
105
  for (const [name, x] of [
99
106
  ['a', a],
100
107
  ['b', b],
101
108
  ['c', c],
102
109
  ] as const) {
103
110
  if (typeof x !== 'number' || !Number.isFinite(x) || x <= 0) {
104
- throw new RangeError(`latticeFromParameters: edge ${name} must be a positive number, not ${JSON.stringify(x)}`);
111
+ throw new RangeError(`latticeFromParameters: edge ${name} must be a positive number, not ${shown(x)}`);
105
112
  }
106
113
  }
107
114
  }
@@ -114,24 +121,26 @@ function checkEdges({ a, b, c }: LatticeParameters): void {
114
121
  * axis b, b lies along y (docs/conventions.md, "Cell, metric and indices"). The basis is
115
122
  * right-handed. Throws a RangeError for an edge that is not positive and for angles that give no cell.
116
123
  */
117
- export function latticeFromParameters(p: LatticeParameters): Mat3 {
118
- checkEdges(p);
119
- if (!isValidCell(p)) {
120
- throw new RangeError(`latticeFromParameters: angles ${p.alpha}°, ${p.beta}°, ${p.gamma}° give no cell`);
124
+ export function latticeFromParameters(parameters: LatticeParameters): Mat3 {
125
+ checkEdges(parameters);
126
+ if (!isValidCell(parameters)) {
127
+ throw new RangeError(
128
+ `latticeFromParameters: angles ${parameters.alpha}°, ${parameters.beta}°, ${parameters.gamma}° give no cell`,
129
+ );
121
130
  }
122
- const ca = cosDeg(p.alpha);
123
- const cb = cosDeg(p.beta);
124
- const cg = cosDeg(p.gamma);
131
+ const ca = cosDeg(parameters.alpha);
132
+ const cb = cosDeg(parameters.beta);
133
+ const cg = cosDeg(parameters.gamma);
125
134
  const sg = Math.sqrt(1 - cg * cg);
126
- const volumeRatio = Math.sqrt(radicand(p));
135
+ const volumeRatio = Math.sqrt(radicand(parameters));
127
136
  // columns: a = (a, 0, 0), b = (b cos γ, b sin γ, 0), c = (c cos β, c (cos α − cos β cos γ)/sin γ, c V/(abc sin γ))
128
- const cy = (p.c * (ca - cb * cg)) / sg;
129
- const cz = (p.c * volumeRatio) / sg;
130
- return [
131
- [p.a + 0, p.b * cg + 0, p.c * cb + 0],
132
- [0, p.b * sg + 0, cy + 0],
137
+ const cy = (parameters.c * (ca - cb * cg)) / sg;
138
+ const cz = (parameters.c * volumeRatio) / sg;
139
+ return deepFreeze([
140
+ [parameters.a + 0, parameters.b * cg + 0, parameters.c * cb + 0],
141
+ [0, parameters.b * sg + 0, cy + 0],
133
142
  [0, 0, cz + 0],
134
- ];
143
+ ]);
135
144
  }
136
145
 
137
146
  function columns(basis: Mat3): [Vec3, Vec3, Vec3] {
@@ -164,13 +173,13 @@ export function latticeParameters(basis: Mat3): LatticeParameters {
164
173
  const cross: Vec3 = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]];
165
174
  return Math.atan2(norm(cross), dot(u, v)) / DEG + 0;
166
175
  };
167
- return { a: norm(a), b: norm(b), c: norm(c), alpha: angle(b, c), beta: angle(a, c), gamma: angle(a, b) };
176
+ return deepFreeze({ a: norm(a), b: norm(b), c: norm(c), alpha: angle(b, c), beta: angle(a, c), gamma: angle(a, b) });
168
177
  }
169
178
 
170
179
  /** The metric tensor G = AᵀA of a basis A (columns a, b, c): g_ij = a_i · a_j, in the unit of the edge lengths squared. */
171
180
  export function metricTensor(basis: Mat3): Mat3 {
172
181
  checkBasis(basis, 'metricTensor');
173
- return multiply(transpose(basis), basis);
182
+ return deepFreeze(multiply(transpose(basis), basis));
174
183
  }
175
184
 
176
185
  /**
@@ -179,7 +188,7 @@ export function metricTensor(basis: Mat3): Mat3 {
179
188
  */
180
189
  export function reciprocalBasis(basis: Mat3): Mat3 {
181
190
  checkBasis(basis, 'reciprocalBasis');
182
- return transpose(inverse(basis));
191
+ return deepFreeze(transpose(inverse(basis)));
183
192
  }
184
193
 
185
194
  function vector(v: Vec3, name: string): Vec3 {
@@ -196,9 +205,9 @@ function angleOf(u: Vec3, v: Vec3): number {
196
205
  }
197
206
 
198
207
  /** The angle in degrees between two lattice directions [uvw] (coordinates in the basis, columns a, b, c), 0° to 180°. Throws a RangeError for a zero vector. */
199
- export function angleBetweenDirections(basis: Mat3, u: Vec3, v: Vec3): number {
208
+ export function angleBetweenDirections(basis: Mat3, direction1: Vec3, direction2: Vec3): number {
200
209
  checkBasis(basis, 'angleBetweenDirections');
201
- return angleOf(apply(basis, vector(u, 'u')), apply(basis, vector(v, 'v')));
210
+ return angleOf(apply(basis, vector(direction1, 'direction1')), apply(basis, vector(direction2, 'direction2')));
202
211
  }
203
212
 
204
213
  /**
@@ -206,15 +215,28 @@ export function angleBetweenDirections(basis: Mat3, u: Vec3, v: Vec3): number {
206
215
  * reciprocal vectors h·a* + k·b* + l·c*. For two faces this is 180° minus the interior dihedral
207
216
  * angle. Throws a RangeError for a zero vector.
208
217
  */
209
- export function angleBetweenPlanes(basis: Mat3, h: Vec3, k: Vec3): number {
218
+ export function angleBetweenPlanes(basis: Mat3, plane1: Vec3, plane2: Vec3): number {
210
219
  checkBasis(basis, 'angleBetweenPlanes');
211
220
  const b = reciprocalBasis(basis);
212
- return angleOf(apply(b, vector(h, 'h')), apply(b, vector(k, 'k')));
221
+ return angleOf(apply(b, vector(plane1, 'plane1')), apply(b, vector(plane2, 'plane2')));
213
222
  }
214
223
 
215
224
  /**
216
- * The operations that leave the metric of a cell unchanged: those W (integer matrices on the
217
- * coordinates of a lattice direction, as `latticeOperations` gives them) with WᵀGW = G, G the metric
225
+ * The unit normal of a plane (hkl) in a cell, a Cartesian vector: h·a* + k·b* + l·c* normalised, the
226
+ * pole of the face (#137 G4). With `latticeBasis(family)` for a family with fixed cell angles, or
227
+ * `latticeFromParameters` for a real cell; Miller indices of hexagonal axes are three, without i.
228
+ * Throws a RangeError for a basis that is not a regular 3 × 3 matrix and for a zero vector.
229
+ */
230
+ export function faceNormal(basis: Mat3, hkl: Vec3): Vec3 {
231
+ checkBasis(basis, 'faceNormal');
232
+ const v = apply(reciprocalBasis(basis), vector(hkl, 'hkl'));
233
+ const n = norm(v);
234
+ return Object.freeze(v.map((x) => x / n + 0)) as unknown as Vec3;
235
+ }
236
+
237
+ /**
238
+ * The operations that leave the metric of a cell unchanged: those W (`matrix`, the integer matrices
239
+ * on the coordinates of a lattice direction, as `operations` gives them) with WᵀGW = G, G the metric
218
240
  * tensor of `basis`. It is the point symmetry of a distorted lattice among the operations of a group;
219
241
  * the list is taken as it is, not closed into a group, and the kept operations are the ones of the
220
242
  * list, in its order. The deviation counts relative to the largest entry of G (`tolerance`, default
@@ -223,15 +245,11 @@ export function angleBetweenPlanes(basis: Mat3, h: Vec3, k: Vec3): number {
223
245
  export function residualOperations<T extends CellOperation>(
224
246
  operations: readonly T[],
225
247
  basis: Mat3,
226
- options?: ResidualOperationsOptions,
227
- ): T[] {
248
+ options?: ToleranceOptions,
249
+ ): readonly T[] {
250
+ checkOptions(options, TOLERANCE_KEYS, 'residualOperations');
228
251
  const g = metricTensor(basis);
229
- const tolerance = options?.tolerance ?? EPSILON;
230
- if (typeof tolerance !== 'number' || !Number.isFinite(tolerance) || tolerance < 0) {
231
- throw new RangeError(
232
- `residualOperations: tolerance must be a non-negative number, not ${JSON.stringify(tolerance)}`,
233
- );
234
- }
252
+ const tolerance = toleranceOf(options, 'metric');
235
253
  if (!Array.isArray(operations)) throw new RangeError('residualOperations: operations must be a list');
236
254
  operations.forEach((op, i) => {
237
255
  const m = (op as { readonly matrix?: unknown } | null)?.matrix;
@@ -240,12 +258,21 @@ export function residualOperations<T extends CellOperation>(
240
258
  m.length === 3 &&
241
259
  m.every((r) => Array.isArray(r) && r.length === 3 && r.every((x) => typeof x === 'number' && Number.isFinite(x)));
242
260
  if (!ok) throw new RangeError(`residualOperations: operation ${i} has no 3×3 matrix of finite numbers`);
261
+ // W is an integer matrix; a Cartesian matrix (±√3/2) passed as `matrix` would give a wrong answer.
262
+ if (!(m as number[][]).every((r) => r.every((x) => Math.abs(x - Math.round(x)) <= 1e-9))) {
263
+ throw new RangeError(
264
+ `residualOperations: the matrix of operation ${i} is not an integer matrix in the lattice basis (the field matrix of an operation, not cartesian): ${shown(m)}`,
265
+ );
266
+ }
243
267
  });
244
268
  const scale = Math.max(...g.flat().map(Math.abs));
245
- return operations.filter((op) => {
246
- const m = multiply(multiply(transpose(op.matrix), g), op.matrix);
247
- return m.every((row, i) => row.every((x, j) => Math.abs(x - g[i][j]) <= tolerance * scale));
248
- });
269
+ // The list is frozen, not the operations in it, which are the caller's.
270
+ return Object.freeze(
271
+ operations.filter((op) => {
272
+ const m = multiply(multiply(transpose(op.matrix), g), op.matrix);
273
+ return m.every((row, i) => row.every((x, j) => Math.abs(x - g[i][j]) < tolerance * scale));
274
+ }),
275
+ );
249
276
  }
250
277
 
251
278
  /** An operation of the magnetic API passed to `residualOperations`: its matrix and whether it is combined with time reversal. */
@@ -256,8 +283,8 @@ export interface MagneticCellOperation extends CellOperation {
256
283
 
257
284
  /**
258
285
  * The operations of a list that keep the metric of a cell: W with WᵀGW = G, G the metric tensor
259
- * of `basis`. The operations are integer matrices on lattice coordinates, as `latticeOperations`
260
- * gives them; the list is taken as it is and the kept operations are the ones of the list, in its
286
+ * of `basis`. The operations are read by `matrix`, the integer matrix W on lattice coordinates, as
287
+ * `operations` gives them; the list is taken as it is and the kept operations are the ones of the list, in its
261
288
  * order. The deviation counts relative to the largest entry of G (`tolerance`, default `EPSILON`).
262
289
  * Every operation must say whether it is combined with time reversal (`timeReversal`), which is
263
290
  * kept; one that does not (an operation of the crystallographic API) is a RangeError.
@@ -265,8 +292,8 @@ export interface MagneticCellOperation extends CellOperation {
265
292
  export function residualMagneticOperations<T extends MagneticCellOperation>(
266
293
  operations: readonly T[],
267
294
  basis: Mat3,
268
- options?: ResidualOperationsOptions,
269
- ): T[] {
295
+ options?: ToleranceOptions,
296
+ ): readonly T[] {
270
297
  if (Array.isArray(operations)) checkTimeReversal(operations as unknown as readonly MagneticOperation[]);
271
298
  return residualOperations(operations, basis, options);
272
299
  }