in Chemistry/src/DataModel/LadderOperator/IndexOrderedSequence.cs [63:106]
public void NormalizeToIndexOrder()
{
var tmp = new NormalOrderedSequence<TIndex>(this);
if (!tmp.IsInIndexOrder())
{
var left = tmp.Sequence.Select((op, idx) => new { op, idx }).Where(x => x.op.Type == RaisingLowering.u);
var right = tmp.Sequence.Select((op, idx) => new { op, idx }).Where(x => x.op.Type == RaisingLowering.d);
var upArrayIndices = tmp.Sequence.Select((op, idx) => new { op, idx }).Where(x => x.op.Type == RaisingLowering.u).Select(x => x.idx).ToArray();
var downArrayIndices = tmp.Sequence.Select((op, idx) => new { op, idx }).Where(x => x.op.Type == RaisingLowering.d).Select(x => x.idx).ToArray();
// Bubble sort spin-orbital indices of creation operator.
while (!tmp.IsInIndexCreationCanonicalOrder())
{
for (int idx = 0; idx < upArrayIndices.Count() - 1; idx++)
{
if (tmp.Sequence.ElementAt(upArrayIndices.ElementAt(idx)).Index.CompareTo(tmp.Sequence.ElementAt(upArrayIndices.ElementAt(idx + 1)).Index) > 0)
{
var tmpLadderOperator = tmp.Sequence.ElementAt(upArrayIndices.ElementAt(idx));
tmp.Sequence[upArrayIndices.ElementAt(idx)] = tmp.Sequence[upArrayIndices.ElementAt(idx + 1)];
tmp.Sequence[upArrayIndices.ElementAt(idx + 1)] = tmpLadderOperator;
tmp.Coefficient = -1 * tmp.Coefficient;
}
}
}
// Bubble sort spin-orbital indices of annihilation operator.
while (!tmp.IsInIndexAnnihilationCanonicalOrder())
{
for (int idx = 0; idx < downArrayIndices.Length - 1; idx++)
{
if (tmp.Sequence.ElementAt(downArrayIndices.ElementAt(idx)).Index.CompareTo(tmp.Sequence.ElementAt(downArrayIndices.ElementAt(idx + 1)).Index) < 0)
{
var tmpLadderOperator = tmp.Sequence.ElementAt(downArrayIndices.ElementAt(idx));
tmp.Sequence[downArrayIndices.ElementAt(idx)] = tmp.Sequence[downArrayIndices.ElementAt(idx + 1)];
tmp.Sequence[downArrayIndices.ElementAt(idx + 1)] = tmpLadderOperator;
tmp.Coefficient = -1 * tmp.Coefficient;
}
}
}
}
Sequence = tmp.Sequence;
Coefficient = tmp.Coefficient;
}