You are viewing a single comment's thread. Return to all comments →
c# version
static void Main(String[] args) { var xs = System.Console.ReadLine().Trim().Split(' '); int m = int.Parse(xs[0]) + 1; int n = int.Parse(xs[1]); var ws = new double[n,m]; /* row,col */ for(int i = 0; i < n; i++) { var zs = System.Console.ReadLine().Trim().Split(' '); for (int k=0;k<m;k++) { ws[i,k] = double.Parse(zs[k]); } } var bs = GetBs(ws, n, m); var q = int.Parse(System.Console.ReadLine().Trim()); for(int i = 0; i < q; i++) { var zs = System.Console.ReadLine().Trim().Split(' '); var qs = new double[1,m]; qs[0,0] = 1; for (int k=0;k<(m-1);k++) { qs[0,k+1] = double.Parse(zs[k]); } var qsbs = Product(qs, bs); System.Console.WriteLine(qsbs[0,0].ToString("f2")); } } static double[,] GetBs(double[,] ws, int row, int col) { var xs = new double[row, col]; var ys = new double[row, 1]; int coly = col - 1; for(int i = 0; i < row; i++) { xs[i, 0] = 1; ys[i, 0] = ws[i, coly]; for(int j = 0; j < coly; j++) { xs[i, j + 1] = ws[i, j]; } } var txs = Transpose(xs); var txsxs = Product(txs, xs); var txsxsInv = Inverse(txsxs); var txsxsInvtxs = Product(txsxsInv, txs); var txsxsInvtxsys = Product(txsxsInvtxs, ys); return txsxsInvtxsys; } /* optimization cache */ static Dictionary<string, double> DeterminantDic = new Dictionary<string, double>(); static double Determinant(double[,] xs) { var pkey = MatrixKey(xs); double mx; if (DeterminantDic.TryGetValue(pkey, out mx)) { return mx; } double det = 0; int r = xs.GetUpperBound(0) + 1; if (r == 2) { det = xs[0,0] * xs[1,1] - xs[0,1] * xs[1,0]; } else { int c = xs.GetUpperBound(1) + 1; for(int i=0;i<c;i++) { det += ((double)(-1.0 * (i % 2 == 0 ? -1.0 : 1.0)) * xs[0, i] * Determinant(SubMatrix(xs, 0, i))); } } DeterminantDic.Add(pkey, det); return det; } static double[,] SubMatrix(double[,] xs, int row, int col) { int r = xs.GetUpperBound(0); int c = xs.GetUpperBound(1); var ds = new double[r, c]; int i2 = 0; for(int i = 0; i <= r; i++) { if (i != row) { int j2 = 0; for(int j = 0; j <= c; j++) { if (j != col) { ds[i2, j2] = xs[i,j]; j2++; } } i2++; } } return ds; } static double[,] Inverse(double[,] xs) { var k = 1.0/Determinant(xs); int r = xs.GetUpperBound(0) + 1; int c = xs.GetUpperBound(1) + 1; var ds = new double[r, c]; for (int i=0; i < r; i++) { for(int j=0; j < c; j++) { ds[i, j] = (-1.0 * (((j + i) % 2 == 0) ? -1.0 : 1.0)) * Determinant(SubMatrix(xs, i, j)) * k; } } return ds; } static double[,] Product(double[,] xs, double[,] ys) { int row = xs.GetUpperBound(0) + 1; int colx = xs.GetUpperBound(1) + 1; int col = ys.GetUpperBound(1) + 1; var ds = new double[row, col]; for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { double r = 0; for(int k = 0; k < colx; k++) { r += xs[i,k] * ys[k,j]; } ds[i,j] = r; } } return ds; } static double[,] Transpose(double[,] xs) { int row = xs.GetUpperBound(0) + 1; int col = xs.GetUpperBound(1) + 1; var ds = new double[col, row]; for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { ds[j, i] = xs[i, j]; } } return ds; } static string MatrixKey(double[,] xs) { string k = string.Empty; int row = xs.GetUpperBound(0) + 1; int col = xs.GetUpperBound(1) + 1; for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { k = string.Concat(k, k.Length == 0 ? string.Empty : "#", xs[i, j].ToString("f5")); } } return k; }
Seems like cookies are disabled on this browser, please enable them to open this website
Day 9: Multiple Linear Regression
You are viewing a single comment's thread. Return to all comments →
c# version