symcrys 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/cell.d.ts +5 -2
  2. package/dist/cell.d.ts.map +1 -1
  3. package/dist/cell.js +7 -7
  4. package/dist/cell.js.map +1 -1
  5. package/dist/crystalClasses.d.ts +13 -8
  6. package/dist/crystalClasses.d.ts.map +1 -1
  7. package/dist/crystalClasses.js +13 -8
  8. package/dist/crystalClasses.js.map +1 -1
  9. package/dist/indices.d.ts +3 -1
  10. package/dist/indices.d.ts.map +1 -1
  11. package/dist/indices.js +6 -2
  12. package/dist/indices.js.map +1 -1
  13. package/dist/linalg.d.ts +7 -1
  14. package/dist/linalg.d.ts.map +1 -1
  15. package/dist/linalg.js +20 -3
  16. package/dist/linalg.js.map +1 -1
  17. package/dist/operations.d.ts +6 -0
  18. package/dist/operations.d.ts.map +1 -1
  19. package/dist/operations.js +19 -0
  20. package/dist/operations.js.map +1 -1
  21. package/dist/pointGroups/identify.d.ts +4 -2
  22. package/dist/pointGroups/identify.d.ts.map +1 -1
  23. package/dist/pointGroups/identify.js +5 -18
  24. package/dist/pointGroups/identify.js.map +1 -1
  25. package/dist/pointGroups/orbits.d.ts +1 -1
  26. package/dist/pointGroups/orbits.d.ts.map +1 -1
  27. package/dist/pointGroups/orbits.js +3 -3
  28. package/dist/pointGroups/orbits.js.map +1 -1
  29. package/dist/pointGroups/properties.d.ts +6 -2
  30. package/dist/pointGroups/properties.d.ts.map +1 -1
  31. package/dist/pointGroups/properties.js +6 -2
  32. package/dist/pointGroups/properties.js.map +1 -1
  33. package/dist/siteSymmetry.d.ts +5 -3
  34. package/dist/siteSymmetry.d.ts.map +1 -1
  35. package/dist/siteSymmetry.js +6 -2
  36. package/dist/siteSymmetry.js.map +1 -1
  37. package/dist/tensors/tensorForm.d.ts +3 -1
  38. package/dist/tensors/tensorForm.d.ts.map +1 -1
  39. package/dist/tensors/tensorForm.js +3 -1
  40. package/dist/tensors/tensorForm.js.map +1 -1
  41. package/dist/tensors/tensorProperties.d.ts +4 -2
  42. package/dist/tensors/tensorProperties.d.ts.map +1 -1
  43. package/dist/tensors/tensorProperties.js +4 -2
  44. package/dist/tensors/tensorProperties.js.map +1 -1
  45. package/dist/version.d.ts +1 -1
  46. package/dist/version.js +1 -1
  47. package/package.json +1 -1
  48. package/src/cell.ts +9 -8
  49. package/src/crystalClasses.ts +13 -8
  50. package/src/indices.ts +5 -2
  51. package/src/linalg.ts +21 -2
  52. package/src/operations.ts +20 -0
  53. package/src/pointGroups/identify.ts +5 -19
  54. package/src/pointGroups/orbits.ts +3 -3
  55. package/src/pointGroups/properties.ts +6 -2
  56. package/src/siteSymmetry.ts +6 -3
  57. package/src/tensors/tensorForm.ts +3 -1
  58. package/src/tensors/tensorProperties.ts +4 -2
  59. package/src/version.ts +1 -1
package/src/indices.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * (docs/conventions.md, "Cell, metric and indices"). Plain integers and matrices in and out.
4
4
  */
5
5
 
6
- import { det, inverse, transpose, type Mat3, type Vec3 } from './linalg.js';
6
+ import { det, inverse, isSingular, transpose, type Mat3, type Vec3 } from './linalg.js';
7
7
 
8
8
  /** A direction [UVTW] or a plane (hkil) in four indices (Miller–Bravais). */
9
9
  export type FourIndices = readonly [number, number, number, number];
@@ -98,7 +98,9 @@ export interface AxisChange {
98
98
  * The transformation of indices for a change of basis (a′, b′, c′) = (a, b, c)·P (ITA Vol. A, 2006,
99
99
  * Section 5.1.3): the columns of `P` are the new basis vectors in the old one. The coordinates of a
100
100
  * direction transform with Q = P⁻¹, the indices of a plane with Pᵀ, so that hu + kv + lw is the same in
101
- * both bases. Throws a RangeError unless det P > 0 (a right-handed basis).
101
+ * both bases. Throws a RangeError unless det P > 0 (a right-handed basis), and for a P that is
102
+ * singular within rounding (the absolute value of det P at most 10⁻⁹ times the product of its
103
+ * column lengths).
102
104
  */
103
105
  export function changeOfAxes(P: Mat3): AxisChange {
104
106
  if (
@@ -109,6 +111,7 @@ export function changeOfAxes(P: Mat3): AxisChange {
109
111
  throw new RangeError('changeOfAxes: P must be a 3×3 matrix of finite numbers');
110
112
  }
111
113
  if (!(det(P) > 0)) throw new RangeError('changeOfAxes: det P must be positive (a right-handed basis)');
114
+ if (isSingular(P)) throw new RangeError('changeOfAxes: P is singular within rounding');
112
115
  const clean = (m: Mat3): Mat3 => m.map((r) => r.map((x) => x + 0)) as unknown as Mat3; // no −0
113
116
  return { direction: clean(inverse(P)), plane: clean(transpose(P)) };
114
117
  }
package/src/linalg.ts CHANGED
@@ -120,10 +120,29 @@ export function matricesEqual(a: Mat3, b: Mat3): boolean {
120
120
  return true;
121
121
  }
122
122
 
123
- /** The inverse of a regular matrix (by the adjugate); throws for a singular one. */
123
+ /**
124
+ * Below this ratio of |det| to the product of the column lengths (Hadamard's bound, the largest
125
+ * |det| columns of these lengths can have) a matrix counts as singular. The ratio depends neither
126
+ * on the unit of the entries nor on the lengths of the columns relative to each other; for a cell
127
+ * it is V/(abc).
128
+ */
129
+ const SINGULAR_RATIO = 1e-9;
130
+
131
+ /**
132
+ * True if `a` is singular within rounding: |det a| at most `SINGULAR_RATIO` times the product of
133
+ * its column lengths. The one criterion for `inverse` and for the bases of the cell functions. Also
134
+ * true for a zero column and for a matrix with NaN or an infinite entry.
135
+ */
136
+ export function isSingular(a: Mat3): boolean {
137
+ const bound = [0, 1, 2].reduce((p, j) => p * Math.hypot(a[0][j], a[1][j], a[2][j]), 1);
138
+ // A comparison that is false for NaN, so a matrix with NaN counts as singular.
139
+ return !(Number.isFinite(bound) && Math.abs(det(a)) > SINGULAR_RATIO * bound);
140
+ }
141
+
142
+ /** The inverse of a regular matrix (by the adjugate); throws for a singular one (`isSingular`), whatever the scale of its entries. */
124
143
  export function inverse(a: Mat3): Mat3 {
144
+ if (isSingular(a)) throw new RangeError('inverse: singular matrix');
125
145
  const d = det(a);
126
- if (Math.abs(d) < EPSILON) throw new RangeError('inverse: singular matrix');
127
146
  const c = (i: number, j: number): number => {
128
147
  const [r0, r1] = [0, 1, 2].filter((k) => k !== i);
129
148
  const [s0, s1] = [0, 1, 2].filter((k) => k !== j);
package/src/operations.ts CHANGED
@@ -40,6 +40,26 @@ export function checkOrthogonal(m: Mat3, what: string): void {
40
40
  }
41
41
  }
42
42
 
43
+ /**
44
+ * Throws unless mᵀm = 1 within the tolerance: the check of `checkOrthogonal` for a function with a
45
+ * `tolerance` option, so that a matrix with rounding errors (e.g. rounded to 4 decimals) passes
46
+ * with a larger one. Never stricter than `checkOrthogonal`.
47
+ */
48
+ export function checkOrthogonalWithin(m: Mat3, what: string, tolerance: number): void {
49
+ if (tolerance <= EPSILON) {
50
+ checkOrthogonal(m, what);
51
+ return;
52
+ }
53
+ for (let i = 0; i < 3; i++) {
54
+ for (let j = 0; j < 3; j++) {
55
+ const d = m[0][i] * m[0][j] + m[1][i] * m[1][j] + m[2][i] * m[2][j];
56
+ if (!(Math.abs(d - (i === j ? 1 : 0)) < tolerance)) {
57
+ throw new RangeError(`${what} is not an orthogonal (Cartesian) matrix: ${JSON.stringify(m)}`);
58
+ }
59
+ }
60
+ }
61
+ }
62
+
43
63
  /**
44
64
  * Checks operations that a caller passes in: each must say whether it is combined with time
45
65
  * reversal. Without this, an operation that lacks `timeReversal` (as the crystallographic API
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import { det, multiply, trace, type Mat3, type Vec3 } from '../linalg.js';
8
- import { checkOrthogonal, checkTimeReversal, type Operation } from '../operations.js';
8
+ import { checkOrthogonalWithin, checkTimeReversal, type Operation } from '../operations.js';
9
9
  import { EPSILON } from '../tolerances.js';
10
10
  import { axialVector, rotationAxis, type OperationKind } from './elements.js';
11
11
  import {
@@ -62,22 +62,6 @@ export interface CartesianOperationDescription {
62
62
  readonly timeReversal: boolean;
63
63
  }
64
64
 
65
- /** Throws unless mᵀm = 1 within the tolerance (a matrix with rounding errors passes with a larger one). */
66
- function checkOrthogonalWithin(m: Mat3, what: string, tolerance: number): void {
67
- if (tolerance <= EPSILON) {
68
- checkOrthogonal(m, what);
69
- return;
70
- }
71
- for (let i = 0; i < 3; i++) {
72
- for (let j = 0; j < 3; j++) {
73
- const d = m[0][i] * m[0][j] + m[1][i] * m[1][j] + m[2][i] * m[2][j];
74
- if (!(Math.abs(d - (i === j ? 1 : 0)) < tolerance)) {
75
- throw new RangeError(`${what} is not an orthogonal (Cartesian) matrix: ${JSON.stringify(m)}`);
76
- }
77
- }
78
- }
79
- }
80
-
81
65
  /** No point group has more than 96 operations (m-3m1'). */
82
66
  const MAX_ORDER = 96;
83
67
 
@@ -181,7 +165,8 @@ function keyOfSignature(s: string): MagneticPointGroupKey | undefined {
181
165
  * Time reversal counts: a list with time reversal gives a magnetic point group. Returns undefined
182
166
  * if the operations generate no point group (an operation of order 5, a group larger than 96).
183
167
  * Throws a RangeError for a matrix that is not orthogonal (within `tolerance`, at least
184
- * `EPSILON`). The classes `1` and `-1` have one setting and no orientation, so they always have
168
+ * `EPSILON`) and for an operation that does not say whether it is combined with time reversal.
169
+ * The classes `1` and `-1` have one setting and no orientation, so they always have
185
170
  * setting 1.
186
171
  */
187
172
  export function pointGroupOf(
@@ -243,7 +228,8 @@ function describe(op: Operation, i: number, tolerance: number): CartesianOperati
243
228
  * by lattice directions, since the list need not belong to a setting; for a point group given by its
244
229
  * key, `describeOperations` names them. Throws a RangeError for a matrix that is not orthogonal or
245
230
  * not a crystallographic operation (a rotation part by an angle other than 360°/n, n = 1, 2, 3, 4,
246
- * 6), both within `tolerance` (default `EPSILON`, as for `pointGroupOf`).
231
+ * 6), both within `tolerance` (default `EPSILON`, as for `pointGroupOf`), and for an operation that
232
+ * does not say whether it is combined with time reversal.
247
233
  */
248
234
  export function describeOperationsOf(
249
235
  operations: readonly Operation[],
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { crystalFamily, latticeBasis } from '../lattice.js';
7
7
  import { apply, inverse, multiply, type Mat3, type Vec3 } from '../linalg.js';
8
- import { checkOrthogonal, type Operation } from '../operations.js';
8
+ import { checkOrthogonalWithin, type Operation } from '../operations.js';
9
9
  import { siteTolerance } from '../siteSymmetry.js';
10
10
  import type { Uvw } from '../symmetryDirections.js';
11
11
  import {
@@ -196,7 +196,7 @@ export interface OrbitPole {
196
196
  * `siteSymmetry`), in the order in which the operations reach them, each with the indices of the
197
197
  * operations that map the direction there. The list is taken as it is, not closed into a group.
198
198
  * Time reversal is ignored. Throws a RangeError for a zero vector and for a matrix that is not
199
- * orthogonal (Cartesian; not one of `latticeOperations`).
199
+ * orthogonal (Cartesian, not one of `latticeOperations`; within `tolerance`, at least `EPSILON`).
200
200
  */
201
201
  export function orbitOf(
202
202
  operations: readonly { readonly matrix: Mat3 }[],
@@ -204,7 +204,7 @@ export function orbitOf(
204
204
  options?: OrbitOptions,
205
205
  ): readonly OrbitPole[] {
206
206
  const tolerance = siteTolerance(options);
207
- operations.forEach((op, i) => checkOrthogonal(op.matrix, `Operation ${i}`));
207
+ operations.forEach((op, i) => checkOrthogonalWithin(op.matrix, `Operation ${i}`, tolerance));
208
208
  const n = Math.hypot(direction[0], direction[1], direction[2]);
209
209
  if (!(n > 0) || !Number.isFinite(n)) throw new RangeError(`Not a direction: ${JSON.stringify(direction)}`);
210
210
  const u: Vec3 = [direction[0] / n, direction[1] / n, direction[2] / n];
@@ -23,7 +23,9 @@ function check(operations: readonly Operation[]): void {
23
23
  /**
24
24
  * True if the group generated by `operations` contains the inversion, not combined with time
25
25
  * reversal: e.g. for the operations of a distorted crystal. A list that is not closed is closed
26
- * first. For a point group given by its key, read `pointGroup(key).centrosymmetric`.
26
+ * first. For a point group given by its key, read `pointGroup(key).centrosymmetric`. Throws a
27
+ * RangeError for a matrix that is not orthogonal and for an operation that does not say whether it
28
+ * is combined with time reversal.
27
29
  */
28
30
  export function isCentrosymmetricOf(operations: readonly Operation[]): boolean {
29
31
  check(operations);
@@ -33,7 +35,9 @@ export function isCentrosymmetricOf(operations: readonly Operation[]): boolean {
33
35
  /**
34
36
  * True if the group generated by `operations` is chiral: proper rotations only, with or without
35
37
  * time reversal (products of proper rotations are proper, so the list need not be closed). For a
36
- * point group given by its key, read `pointGroup(key).chiral`.
38
+ * point group given by its key, read `pointGroup(key).chiral`. Throws a RangeError for a matrix that
39
+ * is not orthogonal and for an operation that does not say whether it is combined with time
40
+ * reversal.
37
41
  */
38
42
  export function isChiralOf(operations: readonly Operation[]): boolean {
39
43
  check(operations);
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  import { apply, matricesEqual, multiply, type Vec3 } from './linalg.js';
7
- import type { Operation } from './operations.js';
7
+ import { checkOrthogonalWithin, type Operation } from './operations.js';
8
8
  import { EPSILON } from './tolerances.js';
9
9
 
10
10
  /** Options of `siteSymmetry` and `wyckoffPositionOf`. */
@@ -119,8 +119,10 @@ export function siteTolerance(options?: SiteSymmetryOptions): number {
119
119
  * within the tolerance are closed under the products found in the list and the direction is set onto
120
120
  * their fixed space, repeated until stable, so that for a group the result is a subgroup and the
121
121
  * set direction lies exactly on its elements. For a list that is not a group, `multiplicity` may
122
- * not be an integer. Throws a RangeError for a zero vector, and if the closed operations fix no
123
- * common direction (a tolerance far too large, e.g. 0.5).
122
+ * not be an integer. The matrices must be Cartesian and orthogonal (not those of
123
+ * `latticeOperations`). Throws a RangeError for a matrix that is not orthogonal (within
124
+ * `tolerance`, at least `EPSILON`), for a zero vector, and if the closed operations fix no common
125
+ * direction (a tolerance far too large, e.g. 0.5).
124
126
  */
125
127
  export function siteSymmetry(
126
128
  operations: readonly Operation[],
@@ -128,6 +130,7 @@ export function siteSymmetry(
128
130
  options?: SiteSymmetryOptions,
129
131
  ): SiteSymmetry {
130
132
  const tolerance = siteTolerance(options);
133
+ operations.forEach((op, i) => checkOrthogonalWithin(op.matrix, `Operation ${i}`, tolerance));
131
134
  const v = unit(direction);
132
135
  const fixing = (w: Vec3): number[] => {
133
136
  const out: number[] = [];
@@ -246,7 +246,9 @@ export function tensorForm(
246
246
  * surface normal (`siteSymmetry`) or the operations of a distorted crystal. The tensor is given by
247
247
  * its specification or by the name of a property in `TENSOR_PROPERTIES`. The operations are
248
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.
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.
250
252
  */
251
253
  export function tensorFormOf(operations: readonly Operation[], spec: TensorSpec | TensorPropertyName): TensorForm {
252
254
  const resolved = specOf(spec);
@@ -38,13 +38,15 @@ export function allowedProperties(key: MagneticPointGroupKey): TensorPropertyNam
38
38
 
39
39
  /**
40
40
  * 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.
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.
42
44
  */
43
45
  export function allowsPropertyOf(operations: readonly Operation[], name: TensorPropertyName): boolean {
44
46
  return tensorFormOf(operations, named(name)).basis.length > 0;
45
47
  }
46
48
 
47
- /** The named properties the group generated by `operations` allows. */
49
+ /** The named properties the group generated by `operations` allows. Checks the operations as `allowsPropertyOf`. */
48
50
  export function allowedPropertiesOf(operations: readonly Operation[]): TensorPropertyName[] {
49
51
  return TENSOR_PROPERTY_NAMES.filter((name) => allowsPropertyOf(operations, name));
50
52
  }
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  *
4
4
  * @remarks Kept equal to `version` in package.json (checked by version.test.ts).
5
5
  */
6
- export const VERSION = '0.8.0';
6
+ export const VERSION = '0.8.1';