跳到正文

算法·理论

代码与高等代数:懒人的奇怪实践

12wty29约 13 分钟
原文
文章信息与版本

文章信息

收录
2026年1月2日
更新
2026年1月2日
篇幅
约 13 分钟

版本

  1. v1当前

作为大一计科的牛马,愚蠢的线代教材不提供作业的答案,面对高考后脑子算力的对数下滑,蒟蒻开始了流水线生产线代答案的工程

我称之为 “The Most Strict Father Of Linear Algebra” 一期代码:

C++
#include <bits/stdc++.h>
#define M 100001
#define it long long
#define For(i, a, b) for (int i = a; i <= b; i++)
using namespace std;

inline int read()
{
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9')
  {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
  {
    f = (f << 1) + (f << 3) + (c - '0');
    c = getchar();
  }
  return f * fu;
}
inline int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
/*
  严谨分数结构体
  支持:
  自动化简
  重载分数加法,分数乘法
  倒数
  LaTex格式打印
*/
struct frac
{
  int up, down;
  frac() { up = 0, down = 1; }
  frac(int x, int y) { up = x, down = y; }
  void reduce()
  {
    if (down < 0)
      up *= -1, down *= -1; // 保证分母为正
    int tmp = gcd(up, down);
    up /= tmp, down /= tmp;
  }
  frac operator+(const frac &t) const
  {
    int nu = up * t.down + down * t.up;
    int nd = down * t.down;
    int tmp = gcd(nu, nd);
    nu = nu / tmp, nd = nd / tmp;
    return frac(nu, nd);
  }
  frac operator*(const frac &t) const
  {
    int nu = up * t.up;
    int nd = down * t.down;
    int tmp = gcd(nu, nd);
    return frac(nu / tmp, nd / tmp);
  }
  void translate()
  {
    int t = up;
    up = down, down = t;
  }
  void output(int op) // 检验是否已经打过labtx标记
  {
    if (!op)
      printf("$");
    if (down != 1)
      printf("\\frac{%d}{%d}", up, down);
    else
      printf("%d", up);
    if (!op)
      printf("$");
  }
} bridge[101][101]; // 始终用于对Matrix赋值的参量
// 应当保证,每次调用Matrix赋值时,bridge中存有恰当的矩阵

inline void clear()
{
  for (int i = 1; i <= 101; i++)
    for (int j = 1; j <= 101; j++)
      bridge[i][j] = frac(0, 1);
}
/*
    矩阵加法,矩阵乘法,
    交换两行
    行倍加
    数乘运算(函数式or重载两次乘号)
    自动化简为行阶梯型
    自动化简为简化行阶梯型
    求逆元
  */
struct Matrix
{
  int lin, col;
  frac a[101][101];
  frac t[101][101];
  Matrix()
  {
    lin = 0, col = 0;
    for (int i = 1; i <= 100; i++)
      for (int j = 1; j <= 100; j++)
        a[i][j] = frac(0, 1), t[i][j] = frac(0, 1);
  }
  Matrix(int nlin, int ncol)
  {
    lin = nlin, col = ncol;
    for (int i = 1; i <= nlin; i++)
      for (int j = 1; j <= ncol; j++)
        a[i][j] = bridge[i][j];
  }

  Matrix operator+(const Matrix &nw) const
  {
    Matrix res;
    res.lin = lin, res.col = col;
    if (lin != nw.lin || col != nw.col)
    {
      printf("ERROR!\n");
    }
    for (int i = 1; i <= lin; i++)
      for (int j = 1; j <= col; j++)
        res.a[i][j] = a[i][j] + nw.a[i][j];
    return res;
  }
  Matrix operator*(const Matrix &nw) const
  {
    Matrix res;
    res.lin = lin, res.col = nw.col;
    if (col != nw.lin)
    {
      printf("ERROR! UNDEFINED OPERATION!\n");
      return res;
    }
    for (int i = 1; i <= lin; i++)
      for (int j = 1; j <= nw.col; j++)
      {
        for (int k = 1; k <= col; k++)
          res.a[i][j] = res.a[i][j] + a[i][k] * a[k][j];
      }
    return res;
  }
  void Translate() // 矩阵转置
  {
    for (int i = 1; i <= col; i++)
      for (int j = 1; j <= lin; j++)
        t[i][j] = a[j][i];
    For(i, 1, lin)
        For(j, 1, col)
            a[i][j] = t[i][j];
  }
  void Rowexchange(int x, int y) // 行交换
  {
    frac tmp;
    For(i, 1, col)
    {
      tmp = a[x][i];
      a[x][i] = a[y][i];
      a[y][i] = tmp;
    }
  }
  void Replacement(int x, int y, frac r) // 第x行加上第y行的r倍
  {
    For(i, 1, col)
    {
      a[x][i] = a[x][i] + a[y][i] * r;
      a[x][i].reduce();
    }
  }
  void Rowtimes(int x, frac r)
  {
    for (int i = 1; i <= col; i++)
      a[x][i] = a[x][i] * r;
  }
  void output()
  {
    printf("$$\\begin{bmatrix}");
    For(i, 1, lin)
    {
      For(j, 1, col)
      {
        if (j != 1)
          printf("&");
        a[i][j].output(1);
      }
      printf("\\\\");
    }
    printf("\\end{bmatrix}$$");
    printf("\n\n");
  }

  void Rowreduce(int op)
  {
    // 多模复合函数,op==1:输出过程和行化简结果 op==2求解方程组  op==3求逆元
    output();
    int num = col, tot;
    if (op == 2)
      num--; // 增广列不能化简
    if (op == 3)
      num = lin; // 逆元能到此步骤说明已经确认n*n,不用二次判定
    // 考察第i列是否为主元列
    int pr = 1, pc = 1; // 主元行和主元列位置
    while (pr <= lin && pc <= num)
    {
      int id = pr;
      while (a[id][pc].up == 0 && id <= lin)
        id++;
      if (id > lin)
      {
        pc++;
        continue;
      }
      if (id != pr)
        Rowexchange(id, pr);
      frac p = a[pr][pc];
      p.translate();
      p.reduce();
      // p.output(0);
      // printf("\n");
      Rowtimes(pr, p);
      for (int i = pr + 1; i <= lin; i++)
        if (a[i][pc].up != 0)
        {
          frac mi = a[i][pc];
          mi.up *= -1;
          Replacement(i, pr, mi);
        }
      pr++, pc++;
      output(); // 输出每一步后得到的矩阵
    } // 到这一步化简为行阶梯型,可以完成简化,肉眼求秩,求零空间等
    // 好消息是这里主元已经完成归一
    // 下面是化简为最简行阶梯型
    if (op == 1)
    {
      return;
    }
    pr = lin, pc = -1; // 找主元
    while (pr >= 1)
    {
      for (int j = 1; j <= num; j++)
        if (a[pr][j].up != 0)
        {
          pc = j;
          break;
        }
      if (pc > 0)
        break;
      pr--;
    }
    while (pr >= 1 && pc >= 1)
    {
      for (int i = pr - 1; i >= 1; i--)
      {
        frac tmp = a[i][pc];
        tmp.up *= -1;
        tmp.reduce();
        Replacement(i, pr, tmp); // 清空主元上方
      }
      pr--;
      if (pr == 0)
        break;
      for (int j = 1; j < pc; j++)
        if (a[pr][j].up != 0)
        {
          pc = j;
          break;
        } // 跳到下一个主元
    }
    output();
  }
};

inline Matrix Inverse(Matrix target)
{
  if (target.col != target.lin)
  {
    printf("ERROR:UNINVERSABLE NOT SQUARE\n");
    return Matrix(0, 0);
  }
  Matrix inv = target;
  inv.col *= 2;
  For(i, 1, target.lin)
      For(j, target.col + 1, target.col * 2)
  {
    if (i + target.col != j)
      inv.a[i][j] = frac(0, 1);
    else
      inv.a[i][j] = frac(1, 1);
  }
  inv.Rowreduce(3);
  return inv;
}

int n, m, ope;

int main()
{
  freopen("matrix.in", "r", stdin);
  freopen("try.md", "w", stdout);
  n = read(), m = read();
  ope = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
    {
      int x = read();
      bridge[i][j].up = x;
      bridge[i][j].down = 1;
    }
  Matrix sol;
  sol = Matrix(n, m);
  if (ope == 1)
  {
    sol.Rowreduce(1);
  }
  if (ope == 2)
  {
    sol.Rowreduce(2);
    sol.output();
  }
  if (ope == 3)
  {
    Inverse(sol).output();
  }
  return 0;
}
原文