// Arithmetique exacte : un complexe est une paire de rationnels (re, im) sur Q(i).
// La source Python calcule sur Fractions et des paires de Fractions ; le jumeau C#
// rejoue exactement le meme corps de calcul avec des rationnels sur Z et des
// paires de rationnels. Un seul scope .NET partage les types ci-dessous a toutes
// les cellules du carnet (le code C# d'une cellule ne partage PAS ses variables
// locales avec la suivante : l'etat que les cellules ulterieures relevent vit
// dans la classe C13, reconstruit ici).
using System;
using System.Numerics;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
public sealed class Rat : IEquatable<Rat>
{
public BigInteger Num { get; }
public BigInteger Den { get; }
public Rat(BigInteger n, BigInteger d)
{
if (d.IsZero) throw new DivideByZeroException("denominateur nul");
if (d.Sign < 0) { n = -n; d = -d; }
BigInteger g = BigInteger.GreatestCommonDivisor(BigInteger.Abs(n), d);
Num = n / g;
Den = d / g;
}
public Rat(BigInteger n) : this(n, BigInteger.One) { }
public static Rat operator +(Rat a, Rat b) => new Rat(a.Num * b.Den + b.Num * a.Den, a.Den * b.Den);
public static Rat operator -(Rat a, Rat b) => new Rat(a.Num * b.Den - b.Num * a.Den, a.Den * b.Den);
public static Rat operator *(Rat a, Rat b) => new Rat(a.Num * b.Num, a.Den * b.Den);
public static Rat operator /(Rat a, Rat b) => new Rat(a.Num * b.Den, a.Den * b.Num);
public static Rat operator -(Rat a) => new Rat(-a.Num, a.Den);
public bool IsZero => Num.IsZero;
public bool IsOne => Num.IsOne && Den.IsOne;
public int Sign => Num.Sign * Den.Sign;
public static Rat Abs(Rat a) => new Rat(BigInteger.Abs(a.Num), a.Den);
public bool Equals(Rat o) => o.Num == Num && o.Den == Den;
public override bool Equals(object o) => o is Rat r && r.Equals(this);
public override int GetHashCode() => Num.GetHashCode() ^ Den.GetHashCode();
public static bool operator ==(Rat a, Rat b) => a is null ? b is null : a.Equals(b);
public static bool operator !=(Rat a, Rat b) => !(a == b);
public override string ToString()
{
return Den.IsOne
? Num.ToString(CultureInfo.InvariantCulture)
: Num.ToString(CultureInfo.InvariantCulture) + "/" + Den.ToString(CultureInfo.InvariantCulture);
}
}
public sealed class Cx : IEquatable<Cx>
{
public Rat Re { get; }
public Rat Im { get; }
public Cx(Rat re, Rat im) { Re = re; Im = im; }
public static Cx operator +(Cx a, Cx b) => new Cx(a.Re + b.Re, a.Im + b.Im);
public static Cx operator -(Cx a, Cx b) => new Cx(a.Re - b.Re, a.Im - b.Im);
public static Cx operator *(Cx a, Cx b) => new Cx(a.Re * b.Re - a.Im * b.Im, a.Re * b.Im + a.Im * b.Re);
public static Cx operator -(Cx a) => new Cx(-a.Re, -a.Im);
public bool IsZero => Re.IsZero && Im.IsZero;
public bool Equals(Cx o) => o.Re == Re && o.Im == Im;
public override bool Equals(object o) => o is Cx c && c.Equals(this);
public override int GetHashCode() => Re.GetHashCode() ^ Im.GetHashCode();
public static bool operator ==(Cx a, Cx b) => a is null ? b is null : a.Equals(b);
public static bool operator !=(Cx a, Cx b) => !(a == b);
}
public sealed class Mat : IEquatable<Mat>
{
public int Rows { get; }
public int Cols { get; }
private readonly Cx[,] _a;
public Mat(int r, int c)
{
Rows = r;
Cols = c;
_a = new Cx[r, c];
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
_a[i, j] = new Cx(new Rat(BigInteger.Zero), new Rat(BigInteger.Zero));
}
public Cx Get(int i, int j) => _a[i, j];
public void Set(int i, int j, Cx v) => _a[i, j] = v;
public bool Equals(Mat o)
{
if (o is null || o.Rows != Rows || o.Cols != Cols) return false;
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
if (_a[i, j] != o._a[i, j]) return false;
return true;
}
public override bool Equals(object o) => Equals(o as Mat);
public override int GetHashCode()
{
int h = Rows * 31 + Cols;
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
h = h * 31 + _a[i, j].GetHashCode();
return h;
}
public static bool operator ==(Mat a, Mat b) => a is null ? b is null : a.Equals(b);
public static bool operator !=(Mat a, Mat b) => !(a == b);
}
public static class Ch
{
public static readonly Rat Zero = new Rat(BigInteger.Zero);
public static readonly Rat One = new Rat(BigInteger.One);
public static readonly Rat NegOne = new Rat(BigInteger.MinusOne);
public static readonly Cx Z = new Cx(Zero, Zero);
public static readonly Cx U = new Cx(One, Zero);
public static readonly Cx CI = new Cx(Zero, One);
public static readonly Cx MINUSONE = new Cx(NegOne, Zero);
// Les trois directions derivees au bout qubit : les lettres de Pauli sur Q(i).
public static readonly List<Mat> DIRS3 = new List<Mat>();
static Ch()
{
var sx = new Mat(2, 2);
sx.Set(0, 0, Z); sx.Set(0, 1, U); sx.Set(1, 0, U); sx.Set(1, 1, Z);
var sy = new Mat(2, 2);
sy.Set(0, 0, Z); sy.Set(0, 1, new Cx(Zero, NegOne)); sy.Set(1, 0, CI); sy.Set(1, 1, Z);
var sz = new Mat(2, 2);
sz.Set(0, 0, U); sz.Set(0, 1, Z); sz.Set(1, 0, Z); sz.Set(1, 1, MINUSONE);
DIRS3.Add(sx);
DIRS3.Add(sy);
DIRS3.Add(sz);
}
// Fabriques de rationnels a partir d'entiers (la source ecrit F(n) / F(n, d)).
public static Rat R(long n) => new Rat(new BigInteger(n));
public static Rat R2(long num, long den) => new Rat(new BigInteger(num), new BigInteger(den));
public static Cx cadd(Cx a, Cx b) => a + b;
public static Cx csub(Cx a, Cx b) => a - b;
public static Cx cmul(Cx a, Cx b) => a * b;
public static Cx cscale(Rat q, Cx a) => new Cx(q * a.Re, q * a.Im);
public static Cx cconj(Cx a) => new Cx(a.Re, -a.Im);
public static string cstr(Cx a)
{
if (a.Re.IsZero && a.Im.IsZero) return "0";
var parts = new List<string>();
if (!a.Re.IsZero) parts.Add(a.Re.ToString());
if (!a.Im.IsZero)
{
string sign = a.Im.Sign < 0 ? "-" : (parts.Count > 0 ? "+" : "");
Rat mag = Rat.Abs(a.Im);
parts.Add(sign + (mag.IsOne ? "" : mag.ToString()) + "i");
}
return string.Concat(parts);
}
public static Mat meye(int n)
{
var M = new Mat(n, n);
for (int i = 0; i < n; i++) M.Set(i, i, U);
return M;
}
public static Mat mzeros(int n) => new Mat(n, n);
public static Mat madd(Mat A, Mat B)
{
var M = new Mat(A.Rows, A.Cols);
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
M.Set(i, j, cadd(A.Get(i, j), B.Get(i, j)));
return M;
}
public static Mat msub(Mat A, Mat B)
{
var M = new Mat(A.Rows, A.Cols);
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
M.Set(i, j, csub(A.Get(i, j), B.Get(i, j)));
return M;
}
public static Mat mmul(Mat A, Mat B)
{
int n = A.Rows, m = B.Cols, K = B.Rows;
var outp = new Mat(n, m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
Cx s = Z;
for (int t = 0; t < K; t++) s = cadd(s, cmul(A.Get(i, t), B.Get(t, j)));
outp.Set(i, j, s);
}
return outp;
}
public static Mat mscale(Rat q, Mat A)
{
var M = new Mat(A.Rows, A.Cols);
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
M.Set(i, j, cscale(q, A.Get(i, j)));
return M;
}
// Echelonnement par un scalaire COMPLEXE (i * sigma_k) : multiplie chaque element par c.
public static Mat mcxscale(Cx c, Mat A)
{
var M = new Mat(A.Rows, A.Cols);
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
M.Set(i, j, A.Get(i, j) * c);
return M;
}
public static Mat mT(Mat A)
{
var M = new Mat(A.Cols, A.Rows);
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
M.Set(i, j, A.Get(j, i));
return M;
}
public static Mat mdag(Mat A)
{
var M = new Mat(A.Cols, A.Rows);
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
M.Set(i, j, cconj(A.Get(j, i)));
return M;
}
public static bool meq(Mat A, Mat B) => A == B;
// Egalite de vecteurs (List<Cx>) : meme longueur, elements egaux (les images de boosts).
public static bool vecq(List<Cx> a, List<Cx> b) => a.Count == b.Count && a.Zip(b).All(p => p.First == p.Second);
public static bool mnul(Mat A)
{
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
if (!A.Get(i, j).IsZero) return false;
return true;
}
public static Cx tr(Mat A)
{
Cx s = Z;
for (int i = 0; i < A.Rows; i++) s = cadd(s, A.Get(i, i));
return s;
}
public static Mat kron(Mat A, Mat B)
{
int n = A.Rows, m = A.Cols, p = B.Rows, q = B.Cols;
var M = new Mat(n * p, m * q);
for (int i = 0; i < n * p; i++)
for (int j = 0; j < m * q; j++)
M.Set(i, j, cmul(A.Get(i / p, j / q), B.Get(i % p, j % q)));
return M;
}
public static string mstr(Mat A)
{
var rows = new List<string>();
for (int i = 0; i < A.Rows; i++)
{
var cells = new List<string>();
for (int j = 0; j < A.Cols; j++) cells.Add(cstr(A.Get(i, j)));
rows.Add(string.Join(", ", cells));
}
return "[" + string.Join(" ; ", rows) + "]";
}
// --- organ Chaines-13 : commutateur, produit par vecteur, aplatissement reel,
// rang / solve exact, fins ouvertes, bloc, lettres i*sigma, jointure. ---
public static Mat mcomm(Mat A, Mat B) => msub(mmul(A, B), mmul(B, A));
public static List<Cx> mv(Mat A, List<Cx> x)
{
var outp = new List<Cx>();
for (int i = 0; i < A.Rows; i++)
{
Cx s = Z;
for (int j = 0; j < x.Count; j++) s = cadd(s, cmul(A.Get(i, j), x[j]));
outp.Add(s);
}
return outp;
}
// Aplatissement d'une matrice en vecteur reel : chaque case (re, im) donne re puis im.
public static List<Rat> areel(Mat A)
{
var outp = new List<Rat>();
for (int i = 0; i < A.Rows; i++)
for (int j = 0; j < A.Cols; j++)
{
outp.Add(A.Get(i, j).Re);
outp.Add(A.Get(i, j).Im);
}
return outp;
}
// Rang reel par elimination de Gauss (les rationnels sont exacts).
public static int rang(List<List<Rat>> vs)
{
var M = vs.Select(row => new List<Rat>(row)).ToList();
int n = M.Count, m = M[0].Count;
int r = 0;
for (int c = 0; c < m; c++)
{
int? piv = null;
for (int i = r; i < n; i++) if (!M[i][c].IsZero) { piv = i; break; }
if (piv == null) continue;
var tmp = M[r]; M[r] = M[piv.Value]; M[piv.Value] = tmp;
for (int i = 0; i < n; i++)
if (i != r && !M[i][c].IsZero)
{
Rat q = M[i][c] / M[r][c];
for (int j = 0; j < m; j++) M[i][j] = M[i][j] - q * M[r][j];
}
r++;
}
return r;
}
// Coordonnees exactes de vr dans la base base_r ; null si vr n'est pas dans l'espace.
public static List<Rat> coeffs_dans(List<Rat> vr, List<List<Rat>> baseR)
{
int nb = baseR.Count;
int nr = vr.Count;
var M = new List<List<Rat>>();
for (int r = 0; r < nr; r++)
{
var row = new List<Rat>();
for (int b = 0; b < nb; b++) row.Add(baseR[b][r]);
row.Add(vr[r]);
M.Add(row);
}
int nc = nb + 1;
int rr = 0;
for (int c = 0; c < nb; c++)
{
int? piv = null;
for (int i = rr; i < nr; i++) if (!M[i][c].IsZero) { piv = i; break; }
if (piv == null) continue;
var tmp = M[rr]; M[rr] = M[piv.Value]; M[piv.Value] = tmp;
for (int i = 0; i < nr; i++)
if (i != rr && !M[i][c].IsZero)
{
Rat q = M[i][c] / M[rr][c];
for (int j = 0; j < nc; j++) M[i][j] = M[i][j] - q * M[rr][j];
}
rr++;
}
for (int i = rr; i < nr; i++)
if (!M[i][nb].IsZero) return null;
var x = new List<Rat>();
for (int b = 0; b < nb; b++) x.Add(Zero);
for (int i = rr - 1; i >= 0; i--)
{
int cidx = -1;
for (int j = 0; j < nb; j++) if (!M[i][j].IsZero) { cidx = j; break; }
Rat s = M[i][nb];
for (int j = cidx + 1; j < nb; j++) s = s - M[i][j] * x[j];
x[cidx] = s / M[i][cidx];
}
return x;
}
// Bloc 4x4 : A en haut a gauche (2x2), B en bas a droite (2x2), zailleurs.
public static Mat bloc(Mat A, Mat B)
{
var M = new Mat(4, 4);
for (int i = 0; i < 2; i++)
{
M.Set(i, 0, A.Get(i, 0)); M.Set(i, 1, A.Get(i, 1));
M.Set(i, 2, Z); M.Set(i, 3, Z);
}
for (int i = 0; i < 2; i++)
{
M.Set(i + 2, 0, Z); M.Set(i + 2, 1, Z);
M.Set(i + 2, 2, B.Get(i, 0)); M.Set(i + 2, 3, B.Get(i, 1));
}
return M;
}
// i * sigma_k : la lettre antiheroiticienne associee a la direction k.
public static Mat isig(int k) => mcxscale(CI, DIRS3[k]);
// sigma_k (la lettre de Pauli reelle de la direction k).
public static Mat sig(int k) => DIRS3[k];
// Fins ouvertes d'une chaine : (n, k, l, S, labels) comme la source.
public static (int n, int k, int l, int S, List<int> labels) fins_ouvertes(List<(string t, int lab)> copies)
{
int n = copies.Count;
int k = copies.Count(c => c.t == "ket");
int l = n - k;
int S = k - l;
var labels = copies.Select(c => c.lab).Distinct().OrderBy(x => x).ToList();
return (n, k, l, S, labels);
}
// Lecture (low, lnz, max) des labels d'une chaine comme la source.
public static (int low, int lnz, int mx) lectures(List<int> labels)
{
var nz = labels.Where(x => x != 0).ToList();
return (labels[0], nz.Count > 0 ? nz[0] : 0, labels[labels.Count - 1]);
}
// Jointure X1 X2 sur le noyau M : X1 M + M^T X2.
public static Mat jointure(Mat X1, Mat X2, Mat M) => madd(mmul(X1, M), mmul(M, mT(X2)));
}
// Etat que les cellules ulterieures relevent (un scope .NET ne partage pas les
// variables locales d'une cellule a la suivante) : chaque section de calcul
// ecritle sa part ici, la cellule finale la lit.
public static class C13
{
// eq. 86 / 87 : corps de l'electron (3,0,3), S = -3, labels {1,3}.
public static int n_e, k_e, l_e, S_e, low, lnz, mx;
public static List<int> lab_e;
// corps (2,3) et nomenclature up.
public static int low_r, lnz_r, max_r, nom_up;
public static (int, int, int) regle;
// signe d'echange + helicite.
public static bool ok_echange;
public static int libres;
// eq. 88 : la septale.
public static List<bool> anti;
public static List<bool> sans_trace;
public static int rang7, fermes;
// application de Lie (eq. 89).
public static int preserve;
// quaternion / stabilisateur.
public static int ferme, rang_D;
public static bool images_nulles, ok_boosts;
// so(3,1) et compactification.
public static int ok_l;
public static List<Rat> co_kk;
public static int rang_compact;
public static List<Mat> J, KL, KC;
// boost euclidien / lorentzien.
public static Mat Grot, Ghyp, Pe, Plc, nul2;
// registre (2,1)+(1,2).
public static bool ok_sq, ok_ab, ok_aa;
public static List<Mat> alphas;
public static Mat masse;
public static int dim_comm;
}
// Corps de l'electron : trois bres bra sur le niveau 1, 1 et 3 (lignes 130-141
// de la source). Les eq. 86 et 87 y sont mesurees sur l'instance meme.
var electron = new List<(string t, int lab)> { ("bra", 1), ("bra", 1), ("bra", 3) };
var fe86 = Ch.fins_ouvertes(electron);
C13.n_e = fe86.n; C13.k_e = fe86.k; C13.l_e = fe86.l; C13.S_e = fe86.S; C13.lab_e = fe86.labels;
Console.WriteLine("(n, k, l) = (" + C13.n_e + ", " + C13.k_e + ", " + C13.l_e + ") S = k - l = " + C13.S_e + " {labels} = [" + string.Join(", ", C13.lab_e) + "]");
Console.WriteLine("eq. 86 attendue : (3, 0, 3), S = -3, {1, 3} -> "
+ (C13.n_e == 3 && C13.k_e == 0 && C13.l_e == 3 && C13.S_e == -3 && C13.lab_e.SequenceEqual(new[] { 1, 3 })));
var lr87 = Ch.lectures(C13.lab_e);
C13.low = lr87.low; C13.lnz = lr87.lnz; C13.mx = lr87.mx;
Console.WriteLine("eq. 87 : 2s_low = " + C13.low + ", 2s_lnz = " + C13.lnz + ", 2s_max = " + C13.mx + " nom (electron) : 1");
Console.WriteLine("egaux au nom aux lectures 1 et 2 : " + (C13.low == 1 && C13.lnz == 1) + " ; parting a la lecture 3 : " + (C13.mx != 1));