phasesmith-workflows 0.1.0

Application-neutral native workflows for PhaseSmith
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! Stable Rietveld parameter identities and structural derivative transforms.

use std::error::Error;
use std::fmt::{Display, Formatter};

use phasesmith_crystallography::P1ParameterLayout;
use phasesmith_model::RecordId;

use crate::{
    LatticeBounds, LatticeError, LatticeParameterization, ParameterBounds, ParameterError,
    ParameterKey, ParameterSet, ParameterSpec, RietveldPhase,
};

/// Structural parameter families selected for one native Rietveld layout.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(clippy::struct_excessive_bools)]
pub struct RietveldStructuralSelection {
    /// Refine symmetry-independent lattice variables.
    pub lattice: bool,
    /// Refine symmetry-allowed asymmetric-site coordinates.
    pub coordinates: bool,
    /// Refine asymmetric-site occupancies.
    pub occupancy: bool,
    /// Refine isotropic displacement values for isotropic sites.
    pub u_iso: bool,
    /// Refine one structural intensity scale per phase.
    pub phase_scale: bool,
}

/// Symmetry-allowed coordinate tangent basis for one asymmetric site.
#[derive(Clone, Debug, PartialEq)]
pub struct SiteCoordinateModel {
    parameter_names: Vec<String>,
    basis: Vec<f64>,
    special_position: bool,
}

impl SiteCoordinateModel {
    /// Derive the null space of the exact site-stabilizer rotations.
    ///
    /// # Errors
    ///
    /// Returns [`RietveldParameterError::InvalidCoordinateModel`] for a
    /// non-finite coordinate or non-positive/non-finite tolerance.
    #[allow(clippy::too_many_lines)]
    pub fn new(
        space_group: &phasesmith_crystallography::SpaceGroup,
        coordinate: [f64; 3],
        tolerance: f64,
    ) -> Result<Self, RietveldParameterError> {
        if coordinate.iter().any(|value| !value.is_finite())
            || !tolerance.is_finite()
            || tolerance <= 0.0
        {
            return Err(RietveldParameterError::InvalidCoordinateModel);
        }
        let mut equations = Vec::<[f64; 3]>::new();
        for operation in space_group.operations() {
            let rotation = operation.rotation();
            let translation = operation.translation();
            let mut difference = [0.0; 3];
            for row in 0..3 {
                difference[row] = translation[row].as_f64() - coordinate[row];
                for column in 0..3 {
                    difference[row] += f64::from(rotation[row][column]) * coordinate[column];
                }
            }
            if difference
                .iter()
                .all(|value| (value - value.round()).abs() <= tolerance)
            {
                for row in 0..3 {
                    let mut equation = rotation[row].map(f64::from);
                    equation[row] -= 1.0;
                    equations.push(equation);
                }
            }
        }
        let (column_count, mut basis) = deterministic_null_space(equations);
        let special_position = column_count != 3;
        if !special_position {
            basis = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
        }
        for value in &mut basis {
            if value.abs() < 1.0e-14 {
                *value = 0.0;
            }
        }
        let parameter_names = if special_position {
            (0..column_count).map(|index| format!("q{index}")).collect()
        } else {
            ["x", "y", "z"].map(str::to_owned).to_vec()
        };
        Ok(Self {
            parameter_names,
            basis,
            special_position,
        })
    }

    /// Borrow stable coordinate parameter names.
    #[must_use]
    pub fn parameter_names(&self) -> &[String] {
        &self.parameter_names
    }

    /// Borrow the row-major `3 x parameter_count` tangent basis.
    #[must_use]
    pub fn basis(&self) -> &[f64] {
        &self.basis
    }

    /// Return whether symmetry removes at least one coordinate direction.
    #[must_use]
    pub const fn is_special_position(&self) -> bool {
        self.special_position
    }
}

#[derive(Clone, Debug, PartialEq)]
struct ParameterMapping {
    global_index: usize,
    native_terms: Vec<(usize, f64)>,
}

#[derive(Clone, Debug, PartialEq)]
struct PhaseDerivativeLayout {
    native_count: usize,
    mappings: Vec<ParameterMapping>,
}

/// Ordered physical parameters and exact transforms to/from engine layouts.
#[derive(Clone, Debug, PartialEq)]
pub struct RietveldStructuralLayout {
    parameters: ParameterSet,
    phases: Vec<PhaseDerivativeLayout>,
    coordinate_models: Vec<Vec<SiteCoordinateModel>>,
    phase_ids: Vec<RecordId>,
    site_ids: Vec<Vec<RecordId>>,
    space_groups: Vec<phasesmith_crystallography::SpaceGroup>,
    anisotropic_masks: Vec<Vec<bool>>,
}

impl RietveldStructuralLayout {
    /// Build stable keys, bounds, scaling, and analytical engine transforms.
    ///
    /// `lattice_bounds` must have one entry per phase. A selected lattice
    /// requires bounds; an unselected lattice ignores a missing entry.
    ///
    /// # Errors
    ///
    /// Returns [`RietveldParameterError`] for shape, lattice, identity, or
    /// parameter-domain failures.
    #[allow(clippy::too_many_lines)]
    pub fn new(
        phases: &[RietveldPhase],
        selection: RietveldStructuralSelection,
        lattice_bounds: &[Option<LatticeBounds>],
    ) -> Result<Self, RietveldParameterError> {
        if phases.len() != lattice_bounds.len() {
            return Err(RietveldParameterError::PhaseCountMismatch);
        }
        let mut specs = Vec::new();
        let mut layouts = Vec::with_capacity(phases.len());
        let mut all_coordinate_models = Vec::with_capacity(phases.len());
        for (phase, supplied_bounds) in phases.iter().zip(lattice_bounds) {
            let definition = phase.definition();
            let native = P1ParameterLayout {
                site_count: definition.fractional_xyz.len(),
            };
            let mut mappings = Vec::new();
            if selection.lattice {
                let bounds = supplied_bounds
                    .as_ref()
                    .ok_or(RietveldParameterError::MissingLatticeBounds)?;
                let parameterization =
                    LatticeParameterization::new(definition.space_group.clone(), definition.cell)?;
                if bounds.parameter_names() != parameterization.parameter_names() {
                    return Err(RietveldParameterError::Lattice(LatticeError::InvalidBounds));
                }
                let values = parameterization.values_from_cell(definition.cell)?;
                let jacobian = parameterization.cell_jacobian(&values)?;
                let columns = values.len();
                for column in 0..columns {
                    let name = &parameterization.parameter_names()[column];
                    let unit = if name.ends_with("_angstrom") {
                        "angstrom"
                    } else {
                        "degree"
                    };
                    push_mapping(
                        &mut specs,
                        &mut mappings,
                        ParameterKey::new("lattice", phase.phase_id().as_str(), name)?,
                        values[column],
                        unit,
                        ParameterBounds::new(bounds.lower()[column], bounds.upper()[column])?,
                        values[column].abs().max(1.0),
                        (0..6)
                            .filter_map(|row| {
                                let coefficient = jacobian[row * columns + column];
                                (coefficient != 0.0).then_some((row, coefficient))
                            })
                            .collect(),
                    )?;
                }
            }
            let coordinate_models = definition
                .fractional_xyz
                .iter()
                .map(|coordinate| {
                    SiteCoordinateModel::new(
                        &definition.space_group,
                        *coordinate,
                        definition.coordinate_tolerance,
                    )
                })
                .collect::<Result<Vec<_>, _>>()?;
            for (site, (site_id, model)) in
                phase.site_ids().iter().zip(&coordinate_models).enumerate()
            {
                let owner = format!("{}/{}", phase.phase_id(), site_id);
                if selection.coordinates {
                    let columns = model.parameter_names.len();
                    for column in 0..columns {
                        let value = if model.special_position {
                            0.0
                        } else {
                            definition.fractional_xyz[site][column]
                        };
                        push_mapping(
                            &mut specs,
                            &mut mappings,
                            ParameterKey::new("site", &owner, &model.parameter_names[column])?,
                            value,
                            "fractional",
                            if model.special_position {
                                ParameterBounds::new(-0.5, 0.5)?
                            } else {
                                ParameterBounds::default()
                            },
                            1.0,
                            (0..3)
                                .filter_map(|row| {
                                    let coefficient = model.basis[row * columns + column];
                                    (coefficient != 0.0)
                                        .then_some((native.coordinate(site, row), coefficient))
                                })
                                .collect(),
                        )?;
                    }
                }
                if selection.occupancy {
                    let value = definition.occupancy[site];
                    push_mapping(
                        &mut specs,
                        &mut mappings,
                        ParameterKey::new("site", &owner, "occupancy")?,
                        value,
                        "fraction",
                        ParameterBounds::new(0.0, (2.0 * value + 0.1).max(1.0))?,
                        value.max(1.0),
                        vec![(native.occupancy(site), 1.0)],
                    )?;
                }
                if selection.u_iso && !definition.anisotropic_mask[site] {
                    let value = definition.u_iso_angstrom2[site];
                    push_mapping(
                        &mut specs,
                        &mut mappings,
                        ParameterKey::new("site", &owner, "u_iso_angstrom2")?,
                        value,
                        "angstrom^2",
                        ParameterBounds::new(0.0, (2.0 * value + 0.05).max(0.5))?,
                        value.max(0.01),
                        vec![(native.u_iso(site), 1.0)],
                    )?;
                }
            }
            if selection.phase_scale {
                let numerical_scale = if definition.scale == 0.0 {
                    1.0
                } else {
                    definition.scale.abs()
                };
                push_mapping(
                    &mut specs,
                    &mut mappings,
                    ParameterKey::new("phase", phase.phase_id().as_str(), "scale")?,
                    definition.scale,
                    "relative",
                    ParameterBounds::new(0.0, f64::INFINITY)?,
                    numerical_scale,
                    vec![(native.scale(), 1.0)],
                )?;
            }
            layouts.push(PhaseDerivativeLayout {
                native_count: native.parameter_count(),
                mappings,
            });
            all_coordinate_models.push(coordinate_models);
        }
        Ok(Self {
            parameters: ParameterSet::new(specs)?,
            phases: layouts,
            coordinate_models: all_coordinate_models,
            phase_ids: phases
                .iter()
                .map(|phase| phase.phase_id().clone())
                .collect(),
            site_ids: phases
                .iter()
                .map(|phase| phase.site_ids().to_vec())
                .collect(),
            space_groups: phases
                .iter()
                .map(|phase| phase.definition().space_group.clone())
                .collect(),
            anisotropic_masks: phases
                .iter()
                .map(|phase| phase.definition().anisotropic_mask.clone())
                .collect(),
        })
    }

    /// Borrow ordered physical parameter specifications.
    #[must_use]
    pub const fn parameters(&self) -> &ParameterSet {
        &self.parameters
    }

    /// Borrow symmetry-allowed site models in phase/site order.
    #[must_use]
    pub fn coordinate_models(&self) -> &[Vec<SiteCoordinateModel>] {
        &self.coordinate_models
    }

    pub(crate) fn validate_phases(
        &self,
        phases: &[RietveldPhase],
    ) -> Result<(), RietveldParameterError> {
        if phases.len() != self.phase_ids.len()
            || phases.iter().enumerate().any(|(index, phase)| {
                phase.phase_id() != &self.phase_ids[index]
                    || phase.site_ids() != self.site_ids[index]
                    || phase.definition().space_group != self.space_groups[index]
                    || phase.definition().anisotropic_mask != self.anisotropic_masks[index]
                    || 6 + 5 * phase.definition().fractional_xyz.len() + 1
                        != self.phases[index].native_count
            })
        {
            return Err(RietveldParameterError::PhaseIdentityMismatch);
        }
        Ok(())
    }

    /// Install physical parameter values into cloned phase definitions.
    ///
    /// # Errors
    ///
    /// Returns [`RietveldParameterError`] for stale phase identities, a wrong
    /// value count, invalid lattice/site values, or a rejected phase domain.
    pub fn apply_values(
        &self,
        phases: &[RietveldPhase],
        values: &[f64],
    ) -> Result<Vec<RietveldPhase>, RietveldParameterError> {
        let current = self
            .parameters
            .specs()
            .iter()
            .map(ParameterSpec::value)
            .collect::<Vec<_>>();
        self.apply_value_change(phases, &current, values)
    }

    /// Install a change between two absolute physical parameter states.
    ///
    /// Special-position coordinates are local tangent coordinates, so only
    /// their difference is applied to the current phase. All other selected
    /// values are installed absolutely.
    ///
    /// # Errors
    ///
    /// Returns [`RietveldParameterError`] for stale phase identities, wrong
    /// value counts, invalid values, or a rejected phase domain.
    pub fn apply_value_change(
        &self,
        phases: &[RietveldPhase],
        current_values: &[f64],
        values: &[f64],
    ) -> Result<Vec<RietveldPhase>, RietveldParameterError> {
        self.validate_phases(phases)?;
        if current_values.len() != self.parameters.specs().len()
            || values.len() != self.parameters.specs().len()
            || current_values.iter().any(|value| !value.is_finite())
            || values.iter().any(|value| !value.is_finite())
        {
            return Err(RietveldParameterError::ValueLengthMismatch);
        }
        if let Some((spec, value)) = self
            .parameters
            .specs()
            .iter()
            .zip(values)
            .find(|(spec, value)| !spec.bounds().contains(**value))
        {
            return Err(RietveldParameterError::Parameter(
                ParameterError::ValueOutsideBounds {
                    key: spec.key().clone(),
                    value: *value,
                },
            ));
        }
        let mut updated = Vec::with_capacity(phases.len());
        for (phase_index, phase) in phases.iter().enumerate() {
            let mut definition = phase.definition().clone();
            let phase_id = phase.phase_id().as_str();
            let parameterization =
                LatticeParameterization::new(definition.space_group.clone(), definition.cell)?;
            let mut lattice_values = parameterization.values_from_cell(definition.cell)?;
            let mut lattice_changed = false;
            for (index, name) in parameterization.parameter_names().iter().enumerate() {
                if let Some(value) = self.value_for("lattice", phase_id, name, values)? {
                    lattice_values[index] = value;
                    lattice_changed = true;
                }
            }
            if lattice_changed {
                definition.cell = parameterization.to_cell(&lattice_values)?;
            }
            for (site, site_id) in phase.site_ids().iter().enumerate() {
                let owner = format!("{phase_id}/{site_id}");
                let model = &self.coordinate_models[phase_index][site];
                if model.special_position {
                    let columns = model.parameter_names.len();
                    for column in 0..columns {
                        if let Some((before, after)) = self.value_change_for(
                            "site",
                            &owner,
                            &model.parameter_names[column],
                            current_values,
                            values,
                        )? {
                            for row in 0..3 {
                                definition.fractional_xyz[site][row] +=
                                    model.basis[row * columns + column] * (after - before);
                            }
                        }
                    }
                } else {
                    for (component, name) in ["x", "y", "z"].iter().enumerate() {
                        if let Some(value) = self.value_for("site", &owner, name, values)? {
                            definition.fractional_xyz[site][component] = value;
                        }
                    }
                }
                if let Some(value) = self.value_for("site", &owner, "occupancy", values)? {
                    definition.occupancy[site] = value;
                }
                if let Some(value) = self.value_for("site", &owner, "u_iso_angstrom2", values)? {
                    definition.u_iso_angstrom2[site] = value;
                }
            }
            if let Some(value) = self.value_for("phase", phase_id, "scale", values)? {
                definition.scale = value;
            }
            updated.push(
                phase
                    .with_definition(definition)
                    .map_err(RietveldParameterError::Rietveld)?,
            );
        }
        Ok(updated)
    }

    fn value_change_for(
        &self,
        module: &str,
        owner: &str,
        name: &str,
        current_values: &[f64],
        values: &[f64],
    ) -> Result<Option<(f64, f64)>, RietveldParameterError> {
        let key = ParameterKey::new(module, owner, name)?;
        Ok(self
            .parameters
            .index_of(&key)
            .map(|index| (current_values[index], values[index])))
    }

    fn value_for(
        &self,
        module: &str,
        owner: &str,
        name: &str,
        values: &[f64],
    ) -> Result<Option<f64>, RietveldParameterError> {
        let key = ParameterKey::new(module, owner, name)?;
        Ok(self.parameters.index_of(&key).map(|index| values[index]))
    }

    /// Expand one physical parameter direction into native per-phase tangents.
    ///
    /// # Errors
    ///
    /// Returns [`RietveldParameterError::DirectionLengthMismatch`] for a wrong
    /// global direction length.
    pub fn native_tangents(
        &self,
        direction: &[f64],
    ) -> Result<Vec<Vec<f64>>, RietveldParameterError> {
        if direction.len() != self.parameters.specs().len() {
            return Err(RietveldParameterError::DirectionLengthMismatch);
        }
        Ok(self
            .phases
            .iter()
            .map(|phase| {
                let mut tangent = vec![0.0; phase.native_count];
                for mapping in &phase.mappings {
                    for &(native_index, coefficient) in &mapping.native_terms {
                        tangent[native_index] += coefficient * direction[mapping.global_index];
                    }
                }
                tangent
            })
            .collect())
    }

    /// Project native per-phase reverse products into physical parameter order.
    ///
    /// # Errors
    ///
    /// Returns [`RietveldParameterError`] for phase or native-gradient shape
    /// mismatches.
    pub fn project_native_gradients(
        &self,
        gradients: &[&[f64]],
    ) -> Result<Vec<f64>, RietveldParameterError> {
        if gradients.len() != self.phases.len() {
            return Err(RietveldParameterError::PhaseCountMismatch);
        }
        let mut projected = vec![0.0; self.parameters.specs().len()];
        for (phase, gradient) in self.phases.iter().zip(gradients) {
            if gradient.len() != phase.native_count {
                return Err(RietveldParameterError::NativeGradientLengthMismatch);
            }
            for mapping in &phase.mappings {
                projected[mapping.global_index] += mapping
                    .native_terms
                    .iter()
                    .map(|(native_index, coefficient)| coefficient * gradient[*native_index])
                    .sum::<f64>();
            }
        }
        Ok(projected)
    }

    /// Project parameter-major native phase Jacobians into physical parameter order.
    ///
    /// # Errors
    ///
    /// Returns [`RietveldParameterError`] for phase, native-row, sample, or
    /// allocation shape mismatches.
    pub fn project_native_jacobians(
        &self,
        jacobians: &[(&[f64], usize)],
        sample_count: usize,
    ) -> Result<Vec<f64>, RietveldParameterError> {
        if jacobians.len() != self.phases.len() {
            return Err(RietveldParameterError::PhaseCountMismatch);
        }
        let element_count = self
            .parameters
            .specs()
            .len()
            .checked_mul(sample_count)
            .ok_or(RietveldParameterError::AllocationOverflow)?;
        let mut projected = vec![0.0; element_count];
        for (phase, (jacobian, native_count)) in self.phases.iter().zip(jacobians) {
            let native_elements = native_count
                .checked_mul(sample_count)
                .ok_or(RietveldParameterError::AllocationOverflow)?;
            if *native_count != phase.native_count || jacobian.len() != native_elements {
                return Err(RietveldParameterError::NativeGradientLengthMismatch);
            }
            for mapping in &phase.mappings {
                let target_start = mapping
                    .global_index
                    .checked_mul(sample_count)
                    .ok_or(RietveldParameterError::AllocationOverflow)?;
                for &(native_index, coefficient) in &mapping.native_terms {
                    let source_start = native_index
                        .checked_mul(sample_count)
                        .ok_or(RietveldParameterError::AllocationOverflow)?;
                    for sample in 0..sample_count {
                        projected[target_start + sample] +=
                            coefficient * jacobian[source_start + sample];
                    }
                }
            }
        }
        Ok(projected)
    }
}

#[allow(clippy::too_many_arguments)]
fn push_mapping(
    specs: &mut Vec<ParameterSpec>,
    mappings: &mut Vec<ParameterMapping>,
    key: ParameterKey,
    value: f64,
    unit: &str,
    bounds: ParameterBounds,
    scale: f64,
    native_terms: Vec<(usize, f64)>,
) -> Result<(), ParameterError> {
    let global_index = specs.len();
    specs.push(ParameterSpec::new(key, value, unit, bounds, scale, true)?);
    mappings.push(ParameterMapping {
        global_index,
        native_terms,
    });
    Ok(())
}

fn deterministic_null_space(mut equations: Vec<[f64; 3]>) -> (usize, Vec<f64>) {
    let mut pivot_columns = Vec::new();
    let mut pivot_row = 0;
    for column in 0..3 {
        let Some(row) =
            (pivot_row..equations.len()).find(|row| equations[*row][column].abs() > 1.0e-12)
        else {
            continue;
        };
        equations.swap(pivot_row, row);
        let pivot = equations[pivot_row][column];
        for value in &mut equations[pivot_row] {
            *value /= pivot;
        }
        let pivot_values = equations[pivot_row];
        for (row, equation) in equations.iter_mut().enumerate() {
            if row != pivot_row {
                let factor = equation[column];
                for index in 0..3 {
                    equation[index] -= factor * pivot_values[index];
                }
            }
        }
        pivot_columns.push(column);
        pivot_row += 1;
    }
    let free_columns = (0..3)
        .filter(|column| !pivot_columns.contains(column))
        .collect::<Vec<_>>();
    let column_count = free_columns.len();
    let mut basis = vec![0.0; 3 * column_count];
    for (basis_column, free_column) in free_columns.iter().copied().enumerate() {
        let mut vector = [0.0; 3];
        vector[free_column] = 1.0;
        for (row, pivot_column) in pivot_columns.iter().copied().enumerate() {
            vector[pivot_column] = -equations[row][free_column];
        }
        let norm = vector.iter().map(|value| value * value).sum::<f64>().sqrt();
        for row in 0..3 {
            basis[row * column_count + basis_column] = vector[row] / norm;
        }
    }
    (column_count, basis)
}

/// Invalid native Rietveld parameter-layout state.
#[derive(Debug)]
pub enum RietveldParameterError {
    /// Phase-aligned inputs have different lengths.
    PhaseCountMismatch,
    /// Lattice refinement was selected without finite bounds.
    MissingLatticeBounds,
    /// One physical direction has the wrong length.
    DirectionLengthMismatch,
    /// One native reverse product has the wrong length.
    NativeGradientLengthMismatch,
    /// A requested dense derivative allocation exceeds addressable memory.
    AllocationOverflow,
    /// A physical value vector has the wrong length or contains non-finite data.
    ValueLengthMismatch,
    /// Layout phase/site identities differ from the calculation request.
    PhaseIdentityMismatch,
    /// Site coordinate or stabilizer tolerance is invalid.
    InvalidCoordinateModel,
    /// Stable parameter construction failed.
    Parameter(ParameterError),
    /// Setting-aware lattice construction failed.
    Lattice(LatticeError),
    /// Reconstructed phase state is invalid.
    Rietveld(crate::RietveldError),
}

impl Display for RietveldParameterError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::PhaseCountMismatch => formatter.write_str("Rietveld phase counts must match"),
            Self::MissingLatticeBounds => {
                formatter.write_str("selected Rietveld lattices require finite bounds")
            }
            Self::DirectionLengthMismatch => {
                formatter.write_str("Rietveld parameter direction length mismatch")
            }
            Self::NativeGradientLengthMismatch => {
                formatter.write_str("Rietveld native gradient length mismatch")
            }
            Self::AllocationOverflow => {
                formatter.write_str("Rietveld derivative allocation overflow")
            }
            Self::ValueLengthMismatch => {
                formatter.write_str("Rietveld physical value length mismatch")
            }
            Self::PhaseIdentityMismatch => {
                formatter.write_str("Rietveld parameter layout phase identities differ")
            }
            Self::InvalidCoordinateModel => {
                formatter.write_str("Rietveld site coordinate model is invalid")
            }
            Self::Parameter(error) => Display::fmt(error, formatter),
            Self::Lattice(error) => Display::fmt(error, formatter),
            Self::Rietveld(error) => Display::fmt(error, formatter),
        }
    }
}

impl Error for RietveldParameterError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Parameter(error) => Some(error),
            Self::Lattice(error) => Some(error),
            Self::Rietveld(error) => Some(error),
            Self::PhaseCountMismatch
            | Self::MissingLatticeBounds
            | Self::DirectionLengthMismatch
            | Self::NativeGradientLengthMismatch
            | Self::AllocationOverflow
            | Self::ValueLengthMismatch
            | Self::PhaseIdentityMismatch
            | Self::InvalidCoordinateModel => None,
        }
    }
}

impl From<ParameterError> for RietveldParameterError {
    fn from(value: ParameterError) -> Self {
        Self::Parameter(value)
    }
}

impl From<LatticeError> for RietveldParameterError {
    fn from(value: LatticeError) -> Self {
        Self::Lattice(value)
    }
}