symcrys 0.8.0 → 0.9.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 +11 -2
- package/dist/cell.d.ts +5 -2
- package/dist/cell.d.ts.map +1 -1
- package/dist/cell.js +7 -7
- package/dist/cell.js.map +1 -1
- package/dist/crystalClasses.d.ts +13 -8
- package/dist/crystalClasses.d.ts.map +1 -1
- package/dist/crystalClasses.js +13 -8
- package/dist/crystalClasses.js.map +1 -1
- package/dist/indices.d.ts +3 -1
- package/dist/indices.d.ts.map +1 -1
- package/dist/indices.js +6 -2
- package/dist/indices.js.map +1 -1
- package/dist/linalg.d.ts +7 -1
- package/dist/linalg.d.ts.map +1 -1
- package/dist/linalg.js +20 -3
- package/dist/linalg.js.map +1 -1
- package/dist/operations.d.ts +6 -0
- package/dist/operations.d.ts.map +1 -1
- package/dist/operations.js +19 -0
- package/dist/operations.js.map +1 -1
- package/dist/plane.d.ts +225 -0
- package/dist/plane.d.ts.map +1 -0
- package/dist/plane.js +219 -0
- package/dist/plane.js.map +1 -0
- package/dist/pointGroups/identify.d.ts +4 -2
- package/dist/pointGroups/identify.d.ts.map +1 -1
- package/dist/pointGroups/identify.js +5 -18
- package/dist/pointGroups/identify.js.map +1 -1
- package/dist/pointGroups/orbits.d.ts +1 -1
- package/dist/pointGroups/orbits.d.ts.map +1 -1
- package/dist/pointGroups/orbits.js +3 -3
- package/dist/pointGroups/orbits.js.map +1 -1
- package/dist/pointGroups/properties.d.ts +6 -2
- package/dist/pointGroups/properties.d.ts.map +1 -1
- package/dist/pointGroups/properties.js +6 -2
- package/dist/pointGroups/properties.js.map +1 -1
- package/dist/siteSymmetry.d.ts +5 -3
- package/dist/siteSymmetry.d.ts.map +1 -1
- package/dist/siteSymmetry.js +6 -2
- package/dist/siteSymmetry.js.map +1 -1
- package/dist/spaceGroups/affine.d.ts +81 -0
- package/dist/spaceGroups/affine.d.ts.map +1 -0
- package/dist/spaceGroups/affine.js +245 -0
- package/dist/spaceGroups/affine.js.map +1 -0
- package/dist/spaceGroups/cell.d.ts +58 -0
- package/dist/spaceGroups/cell.d.ts.map +1 -0
- package/dist/spaceGroups/cell.js +297 -0
- package/dist/spaceGroups/cell.js.map +1 -0
- package/dist/spaceGroups/describe.d.ts +44 -0
- package/dist/spaceGroups/describe.d.ts.map +1 -0
- package/dist/spaceGroups/describe.js +92 -0
- package/dist/spaceGroups/describe.js.map +1 -0
- package/dist/spaceGroups/planeGroupData.d.ts +44 -0
- package/dist/spaceGroups/planeGroupData.d.ts.map +1 -0
- package/dist/spaceGroups/planeGroupData.js +371 -0
- package/dist/spaceGroups/planeGroupData.js.map +1 -0
- package/dist/spaceGroups/planeGroups.d.ts +28 -0
- package/dist/spaceGroups/planeGroups.d.ts.map +1 -0
- package/dist/spaceGroups/planeGroups.js +61 -0
- package/dist/spaceGroups/planeGroups.js.map +1 -0
- package/dist/spaceGroups/planePointGroups.d.ts +31 -0
- package/dist/spaceGroups/planePointGroups.d.ts.map +1 -0
- package/dist/spaceGroups/planePointGroups.js +77 -0
- package/dist/spaceGroups/planePointGroups.js.map +1 -0
- package/dist/spaceGroups/positions.d.ts +65 -0
- package/dist/spaceGroups/positions.d.ts.map +1 -0
- package/dist/spaceGroups/positions.js +183 -0
- package/dist/spaceGroups/positions.js.map +1 -0
- package/dist/spaceGroups/rational.d.ts +43 -0
- package/dist/spaceGroups/rational.d.ts.map +1 -0
- package/dist/spaceGroups/rational.js +95 -0
- package/dist/spaceGroups/rational.js.map +1 -0
- package/dist/tensors/tensorForm.d.ts +3 -1
- package/dist/tensors/tensorForm.d.ts.map +1 -1
- package/dist/tensors/tensorForm.js +3 -1
- package/dist/tensors/tensorForm.js.map +1 -1
- package/dist/tensors/tensorProperties.d.ts +4 -2
- package/dist/tensors/tensorProperties.d.ts.map +1 -1
- package/dist/tensors/tensorProperties.js +4 -2
- package/dist/tensors/tensorProperties.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +7 -2
- package/src/cell.ts +9 -8
- package/src/crystalClasses.ts +13 -8
- package/src/indices.ts +5 -2
- package/src/linalg.ts +21 -2
- package/src/operations.ts +20 -0
- package/src/plane.ts +455 -0
- package/src/pointGroups/identify.ts +5 -19
- package/src/pointGroups/orbits.ts +3 -3
- package/src/pointGroups/properties.ts +6 -2
- package/src/siteSymmetry.ts +6 -3
- package/src/spaceGroups/affine.ts +315 -0
- package/src/spaceGroups/cell.ts +371 -0
- package/src/spaceGroups/describe.ts +133 -0
- package/src/spaceGroups/planeGroupData.ts +407 -0
- package/src/spaceGroups/planeGroups.ts +75 -0
- package/src/spaceGroups/planePointGroups.ts +90 -0
- package/src/spaceGroups/positions.ts +237 -0
- package/src/spaceGroups/rational.ts +115 -0
- package/src/tensors/tensorForm.ts +3 -1
- package/src/tensors/tensorProperties.ts +4 -2
- package/src/version.ts +1 -1
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Points of a plane group: their orbit, site symmetry and Wyckoff position (CLAUDE.md, "Plane
|
|
3
|
+
* groups (and later space groups)"). Points come from callers as floating-point coordinates in the
|
|
4
|
+
* lattice basis; whether an operation fixes a point is decided within a tolerance, as for the
|
|
5
|
+
* point groups (`siteSymmetry`). Internal for now.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { compose, equalModLattice, parseOperation, toPlain, type PlainOperation } from './affine.js';
|
|
9
|
+
import { planeGroupEntry, planeGroupOperations } from './planeGroups.js';
|
|
10
|
+
import { EPSILON } from '../tolerances.js';
|
|
11
|
+
|
|
12
|
+
/** Options of the functions that take a point. */
|
|
13
|
+
export interface PointOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Two points count as the same, modulo the lattice, if each coordinate differs by less than this
|
|
16
|
+
* from an integer (coordinates in the lattice basis). Default 10⁻⁶.
|
|
17
|
+
*/
|
|
18
|
+
readonly tolerance?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The site symmetry of a point under a plane group. */
|
|
22
|
+
export interface PlaneSiteSymmetry {
|
|
23
|
+
/**
|
|
24
|
+
* Indices into the operations of the group (`planeGroupOperations`) of those that map the point
|
|
25
|
+
* to itself modulo the lattice, ascending.
|
|
26
|
+
*/
|
|
27
|
+
readonly operations: readonly number[];
|
|
28
|
+
/**
|
|
29
|
+
* The operations of the site symmetry, each with the lattice translation that makes it fix the
|
|
30
|
+
* point (not only modulo the lattice).
|
|
31
|
+
*/
|
|
32
|
+
readonly fixing: readonly PlainOperation[];
|
|
33
|
+
/**
|
|
34
|
+
* The point set onto the fixed space of its site symmetry (the mean of its images under the
|
|
35
|
+
* fixing operations), in [0, 1), which removes rounding errors within the tolerance.
|
|
36
|
+
*/
|
|
37
|
+
readonly point: readonly number[];
|
|
38
|
+
/** The number of points in the conventional cell equivalent to it. */
|
|
39
|
+
readonly multiplicity: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The Wyckoff position of a point. */
|
|
43
|
+
export interface PlaneWyckoffPosition {
|
|
44
|
+
/** The Wyckoff letter, as ITA prints it. */
|
|
45
|
+
readonly letter: string;
|
|
46
|
+
/** The multiplicity, as ITA prints it. */
|
|
47
|
+
readonly multiplicity: number;
|
|
48
|
+
/** The oriented site-symmetry symbol, as ITA prints it. */
|
|
49
|
+
readonly siteSymmetry: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function tolerance(options?: PointOptions): number {
|
|
53
|
+
const t = options?.tolerance ?? EPSILON;
|
|
54
|
+
// Modulo the lattice two coordinates differ by at most 1/2, so a tolerance of 1/2 or more says nothing.
|
|
55
|
+
if (!(typeof t === 'number' && t > 0 && t < 0.5)) {
|
|
56
|
+
throw new RangeError(`The tolerance must lie between 0 and 1/2, not ${String(t)}`);
|
|
57
|
+
}
|
|
58
|
+
return t;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function checkPoint(point: readonly number[]): void {
|
|
62
|
+
if (point.length !== 2 || !point.every(Number.isFinite)) {
|
|
63
|
+
throw new RangeError(`A point of the plane has two finite coordinates, not ${JSON.stringify(point)}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const mod1 = (v: number): number => {
|
|
68
|
+
const r = v - Math.floor(v);
|
|
69
|
+
return r >= 1 ? 0 : r + 0;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
function image(op: PlainOperation, p: readonly number[]): number[] {
|
|
73
|
+
return op.matrix.map((row, i) => row[0] * p[0] + row[1] * p[1] + op.translation[i]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** True if the two points agree modulo the lattice within `tol` in each coordinate. */
|
|
77
|
+
function sameModLattice(p: readonly number[], q: readonly number[], tol: number): boolean {
|
|
78
|
+
return p.every((v, i) => {
|
|
79
|
+
const d = v - q[i];
|
|
80
|
+
return Math.abs(d - Math.round(d)) < tol;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const plainCache = new Map<string, PlainOperation[]>();
|
|
85
|
+
function plainOperations(key: string): PlainOperation[] {
|
|
86
|
+
const k = planeGroupEntry(key).symbol;
|
|
87
|
+
let ops = plainCache.get(k);
|
|
88
|
+
if (!ops) {
|
|
89
|
+
ops = planeGroupOperations(k).map(toPlain);
|
|
90
|
+
plainCache.set(k, ops);
|
|
91
|
+
}
|
|
92
|
+
return ops;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The orbit of a point under a plane group: its distinct images in the conventional cell, each in
|
|
97
|
+
* [0, 1), in the order of the operations that first produce them. Coinciding images (within the
|
|
98
|
+
* tolerance) are merged.
|
|
99
|
+
*/
|
|
100
|
+
export function planeOrbit(key: string, point: readonly number[], options?: PointOptions): number[][] {
|
|
101
|
+
checkPoint(point);
|
|
102
|
+
const tol = tolerance(options);
|
|
103
|
+
const out: number[][] = [];
|
|
104
|
+
for (const op of plainOperations(key)) {
|
|
105
|
+
const q = image(op, point).map(mod1);
|
|
106
|
+
if (!out.some((r) => sameModLattice(r, q, tol))) out.push(q);
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Indices of `set` closed under products, modulo the lattice, within the operations of the group. */
|
|
112
|
+
function closeWithin(key: string, set: readonly number[]): number[] {
|
|
113
|
+
const exact = planeGroupOperations(planeGroupEntry(key).symbol);
|
|
114
|
+
const out = new Set(set);
|
|
115
|
+
let grew = true;
|
|
116
|
+
while (grew) {
|
|
117
|
+
grew = false;
|
|
118
|
+
for (const a of [...out]) {
|
|
119
|
+
for (const b of [...out]) {
|
|
120
|
+
const product = compose(exact[a], exact[b]);
|
|
121
|
+
const k = exact.findIndex((op) => equalModLattice(op, product));
|
|
122
|
+
if (!out.has(k)) {
|
|
123
|
+
out.add(k);
|
|
124
|
+
grew = true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return [...out].sort((a, b) => a - b);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The operations of `indices`, each shifted by the lattice translation that brings its image of `p` nearest to `p`. */
|
|
133
|
+
function fixingOperations(ops: readonly PlainOperation[], indices: readonly number[], p: readonly number[]) {
|
|
134
|
+
return indices.map((i) => {
|
|
135
|
+
const op = ops[i];
|
|
136
|
+
const n = image(op, p).map((v, k) => Math.round(v - p[k]));
|
|
137
|
+
return { matrix: op.matrix.map((r) => [...r]), translation: op.translation.map((t, k) => t - n[k]) };
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The site symmetry of a point under a plane group. The operations that fix the point modulo the
|
|
143
|
+
* lattice within the tolerance are closed under products and the point is set onto the fixed space
|
|
144
|
+
* of the closure (the mean of its images), repeated until stable, as `siteSymmetry` does for the point
|
|
145
|
+
* groups; so the result is always a subgroup and the set point lies exactly on its elements. Throws a
|
|
146
|
+
* RangeError if that does not settle (a tolerance far too large).
|
|
147
|
+
*/
|
|
148
|
+
export function planeSiteSymmetry(key: string, point: readonly number[], options?: PointOptions): PlaneSiteSymmetry {
|
|
149
|
+
checkPoint(point);
|
|
150
|
+
const tol = tolerance(options);
|
|
151
|
+
const ops = plainOperations(key);
|
|
152
|
+
const fixingAt = (p: readonly number[]): number[] =>
|
|
153
|
+
ops.flatMap((op, i) => (sameModLattice(image(op, p), p, tol) ? [i] : []));
|
|
154
|
+
let p: readonly number[] = point;
|
|
155
|
+
let set = closeWithin(key, fixingAt(p));
|
|
156
|
+
for (let pass = 0; pass < 8; pass++) {
|
|
157
|
+
const fixing = fixingOperations(ops, set, p);
|
|
158
|
+
const mean = [0, 1].map((k) => fixing.reduce((s, op) => s + image(op, p)[k], 0) / fixing.length);
|
|
159
|
+
const next = closeWithin(key, fixingAt(mean));
|
|
160
|
+
const settled = fixing.every((op) => sameModLattice(image(op, mean), mean, 1e-9));
|
|
161
|
+
if (settled && next.length === set.length && next.every((x, i) => x === set[i])) {
|
|
162
|
+
const q = mean.map(mod1);
|
|
163
|
+
return {
|
|
164
|
+
operations: set,
|
|
165
|
+
fixing: fixingOperations(ops, set, q),
|
|
166
|
+
point: q,
|
|
167
|
+
multiplicity: ops.length / set.length,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
p = mean;
|
|
171
|
+
set = next;
|
|
172
|
+
}
|
|
173
|
+
throw new RangeError(
|
|
174
|
+
`No common fixed point for the operations fixing ${JSON.stringify(point)} within ${tol} (tolerance too large?)`,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* True if some parameters x, y make the printed coordinate expression (e.g. `x,2x`, `-x+1/2,y`) give
|
|
180
|
+
* the point modulo the lattice within `tol`: least squares over the nearby lattice translations.
|
|
181
|
+
*/
|
|
182
|
+
function onExpression(expression: string, p: readonly number[], tol: number): boolean {
|
|
183
|
+
const { matrix, translation } = toPlain(parseOperation(expression));
|
|
184
|
+
const c = translation;
|
|
185
|
+
const ex = [matrix[0][0], matrix[1][0]];
|
|
186
|
+
const ey = [matrix[0][1], matrix[1][1]];
|
|
187
|
+
for (let i = -2; i <= 2; i++) {
|
|
188
|
+
for (let j = -2; j <= 2; j++) {
|
|
189
|
+
const r = [p[0] - c[0] - i, p[1] - c[1] - j];
|
|
190
|
+
// Solve min |a·ex + b·ey − r| over the columns that are not zero.
|
|
191
|
+
const cols = [ex, ey].filter((v) => v[0] !== 0 || v[1] !== 0);
|
|
192
|
+
let fit = [0, 0];
|
|
193
|
+
if (cols.length === 1) {
|
|
194
|
+
const v = cols[0];
|
|
195
|
+
const t = (v[0] * r[0] + v[1] * r[1]) / (v[0] * v[0] + v[1] * v[1]);
|
|
196
|
+
fit = [t * v[0], t * v[1]];
|
|
197
|
+
} else if (cols.length === 2) {
|
|
198
|
+
const det = ex[0] * ey[1] - ex[1] * ey[0];
|
|
199
|
+
if (Math.abs(det) < 1e-12) {
|
|
200
|
+
const v = ex;
|
|
201
|
+
const t = (v[0] * r[0] + v[1] * r[1]) / (v[0] * v[0] + v[1] * v[1]);
|
|
202
|
+
fit = [t * v[0], t * v[1]];
|
|
203
|
+
} else {
|
|
204
|
+
fit = r;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (Math.abs(fit[0] - r[0]) < tol && Math.abs(fit[1] - r[1]) < tol) return true;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The Wyckoff position of a point under a plane group, as ITA prints it: the point is first set
|
|
215
|
+
* onto the fixed space of its site symmetry (`planeSiteSymmetry`), then found among the printed
|
|
216
|
+
* coordinates of the positions with its multiplicity, the centring included.
|
|
217
|
+
*/
|
|
218
|
+
export function planeWyckoffPositionOf(
|
|
219
|
+
key: string,
|
|
220
|
+
point: readonly number[],
|
|
221
|
+
options?: PointOptions,
|
|
222
|
+
): PlaneWyckoffPosition {
|
|
223
|
+
const tol = tolerance(options);
|
|
224
|
+
const site = planeSiteSymmetry(key, point, options);
|
|
225
|
+
const entry = planeGroupEntry(key);
|
|
226
|
+
const centrings = [[0, 0], ...entry.centring.map((t) => toPlain(parseOperation(t)).translation)];
|
|
227
|
+
for (const [multiplicity, letter, siteSymmetry, coordinates] of entry.wyckoff) {
|
|
228
|
+
if (multiplicity !== site.multiplicity) continue;
|
|
229
|
+
for (const t of centrings) {
|
|
230
|
+
const shifted = [site.point[0] - t[0], site.point[1] - t[1]];
|
|
231
|
+
if (coordinates.some((expression) => onExpression(expression, shifted, tol))) {
|
|
232
|
+
return { letter, multiplicity, siteSymmetry };
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
throw new RangeError(`No Wyckoff position of ${entry.symbol} holds the point ${JSON.stringify(point)}`);
|
|
237
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exact rational numbers for the translations of space and plane groups (CLAUDE.md, "Plane groups
|
|
3
|
+
* (and later space groups)"). A rational is a pair [numerator, denominator] of safe integers with
|
|
4
|
+
* a positive denominator and no common factor, so that two equal numbers have equal pairs. Every
|
|
5
|
+
* intermediate product is checked: a result outside the safe integers throws instead of rounding.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** A rational number n/d: d > 0, gcd(n, d) = 1, both safe integers. */
|
|
9
|
+
export type Rational = readonly [numerator: number, denominator: number];
|
|
10
|
+
|
|
11
|
+
/** The rational 0. */
|
|
12
|
+
export const ZERO: Rational = [0, 1];
|
|
13
|
+
|
|
14
|
+
/** The rational 1. */
|
|
15
|
+
export const ONE: Rational = [1, 1];
|
|
16
|
+
|
|
17
|
+
function gcd(a: number, b: number): number {
|
|
18
|
+
let x = Math.abs(a);
|
|
19
|
+
let y = Math.abs(b);
|
|
20
|
+
while (y !== 0) {
|
|
21
|
+
[x, y] = [y, x % y];
|
|
22
|
+
}
|
|
23
|
+
return x;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Throws unless `value` is a safe integer. */
|
|
27
|
+
export function checkSafeInteger(value: number, what: string): number {
|
|
28
|
+
if (!Number.isSafeInteger(value)) {
|
|
29
|
+
throw new RangeError(`${what} must be a safe integer, not ${String(value)}`);
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The product a·b of two safe integers; throws if it leaves the safe integers. */
|
|
35
|
+
export function safeProduct(a: number, b: number): number {
|
|
36
|
+
return checkSafeInteger(a * b, 'An intermediate product');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The sum a + b of two safe integers; throws if it leaves the safe integers. */
|
|
40
|
+
export function safeSum(a: number, b: number): number {
|
|
41
|
+
return checkSafeInteger(a + b, 'An intermediate sum');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The rational n/d in lowest terms; throws if d is 0 or a part is not a safe integer. */
|
|
45
|
+
export function rational(numerator: number, denominator = 1): Rational {
|
|
46
|
+
checkSafeInteger(numerator, 'Numerator');
|
|
47
|
+
checkSafeInteger(denominator, 'Denominator');
|
|
48
|
+
if (denominator === 0) {
|
|
49
|
+
throw new RangeError('Denominator must not be 0');
|
|
50
|
+
}
|
|
51
|
+
const g = gcd(numerator, denominator);
|
|
52
|
+
const sign = denominator < 0 ? -1 : 1;
|
|
53
|
+
// `+ 0` turns −0 into 0, so that equal numbers have equal pairs.
|
|
54
|
+
return [(sign * numerator) / g + 0, (sign * denominator) / g];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The sum a + b. */
|
|
58
|
+
export function add(a: Rational, b: Rational): Rational {
|
|
59
|
+
return rational(safeSum(safeProduct(a[0], b[1]), safeProduct(b[0], a[1])), safeProduct(a[1], b[1]));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The difference a − b. */
|
|
63
|
+
export function subtract(a: Rational, b: Rational): Rational {
|
|
64
|
+
return add(a, negate(b));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The product a·b. */
|
|
68
|
+
export function multiply(a: Rational, b: Rational): Rational {
|
|
69
|
+
return rational(safeProduct(a[0], b[0]), safeProduct(a[1], b[1]));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The product of an integer k and a rational a. */
|
|
73
|
+
export function scale(k: number, a: Rational): Rational {
|
|
74
|
+
return rational(safeProduct(k, a[0]), a[1]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The negative −a. */
|
|
78
|
+
export function negate(a: Rational): Rational {
|
|
79
|
+
return rational(-a[0], a[1]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** True if a and b are the same number. */
|
|
83
|
+
export function rationalsEqual(a: Rational, b: Rational): boolean {
|
|
84
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** True if a is an integer. */
|
|
88
|
+
export function isInteger(a: Rational): boolean {
|
|
89
|
+
return a[1] === 1;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The representative of a modulo 1, in [0, 1). */
|
|
93
|
+
export function mod1(a: Rational): Rational {
|
|
94
|
+
const r = ((a[0] % a[1]) + a[1]) % a[1];
|
|
95
|
+
return rational(r, a[1]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The nearest double. */
|
|
99
|
+
export function toNumber(a: Rational): number {
|
|
100
|
+
return a[0] / a[1];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Reads a rational as ITA prints it: an integer or a fraction such as `1/2` or `-2/3`. */
|
|
104
|
+
export function parseRational(text: string): Rational {
|
|
105
|
+
const m = /^([+-]?\d+)(?:\/(\d+))?$/.exec(text.trim());
|
|
106
|
+
if (!m) {
|
|
107
|
+
throw new RangeError(`Not a rational number: "${text}"`);
|
|
108
|
+
}
|
|
109
|
+
return rational(Number(m[1]), m[2] === undefined ? 1 : Number(m[2]));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Writes a rational as ITA prints it: `0`, `1`, `-1/2`, `2/3`. */
|
|
113
|
+
export function formatRational(a: Rational): string {
|
|
114
|
+
return a[1] === 1 ? String(a[0]) : `${a[0]}/${a[1]}`;
|
|
115
|
+
}
|
|
@@ -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