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.
- package/README.md +6 -6
- package/dist/cell.d.ts +19 -16
- package/dist/cell.d.ts.map +1 -1
- package/dist/cell.js +63 -35
- package/dist/cell.js.map +1 -1
- package/dist/checks.d.ts +29 -0
- package/dist/checks.d.ts.map +1 -0
- package/dist/checks.js +79 -0
- package/dist/checks.js.map +1 -0
- package/dist/closure.d.ts +43 -0
- package/dist/closure.d.ts.map +1 -0
- package/dist/closure.js +201 -0
- package/dist/closure.js.map +1 -0
- package/dist/coordinates.d.ts +143 -0
- package/dist/coordinates.d.ts.map +1 -0
- package/dist/coordinates.js +198 -0
- package/dist/coordinates.js.map +1 -0
- package/dist/crystalClasses.d.ts +170 -117
- package/dist/crystalClasses.d.ts.map +1 -1
- package/dist/crystalClasses.js +190 -121
- package/dist/crystalClasses.js.map +1 -1
- package/dist/freeze.d.ts +7 -0
- package/dist/freeze.d.ts.map +1 -0
- package/dist/freeze.js +15 -0
- package/dist/freeze.js.map +1 -0
- package/dist/index.d.ts +13 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/indices.d.ts +30 -20
- package/dist/indices.d.ts.map +1 -1
- package/dist/indices.js +101 -31
- package/dist/indices.js.map +1 -1
- package/dist/lattice.d.ts +15 -1
- package/dist/lattice.d.ts.map +1 -1
- package/dist/lattice.js +30 -1
- package/dist/lattice.js.map +1 -1
- package/dist/linalg.d.ts +2 -2
- package/dist/linalg.d.ts.map +1 -1
- package/dist/linalg.js +3 -3
- package/dist/linalg.js.map +1 -1
- package/dist/magnetic.d.ts +14 -10
- package/dist/magnetic.d.ts.map +1 -1
- package/dist/magnetic.js +10 -6
- package/dist/magnetic.js.map +1 -1
- package/dist/notation.d.ts +16 -0
- package/dist/notation.d.ts.map +1 -0
- package/dist/notation.js +66 -0
- package/dist/notation.js.map +1 -0
- package/dist/operations.d.ts +40 -26
- package/dist/operations.d.ts.map +1 -1
- package/dist/operations.js +48 -60
- package/dist/operations.js.map +1 -1
- package/dist/plane.d.ts +297 -80
- package/dist/plane.d.ts.map +1 -1
- package/dist/plane.js +289 -94
- package/dist/plane.js.map +1 -1
- package/dist/pointGroups/crystalForms.d.ts +6 -21
- package/dist/pointGroups/crystalForms.d.ts.map +1 -1
- package/dist/pointGroups/crystalForms.js +9 -16
- package/dist/pointGroups/crystalForms.js.map +1 -1
- package/dist/pointGroups/crystalFormsData.d.ts +4 -1
- package/dist/pointGroups/crystalFormsData.d.ts.map +1 -1
- package/dist/pointGroups/crystalFormsData.js +64 -1
- package/dist/pointGroups/crystalFormsData.js.map +1 -1
- package/dist/pointGroups/elements.d.ts +19 -17
- package/dist/pointGroups/elements.d.ts.map +1 -1
- package/dist/pointGroups/elements.js +65 -83
- package/dist/pointGroups/elements.js.map +1 -1
- package/dist/pointGroups/identify.d.ts +10 -18
- package/dist/pointGroups/identify.d.ts.map +1 -1
- package/dist/pointGroups/identify.js +27 -79
- package/dist/pointGroups/identify.js.map +1 -1
- package/dist/pointGroups/itaOrder.d.ts +15 -14
- package/dist/pointGroups/itaOrder.d.ts.map +1 -1
- package/dist/pointGroups/itaOrder.js +116 -94
- package/dist/pointGroups/itaOrder.js.map +1 -1
- package/dist/pointGroups/itcData.d.ts +96 -0
- package/dist/pointGroups/itcData.d.ts.map +1 -0
- package/dist/pointGroups/itcData.js +336 -0
- package/dist/pointGroups/itcData.js.map +1 -0
- package/dist/pointGroups/itcMagneticData.d.ts +55 -0
- package/dist/pointGroups/itcMagneticData.d.ts.map +1 -0
- package/dist/pointGroups/itcMagneticData.js +172 -0
- package/dist/pointGroups/itcMagneticData.js.map +1 -0
- package/dist/pointGroups/keys.d.ts +32 -0
- package/dist/pointGroups/keys.d.ts.map +1 -0
- package/dist/pointGroups/keys.js +120 -0
- package/dist/pointGroups/keys.js.map +1 -0
- package/dist/pointGroups/magneticSymbols.d.ts +6 -5
- package/dist/pointGroups/magneticSymbols.d.ts.map +1 -1
- package/dist/pointGroups/magneticSymbols.js +15 -12
- package/dist/pointGroups/magneticSymbols.js.map +1 -1
- package/dist/pointGroups/orbits.d.ts +23 -42
- package/dist/pointGroups/orbits.d.ts.map +1 -1
- package/dist/pointGroups/orbits.js +20 -62
- package/dist/pointGroups/orbits.js.map +1 -1
- package/dist/pointGroups/pointGroups.d.ts +145 -68
- package/dist/pointGroups/pointGroups.d.ts.map +1 -1
- package/dist/pointGroups/pointGroups.js +455 -243
- package/dist/pointGroups/pointGroups.js.map +1 -1
- package/dist/pointGroups/properties.d.ts +21 -22
- package/dist/pointGroups/properties.d.ts.map +1 -1
- package/dist/pointGroups/properties.js +40 -37
- package/dist/pointGroups/properties.js.map +1 -1
- package/dist/pointGroups/seitzData.d.ts +26 -0
- package/dist/pointGroups/seitzData.d.ts.map +1 -0
- package/dist/pointGroups/seitzData.js +130 -0
- package/dist/pointGroups/seitzData.js.map +1 -0
- package/dist/pointGroups/shubnikovData.d.ts +11 -0
- package/dist/pointGroups/shubnikovData.d.ts.map +1 -0
- package/dist/pointGroups/shubnikovData.js +101 -0
- package/dist/pointGroups/shubnikovData.js.map +1 -0
- package/dist/pointGroups/wyckoff.d.ts +70 -40
- package/dist/pointGroups/wyckoff.d.ts.map +1 -1
- package/dist/pointGroups/wyckoff.js +98 -91
- package/dist/pointGroups/wyckoff.js.map +1 -1
- package/dist/pointGroups/wyckoffData.d.ts +7 -4
- package/dist/pointGroups/wyckoffData.d.ts.map +1 -1
- package/dist/pointGroups/wyckoffData.js +789 -168
- package/dist/pointGroups/wyckoffData.js.map +1 -1
- package/dist/siteSymmetry.d.ts +8 -16
- package/dist/siteSymmetry.d.ts.map +1 -1
- package/dist/siteSymmetry.js +34 -24
- package/dist/siteSymmetry.js.map +1 -1
- package/dist/spaceGroups/affine.d.ts.map +1 -1
- package/dist/spaceGroups/affine.js +3 -2
- package/dist/spaceGroups/affine.js.map +1 -1
- package/dist/spaceGroups/cell.d.ts +43 -31
- package/dist/spaceGroups/cell.d.ts.map +1 -1
- package/dist/spaceGroups/cell.js +173 -85
- package/dist/spaceGroups/cell.js.map +1 -1
- package/dist/spaceGroups/describe.d.ts +13 -10
- package/dist/spaceGroups/describe.d.ts.map +1 -1
- package/dist/spaceGroups/describe.js +11 -10
- package/dist/spaceGroups/describe.js.map +1 -1
- package/dist/spaceGroups/planeGroupData.d.ts +3 -1
- package/dist/spaceGroups/planeGroupData.d.ts.map +1 -1
- package/dist/spaceGroups/planeGroupData.js +25 -8
- package/dist/spaceGroups/planeGroupData.js.map +1 -1
- package/dist/spaceGroups/planeGroups.d.ts.map +1 -1
- package/dist/spaceGroups/planeGroups.js +2 -1
- package/dist/spaceGroups/planeGroups.js.map +1 -1
- package/dist/spaceGroups/planeLattice.d.ts +78 -0
- package/dist/spaceGroups/planeLattice.d.ts.map +1 -0
- package/dist/spaceGroups/planeLattice.js +143 -0
- package/dist/spaceGroups/planeLattice.js.map +1 -0
- package/dist/spaceGroups/planePointGroups.d.ts +6 -2
- package/dist/spaceGroups/planePointGroups.d.ts.map +1 -1
- package/dist/spaceGroups/planePointGroups.js +14 -2
- package/dist/spaceGroups/planePointGroups.js.map +1 -1
- package/dist/spaceGroups/positions.d.ts +48 -19
- package/dist/spaceGroups/positions.d.ts.map +1 -1
- package/dist/spaceGroups/positions.js +166 -67
- package/dist/spaceGroups/positions.js.map +1 -1
- package/dist/spaceGroups/rational.d.ts +18 -0
- package/dist/spaceGroups/rational.d.ts.map +1 -1
- package/dist/spaceGroups/rational.js +58 -0
- package/dist/spaceGroups/rational.js.map +1 -1
- package/dist/tensors/propertyTable.d.ts +3 -1
- package/dist/tensors/propertyTable.d.ts.map +1 -1
- package/dist/tensors/propertyTable.js +17 -10
- package/dist/tensors/propertyTable.js.map +1 -1
- package/dist/tensors/rowEchelon.d.ts +3 -3
- package/dist/tensors/rowEchelon.d.ts.map +1 -1
- package/dist/tensors/rowEchelon.js +4 -4
- package/dist/tensors/rowEchelon.js.map +1 -1
- package/dist/tensors/tensorForm.d.ts +75 -34
- package/dist/tensors/tensorForm.d.ts.map +1 -1
- package/dist/tensors/tensorForm.js +171 -67
- package/dist/tensors/tensorForm.js.map +1 -1
- package/dist/tensors/tensorProperties.d.ts +10 -9
- package/dist/tensors/tensorProperties.d.ts.map +1 -1
- package/dist/tensors/tensorProperties.js +22 -12
- package/dist/tensors/tensorProperties.js.map +1 -1
- package/dist/tensors/voigt.d.ts +56 -0
- package/dist/tensors/voigt.d.ts.map +1 -0
- package/dist/tensors/voigt.js +188 -0
- package/dist/tensors/voigt.js.map +1 -0
- package/dist/tolerances.d.ts +56 -1
- package/dist/tolerances.d.ts.map +1 -1
- package/dist/tolerances.js +51 -1
- package/dist/tolerances.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/src/cell.ts +78 -51
- package/src/checks.ts +84 -0
- package/src/closure.ts +214 -0
- package/src/coordinates.ts +370 -0
- package/src/crystalClasses.ts +271 -157
- package/src/freeze.ts +14 -0
- package/src/index.ts +33 -12
- package/src/indices.ts +102 -42
- package/src/lattice.ts +41 -1
- package/src/linalg.ts +3 -3
- package/src/magnetic.ts +24 -11
- package/src/notation.ts +73 -0
- package/src/operations.ts +78 -71
- package/src/plane.ts +531 -151
- package/src/pointGroups/crystalForms.ts +11 -44
- package/src/pointGroups/crystalFormsData.ts +66 -1
- package/src/pointGroups/elements.ts +82 -92
- package/src/pointGroups/identify.ts +38 -97
- package/src/pointGroups/itaOrder.ts +117 -101
- package/src/pointGroups/itcData.ts +404 -0
- package/src/pointGroups/itcMagneticData.ts +206 -0
- package/src/pointGroups/keys.ts +144 -0
- package/src/pointGroups/magneticSymbols.ts +18 -17
- package/src/pointGroups/orbits.ts +41 -89
- package/src/pointGroups/pointGroups.ts +593 -298
- package/src/pointGroups/properties.ts +44 -40
- package/src/pointGroups/seitzData.ts +147 -0
- package/src/pointGroups/shubnikovData.ts +103 -0
- package/src/pointGroups/wyckoff.ts +137 -112
- package/src/pointGroups/wyckoffData.ts +793 -170
- package/src/siteSymmetry.ts +43 -40
- package/src/spaceGroups/affine.ts +3 -2
- package/src/spaceGroups/cell.ts +227 -116
- package/src/spaceGroups/describe.ts +23 -19
- package/src/spaceGroups/planeGroupData.ts +28 -9
- package/src/spaceGroups/planeGroups.ts +2 -1
- package/src/spaceGroups/planeLattice.ts +208 -0
- package/src/spaceGroups/planePointGroups.ts +23 -3
- package/src/spaceGroups/positions.ts +202 -78
- package/src/spaceGroups/rational.ts +59 -0
- package/src/tensors/propertyTable.ts +26 -14
- package/src/tensors/rowEchelon.ts +4 -3
- package/src/tensors/tensorForm.ts +258 -88
- package/src/tensors/tensorProperties.ts +34 -14
- package/src/tensors/voigt.ts +268 -0
- package/src/tolerances.ts +66 -1
- package/src/version.ts +1 -1
- package/dist/pointGroups/data.d.ts +0 -75
- package/dist/pointGroups/data.d.ts.map +0 -1
- package/dist/pointGroups/data.js +0 -212
- package/dist/pointGroups/data.js.map +0 -1
- package/dist/pointGroups/magneticData.d.ts +0 -66
- package/dist/pointGroups/magneticData.d.ts.map +0 -1
- package/dist/pointGroups/magneticData.js +0 -184
- package/dist/pointGroups/magneticData.js.map +0 -1
- package/dist/spaceGroups/snap.d.ts +0 -20
- package/dist/spaceGroups/snap.d.ts.map +0 -1
- package/dist/spaceGroups/snap.js +0 -103
- package/dist/spaceGroups/snap.js.map +0 -1
- package/src/pointGroups/data.ts +0 -305
- package/src/pointGroups/magneticData.ts +0 -328
- package/src/spaceGroups/snap.ts +0 -121
package/src/checks.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks of what a caller passes in, shared by the entry points (#137 D2): wrong input is a
|
|
3
|
+
* RangeError that names what is wrong, never a TypeError from deep inside the library.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A value for an error message; never throws: numbers as written (NaN, Infinity), BigInt with its
|
|
8
|
+
* `n`, a function or symbol by its kind, and a value with a cycle as such.
|
|
9
|
+
*/
|
|
10
|
+
export function shown(value: unknown): string {
|
|
11
|
+
if (typeof value === 'number') return String(value);
|
|
12
|
+
if (typeof value === 'bigint') return `${value}n`;
|
|
13
|
+
if (typeof value === 'function') return 'a function';
|
|
14
|
+
if (typeof value === 'symbol') return 'a symbol';
|
|
15
|
+
try {
|
|
16
|
+
return (
|
|
17
|
+
JSON.stringify(value, (_, v: unknown) =>
|
|
18
|
+
typeof v === 'bigint'
|
|
19
|
+
? `${v}n`
|
|
20
|
+
: typeof v === 'symbol'
|
|
21
|
+
? 'a symbol'
|
|
22
|
+
: typeof v === 'number' && !Number.isFinite(v)
|
|
23
|
+
? String(v)
|
|
24
|
+
: v,
|
|
25
|
+
) ?? String(value)
|
|
26
|
+
);
|
|
27
|
+
} catch {
|
|
28
|
+
return 'a value that contains itself';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The keys of the options of the functions that choose a setting of a point group. */
|
|
33
|
+
export const SETTING_KEYS = ['setting', 'axes', 'uniqueAxis'] as const;
|
|
34
|
+
/** The key of the options of the functions that compare numbers of a caller. */
|
|
35
|
+
export const TOLERANCE_KEYS = ['tolerance'] as const;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Throws unless `options` is undefined or a plain object whose keys are all among `keys`: an
|
|
39
|
+
* options argument of the wrong type and a key the function does not know (a misspelling such as
|
|
40
|
+
* `uniqueaxis`, which would otherwise be ignored) are RangeErrors. The option `convention`, removed
|
|
41
|
+
* in 0.11.0, gets a message of its own.
|
|
42
|
+
*/
|
|
43
|
+
export function checkOptions(options: unknown, keys: readonly string[], what: string): void {
|
|
44
|
+
if (options === undefined) return;
|
|
45
|
+
if (!isPlainObject(options)) {
|
|
46
|
+
throw new RangeError(`${what} takes its options as an object { ${keys.join(', ')} }, not ${shown(options)}`);
|
|
47
|
+
}
|
|
48
|
+
const unknown = Object.keys(options).filter((k) => !keys.includes(k));
|
|
49
|
+
if (unknown.includes('convention')) {
|
|
50
|
+
throw new RangeError(
|
|
51
|
+
'The option convention was removed in symcrys 0.11.0: symcrys follows ITC only (CHANGELOG.md, 0.11.0)',
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (unknown.length > 0) {
|
|
55
|
+
throw new RangeError(
|
|
56
|
+
`${what} takes the options ${keys.join(', ')}, not ${unknown.map((k) => JSON.stringify(k)).join(', ')}`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The options restricted to `keys`, for a function that passes part of its options on. */
|
|
62
|
+
export function pick<T extends object>(options: T | undefined, keys: readonly string[]): Partial<T> | undefined {
|
|
63
|
+
if (options === undefined) return undefined;
|
|
64
|
+
return Object.fromEntries(Object.entries(options).filter(([k]) => keys.includes(k))) as Partial<T>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Throws unless `v` is an array of `length` finite numbers. */
|
|
68
|
+
export function checkNumbers(v: unknown, length: number, what: string): asserts v is readonly number[] {
|
|
69
|
+
if (!Array.isArray(v) || v.length !== length || !v.every((x) => typeof x === 'number' && Number.isFinite(x))) {
|
|
70
|
+
throw new RangeError(`${what} must be ${length} finite numbers, not ${shown(v)}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** True for a plain object: `{ … }` or one without a prototype, not an array, a Map or a Date. */
|
|
75
|
+
export function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
76
|
+
if (typeof v !== 'object' || v === null) return false;
|
|
77
|
+
const proto = Object.getPrototypeOf(v) as unknown;
|
|
78
|
+
return proto === Object.prototype || proto === null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Throws unless `v` is a plain object. */
|
|
82
|
+
export function checkObject(v: unknown, what: string): asserts v is object {
|
|
83
|
+
if (!isPlainObject(v)) throw new RangeError(`${what}, not ${shown(v)}`);
|
|
84
|
+
}
|
package/src/closure.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The group generated by operations a caller passes in, within a tolerance, and that group made
|
|
3
|
+
* exact (docs/conventions.md, "Tolerances"). Operations read from a file or computed elsewhere carry
|
|
4
|
+
* rounding errors (√3/2 written as 0.866025); compared within the tolerance they still generate the
|
|
5
|
+
* right group, but its matrices are not exactly a group, and a tensor form computed from them has
|
|
6
|
+
* the wrong number of free parameters (#89). `idealGroup` therefore replaces them by the exact group
|
|
7
|
+
* nearest to them before anything is computed from them.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { det, inverse, multiply, snap, trace, transpose, type Mat3 } from './linalg.js';
|
|
11
|
+
import type { CartesianOperation } from './operations.js';
|
|
12
|
+
import { IDEAL_PASSES, IDEAL_SETTLED, IDEAL_SNAP, IDEAL_UNSETTLED, PRODUCT_MATCH } from './tolerances.js';
|
|
13
|
+
|
|
14
|
+
const IDENTITY: CartesianOperation = {
|
|
15
|
+
cartesian: [
|
|
16
|
+
[1, 0, 0],
|
|
17
|
+
[0, 1, 0],
|
|
18
|
+
[0, 0, 1],
|
|
19
|
+
],
|
|
20
|
+
timeReversal: false,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** No finite group of Cartesian operations that symcrys computes with has more elements (m-3m1' has 96). */
|
|
24
|
+
export const MAX_GROUP_ORDER = 200;
|
|
25
|
+
|
|
26
|
+
/** True if the two operations agree in time reversal and in every matrix entry within `tolerance`. */
|
|
27
|
+
export function sameWithin(a: CartesianOperation, b: CartesianOperation, tolerance: number): boolean {
|
|
28
|
+
return (
|
|
29
|
+
a.timeReversal === b.timeReversal &&
|
|
30
|
+
a.cartesian.every((row, i) => row.every((v, j) => Math.abs(v - b.cartesian[i][j]) < tolerance))
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The kind of an operation (rounded trace, sign of the determinant, time reversal): equal operations have the same. */
|
|
35
|
+
const kindOf = (op: CartesianOperation): string =>
|
|
36
|
+
`${Math.round(trace(op.cartesian))}${det(op.cartesian) > 0 ? '+' : '-'}${op.timeReversal ? "'" : ''}`;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The group the operations generate within `tolerance` (two operations equal when they agree in
|
|
40
|
+
* time reversal and in every matrix entry within it), the identity first, then the operations of
|
|
41
|
+
* the list that are new, then products in the order they are found; or undefined if it has more
|
|
42
|
+
* than `maxOrder` elements. A new operation is compared only with the operations of its kind.
|
|
43
|
+
*/
|
|
44
|
+
export function closeWithin(
|
|
45
|
+
operations: readonly CartesianOperation[],
|
|
46
|
+
tolerance: number,
|
|
47
|
+
maxOrder = MAX_GROUP_ORDER,
|
|
48
|
+
): CartesianOperation[] | undefined {
|
|
49
|
+
const group: CartesianOperation[] = [];
|
|
50
|
+
const byKind = new Map<string, CartesianOperation[]>();
|
|
51
|
+
const has = (op: CartesianOperation): boolean =>
|
|
52
|
+
(byKind.get(kindOf(op)) ?? []).some((g) => sameWithin(g, op, tolerance));
|
|
53
|
+
const push = (op: CartesianOperation): void => {
|
|
54
|
+
group.push(op);
|
|
55
|
+
const k = kindOf(op);
|
|
56
|
+
const list = byKind.get(k);
|
|
57
|
+
if (list) list.push(op);
|
|
58
|
+
else byKind.set(k, [op]);
|
|
59
|
+
};
|
|
60
|
+
push(IDENTITY);
|
|
61
|
+
const generators: CartesianOperation[] = [];
|
|
62
|
+
for (const s of operations) {
|
|
63
|
+
const op: CartesianOperation = { cartesian: s.cartesian, timeReversal: s.timeReversal };
|
|
64
|
+
if (has(op)) continue;
|
|
65
|
+
generators.push(op);
|
|
66
|
+
for (let i = 0; i < group.length; i++) {
|
|
67
|
+
for (const t of generators) {
|
|
68
|
+
const product: CartesianOperation = {
|
|
69
|
+
cartesian: multiply(group[i].cartesian, t.cartesian),
|
|
70
|
+
timeReversal: group[i].timeReversal !== t.timeReversal,
|
|
71
|
+
};
|
|
72
|
+
if (has(product)) continue;
|
|
73
|
+
if (group.length >= maxOrder) return undefined;
|
|
74
|
+
push(product);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return group;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The group the operations generate within `tolerance` (`closeWithin`); throws a RangeError if it
|
|
83
|
+
* has more than `MAX_GROUP_ORDER` elements.
|
|
84
|
+
*/
|
|
85
|
+
export function closedGroup(operations: readonly CartesianOperation[], tolerance: number): CartesianOperation[] {
|
|
86
|
+
const group = closeWithin(operations, tolerance);
|
|
87
|
+
if (!group) {
|
|
88
|
+
throw new RangeError(
|
|
89
|
+
`The operations generate no finite point group within the tolerance ${tolerance} (more than ${MAX_GROUP_ORDER} elements): matrices with errors too large for the tolerance (pass a larger one, about ten times their rounding), or a rotation by an angle that is not 360°/n`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return group;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The largest difference of two matrix entries. */
|
|
96
|
+
function distance(a: Mat3, b: Mat3): number {
|
|
97
|
+
let d = 0;
|
|
98
|
+
for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) d = Math.max(d, Math.abs(a[i][j] - b[i][j]));
|
|
99
|
+
return d;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const add = (a: Mat3, b: Mat3): Mat3 => a.map((row, i) => row.map((v, j) => v + b[i][j])) as unknown as Mat3;
|
|
103
|
+
const scale = (a: Mat3, s: number): Mat3 => a.map((row) => row.map((v) => v * s)) as unknown as Mat3;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The square root of a symmetric positive definite matrix and its inverse, by the iteration of
|
|
107
|
+
* Denman and Beavers (Y ← (Y + Z⁻¹)/2, Z ← (Z + Y⁻¹)/2 from Y = M, Z = 1), which converges
|
|
108
|
+
* quadratically; here M lies near the identity.
|
|
109
|
+
*/
|
|
110
|
+
function squareRoots(m: Mat3): [Mat3, Mat3] {
|
|
111
|
+
let y = m;
|
|
112
|
+
let z = IDENTITY.cartesian;
|
|
113
|
+
for (let pass = 0; pass < IDEAL_PASSES; pass++) {
|
|
114
|
+
const nextY = scale(add(y, inverse(z)), 0.5);
|
|
115
|
+
const nextZ = scale(add(z, inverse(y)), 0.5);
|
|
116
|
+
const change = distance(nextY, y);
|
|
117
|
+
y = nextY;
|
|
118
|
+
z = nextZ;
|
|
119
|
+
if (change < IDEAL_SETTLED) break;
|
|
120
|
+
}
|
|
121
|
+
return [y, z];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A group made exact: its operations, and how far they lie from the operations it was made from. */
|
|
125
|
+
export interface IdealGroup {
|
|
126
|
+
readonly operations: readonly CartesianOperation[];
|
|
127
|
+
/** The largest difference of a matrix entry from the closed operations of the caller. */
|
|
128
|
+
readonly deviation: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The group generated by `operations` within `tolerance` (`closeWithin`, at most `MAX_GROUP_ORDER`
|
|
133
|
+
* elements), made exact: each matrix f(x) is replaced by the mean of f(xy)·f(y)⁻¹ over the group,
|
|
134
|
+
* repeated until nothing changes. For an exact group this is f(x) itself; for matrices that are a
|
|
135
|
+
* group only within a small error the passes settle on an exact representation nearby (tested in
|
|
136
|
+
* closure.test.ts and tensors/tolerance.test.ts), and the invariant metric of the result makes it
|
|
137
|
+
* orthogonal. Entries within 10⁻¹² of 0, ±1/2, ±√2/2, ±√3/2, ±1 are then set to them, so that exact
|
|
138
|
+
* input stays exact. Throws a RangeError if the operations generate more than `MAX_GROUP_ORDER`
|
|
139
|
+
* elements or do not settle into a group (a tolerance too large or too small for their errors).
|
|
140
|
+
*/
|
|
141
|
+
export function idealGroup(operations: readonly CartesianOperation[], tolerance: number): IdealGroup {
|
|
142
|
+
const group = closedGroup(operations, tolerance);
|
|
143
|
+
const n = group.length;
|
|
144
|
+
// The product table: the element nearest to each product, of the same time reversal, sought
|
|
145
|
+
// among the elements of its kind first and among all if none of them is near enough.
|
|
146
|
+
const byKind = new Map<string, number[]>();
|
|
147
|
+
group.forEach((g, k) => {
|
|
148
|
+
const list = byKind.get(kindOf(g));
|
|
149
|
+
if (list) list.push(k);
|
|
150
|
+
else byKind.set(kindOf(g), [k]);
|
|
151
|
+
});
|
|
152
|
+
const all = group.map((_, k) => k);
|
|
153
|
+
const nearest = (product: CartesianOperation, candidates: readonly number[]) => {
|
|
154
|
+
let best = -1;
|
|
155
|
+
let bestDistance = Infinity;
|
|
156
|
+
for (const k of candidates) {
|
|
157
|
+
if (group[k].timeReversal !== product.timeReversal) continue;
|
|
158
|
+
const d = distance(group[k].cartesian, product.cartesian);
|
|
159
|
+
if (d < bestDistance) {
|
|
160
|
+
best = k;
|
|
161
|
+
bestDistance = d;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return bestDistance < PRODUCT_MATCH ? best : -1;
|
|
165
|
+
};
|
|
166
|
+
const table = group.map((a) =>
|
|
167
|
+
group.map((b) => {
|
|
168
|
+
const product: CartesianOperation = {
|
|
169
|
+
cartesian: multiply(a.cartesian, b.cartesian),
|
|
170
|
+
timeReversal: a.timeReversal !== b.timeReversal,
|
|
171
|
+
};
|
|
172
|
+
let k = nearest(product, byKind.get(kindOf(product)) ?? []);
|
|
173
|
+
if (k < 0) k = nearest(product, all);
|
|
174
|
+
if (k < 0) throw new RangeError(`The operations do not form a group within the tolerance ${tolerance}`);
|
|
175
|
+
return k;
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
let f: Mat3[] = group.map((g) => g.cartesian);
|
|
179
|
+
for (let pass = 0; ; pass++) {
|
|
180
|
+
const inverses = f.map(inverse);
|
|
181
|
+
const next = f.map((_, x) => {
|
|
182
|
+
const sum = [
|
|
183
|
+
[0, 0, 0],
|
|
184
|
+
[0, 0, 0],
|
|
185
|
+
[0, 0, 0],
|
|
186
|
+
];
|
|
187
|
+
for (let y = 0; y < n; y++) {
|
|
188
|
+
const p = multiply(f[table[x][y]], inverses[y]);
|
|
189
|
+
for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) sum[i][j] += p[i][j] / n;
|
|
190
|
+
}
|
|
191
|
+
return sum as unknown as Mat3;
|
|
192
|
+
});
|
|
193
|
+
const change = Math.max(...next.map((m, k) => distance(m, f[k])));
|
|
194
|
+
f = next;
|
|
195
|
+
if (change < IDEAL_SETTLED) break;
|
|
196
|
+
if (pass >= IDEAL_PASSES) {
|
|
197
|
+
if (change < IDEAL_UNSETTLED) break;
|
|
198
|
+
throw new RangeError(`The operations do not settle into a group within the tolerance ${tolerance}`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// The representation is exact but need not be orthogonal: it may be an orthogonal one seen in a
|
|
202
|
+
// slightly oblique frame. Its invariant metric M = (1/n) Σ fᵀf gives that frame, and M^½ f M^-½
|
|
203
|
+
// is orthogonal.
|
|
204
|
+
const metric = f.reduce((sum, m) => add(sum, scale(multiply(transpose(m), m), 1 / n)), scale(IDENTITY.cartesian, 0));
|
|
205
|
+
const [root, inverseRoot] = squareRoots(metric);
|
|
206
|
+
f = f.map((m) => multiply(multiply(root, m), inverseRoot));
|
|
207
|
+
// Only rounding of the last bits is snapped: a group turned by a small angle has entries near the
|
|
208
|
+
// exact values that are not equal to them.
|
|
209
|
+
const exact = f.map((m, k) => ({ cartesian: snap(m, IDEAL_SNAP), timeReversal: group[k].timeReversal }));
|
|
210
|
+
return {
|
|
211
|
+
operations: exact,
|
|
212
|
+
deviation: Math.max(...exact.map((op, k) => distance(op.cartesian, group[k].cartesian))),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The change of the coordinate system by a matrix–column pair (P, p) after ITA (6th ed., 2016)
|
|
3
|
+
* Chapter 1.5 (§§ 1.5.1.3, 1.5.2, pp. 83–85), for d = 2 and d = 3 (#120, #137 G2). Internal; the
|
|
4
|
+
* entry points wrap it with their matrix types (docs/conventions.md, "Change of the coordinate
|
|
5
|
+
* system").
|
|
6
|
+
*
|
|
7
|
+
* The linear part P, the origin shift p and the operations (W, w) are read each number as the
|
|
8
|
+
* simplest fraction within the tolerance and transformed exactly, so that W′ = P⁻¹WP is exactly
|
|
9
|
+
* integral where it is and every result, the inverse pair included, is read back exactly; points,
|
|
10
|
+
* direction and plane coefficients and the metric are floating point.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { checkObject, checkOptions, shown, TOLERANCE_KEYS } from './checks.js';
|
|
14
|
+
import { toleranceOf, type ToleranceOptions } from './tolerances.js';
|
|
15
|
+
import { deepFreeze } from './freeze.js';
|
|
16
|
+
import type { Mat3, Vec3 } from './linalg.js';
|
|
17
|
+
import {
|
|
18
|
+
ONE,
|
|
19
|
+
add,
|
|
20
|
+
isInteger,
|
|
21
|
+
multiply,
|
|
22
|
+
simplestFraction,
|
|
23
|
+
negate,
|
|
24
|
+
rational,
|
|
25
|
+
subtract,
|
|
26
|
+
toNumber,
|
|
27
|
+
ZERO,
|
|
28
|
+
type Rational,
|
|
29
|
+
} from './spaceGroups/rational.js';
|
|
30
|
+
|
|
31
|
+
/** A matrix as rows of numbers. */
|
|
32
|
+
type Rows = readonly (readonly number[])[];
|
|
33
|
+
type RMatrix = readonly (readonly Rational[])[];
|
|
34
|
+
|
|
35
|
+
/** The pair (P, p): the new basis (a′, b′, c′) = (a, b, c)·P and the new origin O′ = O + p. */
|
|
36
|
+
export interface GenericTransformation {
|
|
37
|
+
/** P, the linear part: its columns are the new basis vectors in the old basis (ITA eq. (1.5.1.4)). */
|
|
38
|
+
readonly linearPart: Rows;
|
|
39
|
+
/** p, the origin shift: the coordinates of the new origin in the old coordinate system; default zero. */
|
|
40
|
+
readonly originShift?: readonly number[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** An operation (W, w) in the lattice basis, x ↦ W·x + w. */
|
|
44
|
+
export interface GenericOperation {
|
|
45
|
+
/** The linear part W, integer entries. */
|
|
46
|
+
readonly matrix: Rows;
|
|
47
|
+
/** The translation part w; default zero (an operation of a point group). */
|
|
48
|
+
readonly translation?: readonly number[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The quantities to transform; each is optional. */
|
|
52
|
+
export interface GenericQuantities {
|
|
53
|
+
/** Point coordinates x: x′ = P⁻¹(x − p), eq. (1.5.1.7). */
|
|
54
|
+
readonly points?: readonly (readonly number[])[];
|
|
55
|
+
/** Coefficients [uvw] of vectors (directions): [uvw]′ = P⁻¹[uvw], eq. (1.5.2.3). */
|
|
56
|
+
readonly directions?: readonly (readonly number[])[];
|
|
57
|
+
/** Miller indices (hkl) of planes, or coefficients of reciprocal vectors: (hkl)′ = (hkl)P, eq. (1.5.2.2). */
|
|
58
|
+
readonly planes?: readonly (readonly number[])[];
|
|
59
|
+
/** The metric tensor G: G′ = PᵀGP, eq. (1.5.2.4). */
|
|
60
|
+
readonly metric?: Rows;
|
|
61
|
+
/** Operations (W, w): W′ = P⁻¹WP, w′ = P⁻¹(w + (W − I)p), eqs. (1.5.2.12), (1.5.2.13). */
|
|
62
|
+
readonly operations?: readonly GenericOperation[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The result: the inverse pair and every given quantity in the new coordinate system. */
|
|
66
|
+
export interface GenericChange {
|
|
67
|
+
/** The inverse pair (Q, q) = (P, p)⁻¹ = (P⁻¹, −P⁻¹p), eq. (1.5.1.7), which takes the new coordinates back. */
|
|
68
|
+
readonly inverse: { readonly linearPart: Rows; readonly originShift: readonly number[] };
|
|
69
|
+
readonly points?: Rows;
|
|
70
|
+
readonly directions?: Rows;
|
|
71
|
+
readonly planes?: Rows;
|
|
72
|
+
readonly metric?: Rows;
|
|
73
|
+
readonly operations?: readonly { readonly matrix: Rows; readonly translation: readonly number[] }[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const QUANTITY_KEYS = ['points', 'directions', 'planes', 'metric', 'operations'] as const;
|
|
77
|
+
|
|
78
|
+
function isRows(m: unknown, d: number): m is Rows {
|
|
79
|
+
return (
|
|
80
|
+
Array.isArray(m) &&
|
|
81
|
+
m.length === d &&
|
|
82
|
+
m.every((r) => Array.isArray(r) && r.length === d && r.every((x) => typeof x === 'number' && Number.isFinite(x)))
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function isVector(v: unknown, d: number): v is readonly number[] {
|
|
87
|
+
return Array.isArray(v) && v.length === d && v.every((x) => typeof x === 'number' && Number.isFinite(x));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function product(a: RMatrix, b: RMatrix): RMatrix {
|
|
91
|
+
return a.map((row) => b[0].map((_, j) => row.reduce((s, x, k) => add(s, multiply(x, b[k][j])), ZERO)));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function apply(a: RMatrix, v: readonly Rational[]): Rational[] {
|
|
95
|
+
return a.map((row) => row.reduce((s, x, k) => add(s, multiply(x, v[k])), ZERO));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The determinant of a 2 × 2 or 3 × 3 rational matrix. */
|
|
99
|
+
function determinant(m: RMatrix): Rational {
|
|
100
|
+
const t = (i: number, j: number) => m[i][j];
|
|
101
|
+
if (m.length === 2) return subtract(multiply(t(0, 0), t(1, 1)), multiply(t(0, 1), t(1, 0)));
|
|
102
|
+
const minor = (i: number, j: number, k: number, l: number) =>
|
|
103
|
+
subtract(multiply(t(i, j), t(k, l)), multiply(t(i, l), t(k, j)));
|
|
104
|
+
return add(
|
|
105
|
+
subtract(multiply(t(0, 0), minor(1, 1, 2, 2)), multiply(t(0, 1), minor(1, 0, 2, 2))),
|
|
106
|
+
multiply(t(0, 2), minor(1, 0, 2, 1)),
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The inverse of a regular 2 × 2 or 3 × 3 rational matrix, by the adjugate. */
|
|
111
|
+
function inverse(m: RMatrix): RMatrix {
|
|
112
|
+
const det = determinant(m);
|
|
113
|
+
const over = (x: Rational) => multiply(x, rational(det[1], det[0]));
|
|
114
|
+
if (m.length === 2) {
|
|
115
|
+
return [
|
|
116
|
+
[over(m[1][1]), over(negate(m[0][1]))],
|
|
117
|
+
[over(negate(m[1][0])), over(m[0][0])],
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
const c = (i: number, j: number) => {
|
|
121
|
+
const rows = [0, 1, 2].filter((r) => r !== i);
|
|
122
|
+
const cols = [0, 1, 2].filter((k) => k !== j);
|
|
123
|
+
const minor = subtract(
|
|
124
|
+
multiply(m[rows[0]][cols[0]], m[rows[1]][cols[1]]),
|
|
125
|
+
multiply(m[rows[0]][cols[1]], m[rows[1]][cols[0]]),
|
|
126
|
+
);
|
|
127
|
+
return (i + j) % 2 === 0 ? minor : negate(minor);
|
|
128
|
+
};
|
|
129
|
+
// The inverse is the transposed matrix of cofactors over the determinant.
|
|
130
|
+
return [0, 1, 2].map((i) => [0, 1, 2].map((j) => over(c(j, i))));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const numbers = (v: readonly Rational[]): number[] => v.map((x) => toNumber(x) + 0);
|
|
134
|
+
const floats = (m: RMatrix): number[][] => m.map((row) => numbers(row));
|
|
135
|
+
|
|
136
|
+
function floatProduct(a: Rows, b: Rows): number[][] {
|
|
137
|
+
return a.map((row) => b[0].map((_, j) => row.reduce((s, x, k) => s + x * b[k][j], 0) + 0));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function floatApply(a: Rows, v: readonly number[]): number[] {
|
|
141
|
+
return a.map((row) => row.reduce((s, x, k) => s + x * v[k], 0) + 0);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The change of the coordinate system (P, p) applied to the given quantities, in d = 2 or 3
|
|
146
|
+
* dimensions (ITA 2016 §§ 1.5.1.3, 1.5.2). `who` names the calling function in the messages.
|
|
147
|
+
*/
|
|
148
|
+
export function changeCoordinates(
|
|
149
|
+
d: 2 | 3,
|
|
150
|
+
transformation: GenericTransformation,
|
|
151
|
+
quantities: GenericQuantities,
|
|
152
|
+
options: ToleranceOptions | undefined,
|
|
153
|
+
who: string,
|
|
154
|
+
): GenericChange {
|
|
155
|
+
try {
|
|
156
|
+
return transform(d, transformation, quantities, options, who);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
// Exact arithmetic that leaves the safe integers names the function too.
|
|
159
|
+
if (e instanceof RangeError && !e.message.startsWith(`${who}`))
|
|
160
|
+
throw new RangeError(`${who}: ${e.message}`, { cause: e });
|
|
161
|
+
throw e;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function transform(
|
|
166
|
+
d: 2 | 3,
|
|
167
|
+
transformation: GenericTransformation,
|
|
168
|
+
quantities: GenericQuantities,
|
|
169
|
+
options: ToleranceOptions | undefined,
|
|
170
|
+
who: string,
|
|
171
|
+
): GenericChange {
|
|
172
|
+
checkOptions(options, TOLERANCE_KEYS, who);
|
|
173
|
+
const tolerance = toleranceOf(options, 'operations');
|
|
174
|
+
checkObject(transformation, `${who} takes the transformation { linearPart, originShift }`);
|
|
175
|
+
checkObject(quantities, `${who} takes the quantities { ${QUANTITY_KEYS.join(', ')} }`);
|
|
176
|
+
for (const key of Object.keys(transformation)) {
|
|
177
|
+
if (key !== 'linearPart' && key !== 'originShift') {
|
|
178
|
+
throw new RangeError(`${who}: unknown field ${shown(key)} of the transformation (linearPart, originShift)`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (const key of Object.keys(quantities)) {
|
|
182
|
+
if (!(QUANTITY_KEYS as readonly string[]).includes(key)) {
|
|
183
|
+
throw new RangeError(`${who}: unknown quantity ${shown(key)} (${QUANTITY_KEYS.join(', ')})`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const P0 = transformation.linearPart;
|
|
187
|
+
if (!isRows(P0, d)) throw new RangeError(`${who}: the linear part P must be a ${d} × ${d} matrix, not ${shown(P0)}`);
|
|
188
|
+
const fraction = (x: number, what: string): Rational => simplestFraction(x, tolerance, what);
|
|
189
|
+
const P = P0.map((row) => row.map((x) => fraction(x, 'entry of P')));
|
|
190
|
+
if (determinant(P)[0] === 0) throw new RangeError(`${who}: the linear part P is singular (det P = 0)`);
|
|
191
|
+
// Only a missing origin shift is zero; null is a wrong value, not a missing one.
|
|
192
|
+
const shift0 =
|
|
193
|
+
transformation.originShift === undefined ? Array.from({ length: d }, () => 0) : transformation.originShift;
|
|
194
|
+
if (!isVector(shift0, d))
|
|
195
|
+
throw new RangeError(`${who}: the origin shift p must be ${d} numbers, not ${shown(shift0)}`);
|
|
196
|
+
const p = shift0.map((x) => fraction(x, 'coordinate of p'));
|
|
197
|
+
const Q = inverse(P);
|
|
198
|
+
const q = apply(Q, p).map(negate);
|
|
199
|
+
const Pf = floats(P);
|
|
200
|
+
const Qf = floats(Q);
|
|
201
|
+
const pf = numbers(p);
|
|
202
|
+
|
|
203
|
+
const vectors = (name: string, list: unknown): readonly (readonly number[])[] => {
|
|
204
|
+
if (!Array.isArray(list) || !list.every((v) => isVector(v, d))) {
|
|
205
|
+
throw new RangeError(`${who}: ${name} must be a list of vectors of ${d} numbers, not ${shown(list)}`);
|
|
206
|
+
}
|
|
207
|
+
return list as readonly (readonly number[])[];
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const result: Record<string, unknown> = { inverse: { linearPart: floats(Q), originShift: numbers(q) } };
|
|
211
|
+
if (quantities.points !== undefined) {
|
|
212
|
+
result.points = vectors('points', quantities.points).map((x) =>
|
|
213
|
+
floatApply(
|
|
214
|
+
Qf,
|
|
215
|
+
x.map((xi, i) => xi - pf[i]),
|
|
216
|
+
),
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
if (quantities.directions !== undefined) {
|
|
220
|
+
result.directions = vectors('directions', quantities.directions).map((u) => floatApply(Qf, u));
|
|
221
|
+
}
|
|
222
|
+
if (quantities.planes !== undefined) {
|
|
223
|
+
// A row (hkl) times P: the j-th index is Σ_i h_i P_ij.
|
|
224
|
+
result.planes = vectors('planes', quantities.planes).map((h) =>
|
|
225
|
+
Pf[0].map((_, j) => h.reduce((s, hi, i) => s + hi * Pf[i][j], 0) + 0),
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
if (quantities.metric !== undefined) {
|
|
229
|
+
const G = quantities.metric;
|
|
230
|
+
if (!isRows(G, d)) throw new RangeError(`${who}: the metric must be a ${d} × ${d} matrix, not ${shown(G)}`);
|
|
231
|
+
const Pt = Pf[0].map((_, j) => Pf.map((row) => row[j]));
|
|
232
|
+
result.metric = floatProduct(floatProduct(Pt, G), Pf);
|
|
233
|
+
}
|
|
234
|
+
if (quantities.operations !== undefined) {
|
|
235
|
+
const ops: unknown = quantities.operations;
|
|
236
|
+
if (!Array.isArray(ops)) throw new RangeError(`${who}: operations must be a list, not ${shown(ops)}`);
|
|
237
|
+
result.operations = ops.map((op: unknown, n) => {
|
|
238
|
+
checkObject(op, `${who}: operation ${n + 1} is a record { matrix, translation }`);
|
|
239
|
+
const { matrix, translation } = op as GenericOperation;
|
|
240
|
+
if (!isRows(matrix, d)) {
|
|
241
|
+
throw new RangeError(`${who}: the matrix W of operation ${n + 1} must be ${d} × ${d}, not ${shown(matrix)}`);
|
|
242
|
+
}
|
|
243
|
+
const W = matrix.map((row) =>
|
|
244
|
+
row.map((x) => {
|
|
245
|
+
if (Math.abs(x - Math.round(x)) >= tolerance) {
|
|
246
|
+
throw new RangeError(
|
|
247
|
+
`${who}: the matrix W of operation ${n + 1} has integer entries, not ${shown(matrix)}`,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
return rational(Math.round(x));
|
|
251
|
+
}),
|
|
252
|
+
);
|
|
253
|
+
if (determinant(W)[1] !== 1 || Math.abs(determinant(W)[0]) !== 1) {
|
|
254
|
+
throw new RangeError(`${who}: the matrix W of operation ${n + 1} has det W = ±1, not ${shown(matrix)}`);
|
|
255
|
+
}
|
|
256
|
+
const t0 = translation === undefined ? Array.from({ length: d }, () => 0) : translation;
|
|
257
|
+
if (!isVector(t0, d)) {
|
|
258
|
+
throw new RangeError(`${who}: the translation of operation ${n + 1} must be ${d} numbers, not ${shown(t0)}`);
|
|
259
|
+
}
|
|
260
|
+
const w = t0.map((x) => fraction(x, 'translation'));
|
|
261
|
+
const W1 = product(product(Q, W), P);
|
|
262
|
+
if (!W1.every((row) => row.every(isInteger))) {
|
|
263
|
+
throw new RangeError(
|
|
264
|
+
`${who}: operation ${n + 1} has no integral matrix in the new basis (P⁻¹WP = ${shown(floats(W1))}): ` +
|
|
265
|
+
'the new basis does not span a lattice that the operation maps onto itself',
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
// w′ = Q(w + (W − I)p)
|
|
269
|
+
const WminusI = W.map((row, i) => row.map((x, j) => (i === j ? subtract(x, ONE) : x)));
|
|
270
|
+
const shifted = apply(WminusI, p).map((x, i) => add(w[i], x));
|
|
271
|
+
return { matrix: floats(W1), translation: numbers(apply(Q, shifted)) };
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return result as unknown as GenericChange;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// --- the three-dimensional API (symcrys, symcrys/magnetic) -------------------------------------------
|
|
278
|
+
|
|
279
|
+
/** A change of the coordinate system (P, p) (ITA 2016 § 1.5.1.3, p. 83): (a′, b′, c′) = (a, b, c)·P, origin O′ = O + p. */
|
|
280
|
+
export interface CoordinateTransformation {
|
|
281
|
+
/** P, the linear part: its columns are the new basis vectors a′, b′, c′ in the old basis (eq. (1.5.1.4)); det P ≠ 0, also negative. */
|
|
282
|
+
readonly linearPart: Mat3;
|
|
283
|
+
/** p, the origin shift: the coordinates of the new origin in the old coordinate system; default zero. */
|
|
284
|
+
readonly originShift?: Vec3;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** An operation (W, w) in the lattice basis, x ↦ W·x + w; a record of `operations` passes as it is. */
|
|
288
|
+
export interface AffineOperation {
|
|
289
|
+
/** The linear part W, integer entries. */
|
|
290
|
+
readonly matrix: Mat3;
|
|
291
|
+
/** The translation part w; default zero, as for the operations of a point group. */
|
|
292
|
+
readonly translation?: Vec3;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** An operation (W′, w′) in the new coordinate system. */
|
|
296
|
+
export interface TransformedOperation {
|
|
297
|
+
/** W′ = P⁻¹WP, integer entries. */
|
|
298
|
+
readonly matrix: Mat3;
|
|
299
|
+
/** w′ = P⁻¹(w + (W − I)p), not reduced modulo the lattice. */
|
|
300
|
+
readonly translation: Vec3;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** The quantities that `changeOfCoordinates` transforms; each is optional. */
|
|
304
|
+
export interface CoordinateQuantities {
|
|
305
|
+
/** Point coordinates x: x′ = P⁻¹(x − p) (eq. (1.5.1.7)). */
|
|
306
|
+
readonly points?: readonly Vec3[];
|
|
307
|
+
/** Coefficients [uvw] of vectors, e.g. directions: [uvw]′ = P⁻¹[uvw] (eq. (1.5.2.3)); not changed by the origin shift. */
|
|
308
|
+
readonly directions?: readonly Vec3[];
|
|
309
|
+
/** Miller indices (hkl) of planes: (hkl)′ = (hkl)P (eq. (1.5.2.2)); not changed by the origin shift. */
|
|
310
|
+
readonly planes?: readonly Vec3[];
|
|
311
|
+
/** The metric tensor G: G′ = PᵀGP (eq. (1.5.2.4)). */
|
|
312
|
+
readonly metric?: Mat3;
|
|
313
|
+
/** Operations (W, w): W′ = P⁻¹WP, w′ = P⁻¹(w + (W − I)p) (eqs. (1.5.2.12), (1.5.2.13)). */
|
|
314
|
+
readonly operations?: readonly AffineOperation[];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The inverse change of the coordinate system (Q, q) = (P, p)⁻¹. */
|
|
318
|
+
export interface InverseTransformation {
|
|
319
|
+
/** Q = P⁻¹. */
|
|
320
|
+
readonly linearPart: Mat3;
|
|
321
|
+
/** q = −P⁻¹p. */
|
|
322
|
+
readonly originShift: Vec3;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** The result of `changeOfCoordinates`: the inverse pair and each given quantity in the new coordinate system. */
|
|
326
|
+
export interface CoordinateChange {
|
|
327
|
+
/** (Q, q) = (P⁻¹, −P⁻¹p), which takes the new coordinates back to the old (eq. (1.5.1.7)). */
|
|
328
|
+
readonly inverse: InverseTransformation;
|
|
329
|
+
/** The points, as given, in the new coordinates. */
|
|
330
|
+
readonly points?: readonly Vec3[];
|
|
331
|
+
/** The vector coefficients, as given, in the new basis. */
|
|
332
|
+
readonly directions?: readonly Vec3[];
|
|
333
|
+
/** The Miller indices, as given, in the new basis. */
|
|
334
|
+
readonly planes?: readonly Vec3[];
|
|
335
|
+
/** The metric tensor in the new basis. */
|
|
336
|
+
readonly metric?: Mat3;
|
|
337
|
+
/** The operations, as given, in the new coordinate system. */
|
|
338
|
+
readonly operations?: readonly TransformedOperation[];
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Changes the coordinate system by the pair (P, p) of ITA (6th ed., 2016) § 1.5.1.3 (p. 83): the
|
|
343
|
+
* new basis is (a′, b′, c′) = (a, b, c)·P and the new origin lies at p. Transforms the given
|
|
344
|
+
* quantities by ITA's equations (§ 1.5.2, pp. 83–85): point coordinates x′ = P⁻¹(x − p), vector
|
|
345
|
+
* coefficients [uvw]′ = P⁻¹[uvw], Miller indices (hkl)′ = (hkl)P, the metric tensor G′ = PᵀGP, and
|
|
346
|
+
* operations W′ = P⁻¹WP, w′ = P⁻¹(w + (W − I)p); and gives the inverse pair (Q, q) = (P⁻¹, −P⁻¹p).
|
|
347
|
+
* det P may be negative (a change of handedness, which ITA allows; § 1.5.1.2, p. 76).
|
|
348
|
+
*
|
|
349
|
+
* P, p and the operations are read each number as the simplest fraction within the tolerance
|
|
350
|
+
* (default `EPSILON`; 0.3333333 is 1/3) and transformed exactly, so that every result, the inverse
|
|
351
|
+
* pair included, can be passed back; the other quantities are floating point. P is rational in a
|
|
352
|
+
* change between lattice bases, as in ITA; an irrational P (a rotation by 30°) is read as the
|
|
353
|
+
* nearby fractions, about as exact as the tolerance, and a much smaller tolerance can make the exact
|
|
354
|
+
* products leave the safe integers, a RangeError.
|
|
355
|
+
* A transformed
|
|
356
|
+
* operation is `{ matrix, translation }` alone: other fields of the record (`cartesian`, in the
|
|
357
|
+
* magnetic API `timeReversal`) are not carried over, so they are matched by index. Throws a
|
|
358
|
+
* RangeError for a singular P, a quantity of the wrong shape, an unknown quantity, an operation with
|
|
359
|
+
* det W ≠ ±1, and one whose W′ is not integral (the new basis does not span a lattice that the
|
|
360
|
+
* operation maps onto itself).
|
|
361
|
+
*/
|
|
362
|
+
export function changeOfCoordinates(
|
|
363
|
+
transformation: CoordinateTransformation,
|
|
364
|
+
quantities: CoordinateQuantities,
|
|
365
|
+
options?: ToleranceOptions,
|
|
366
|
+
): CoordinateChange {
|
|
367
|
+
return deepFreeze(
|
|
368
|
+
changeCoordinates(3, transformation, quantities, options, 'changeOfCoordinates') as unknown as CoordinateChange,
|
|
369
|
+
);
|
|
370
|
+
}
|