using System;

class Program
{
    static void Main()
    {
        // 测量的红色、绿色和蓝色分量的色度坐标和亮度
        double[] red = { 0.6961, 0.3014, 4.144e+01 };
        double[] green = { 0.1615, 0.7275, 1.505e+02 };
        double[] blue = { 0.1451, 0.0379, 1.806e+01 };

        // 计算原始 XYZ 矩阵
        double[,] XYZMatrix = new double[3, 3] {
            { red[0] * red[2] / red[1], green[0] * green[2] / green[1], blue[0] * blue[2] / blue[1] },
            { red[2], green[2], blue[2] },
            { (1 - red[0] - red[1]) * red[2] / red[1], (1 - green[0] - green[1]) * green[2] / green[1], (1 - blue[0] - blue[1]) * blue[2] / blue[1] }
        };

        // 目标 RGB 矩阵
        int[,] targetRGBMatrix = new int[3, 3] {
            { 255, 0, 0 },
            { 0, 255, 0 },
            { 0, 0, 255 }
        };

        // 计算原始 RGB 矩阵到目标 RGB 矩阵的变换矩阵
        double[,] transformMatrix = MultiplyMatrices(InverseMatrix(XYZMatrix), targetRGBMatrix);

        // 使用变换矩阵将原始 RGB 矩阵转换为新矩阵
        int[,] newRGBMatrix = MultiplyMatrices(transformMatrix, targetRGBMatrix);

        // 输出新矩阵
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write(newRGBMatrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }

    // 矩阵乘法函数
    static int[,] MultiplyMatrices(double[,] a, int[,] b)
    {
        int rowsA = a.GetLength(0);
        int colsA = a.GetLength(1);
        int rowsB = b.GetLength(0);
        int colsB = b.GetLength(1);
        if (colsA != rowsB)
        {
            throw new ArgumentException("矩阵维度不匹配，无法相乘。");
        }
        int[,] result = new int[rowsA, colsB];
        for (int i = 0; i < rowsA; i++)
        {
            for (int j = 0; j < colsB; j++)
            {
                double sum = 0;
                for (int k = 0; k < colsA; k++)
                {
                    sum += a[i, k] * b[k, j];
                }
                result[i, j] = (int)Math.Round(sum);
            }
        }
        return result;
    }

    // 计算矩阵的逆矩阵    // 计算矩阵的逆矩阵
    static double[,] InverseMatrix(double[,] matrix)
    {
        int n = matrix.GetLength(0);
        double[,] result = new double[n, n];
        double[,] temp = new double[n, 2 * n];

        // 构造增广矩阵
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                temp[i, j] = matrix[i, j];
            }
            temp[i, n + i] = 1;
        }

        // 高斯消元法
        for (int i = 0; i < n; i++)
        {
            // 寻找主元
            double maxVal = Math.Abs(temp[i, i]);
            int maxRow = i;
            for (int k = i + 1; k < n; k++)
            {
                if (Math.Abs(temp[k, i]) > maxVal)
                {
                    maxVal = Math.Abs(temp[k, i]);
                    maxRow = k;
                }
            }

            // 交换行
            for (int k = i; k < 2 * n; k++)
            {
                double tmp = temp[maxRow, k];
                temp[maxRow, k] = temp[i, k];
                temp[i, k] = tmp;
            }

            // 使主元为 1
            double pivot = temp[i, i];
            if (Math.Abs(pivot) < 1e-10)
            {
                throw new ArgumentException("矩阵不可逆。");
            }
            for (int k = i; k < 2 * n; k++)
            {
                temp[i, k] /= pivot;
            }

            // 消元
            for (int k = 0; k < n; k++)
            {
                if (k != i)
                {
                    double coef = temp[k, i];
                    for (int j = i; j < 2 * n; j++)
                    {
                        temp[k, j] -= coef * temp[i, j];
                    }
                }
            }
        }

        // 提取逆矩阵
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                result[i, j] = temp[i, n + j];
            }
        }

        return result;
    }
}