public VEC(VEC v1) { dim = v1.len(); val = new double[dim]; for (int i = 0; i < dim; i++) { val[i] = v1[i]; } }
public static double Lp_norm(VEC v, double p) { double s = 0; if (p == -1) { for (int i = 0; i < v.len(); i++) { if (s < Math.Abs(v[i])) { s = Math.Abs(v[i]); } } } else { for (int i = 0; i < v.len(); i++) { s += Math.Pow(v[i], p); } s = Math.Pow(s, 1.0 / p); } return(s); }
public static VEC Normalize(VEC v) { int n = v.len(); VEC X = new VEC(n); double abs = v.abs(); if (abs < 1E-12) { return(X); } else { X = v / abs; return(X); } }
public static VEC fwdSubs(MAT m1, VEC b) { int len = b.len(); VEC v = new VEC(len); for (int i = 0; i < len; i++) { double s = 0; for (int j = 0; j < i; j++) { s += m1[i][j] * v[j]; } v[i] = (b[i] - s) / m1[i][i]; } return(v); }
public static VEC bckSubs(MAT m1, VEC b) { int len = b.len(); VEC v = new VEC(len); for (int i = 0; i < len; i++) { v[i] = b[i]; } for (int i = len - 1; i >= 0; i--) { v[i] = v[i] / m1[i][i]; for (int j = i - 1; j >= 0; j--) { v[j] = v[j] - (m1[j][i] * v[i]); } } return(v); }